ROBOTIS DYNAMIXEL XL330 PID Example

From Lofaro Lab Wiki
Revision as of 22:54, 16 September 2021 by Dlofaro (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Prerequisites

To utilize this tutorial, the assumption is made that you have the following:

  • 1x DYNAMIXEL Shield for Arduino MKR Series (MKR Zero Preferred)
  • 1x Arduino MKR compatible device: Please note that this tutorial uses the MKR ZERO
  • 1xCompatible 1S LiPo battery with a JST2.0 connector
  • Nx DYNAMIXEL XL330-M288-T ROBOTIS actuators where N >= 2
  • 1x Micro-USB to USB-A Cable Overview

This tutorial gives examples of how to change the P gain, D gain, and, in the process, change the actuator ID of the XL330-M288-T ROBOTIS actuators when connected to the DYNAMIXEL Shield for MKR.

Video

Tutorial

Written Explanation

To perform this tutorial the following steps must be completed:

  1. Put the DYNAMIXEL shield on top of the Arduino MKR Zero. The ports should exactly match each other. For instance, 5volts from the DYNAMIXEL shield should go into the 5volt channel of the MKR zero and so on.
  2. Connect one of the three stringed wires that came with the DYNAMIXEL actuator to the actuator. Please take care in how you attach the cable as the cable should easily fit into the connection port for the actuator.
  3. Please connect the other end of that wire to port DXL_T1 of the DYNAMIXEL shield.
  4. Once the cable has been attached from one actuator to the DYNAMIXEL shield, connect another 3 stringed cable into the other port on that same XL330 actuator.
  5. Connect the other end to a second XL330 actuator.
  6. Connect the fully charged 1S batter to the JSt2.0 battery port on the DYNAMIXEL shield. Note: this will power the actuators only!
  7. Connect the Micro-USB to USB-A cable to the MKR Zero.
  8. Connect the other end to the USB port of your computer.


Code Explanation (Assuming Dynamixel library is already installed)

Note: No matter what we are trying to do, when using the DYNAMIXEL hardware components, we must always import the DYNAMIXEL library. This means that the Dynamixel2 library must be installed from this website:

Changing the Actuator ID

Since this tutorial requires two actuators, one of the actuator’s ID must be changed. Please note: that this tutorial will only change the ID of one of the IDs at a time. Please unplug one of the three string cables from the actuator. Furthermore, the default ID is 1.

Setup serial ports

Because DYNAMIXEL runs on half-duplex, we must use pin A6 to turn the full-duplex communication into half duplex.

Define the ID

(default = 1), protocol version, and baud rate, and initial values of our positions Note: creating a constant variable that holds the default ID will be used to identify the actuator whose ID needs to be changed.

Pos_d and pos_e are used to acquire each actuator’s current position. Here we are just initializing them to zero.

Create an object of Dynamixel2Arduino type called dxl

Setup the control table

This control table is extremely important as it grants us access to the registers that regulate the values of the P gain and D gain. We are in essence writing to registers within this overall control table.



Inside the setup function

Create the variable that will hold your new ID for your actuator.

Set the baud rate, define it for your dxl object, and define your protocol version

Turn off the torque and set your new ID

Note: from here on out, your actuator that used to have the ID of 1 now has an ID of 150. Therefore, any reference to it other than 150 will not be recognized by the program.

Inside the loop function

Please leave the loop empty.

Acknowledgement about changing ID

All that will happen is that the ID will change. The ID has now been changed from 1 to 150. Please repeat these steps, however. Set new_id = 100 instead of 150. Please replace the actuator with ID =150 with an actuator with ID =1. This will change the ID from 1 to 100.

After the second run, you should have two actuators. One should have ID = 100 and the other ID = 150.We may now begin to change the gains!

Changing P gain and D gain

Note: our loop will not remain empty, and we will be building on top of the code that was used to change the ID.


Back inside the setup loop

Change the baud rate, set the operating modes and turn the torque on for both the actuators

Note: It was brought to my attention that I should not limit the velocity value to a maximum value. This will limit some of the affects that changing the pid gain will have on the actuators. I received my values before realizing that a more noticeable output could be produced if the velocity wasn’t limited. My tutorial limits the velocity, however, for more pronounced outputs, ignore the next step.


Using the control table, as mentioned before, we can write to the P gain register and the D gain register to write a value to it.

  • We have now changed the P gain and D gain and are exiting the setup loop.

Create variables that will be used to toggle between 0 degrees and 90 degrees. Also, create a variable that will be used as our goal position and define a variable that will be used to iterate up to 150 intervals.

Loop function

We are using GOAL_POS_I_MAX to iterate up to 150 times. (This translates to a 3 second delay). During those 3 seconds, we are going to toggle between 0 degrees and 90 degrees.


After toggling the actuator’s angle of degree, acquire the position of each actuator and print it.


Examples

P Gain

The P gain determines how forcefully the actuator responds to its position command. Higher P gains cause the actuator to reach its position quicker and with more accuracy. Lower P gains cause the actuator to be more slack in its response to the position command so the movement will be slower and less accurate. These actuators have a maximum P gain of 16,383 and the factory or default setting is 400.

Fig.1 No Change to p-gain or d-gain (for reference)

Fig 2:P gain is 1200

Fig3. Closeup of P: 1200 D gain is default value (0)

On this graph, the blue line represents the control actuator with P gain of 400. But the other actuator’s p gain is 1200. Notice the overshoot of the red line because the increased P gain is making the actuator respond more aggressively to the position command.




Fig 5. Closup of P: 3000

Fig4. P Gain: 3000

In Figures 4 and 5, the control actuator has a p gain of 400 but increased the P gain for the other actuator to 3000. Notice, the overshoot of the red line, representing the 3000 P gain. The actuator responded so aggressively to the position command that it overshot the goal position but then readjusted itself.

Fig. 6 P gain: 6000

Fig. 7 Closeup of P gain: 6000

In figures, 6 and 7, I increased the actuator from 3000 to 6000. There is even more oscillation after the overshoot, showing that the 6000 P gain caused the actuator to respond with an even greater amount of force than the 3000 P gain.


Fig. 8 P gai: 9000

This graph shows the P gain of the actuator increased from 6000 to 9000. The actuator responded so aggressively that it was constantly overshooting and having to readjust. This is why there are constant zigzag lines showing the

actuator trying to readust itself to obtain the goal position. Even though the manual says that the max p gain is 16383, I didn’t go past 9000 because I didn’t want to break the actuator since it was going out of control at 9000. D gain

Fig. 9 P Gain: 3000 D Gain = 2000 (Control) P Gain: 3000 D Gain = 0

Fig. 10 P Gain = 3000 D Gain: 2000 (close up)

The D gain is short for the derivative gain. The derivative gain can be likened to brakes on a car because the D gain causes the actuator to slow down as it reaches its desired position. To show the affect that the D gain has, I set both p gain of both actuators at 3000 so that each will have an overshoot. The control actuator will have a d gain of zero and I will increase the D gain of the other actuator to show how the brakes being applied will lessen the overshoot. Figs. 9 and 10, on the left shows the D Gain of one of the actuators increased to 2000. Notice that the red line doesn’t have as big of an overshoot as the blue line because the D gain is higher for that actuator.


Fig. 11 P Gain: 3000 D Gain: 3000

Fig. 12 P Gain: 3000 D Gain: 3000 (close up)

In Fig. 11 and 12, the graph represents the d gain of the actuator being increased to 3000. That increase in D gain almost completely eliminated the overshoot.