Hardware
-
Arduino Nano R3 × 1 -
SparkFun Stepper Motor Driver A4988 × 2 -
Stepper Motor, Mini Stepper × 2 -
DFRobot MPU-6050 6 DOF Sensor × 1
Modeling
Simulation link: https://www.kaggle.com/zjor86/simulations-self-balancing-robot
I have detailed the model of the project and the simulation in the link above.
I strongly recommend everyone to perform simulations before establishing the system and adjust parameters in a virtual environment.
Device Selection
So far, the setup is as follows:
-
Arduino Nano, 16MHz, I am trying to squeeze as much computing power from the board as possible; later I might replace it with BlackPill (100MHz) or ESP32 WROOM (80-240MHz). Experiments show that frequency is a very important factor. -
Nema 17 Stepper Motor × 2 -
A4988 Stepper Driver × 2 -
MPU6050 – Gyroscope and Accelerometer with onboard DMP. I also tried using the more advanced chip MPU9250, but I have not found good library support yet.
Feedback Control Loop
I used 2 embedded PID control loops, one for maintaining the upright position;
the other for stabilizing speed or position.
Implementation Instructions
Stepper Control
The robot balances itself by applying a force that is roughly proportional to the angle. This means that the motors should rotate with the acceleration. Since we have stepper motors, we need to calculate the delay for the next step, ideally after each step (or as quickly as possible).
However, stepping itself is precise in timing, so I used a timer interrupt for stepping at a frequency of 40kHz (I tried frequencies from 8kHz to 100kHz, which worked well, but it consumed CPU cycles and the control was not very smooth). This process should be as fast as possible and should not contain any floating-point calculations.
Speed Update
The next time-sensitive task is to update the speed and recalculate the step delay, which should be faster than measuring the speed at which the route is running; otherwise, the “force” will not have enough time to act and produce an effect.
Control Loop
This program is responsible for reading the IMU (MPU6050), calculating the necessary control signals, such as the acceleration for the speed update loop.
Using DMP
The DMP (Digital Motion Processor) is responsible for merging the data from the gyroscope and accelerometer to provide you with nice and smooth yaw-pitch-roll data. It offloads the slow CPU operations from the Arduino, and in practice, I achieved a 5-fold increase in main loop speed after switching to DMP compared to using a hand-written complementary filter.
Circuit Diagram
▲ Click to enlarge
Project Follow-up
I will share my improvements and follow-up upgrade plans; if anyone is interested, feel free to join in.
Future considerations include:
-
Using a more powerful CPU (BlackPill, ESP32) -
Adding remote control capabilities via WiFi or BLE or nRF24L01 -
Turning this into an educational kit that is easier to assemble and understand -
Transforming this robot into a platform with an API, allowing for the installation of other devices on top of it
Leave a Comment
Your email address will not be published. Required fields are marked *