Dexter the Balancing Robot
Introducing Dexter: the cutest balancing robot ever made.
Dexter
Dexter is a two-wheeled balancing robot controlled by an Android App. I originally created Dexter as a modified version of the wifi-rover project that I made a few years ago, but this time I was looking for a bigger challenge.
Hardware
- Teensy LC Microncontroller pjrc
- MPU-6050 IMU sparkfun
- 2 A4988 Stepper Drivers pololu
- HC-05 Bluetooth Module amazon
- 2 Stepper Motors sparkfun
- 1300 mAh 3S LiPo Battery
The Teensy LC was a good fit for this project because it has 3 hardware serial lines and multiple timers. Dexter uses serial to communicate with the HC-05, and the stepper drivers each require a 16 bit timer to work.
I don’t know the exact model number of the motors I used, but they look pretty standard.
I also added some iron weight on top for added stability.
Custom Frame and PCB
3D Printed Frame | Custom Circuit Board |
---|---|
I designed the 3D-printed frame using Fusion 360, and designed the custom circuit board in Eagle.
IMU
The MPU-6050 IMU includes a gyroscope and accelerometer, and I combined the measured angle from the accelerometer with the rate from the gyro using a basic filter. A more advanced Kalman filter would be more accurate, but IMU accuracy was not a limiting factor.
pitch = 0.99 * (pitch + G_y * dt) - 0.01 * atan2(A_x, A_z)
The rate from the gyroscope reacts quickly to changes in pitch, and the small correction from the accelerometer prevents drifting over time. The accelerometer is very sensitive to quick movements, so it can’t be used by itself.
IMU code was taken from the Arduino example (link)
Motor Drivers
The Teensy controls the A4988 motor drivers by sending pulses; each time the Teensy sends a pulse, the driver takes one step. To send pulses, I used the TimerOne
and TimerThree
libraries. These libraries trigger a callback funciton at a specified frequency, and in the callback function I send a pulse by toggling the trigger pin on and off.
Both motors were operated in 1/8 microstepping mode for smoother motion.
PID Control
Dexter uses two cascading controllers. First, the ground speed PI controller calculates the desired angle based on the speed error. Second, the angle PD controller calculates the output steps per second to reach the desired angle. Since these motors don’t have encoders, the only way to measure the current ground speed is to use the steps per second value calculated by the angle controller.
To tune Dexter, I added a basic Twiddle function. If Dexter receives the letter “t” over bluetooth, he will drive forward for a few seconds, stop, drive back, and report the total error during the trip. All of the PID constants can be changed over bluetooth and optimized to reduce the twiddle error.
Android App
The included Android app steers Dexter by reading the accelerometer. In order to tune the PID constants, I used a bluetooth terminal app (Play Store Link)
Github
Android and Arduino source code link
References
I found both of these examples to be extremely helpful
jjrobots B-robot EVO 2
Brokking YABR