Introduction: Ball Balancing Robot

About: Mechanical engineering student at Purdue University. 3D printing, robots, and various mechanisms.

In this instructable, I'll show you step-by-step how I created a ball-balancing robot. More formally known as a Stewart platform, this robot has 6DOF robot is omni-rotational and omni-translational. With the addition of a camera, this Stewart platform can be transformed into a robot that prevents a ball from falling. Let's begin!

Supplies

Here is a list of the tools and parts needed for the build. For a more detailed overview of the supplies needed, download the bill of material excel file here.

Tools

  • needle nose pliers
  • wire cutters
  • wire strippers
  • 2.5mm allen wrench
  • soldering iron and solder
  • exacto knife
  • hot glue gun and hot glue

Parts ($483)


Electronics ($400)

Other supplies ($83)

Step 1: Make the Platform

The ball rests on a clear 0.118" thick 8" x 8" polycarbonate platform. You will need to manufacture this platform. I do not have access to a waterjet, so I used the waterjet service on the Andymark site. I have attached the dxf file below if you take this route. Another option is to simply cut out an 8" x 8" platform from a sheet of polycarbonate and drill out all of the holes. I have attached a pdf drawing of the platform below as well.

Step 2: 3D Print the Parts

I designed this robot in Autodesk Inventor. There are 18 3D-printed parts in total. I printed all of these parts in PLA filament. I have attached all of the STL files below. Keep in mind that some of the parts must be printed multiple times. This number is specified in the file name. The color of the parts does not matter except for the ball and the center markers. Those should be printed in red to be easily detected by the camera.

I'd recommend using the following printer settings:

  • 20% infill (except for the ball, I printed it at 100% infill to be really heavy)
  • Auto-generated supports
  • PLA (210° extruder and 45° bed)

The total filament usage for all parts is about 183g. It took a cumulative 15 hours to print all of the parts.

Stewart Platform

Step 3: Assemble the Ball

The ball comes in three parts, the two hemispheres, and the middle connection piece. You simply need to squish all three parts together to create the ball. If the fit is too tight then sand the middle connecting piece. If the fit becomes too loose, then apply some super glue to hold it in.

Step 4: Add Inserts to the Printed Parts

7 out of the 18 parts need threaded inserts to be pressed into them. These inserts serve as threads for the screws to screw into. You will need 36 inserts in total. The parts requiring these inserts are:

  • Image #1: platform frame (6 inserts)
  • Image #2: base plate (14 inserts)
  • Image #3: cover plate (12 inserts)
  • Image #4: 4 x center marker (1 insert each)

To press these inserts into the parts, use a heated soldering iron.

Step 5: Teensy 4.1

Now onto the heart of the project, the electronics! Creating the circuit correctly is vital to this project. The first part in making the circuit is to prepare the teensy 4.1 microcontroller. The teensy is the brain of the robot. All of the other electronic components are connected to the teensy. We will be programming the teensy using the Arduino IDE. If you have never used a teensy before, you will need to do one of two things to allow the Arduino software to support the teensy.

  • If you are using Arduino version 2.0.0 or above you will need to follow the instructions here to allow Arduino to support the teensy.
  • If you are using a version of Arduino below 2.0.0 you will need to download Teensyduino in order to allow Arduino to support the teensy.

Once this is done, I recommend verifying that all is well by uploading the blink example sketch to the teensy.

After this, you will need to modify the teensy by cutting the VIN to USB trace on the back of the teensy. I point to where to cut this trace in Image #2. It is also labeled on the diagram in Image #4. Doing this ensures that the teensy needs an external power source to work. We want this done because we will be powering the teensy with the 7.4V battery. We don't need the teensy to be powered by the computer when it is plugged in.

More information on the teensy 4.1 microcontroller can be found here.

Note: The teensy will not receive 7.4V from the battery because this is too high. The teensy will receive 5V. It will get this 5V from the polulu maestro (more on this in the next step).

Step 6: Polulu Maestro

The 6 pin polulu maestro is what controls the servos. The servos connect to the maestro and the maestro connects to the Teensy via the maestro's RX and TX pins (Teensy pins 1 and 0 respectively). The maestro is the only component that connects directly to the 7.4V battery. The maestro has a 5V regulator that will be used to power the Teensy. In Image #1 you can see that I have soldered two wires (+5V and ground) to connect to the Teensy.

