gyro c++ Programing

can anyone direct me on how to properly program the robot to drive in autonomous mode with the gyro?

That is a very broad question. Can you narrow it down to more specifics what you want to do in autonomous? Gyro is normally used to determine your heading. If you want to tell the robot to turn a certain angle during autonomous, you can certainly use it to achieve what you want. If that’s what you want to do, you can either program the robot to turn at a constant speed while monitoring the reading of the gyro until it reaches the target then you stop, or you can use PID control to adjust the speed while you turn (i.e. slow down when you are close to your target).

in the gyro example they use this

gyro.Reset();
		while (IsAutonomous())
		{
			float angle = gyro.GetAngle();			// current heading (0 = target)
			myRobot.Drive(-1.0, -angle / 30.0);		// proportionally drive in a straight line
			Wait(0.004);
		}
		myRobot.Drive(0.0, 0.0); 	// stop robot

how do we make the robot turn lets say 45 degrees then drive for 3 seconds

Would this work


//what tommy wants
			angle = 45;
			myRobot.Drive(-1.0, -angle / 30.0);
			Wait(3.0);

A simple way, not necessarily the best way, is to do the following:


float targetAngle = gyro.GetAngle() + 45.0;
 
while (gyro.GetAngle() < targetAngle)
{
    //
    // Turn the robot to the right at half speed.
    //
    myRobot.ArcadeDrive(0.0, 0.5);
}
//
// Go straight at half speed.
//
myRobot.ArcadeDrive(0.5, 0.0);
Wait(3.0);
//
// Stop.
//
myRobot.ArcadeDrive(0.0, 0.0);

That looks like it will work better than mine

Well, it is not the best way especially on the Wait(3.0). The cRIO has a watchdog timer. You need to feed it periodically or your motors will be cut out. Waiting for 3 seconds will definitely starve your dog. So instead of a plain Wait(3.0) statement, you may want to read the timer in a loop. I beleive the WPI library this year will feed your watchdog for you when you call any of the Drive methods. If not add a feed watchdog statement in each of the loops. So something like this:


float targetAngle = gyro.GetAngle() + 45.0;
 
while (gyro.GetAngle() < targetAngle)
{
    GetWatchdog().Feed();
    //
    // Turn the robot to the right at half speed.
    //
    myRobot.ArcadeDrive(0.0, 0.5);
    Wait(0.1);
}
//
// Go straight at half speed for 3 seconds.
//
UINT32 targetTime = GetFPGATime()/1000 + 3000;
while (GetFPGATime()/1000 < targetTime)
{
    GetWatchdog().Feed();
    myRobot.ArcadeDrive(0.5, 0.0);
    Wait(0.1);
}
//
// Stop.
//
myRobot.ArcadeDrive(0.0, 0.0);

And of course this is not the best way either, but it should work for your purpose. BTW, the rule of thumb is: never have a Wait statement in your code that’s close to or greater than your watchdog timer interval.

Placing any sort of loop in your overridden functions (such as AutonomousPeriodic()) is discouraged.

Code off the top of my head:


// ...
float targetAngle;
int currentState;
float fowardStartTime;

const static int TURNING = 0;
const static int FORWARD = 1;
// ...
void AutonomousInit() {
  targetAngle = gyro.GetAngle() + 45.0;

  GetWatchdog().SetEnabled(true);
  GetWatchdog().SetExpiration(0.5);

  currentState = FORWARD;
}

void AutonomousPeriodic() {
  GetWatchdog().Feed();
  if (currentState == TURNING) {
    if (gyro.GetAngle() < targetAngle) {
      Turn(); // Implement this function
    } else {
       currentState = FORWARD;
       forwardStartTime = GetTime(); // Implement this function with your preferred method of time.
    }
  } else if (currentState == FORWARD) { 
    if (GetTime() < forwardStartTime + 3000)
      DriveFoward(); // Implement this function.
    else
      currentState = -1;
  }
}

Basicxman,
That’s why I said it’s not the best way. What you are doing is essentially a state machine. That’s the preferred way to do it. In general, one should be able to code the entire competition without using a single wait loop. Whenever you need to wait, you return. And the next time you get called, you check the condition again. If it’s still not ready, you return again. When it is finally ready, you move on to the next state. Using a state machine not only allows you to eliminate wait loops, it also allow you to do several things concurrently. For example, moving your claw while tracking the line simultaneously.

Correct, some people have also referred to this technique as ‘hooping.’