Balance Bot PID Tuning

Hey, so I am working on a Self Balancing Bot with Arduino Mega. I'm using 12V 200 RPM motors with built in 840 PPR quadrature encoders. The torque rating is as follows:

Rated Torque: 2.4 Kg-cm
Stall Torque: 6 Kg-cm

As of now I've implemented a simple PID controller (based on Brett Beauregard's PID library) to minimize only the tilt angle error. Still haven't implemented PID using the encoder for the position error.

I've tried a lot to tune the PID values. The robot is quite stable when it is standing its own. However, here's the issue, when the robot tilts by an angle greater than 7-8 degrees (or when it is pushed slightly) from its stable position, the motors start rotating at the maximum speed (PWM: 255) and the robot still doesn't seem to recover back and just keeps running and then finally falls down in the direction in which it was moving. It doesn't seem to recover at all.

Is it just a problem with the PID tuning? As I said earlier, it's stable for the range -5 to +5 degrees (0 degrees is the upright position) without oscillating much. However it is not able to recover for errors greater than 7 degrees.

Or could this be an issue with the robot being too heavy and insufficient counter motor torque? Total weight of the robot is around 1 kg (including the motors). I'm using 2200 mAh LiPo battery and have placed it at the highest level so as to decrease the angular acceleration.

Would implementing the PID feedback for encoders as well solve this issue? Also do suggest some links on how to implement dual PID using an IMU and encoders to correct tilt angle and position. I'm planning to use cascaded PID controllers in which the encoder PID measures the error in position and its output will be the set-point for the angle PID loop which in turn will control the PWM of the motors. So if there's an offset in position the angle set-point will change to, let say -2 degrees (from upright 0 degrees). So now the angle PID will make the robot move in order to correct the angle offset and therefore the position error also gets corrected. Is this a good method. Any other suggestions or different approaches to this?

I'm using 2200 mAh LiPo battery and have placed it at the highest level so as to decrease the angular acceleration.

I've never understood this practice. Moving weight away from the CG means that more torque is required to move the weight. More torque means bigger motors, which means slower motors, as bigger motors take more time to get moving.

You may be interested in this Post by @zhomeslice and its video link

...R

PaulS:
I've never understood this practice. Moving weight away from the CG means that more torque is required to move the weight. More torque means bigger motors, which means slower motors, as bigger motors take more time to get moving.

That's actually the point. If you read between the lines on a few of the statements this juggler makes, it's easier to balance things with a higher center of gravity than a lower one. This is because when you balance something on your hand or a robot on its wheels, you can't apply any force to the top (otherwise that's hanging, not balancing, a much less impressive feat). Since the top can't be controlled, you need to move the bottom to keep it under the COG. You want the bottom light and quick, and the top heavy and slow.

If I am going to attach a bowling ball and a stick, and then balance that on my hand, I'm going to have a much easier time doing that with the bowling ball on my hand than with the bowling ball on the top end of the stick I'm holding.

What am I missing?

PaulS:
If I am going to attach a bowling ball and a stick, and then balance that on my hand, I'm going to have a much easier time doing that with the bowling ball on my hand than with the bowling ball on the top end of the stick I'm holding.

What am I missing?

You are missing the fact that your intuition is completely wrong. It will be easier to balance with the bowling ball at the top of the stick.

Taken straight from the juggling page I linked to:

If you can plate spin, you will find that the weight of the plate on the stick makes it easier than when you just try and balance the stick!

Weight at the top makes things easier to balance, not harder.

I think I know the mistake you're making, and if that's right then you're wrong about the way you think the forces are being applied. I believe you're thinking of torque being applied directly to the stick in order to counteract any rotation away from the balance point. In this case, the applied torque would be proportional to the moment of inertia, and a lower center of gravity would be desired.

However, that is not the way a balancing bot works. Because the base pivot is not fixed but is free to move laterally, the forces involved are different. Instead of applying a torque to rotate the arm back up to the balancing point, the pivot is moved to keep it underneath the COG. With this balancing method, a high COG is desirable. You want the top to be as insensitive as possible to the lateral forces applied to the base pivot.

If your technique is quick and dexterous enough, even a 16 pounder would only require very light lateral movements to balance it on top of a stick.

PaulS:
If I am going to attach a bowling ball and a stick, and then balance that on my hand, I'm going to have a much easier time doing that with the bowling ball on my hand than with the bowling ball on the top end of the stick I'm holding.

What am I missing?

Your analogy is clouding the issue.

Assume you are required to balance a bowling ball at the top of a stick ...

Now, will that be easier if the bottom of the stick is light, or if it has a large mass attached to it.

Also assume the shape of the lower mass cannot contribute to the balancing solution - assume it is a pyramid on its point, for example.

...R

kushal992:
Is it just a problem with the PID tuning? As I said earlier, it's stable for the range -5 to +5 degrees (0 degrees is the upright position) without oscillating much. However it is not able to recover for errors greater than 7 degrees.

Or could this be an issue with the robot being too heavy and insufficient counter motor torque? Total weight of the robot is around 1 kg (including the motors). I'm using 2200 mAh LiPo battery and have placed it at the highest level so as to decrease the angular acceleration.

The motors aren't fast or powerful enough. Every balancing system like this is going to have a range of angles that it can recover stability from, but outside of that range it will fall. Since your motors are hitting 255, I don't think there's any tuning you could do to make it much better. You're at the limit of their performance.

If you want a wider control range, you'll need faster motors.

Would implementing the PID feedback for encoders as well solve this issue?

No. In fact it will probably make it worse since you've added another variable that needs controlling.

Also do suggest some links on how to implement dual PID using an IMU and encoders to correct tilt angle and position. I'm planning to use cascaded PID controllers in which the encoder PID measures the error in position and its output will be the set-point for the angle PID loop which in turn will control the PWM of the motors. So if there's an offset in position the angle set-point will change to, let say -2 degrees (from upright 0 degrees). So now the angle PID will make the robot move in order to correct the angle offset and therefore the position error also gets corrected. Is this a good method. Any other suggestions or different approaches to this?

That sounds like what I would try, so go for it.