Overview


The BalanceTable will balance a ball on a tilting platform using computer-vision (CV) and proportional-integral-derivative (PID) control. Using an Arduino, a PixyCam, and two servos, the table will analyze the ball’s position, calculate the needed corrections, and then tilt to center the ball. This project will provide a fun and easy introduction to CV and linear control that can entertain for hours.
This guide will look at the necessary components to make a BalanceTable, how it works, and how to program the camera-guided feedback loop. The PID fundamentals addressed in BalanceProp will be used here with very few changes (if a refresher in PID control is needed I suggest starting there). By using the PixyCam to find the position of the ball we can use linear PID control to minimize the error between where the ball is and where we want it to be.
Materials and Parts
Most of the structure of both the stand and the table are composed of laser-cut 1/4in plywood. The next largest contribution is from the 3D-printed parts that form most of the joints and mounts. The electronics are mainly purchased but a proto-board will still be required for an easier, sleeker, and more reliable assembly. The main electronics components are the PixyCam to perform off-board CV, the Arduino that communicates with Pixy and does math, and the servos which just move accurately and with high torque.
There are two main parts to the BalanceTable: 1) the tilting table and 2) the camera stand. To make these two parts quickly, laser-cutting was used for the larger elements while 3D-printing was used to make everything else.
The table requires nine parts to be made before its assembly: two control horns, two servo holders, and one of each the table top, base, pivot post, pivot cross, and pivot pad.
The last 3 ‘pivot’ parts all snap together to form a universal joint that will allow motion around two rotational axes but constrain all others. The pivot pad is screwed to the bottom of the table with M3 screw (or smaller) while the pivot post screws to the base. This is the primary operating component of the table and should be relatively free to pivot once installed
The servos are held in the servo holders and can be assembled before the table parts are finished. The servos have their own control horns that connect to the printed control horns via stiff wire linkages. This allows the servo and table to move together while still allowing room to move. The servo and its associated parts will be controlled the Arduino to actuate the table. It is best if the table is relatively level before starting tuning the controller. This can be fixed by adjusting the linkages.
The stand is far simpler and only require six parts, five of which are laser-cut. The DXF plans can be found below and the cut should take less than 15 minutes. Once cut, the stand can be assembled with super-glue, hot-glue, or wood glue. The PixyCam holder is the only 3D-printed part on the stand but it helps hold together the stand and hold the PixyCam steady while it is watching the ball. An extender for the PixyCam wires may be desired as the stand is a bit further away from the computer (aka Arduino) than some of the cables provided with the PixyCam can reach.
Mechanical Fabrication

















Electronic Fabrication




There are three main interactions going on in the circuit needed to make the BalanceTable: 1) the PixyCam talks to the Arduino, 2) the Arduino talks to the servos, and 3) the Arduino talks to your computer.
In the wiring diagram displayed, interactions 1 and 2 can not happen at the same time as 3 because USB ports cannot provide enough amperage to power the servos. For reprogramming, the battery should be disconnected. If issues persist, disconnect the servos as well or use a benchtop power-supply.
The PixyCam talks to the Arduino through the ICSP/SPI header on the Arduino (these are those six pins near the back edge of the Arduino that no one ever uses). Do not worry about the wiring here too much, just use the cable that came with the PixyCam. If you want to make a longer cable here is the PixyCam tutorial and the rest of the documentation.
Connecting and powering the servos requires a little finesse. The Arduino and PixyCam require 5V and can pull around 1A together at max draw, maxing out a standard USB. The servos each can take 5-7.4V and can draw around 2A each. That is more than any computer USB port can handle. To get around this problem, 5V/3A is being fed to the shield and then to the Arduino, powering both without the need for a wired connection to a computer.
The servos both take PWM signals so Arduino output with PWM capability (D5 and D6) were selected for the yellow signal leads of the servos. All of the rest of the communication between the servos and the Arduino will be implemented in the code.
Programming
The first step to controlling the position of the ball is detecting it with the PixyCam. PixyCam has really good documentation about this but I will repeat my takeaways from the process here to help anyone who might be struggling. PixyCam has a number of internal setting that you will most likely want to play around with for the best performance. This can all be accomplished through a graphical user interface/application called PixyMon. PixyMon will give you the live video footage so toy can make sure you camera is working and has all of the more advanced detection, lighting, and object signature setting. I highly suggest just plugging the PixyCam into you computer first to make sure your video is working and detection is taking place. After this, run the HelloWorld.ino example to get a feel for what Pixy is calculating.
With the PixyCam working, the next step is finding the best way of getting the ball back to the center of the image. Two PID loops, one for X one for Y, were used to minimize the error in both directions. Unlike with the BalanceProp projects, these two PID loops are independent of each other to achieve independent actuation. The controller turns out to be mainly a PD controller with very little I contribution but different setups may have different results.
The current position is where Pixy detects the ball. The desired position is the center of the image/table. This means that the error in position is just the difference of these two. Taking the integral and derivative of that error over time yields the I- and D-terms, multiplying the error by a constant give the P-term. Adding the P,- I-, and D-terms produces the output that is fed to the servos. This process is repeated for both the X- and the Y-directions at most 50 times per second (the frame rate of the PixyCam).
That is literally all that is going on. PixyCam does its thing and sends the data over ICSP to the Arduino who crunches the numbers and yells at the servos to move faster.