hello we are learning about the yaw rate gyroscope and we were wondering if anyone could please provide a simple gyro code and an explanation. we are using robotc.
Hi there!
Gyros track rotation in degrees. If you plug in a gyro and rotate it a full revolution, it will return a value of 360.
The gyro code can look as simple as
if(SensorValue(gyro) < 200){
motor(leftDrive) = 127;
motor(rightDrive) = -127;
}
Or if you want to get a little fancier, you can use P or PID control. This can be tricky to learn, but here’s a simplified version of the P loop I used on my worlds bot last year. If you want to learn PID control for the first time, check out this youtube video and it’s sequels.
Here’s my P loop.
float kp = 0.1;
error = target- SensorValue(gyro); //error is distance from target to actual
if(error != 0){
turnSpeed = error * kp; //set turn speed to error times a constant
}
and then later in the code,
motor(rightFrontDrive) = motor(rightBackDrive) = forwardSpeed - turnSpeed; //move
motor(leftFrontDrive) = motor(leftBackDrive) = forwardSpeed + turnSpeed;
Basically, at some point in autonomous, I set the target to (for instance) 100. Then, the P loop calculated the error between the real value, 0, and the target, 100 and set the motors accordingly to a speed of 10. The kp is an arbitrary constant that you need to tune. This P loop was very simple and produced a consistent autonomous routine (at least when we were testing on our own fields without antistatic, ;( )
However, 2 things to note about the gyro:
Gyros are fairly finicky. If you run too long a wire between the gyro and the cortex or if you have too much metal near either, it may return random values and be inaccurate. Further, gyros are not compatible with v5 even though most other legacy sensors are. If you’re using v5, you will need to track your location some other way.
Hope this helps, good luck!
In ROBOTC, if you go to the file menu you’ll see an option for Sample Programs, you should find something in there.