# PID vex IQ code blocks help

We’re new to coding and looking for an accurate autonomous and found PID. However, we didn’t know how to use vexcode text efficiently, because none of us have coded in text form before. Therefore, we decided to try to create a basic PI (yes PI not PID) loop with vex IQ code blocks. At the moment we’re just doing it for accurate turns, because we can configure the motors into a drivetrain instead of configuring the motors seperately for this (we think?). Anyways, we were having trouble with it. First, when we try to turn instead of just turning on the spot it moves forward and left, while turning at the same time. We know that this has something to do with our velocity block underneath the turnpid broadcast, we’re just not sure why the velocity is changing how the robot turns. Second, the robot will turn so that error gets to 0, but then the error gradually increases until it starts skyrocketing well into the hundreds. Again, not sure why this is happening. Here is our code, hopefully somebody can help. Thanks

P.S. what’s the difference between drive-heading and drive rotation?

Heading is always in the range 0 to 360 (i.e., it resets after making a complete revolution); rotation is unbounded.

For example:

• If you start with a heading and rotation of 0 degrees, and then make two full 360 degree turns to the right, after the turn the heading will be 0 degrees but the rotation will be 720 degrees.
• If you then make a 360 degree turn to the left, the heading will still be 0 but the rotation will be 360.

Etc.

6 Likes

Update: We’ve fixed the part where the robot moves forward and left, instead of just left. Still unsure about the second problem though.

Umm, why is my PID for driving straight slowly going more right? Here’s my code:

1 Like

It might be slight motor differences but the program should be able to correct it.

1 Like

Sometimes, it actually goes straight, though, and then the turning part turns too far.

1 Like

You should try a overshooting correction program. Basically, add a function called “Turn Correction” and then do a while loop with the condition and adjusts the motor speed until the error angle is <0.5 degrees.

While(errorangle > 0.5)
{
setMotor(leftMotor, errorangle* speed_coefficent);
setMotor(rightMotor, errorangle* -1*speed_coefficent);
errorangle = desiredangle-getGyroDegrees(Gyrosensor)
}

this is a code skeleton done in C so you need to change it to Scratch blocks

3 Likes

It works really well now, thanks!

1 Like

We are using similar pcontrol block as yours but our robot keeps swaying a lot, how can we fix it? Also could you please share how you translated the overshooting code in text to vex blocks? Thank you so much for your time