Small guide on how to use ODriveArduino Commands

Okay no one would answer my question so I have to figure it out. I wanted to share what I learned for other people that are struggling. This is how to use an Arduino to control ODrive very simply stated.

If you want to set position use a command like odrive.SetPosition(0, 100000);
This sets the position to 100000 counts for motor 0.

To set velocity use a command like odrive.SetVelocity(0, -2000, 10);
This sets velocity to negative 2000 [count/s] for motor 0 at 10 amps. If you don’t want to control the amps just don’t add it.

And to the part that was very poorly explained in my opinion on GitHub.

To get the bus voltage use two commands like this:
odrive_serial.write(“r vbus_voltage\n”);
volt = odrive.readFloat();

And to get the encoder position and add it to a variable use two commands like this:
odrive_serial.write(“r axis0.encoder.pos_estimate\n”);
endstopPos = odrive.readFloat();

I believe the “r” means read or something like that? Idk I like the GetParameters command better but I guess this works too. I would do your odrive.readFloat(); immediately after you ask for the variable because it seems to go away after you ask the ODrive for it (this doesn’t seems like a strong way to do this to me). If you want to get the position of the other motor I believe that you need to use axis1. Not sure because I am only driving one motor. You don’t have to put an axis number to get the voltage because it is a constant I believe.

If you are not a super strong programmer like me then I hope this clears up what is happening on GitHub for ODriveArduino. Some of the code I copied directly from GitHub wouldn’t compile. But I know that these commands work as of writing this because I have tested it. I am sure that this could change in the future as ODrive evolves. It probably should because this is confusing.

7 Likes

Hi @Dylan

Perhaps the ascii protocol documentation can be of help as well. It documents the commands that you can send by calling the odrive_serial.write() function.

1 Like

Thanks, I guess it helps a little bit. Probably would have more if I had seen that first. I guess you could send movement commands this way on arduino as well, but again, I don’t really like this way. Another thing is that I noticed a lag in my code when hooked to the serial monitor when using the odrive_serial.write() function. I am not sure if this is something I am doing or if it is something to do with the library or commands or what.

Thanks a lot for your input @Dylan. If there is any of this you would like to integrate to the official version of the ODriveArduino example code: changes to either code or comments, please do submit any proposed changes here.

1 Like

Yes I might, you commented on my other post and I will be taking a look at that as well here Getting Bus Voltage and Encoder Position with ODriveArduino . As a novice, serial writing seems simpler to me than bitshifting.

Unfortunately after I wrote this I started to experience a weird issue. Take a look at this code that is running in my loop real quick:

> //Adding Pot values to Velocity and Torq and scaling
>   torqInt = map(mydata_remote.torq, 0, 1023, 1023, 0);                      // Remaping torq data
>   velocity = (mydata_remote.vel * ((velTop - velBottom)/1023L)) + velBottom; // Set up Velocity Range.  8192 is cpr and max rpm is 7030
>   torq = (torqInt * ((ampTop - ampBottom)/1023)) + ampBottom;               // Set up Amperage range given ampBottom and ampTop
> 
> 
> //Move commands to the ODrive 
> 
> //Going Up
>   if (mydata_remote.up == 1  ) {
>     moving = 1;
>     odrive.SetVelocity(0, velocity, torq); 
>   }
> 
> 
>  //Going Down
>   if (mydata_remote.down == 1 ) {
>     odrive.SetVelocity(0, -velocity, torq);
>     moving = 1;
>   }
> 
> 
> //Stop
>   else {
>     odrive.SetVelocity(0, 0);
>     moving = 0;
>   }

The remote date is coming from a controller that I built is sending data to this Arduino via <EasyTransfer.h> library to control the ODrive. The problem is that whichever if statement comes first when it sends the set velocity command to the ODrive the motor spins very slowly and makes a lot more noise. Like it is not spinning correctly and is drawing too much current. This happens to the first If statement whichever I put first (going up or going down). The only way that I have been able to fix this is by sending the SetVelocity command more than once like this:

    //DOWN
      if (mydata_remote.down == 1 && endstop == 0) {
        odrive.SetVelocity(0, -velocity, torq);
        odrive.SetVelocity(0, -velocity, torq);
        moving = 1;
      }

    //UP
      if (mydata_remote.up == 1 && currentPos >= farEndStop) {
        odrive.SetVelocity(0, velocity, torq);
        odrive.SetVelocity(0, velocity, torq);
        moving = 1; 
      }
     
      else {
        odrive.SetVelocity(0, 0);
        moving = 0;
      }

Any ideas on why I am having to send the commands more than once to get the desired result? Baud rate is set to 115200. Do I need to do a firmware update? I feel like this issue is something going on in the ODrive but who knows.

As background on what I am doing I am using the ODrive and brushless motor to turn a hydraulic pump to make an electro hydraulic actuator.

Also thanks @madcowswe for commenting and helping.

Hi Dylan

Did you solved the issue of having to send the instruction twice?

I cannot even install de odrive library in arduino; not through Sketch > Include Library > Add .ZIP Library
and neither by installing it in my private “libraries” folder. It doesn’t give an error but the examples don’t show and I cannot use the library. Pretty bad for such an expensive board. I am now going to build my own amplifier.

1 Like

I doubt the arduino library will install correctly with the built-in arduino manager. You simply download the ODrive source code from github, then copy & paste the ODriveArduino library into your Documents/Arduino/libraries folder. I just checked and it even shows up in the Arduino IDE under “Custom Library Examples”

Nope, if I install it in my Documents\Arduino\libraries, it will not show in the Arduino IDE under “Examples from Custom Libraries”. Also not after a restart. The other four libraries that I installed there, are shown.

Any chance someone would post a working example sketch. Something that just sets a motor velocity. THe ODriveArduinoTest that is packaged with the library worked one time but I messed it up some how and it will not verify anymore.

1 Like