Geek my driving
My robot turns when I drive it in autonomous or in driver mode. What can I do to help it drive straight?One answer to this problem is to use the gyro to help control the robot direction. This article describes how to do this. The language I will use is the Android Block language.
Robot Hardware
Here is a picture of the robot that I will control:The key parts you will need on your robot is two drive motors and the integrating Gyro.
Robot Software
The block programing language to initialize the software is shown in the diagram below.The motors are setup to command a speed using the RUN_USING_ENCODER mode. This command is optional. Steering using the gyro works with or without encoders.
The gyro needs to be calibrated in the initialize section of the software. When calibration is running it is important not to move the robot.
The variables turnRate, turnRateOld1 and turnRateOld2 are initialized to zero. These variables will be used later on.
The block programming language to calculate the turn command and to drive the motors is shown in the diagram below.
One while loop runs as long as the drive opmod is active.
The calculations used to control the direction are described here:
- turnRate - The gyro block reads in the gyro rate and stores it in the variable turnRate.
- turnCmd - The turnCmd variable is the commanded speed the robot should turn. This is calculated by subtracting the desired turn rate (the left stick x position LeftStickX) from the measured turn rate (turnRate). In an ideal world the error between these two signals is zero. To control the robot motors to move the robot at this rate this error term is multiplied by a gain. The higher the gain number, the smaller the error. This type of control is called a proportional controller.
- turnRateOld1 / turnRateOld2 - Store off the past values of the turn rate.
- m1Cmd - The motor 1 command is the turn command subtracted from the speed command (The left stick y position LeftStickY).
- m2Cmd - The motor 2 command is the turn command added to the the speed command (The left stick y position LeftStickY).
- m1 / m2 - It is important to limit the size of the commands sent into the motor. The constraint block is set to limit the command to between -1 and 1.
Tuning the proportional controller.
STEP 1 - Adjust the gyro turn rate gain. The gyro turn rate number is 80000 in the example above. (turnCmd = js + (turnRate + turnRateOld1 + turnRateOld2)/8000). To set this number send turnCmd out on telemetry. When your robot is running, pick it up and rotate it as fast as you would like the robot to turn. The number you see in telemetry should be between 0.3 and 0.7 (or -0.3 and -0.7). Adjust the number 80000 until you see a turnCmd number in this range.STEP 2 - Adjust the proportional control gain. In the above example, the proportional gain is 1. This seems to work well for most robots. If you desire better control, increase the gain number until the robot begins to oscillate left and right. Decrease the number until the oscillations stop. In the following example, the gain was increased to 3.
FAQ - My controller does not work at all. When I start driving the robot just turns around in a circle and never stops. Why is this?
Answer - The controller needs to generate an error signal. If the gyro rate is commanding the same direction as the joystick, a positive feedback will happen. A positive feedback signal results in an unstable system. We need negative feedback to keep the system stable. To fix this, just change the sign of the turnCmd calculation. Change (turnCmd = js + (turnRate + turnRateOld1 + turnRateOld2)/8000) to (turnCmd = js - (turnRate + turnRateOld1 + turnRateOld2)/8000). Notice the change in the sign after js from + to -. In the following example, the sign was changed to a negative sign.
FAQ - My controller is sluggish. It does not seem to track very well. How do I fix it?
Answer - This is the result of a proportional gain that is to small. Change (turnCmd = js + (turnRate + turnRateOld1 + turnRateOld2)/8000) to (turnCmd = (js + (turnRate + turnRateOld1 + turnRateOld2)/8000)*3). Adjust the gain (3 in the previous example) to a larger number to have tighter control. Adjust the gain to a smaller number if the control begins to oscillate back and forth.
FAQ - My robot does not turn very fast or it turns way to fast. How do I adjust the turn speed?
Answer - This is the result the gain used to match the gyro turn rate with the joystick. To set this number send turnCmd out on telemetry. When your robot is running, pick it up and rotate it as fast as you would like the robot to turn. The number you see in telemetry should be between 0.3 and 0.7.
In the calculation (turnCmd = js + (turnRate + turnRateOld1 + turnRateOld2)/8000) adjust the number 80000 up and down until you get a telemetry number in this range.
FAQ - My controller oscillates back and forth. What should I do?
Answer - the is is a result of a proportional gain that is to large. The proportional gain is the number 3 in the following example: (turnCmd = (js + (turnRate + turnRateOld1 + turnRateOld2)/8000)*3). Keep reducing this number until the oscillation goes away. For example, 2, 1, .5, .25, .1.