How can we optimize the line following algorithm?

Usually an algorithm based on if-then conditions isn’t very accurate. We can consider that kind of algorithm a Bang-Bang algorithm.

The red lines represent the bang lines. As you can see, the robot’s trajectory, marked with green, isn’t very accurate.

How can we improve the trajectory? We can write an algorithm which uses some kind of controller.

**P**- proportional controller**I**- integral controller**D**- derivative controller**PID**- proportional-integral-derivative controller

Notations used in images below:

**P**represents the P controller.**I**represents the I controller.**D**represents the D controller.**T**represents the target.**R(s)**represents the reference value.**E(s)**represents the error value.**U(s)**represents the controller's output.**Y(s)**represents the system's response.

- Advantages:
- It’s one of the most often used control loops.
- The controller’s output is directly proportional to the error.
- Good results for most applications where the functioning parameters are not critical (overshoot, stability, etc.)

- Drawbacks:
- It takes a long time to stabilize.
- The system can become unstable for unsuitable constant values (i.e controlled variables).

- It’s used to ensure long-term system precision.
- It’s almost always used together with P controller.
- The controller sums up the errors until it reaches a reference value.
- A pure integral control system is very slow and can sometimes determine system oscillations.

- If P controls the present state of the system and I controls the past state of the system ⇒ D controls the future behavior of our system.
- We cannot stabilize the system if we don’t know how it will behave. The D controller monitors the error changing rate of the system.
- The D controller can be considered the most powerful but also the most problematic controller, because the system becomes unstable to noise and sampling issues.
- It’s very useful in fine tuning a system, and it can reduce the overshoot in P controlled systems.

- It uses all the control types mentioned before.
- It is very stable and has a good response rate
- If the PID parameters are incorrect, the system can become unstable.

How can we add PID control to our robot?

In our particular case:

unsigned int position = read_line(sensors,IR_EMITTERS_ON); /* * The "proportional" term should be 0 if the middle sensor is above the line. */ int proportional = ((int)position) - 2000; /* * Compute the derivative (change) and integral (sum) of the position. */ int derivative = proportional - last_proportional; integral += proportional; /* * Remember the last position. */ last_proportional = proportional; int power_difference = proportional/20 + integral/10000 + derivative*3/2; /* * Compute the actual motor settings. * We never set either motor to a negative value. */ const int max = 100; /* sets the maximum speed */ if(power_difference > max) power_difference = max; if(power_difference < -max) power_difference = -max; if(power_difference < 0) set_motors(max+power_difference, max); else set_motors(max, max-power_difference);

Values **1/20**, **1/10000**, **3/2** represent parameters which determines how the robot reacts to the line.
Those are the values which need to tuned.

More documentation can be found here: http://www.pololu.com/docs/0J21/7.c

- Embedded Systems courses 7 and 8, PhD Tudose Dan