Add inertial sensor to PID to make it drive forward straight

I cant figure out how to programm my inertial when going forward to go straight, im using a PID right now but sometimes the forward function is not accurate and it doesn’t go straight


Not driving straight is usually a mechanical issue due to friction. If you were to try to correct using an inertial sensor, the simplest way would be to adjust the power of the motors depending on the angle given by the inertial sensor. When the robot is moving perfectly straight, the angle is zero, and no correction is needed. If the robot is turning slightly to the left (right motors are moving further) then slow down the right based on the angle. The same would apply when the robot curves in the other direction.

I’m doing the same thing but it’s a real pain to get everything right. I believe it’s possible to manipulate the inertial sensor readings in certain ways, but this is sorta how I did it:

Before attempting this, I suggest, if you haven’t already, to try and learn how to make turns with your inertial sensor. After that, you should start a more complicated approach. You should make is so if the desired turn is greater than 0 and less than or equal to 180, only make a clockwise (left) turn. Then, when the desired turn is between 180 and 360 (or 359.9,) the program should tell the robot to turn counterclockwise (right.)

Once you figured out how to properly make efficient turns with the right tuning, you can move on to lateral driving with the inertial sensor. If you can, try to make it so the two PID loops (lateral and turning) can be activated or deactivated based on the number you set each of your desired values to.

Basically what you want to do is create two different sections in your PID loop. You want to do this, because if the robot drifts to the right, it should make a counterclockwise turn, and the opposite goes for a left drift. This is where your work of figuring out how to make proper turns will pay off. The two sections of the lateral PID should be opened depending on the inertial readings. For example, if the inertial reads that it is at an angle of 356, it will make a clockwise turn to recover from the error. Same goes for the opposite side of the circle. You want to take note that you might need to make the kP constant of the running really low in both loops, because the turn can sometimes be overdramatic, leading to unwanted results.

Another you should know is you need to be patient with the programming. It can be challenging but very rewarding. Also, make sure you are calibrating the inertial sensor correctly.

Hope any of this helps, if not, I wish you the best of luck.
If you do find this helpful, but have some questions or want to see my program as a reference, I’ll try my best to help.

1 Like