Salim & Muhammed Ø:
Developing and Optimizing PID Algorithm for Selfbalancing – Wheel
In this week have Salim and Muhammed Ø discussed and planed the development of the PID controller used in the balancing wheel. The main aim is to create a stable system using the IMU sensor to drive the motors and keep the bike stable. We will not calculate the PID values, rather will we find them through trial and error. When testing the system.
The main goal of the PID algorithm is to maintain the motorcycles side to side balance for this we have divided the process into 7 steps. These steps are meant to act as a pointer to guide us through the development of the algorithm. We will first focus on developing the algorithm while the motorcycle is stationary, this will make it easier for us to debug if something goes wrong. After successfully developing the code for the self balancing element of the system while stationary we plan to adapt it to also function while in motion.
Step one, connecting electronic components:
The first step in the process is to connect all the necessary components, these being: a micro:bit microcontroller, an Arduino microcontroller, an IMU sensor, a battery, a motor connected to the rear wheel, a motor driver to control the motors, and lastly another motor connected to the stabilator wheel. During this step it is crutial to focus on weight balance and if needed to add counter weight to ensure that the motorcycle is stable.
Step two, setting up the IMU sensor:
The second step in the process involves calibrating the IMU sensor. First we will take the readings of the IMU sensor while stationary, this is done to calculate the offset so that the readings from the sensor is “zeroed” out.
Step three, calculating the angle:
The IMU sensor consists of two main sensors these being an accelerometer and a gyroscope. These two sensors have their strengths and weaknesses, the accelerometer gives a stable but noisy estimate of the tilt angle, and the gyroscope provides smooth measurements that drift over time.
To calculate the angle in a reliable way using these two sensor we need to combine them, the way we plan to achieve this by using a complementary filter. The filter will blend the readings from both the gyroscope and the accelerometer. In practice, this will be a calculation that will be done for each loop iteration, where we will be taking the previous angle estimate, add the change in angle measured by the gyroscope, and slightly correct it using the accelerometer reading.
this can be expressed as a mathematical formula:
Angle = α (previous angle + gyro rate * delta t) + (1- α) * accelerometer angle
The tuning constant α will determine how much weight is given to the gyroscope versus the accelerometer. By adjusting α, we can control how responsive or stable the angle estimation becomes. This calculated estimate will later be used as input for PID algorithm
Step four, implementing the PID control logic:
After obtaining a stable angle reading, the next step is to implement the PID controller. The goal of PID algorithms is to minimize the difference between the desired angle, in this case being 0 and the angle readings from the IMU sensor. The PID algorithm consists of three main components, these being:
Proportional: corrects the angle based on its magnitude.
Integral: accumulates small errors over time to remove offset drift and help the system maintain a perfect balance.
Derivative: predicts future movement based on change in error and smooths the response to avoid oscillations
The output of the PID controller will be control signal that will determine how much and which direction the stabilator wheel will rotate to keep the motorcycle in balance.
Step five, motor control:
Once we have the PID output, it needs to be translated into motor control. The direction of rotation is based on the tilt of the motor, if it tilts to the right the wheel will spin left and vice versa. The magnitude of the PID output determines the motor speed required to keep the motorcycle in balance. We plan to connect the motor responsible for the self balancing to an Arduino so that it is independent from rest of the system, since we think that the algorithm will be taxing on the micro:bit hardware wise.
in addition this is the code that will in the end be responsible for controlling the motor:
pwm_s = -constrain(X1 * robot_angle + X2 * gyroZfilt + X3 * -motor_speed, -255, 255);
It may need to be changed a bit but the values from the PID algorithm is multiplied with its corresponding tuning constants before constraining it to pmw values.
Step six, testing and tuning values:
After implementing the basic PID loop, we will begin tuning the values in the PID algorithm. This involves choosing the right values for X1, X2, and X3(representing the P, I and D gains), we will determine these through trial and error. We have set up a overview of what happens if the values are too high or too low in a table:
| PID | In the code | Influence | Too High/Low values |
| proportional | X1 (Kp) | How much the system would react to angular error | Too high: Swinging too hard Too Low: Slow response, can’t balance properly |
| Derivat | X2 (Kd) | How fast the system will react to the error | Too high: The system reacts too slow and there is stiff movement of the system Too low: The system is unstable |
| Integral | X3 (Ki) | Removes small errors that always occurs, for example the motorcycle leans a little to one side | Too high: causes vibrations and instability over time. Too low: The system doesn’t compensate for small biases. |
We plan on to start with moderate values and increase or decrease based on the result the test produce. In addition to the PID values there are some other values we can determine through trial and error, these being:
Complementary filter: we can adjust the percentage of the gyroscope in the complementary filter. We plan to start with 90/10 distribution, of the gyroscope and accelerometer respectively. Increase in the gyroscope favour can increase the drift and increase in the accelerometer favour can increase the algorithms susceptibility to noise
Loop time: we can adjust the loop time of the PID algorithm, that is how often the loop is run. Base value for this would be every ten seconds, this can be taxing on the cpu of the microcontroller therefore we might increase the loop time, or potentially decrease the loop time if the system is too unresponsive.
Step seven, data monitoring and debugging:
During the test phase, we will log the angle readings, motor speed, and PID outputs over time to analyse how the system behaves. These reedings will helps us understand if the values in the PID algorithm needs to be changed or if the IMU filtering or motor control signal needs improvement. Monitoring will be done through the Arduino serial monitor, and be logged to a excel sheet.
Next week will we do some coding and discuss which of the balancing wheel fits the best and how many screws does it need.
Meron:
This week as a mechanical engineering student I wanted to show how the hydraulic brake system would been even though the bike is mostly automatic. I took the initiative to design and test hydraulic brake systems. I made the design in SolidWorks, 3 D printed the parts and put everything to gather in the workshop
Testing it in real life was very exciting and it worked well as you can see in the video below. It was a great experience to see my own design actually work.
In addition, we had discussions about how we will design the balancing wheels and weight distribution for the motorcycle. This is an important step for Phase 2 to make the bike more stable and ready for future testing.
Next, we will work on the balancing wheel system to improve the stability and control of the motorcycle.