The first thing we will need to do to the polulu maestro is to get it set up. You will need to download the maestro control center software to verify/change two settings. Once you have downloaded the software and connected the maestro, plug in one of the servos and move it around from the software to verify that the board works. Here is a good tutorial on how to get set up with the maestro.

Once you have verified that the maestro works, you will need to verify/change 2 of the settings on the board. Go to the tab titled Serial Settings and ensure that:

  • Serial mode is UART fixed baud rate (9600)
  • CRC is Disabled

These settings are shown in Image #4

Next, we will modify the maestro by soldering a wire connecting VIN to servo power (+). These pins are labeled in Image #3 and the soldered wire is shown in Image #2. This connection allows the servos and the maestro board to be powered by the 7.4V battery.

For more information on the 6-channel polulu maestro go here.

Step 7: Pixy 2 Camera

The pixy2 camera is what we will use for object detection. Nothing on the pixy needs to be modified, but you will need to download Pixymon v2 in order to view the camera footage and set up an object to be detected. For now, all you need to do with the pixy is to plug it into your computer and ensure that it works by viewing the footage on Pixymon. I would also suggest testing the object recognition by setting up a random object to be detected. There are 2 methods to set up object detection. I found that the manual method works.

Here are some useful resources for setting up the Pixy and getting it to recognize objects:

Note: When the circuit is set up in the next step the Pixy2 camera will not be powered by the 7.4V battery because it draws too much power. Instead, the camera plugs into the computer via micro usb. In other words, whenever you use the robot, the teensy, the polulu maestro, and the servos will be powered by the 7.4V battery, and the camera will be powered by the computer.

Step 8: Create the Circuit

Now let's create the full circuit.

Electronic components needed:

  • Modified Teensy 4.1 microcontroller
  • Modified 6-pin Polulu Maestro servo controller
  • Pixy2 camera
  • 3.3 to 5v logic level converter
  • half proto board
  • Switch

So far we have discussed 3 of the 5 main electronics: the teensy 4.1, the polulu maestro, and the pixy2 camera. Here is a brief explanations of the two remaining electronic components: the Logic Level Converter (LLC) and the Servos.

  • Logic Level Converter (LLC): Since the teensy 4.1 microcontroller uses a 3.3V logic level and the maestro uses a 5V logic level, the Pololu maestro's RX and TX pins cannot be connected directly to the Teensy. Instead, they must connect to the LLC (that steps down the logic level) which then connects to the teensy.
  • Servos: The 6 servos connect to the 6 channels on the pololu maestro. The specific channel in which each servo connects is important! I'll discuss this in the next step. I specifically chose the JX CLS6336HV servos because they are high-speed. A fast reaction time is vital to this project.

Creating the Circuit

Creating the Circuit is as simple as following the schematic shown in Image#3 (also available in PDF form below). You will want to solder the teensy 4.1 and the LLC onto the protoboard. The maestro and the pixy will be external components that connect to the teensy through wires.

Tips for making the circuit

  • Use header pins to allow the electronics to be removable.
  • Use heat shrink tubing to insulate the wires.
  • Have a multimeter handy to test connectivity to ensure that correct connections are made.
  • I found it best to use servo extension cables to connect a bunch of the electronics. Allowing for plug-and-play action makes things easily removable. This is helpful when things don't work as expected.
  • Image #2 shows the switch wiring used. The switch turns the robot on and off by completing the circuit on one lead of the battery. Use the XT60H female connector to connect to the switch. This way, the 7.4V lipo battery can be plugged and unplugged.
  • The ground pins on all of the components should be connected together. This is known as common ground. You can confirm this with a multimeter by checking connectivity.

Step 9: Assemble the Base

