Skip to content

Commit

Permalink
Merge pull request #39 from powerdesigns/native-CANbus
Browse files Browse the repository at this point in the history
Add native CAN bus support on Linux
  • Loading branch information
vedderb committed Jul 19, 2019
2 parents 91437c7 + 076ee6c commit 284bc91
Show file tree
Hide file tree
Showing 8 changed files with 657 additions and 2 deletions.
1 change: 1 addition & 0 deletions commands.cpp
Expand Up @@ -1061,6 +1061,7 @@ void Commands::pingCan()
mTimeoutPingCan = 500;

VByteArray vb;

vb.vbAppendInt8(COMM_PING_CAN);
emitData(vb);
}
Expand Down
31 changes: 31 additions & 0 deletions datatypes.h
Expand Up @@ -414,6 +414,37 @@ typedef enum {
COMM_BM_DISCONNECT
} COMM_PACKET_ID;

typedef enum {
CAN_PACKET_SET_DUTY = 0,
CAN_PACKET_SET_CURRENT,
CAN_PACKET_SET_CURRENT_BRAKE,
CAN_PACKET_SET_RPM,
CAN_PACKET_SET_POS,
CAN_PACKET_FILL_RX_BUFFER,
CAN_PACKET_FILL_RX_BUFFER_LONG,
CAN_PACKET_PROCESS_RX_BUFFER,
CAN_PACKET_PROCESS_SHORT_BUFFER,
CAN_PACKET_STATUS,
CAN_PACKET_SET_CURRENT_REL,
CAN_PACKET_SET_CURRENT_BRAKE_REL,
CAN_PACKET_SET_CURRENT_HANDBRAKE,
CAN_PACKET_SET_CURRENT_HANDBRAKE_REL,
CAN_PACKET_STATUS_2,
CAN_PACKET_STATUS_3,
CAN_PACKET_STATUS_4,
CAN_PACKET_PING,
CAN_PACKET_PONG,
CAN_PACKET_DETECT_APPLY_ALL_FOC,
CAN_PACKET_DETECT_APPLY_ALL_FOC_RES,
CAN_PACKET_CONF_CURRENT_LIMITS,
CAN_PACKET_CONF_STORE_CURRENT_LIMITS,
CAN_PACKET_CONF_CURRENT_LIMITS_IN,
CAN_PACKET_CONF_STORE_CURRENT_LIMITS_IN,
CAN_PACKET_CONF_FOC_ERPMS,
CAN_PACKET_CONF_STORE_FOC_ERPMS,
CAN_PACKET_STATUS_5
} CAN_PACKET_ID;

typedef struct {
int js_x;
int js_y;
Expand Down
57 changes: 57 additions & 0 deletions pages/pageconnection.cpp
Expand Up @@ -83,8 +83,23 @@ void PageConnection::setVesc(VescInterface *vesc)
ui->serialBaudBox->setValue(mVesc->getLastSerialBaud());
#endif

#ifdef HAS_CANBUS
ui->CANbusBitrateBox->setValue(mVesc->getLastCANbusBitrate());

ui->CANbusInterfaceBox->clear();
QList<QString> interfaces = mVesc->listCANbusInterfaces();

for(int i = 0; i<interfaces.size(); i++)
ui->CANbusInterfaceBox->addItem(interfaces.at(i), interfaces.at(i));
ui->CANbusInterfaceBox->setCurrentIndex(0);
#endif

connect(mVesc->commands(), SIGNAL(pingCanRx(QVector<int>,bool)),
this, SLOT(pingCanRx(QVector<int>,bool)));
connect(mVesc, SIGNAL(CANbusNewNode(int)),
this, SLOT(CANbusNewNode(int)));
connect(mVesc, SIGNAL(CANbusInterfaceListUpdated()),
this, SLOT(CANbusInterfaceListUpdated()));
connect(mVesc, SIGNAL(pairingListUpdated()),
this, SLOT(pairingListUpdated()));

Expand Down Expand Up @@ -173,6 +188,21 @@ void PageConnection::pingCanRx(QVector<int> devs, bool isTimeout)
}
}

void PageConnection::CANbusNewNode(int node)
{
ui->CANbusTargetIdBox->addItem(QString::number(node), QString::number(node));
}

void PageConnection::CANbusInterfaceListUpdated()
{
ui->CANbusInterfaceBox->clear();
QList<QString> interfaces = mVesc->listCANbusInterfaces();

for(int i = 0; i<interfaces.size(); i++)
ui->CANbusInterfaceBox->addItem(interfaces.at(i), interfaces.at(i));
ui->CANbusInterfaceBox->setCurrentIndex(0);
}

void PageConnection::pairingListUpdated()
{
ui->pairedListWidget->clear();
Expand Down Expand Up @@ -217,6 +247,33 @@ void PageConnection::on_serialConnectButton_clicked()
}
}

