Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   C/C++ (http://www.chiefdelphi.com/forums/forumdisplay.php?f=183)
-   -   gyro c++ Programing (http://www.chiefdelphi.com/forums/showthread.php?t=90626)

tomy 31-01-2011 18:41

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

mikets 31-01-2011 18:52

Re: gyro c++ Programing
 
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).

tomy 31-01-2011 18:56

Re: gyro c++ Programing
 
in the gyro example they use this

Code:

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

ElliotCourant 31-01-2011 19:02

Re: gyro c++ Programing
 
Would this work


Code:

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


mikets 31-01-2011 19:07

Re: gyro c++ Programing
 
A simple way, not necessarily the best way, is to do the following:
Code:

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);


ElliotCourant 31-01-2011 19:19

Re: gyro c++ Programing
 
That looks like it will work better than mine

mikets 31-01-2011 19:57

Re: gyro c++ Programing
 
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:
Code:

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.

basicxman 01-02-2011 14:49

Re: gyro c++ Programing
 
Placing any sort of loop in your overridden functions (such as AutonomousPeriodic()) is discouraged.

Code off the top of my head:
Code:

// ...
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;
  }
}


mikets 01-02-2011 14:58

Re: gyro c++ Programing
 
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.

basicxman 01-02-2011 15:00

Re: gyro c++ Programing
 
Quote:

Originally Posted by mikets (Post 1013426)
Basicxman,
That's why I said it's not the best way. What you are doing is essentially a state machine.

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


All times are GMT -5. The time now is 17:43.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi