# LEGO Reaction Wheel Inverted Pendulum

- Werbung -

Well, that’s the theory, but in practice it’s not quite so easy and, as shown in his YouTube video, Juha had to alter his Python code repeatedly and make hardware adjustments to get the system to work to his satisfaction.

## Precision control

A gyroscope and accelerometer on a mini IMU (inertia measurement unit) board are used to measure the pendulum angle, while Raspberry Pi runs a control loop for filtering data and calculating PID (proportional–integral–derivative) controller outputs for adjusting the motor’s speed and direction. Automatically calculating corrections based on feedback, PID is one of the most common control methods used in industrial and mechanical applications, such as in a car’s cruise control system. It’s also fairly easy to implement.

- Werbung -
- Werbung -

“Others have used LQR [linear–quadratic regulator] control for inverted pendulums,” notes Juha, “but it looks too mathematical and difficult for me. As for tuning the PID parameters, I didn’t have any approach other than ‘try and see.’ It got good laughs in the YouTube comment section as it looks so unprofessional.”

While Juha opted to use a Raspberry Pi Zero 2 W for its fast bootup speed, “CPU load was only 5% running the control loop with a 1 ms interval, so it would work with a much less capable board.” He even tried using a Pico, which “worked OK in terms of processing power, but then I realised I need to store tens of megabytes of log data for drawing nice graphs for the video.”

Amazingly, before this project, Juha had never used a Raspberry Pi before and had minimal experience with electronics. “I had to figure out how GPIO works, what are pull-down and pull-up resistors, how I2C works, etc.”

- Werbung -

## Keeping it up

The next major challenge was getting the pendulum to stay upright for more than two seconds. “The problem was with the reaction wheel top speed limitation,” says Juha. “There is a short time window for acceleration before the limit is reached, so you need to get past the top equilibrium point before that. A plain PID controller would just minimise angle error and keep the wheel rotating too fast. I read many studies of different inverted pendulums, until I found one paper that mentioned continuously changing the target angle for the PID controller. That solved it finally.”

So, in the finished system, is it impossible to push the pendulum off balance so that it falls over? “No, not at all,” replies Juha. “It will easily fall over if you push it. The controller will immediately try to compensate for the push by accelerating the wheel, but it can correct only for small errors. With a more powerful motor, higher top speed, and higher rotational inertia for the wheel, it would resist stronger pushes.”

- Werbung -

- Werbung -