Librería de Arduino para controlar ODrive por UART (protocolo ASCII), con foco en control de alta frecuencia y baja latencia.
Este proyecto parte de la librería oficial de ODrive y mantiene su estilo de uso, agregando funciones optimizadas para enviar comandos y leer feedback en menos transacciones.
Repositorio original de referencia:
Documentación oficial del protocolo ASCII (ODrive 0.5.6 / ODrive 3.6):
- API simple para comandos de posición, velocidad y corriente.
- Funciones de lectura agrupada de feedback (posición/velocidad y
vbus). - Funciones combinadas para setpoint + feedback en una sola secuencia de UART.
- Soporte de constantes de estado/modo mediante
ODriveEnums.h.
- ODrive con interfaz UART ASCII habilitada.
- Pensada para ODrive v3.x (puede funcionar con otras versiones si mantienen comandos ASCII compatibles).
- Cualquier
Streamde Arduino (HardwareSerial,SoftwareSerial, etc.).
- Descarga el repositorio como
.zip. - En Arduino IDE:
Programa > Incluir Librería > Añadir biblioteca .ZIP. - Selecciona el archivo
.zipdescargado. - Incluye en tu sketch:
#include "ODriveArduino.h"#include <HardwareSerial.h>
#include "ODriveArduino.h"
HardwareSerial& odrive_serial = Serial1;
ODriveArduino odrive(odrive_serial);
void setup() {
Serial.begin(115200);
odrive_serial.begin(115200);
odrive.run_state(0, AXIS_STATE_FULL_CALIBRATION_SEQUENCE, true);
odrive.run_state(0, AXIS_STATE_CLOSED_LOOP_CONTROL, false);
}
void loop() {
odrive.SetVelocity(0, 2.0f);
delay(10);
}void SaveConfig()void EraseConfig()void Reboot()void ClearErrors()void SendCommand(const char* command)ODriveAxisState read_state(int axis)bool run_state(int axis, ODriveAxisState requested_state, bool wait_for_idle, float timeout = 10.0f)void WaitIdle(int motor_number = -1, float timeout = 10.0f)
Notas:
motor_number = -1enWaitIdle()espera ambos ejes.run_state()devuelvetruesi no expira el timeout.
void WriteProperty(int motor_number, const char* property, double value)void WriteProperty(int motor_number, const char* property, float value)void WriteProperty(int motor_number, const char* property, int value)void ReadProperty(int motor_number, const char* property, double* value)void ReadProperty(int motor_number, const char* property, float* value)void ReadProperty(int motor_number, const char* property, int* value)
Ejemplo:
float vel_limit;
odrive.ReadProperty(0, "controller.config.vel_limit", &vel_limit);
odrive.WriteProperty(0, "controller.config.vel_limit", 15.0f);void SetPosition(int motor_number, float position)void SetPosition(int motor_number, float position, float velocity_feedforward)void SetPosition(int motor_number, float position, float velocity_feedforward, float current_feedforward)void SetVelocity(int motor_number, float velocity)void SetVelocity(int motor_number, float velocity, float current_feedforward)void SetCurrent(int motor_number, float current)void TrapezoidalMove(int motor_number, float position)
float GetVelocity(int motor_number)float GetPosition(int motor_number)float GetVbus()void GetFeedback(int motor_number, float* velocity_M, float* position_M)void GetFeedbackBoth(float* velocity_M0, float* position_M0, float* velocity_M1, float* position_M1)void GetFeedback_Vbus(int motor_number, float* velocity_M, float* position_M, float* vbus)void GetFeedbackBoth_Vbus(float* velocity_M0, float* position_M0, float* velocity_M1, float* position_M1, float* vbus)
void SetVelocityBoth(float velocity_M0, float velocity_M1)void SetVelocity_GetFeedback(int motor_number, float set_VM, float* velocity_M, float* position_M)void SetVelocityBoth_GetFeedback(float set_VM0, float set_VM1, float* velocity_M0, float* position_M0, float* velocity_M1, float* position_M1)void SetVelocityBoth_GetFeedback_Vbus(float set_VM0, float set_VM1, float* vbus, float* velocity_M0, float* position_M0, float* velocity_M1, float* position_M1)void SetVelocityBoth_GetVbus(float set_VM0, float set_VM1, float* vbus)
Estas funciones reducen transacciones UART al combinar en una sola secuencia:
- escritura de setpoints,
- lectura de feedback,
- y opcionalmente lectura de
vbus.
void loop() {
float vbus, vel0, pos0, vel1, pos1;
float set_v0 = 2.0f;
float set_v1 = -2.0f;
odrive.SetVelocityBoth_GetFeedback_Vbus(
set_v0, set_v1,
&vbus,
&vel0, &pos0,
&vel1, &pos1
);
Serial.print("Vbus: "); Serial.print(vbus);
Serial.print(" | M0 vel/pos: "); Serial.print(vel0); Serial.print(" / "); Serial.print(pos0);
Serial.print(" | M1 vel/pos: "); Serial.print(vel1); Serial.print(" / "); Serial.println(pos1);
delay(10);
}- El operador
<<está sobrecargado paraPrint, y parafloatse imprime con 4 decimales. - Las funciones de feedback llaman internamente a
CleanSerial()para vaciar datos pendientes antes de la transacción. - Lecturas de bajo nivel disponibles:
float readFloat(unsigned long timeout = 100)int32_t readInt(unsigned long timeout = 100)
- El timeout de lectura se mide internamente con
micros().
Si quieres usar UART a velocidades altas (por ejemplo 1M o 2M baudios), se recomienda eliminar el filtro pasa bajos pasivo en los GPIO 1 y 2 de ODrive 3.6 (UART A).
Estos GPIO incluyen filtro RC con jumpers soldables (SMD):
- GPIO 1:
R52en paralelo aJ5(NO)C63en serie conJ6(NC)
- GPIO 2:
R53en paralelo conJ10C64en paralelo conJ15
Opciones para deshabilitar el filtro:
- Opción 1:
- Soldar puentes
J5yJ10(bypass deR52yR53). - Opcionalmente cortar
J6yJ15para desconectarC63yC64.
- Soldar puentes
- Opción 2:
- Remover
R52,R53,C63,C64. - Soldar resistencias
0RenR52yR53.
- Remover
Notas:
- En algunos clones chinos, la numeración de componentes puede variar respecto al esquemático original.
- Esta modificación permite aumentar significativamente la frecuencia efectiva de actualización del setpoint de velocidad (por ejemplo, ~1000 Hz o más, según firmware, CPU y calidad de enlace UART).
ODriveEnums.h incluye constantes como:
AXIS_STATE_*CONTROL_MODE_*INPUT_MODE_*
Usarlas mejora la legibilidad y evita valores mágicos.
Puedes revisar un ejemplo funcional en:
examples/ODriveArduinoTest/ODriveArduinoTest.ino