1
-
1
+ // includes
2
+ #include < HardwareSerial.h>
2
3
#include < SoftwareSerial.h>
3
4
#include < ODriveArduino.h>
4
-
5
- // Printing with stream operator
5
+ // Printing with stream operator helper functions
6
6
template <class T > inline Print& operator <<(Print &obj, T arg) { obj.print (arg); return obj; }
7
7
template <> inline Print& operator <<(Print &obj, float arg) { obj.print (arg, 4 ); return obj; }
8
8
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
+
12
37
13
38
// ODrive object
14
39
ODriveArduino odrive (odrive_serial);
@@ -28,7 +53,7 @@ void setup() {
28
53
// You can of course set them different if you want.
29
54
// See the documentation or play around in odrivetool to see the available parameters
30
55
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 ' ;
32
57
odrive_serial << " w axis" << axis << " .motor.config.current_lim " << 11 .0f << ' \n ' ;
33
58
// This ends up writing something like "w axis0.motor.config.current_lim 10.0\n"
34
59
}
@@ -52,23 +77,23 @@ void loop() {
52
77
53
78
requested_state = ODriveArduino::AXIS_STATE_MOTOR_CALIBRATION;
54
79
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 ;
56
81
57
82
requested_state = ODriveArduino::AXIS_STATE_ENCODER_OFFSET_CALIBRATION;
58
83
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 ;
60
85
61
86
requested_state = ODriveArduino::AXIS_STATE_CLOSED_LOOP_CONTROL;
62
87
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 ;
64
89
}
65
90
66
91
// Sinusoidal test move
67
92
if (c == ' s' ) {
68
93
Serial.println (" Executing test move" );
69
94
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);
72
97
odrive.SetPosition (0 , pos_m0);
73
98
odrive.SetPosition (1 , pos_m1);
74
99
delay (5 );
0 commit comments