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 diff --git a/src/ODriveUART.cpp b/src/ODriveUART.cpp index 10e8823..e7477f2 100644 --- a/src/ODriveUART.cpp +++ b/src/ODriveUART.cpp @@ -5,8 +5,6 @@ #include "Arduino.h" #include "ODriveUART.h" -static const 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; } @@ -18,6 +16,16 @@ void ODriveUART::clearErrors() { serial_ << F("sc\n"); } +void ODriveUART::selectAxis(int axis){ + if(axis >= 0 && axis < 2){ + selected_axis = axis; + } +} + +int ODriveUART::getAxis(){ + return selected_axis; +} + void ODriveUART::setPosition(float position) { setPosition(position, 0.0f, 0.0f); } @@ -27,7 +35,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_ << F("p ") << selected_axis << F(" ") << position << F(" ") << velocity_feedforward << F(" ") << torque_feedforward << F("\n"); } void ODriveUART::setVelocity(float velocity) { @@ -35,15 +43,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_ << F("v ") << selected_axis << F(" ") << velocity << F(" ") << torque_feedforward << F("\n"); } void ODriveUART::setTorque(float torque) { - serial_ << F("c ") << kMotorNumber << F(" ") << torque << F("\n"); + serial_ << F("c ") << selected_axis << F(" ") << torque << F("\n"); } void ODriveUART::trapezoidalMove(float position) { - serial_ << F("t ") << kMotorNumber << F(" ") << position << F("\n"); + serial_ << F("t ") << selected_axis << F(" ") << position << F("\n"); } ODriveFeedback ODriveUART::getFeedback() { @@ -52,7 +60,7 @@ ODriveFeedback ODriveUART::getFeedback() { serial_.read(); } - serial_ << F("f ") << kMotorNumber << F("\n"); + serial_ << F("f ") << selected_axis << F("\n"); String response = readLine(); @@ -77,11 +85,13 @@ void ODriveUART::setParameter(const String& path, const String& value) { } void ODriveUART::setState(ODriveAxisState requested_state) { - setParameter(F("axis0.requested_state"), String((long)requested_state)); + String parameter = "axis" + String(selected_axis) + ".requested_state"; + setParameter(parameter, String((long)requested_state)); } ODriveAxisState ODriveUART::getState() { - return (ODriveAxisState)getParameterAsInt(F("axis0.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 714015a..44c650e 100644 --- a/src/ODriveUART.h +++ b/src/ODriveUART.h @@ -18,6 +18,16 @@ 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(); + /** * @brief Clears the error status of the ODrive and restarts the brake * resistor if it was disabled due to an error. @@ -101,6 +111,8 @@ class ODriveUART { ODriveAxisState getState(); private: + int selected_axis = 0; + String readLine(unsigned long timeout_ms = 10); Stream& serial_;