Planning

Step 1
A **PID controller** is a feedback loop system.

The basic idea is to steer the robot towards the line, and the farther off it is, the more it corrects itself.

The process is a feedback loop as shown in the picture:

1. Compare the desired position to the current position 2. The result is stored as the "Error" 3. The PID system decides how much to steer 4. The motors steer the robot and the sensors check the position again 5. Repeat

Information

Step 2
We'll do this by giving a numeric value to the position of our cart based on where it is in relation to the line

Perfectly on the line corresponds to a position of 1000.

The further to the left our cart is, the closer position is to 0

The further to the right our cart is, the closer position is to 2000.

*Position is never greater than 2000 or less than 0!*

Planning

Step 5
We can have each sensor report how much light is reflected and get a picture of which side the line is on.

If we then take a **weighted average** of the sensor values, we get a single number representing our position.

In this case we know we know the sensor is to the left of the center position, 1000.

So we can expect the values to average out to a bit lower, around 750.

Step 7

This function needs to take a weighted average of your calibrated sensor values.

int findLine() { //call readCalibrated() //return a weighted average of the sensor values }

Check Your Work

Step 8
Turn on your robot, calibrate it, and move it back and forth across the line.

Use the Serial Monitor commands to output the position of the robot.

You'll notice that you get some weird results if you move *completely* off of the line.

Step 9

The sensor gives strange values if all of the sensors move off the line. In this case we want to set the sensor to either 2000 or 0 depending on which side of the line it slid off.

```
IF [sensor1 sees the line OR
sensor2 sees the line OR
sensor3 sees the line]
THEN
IF position > 1000
THEN lastDir = 1
ELSE
lastDir = -1
ELSE
IF lastDir == 1
position = 2000
ELSE
position = 0
```

If the car slid off to the left, position should be set to 0.

If the car slid off to the right, position should be set to 2000.

Planning

Step 11
Information

Step 12
**Error** is a measure of how far the cart is from the track. The further the cart is from the track, the larger the error.

More specifically we can say that **error** is the difference between the current position and the target position.

If the cart is to the left of the track, the error is negative. If it's to the right of the track, the error is positive.

Step 13

`int error = position - target;`

Planning

Step 14
We come to the PID controller step with an **error** which is a value from -1000 to 1000 which represents how far our robot is from the line.

After this step we want to modify the speed of our motors so that the robot will steer back towards the track.

The PID controller is a formula which will take in the error, and output an amount by which the motor speeds change

Information

Step 16
This is a good start but here is the problem. If we only have Kp we'll have something like this.

Notice how the square reacts to changing Kp

Information

Step 18
Instead of multiplying the error by Kd, we multiply the *change in error* since the last error calculation, (error - lastError)

The result off adding these two terms together gets us this:

Play around and see how quickly you can get the green rectangle to settle on the line. What happens if your Kd is too high/too low?

Step 19

That means our formula- which you will need to construct in the next step is:

```
correction = error * Kp + (error - lastError) * Kd
```

Step 20

This function will take Kp, Kd, and the error, and return a value which will be used to change the speed of our motors.

int getPID() { //Define Kp, start with around 0.1 //Define Kd, it should be at least 20 times as large as Kp //Calculate the error from position 1000; //Calculate the correction using Kp, and Kd; return correction; }

Step 21

The last step of the feedback system is to actually set the output of the motors.

The motors speed will always be a set amount, say 100, plus the correction calculated by the PID controller.

Example:

int PIDcorrection = getPID(); int leftMotorSpeed = 100 + PIDcorrection; int rightMotorSpeed = 100 - PIDcorrection;

Information

Step 22
Here is a slideshow which steps through the feedback control loop from beginning to end.

Your program should seek to emulate this controller

PID control Info

For Teachers and Schools

Teach coding to your students with MVCode Teach

MVCode offers an integrated product designed to teach coding to students.