Balancing Ball

I don't have a good name for this project, but it's also not an original idea. Many people have created this before and posted it on youtube, I just wanted to build one because it looked cool and difficult. 

I originally set out to build a cool project with PID control, but didn't have any idea what it would be. PID is an algorithm used to accurately control complex systems, like the speed of a car or the temperature of an oven. In a naive approach, you could just set your output (like a voltage to a motor or heating element) to what you think will achieve the desired result. But external factors cause this approach to be quite inaccurate, unless you have very good knowledge of your system. It's much easier to control if you can read back the actual state of your system (feedback), and compare that to where you want it to be (the "setpoint"). That's where PID comes in. It is an algorithm for accurately tracking a desired output by constantly comparing the actual state to the desired state, and making output adjustments from there.

I won't fully dive into how PID works, since there are many good resources about it online. Here's one that I think is particularly useful.

 

PID is an incredibly powerful algorithm, and it can be cleverly applied to build things that seem like magic. In college we used it to levitate a steel ball with an electromagnet. You can get results that seem, to an outside observer, much more difficult to achieve than they actually are. Everything just emerges from the algorithm. I don't mean to say that making a PID controller isn't hard, it's just not hard in the way that you'd think. All you really need is a way to actuate your output, and a way to measure your current state. I've been thinking about systems like this, where the behavior naturally arises from simple rules or constraints. Conway's game of life is a good example. You can get some crazy complex patterns, but everything derives from a simple set of rules. What other systems fall into this category?

The hardest part of this project was tuning the PID controller. I spent hours tuning it and I still feel like it's not right. I tried many different tuning methods with limited success; it's unfortunate that there wasn't great information on this topic online.

You can check out the arduino code for this project on Github. I designed and 3D printed all the blue plastic parts, and purchased servo motors and servo linkages to control the angles. There is a universal joint connecting the plate to its base, allowing it to rotate in two axes. Feedback is sensed using a resistive touchscreen.