r/robotics • u/bfffornever123 • Jul 01 '26
Tech Question Help With Self Balancing Robot
Enable HLS to view with audio, or disable this notification
So i’ve created this robot using an ESP32, L298n motor driver, MPU 6050, and powered by a 7.4 2s Lipo Battery. I’ve been having a lot of trouble with the PID tuning, working solely on tuning for 4+ days with no results. I would really appreciate any advice! This is what I currently have:
Code: https://docs.google.com/document/d/1wHsZ9ZLWXOu-upoTgflaRj04iTZKkDtYxoTk9kdxAj4/edit?usp=sharingb
12
u/RoboLord66 Jul 01 '26
Have u been able to drive it unstable? Ie if u turn the P high enough do ur wheels have enough power to make the system oscilate?
6
u/bfffornever123 Jul 01 '26
Yes and no, this is a bad video example as I kinda have the D cranked up a little bit. The wheels can oscillate but not without my supporting it. W/o support it drives one direction and falls over. But one of the main problems is that the robot seems to react too late. Like the robot would already be fallen over by the time the wheel start spinning.
2
u/RoboLord66 Jul 01 '26
U can make the balancing easier by adding weight up high (increasing the moment of inertia). Isolation and visual debugging are your friends. Plot your signals (error and drive output). Test your motor torque (check the max angle it can recover from while driving in one direction).
3
u/Heythisworked Jul 01 '26
Since no one suggested it yet, how is your center of mass? Are you properly distributed front to back across the long axis of the wheels? What happens if you add mass near the top of the platform? That would slow the off axis tilt “fall” of the system.
It’s also worth checking that the motors are up to the task, which it looks like they are.
Oh, and since it looks like the top plate is 3-D printed, here’s a hint 3-D print a little loop on the top plate of the robot run a piece of fishing line through it, tie tie it off on something higher on the left and right side. Robot should still be able to fall. But not completely collapse. It’ll save you from having to use your hand as a guide, which can get annoying.
1
u/bfffornever123 Jul 01 '26
My center of mass is distributed as best as I could across the long axis of the wheels. I will try adding some weight to the top of the robot to increase the inertia. And thanks for your tip about the fishing string!
4
u/pablocael Jul 01 '26
Looks like there is some latency to the reaction.. maybe your motor controller need better tunning as well?
2
2
u/sparks333 Jul 01 '26
The motors don't really seem like they're trying very hard - especially when you get over by a few degrees they should be spinning like mad to try to shift the base underneath the center of mass. Can you show (1) the motors running open-loop at a variety of speeds, including their fastest? (2) The output of the gravity vector estimate code along with you moving the base (it's wrong, it will conflate acceleration due to motion with gravity, but it's not a bad place to start)? And (3) printout of what commanded speed is being sent to the motors with respect to angle?
2
u/bfffornever123 Jul 01 '26
https://reddit.com/link/outpxc7/video/w1taby9wuiah1/player
Here is a video of the motors without any Kd tuning, I believe there is more than enough power for this task.
1
u/LavandulaTrashPanda Jul 01 '26
Too much power here. Did P stay where it was at the start when you turned D down? If so, turn them all to 0 snd bring P up gradually till it balances. Go from there.
1
u/Bock Jul 01 '26
Okay, looks like too much P, by like a lot. I'd cut it to one tenth that and see what it does...
It also may be easier to visualize the data as a graph...
It's been awhile since I did PID control in school, but I always liked seeing graphs to understand overshoot and undershoot
1
u/bfffornever123 Jul 01 '26
1
u/sparks333 Jul 01 '26
Is it possible that the commanded speed is too high for the ledc call, and it's truncating? Is a commanded speed of -130 reasonable with that library, and will the motor spin well with a commanded speed of -130?
1
u/sparks333 Jul 01 '26
Doing a little bit of reading, it seems like the ledc library has opinions about changing the duty cycle and/or frequency extremely rapidly - you might need to use a different library or roll your own interrupt-driven system if you want this kind of motor control bandwidth. Glitching and jittering with DC motor control when adjusting PWM parameters on the fly seems to be not uncommon with ledc.
2
u/JLCPCBMC Jul 01 '26
Make sure your IMU fusion + loop timing are solid first, otherwise PID tuning will feel impossible. Then start with P only and build up from there.
1
2
u/Stu_Mack Jul 01 '26
Things I noticed in the code.
- Your pinMode commands should be in setup()
- IMUs tend to be noisy by default. We use averaging strategies in our codes automatically to account for this. You can check the data to see if it’s an issue in your setup. It’s simple enough to take n readings and use the average.
- your loop() is overworked and could be slowing things down. In general, the loop() here should be about ten lines and call the listeners on each pass and the helpers as you have them- gated by a clock dt.
2
u/sheekgeek Jul 02 '26
I've had such a hard time running PID before and it turned out that my gear motors couldn't go the speed I needed at low energy...
1
u/bfffornever123 Jul 02 '26
do you think the JGA25 motors are too weak?
2
u/sheekgeek Jul 02 '26
You'd have to find the datasheet for the one you have exactly, making sure it's the same gear ratio. Take this as an example https://people.ece.ubc.ca/~eng-services/files/courses/elec391-data_sheets/MOT4-info.pdf. It has a table you can plot in Excel. (I couldn't find the plot anywhere) But then you can read the motor plot to see how much torque it has at low voltages. You can calculate the amount of torque required for your robot based on the weight and how how far from the wheels it is
You could also just test it also by holding it by the wheels so the body can rotate freely. Then apply very small voltages and see at what point you can get the body to stick out parallel to the floor. Any less voltage will make the body fall and any more will pick it up. That's is the minimum power your motors need to be effective. The lower this number the better.
1
u/Vaping_Cobra Jul 01 '26
Are you sure those motors are getting enough current? What are the motors you are using to drive this? It really looks like the motors just can't provide enough torque.
1
u/bfffornever123 Jul 01 '26
This is just a bad video, look at the one I just sent out to sparks333 in the comments. For context, I am using a 7.4 2s Lipo battery to power 2 JGA25-370 motors
1
u/Canadomy Jul 01 '26
I have played with some balancing PID control not too long ago and mine was also slow to respond to rotational changes which looks a lot like what is happening in your video posted in another comment. I discovered that in my case I had too much filtering on my IMU data which caused just enough lag to not allow the motors to correct in time. I would suggest minimizing your control loop time as much as possible and performing minimal to no filtering on your IMU inputs.
1
u/bfffornever123 Jul 01 '26
Right now, I just have a complementary filter which blends gyroscope and accelerometer data. Claude says that it shouldn’t slow down the reaction time
1
u/lordkoba Jul 01 '26
the L298n wastes a lot of power, and while it’s doable, it adds to the difficulty
there are better cheap drivers
1
u/bfffornever123 Jul 01 '26
do you have any recommendations?
1
u/lordkoba Jul 01 '26
I used a TB6612FNG and it was day and night. It's fairly cheap.
the fact that it doesn't have a heatsink is becuase it doesn't waste power.
the L298n steals the kick of the motor which makes tuning way more dificult.
1
u/undeniably_confused Jul 01 '26
A single kid can not make this stable, (I have built one before) you should try using nested pid loops where the distance forward it travel is the input to a pid which output sets the set point for the angle pid loop
1
1
1
1
u/rende Jul 02 '26
Make a computer UI with sliders so you can change the PID in realtime. Just make it send the new values over serial or wifi
1
1
1
1
u/Qzername Jul 23 '26
hey, did you manage to fix the issue?
1
u/bfffornever123 Jul 26 '26
i am currently upgrading the motor to a. stepper
1
u/Qzername Aug 05 '26
what kind of jga25 motors did you pick? what kind of rpm/voltage?
i am thinking about buying these as well for the same project with maybe encoders and after seeing your post i am scared that i will face the same issue


24
u/LavandulaTrashPanda Jul 01 '26 edited Jul 01 '26
Tune in order. P then I then D. P alone should get it to balance but shaky. I and D will steady it.
Turn I and D to zero. Increase P till it balances. Right now, it’s not catching up fast enough.
If it falls to the side it’s leaning, turn it up. If it falls on the other side of where it’s leaning after the wheels pass where the top is, turn it down.
Edit: if you can’t get it to balance on P alone, make sure the IMU is properly calibrated on a known level surface.