## 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