Log in

View Full Version : Gyro Turning


tuXguy15
08-10-2013, 21:58
So i know that gyros can help your robot drive straight in autonomous, but can they also turn your robot? Say im going forward for 3 seconds and then i want to turn 90 degrees to the left. Can the gyro help me do this and then continue driving straight?

Pault
08-10-2013, 22:16
Yes, it is definitely possible. You would probably want to use PID. The basic logic goes something like this.

The input of the PID is the gyro angle.
The setpoint of the PID is 90 degrees.
The output of the PID is 1 side of your drivetrain, and then the other side of your drivetrain should run at the exact opposite of the normal output.

Hope this helps.

tuXguy15
09-10-2013, 08:05
So Kp would be the value of the angle? Say i were to want it to turn 90 degrees, P if PID would be 90?

Ether
09-10-2013, 09:30
So Kp would be the value of the angle?

From the source code that came with your Java installation:

/**
* Allocate a PID object with the given constants for P, I, D, and F
* @param Kp the proportional coefficient
* @param Ki the integral coefficient
* @param Kd the derivative coefficient
* @param Kf the feed forward term
* @param source The PIDSource object that is used to get values
* @param output The PIDOutput object that is set to the output percentage
* @param period the loop time for doing calculations. This particularly effects calculations of the
* integral and differential terms. The default is 50ms.
*/
public PIDController(double Kp, double Ki, double Kd, double Kf,
PIDSource source, PIDOutput output,
double period) {

otherguy
09-10-2013, 09:31
No, Kp is the proportional gain term. It's a value which is multiplied by the measured error. Where "error" is your desired position (input into the PID controller) minus actual measured position.

I would suggest reading up on PID controllers, and learn at least at a high level how they operate before attempting to use one on your robot.
Some helpful links:
PID without a PHD (pdf) (http://m.eet.com/media/1112634/f-wescot.pdf) a good overview of how different controllers work
PID Tuning (https://controls.engin.umich.edu/wiki/index.php/PIDTuningClassical) This one will help you choose your gains.

That said, you can turn x number of degrees using the gyro without a PID controller. A simple implementation is provided below.
If you have never used a PID controller before, it may be easier to implement a simple algorithm like what is below, then itterate on the design to improve the performance problems you identify.

Sudocode (this needs to be run in a loop in a simple robot project, or have supporting state variables in an itterative or command based robot project (to flag when you're finished)

//Positive angle is roatation clockwise
//currentAngle is the angle measured off the gyro
//destinationAngle is the desired angle to rotate to
//forwardSpeed and reverseSpeed are variables between 1.0 and -1.0, the larger they are the faster you will turn.
// making one the negative of the other will
if ( abs(currentAngle) >= abs(destinationAngle)) {
//we're there, stop turning
drivetrain.tankdrive(0,0)
} else if ( destinationAngle > currentAngle) {
//rotate clockwise
drivetrain.tankDrive(reverseSpeed, forwardSpeed);
} else {
//rotate counter-clockwise
drivetrain.tankDrive(forwardSpeed, reverseSpeed)
}

Note, this code could (Read will) overshoot its destination. Depending on your intended applicaiton this may or may not be a desireable feature.