Skip to content

Commit 848bdb1

Browse files
authored
Merge pull request #14 from BrechtSerckx/requested-cyclic-messages
Check requested message before trying callbacks
2 parents 8479e07 + c88f8a4 commit 848bdb1

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/ODriveCAN.cpp

+9-4
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,17 @@ void ODriveCAN::onReceive(uint32_t id, uint8_t length, const uint8_t* data) {
145145
Serial.print(msg.data[byte_index--], HEX);
146146
Serial.println(F(""));
147147
#endif // DEBUG
148+
// Check that the message is meant for this node.
148149
if (node_id_ != (id >> ODriveCAN::kNodeIdShift))
149150
return;
151+
// If the message is requested, copy it in the request buffer and exit.
152+
if ((id & ODriveCAN::kCmdIdBits) == requested_msg_id_) {
153+
memcpy(buffer_, data, length);
154+
requested_msg_id_ = REQUEST_PENDING;
155+
return;
156+
};
157+
// Check if any of the registered callback handlers apply. Useful for cyclic
158+
// messages.
150159
switch (id & ODriveCAN::kCmdIdBits) {
151160
case Get_Encoder_Estimates_msg_t::cmd_id: {
152161
Get_Encoder_Estimates_msg_t estimates;
@@ -178,10 +187,6 @@ void ODriveCAN::onReceive(uint32_t id, uint8_t length, const uint8_t* data) {
178187
Serial.print(F("waiting for: 0x"));
179188
Serial.println(requested_msg_id_, HEX);
180189
#endif // DEBUG
181-
if ((id & ODriveCAN::kCmdIdBits) != requested_msg_id_)
182-
return;
183-
memcpy(buffer_, data, length);
184-
requested_msg_id_ = REQUEST_PENDING;
185190
}
186191
}
187192
}

src/ODriveCAN.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class ODriveCAN {
161161
bool getVersion(Get_Version_msg_t& msg, uint16_t timeout_ms = 10);
162162

163163
/**
164-
* @brief Requests encoder feedback data. May trigger onFeedback callback if it's registered
164+
* @brief Requests encoder feedback data.
165165
*
166166
* This function will block and wait for up to timeout_ms (default 10msec) for ODrive to reply
167167
*/

0 commit comments

Comments
 (0)