void PageConnection::on_CANbusScanButton_clicked()
{
if (mVesc) {
mVesc->connectCANbus("socketcan", ui->CANbusInterfaceBox->currentData().toString(),
ui->CANbusBitrateBox->value());

ui->CANbusTargetIdBox->clear();
mVesc->scanCANbus();
}
}

void PageConnection::on_CANbusDisconnectButton_clicked()
{
if (mVesc) {
mVesc->disconnectPort();
}
}

void PageConnection::on_CANbusConnectButton_clicked()
{
if (mVesc) {
mVesc->setCANbusReceiverID(ui->CANbusTargetIdBox->currentData().toInt());
mVesc->connectCANbus("socketcan", ui->CANbusInterfaceBox->currentData().toString(),
ui->CANbusBitrateBox->value());
}
}

void PageConnection::on_tcpDisconnectButton_clicked()
{
if (mVesc) {
Expand Down
5 changes: 5 additions & 0 deletions pages/pageconnection.h
Expand Up @@ -43,11 +43,16 @@ private slots:
void timerSlot();
void bleScanDone(QVariantMap devs, bool done);
void pingCanRx(QVector<int> devs, bool isTimeout);
void CANbusNewNode(int node);
void CANbusInterfaceListUpdated();
void pairingListUpdated();

void on_serialRefreshButton_clicked();
void on_serialDisconnectButton_clicked();
void on_serialConnectButton_clicked();
void on_CANbusScanButton_clicked();
void on_CANbusDisconnectButton_clicked();
void on_CANbusConnectButton_clicked();
void on_tcpDisconnectButton_clicked();
void on_tcpConnectButton_clicked();
void on_helpButton_clicked();
Expand Down
153 changes: 151 additions & 2 deletions pages/pageconnection.ui
Expand Up @@ -14,7 +14,7 @@
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<item alignment="Qt::AlignVCenter">
<widget class="QTabWidget" name="tabWidget">
<property name="tabPosition">
<enum>QTabWidget::North</enum>
Expand All @@ -23,7 +23,7 @@
<enum>QTabWidget::Triangular</enum>
</property>
<property name="currentIndex">
<number>0</number>
<number>1</number>
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
Expand Down Expand Up @@ -148,6 +148,155 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_4">
<attribute name="title">
<string>CAN bus</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_7">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_7">
<item>
<widget class="QLabel" name="label_6">
<property name="text">
<string>Interface</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="CANbusInterfaceBox">
<property name="toolTip">
<string>CAN bus interfaces detected</string>
</property>
<property name="editable">
<bool>false</bool>
</property>
<property name="currentText">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_5">
<property name="text">
<string>VESC ID</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="CANbusTargetIdBox">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="CANbusBitrateBox">
<property name="enabled">
<bool>false</bool>
</property>
<property name="focusPolicy">
<enum>Qt::WheelFocus</enum>
</property>
<property name="toolTip">
<string>This configuration is not supported by the socketcan backend. However it is possible to set the rate when configuring the CAN network interface using the ip link command.</string>
</property>
<property name="suffix">
<string> kbit/s</string>
</property>
<property name="prefix">
<string>Bit rate: </string>
</property>
<property name="maximum">
<number>1000</number>
</property>
<property name="value">
<number>500</number>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="CANbusScanButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>Discover VESC nodes in the CAN bus</string>
</property>
<property name="text">
<string>Scan</string>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons/Refresh-96.png</normaloff>:/res/icons/Refresh-96.png</iconset>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="CANbusDisconnectButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>Disconnect</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons/Disconnected-96.png</normaloff>:/res/icons/Disconnected-96.png</iconset>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="CANbusConnectButton">
<property name="enabled">
<bool>true</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>Connect to a VESC node</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons/Connected-96.png</normaloff>:/res/icons/Connected-96.png</iconset>
</property>
</widget>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer_4">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>TCP</string>
Expand Down
7 changes: 7 additions & 0 deletions vesc_tool.pro
Expand Up @@ -28,6 +28,9 @@ CONFIG += c++11
# Bluetooth available
DEFINES += HAS_BLUETOOTH

# CAN bus available
DEFINES += HAS_CANBUS

# Debug build (e.g. F5 to reload QML files)
#DEFINES += DEBUG_BUILD

Expand Down Expand Up @@ -55,6 +58,10 @@ contains(DEFINES, HAS_SERIALPORT) {
QT += serialport
}

contains(DEFINES, HAS_CANBUS) {
QT += serialbus
}

contains(DEFINES, HAS_BLUETOOTH) {
QT += bluetooth
}
Expand Down

0 comments on commit 284bc91

Please sign in to comment.