This endeavour of coding and optimizing a self-balancing robot demands adeptness in signal analysis and interpretation using Matlab, alongside the capability to evaluate basic feedback systems to ensure stability. Drawing upon expertise in Python, a proportional-integral-differential controller was fine-tuned, empowering the robot to maintain equilibrium and promptly respond to external forces.

The PyBench Board

The self-balancing robot comprises a PyBoard, an accelerometer, a gyroscope, and a potentiometer to adjust the DC voltage supplied to the module, along with several other components.

The PyBench board can be utilized for various tasks unrelated to the self-balancing robot. This includes sinusoidal signal generation and spectrum analysis using Matlab and an external oscilloscope. Additionally, it was used to analyze the DC response of a non-linear system, understanding system modeling in the s-domain, and how to obtain the frequency response from the transfer function of a 'black-box' system, as well as discerning the difference between the measured and predicted frequency response of a bulb box.

Interested in learning more? Feel free to reach out...

Robot Features

The robot is powered by two DC motors connected to a 6V battery pack and controlled by an H-bridge chip (TB6612). Each motor is equipped with a hall effect sensor to detect changes in magnetic field strength. The IMU outputs can be derived to determine the pitch and roll angles of the robot. Additionally, a potentiometer controls the speed and direction of both motors. By combining these components, the speed and direction can be adjusted based on the tilt of the robot, forming the basis for enabling it to balance.

Figure 1. Writing Matlab code

Explaining PID

The final step to achieve balance in the robot is to implement a PID (Proportional-Integral-Derivative) algorithm. The pitch angle serves as the set-point variable, controlling the motors to maintain a pitch angle of zero (vertical).

The tuning of the three variables was accomplished through trial and error, testing the robot's balance each time they were adjusted. The proportional term can be likened to a desire to reach the destination as quickly as possible, but it may lead to oscillations as it 'overcompensates'. The derivative term aims to prevent the Segway from reaching the destination too rapidly by sensing a decreasing distance and adjusting based on the rate of change of error. The integral term serves to minimize steady-state errors when the pitch is at zero. Without this term, the motors would cease functioning, leading to errors once more.

Figure 2. Logbook kept during the project

Related Articles