Version 9 (modified by 6 years ago) (diff) | ,
---|
Closed Loop Control
In the last example, we saw why robot programming is challenging: robots work in the real world where wheels slip, motors have different powers, wheels aren't inflated equally, and floors aren't perfectly level. The robot didn't go straight and didn't stop at exactly 36"; traveling in a straight line is more difficult than it sounds. In the basic Autonomous example, one wheel was turning faster than the other so the robot turned. Since the encoders were reporting this, we can modify our program to compensate. The simplest way to do this is to proportionally increase the motor power of the wheel that's going slower / reduce the power on the wheel that's going faster.
Create a new project named ClosedLoopControl and replace the default Robot.java with:
- PWM Robot.java
- CAN Robot.java
- Add the following imports:
import edu.wpi.first.wpilibj.Timer;
- Add variables for closed loop control in the Robot class
private double leftPower, rightPower; private Timer autoTimer; private boolean autoDone;
- In robotInit() instantiate the new Timer object
autoTimer = new Timer();
- In robotPeriodic() add the motor power settings to the smart dashboard
SmartDashboard.putNumber("Left power", leftPower); SmartDashboard.putNumber("Right power", rightPower);
- In autonomousInit() initialize the motor powers and start the timer we created
so we can periodically adjust the motor power to keep the robot going straight:
// start motors turning forward at 20% power leftPower = 0.20; leftMotor.set(leftPower); // to go forward, left motor turns clockwise, right motor counter-clockwise rightPower = -0.20; rightMotor.set(rightPower); autoTimer.start(); autoDone = false;
- In the default section of autonomousPeriodic() periodically adjust the motor powers
and stop if we've gone the desired distance.
if (!autoDone) { // adjust motor power every 1/4 second to keep us going straight if (autoTimer.hasPeriodPassed(0.25)) { // if we're going straight, leftDistance == rightDistance // otherwise, compute the error and adjust the motor powers to minimize it double error = leftEncoder.getDistance() - rightEncoder.getDistance(); double kP = 0.01; // proportion of error used to correct motor power // adjust motor power to try to keep left/right distances same leftPower -= kP*error; // positive error means left is going too fast rightPower -= kP*error; // right motor spins opposite direction of left leftMotor.set(leftPower); rightMotor.set(rightPower); } // For PWM Motor Controllers uncomment the following 2 lines //if ((leftEncoder.getDistance() > 36.0) || // (rightEncoder.getDistance() > 36.0)) { // For CAN motor controllers uncomment the following 4 lines //int l_raw = leftMotor.getSelectedSensorPosition(); //int r_raw = rightMotor.getSelectedSensorPosition(); //if ((raw2inches(l_raw, 360*2, 7.5) > 36.0) || // (raw2inches(r_raw, 360*2, 7.5) > 36.0)) { leftMotor.stopMotor(); rightMotor.stopMotor(); autoDone = true; } }
This is the simplest form of closed-loop control: we are sensing position information (left, right distances) and adjusting powers proportionally (kP) to try to minimize the error. There are more sophisticated ways to do this that consider how quickly the robot is approaching its goal (derivative of error) and how long and how severely it has been off its target (integral of error). The PID control loop uses all of these for more precise and responsive closed loop control.
Notice that there are two critical values in this control loop:
- How often the control loop runs (every 0.25 seconds in the above example)
- How much the control loop adjusts the power based on the sensed inputs (kP=0.01 in the above example)
- Try printing the error and adjusted motor powers using System.out.println(...) to see what adjustments are taking place.
Extra Credit
Try experimenting with the control values (carefully and changing them a little at a time) to see what effect they have. Be prepared to hit the Disable button or space bar when testing!
Attachments (1)
-
walkTheLine.jpg (38.4 KB) - added by 6 years ago.
walk a straight line
Download all attachments as: .zip