Fixed timeout handling

This commit is contained in:
Timothy Scott
2019-07-04 16:50:31 +02:00
committed by Julian Oes
parent f780191c5a
commit 71067a7e2d

View File

@@ -59,7 +59,8 @@
#include <math.h> #include <math.h>
// The RoboClaw has a serial communication timeout of 10ms. // The RoboClaw has a serial communication timeout of 10ms.
#define TIMEOUT_US 10000 // Add a little extra to account for timing inaccuracy
#define TIMEOUT_US 10500
// TODO: Make these all parameters // TODO: Make these all parameters
#define FAILED_TRANSACTION_RETRIES 1 #define FAILED_TRANSACTION_RETRIES 1
@@ -182,6 +183,7 @@ void RoboClaw::taskMain()
// If disarmed, I want to be certain that the stop command gets through. // If disarmed, I want to be certain that the stop command gets through.
while ((drive_ret = drive(0.0)) <= 0 || (turn_ret = turn(0.0)) <= 0) { while ((drive_ret = drive(0.0)) <= 0 || (turn_ret = turn(0.0)) <= 0) {
PX4_ERR("Error trying to stop: Drive: %d, Turn: %d", drive_ret, turn_ret); PX4_ERR("Error trying to stop: Drive: %d, Turn: %d", drive_ret, turn_ret);
px4_usleep(TIMEOUT_US);
} }
} else { } else {
@@ -469,9 +471,13 @@ int RoboClaw::_transaction(e_command cmd, uint8_t *wbuff, size_t wbytes,
// select(...) returns as soon as even 1 byte is available. read(...) returns immediately, no matter how many // select(...) returns as soon as even 1 byte is available. read(...) returns immediately, no matter how many
// bytes are available. I need to keep reading until I get the number of bytes I expect. // bytes are available. I need to keep reading until I get the number of bytes I expect.
while (bytes_read < rbytes) { while (bytes_read < rbytes) {
// select(...) may change this timeout struct (because it is not const). So I reset it every time.
_uart_timeout.tv_sec = 0;
_uart_timeout.tv_usec = TIMEOUT_US;
err_code = select(_uart + 1, &_uart_set, nullptr, nullptr, &_uart_timeout); err_code = select(_uart + 1, &_uart_set, nullptr, nullptr, &_uart_timeout);
if (err_code < 0) { // An error code of 0 means that select timed out, which is how the Roboclaw indicates an error.
if (err_code <= 0) {
return err_code; return err_code;
} }