Tuning the GyroDrive PIDs

Steps to Tuning the GyroDrive PIDs

This section describes the GyroDrive configuration and tuning practices so that the robot can travel in a straight line using the gyro.

This section contains:

  • Understanding PIDs
  • Step 1: Use the Shuffleboard dashboard to display values
  • Step 2: Make sure the motors and the encoders are travelling in the same direction
  • Step 3: tuning the Speed PIDs and set the encoder counts/inch
  • Step 4: Tuning the Gyro PID

After this step is completed, the robot can be successfully programmed for autonomous patterns.

Understanding PIDs

What is a PID Controller?

Proportional, Integral, Differential control systems are used to control the output of a system, in our case a robot.

The GyroDrive example uses two types of PID controllers:

  • SpeedPIDs to control the speed of a motor
  • GyroPID to control the robot heading using a gyro

The TorontoCodingCollective does not recommend the use of derivative control since it is very susceptable to noise and difficult to tune, and is not required in the vast majority of cases. The SpeedPID and GyroPID classes are PI control classes and do not support differntial control.

Step 1: Using the Shuffleboard to display Values from the Robot

The Shuffleboard dashboard takes more time to configure than the SmartDashboard, but the configuration of the dashboard can be saved. Unfortunately there are bugs in the SmartDashboard, and the save function does not work so you cannot properly save the layout.

It is highly recommended that teams use the Shuffleboard dashboard. TorontoCodingCollective provides a starter shuffleboard layout as part of the GyroDrive Project.

Connect to the Robot and Start the Driver Station

Use a radio connection or a USB cable to connect to the robot and start the DriverStation software

Ensure you are connected to the robot! When connected to the robot, the Communications light is green.

To start the shuffleboard dashboard, open the DriverStation software and Select Shuffleboard from the selection menu. This should open the Shuffleboard dashboard after the release in Jan, 2019. Currently it may not unless you have the 2018 developer tools installed.

Alternately download the 2018 Shuffleboard and Outline Viewer and unzip on the desktop. Double click the Shuffleboard.jar to start the Shuffleboard dashboard.

Loading the TorontoCodingCollective Shuffleboard Dashboard Layout

After connecting to the robot, and starting the shuffleboard dashboard, load the TorontoCodingCollective layout by opening the shuffleboard.json file that was downloaded with the GyroDrive project:

  • select File->Open, or ctrl-O
  • Open the shuffleboard.json file in the GyroDrive project folder

After loading the shuffleboard.json file, the shuffleboard layout should look something like this when connected to the robot.

Step 2: Ensure the Motors and Encoders are moving in the same direction

Why does it matter that they are in the same direction?

PID Controllers require that the motors and the encoders are moving in the same direction.

A PID Controller uses feedback from the sensors to control the motor speed. In order for the feedback to function correctly, the output from the PID must directly affect the output of the robot. When the controller senses that the wheel is spinning too slowly through the encoder feedback, it will try to increase the output to compensate for the slow wheel speed. The increase in output will ideally drive the robot faster, and the measured wheel speed will increase.

If the SpeedController output drives in the opposite direction to the robot, then the robot will run away because the desired wheel speed cannot be achieved

Rotate the wheels by hand in the forward direction

Did the encoders register a forward value?

If the encoders do not register in the forward direction:

  • open the RobotMap.java class and set the encoder inversion
  • redeploy the robot code and test again

Test the motor direction

  • enable the robot
  • drive in the forward direction

If the motors do not move in the forward direction:

  • open the RobotMap.java class and set the encoder inversion
  • redeploy the robot code and test again

Step 3: Tune the speed PIDs

Find the Maximum Rotational Speed

Why do I need this? The PID controller will translate the output from the joystick (0.0-1.0) into a rotational speed (0-330 in this case). In order to control the motors, the maximum rotation speed needs to be known... and at full output (1.0), the system must be able to attain the maximum speed (330). If the system cannot achieve the maximum speed, the controller will output full power, and the system will behave as if there is no PID control.

To find the maximum rotational speed:

  • enable the robot
  • observe the maximum speed on the L and R encoders (these may be different by 5 or 10%)
  • choose a number about 5-10% lower than either value if the wheels have no load.
  • enter this value in the RoboConst.java file under MAX_LOW_GEAR_SPEED
  • redeploy the code

Tuning the PID

  • Enable the speed PIDs by pressing the right joystick on the speed controller. Observe that the left and right PIDs are enabled
  • Tune the PIDS by setting the integral gain to zero, and adjusting the proportional gain as high as possible until the wheels can be driven without oscillating when stopping.
  • NOTE: when changing the P (Proportional) value of the PIDs, enter the same value on both PID controllers (Left and Right), and hit enter. Re-enable the robot, and use the right joystick to enable and disable the PIDs
  • Enter the final proportional gain in the RobotConst.java file constant DRIVE_SPEED_PID_KP
  • The integral gain will be calculated as 1/20 of the proportional gain
  • Redeploy the code and test the PIDs

Test the Loaded MAX speed

Run the robot on the ground at full speed and have a partner observe the max L and R encoder speeds under load. Ensure that the MAX_LOW_GEAR_SPEED is set to a value lower than the observed maximum loaded speed for both encoders.

Enable the loaded robot and test the the drive with the PIDs enabled. Adjust the gain of the PIDs in the dashboard as required. Enter any final adjusted values in the RobotConst.java file.

Set the Encoder Counts/inch

Once the speed PIDs are tuned, drive the loaded robot and measure the encoder counts/inch:

  • Set the robot to a known starting position
  • Enable the robot and reset the encoder counts using the START button on the controller.
  • Drive the robot forward.
  • Measure the distance with a measuring tape
  • Divide the distance by the average EncDist displayed on the dashboard
  • Enter this value in the RobotConst.java ENCODER_COUNTS_PER_INCH and redeploy the code

Step 3: Tune the Gyro PID

Ensure the Direction of the Gyro

Rotate the robot by hand. Is the Gyro moving? Is the Gyro direction the same direction as the robot movement?

If the gyro is not reading in the same direction as the robot movement, then invert the gyro using the RobotMap.java GYRO_ISINVERTED flag and redeploy the code.

Tuning the GyroPID

Set up the straight auto pattern distance in the AutoCommand.java to the distance that is available for the robot to drive SAFELY.



Set the robot up in a space where there is sufficient room on either side for the robot to drive off course without hitting anything!!!

Be prepared to STOP the robot by pressing the space bar or enter key.

  • Adjust the GyroPID integral gain to zero
  • Set the proportional gain
  • Select the Straight auto pattern on the SmartDashboard
  • Set the Driver Station to Autonomous
  • Enable the robot

Repeat the above steps, increasing the gain until the robot is just unstable, and then select a value approximately 1/3 of the unstable value. The robot should not wiggle when it tracks the straight line. Adjust the Integral gain to approx 1/20 of the proportional gain and continue testing.

Once the robot is stable, run the straight line pattern and have someone push the back end of the robot from the side until it is out of alignment. The robot should auto-adjust to the straight line pattern. VERY COOL!

Remember to enter the final value in the RobotConst.java file DRIVE_GYRO_PID_KP.

Redeploy the code.

Test the Rotate To Angle Command

Enable the robot in Teleop and press any direction on the POV button on the controller.

The robot should rotate to that angle. Adjust the proportional gain if required.

Test the Box Pattern

Select the Auto Pattern -> box, and run the auto pattern. If the robot drives in a nice 3 ft box, you are ready to start developing a new auto pattern.