Ball Balancer
Several months prior to this project I created this ball balancer. It's able to balance a 1.5" 3D printed ball on an 8" x 8" platform. I decided to revisit this project to make an even better ball balancer. This iteration is quieter, less bulky, easier to build, and more precise. Not only can this version balance a ball but it can also move the ball around in different patterns. In this instructable, I'll walk you through the step-by-step process of making a ball-balancing robot.
Supplies
Tools
- needle nose pliers
- wire cutters
- wire strippers
- 2.5mm allen wrench
- soldering iron
- exacto knife
- hot glue gun
Electronics
- Teensy 4.1 Microcontroller
- Nema 17 59 Ncm Stepper Motors (Bipolar)
- TMC2208 Stepper Motor Drivers
- Mini Protoboard + Screw Terminals
- 100uF Capacitor 35V
- Male and Female Header Pins
- 30V Bench Power Supply (or any 24V power supply)
- 22 AWG Wire
- 5V Regulator
General Parts
3D Print the Parts
Start by printing the parts. There are a total of 9 different parts which go as follows:
- base_stand
- base_plate
- spacer (x3)
- link_#1 (x3)
- link_#2 (x3)
- platform_frame
- retainer_clip (x4)
- slot_insert_1 (x3 optional)
- slot_insert_2 (x3 optional)
I printed all of these parts in PLA filament. I'd recommend using the following printer settings:
- 20% infill
- Auto-generated supports
- PLA (210° extruder and 45° bed)
The filament usage is about 310g. It took a cumulative 25 hours to print everything.
Add the Heated Inserts
Use a hot soldering iron to press four heated inserts into the base stand and three heated inserts into the base plate.
Create the Circuit
Follow the schematic to build the circuit. I designated one protoboard for the teensy microcontroller and the other protoboard for the 3 stepper motor drivers. I'd recommend using header pins to allow the teensy and motor drivers to be removable.
Power
- Set your bench power supply to 24V or use an alternative 24V power supply.
5V Regulator
- The teensy 4.1 is powered by 5V. The regulator steps down the 24V from the power supply to 5V.
- Connect the positive and negative terminals of your power supply to the IN+ and GND pins respectively. The VO+ pin outputs 5V once you adjust the voltage on the regulator. Adjust this voltage by turning the potentiometer and measuring the voltage with a voltmeter.
Teensy 4.1 Microcontroller
- The drivers and the resistive touchpad connect to the teensy 4.1 microcontroller.
- You must cut the 5V trace on the teensy. This trace connects the USB 5V to the teensy 5V. We will power the teensy with the bench power supply using the 5V regulator. Refer to the images to see the location of this trace.
TMC2208 Stepper Motor Drivers
- The resistive touchpad has 4 pins that connect to the teensy analog pins A0, A1, A2, and A3. Refer to the images to see which touchpad pin connects to which analog pin.
TMC2208 Stepper Motor Drivers
- Each stepper motor connects to a driver and each driver connects to the teensy.
- The VIO pin on each driver connects to the positive terminal of the power supply.
- The GND pin on each driver connects to the negative terminal of the power supply.
- You will need to add a 100 microfarad capacitor in parallel to the VIO and GND pins on each driver. This is done to prevent voltage spikes from damaging the drivers.
- This project uses 16 microstepping meaning that the steppers have 3200 steps/rev or a step angle of 0.1125°. In order to set the steppers to 16 microstepping, connect the MS1 and MS2 pins on all drivers to the 3.3V pin on the teensy. This is the highest microstepping setting on the driver. Contrasting this with the regular 200 steps/rev or a step angle of 1.8° you'll see that the stepper motors turn very precisely with 16 microstepping.
- The VM pin on each driver connects to the 3.3V pin on the teensy in order to power each driver's logic board.
- Teensy Pin Assignments:
- pin 0 connects to the ENA pin on each driver (we want to be able to enable and disable all the stepper motors at the same time)
- pins 1 and 2 connect to the STEP and DIR pins on Driver A
- pins 3 and 4 connect to the STEP and DIR pins on Driver B
- pins 5 and 6 connect to the STEP and DIR pins on Driver C
- pins A0, A1, A2, and A3 connect to the touchpad
**Make sure that you have a common ground i.e. all ground pins on all components should be connected**
Downloads
Assemble the Base
- Start by adding a spacer on each stepper motor.
- Screw each stepper motor onto the base plate using M3 x 10mm screws (x12)
- Mount the protoboards with the electronics onto the base stand using M3 x 5mm screws (x4)
- Connect stepper motors A, B, and C to drivers A, B, and C by connecting each stepper motor's pins to pins M1B, M1A, M2A, and M2B on each driver. Remember which stepper motors were assigned A, B, and C. This is important for step #6.
- Screw each link_ #1 onto each stepper motor using an M4 x 20mm screw and an M4 locknut.
Upload the Sketch
- First download these three Arduino libraries
- AccelStepper
- InverseKinematics
- Adafruit_TouchScreen-master
- Confirm that the stepper motors move in the right direction by uploading the Stepper_Test Arduino sketch to the teensy. All of the stepper motors should move inward. If any moves outward flip its connection to its driver.
- Next, Upload the Ball Balancing sketch to the teensy. This is the code that allows the robot to balance the ball.
Finish the Assembly
- On each link_#2, screw a tie rod onto the end using an M3 x 8mm screw.
- Screw each link_#2 to the platform frame (on the tie rod end) using an M3 x 35mm screw. You will also want to use M3 x 5mm standoffs (x2)to fill in the gap on either side of each tie rod.
- Screw the other end of each link_#2 to the end of each link_#1 using an M4 x 25mm screw and M4 locknut. While doing this ensure that Stepper A connects to the side of the platform frame that is labeled on image #3.
- Screw the base plate onto the base stand using M3 x 8mm screws (x3).
- Using the four retainer clips, clip the resistive touchpad onto the platform frame.
Operation
To get the robot up and running simply ensure that the platform starts at a fully down and flat position, turn on the robot, and place the steel ball on it. The platforms should now balance the ball.
Patterns:
The initial uploaded program is set to balance the ball, but I have also programmed a couple of different patterns to move the ball around in. By uncommenting a different line in the code, you can make the platform move the ball around in a line, a triangle, a rectangle, a circle etc....
Changing the Offset Values
If you find that the ball doesn't move exactly to the center of the platform, you can change the X and Y offset values in the code. If you are feeling adventurous (and you know a bit about PID algorithms) you can also change the proportional gain (kp), integral gain (ki), and derivative gain (kd) values in the code to get sharper or softer reactions from the robot.
Downloads
How It Works
Design Approach
The robot is a 3RPS parallel manipulator. This is essentially a 3DOF platform with 3 legs each having a rotational joint and a spherical joint. Extending or contracting each leg will result in the platform being angled in a different way. This is done by each stepper motor. My biggest considerations when designing the balancer was to have sleekness and simplicity. For sleekness, I ensured that the electronics and most of the wires were well hidden. For simplicity, I decided to use 3 motors (3DOF) rather than the 6 motors (6DOF) used in version 1. Weighing the cost-benefits, I also chose not to use position encoders with the stepper motors. Using encoders would only eliminate the need to have the machine start at a common position. Without them, you simply just have to push the platform down every time.
Inverse Kinematics
The machine is able to angle itself through the use of inverse kinematic equations. These equations calculate the position to move each stepper motor in order to achieve the desired platform orientation. I derived these equations myself mainly using vector calculus, trigonometry, and lots of patience. The equations are fully parameterized meaning that they would work with any 3RPS parallel manipulator of any shape or size.
Ball Balancing PID Algorithm
The ball-balancing aspect of the code uses a PID algorithm. The algorithm constantly finds the most effective platform orientation to balance the ball.
- Error - The error is defined as the distance from the ball to the setpoint. The setpoint in this case is the center of the platform.
- Proportional Term (P) = kp*error --- The proportional term is proportional to the error
- Integral Term (I) = ki*∫error --- The integral term is the accumulation of the error as time moves on. The integral term helps to fine-tune the position of the ball and ensure that the ball is exactly at the center.
- Derivative Term (D) = kd*(d/dt)*error -- The derivative term is the derivative of the error which is essentially the speed of the ball. This term helps to slow the ball down.
- Output = P + I + D ---The output value is the sum of the proportional, integral, and derivative terms. This output is essentially the most effective orientation to orient the platform in order to keep the ball from falling. The robot is constantly moving the ball toward the center (P), fine-tuning the position of the ball (I), and slowing the ball down (D). Each term in the output is tuned with its respective constants (kp, ki, kd) also known as the proportional gain, integral gain, and derivative gain. One thing that I found during testing is that the robot can balance the ball with a PD algorithm just as well as it can with a PID algorithm. The integral term just gets the ball exactly to the center of the platform if it's a little off.
The control algorithm consists of 2 PID algorithms for the X and Y directions of the error.
Stepper Motor Control
Simply having a ball-balancing algorithm only got me so far with this project. During my initial testing, I found that I had very jittery motion. To solve this, I decided to make the speed and acceleration of each stepper motor proportional to the difference between the motor’s current and target positions. In other words, when a motor is close to its target position its speed and acceleration are low, and when it’s far away from its target position its speed and acceleration are high. In addition to this, I also decided to switch the drivers to 1/16th micro stepping which means that instead of having 200 steps/revolution they have 3200 steps/revolution which makes actuation much more smooth and more precise.
Synopsis of Events
- The touchpad reads the position of the ball, waits 20 milliseconds, and reads the position of the ball again.
- The program then calculates the terms in the PID algorithm. The output (aka the projected orientation of the platform) is fed to the inverse kinematic equations. These equations derive the position to move each stepper motor in order to achieve the platform's projected orientation.
- With the new stepper motor positions, the speed and acceleration of the stepper motors are calculated and set. The stepper motors are then moved to their target positions which put the platform in the orientation that best slows the ball down and moves it to the center. The process is then repeated.
Patterns
The balancer can also move the ball around in different patterns. Implementing this was fairly simple. The ball balancing algorithm moves the ball to the center of the platform. But if you change what the platform thinks is the center (ie change the setpoint) then you can move the ball to a different point on the platform. Constantly doing this creates motion and doing this with equations creates shapes. You can essentially create any shape that can be described in rectangular equations, parametric equations, or polar coordinates. Here are the shapes that I was able to create.
- line
- rectangle
- triangle
- circle
- ellipse
- sinusoidal functions
- zigzags
- figure 8