From 1f037593e1d521783d4e36a8d2c4a47fa3781605 Mon Sep 17 00:00:00 2001 From: Mathew96 Date: Sat, 8 Feb 2025 01:12:40 +0100 Subject: [PATCH 1/4] added axis selector command for odrive 3.6 --- src/ODriveUART.cpp | 34 +++++++++++++++++++++++----------- src/ODriveUART.h | 3 +++ 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/ODriveUART.cpp b/src/ODriveUART.cpp index 10e8823..9c6ee7d 100644 --- a/src/ODriveUART.cpp +++ b/src/ODriveUART.cpp @@ -5,7 +5,7 @@ #include "Arduino.h" #include "ODriveUART.h" -static const int kMotorNumber = 0; +static int kMotorNumber = 0; // Print with stream operator template inline Print& operator <<(Print &obj, T arg) { obj.print(arg); return obj; } @@ -15,7 +15,17 @@ ODriveUART::ODriveUART(Stream& serial) : serial_(serial) {} void ODriveUART::clearErrors() { - serial_ << F("sc\n"); + serial_ << "sc\n"; +} + +void ODriveUART::selectAxis(int axis){ + if(axis > 0 && axis < 2){ + kMotorNumber = axis; + } +} + +int ODriveUART::getAxis(){ + return kMotorNumber; } void ODriveUART::setPosition(float position) { @@ -27,7 +37,7 @@ void ODriveUART::setPosition(float position, float velocity_feedforward) { } void ODriveUART::setPosition(float position, float velocity_feedforward, float torque_feedforward) { - serial_ << F("p ") << kMotorNumber << F(" ") << position << F(" ") << velocity_feedforward << F(" ") << torque_feedforward << F("\n"); + serial_ << "p " << kMotorNumber << " " << position << " " << velocity_feedforward << " " << torque_feedforward << "\n"; } void ODriveUART::setVelocity(float velocity) { @@ -35,15 +45,15 @@ void ODriveUART::setVelocity(float velocity) { } void ODriveUART::setVelocity(float velocity, float torque_feedforward) { - serial_ << F("v ") << kMotorNumber << F(" ") << velocity << F(" ") << torque_feedforward << F("\n"); + serial_ << "v " << kMotorNumber << " " << velocity << " " << torque_feedforward << "\n"; } void ODriveUART::setTorque(float torque) { - serial_ << F("c ") << kMotorNumber << F(" ") << torque << F("\n"); + serial_ << "c " << kMotorNumber << " " << torque << "\n"; } void ODriveUART::trapezoidalMove(float position) { - serial_ << F("t ") << kMotorNumber << F(" ") << position << F("\n"); + serial_ << "t " << kMotorNumber << " " << position << "\n"; } ODriveFeedback ODriveUART::getFeedback() { @@ -52,7 +62,7 @@ ODriveFeedback ODriveUART::getFeedback() { serial_.read(); } - serial_ << F("f ") << kMotorNumber << F("\n"); + serial_ << "f " << kMotorNumber << "\n"; String response = readLine(); @@ -68,20 +78,22 @@ ODriveFeedback ODriveUART::getFeedback() { } String ODriveUART::getParameterAsString(const String& path) { - serial_ << F("r ") << path << F("\n"); + serial_ << "r " << path << "\n"; return readLine(); } void ODriveUART::setParameter(const String& path, const String& value) { - serial_ << F("w ") << path << F(" ") << value << F("\n"); + serial_ << "w " << path << " " << value << "\n"; } void ODriveUART::setState(ODriveAxisState requested_state) { - setParameter(F("axis0.requested_state"), String((long)requested_state)); + if(kMotorNumber == 0) setParameter("axis0.requested_state", String((long)requested_state)); + if(kMotorNumber == 1) setParameter("axis1.requested_state", String((long)requested_state)); } ODriveAxisState ODriveUART::getState() { - return (ODriveAxisState)getParameterAsInt(F("axis0.current_state")); + if(kMotorNumber == 0) return (ODriveAxisState)getParameterAsInt("axis0.current_state"); + if(kMotorNumber == 1) return (ODriveAxisState)getParameterAsInt("axis1.current_state"); } String ODriveUART::readLine(unsigned long timeout_ms) { diff --git a/src/ODriveUART.h b/src/ODriveUART.h index 714015a..a1c400a 100644 --- a/src/ODriveUART.h +++ b/src/ODriveUART.h @@ -18,6 +18,9 @@ class ODriveUART { */ ODriveUART(Stream& serial); + void selectAxis(int axis); + int getAxis(); + /** * @brief Clears the error status of the ODrive and restarts the brake * resistor if it was disabled due to an error. From ea97c0eef8e86abdf78c54865746199cd03f1984 Mon Sep 17 00:00:00 2001 From: Mathew96 Date: Sat, 8 Feb 2025 01:15:00 +0100 Subject: [PATCH 2/4] bumped version --- LICENSE | 1 + library.properties | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 3952c6d..a0ed272 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,7 @@ MIT License Copyright (c) 2017-2023 ODrive Robotics Inc. +Copyright (c) 2025 Mathew3000 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/library.properties b/library.properties index 5b3eb53..1ca54aa 100644 --- a/library.properties +++ b/library.properties @@ -1,6 +1,6 @@ # https://arduino.github.io/arduino-cli/0.33/library-specification/ name=ODriveArduino -version=0.10.3 +version=0.10.4 author=ODrive Robotics Inc. maintainer=ODrive Robotics Inc. sentence=Library to control ODrive motor controllers From 7850b5bb5c874fe35950ee2d5fd5e411e9bf5915 Mon Sep 17 00:00:00 2001 From: Mathew96 Date: Sat, 8 Feb 2025 01:19:05 +0100 Subject: [PATCH 3/4] fixed oversight in checking axis validity --- src/ODriveUART.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ODriveUART.cpp b/src/ODriveUART.cpp index 9c6ee7d..53336b4 100644 --- a/src/ODriveUART.cpp +++ b/src/ODriveUART.cpp @@ -19,7 +19,7 @@ void ODriveUART::clearErrors() { } void ODriveUART::selectAxis(int axis){ - if(axis > 0 && axis < 2){ + if(axis >= 0 && axis < 2){ kMotorNumber = axis; } } From a817aece8b06d06d8ea4af629049b1d9d0ae9e02 Mon Sep 17 00:00:00 2001 From: Mathew96 Date: Thu, 13 Feb 2025 21:10:08 +0100 Subject: [PATCH 4/4] reinstated F() function and renamed axis-variable --- src/ODriveUART.cpp | 30 ++++++++++++++---------------- src/ODriveUART.h | 9 +++++++++ 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/ODriveUART.cpp b/src/ODriveUART.cpp index 53336b4..e7477f2 100644 --- a/src/ODriveUART.cpp +++ b/src/ODriveUART.cpp @@ -5,8 +5,6 @@ #include "Arduino.h" #include "ODriveUART.h" -static int kMotorNumber = 0; - // Print with stream operator 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; } @@ -15,17 +13,17 @@ ODriveUART::ODriveUART(Stream& serial) : serial_(serial) {} void ODriveUART::clearErrors() { - serial_ << "sc\n"; + serial_ << F("sc\n"); } void ODriveUART::selectAxis(int axis){ if(axis >= 0 && axis < 2){ - kMotorNumber = axis; + selected_axis = axis; } } int ODriveUART::getAxis(){ - return kMotorNumber; + return selected_axis; } void ODriveUART::setPosition(float position) { @@ -37,7 +35,7 @@ void ODriveUART::setPosition(float position, float velocity_feedforward) { } void ODriveUART::setPosition(float position, float velocity_feedforward, float torque_feedforward) { - serial_ << "p " << kMotorNumber << " " << position << " " << velocity_feedforward << " " << torque_feedforward << "\n"; + serial_ << F("p ") << selected_axis << F(" ") << position << F(" ") << velocity_feedforward << F(" ") << torque_feedforward << F("\n"); } void ODriveUART::setVelocity(float velocity) { @@ -45,15 +43,15 @@ void ODriveUART::setVelocity(float velocity) { } void ODriveUART::setVelocity(float velocity, float torque_feedforward) { - serial_ << "v " << kMotorNumber << " " << velocity << " " << torque_feedforward << "\n"; + serial_ << F("v ") << selected_axis << F(" ") << velocity << F(" ") << torque_feedforward << F("\n"); } void ODriveUART::setTorque(float torque) { - serial_ << "c " << kMotorNumber << " " << torque << "\n"; + serial_ << F("c ") << selected_axis << F(" ") << torque << F("\n"); } void ODriveUART::trapezoidalMove(float position) { - serial_ << "t " << kMotorNumber << " " << position << "\n"; + serial_ << F("t ") << selected_axis << F(" ") << position << F("\n"); } ODriveFeedback ODriveUART::getFeedback() { @@ -62,7 +60,7 @@ ODriveFeedback ODriveUART::getFeedback() { serial_.read(); } - serial_ << "f " << kMotorNumber << "\n"; + serial_ << F("f ") << selected_axis << F("\n"); String response = readLine(); @@ -78,22 +76,22 @@ ODriveFeedback ODriveUART::getFeedback() { } String ODriveUART::getParameterAsString(const String& path) { - serial_ << "r " << path << "\n"; + serial_ << F("r ") << path << F("\n"); return readLine(); } void ODriveUART::setParameter(const String& path, const String& value) { - serial_ << "w " << path << " " << value << "\n"; + serial_ << F("w ") << path << F(" ") << value << F("\n"); } void ODriveUART::setState(ODriveAxisState requested_state) { - if(kMotorNumber == 0) setParameter("axis0.requested_state", String((long)requested_state)); - if(kMotorNumber == 1) setParameter("axis1.requested_state", String((long)requested_state)); + String parameter = "axis" + String(selected_axis) + ".requested_state"; + setParameter(parameter, String((long)requested_state)); } ODriveAxisState ODriveUART::getState() { - if(kMotorNumber == 0) return (ODriveAxisState)getParameterAsInt("axis0.current_state"); - if(kMotorNumber == 1) return (ODriveAxisState)getParameterAsInt("axis1.current_state"); + String parameter = "axis" + String(selected_axis) + ".current_state"; + return (ODriveAxisState)getParameterAsInt(parameter); } String ODriveUART::readLine(unsigned long timeout_ms) { diff --git a/src/ODriveUART.h b/src/ODriveUART.h index a1c400a..44c650e 100644 --- a/src/ODriveUART.h +++ b/src/ODriveUART.h @@ -18,7 +18,14 @@ class ODriveUART { */ ODriveUART(Stream& serial); + /** + * @brief (Only ODrive 3.6) Select which axis to controll + */ void selectAxis(int axis); + + /** + * @brief (Only ODrive 3.6) Check which axis is selected + */ int getAxis(); /** @@ -104,6 +111,8 @@ class ODriveUART { ODriveAxisState getState(); private: + int selected_axis = 0; + String readLine(unsigned long timeout_ms = 10); Stream& serial_;