I cannot set the control mode from Arduino.
Below is my code.
I send commands from Serial (0, 1, 2, 3) … but the control mode always is shown as 0.
What I do wrong?
#define s_version "M.2025.07.16.1"
//-------------------------------------------------------------------
#include <HardwareSerial.h>
#include <SoftwareSerial.h>
#include <ODriveArduino.h>
//-------------------------------------------------------------------
template<class T> 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; }
// ODRIVE variables
HardwareSerial& odrive_serial = Serial1;
ODriveArduino odrive(odrive_serial);
//-------------------------------------------------------------------
void setup() {
Serial.begin(115200);
while (!Serial) {
// some boards need to wait to ensure access to serial over USB
}
Serial.print("Version: ");
Serial.println(s_version);
Serial.println("Connecting to ODrive ...");
odrive_serial.begin(115200);
while (!odrive_serial) {
// some boards need to wait to ensure access to serial over USB
}
Serial.println("ODrive alive ...");
for (int m = 0; m < 2; ++m) {
int requested_state = AXIS_STATE_IDLE;
Serial << "Axis" << m << ": Requesting state " << requested_state << '\n';
if(!odrive.run_state(m, requested_state, false /*don't wait*/))
return;
odrive_serial << "w axis" << m << ".controller.config.input_mode " << INPUT_MODE_PASSTHROUGH << '\n';
delay(15);
}
}
void loop()
{
if (Serial.available()){
char c = Serial.read();
if (c =='0'){
Serial.println("Voltage control");
for (int m = 0; m < 2; ++m) {
odrive_serial << "w axis" << m << ".controller.control_mode " << CONTROL_MODE_VOLTAGE_CONTROL << '\n';
delay(15);
}
}
else
if (c =='1'){
Serial.println("Torque control");
for (int m = 0; m < 2; ++m) {
odrive_serial << "w axis" << m << ".controller.control_mode " << CONTROL_MODE_TORQUE_CONTROL << '\n';
delay(15);
}
}
else
if (c =='2'){
Serial.println("Velocity control");
for (int m = 0; m < 2; ++m) {
odrive_serial << "w axis" << m << ".controller.control_mode " << CONTROL_MODE_VELOCITY_CONTROL << '\n';
delay(15);
}
}
else
if (c =='3'){
Serial.println("Position control");
for (int m = 0; m < 2; ++m) {
odrive_serial << "w axis" << m << ".controller.control_mode " << CONTROL_MODE_POSITION_CONTROL << '\n';
delay(15);
}
}
else
Serial.println("Invalid command");
}
for (int m = 0; m < 2; ++m) {
odrive_serial << "r axis" << m << ".controller.control_mode\n";
delay(5);
Serial << "Control mode of axis" << m << " " << odrive.readInt() << '\n';
}
Serial.println(" ");
delay (1000);
}