# 3DOF Ball on Plate Using Closed Loop Stepper Motors

74,184

544

153

## Introduction: 3DOF Ball on Plate Using Closed Loop Stepper Motors

The ball on plate problem consists of a flat plate on which a ball needs to be positioned. Ball positioning is only achieved through unstable equilibrium where any small changes in the plate angle will result in the continual acceleration of the ball until it leaves the plate. Such a system presents an interesting controls problem as closed loop control is needed for stable ball positioning on the plate.

A good approximation for controlling the ball’s motion is to decouple the x and y directions on the plate. This allows for two separate independent control loops. One loop controls the x-location of the ball and another controls the y-location. Each control loop for the x and y axis consists of two parts; an inner control loop and an outer loop. The inner loop is responsible for running the stepper motors in closed loop for angle control. Motor angle is obtained from quadrature encoders on each stepper motor. A set angle for the stepper motors is provided from the outer loop and the difference between the set angle and measured angle drives the stepper motor angular velocity.

The outer loop controls the actual ball position on the plate. The input to this loop is desired ball location and feedback is measured ball location. The ball location is obtained using a 4-wire resistive touch screen on which the ball rolls. The difference and rate of change of the difference between the set and measured locations determines the output angle that is fed into the inner control loop. The outer control loop takes on the form of a proportional-derivative (PD) controller, while all that is needed for the inner loop is a proportional controller.

The output from the entire control system is the position of the ball on the plate. The position is controlled by adjusting ball acceleration. Ball acceleration is a function of plate angle and plate angle is a function of stepper motor angle. Using the small angle approximation, a small change in motor angle from equilibrium should result in a linearly related change in plate angle and therefore change in acceleration of the ball. This rudimentary approximation works quite well for controlling the ball even at larger angles.

## Step 1: ​Design Approach

The platform is designed to have three degrees of freedom. The stepper motors are set up in an equilateral triangle pattern. This configuration couples the x and y motion but results in a simpler mechanical design to fully restrain the platforms position. The design also allows the platform to pivot around the location of the ball instead of just the center. This approach should permit for more abrupt acceleration changes of the ball, as the ball goes through no vertical displacement during plate angle adjustments. Currently the platform is only programed to pivot about the center.

Closed loop stepper motors were chosen because they work with existing 3D printer electronics. Adding feedback eliminates the missed step problem inherent to stepper motors and allows for more accurate microstepping as the measured angle is controlled, not the step count.

## Step 2: Materials and Tools

MATERIALS

