Arduino Mega Uart control - Position AND Velocity

Hi there,

I am trying to control two O drive motors with S1 boards from an Arduino Mega

They are working well in Position, Torque and Velocity mode individually however they just vibrate loads whilst moving when I try telling them a position with a desired velocity e.g

void setPosition(float position, float velocity_feedforward)

Why is this? Any help would be greatly appreciated

Many thanks!

My simple Arduino code is here below

 
#include <ODriveUART.h>
#include <HardwareSerial.h>

HardwareSerial& odrive1_serial = Serial1;
HardwareSerial& odrive2_serial = Serial2;

int baudrate = 19200; 

ODriveUART odrive1(odrive1_serial);
ODriveUART odrive2(odrive2_serial);

void setup() {
  
  odrive1_serial.begin(19200);
  odrive2_serial.begin(19200);
  
  Serial1.begin(19200); 
  Serial2.begin(19200); 

  Serial.println("Waiting for ODrive...");
  while (odrive1.getState() == AXIS_STATE_UNDEFINED) {
    delay(100);
  }

  Serial.println("found ODrive");
  
  Serial.print("DC voltage: ");
  Serial.println(odrive1.getParameterAsFloat("vbus_voltage"));
  
  Serial.println("Enabling closed loop control...");
  while (odrive1.getState() != AXIS_STATE_CLOSED_LOOP_CONTROL) {
    odrive1.clearErrors();
    odrive1.setState(AXIS_STATE_CLOSED_LOOP_CONTROL);
    delay(10);
  
  }
    Serial.print("DC voltage: ");
  Serial.println(odrive1.getParameterAsFloat("vbus_voltage"));
  
  Serial.println("Enabling closed loop control...");
  while (odrive2.getState() != AXIS_STATE_CLOSED_LOOP_CONTROL) {
    odrive2.clearErrors();
    odrive2.setState(AXIS_STATE_CLOSED_LOOP_CONTROL);
    delay(10);
  
  }
  
  Serial.println("ODrive running!");

}

void loop() {


odrive1.setPosition(20, 5);

}

The velocity feedforward is typically for use when you’re sending a motion trajectory – e.g. where you’re streaming position setpoints at a high rate, and you don’t want the ODrive to decelerate when reaching each setpoint. It also increases the position tracking accuracy in that motion trajectory situation. It should only be used when you’re streaming an actual motion profile - just sending a single pos setpoint w/ a nonzero feedforward will result in the behavior you’re experiencing – the velocity controller is trying to drive the velocity to 5, which is directly opposed to the position controller’s goal of keeping the position at 20, so they fight.

1 Like

Ok thank you I see the conflict there. Do you know the best way to send the motor to a certain position at a desired velocity? Even when I set position and velocity separately the motor still makes an awful vibrating noise when its moving.

Many thanks for your help

The trajectory planner (trap_traj input mode) is the right way here. Alternatively, you can set position only, or use pos_filter mode and set a soft_vel_limit

1 Like