Self balancing robot

Showroom for MicroPython related hardware projects.
Target audience: Users wanting to show off their project!
stmfresser
Posts: 22
Joined: Sat Jun 20, 2015 11:48 am

Re: Self balancing robot

Post by stmfresser » Tue Nov 15, 2016 1:44 pm

i've built my self balancing robot and trying to port the c code into micropython. Unfortunately micropython is too slow on STM32F407 Board.
Is there any better µcontroller which is for micropython optimized.

https://www.youtube.com/watch?v=jPH4uLOQLpM

User avatar
deshipu
Posts: 1359
Joined: Thu May 28, 2015 5:54 pm

Re: Self balancing robot

Post by deshipu » Tue Nov 15, 2016 2:00 pm

Can you tell us what exactly is too slow? I'm sure the code can be improved to run faster in the critical places.

User avatar
pythoncoder
Posts: 4014
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Self balancing robot

Post by pythoncoder » Tue Nov 15, 2016 2:26 pm

Indeed. I'm not alone in having used MicroPython for this purpose without needing any serious optimisation. Someone - I forget who - has flown a quadcopter which is a considerably more demanding proposition.

Are you sure speed is the problem? I experienced major difficulties which turned out to be entirely mechanical in origin. Vibration affecting the sensors.
Peter Hinch

stmfresser
Posts: 22
Joined: Sat Jun 20, 2015 11:48 am

Re: Self balancing robot

Post by stmfresser » Tue Nov 15, 2016 4:42 pm

my python control loop code runs 30ms, it's getting slower with the time, my C control loop runs 10ms. The balance control loop needs max. 10ms.

User avatar
pythoncoder
Posts: 4014
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Self balancing robot

Post by pythoncoder » Tue Nov 15, 2016 5:54 pm

In which case there is room for improvement. My control loop has an explicit delay to set the loop time to 5ms. Have you read the tutorial on improving MicroPython speed? See the list of tutorials herehttp://docs.micropython.org/en/latest/p ... index.html.
Peter Hinch

juggler
Posts: 3
Joined: Sat Nov 17, 2018 10:51 pm

Re: Self balancing robot

Post by juggler » Mon Mar 16, 2020 3:48 am

Hi,
A little late to the game I guess, but I have been working on a version of this for the ESP32, MPU6050, A4988/Nema steppers and have had some issues with the Timer frequency.

Running at 1/16 microstepping, I tried to find the maximum speed the stepper would run using the following Timer:

tim.init(freq=3000, mode=machine.Timer.PERIODIC, callback=issr)

issr toggles the step pins, but when I change the frequency to a higher value, the stepper speed does not increase (pulserate = 1 to toggle on each call)

Does the ESP32 have a max frequency for this?

I ended up switching over to use PWM on the step pins instead of Timer and could run the frequency to over 9000 (although at that speed the stepper could stall). I ran reliably at 6000.

I'm using the Syntien OSC client on an iphone to control over wifi to the ESP32. So far, my robot (using the JJRobot 3d printed model) balances and can move forward/backward, but needs some tuning to keep it in balancing boundaries. I wish I could say I have a firm grasp on the math for this, I understand PIDs and read most of the related material, but still struggle on how some of the values relate to distance and time.

I'd like to see if anyone else there has any other tips on using the ESP32 for this, maybe also using Bluetooth? It would be great to just have someone to help work through some of the tuning.

Thank you all for your previous posts, they were all helpful!
--Doug

User avatar
pythoncoder
Posts: 4014
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Self balancing robot

Post by pythoncoder » Mon Mar 16, 2020 8:38 am

I can't really help with steppers as my build used DC motors with H bridges. But in reviewing the previous posts, comments about concurrency are out of date. In 2015 MicroPython had no usable official cooperative scheduler. Now it has uasyncio, with a new version imminent.

I still have ideas about rebuilding my balancing robot with various mechanical improvements including more powerful motors so it can climb hills. A lead-acid battery on top is well and good dynamically, but you need the welly to lift it. But other projects grab my attention before I actually start...
Peter Hinch

juggler
Posts: 3
Joined: Sat Nov 17, 2018 10:51 pm

Re: Self balancing robot

Post by juggler » Wed Mar 18, 2020 3:46 am

Thanks for the reply Peter!

Since I have had better success with PWM speed control, my next step is to figure out how to make this turn. The method used was to spin the motors at different speeds (which I can't do with PWM which is limited by a single frequency on the ESP32 as far as I can tell).

I was thinking about just using a stalling method to turn off the enable pin on one or the other motor so that the drag would turn the robot when moving. Just need to figure out how long to do that for without causing other problems!

--Doug

User avatar
pythoncoder
Posts: 4014
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Self balancing robot

Post by pythoncoder » Wed Mar 18, 2020 7:30 am

I don't know about steppers, but with DC motors turning was easy. The PWM operates at constant frequency with speed and direction controlled by duty ratio. Turning just involved injecting an offset into the computed speed, so that one wheel turns at speed+offset and the other at speed-offset.

Stepper motors are quite difficult things to control. Would external hardware help, I wonder?
Peter Hinch

juggler
Posts: 3
Joined: Sat Nov 17, 2018 10:51 pm

Re: Self balancing robot

Post by juggler » Mon Mar 23, 2020 1:41 am

More progress:
Instead of "stalling" a wheel to turn, I used the step rate pins on the A4988 to switch between 1/16th and 1/8th step for a burst of speed on one or the other wheel to turn. Still a little awkward with the control being the OSC app on the phone, but seems to be working enough to turn. I think I still need to work on balancing while turning though.

I do think I'm getting a better understanding of the calculations and how they work together, but it's still tough to think about velocity, acceleration and angle working together to make a balancing robot move around while not falling...

Post Reply