/Two-Wheels-Inverted-Pendulum-with-Linear-Quadratic-Integral-Control-

Code to a TWIP (Self Balancing Robot) based on WEMOS R1 D32 (ESP32) board, using tb6612fng H-Bridge, jgb37-520b (178 RPM, 12V) DC motor and Hall Magnetic Encoder, MPU6050. It uses 2 controllers, a LQR to rotation control and a LQI to stability and linear position control.

Primary LanguageC++

Two-Wheels-Inverted-Pendulum-with-Linear-Quadratic-Integral-Control

Code to a TWIP (Self Balancing Robot) based on WEMOS R1 D32 (ESP32) board, using tb6612fng H-Bridge, jgb37-520b (178 RPM, 12V) DC motor and Hall Magnetic Encoder, MPU6050. It uses 2 controllers, a LQR to rotation control and a LQI to stability and linear position control.

The code was done in platformIO + Vscode, but you can just rename "src" folder to "main" and rename "main.cpp" to "main.ino", and run it on arduino IDE.

I use this robot, from aliexpress: Self Balancing Robot Aliexpress

But, of course, i replace the Uno Board to a Wemos D1 R32 (Esp32).

I did a controller (joystick) using HTML, so you just need start the robot, see the IP and put it on a Web Browser. I did too the same in python.

Joystick html

Joystick Python

Has you can see in the above images, we can control the robot with the joytick and sending desired positions to the robot. Furthemore, desired position have some limitations (40cm to linear position and 120 degrees to z rotation), and we just can use it when the robot is in initial position, because if, for example, do you do a full rotation using joystick (360 degrees) and after it you write 30 degrees do the desired position rotation, what the controller will see is 360-30 = 330 degrees,and that drives the robot out of balance due to physical limitations. In general purposes, the desired positions option may be just used to see control demos. In all other cases, is most recommendable use the joystick one.

Many thanks to @HeavyMSV for helping me throughout the development of this project, especially in the html and python controllers