Lego, Raspberry and Python Project – Reaction Wheel Inverted Pendulum

Single axis self-balancing reaction wheel inverted pendulum.
This thing is inherently unstable and a common challenge in control theory.
The mechanical structure is built using only Lego parts.
Motor is also Lego. Angle sensor and electronics are not Lego.
Enjoy!

00:00 platform
00:27 inertial measurement unit
02:39 motor driver
04:08 PID controller
07:10 tachometer
10:26 rise up sequence
10:57 the final result
13:06 change parts and parameters to see how they affect performance
18:32 specs (including python code)

PARTS:
Raspberry Pi Zero 2 W
OKYSTAR TB6612FNG motor driver
Elecrow SM9250MPU 9DOF IMU
Lego EV3 Medium Servo motor [45503]
Lego PF Rechargeable Battery Box [8878]
Lego Gear Rack 11 x 11 Curved [24121]

CODE:
https://mega.nz/folder/h6Y2EYwQ#t1s6ihP5UcFOJb5QGhX1Ww
or
https://www.dropbox.com/scl/fo/ak56abqycl29713epxa78/h?rlkey=fxi8j1r7rv2ijhaflwjsl77lh