Help with PID programming

Hello, I need help with my PID code for the robot to turn on both sides, left and right, I have a problem that has been recurring over time, when turning 90 degrees to the right it turns perfectly but when turning left the robot goes out of control, it is worth clarifying that it is the “same” code for the two turns only that the directions of the motors have changed, here are images of the code so that they can help, thank you very much.



I have a series of functions and libraries for each job but I would like to know how to solve it

How are you calling the function? There are many potential things I see for error but I can’t tell without further information. First, it may be the motors maybe turning the wrong way depending on how you give the angle. Second, heading may be causing errors because heading only goes 0-360 so I recommend using yaw (-180 to 180) or rotation (uncapped). In general, in coding you try to follow the D.R.Y. principle which stands for Don’t Repeat Yourself so turning left and right probably should be condensed into a single function.

I also have some things of personal preference I would like to share. First, I would not recommend have every PID variable be an attribute of the robot class and instead locally define them in the function. Second, I wouldn’t recommend resetting your gyro heading at the beginning of the turn as this eliminates your knowledge of your global heading from start, which can be essential to reducing error building up over a long skills run.

I believe the problem is you are using inertial.heading rather than inertial.rotation. This is a problem because inertial.heading limits the reading between [0, 360). This means that when you turn right, it acts exactly as you expect, but when turning left, it jumps to 359 and moves back from there, making the PID act unexpectedly. inertial.rotation does not have a limit and acts the same as inertial.heading otherwise.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.