# Freescale smart car-steering gear and PID control

Once the robot couldn’t stand up (you must choose ALPHA1S), I asked the customer service and said that it was the steering gear, and then I knew there was something like a steering gear.

# 1 Servo

Servo: The steering mechanism of the car. That is to control the steering of the car.

## 1.1 Features of steering gear

It is characterized by compact structure, easy installation and debugging, simple control, large torque, and low cost. The main performance of the steering gear depends on the maximum torque and working speed (usually in seconds/60°). It is a position servo drive, suitable for those control systems that require constant angle changes and can be maintained. In the robot control system, the steering gear control effect is an important factor affecting performance. The steering gear can be used as the basic output actuator in the micro-electromechanical system and airplane model. Its simple control and output are worthy of the single-chip microcomputer system and easy interface with it.

## 1.2 The composition of the steering gear

Composition: rudder disc, reduction gear set, position feedback potentiometer, DC motor, control circuit, etc.

## 1.3 The working principle of the steering gear

Working principle: control signal → control circuit board → motor rotation → gear set deceleration → steering wheel rotation → position feedback potentiometer → control circuit board feedback.
(1) Input wire: red in the middle-power line Vcc; black-ground wire GND; white/orange-control signal line

(2) Signal: pwm signal, in which the pulse width is from 0.5-2.5ms (period is 20ms), and the corresponding steering wheel position is 0-180 degrees, It changes linearly.
The relationship between pwm wave pulse width and steering gear angle:

** is the pwm wave with different pulse widths, the servo will output different shaft angles. So to control the turning angle of the car, we have to control the output of pwm waves with different pulse widths. **

## 1.4 What is PWM wave?

PWM: Pulse Width Modulation

### 1.4.1 Principle of PWM wave

Principle: Control the on-off of circuit elements, so that the output terminal gets a series of pulses of equal amplitude. Zhu Bajie’s rake can look like PWM waveforms with equal pulse widths. For those who are not equal, a row of people of equal height but different fat and thin can be regarded as a PWM waveform with unequal pulse widths.
For example, here is a simple circuit:

We take 5s as a cycle. In each 5s, the switch is opened in the first 3s and the switch is closed in the last 2s, then the voltage at terminal ab will change like this:

(Control the on-off of the circuit components, so that the output terminal gets a series of pulses with equal amplitudes.)
(In this example, the input signal pulse width is 3s and the period is 5s.)
Repeat: **So to control the corner of the car, we have to control the output of pwm waves with different pulse widths.

## 1.5 What is the duty cycle?

There is a new concept-duty cycle.
Duty cycle: In a periodic phenomenon, the ratio of the duration of a certain phenomenon to the total time
For example, there is a sentence in the idiom: “fishing for three days and drying the net for two days.” If a cycle of three days is used, the duty cycle of “fishing” is one-third. So for the simple circuit shown in the figure above, its duty cycle should be 3/5.

# 2 PID control

## 2.1 What is PID control?

PID control: A regulator control law is proportional, integral, and derivative control. Among them: P: proportion (proportion), I: integral (integral), D: derivative (derivative)

In the formula, Kp is the proportional coefficient, Ti is the integral time parameter, and Td is the derivative time constant.
The significance of each parameter:
(1) Kp: proportional coefficient. Generally increasing the scale factor will speed up the response of the system.
(2) Ti: integral time constant. Generally, integral control is usually used in conjunction with proportional control or proportional-derivative control to form PI or PID control. Increasing the integral time constant (the integral becomes weaker) is conducive to small overshoot, reduces oscillation, and makes the system more stable, but at the same time, it is necessary to extend the time for the system to eliminate static errors. Too small integral time constant will reduce the stability of the system and increase the number of oscillations of the system.
(3) Td: Differential time constant. Generally, differential control is combined with proportional control and proportional integral control to form PD or PID control. Differential control can improve the dynamic characteristics of the system.

## 2.2 PID control method

There are two commonly used PID control methods: incremental PID and positional PID.

### 2.2.1 Incremental PID

1. Incremental PID
The so-called increment is the difference between the current control value and the last control value. Incremental PID is a control algorithm for PID control of the increment of the control quantity.
formula:

(Description: Kp->P, Ki->I, Kd->D, e array ->error array,
e[n]->this time difference, e[n-1]->last time difference, e[n-2]->last time difference)
For example, incremental PID can be applied to motors.
Assume that the pwm value of the current motor PID is 5000 (the accuracy is 10000, that is, the duty cycle at this time is 50%). The corresponding speed is 100r/s.
The program sends out a command to request the pwm output to be 0, that is, to stop. (Some people may have questions why not directly program pwm to 0. This is also a method, but due to the existence of inertia, the car will stop after a period of time.)
At this time, we can use PID control method to achieve.
We define several variables in the program:

int speed_now=100; //The speed at the moment
int speed_want=0; //desired output speed
int pwm_duty=0; //this pwm output value
float P=100,I=20,D=2; //P I D value
float error_pre_pre=0; //Last last difference
float error_pre=0.0; //Last difference
float error=0.0; //This time the difference

According to the formula, we write the program:
void PID()
{
/*
Incremental PID
P=Kp*(error-error_pre);
D=Kd*(error-2*error_pre+error_pre_pre);
I=Ki*error;
Pwm+=P+I+D;
*/
error=speed_want-speed_now; //speed_now can be obtained by the encoder, etc.
pwm_duty+=(int)(P*(error-error_pre)+I*error+D*(error-2*error_pre+error_pre_pre));
//Pay attention to the plus sign above, the plus sign is the embodiment of incremental PID. We perform PID control on the increment (that is, the formula on the right).
error_pre_pre=error_pre;
error_pre=error;
}


When the function runs for the first time, the output pwm is:

The motor gave a reversal force, and the cart was resisted from moving forward, so it could stop quickly.

(1) Excellent:
① There is no need to add up in the formula. The determination of the control increment Δu(k) is only related to the last 3 sampling values, and it is easy to obtain a better control effect through weighting processing;
②The computer only outputs the control increment each time, that is, the amount of change corresponding to the position of the actuator, so when the machine fails, the impact range is small and will not seriously affect the production process;
③The impact is small when switching between manual and automatic. When the control is switched from manual to automatic, it can be switched without disturbance.
(2) Deficiency: Need to memorize the control quantity.

### 2.2.2 Positional PID

2. Positional PID
Different from the incremental type, the position type PID does not need to memorize the control quantity, and directly calculates the deviation value to obtain the desired pwm.
formula:

(Description: Kp->P, Ki->I, Kd->D, ek->this time error, ek-1->last time error)
For example, positional PID can be applied to the steering gear. Because the current pwm output value of the servo is not related to the previous pwm output value, what the servo needs is to quickly turn to a certain angle.
Suppose that when the steering gear pwm outputs 1000, the steering wheel axis is 90°, when the pwm outputs 0, the rudder wheel axis is 0°, and when the pwm outputs 2000, the rudder wheel axis is 180°.
Now the pwm output of the servo is 1500, we need to turn the rudder to the middle.
Here we use PD control, that is, the I value is 0 (I is the integral of the deviation, that is, the sum of the deviation. When we tested the steering gear control of the car, we found that the I value can be omitted, and the PD control is sufficient. Of course, specific needs It is not necessary for item I to be analyzed and verified in practice).
Code:

pwm_duty=(int)(P*error+D*(error-error_pre); //The red part means this is a positional PID control


Feedback system:

0