To assemble the base of the robot you will need:

  • The 6 servos
  • The base plate
  • The pixy2 bottom mount
  • m3 x 5mm screws (x8)
  • m3 x 15mm screws (x4)
  • Start by using the m3 x 15mm screws to mount the pixy2 bottom onto two of the servos and then to the front of the base plate (the front of the robot is labeled in Image #1, Image #2, and Image #3).
  • You will then want to screw the remaining 4 servos onto the other sides of the base plate using the m3 x 5mm screws.
  • Next, screw the half protoboard onto the top of the base plate. You will need to remove the teensy 4.1 and LLC to get access to the holes on the protoboard to screw the board on (this is why I recommend using header pins).
  • Hot glue the polulu maestro onto the base plate.
  • Finally, you will want to connect the 6 servos to the 6 channels on the maestro. The order is important and is labeled on Image #1, Image #2, and Image #3. The servos must be connected to the specified channel on the maestro.

Step 10: Cover the Base

Now we will cover the base of the robot. You will need:

  • The base from the previous step
  • m3 x 5mm screws (x12)
  • Hot glue the switch onto the cover plate
  • Using the m3 x 5mm screws, screw the cover plate onto the top part of each servo.

Step 11: Assemble the Servo Arms

Now we will assemble the servo arms. You will need:

  • link 1 (x6)
  • 25T servo disc (x6)
  • m3 locknuts (x18)
  • m3 x 120mm pushrod connector (x6)
  • 22mm long m3 tie rod (x12)
  • m3 x 12mm screws (x12)
  • m3 x 22mm screws (x6)
  • Start by screwing each 25T servo disc onto each 3D printed link using two locknuts and two m3 x 12mm screws per disc as seen in Image #1 and Image #2.
  • Assemble the six tie rods by attaching two tie rod ends to each of the pushrod connectors as seen in Image #3.
  • Screw on the other end of each of the six 3D printed links onto one of the ends of the tie rods using an m3 x 22mm screw and a locknut for each link to form six servo arms. This is shown in Image #4 and Image #5.

Step 12: Install Two Arduino Libraries

Before we continue with this build, we must install two Arduino Libraries:

  • A library for the pololu maestro
  • A library for the pixy2 camera

To learn how to install an Arduino library go here

Step 13: Mount the Servo Arms

We will now mount the servo arms onto the servos. In order to do so we must first upload an Arduino sketch that will move all of the servos to their home location.

  • Make sure the battery is plugged in and the switch is turned on, then upload the Arduino sketch below to the teensy. You should hear all the servos turn to their home positions.
  • Now screw on the servo arms (using m3 x 12mm screws (x6)) so that they are as close to being perpendicular to the servos as shown in Image #4. Do this for all of the servos.

Fine-tuning servo positions

As you will notice when the servo arms are attached, they will not be perfectly perpendicular like in Image #4. They will most likely look more like Image #3. To get these servo arms to be perfectly perpendicular, you will need to change the offset value for each servo in the code.

In this section of the code, all of the offset values will originally be set to 0 as shown in Image #5. Go through each servo and change the offset value from 0 to 5. See if the servo arm moves closer to being perpendicular.

  • If it does move closer to being perpendicular, fine-tune the position of the servo arm by going higher or lower than 5 but still keeping the value positive. For example, if the servo arm needs to move closer to being perpendicular you may change the value to 7.
  • If the servo moves farther away from being perpendicular, then make the offset value -5 and fine-tune the value (while still keeping it negative) until the arm is completely perpendicular. Do this for all servos. In the end, you will have an offset value for each servo as shown in Image #6.
  • None of the servos should exceed the range of offset values between -14 and 14.

Note: Every time you change an offset value, reupload the sketch to see its effects.

Make sure you save these offset values somewhere, you will need to change the offset values of two other programs later.

Once this is done, every time this sketch is uploaded to the robot all of the arms should move to their home positions and be perfectly perpendicular.

Step 14: Build the Platform

Let's build the platform using:

  • The polycarbonate platform
  • The platform frame
  • m3 x 20mm button head screws (x3)
  • m3 locknuts (x3)
  • m3 x 5mm standoffs (x3)

Screw the polycarbonate platform onto the printed platform frame using the m3 x 20mm screws and the locknuts. Make sure you add a 5mm standoff on each screw in the space between the platform and the platform frame.

Step 15: Attach the Platform

Now let's attach the platform to the main body of the robot. We will need:

  • m3 x 5mm standoffs (x6)
  • m3 x 14mm screws (x6)

Attach each m3 x 14mm screw onto the end of each servo arm. Screw on each m3 x 5mm standoff onto the other side of the screw, then screw the servo arm onto the platform frame where the threaded inserts are.

Step 16: Attach the Camera Mounts

  • Cut the 3/4" wooden dowel to 19" long.
  • Use #6 screws (x2) to screw the wooden dowel onto the pixy2 bottom mount.
  • Use #6 screws (x2) to screw the pixy2 top mount onto the other end of the wooden dowel.

Step 17: Attach the Pixy2 Camera

Now attach the Pixy2 Camera onto the Pixy2 top mount using:

  • m3 x 30mm screws (x3)
  • m3 x 10mm standoffs (x3)
  • m3 locknuts (x3)

Ensure that the pixy2 cam has a good view of the platform by viewing the footage on pixymon. When looking at the camera footage, you should be able to see the entire platform. If not, adjust the placement of the top mount.

Step 18: Teach the Pixy2 to Detect the Ball

Let's now teach the pixy how to detect the ball. Just like in step 7, Refer to this video to learn how to teach the Pixy2 an object (starting at 14:00). As a reminder, I recommend using the manual method. I also recommend placing the ball on the platform when training the pixy since this is where the ball will be when that robot is trying to balance it. It doesn't matter which signature you set the ball to be. For simplicity, I set mine to be signature #1. You may also need to change the signature range as shown in Image #2. This setting changes the pixy's detection accuracy. More on this here.

Step 19: Calibrate the Platform

In order for the ball balancing program to work properly, we must find the location of the center of the platform from the camera's view and another value called r_platform that will help determine when the ball is on the platform or not. To do this we will use the red 3D-printed center markers.

  • First, attach each center marker onto each corner of the platform using m3 x 5mm screws (x4).
  • The pixy cam should recognize the center markers as the ball that we trained it to recognize in the previous step. This is why the ball and the center markers should both be red. Ensure that the pixy cam can recognize each center marker by viewing the camera footage on pixymon. Make sure you only see four signatures for the four markers. If the pixy detects more or less than four signatures, this program will not work.
  • Download the Arduino program below and change the offset values from 0 to the ones that you found in step 13. This section will look similar to the section in step 13 as shown in Image #4.
  • Upload the sketch
  • The program will move each servo to its home position and then display some values on the serial monitor (shown in Image #5). The first value is the location of the center in the format (X, Y). The second value is a single number labeled r_platform. Save these three values!

Note: The values will take about 30 secs to be displayed on the Serial monitor

Note: The location of the ball should be in the ballpark of (150, 100) and the r_platform value should be in the ballpark of 113. I would give an error range of +/- 20 for all three of these values.

Step 20: Upload the Ball Balancing Sketch

This is the final step in getting the ball-balancing robot to work.

  • First, download the ball-balancing Arduino code below.
  • Just like before, you will need to change the offset values of the servos. This section should look similar to the last two programs as shown in Image #2.
  • Next change the origin and r_platform values that are set to 0 to the values that you found in the last step. This section is shown in Image #3.
  • Now upload the code and you should see that robot balances the ball when the ball is placed on the platform.
  • If the pixy cam sees more than one signature in the frame, the robot will go to its home position and not move.
  • If the ball is in the frame, but not on the platform, the robot will go to its home position and not move.

Step 21: How This Works

There are two main components to programming this robot: rotating the platform and balancing the ball. Being able to rotate the platform in any direction is what allows the robot to balance the ball. So how does the robot accomplish these tasks?

Rotating the platform:

To be able to rotate the platform anyway, the robot must use Inverse Kinematic equations. These equations take in an input (how we wish the robot to be oriented) and output 6 values (the position for each servo to turn to). For example, when we want the platform to be completely flat, the robot calculates that all of the servos must move to their home position. I derived all of these equations using vector calculus. You can see these equations in Image #1.

Balancing the Ball:

To be able to balance the ball the robot uses a PD algorithm where:

  • The set point is the center of the platform.
  • The error is the location of the ball relative to the center of the platform.
  • The derivative term is the velocity of the ball.

This is drawn in Image #4

So in order to balance, the ball the robot must take into account the location of the ball and the velocity of the ball at any given moment. It then uses those values to simultaneously move the ball closer to the center of the platform and slow the ball down.

  • If only the position of the ball is considered, then the robot will not be able to slow the ball down and the ball would fall.
  • If only the velocity of the ball is considered, then the robot would not be able to move the ball closer to the center of the platform and the ball would eventually fall.

Once the robot determines the most effective direction to angle the platform to slow the ball down and move it closer to the center, it uses the inverse kinematic equations to determine what position to move the servos to get the platform to move to that angled position.

Optional: As shown in Image #5 you can change the kp and kd constants in the Ball Balance program to get different levels of reactiveness from the robot. I discuss the effects of increasing or lowering these values in my video.

Anything Goes Contest

First Prize in the
Anything Goes Contest