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:
- Measure the time which the pulse width stay high
- Find the period using the formula 2*PW.
- 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.
- 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.
- To find the frequency of the pulse width in 1 minute, use the formula,.
- Finally, using the relations of 562.25 square wave per revolution, divide the frequency by 562.25.
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:
- Set the speed to 250 and wait for the motor to reach the desired speed.
- Measure the pulse width for the speed 250 every 5ms.
- Let the motor remain at the speed of 250 for 0.5 seconds.
- Change the motor speed to 300 after 0.5 seconds immediately and let the motor run for another 0.5second.
- Measure the pulse width for the speed 300 every 5ms.
- Logged all the data to an excel sheet and use the pulse width to RPM conversion formula.
- Plot the RPM of the motor against time.
- The characteristic of the motors can be obtained from the graph by drawing a tangent and using the Ziegler-Nichols Open-Loop tuning method.
- 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.