Skip to content

Commit bc050d3

Browse files
committed
ODrive disconnect detection and command added
1 parent a571125 commit bc050d3

3 files changed

Lines changed: 30 additions & 14 deletions

File tree

Configurator

Firmware/FFBoard/UserExtensions/Inc/ODriveCAN.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ enum class ODriveEncoderFlags : uint32_t {ERROR_NONE = 0,ERROR_UNSTABLE_GAIN = 0
2929
enum class ODriveAxisError : uint32_t {AXIS_ERROR_NONE = 0x00000000,AXIS_ERROR_INVALID_STATE = 0x00000001, AXIS_ERROR_WATCHDOG_TIMER_EXPIRED = 0x00000800,AXIS_ERROR_MIN_ENDSTOP_PRESSED = 0x00001000, AXIS_ERROR_MAX_ENDSTOP_PRESSED = 0x00002000,AXIS_ERROR_ESTOP_REQUESTED = 0x00004000,AXIS_ERROR_HOMING_WITHOUT_ENDSTOP = 0x00020000,AXIS_ERROR_OVER_TEMP = 0x00040000,AXIS_ERROR_UNKNOWN_POSITION = 0x00080000};
3030

3131
enum class ODriveCAN_commands : uint32_t{
32-
canid,canspd,errors,state,maxtorque,vbus,anticogging
32+
canid,canspd,errors,state,maxtorque,vbus,anticogging,connected
3333
};
3434

3535
class ODriveCAN : public MotorDriver,public PersistentStorage, public Encoder, public CanHandler, public CommandHandler, cpp_freertos::Thread{
@@ -95,6 +95,7 @@ class ODriveCAN : public MotorDriver,public PersistentStorage, public Encoder, p
9595
float posOffset = 0;
9696
float lastVoltage = 0;
9797
uint32_t lastVoltageUpdate = 0;
98+
uint32_t lastCanMessage = 0;
9899

99100
int8_t nodeId = 0; // 6 bits can ID
100101
int8_t motorId = 0;
@@ -107,14 +108,13 @@ class ODriveCAN : public MotorDriver,public PersistentStorage, public Encoder, p
107108
volatile uint32_t odriveControllerFlags = 0;
108109

109110
float maxTorque = 1.0; // range how to scale the torque output
110-
//bool ready = false;
111111
volatile bool waitReady = true;
112112

113-
//volatile bool requestFirstRun = false;
114113
bool active = false;
115114

116115
int32_t filterId = 0;
117116
volatile ODriveLocalState state = ODriveLocalState::IDLE;
117+
bool connected = false;
118118

119119
uint8_t baudrate = CANSPEEDPRESET_500; // 250000, 500000, 1M
120120
};

Firmware/FFBoard/UserExtensions/Src/ODriveCAN.cpp

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,14 @@ ODriveCAN::~ODriveCAN() {
7171

7272
void ODriveCAN::registerCommands(){
7373
CommandHandler::registerCommands();
74-
registerCommand("canid", ODriveCAN_commands::canid, "CAN id of ODrive");
75-
registerCommand("canspd", ODriveCAN_commands::canspd, "CAN baudrate");
76-
registerCommand("errors", ODriveCAN_commands::errors, "ODrive error flags");
77-
registerCommand("state", ODriveCAN_commands::state, "ODrive state");
78-
registerCommand("maxtorque", ODriveCAN_commands::maxtorque, "Max torque to send for scaling");
79-
registerCommand("vbus", ODriveCAN_commands::vbus, "ODrive Vbus");
80-
registerCommand("anticogging", ODriveCAN_commands::anticogging, "Set 1 to start anticogging calibration");
74+
registerCommand("canid", ODriveCAN_commands::canid, "CAN id of ODrive",CMDFLAG_GET | CMDFLAG_SET);
75+
registerCommand("canspd", ODriveCAN_commands::canspd, "CAN baudrate",CMDFLAG_GET | CMDFLAG_SET);
76+
registerCommand("errors", ODriveCAN_commands::errors, "ODrive error flags",CMDFLAG_GET);
77+
registerCommand("state", ODriveCAN_commands::state, "ODrive state",CMDFLAG_GET);
78+
registerCommand("maxtorque", ODriveCAN_commands::maxtorque, "Max torque to send for scaling",CMDFLAG_GET | CMDFLAG_SET);
79+
registerCommand("vbus", ODriveCAN_commands::vbus, "ODrive Vbus",CMDFLAG_GET);
80+
registerCommand("anticogging", ODriveCAN_commands::anticogging, "Set 1 to start anticogging calibration",CMDFLAG_SET);
81+
registerCommand("connected", ODriveCAN_commands::connected, "ODrive connection state",CMDFLAG_GET);
8182
}
8283

8384
void ODriveCAN::restoreFlash(){
@@ -174,6 +175,16 @@ void ODriveCAN::Run(){
174175
if(HAL_GetTick() - lastVoltageUpdate > 1000){
175176
requestMsg(0x17); // Update voltage
176177
}
178+
179+
if(HAL_GetTick() - lastCanMessage > 2000){ // Timeout
180+
odriveCurrentState = ODriveState::AXIS_STATE_UNDEFINED;
181+
state = ODriveLocalState::IDLE;
182+
waitReady = true;
183+
connected = false;
184+
}else{
185+
connected = true;
186+
}
187+
177188
}
178189
}
179190

@@ -282,9 +293,7 @@ CommandStatus ODriveCAN::command(const ParsedCommand& cmd,std::vector<CommandRep
282293
break;
283294

284295
case ODriveCAN_commands::canid:
285-
if(cmd.type == CMDtype::get){
286-
return handleGetSet(cmd, replies, this->nodeId);
287-
}
296+
return handleGetSet(cmd, replies, this->nodeId);
288297
break;
289298
case ODriveCAN_commands::state:
290299
if(cmd.type == CMDtype::get){
@@ -321,6 +330,11 @@ CommandStatus ODriveCAN::command(const ParsedCommand& cmd,std::vector<CommandRep
321330
}
322331
break;
323332
}
333+
case ODriveCAN_commands::connected:
334+
if(cmd.type == CMDtype::get){
335+
replies.push_back(CommandReply(connected ? 1 : 0));
336+
}
337+
break;
324338

325339
default:
326340
return CommandStatus::NOT_FOUND;
@@ -340,6 +354,8 @@ void ODriveCAN::canRxPendCallback(CAN_HandleTypeDef *hcan,uint8_t* rxBuf,CAN_RxH
340354
uint64_t msg = *reinterpret_cast<uint64_t*>(rxBuf);
341355
uint8_t cmd = rxHeader->StdId & 0x1F;
342356

357+
lastCanMessage = HAL_GetTick();
358+
343359
switch(cmd){
344360
case 1:
345361
{

0 commit comments

Comments
 (0)