# Help With Using Gyro to Go Straight

Hello! I went onto the PROS documentation, and found little to nothing about gyroscopes, sadly. We want to use the gyro sensor to ensure that the robot does not drift, essentially course correction, for both autonomous and driver control if possible. Can someone get me started with the classes/tips/mathematics I would need to code something like this? Thank you in advance!

This is the logic you need. Let say you want to create a forward function that will stop when the encoders reach a point and will use the gyro to prevent drifting. Here is an outline:

``````Start your motors at the desired speed
while(1){
if encoder reached target ticks, break loop and stop all motors
if abs(gyro value) is more than buffer, change the speed of the motors proportional to the gyro value
}
``````

A example of how you can change your speed:
First, you need two variables to store the current speed (lets say they are Lspeed and Rspeed)
Lspeed -= 3*gyrovalue
Rspeed += 3*gyrovalue
LeftMotors = Lspeed
RightMotors = Rspeed
(3 the just a number for example. Test your own magic number)

You dont really have to use classes at this point.
Also, to get the value of a gyro, use `[gyro name here].get_value()`

2 Likes

Ah, cool. Thank you!

For posterity, here’s:

1 Like