From db4022a0448ad2ff28ad8f85d5fb44368403c155 Mon Sep 17 00:00:00 2001 From: Oskar Weigl Date: Fri, 9 Oct 2020 17:22:23 -0700 Subject: [PATCH 1/4] improve arduino run_state and fix sine example movement units --- Arduino/ODriveArduino/ODriveArduino.cpp | 4 ++-- Arduino/ODriveArduino/ODriveArduino.h | 2 +- .../examples/ODriveArduinoTest/ODriveArduinoTest.ino | 10 +++++----- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Arduino/ODriveArduino/ODriveArduino.cpp b/Arduino/ODriveArduino/ODriveArduino.cpp index 3f4eab5a..bca507f2 100644 --- a/Arduino/ODriveArduino/ODriveArduino.cpp +++ b/Arduino/ODriveArduino/ODriveArduino.cpp @@ -59,8 +59,8 @@ int32_t ODriveArduino::readInt() { return readString().toInt(); } -bool ODriveArduino::run_state(int axis, int requested_state, bool wait) { - int timeout_ctr = 100; +bool ODriveArduino::run_state(int axis, int requested_state, bool wait_for_idle, float timeout) { + int timeout_ctr = (int)(timeout * 10.0f); serial_ << "w axis" << axis << ".requested_state " << requested_state << '\n'; if (wait) { do { diff --git a/Arduino/ODriveArduino/ODriveArduino.h b/Arduino/ODriveArduino/ODriveArduino.h index 8e39b244..6620f859 100644 --- a/Arduino/ODriveArduino/ODriveArduino.h +++ b/Arduino/ODriveArduino/ODriveArduino.h @@ -35,7 +35,7 @@ public: int32_t readInt(); // State helper - bool run_state(int axis, int requested_state, bool wait); + bool run_state(int axis, int requested_state, bool wait_for_idle, float timeout = 10.0f); private: String readString(); diff --git a/Arduino/ODriveArduino/examples/ODriveArduinoTest/ODriveArduinoTest.ino b/Arduino/ODriveArduino/examples/ODriveArduinoTest/ODriveArduinoTest.ino index 1e835026..c2cc8b77 100644 --- a/Arduino/ODriveArduino/examples/ODriveArduinoTest/ODriveArduinoTest.ino +++ b/Arduino/ODriveArduino/examples/ODriveArduinoTest/ODriveArduinoTest.ino @@ -52,23 +52,23 @@ void loop() { requested_state = ODriveArduino::AXIS_STATE_MOTOR_CALIBRATION; Serial << "Axis" << c << ": Requesting state " << requested_state << '\n'; - odrive.run_state(motornum, requested_state, true); + if(!odrive.run_state(motornum, requested_state, true)) return; requested_state = ODriveArduino::AXIS_STATE_ENCODER_OFFSET_CALIBRATION; Serial << "Axis" << c << ": Requesting state " << requested_state << '\n'; - odrive.run_state(motornum, requested_state, true); + if(!odrive.run_state(motornum, requested_state, true, 25.0f)) return; requested_state = ODriveArduino::AXIS_STATE_CLOSED_LOOP_CONTROL; Serial << "Axis" << c << ": Requesting state " << requested_state << '\n'; - odrive.run_state(motornum, requested_state, false); // don't wait + if(!odrive.run_state(motornum, requested_state, false /*don't wait*/)) return; } // Sinusoidal test move if (c == 's') { Serial.println("Executing test move"); for (float ph = 0.0f; ph < 6.28318530718f; ph += 0.01f) { - float pos_m0 = 20000.0f * cos(ph); - float pos_m1 = 20000.0f * sin(ph); + float pos_m0 = 2.0f * cos(ph); + float pos_m1 = 2.0f * sin(ph); odrive.SetPosition(0, pos_m0); odrive.SetPosition(1, pos_m1); delay(5); From 680a4a2953e968cb0386d3296de30c8dcac7b0c7 Mon Sep 17 00:00:00 2001 From: Oskar Weigl Date: Fri, 9 Oct 2020 17:34:42 -0700 Subject: [PATCH 2/4] improve ardiuno serial connection guide --- .../ODriveArduinoTest/ODriveArduinoTest.ino | 36 +++++++++++++++---- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/Arduino/ODriveArduino/examples/ODriveArduinoTest/ODriveArduinoTest.ino b/Arduino/ODriveArduino/examples/ODriveArduinoTest/ODriveArduinoTest.ino index c2cc8b77..eb1a4e73 100644 --- a/Arduino/ODriveArduino/examples/ODriveArduinoTest/ODriveArduinoTest.ino +++ b/Arduino/ODriveArduino/examples/ODriveArduinoTest/ODriveArduinoTest.ino @@ -1,14 +1,38 @@ - +// includes #include #include - -// Printing with stream operator +// Printing with stream operator helper functions template inline Print& operator <<(Print &obj, T arg) { obj.print(arg); return obj; } template<> inline Print& operator <<(Print &obj, float arg) { obj.print(arg, 4); return obj; } -// Serial to the ODrive -SoftwareSerial odrive_serial(8, 9); //RX (ODrive TX), TX (ODrive RX) -// Note: you must also connect GND on ODrive to GND on Arduino! + +//////////////////////////////// +// Set up serial pins to the ODrive +//////////////////////////////// + +// Below are some sample configurations. +// You can comment out the default Teensy one and uncomment the one you wish to use. +// You can of course use something different if you like +// Don't forget to also connect ODrive GND to Arduino GND. + +// Teensy 3 and 4 (all versions) - Serial1 +// pin 0: RX - connect to ODrive TX +// pin 1: TX - connect to ODrive RX +// See https://www.pjrc.com/teensy/td_uart.html for other options on Teensy +Stream& odrive_serial = Serial1; + +// Arduino Mega or Due - Serial1 +// pin 19: RX - connect to ODrive TX +// pin 18: TX - connect to ODrive RX +// See https://www.arduino.cc/reference/en/language/functions/communication/serial/ for other options +// Stream& odrive_serial = Serial1; + +// Arduino without spare serial ports (such as Arduino UNO) have to use software serial. +// Note that this is implemented poorly and can lead to wrong data sent or read. +// pin 8: RX - connect to ODrive TX +// pin 9: TX - connect to ODrive RX +// SoftwareSerial odrive_serial(8, 9); + // ODrive object ODriveArduino odrive(odrive_serial); From 5d42a68e7109f613209f0f9ac95abeecb14aee4d Mon Sep 17 00:00:00 2001 From: Oskar Weigl Date: Tue, 13 Oct 2020 17:42:56 -0700 Subject: [PATCH 3/4] Serial1 has type HardwareSerial --- .../examples/ODriveArduinoTest/ODriveArduinoTest.ino | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Arduino/ODriveArduino/examples/ODriveArduinoTest/ODriveArduinoTest.ino b/Arduino/ODriveArduino/examples/ODriveArduinoTest/ODriveArduinoTest.ino index eb1a4e73..9f74d09d 100644 --- a/Arduino/ODriveArduino/examples/ODriveArduinoTest/ODriveArduinoTest.ino +++ b/Arduino/ODriveArduino/examples/ODriveArduinoTest/ODriveArduinoTest.ino @@ -1,4 +1,5 @@ // includes +#include #include #include // Printing with stream operator helper functions @@ -19,13 +20,13 @@ template<> inline Print& operator <<(Print &obj, float arg) { obj.print(a // pin 0: RX - connect to ODrive TX // pin 1: TX - connect to ODrive RX // See https://www.pjrc.com/teensy/td_uart.html for other options on Teensy -Stream& odrive_serial = Serial1; +HardwareSerial& odrive_serial = Serial1; // Arduino Mega or Due - Serial1 // pin 19: RX - connect to ODrive TX // pin 18: TX - connect to ODrive RX // See https://www.arduino.cc/reference/en/language/functions/communication/serial/ for other options -// Stream& odrive_serial = Serial1; +// HardwareSerial& odrive_serial = Serial1; // Arduino without spare serial ports (such as Arduino UNO) have to use software serial. // Note that this is implemented poorly and can lead to wrong data sent or read. From bd51fca58bf1a61f4a01b9dea2309d83f0782166 Mon Sep 17 00:00:00 2001 From: PAJohnson Date: Wed, 14 Oct 2020 15:18:37 -0400 Subject: [PATCH 4/4] Fix ::run_state method (wait_for_idle instead of idle) and better vel_limit setting in .ino sketch example --- Arduino/ODriveArduino/ODriveArduino.cpp | 2 +- .../examples/ODriveArduinoTest/ODriveArduinoTest.ino | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Arduino/ODriveArduino/ODriveArduino.cpp b/Arduino/ODriveArduino/ODriveArduino.cpp index bca507f2..cd3fcd5e 100644 --- a/Arduino/ODriveArduino/ODriveArduino.cpp +++ b/Arduino/ODriveArduino/ODriveArduino.cpp @@ -62,7 +62,7 @@ int32_t ODriveArduino::readInt() { bool ODriveArduino::run_state(int axis, int requested_state, bool wait_for_idle, float timeout) { int timeout_ctr = (int)(timeout * 10.0f); serial_ << "w axis" << axis << ".requested_state " << requested_state << '\n'; - if (wait) { + if (wait_for_idle) { do { delay(100); serial_ << "r axis" << axis << ".current_state\n"; diff --git a/Arduino/ODriveArduino/examples/ODriveArduinoTest/ODriveArduinoTest.ino b/Arduino/ODriveArduino/examples/ODriveArduinoTest/ODriveArduinoTest.ino index 9f74d09d..e75a1862 100644 --- a/Arduino/ODriveArduino/examples/ODriveArduinoTest/ODriveArduinoTest.ino +++ b/Arduino/ODriveArduino/examples/ODriveArduinoTest/ODriveArduinoTest.ino @@ -53,7 +53,7 @@ void setup() { // You can of course set them different if you want. // See the documentation or play around in odrivetool to see the available parameters for (int axis = 0; axis < 2; ++axis) { - odrive_serial << "w axis" << axis << ".controller.config.vel_limit " << 22000.0f << '\n'; + odrive_serial << "w axis" << axis << ".controller.config.vel_limit " << 10.0f << '\n'; odrive_serial << "w axis" << axis << ".motor.config.current_lim " << 11.0f << '\n'; // This ends up writing something like "w axis0.motor.config.current_lim 10.0\n" }