As pretty much everyone who wanted to start an arduino project, I've decided to make a 2 wheeled balancing bot.
I'm using a normal arduino UNO, an arduino motor shield, the MPU6050's DMP to calculate the tilt angle ( pitch ) and this pack of 2 DC motors with their wheels :
As you can see these motors don't have any encoders. I've started my project and after days of PID tuning, I managed to stabilise the bot with of course some bothering oscillations.
At first it didn't bother me, but then I tried to turn the bot by adding an offset to one motor and substract it from the other but the bot was too concentrated on stabilising due to the oscillations and I failed to turn it, so I decided to use encoders.
I calculated the speed of the wheels by counting the number of changing digitalReads in a certain interval of time (10 ms), and multipply it by the distance between a HIGH and a LOW of the encoder, divided by the interval of time since v=dist/time.
I know I should be adding another PID ( to control the speed) in cascade with the first one responsible for the balancing , but I have no idea how, and how to tune it.
Just realised that the speed of the wheels should be dangle/dtime, so I should be multiplying the number of the counts by the angle between each HIGH and LOW instead of the distance.
Sounds like a fun project! Of course this is a pretty complex thing, in order to dimension the PID you need to know the mechanics of your robot and from that calculate the constants. However a petter solution is probably to just test these variables until you're happy with it.
Are your question about how you should structure this in the code?
Yes, after some research, I've found out that the first PID ( speed control) will have 0 as a setpoint since we want it stable, and the speed in d angle/ dtime as Input, the ouput should be the angle that would be set as a setpoint to the balancing PID, who gets as input the angle tilt calculated by the DMP. my problem is that when I adjusted the parameters of the balancing PID I knew what I was expecting and trying to get, but for the speed PID apparently nobody knows a certain way to tune it.
One way to approach it is to make the output of the balancing PID drive one motor, and have the
difference between the encoders drive a PID loop for the other motor. If the setpoint for the difference
is zero, the second motor is slaving on the first and hopefully it works as a standard balancer.
By changing the difference setpoint slowly, it ought to simply rotate the bot without much
disturbance to the balancing.
Alternatively you can sum the outputs of two loops linearly, a balance loop and a difference loop,
so each motor is driven by both loops, in phase for balance, out of phase for turning. Again
only change the difference setpoint slowly.
The difference loop is likely to have a much longer timeconstant especially if the encoders are low
resolution (in otherwords the step response should be smooth and slow). Then it naturally avoids
disturbing the higher speed balance loop.
@MarkT okay so right now I'm only concentrating on making it balance the best way possible, and for that I need to know the best PID algorithms to use. the one with a PI speed control in cascade with a PD balance control seems good. I'm only having troubles to understand the input of the speed control, is it just the numbers of ticks given by the encoder? I don't seem to understand how is this the wheels velocity?
For a balancing bot I'd try to find a single axis gyro with good specs as the main sensor, an analog one
even.
Latency is the enemy of control loops, so any delays involved in the digital processing in the 6050
are best avoided - I think you can read the rate gyro sensor outputs direct on that chip though, which
is probably worth trying.
The smaller the bot the harder it will be to stabilise - tall with the mass near the top is the
easiest configuration to balance as everything is slower and all latencies and backlash become
less significant.
I think you can just do PID on the angle (which is PI on the angular rate plus an I term on the angle
if you think about it).
Recovery from being badly off balance is in a non-linear part of the control space, a well tuned PID for
normal balance will typically just fail if too far from balance, its really a different problem.
Lastly any backlash in your drive system is an enemy of stability too, try to avoid this.