Skip to content

Commit ed87050

Browse files
committed
Fix ::run_state method (wait_for_idle instead of idle) and better vel_limit setting in .ino sketch example
Pull changes from arduino-improvements branch into master
1 parent b4e5469 commit ed87050

File tree

3 files changed

+41
-16
lines changed

3 files changed

+41
-16
lines changed

Arduino/ODriveArduino/ODriveArduino.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ int32_t ODriveArduino::readInt() {
5959
return readString().toInt();
6060
}
6161

62-
bool ODriveArduino::run_state(int axis, int requested_state, bool wait) {
63-
int timeout_ctr = 100;
62+
bool ODriveArduino::run_state(int axis, int requested_state, bool wait_for_idle, float timeout) {
63+
int timeout_ctr = (int)(timeout * 10.0f);
6464
serial_ << "w axis" << axis << ".requested_state " << requested_state << '\n';
65-
if (wait) {
65+
if (wait_for_idle) {
6666
do {
6767
delay(100);
6868
serial_ << "r axis" << axis << ".current_state\n";

Arduino/ODriveArduino/ODriveArduino.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class ODriveArduino {
3535
int32_t readInt();
3636

3737
// State helper
38-
bool run_state(int axis, int requested_state, bool wait);
38+
bool run_state(int axis, int requested_state, bool wait_for_idle, float timeout = 10.0f);
3939
private:
4040
String readString();
4141

Arduino/ODriveArduino/examples/ODriveArduinoTest/ODriveArduinoTest.ino

+37-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,39 @@
1-
1+
// includes
2+
#include <HardwareSerial.h>
23
#include <SoftwareSerial.h>
34
#include <ODriveArduino.h>
4-
5-
// Printing with stream operator
5+
// Printing with stream operator helper functions
66
template<class T> inline Print& operator <<(Print &obj, T arg) { obj.print(arg); return obj; }
77
template<> inline Print& operator <<(Print &obj, float arg) { obj.print(arg, 4); return obj; }
88

9-
// Serial to the ODrive
10-
SoftwareSerial odrive_serial(8, 9); //RX (ODrive TX), TX (ODrive RX)
11-
// Note: you must also connect GND on ODrive to GND on Arduino!
9+
10+
////////////////////////////////
11+
// Set up serial pins to the ODrive
12+
////////////////////////////////
13+
14+
// Below are some sample configurations.
15+
// You can comment out the default Teensy one and uncomment the one you wish to use.
16+
// You can of course use something different if you like
17+
// Don't forget to also connect ODrive GND to Arduino GND.
18+
19+
// Teensy 3 and 4 (all versions) - Serial1
20+
// pin 0: RX - connect to ODrive TX
21+
// pin 1: TX - connect to ODrive RX
22+
// See https://www.pjrc.com/teensy/td_uart.html for other options on Teensy
23+
HardwareSerial& odrive_serial = Serial1;
24+
25+
// Arduino Mega or Due - Serial1
26+
// pin 19: RX - connect to ODrive TX
27+
// pin 18: TX - connect to ODrive RX
28+
// See https://www.arduino.cc/reference/en/language/functions/communication/serial/ for other options
29+
// HardwareSerial& odrive_serial = Serial1;
30+
31+
// Arduino without spare serial ports (such as Arduino UNO) have to use software serial.
32+
// Note that this is implemented poorly and can lead to wrong data sent or read.
33+
// pin 8: RX - connect to ODrive TX
34+
// pin 9: TX - connect to ODrive RX
35+
// SoftwareSerial odrive_serial(8, 9);
36+
1237

1338
// ODrive object
1439
ODriveArduino odrive(odrive_serial);
@@ -28,7 +53,7 @@ void setup() {
2853
// You can of course set them different if you want.
2954
// See the documentation or play around in odrivetool to see the available parameters
3055
for (int axis = 0; axis < 2; ++axis) {
31-
odrive_serial << "w axis" << axis << ".controller.config.vel_limit " << 22000.0f << '\n';
56+
odrive_serial << "w axis" << axis << ".controller.config.vel_limit " << 10.0f << '\n';
3257
odrive_serial << "w axis" << axis << ".motor.config.current_lim " << 11.0f << '\n';
3358
// This ends up writing something like "w axis0.motor.config.current_lim 10.0\n"
3459
}
@@ -52,23 +77,23 @@ void loop() {
5277

5378
requested_state = ODriveArduino::AXIS_STATE_MOTOR_CALIBRATION;
5479
Serial << "Axis" << c << ": Requesting state " << requested_state << '\n';
55-
odrive.run_state(motornum, requested_state, true);
80+
if(!odrive.run_state(motornum, requested_state, true)) return;
5681

5782
requested_state = ODriveArduino::AXIS_STATE_ENCODER_OFFSET_CALIBRATION;
5883
Serial << "Axis" << c << ": Requesting state " << requested_state << '\n';
59-
odrive.run_state(motornum, requested_state, true);
84+
if(!odrive.run_state(motornum, requested_state, true, 25.0f)) return;
6085

6186
requested_state = ODriveArduino::AXIS_STATE_CLOSED_LOOP_CONTROL;
6287
Serial << "Axis" << c << ": Requesting state " << requested_state << '\n';
63-
odrive.run_state(motornum, requested_state, false); // don't wait
88+
if(!odrive.run_state(motornum, requested_state, false /*don't wait*/)) return;
6489
}
6590

6691
// Sinusoidal test move
6792
if (c == 's') {
6893
Serial.println("Executing test move");
6994
for (float ph = 0.0f; ph < 6.28318530718f; ph += 0.01f) {
70-
float pos_m0 = 20000.0f * cos(ph);
71-
float pos_m1 = 20000.0f * sin(ph);
95+
float pos_m0 = 2.0f * cos(ph);
96+
float pos_m1 = 2.0f * sin(ph);
7297
odrive.SetPosition(0, pos_m0);
7398
odrive.SetPosition(1, pos_m1);
7499
delay(5);

0 commit comments

Comments
 (0)