Skip to content

Simple addition to controll multi-axis odrives such as 3.6 #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -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. <info@odriverobotics.com>
maintainer=ODrive Robotics Inc. <info@odriverobotics.com>
sentence=Library to control ODrive motor controllers
Expand Down
34 changes: 23 additions & 11 deletions src/ODriveUART.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "Arduino.h"
#include "ODriveUART.h"

static const int kMotorNumber = 0;
static int kMotorNumber = 0;

// Print with stream operator
template<class T> inline Print& operator <<(Print &obj, T arg) { obj.print(arg); return obj; }
Expand All @@ -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) {
Expand All @@ -27,23 +37,23 @@ 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) {
setVelocity(velocity, 0.0f);
}

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() {
Expand All @@ -52,7 +62,7 @@ ODriveFeedback ODriveUART::getFeedback() {
serial_.read();
}

serial_ << F("f ") << kMotorNumber << F("\n");
serial_ << "f " << kMotorNumber << "\n";

String response = readLine();

Expand All @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions src/ODriveUART.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class ODriveUART {
*/
ODriveUART(Stream& serial);

void selectAxis(int axis);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would have to be documented, especially that it can be ignored for all current-generation ODrives

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above, could be mitigated with the overload

int getAxis();

/**
* @brief Clears the error status of the ODrive and restarts the brake
* resistor if it was disabled due to an error.
Expand Down