The microcontroller is programmed using C programming language. In this project, we used UVision Keil Integrated development environment to debug, build and compile the code.
At startup, the following are the modules configured:
A. UART
B. TIMER1A
C. TIMER 0A
D. GPIO and PWM
E. I2C
The Universal asynchronous receive transmit module is used to analyze and monitor the data generated by the sensor. To balance the robot, the coefficients of the PID controller where changed by monitoring the sensor values.
This timer is configured as a one-shot timer clocked at 40 MHz. This timer is used for multiple functions:
1. LED status
2. Initialize PWM
3. Update PID coefficient
4. Enable TIMER 0A
The LED is used display the status of the robot. At startup, the red LED turn on which states the system is on and ready. When the green LED is on, the system starts to gather data from the IMU and balance the robot to its ideal position. In addition, the timer is used to enable the PWM generators. The data from the sensor is then sent to the filter and PID Controller to update the coefficients of PID in accordance to the leaning angle of the robot. Further TIMER 0A is initialized for to get the processed data from the feedback control system.
This timer is configured as a periodic timer set at 80000 step clocked at 40 MHz. This timer is used for following functions:
1. MPU data read
2. Calculate angle
3. Complimentary Filter
4. PID Controller
5. Generate PWM signal
As discussed earlier, the data from the MPU is collected into FIFO of I2C. The leaning angle of the robot is calculated using combination of data from accelerometer and gyroscope. The following function calculates the leaning angle:
Complementary filter[2]: The gyroscope values provide a good dynamic indication of the tilt, whereas the accelerometer provide a good static indication of the current orientation. Moreover, gyroscope shows the degree-per-second change, and with the accelerometer values, the current angle is calculated.
To get a reliable angle measurement, both values are used in this simple equation angle change =
(1- alpha)*(angle + gyro*dt) + (alpha)*acc
where alpha is how much percentage of gyro/accel value affects the angle measurement. Typically, alpha is about 0.98 which means gyroscope has more weight compared to accelerometer when calculating the change in angle. dt is the sampling frequency or how often we sample the data.
The values from the complementary filter are fed to the PID Controller which then calculates the speed of the robot based on leaning angle and feedback values.
The calculated speed is given to the motor function which generates the PWM signal as stated below:
The PID controller stands for Proportional, Integral and differential controller. This PID controller [1] generates an error based on 3 coefficients
1. Proportional
2. Integral
3. Differential
Proportional:
The proportional component depends only on the difference between the set point and the process variable. The difference between these terms are referred as the error from the proportional component. The proportional gain (Kp) determines the ratio of output response to the error signal.
Integral:
The integral sums the error over time. Hence the integral response is dependent upon time. The integral response will continually increase over time unless the error is zero, so the effect is to drive the Steady-State error to zero. The steady state error is the final difference between the set point and the process variable.
Derivative:
If the process variable is increasing rapidly, the derivate component tries to bring the error to zero. The response of this component is proportional to rate of change of process variable. In this project we have used small coefficient for derivative component to reduce the oscillations of the robot.