4. Robot Motion Control (PWM and PID)


1. Overview

In order to success in the exploration and fastest path competition, an accurate motion control of the robot is very necessary. Even though errors always exist no matter how hard to config the robot, how to decrease the errors or to compensate the errors still play significant roles in our project.

In our project, two DC motors are required to run in the same speed to make sure the robot moves in a straight line. However, open-loop control with only PWM is not easy to tune and the errors could not be compensate by feedbacks of encoders on the DC motors. As a result, it is necessary to adopt this encoder counts as feedbacks to create a close-loop control for more accurate and stable rotational speed. PID control is one of this close-loop control, which is widely used in industry.


2. PWM Control

Instead of varying the voltage level, PWM (Pulse-width modulation) is used to  to control the rotational speed of DC Motor. The basic idea of PWM is that it uses a square wave with a fix voltage value for logic 1 and 0 but different duty cycles to control the speed.

The duty cycle is defined as proportion of time for logic 1 in one period to the whole period of the square wave. Only when the PWM signal is in logic 1 state, the DC motor would perform rotation. In addition, the duty cycle reflects how often the PWM signal is in logic 1 state. As a result, the rotational speed is proportional to how high the duty cycle is. Figure 1 below shows multiple PWM signals with different duty cycles.

However, duty cycle is not the only factor to decide rotational speed. The speed upper limit of DC Motor is proportional to the voltage value of logic 1. This voltage value also has impact on the rotation speed when the duty cycle is not 100%.

duty cycles

Figure 1: Multiple PWM Signals with Different Duty Cycles.


3. DC Motor

The metal gear-motor provided is shown in Figure 2 below. It has a quadrature encoder on the right side shown in Figure 2, which could be used to track how many degrees has the DC motor rotates. For open-loop control, this encoder is only used to calculate the DC motor’s rotation speed.

DC Motor

Figure 2: DC Motor


4. Open-loop Control

If the motion control only uses PWM, the robot would never know how fast the motor really rotates. In our project, two DC motors are fixed on the robot to rotate the wheels, so the two motors must run at the same speed for the robot to move in a straight line. However, two motors may have difference in response time, friction of wheel, and other manufacturing differences.  In order to visually illustrate this, a plot in Figure 3 below generated by Matlab reflects the rotation speeds for two DC motors with the same PWM duty cycle value.

open_loop_controlFigure 3:  Rotation Speeds for two DC Motors with the same PWM Duty Cycle Value

Inside Figure 3, the y axis depicts speeds measured by encoders and the scale is encoder count per millisecond. The x axis describes time in seconds. It is quite obvious that the speed difference is quite large and the accumulated speed differences would finally lead the robot to deviate from its original direction.

A close-loop control is needed in order to keep the robot not deviate this much from its original direction. This is demonstrated in PID control.


5. Encoders on DC Motor

There is an integrated quadrature encoder on each of the DC motors on the robot. This encoders could be used to monitor how many degrees the DC motor has rotated. The quadrature encoder is shown in Figure 1 below.

encoders

Figure 1: Quadrature Encoder on the DC Motor

The basic idea of the encoder is that it contains two signals A and B. Each of signal could be viewed as clock and the other could be viewed as data. At the positive edge of the clock signal, if the data signal is high, the DC motor is rotating in forward direction. If the data signal is low, the DC motor is rotating in backward direction. This positive edge of the clock signal could be programmed as an interrupt inside Arduino. Then a global counter is maintained for each motor to increase by one for forward rotation and decrease by one for backward rotation. As a result, the value of this counter reflects how many degrees the DC motor has rotated. Then a system clock could be used to monitor how many encoder counts the motor has rotated in one clock so as to calculate the rotational speed. This rotational speed found be used as the feedback for our close-loop control.


6. Close-loop and PID Control

Close-loop Control is to get feedbacks from multiple sensors and then adjust the input signal accordingly to finally achieve that the sensor feedbacks would become stable at a certain preset target value. It could provide with high performance control for more stable and fast response of the system.

PID Control is to calculate an error value as the difference between a measured variable and a desired set-point. Then this error is going to be adjusted proportionally, integrally and derivatively to compensate for the input signal. After an oscillation of the measured variables, the measured variable would finally become stable and reach the desired set-point.

A control block diagram of our project is shown in the Figure 2 below.

block diagram

Figure 2: Block Diagram of PID control


7. Implementations of PID Control

After tuning of PID parameters, a new plot in Figure 3 below generated by Matlab reflects the rotation speeds for two DC motors with PID control.

close_loop_control

Figure 3: Rotation Speeds for two DC Motors with PID control.

Inside Figure 3, the y axis depicts speeds measured by encoders and the scale is encoder count per millisecond. The x axis describes time in seconds. From the plot, it could be seen that the PID control would provide a better performance than the open-loop control.