== Closed Loop Control [[Image(walkTheLine.jpg,right,250px,margin=10)]]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 [wiki:ControlSystems/SoftwareTeam/Training/GettingStarted/ClosedLoopControl/PWMCode Robot.java] (Macadamia) * CAN [wiki:ControlSystems/SoftwareTeam/Training/GettingStarted/ClosedLoopControl/CANCode Robot.java] (Hazelnut, Almond) 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 difference (error) measured between the encders (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. === TalonSRX Closed Loop Control === Although closed loop control can be implemented using the RoboRIO, the task can also be offloaded to a smart motor controller such as the TalonSRX; this can make the control smoother and leaves the RoboRIO free to focus on other tasks. You can read about closed loop control using the TalonSRX [https://phoenix-documentation.readthedocs.io/en/latest/ch16_ClosedLoop.html here] === 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!