Closed Loop Motor Control

Two closed-loop methods were attempted to allow the robot to move straight. The first was using the pulse width methods which a discrete time Proportional–Integral–Derivative (PID) was used, and the second using the count ticks method which an analog PID was used.

Speed VS RPM Graph

The RPM of each motor is found using an interrupt program which measures the pulse width of the encoder square wave. “PW” refers to the time it takes for the square wave to stay high. In theory, the time it stays low is same as the time which the square wave stay high. Therefore, the period “TW” can be found by using the formula 2 * PW.

To convert the measured pulse width to RPM, the following steps are needed:

  1. Measure the time which the pulse width stay high
  2. Find the period using the formula 2*PW.
  3. The unit of the period is in microseconds. Hence, there is a need to take (2*PW) and multiply by 10-6 to convert this timing to seconds.
  4. Next, we want the revolution in minutes. To convert seconds to minutes, there is a need to divide the period in second by 60 i.e. 
  5. To find the frequency of the pulse width in 1 minute, use the formula,.
  6. Finally, using the relations of 562.25 square wave per revolution, divide the frequency by 562.25.

The complete formula is:

The equation is then simplified to   for faster computation in Arduino.

Using the formula derived, the Speed VS RPM graph was plotted for the left and right motor. The speed was tested using a step size of 50 starting with speed 50 and ending with the maximum speed 400. The “y” equation shown in the graph can be used to approximate the speed needed to achieve the desired RPM.

The RPM VS Speed graph is shown below:

Next, the step test was conducted on each motor to find their characteristics. The step test graph of the right and left motors is shown below:

The step response of the respective motor is measured by placing the robot in the air:

  1. Set the speed to 250 and wait for the motor to reach the desired speed.
  2. Measure the pulse width for the speed 250 every 5ms.
  3. Let the motor remain at the speed of 250 for 0.5 seconds.
  4. Change the motor speed to 300 after 0.5 seconds immediately and let the motor run for another 0.5second.
  5. Measure the pulse width for the speed 300 every 5ms.
  6. Logged all the data to an excel sheet and use the pulse width to RPM conversion formula.
  7. Plot the RPM of the motor against time.
  8. The characteristic of the motors can be obtained from the graph by drawing a tangent and using the Ziegler-Nichols Open-Loop tuning method.
  9. After obtaining the settling time and time difference from the motor, the discrete time PID can be obtained using the formula shown in the table below.

Ticks Method

The left and right motor’s ticks are measured using an interrupt. The right motor is used as the reference and hence the error is calculated using the difference between the right ticks and the left ticks. As the robot travels, the error becomes negative and visually it is observed that the robot is moving towards the left side as the right motor is faster.

To ensure that the robot moves straight, the error of the ticks between the right and the left motor must be equal to 0. Proportional Response kp was added to the code. After a few trial and errors, the value of kp was found to be 25. Using kp value of 25, the error is now 0.

Although the error is now 0, it was observed that the robot is not moving in the intended path but moving straight in a deviated starting angle as shown in the diagram below.

Integral Response ki, was added to the code to eliminate the steady-state error of the motor. After adding in this component, the robot is able to move very straight as expected. Lastly, the optional derivative component kd is also added to the system.