• 3" x 1/4" aluminum bar (2-3 ft)
• 1/8" aluminum plate(enough to cut a 6" circle from)
• 1/8" acrylic sheet (6" x 7.5" min.) and enough to cut three1/2" circles from
• 1/2" aluminum round (about 6")
• 9X Traxxas 5347 rod ends
• 4.9mm OD X 2.8mm ID pultruded carbon fiber rod (about 10" worth)
• Arduino Mega 2560
• Ramps 1.4 3D printer control board
• 3X DRV8825 stepper motor driver
• 3X NEMA 14 stepper motors with minimum 26.0 OZ-in torque, 5mm double ended shaft
• 3X US Digital E2-1000-197 quadrature encoders and method to attach to stepper motors
• 8.4" 4-wire resistive touch screen and hardware to wire to microcontroller
• quadrature encoder knob with push button
• 12v minimum 4 amp DC power supply
• 1-1/4" chrome steel ball
• 9X m3-0.5 x 14mm socket cap screw
• 6X countersunk screws to mount motors
• 3X #6-32 x 7/16 in. socket cap screw
• 6X #6-32 x 1/2 in. button head screw
• 3X #8-32 x 1/2 in. socket cap screw and washers
• wire for electronics
• two part epoxy
• double sided foam tape
• thin double sided tape

TOOLS

• CNC mill
• manual lathe
• drill press and bits
• taps and drill bits for: #6-30, #8-32, m3-0.5
• SAE and metric hex keys
• screw drivers
• electrical soldering supplies
• computer with Arduino IDE installed

## Step 3: Part Models

The included SolidWorks part models and drawing are for reference only. My intention is to provide enough detail for someone to make a similar project but not make an exact copy. One should improve on the design and adapt it to fit one's needs.

## Step 4: CNC Mill 1/4 Inch Parts

Use the included SolidWorks models and reference drawing to generate machine code for the 1/4" bar. Parts were cut using a 1/8" two flute carbide endmill with a 1/2" depth of cut.

## Step 5: CNC Mill 1/8 Inch Parts

Use the included SolidWorks models and reference drawing to generate machine code for the 1/8" aluminum plate and acrylic sheet. Parts were cut using a 1/8" two flute carbide endmill with a 1/2" depth of cut. A 1/8" engraving bit was used to cut the pattern on top of the acrylic sheet.

## Step 6: Turn Inserts and Tubing

• Cut 15 x 5/8" pieces of #8-32 all thread
• Turn down 12 of the 15 pieces so 1/2 (5/16") of the piece slips inside of the carbon tube leaving the threads intact on the other half of the piece
• Cut 3 x 5/8" pieces of #6-32 all thread
• Cut 6 x 1" pieces of carbon tubing. These pieces should be as close in length as possible. I used the parting tool on the lathe.

## Step 7: Turn Standoffs

• Turn 6 pieces of 1/2" aluminum round to 1/2" long
• Drill and tap (m3-0.5) one end of each piece to a depth of 1/4"
• Chamfer the tapped end leaving a 0.2" diameter face
• Reverse the part and drill/tap (#6-32) other end to a depth of 1/4"

## Step 8: Drill Holes and Tap

• Drill and tap the aluminum parts according to the provided drawing and solid models.
• Before tapping the acrylic top sheet, glue the three small acrylic disks to the underside of the sheet aligned with the holes. Tap the holes in the acrylic after the glue is set.
• Tap the Traxxas rod ends using a #8-32 tap.

## Step 9: Assemble Frame

The first version of the motor arms and platform are shown while the finished project shows a revised set. The SolidWorks models for the revised parts are provided.

• Attach the legs to the round 1/8" disk using the #6-32 button head screws.
• Attach the standoffs to the motor arms using the #6-32 all thread pieces
• Add the #6-32 socket head cap screws to the clamping end of the motor arms
• Assemble the rod ends by pushing the balls into place
• Attach the rod ends to the standoffs on the motor arms using the m3-0.5 screws
• Attach the encoders to the backside of the stepper motors
• Attach the stepper motors to the platform
• Assemble the top triangular platform using m3-0.5 screws with the rod ends in place
• Attach the arm Y pieces to the rod ends on the triangular platform using the 3 pieces of #8-32 all thread
• Thread in the 12 pieces of #8-32 all thread into the arm Ys and remaining rod ends
• Epoxy the 6 carbon tubes onto the threaded rods
• After the epoxy is cured, slide the motor arms onto the stepper motor shafts and tighten the clamping screws
• Fasten the top acrylic sheet to the triangular platform using 3 #8-32 x 1/2" socket cap screw
• Adhere the touch screen to the acrylic sheet using thin double sided tape in the corners. Make sure the active side is up.

## Step 10: Wire Electrical

The control electronics consist of easily obtained parts: an Arduino Mega 2560, RAMPS 1.4 3D printer control board and three DRV8825 stepper motor drivers. The three stepper motors will be labeled A, B, and C.

• Attach the Arduino Mega to the underside of the assembly using foam double sided tape. Make sure contact between traces on the Arduino and the aluminum plate is not possible.
• Modify two of the DRV8825 stepper motor drivers so that the STEP pin goes up instead of down. This will allow the pins to be connected to hardware timers on the Arduino.
• Insert the RAMPS 1.4 control board into the Arduino board and DRV8825 drives into the X, Y, and Z sockets on the RAMPS board with the two modified drivers in the X and Y positions. The RAMPS should be set for 32 microsteps.
• Connect stepper motor A to the X drive, motor B to the Y drive and motor C to the Z drive. If the motors spin the wrong direction when testing adjust the code or wiring.
• Make the following pin connections:
1. X driver step pin ----- D6
2. Y driver step pin ---- D5
3. motor A encoder a ----- D2
4. motor A encoder b ----- D3
5. motor B encoder a ----- D18
6. motor B encoder b ----- D19
7. motor C encoder a ----- D20
8. motor C encoder b ----- D21
9. 3X encoder +5V ----- +5V
10. 3X encoder GND ----- GND
11. touch screen X +5V ----- A12
12. touch screen X GND ----- D44
13. touch screen Y +5V ----- A10
14. touch screen Y GND ----- A5
15. quadrature knob a ----- D32
16. quadrature knob b ----- D47
17. quadrature knob button ----- D45
18. quadrature knob GND ----- GND
• The stepper motors are powered from 12V DC supplied to the outer power connectors on the RAMPS board
• Removing diode D1 might be necessary if the Arduino 5V regulator overheats as was occurring on my board. The Arduino will need separate power if D1 is removed.

## Step 11: 4 Wire Resistive Touch Screen

Ball location measurements are accomplished using an 8.4 in. 4-wire resistive a touch screen. Resistive
touch screens are effectively voltage dividers with the x and y locations measured sequentially. To obtain position from the screen, 4 microcontroller pins are required. All bins must be bidirectional with low output impedance and high input impedance. Two of the pins need to measure analog voltage. The top and bottom plates inside of the touch screen are resistive, in the range of 1K ohm, but insulated from each other when the screen is not touched. To make an X location measurement the two pins connected to the bottom portion of the screen are set to outputs with low impedance. One of the pins is set high and the other is set low. This creates an electrical potential across the bottom portion of the screen. The pins connected to the top portion of the screen are set as high impedance inputs and an analog value is recorded from one of the pins. When the screen is now touched, the top portion of the screen makes contact with the bottom, creating a voltage divider and producing an analog voltage proportional to the touch location in the X direction. The process is reversed to record the Y location of the touch.

It is desirable to only take measurement when a touch is present. A third configuration is set to wait for
a touch condition and only enter the location measurement state when a touch is present. This is accomplished by setting the top or bottom side of the screen to ground; setting the connected pins to output low. The other layer is connected to high impedance inputs with a pullup condition on one of the connected pins. The digital state of the pullup pin is monitored until a touch on the screen pulls the layer low by connecting to the other grounded layer.

## Step 12: ​Control Methods

Stepper motors are normally operated in an absolute fashion where the amount of steps sent to the motor are tracked in order to determine motor movement. This procedure has two unwanted qualities. The most obvious is lost steps. If the motor encounters a load sufficient to stop motion, the actual position is lost, as the commanded steps no longer match the motors location. The less obvious problem is producing and counting motor steps when using microstepping at high speeds. A typical stepper motor has 200 steps for a full revolution. This translates to 6400 steps for a full revolution if using 32 microstep controllers. When running the motors at 300 RPMs, an output of 32000 steps per second are needed per motor. Running three motors would result in almost 200K logic changes per second. Doing this level of real-time processing on a 16MHz 8-bit microcontroller leaves no headroom for other tasks.

The solution is to offload the step generation to hardware level timers and compare registers while using encoders on the motors to directly measure movement. A closed loop system can then be setup with the input value representing the difference between a desired motor angle and the measured angle from the encoder. The output from the control loop would then set the motor’s RPM. The needed pulse train is generated from three 16-bit hardware timers with three compare registers. The timers are setup with no pre-scaling producing a count rate of 16MHz that resets when the count equals the compare register. A corresponding output pin is toggled on timer reset, generating a pulse train needed to move the stepper motor. The frequency of the pulse train is set by the size of the compare register and determines the RPM of the motor. Scaled output from the stepper motor control loop can now be fed into the compare register to set motor RPM. With this method, all stepper motor signal generation is accomplished at a hardware level leaving the microcontroller free for other tasks.

A proportional-derivative (PD) control loop is implemented in order to achieve staple ball positioning. An integral component was added, but not needed. The proportional term in the control loop is simply the difference between the commanded location and the measured ball location multiplied by a proportional gain. The proportional term results in smooth movement of the plate angle, as changes in ball location normally result in a large number. This is not true when calculating a simple first order derivative dx ≈ [x(i) − x(i−1)]/h as ball movement between measurements is small with relatively large noise. The behavior can be improved by increasing the time between measurements but then the system response time becomes large. The solution is to use more of the balls history to better predict the current velocity. A good approximation for the balls motion is constant acceleration as the plate’s angle is not rabidly changing. A second order accurate stencil using only past measurements to predict the current derivative is desired. The stencil should have good noise rejection and time response behavior. Pavel Holoborodko has published such a list of stencils for one sided derivative estimation from which a 16 point stencil was selected. The resulting derivative is significantly smoother than the simple case while maintaining good system response time.

Both proportional and derivative components are added together such that the proportional part tilts the plate to accelerate the ball toward the set location and the derivative component tilts the plate to slow the balls motion. The magnitude of each value can be set by adjusting the gain values until the system is critically damped.

Platform angles representing X and Y tilt need to be transformed into the three stepper motor angles. The X and Y axis are projected onto the three motor axis to determine relative control weightings. This approach is only an approximation of the desired behavior but works as needed.

Consistent code execution rates are needed. This is achieved through the use of an interrupt routine that triggers every 1ms off of Timer0. Code execution flags are activated in the interrupt routine that allow different portions of the code to run.

## Step 13: Program and Tune Platform

The code requires several libraries including: encoder, running median, running average, and PID.

The PID library could be easily eliminated as only the proportional part is used for the stepper motor angle control.

The screen will need an initial calibration. In the beginning portion of the code under "touch screen stuff" calibration values can be entered. Uncomment "Serial.print(measured_x_pos)" and "Serial.println(measured_y_pos)" at the bottom of main loop to display the raw screen readings. Touch the screen at the indicted locations under the "touch screen stuff" section and enter the displayed values in the code. After calibration, re-comment the serial prints.

The quadrature control knob is used to adjust values during operation. The Arduino IDE serial monitor can be used to display the values. The first value displayed is the main control loop time in uS. This value should not exceed 5mS as that is the call interval of the main loop. The quadrature push button is used to advance to the next value. The next three values are the proportional, derivative, and integral gains. These values can be adjusted using the knob in order to achieve desired tuning. The ball should quickly move to the set location with minimum overshoot. The values will be lost during power cycle so they should be manually entered in code after tuning is complete. Offset values for the X and Y directions can be adjusted next. The ball will be offset from the desired position if the platform is not level and integral gain is not used. Change the offset values to center the ball on the platform when "0 pattern" is set. Different ball patterns can be selected with 8 patterns currently programed using parametric equations. The rate of ball movement is also adjusted with the "pattern rate" variable; smaller numbers equate to faster ball motion. The final value is "pattern direction" which sets the direction of ball movement.

The provided code is functional but still a work in progress. Feel free to make improvements and share.

Don't forget to have fun!

### Attachments

Grand Prize in the
Sensors Contest 2016

First Prize in the
Robotics Contest 2016

## Recommendations

152 23K
86 6.3K
29 4.7K
194 17K

• ### For the Home Contest

About 4 wires resistive touch screen . They are bunch of it . So what is the standard for choosing touch screen ? Then do I need to connect it through a touch screen driver ? Help please :'<

Hey,
Firstly, I would like to congratulate you for doing such an amazing job! well, i have a question, how can you control 3 motors while you are getting only x & y in feedbacks ?
Thank you!

Thank you! The motors are coupled through the use of some trig to allow the X/Y feedback to control sets of motors achieving X/Y motion.

So you did not use inverse kinematics to get the motor angles? because you have only x & y coordinates as feedback!

The motor angles are not directly solved for, just a relative amount of angle change needed to influence the x/y position of the ball.

Thank you for sharing this project. It's really an inspiration. I'm trying to wrap my head around this problem specifically. I think its awesome that you just take a stab with some trig to figure out an approximation and let the PID do the rest. I was wondering if that would work, and its cool to find out it did. Could you provide a little more clarification on "some trig". Are you converting cartesian to polar at all? Or just using some formula to go from ball_X/ball_Y -> target_X/target_Y?

It's been an awesome project! I have it running quite well. I was able to follow instructions well enough to make it work, and I'd like to add a feature but I don't know how. To use an I2C 2 line LCD, to show the serial monitor info so you don't need a computer, would be icing on the cake! When you want to change something with the quad knob, you need to use the serial monitor and a computer. An I2C display would be perfect. Thanks to all!

Hello, it's a great project.
could you please explain how to determine values for sin_LUT and cos_LUT and the equation on void ball_patterns_and_display()

Thank you! sin_LUT and cos_LUT are sine and cosine lookup tables that are found using something like 127*sine(x/255*2*PI), 127*cosine(x/255*2*PI) where x goes from 0,1,2,...254,255 so you get 256 values. Using a lookup table is much faster than doing the math in real-time.

The equations in ball_patterns_and_display() are primarily trig parametric equations that use the lookup tables and are steped through using the counter variable pattern_counter.

maybe that 's why the reasons needed to calibrate analog out put from the screen using this function Serial.print(measured_x_pos).....
and change the initial values x-pos and y_pos base on the measured values

The calibration is needed because the resistive touch screen is not linear/consistent across its surface.

I'm back at it! What has been the biggest problem for tuning has been that when I run the serial monitor while trying to operate any motor energized function, the monitor locks up and I can't see what parameters the encoder knob is and what it's doing. I can go through the encoder knob functions in the serial monitor, but when I try with the motors energized, it crashes. Any ideas?

Hello all. I have built my own, it looks a lot like the original. I'm on step 13, and a bit lost. When I power the 12 volts, the platform raises and waits for a ball. If I place a ball and guide it with my hand, it's apparent that the circle routine is trying to run. This leads me to believe the motors and encoders are right, and the motor current levels are ok, it's just too much gain or the touch screen needs calibration.

I uncomment the lines in the code to start calibrating the screen. It says to touch the screen at the indicted locations under the "touch screen stuff". I don't know where to look for this data and the serial monitor just scrolls random stuff. I also don't know how to verify the installed encoder knob is doing anything. I swear I once had some indication with the knob on a previous attempt, but can't duplicate that right now.

Thanks in advance whether anyone can help or not. This is a cool project!

Your setup looks good! If the platform raises to a center position and waits, most of the setup is already working properly.

The first thing I would try is finding a code that just measures the touch screen to make sure logical values are measured with the ball on the screen.

When calibrating, the serial monitor should output the x and y positions of a touch if everything is working properly. The ball can be held at the indicated locations and the x/y position recorded from the serial monitor and entered into the values listed under "touch screen stuff"
# define test_C_x 493
# define test_C_y 520

# define test_L_x 900
# define test_R_x 89

# define test_T_y 835
# define test_B_y 205

The locations are: T is top of screen, L left, R right, B bottom and C center.

OK, well I seem to have it running. I went through touch screen tutorials and identified my pins, re-connected them and it still didn't seem to respond in the right direction in the serial monitor. My left was a low number and my right was a high number, opposite yours. I swapped pins till I got numbers like yours. I left your numbers and tried it out. Pattern 1 works but it's jumpy . All the other patterns work but they're all too jumpy to run right, and they drop the ball. Reminds me of trying a golf ball on a 2dof I previously made. It didn't like it.

I'm still making 2 steps up for 1 back, almost there!

Great! The steel ball might need to be a little heavier to make better contact on the screen? I had the same problems with a lighter ball. You could also try adjusting the smoothing algorithms in the code.

Once I got the baud rate right, the serial monitor shows the data in uS, the data we need to enter is in mS. I think I need to drop 3 digits. The knob shows up here too, it's working perfectly. I built this long ago, got frustrated and set it aside. I came back to it with a clear head and made progress. I'm on the right track!

THANKS!

I was asked where I sourced the encoders. It was 2 + years ago, I emailed US Digital and bought directly from them. I'm sure there was a cheaper option at the time, but I didn't care. When you order, you build the part number using their order form and your specs. Photo shows that part number, for MY specifics.

This is a great project. Just want to know how you have modified the two motor drivers. Have you done soldering or what to keep your wire intact at step pin.
What is the purpose of modifying both of the drivers :).

The step pins on two of the motor drives were attached (soldered) so they point up instead of down. This setup allowed them to be attached to D5 and D6 on the microcontroller using jumper wires. I think the last drive was already attached to a separate hardware timer so it did not need to be modified. Each drive is stepped using an independent hardware timer which effectively sets the speed of each motor based on the timer rate. The speed is then adjusted by updating the timers with feedback from the encoders. The stepper motors can now miss steps and the microcontroller does not need to count steps to calculate motor angle (just keep track of the encoders).