LEGO Reaction Wheel Inverted Pendulum

By Phil King. Posted

Keeping an inverted pendulum aloft is a popular challenge in control theory – which deals with the behaviour of dynamical systems – and requires precise motion control to prevent it from falling over. It’s something maker and YouTuber Juha, of the Brick Experiment Channel, learned about in courses he did 20 years ago, which inspired him to try it with LEGO and Raspberry Pi.

Made of LEGO bricks, his inverted pendulum requires active control to stay upright. A ‘reaction wheel’, also made from LEGO, is mounted on the pendulum and attached to a motor controlled by a Raspberry Pi Zero 2 W. “Rotating the wheel will generate torque on the pendulum and thereby change the pendulum angle,” he explains. “The rest is just measurement and computation.”

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.

“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.”

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.”


https://hsmag.cc

From The MagPi store

Subscribe

Subscribe to the newsletter

Get every issue delivered directly to your inbox and keep up to date with the latest news, offers, events, and more.