What code do you use for precise Gyro turns?

What I’m using right now is the sample code from RobotC.

What do you mean “What code do you use”? Do you want help with making your gyro program more accurate? Are you just looking for someone to hand you some code? Please be more descriptive.

How to make it more accurate

Can you describe what is happening with your current code? Does the sensor believe you are pointing in the correct position afterwards?

I will take a guess that your code consistently turns your bot past the desired angle. This is simply due to momentum. The best solution is to use proportional motor control. For a quick fix, just use the minimum motor power that will turn your bot.

If your problem is overturning, I would guess that your problem is momentum (as mentioned above). You could turn slower, or just tell your gyro to stop a little bit earlier and let your momentum carry you the rest of your turn.

What language are you using?

Robotc or PROS

Oh, sorry I cannot be of specific assistance with this one.

But I did just finish writing a blog post that’s basically a brain dump of everything I know about the gyro sensor (from my 20+ hours of failed attempts to use it; no joke), and everything I could pull together from various forum posts (thanks to jpearman & Team 8756/RunTheCode & several others) and online sources. It does not contain specific programming information, but it does include information about vibration and drift and scaling, any one of which could be the source of your less-than-accurate turning. http://renegaderobotics.org/getting-started-with-the-vex-gyro-sensor/

The video at the bottom of the post, from Team 8756, does have specific RobotC instruction, but I recommend reading all of the background information before you jump to the programming, as vibration, general drift, and scaling factor—for your specific component—need to be addressed and understood completely before you can have any expectation of using it effectively in your programming. Hope it’s helpful.

Leslie Peters, coach
Renegade Robotics, Team 1666

P.S. If anyone finds errors / omissions / mis-information in the blog post, please let me know and I will correct immediately. I want the post to be a central repository of knowledge so other people don’t have to read little bits of 25 different forum posts to learn things.

So a pretty standard answer would be to use a PID controller on your gyro turns. I am going to assume that you know the basics of how a PID loop operates so I just added some psudo-code below. However, if you are not that confident in PID, there are lots of posts on these forums that explain it better and more in depth than me, like this one.


function gyroTurn (setPoint, kP, kI, kD) {
     while(gyroSensor < setPoint) {
          // Calculate error
          error = setPoint - gyroSensor;
          errorTotal += error;
          errorLast = error;

          // Find proportional term
          pTerm = error * kP;
          // Find integral term
          iTerm = kI * errorTotal;
          // Find derivative term
          dTerm = kD * (error - errorLast);

          // Compute output to send to motors
          power = pTerm + iTerm + dTerm;

          // Set your motor speeds
          motorSet(motorA, power);
          motorSet(motorB, power);
     }
}

...
...
...

// Somewhere in your auto...
gyroTurn(90, 0.55, 0.03, 1.3);


There are a few more things You are obviously going to need to translate this into RobotC or PROS syntax but that is pretty simple. If you don’t know PID that well, a quick run down of variables


kP, kI, kD

: they are constants that basically determine how much of an impact your proportional, integral, and derivative terms have on your turn. For example, if you have a high kP term, you will find that your robot will reach the target turn really fast, however then it will overshoot, causing an oscillation. Finding the right constant is key and you need to find them yourself - it’s called tuning a PID loop.
Another quick thing. The I term in a PID loop helps when the turn is getting closer to the target position, that the P term does not have a great effect. For this reason we only want to use the I term when it is close to the target position. For this you just need to replace the


errorTotal += error

with some simple logic like:


// In same gyroTurn() function
...
...

if (error < errorZone) {
     errorTotal += error;
} else {
     errorTotal = 0;
}


Once again, you will need to determine the right errorZone for your robot.

  • For tuning a PID loop look at the top answer from this post

Happy coding :slight_smile:

Try the code from this thread:
https://vexforum.com/t/qcc2-gyro-and-pid-control-code/29292/1

The link in that thread is outdated. I’m maintaining this code here from now on. There’s also a tool in there (temporarily called autoHelper until I think of a better name) that I’m currently working on that basically handles the sensor filtering and PID calculations all for you for things like encoders, pots and gyros. All you need to do is set up the tool with a few lines of code and then call the different functions to for turning or driving around. A few of the teams I mentor have had good success so far with it, so take a look.

That repo is a constant work in progress so I apologize about the current lack of documentation.