Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   C/C++ (http://www.chiefdelphi.com/forums/forumdisplay.php?f=183)
-   -   Use Gyro to turn 90 Degrees (http://www.chiefdelphi.com/forums/showthread.php?t=103965)

DavisC 28-02-2012 16:38

Use Gyro to turn 90 Degrees
 
We are trying to modify the example gyro program so that the robot will turn 90 degrees then continue in its straight line.

How is the best way to acomplish this?

mikets 28-02-2012 19:13

Re: Use Gyro to turn 90 Degrees
 
There are many ways to do that.
  • You could just reset the gyro angle then command the robot to start to turn while monitoring the gyro angle until it reaches or passes beyond your target angle (e.g. 90). This is the simplest but you probably will overshoot (e.g. turn 95 degrees instead of 90).
  • You could use a PID controller to control the speed of turning so that when it is approaching your target angle, it slows down and hopefully stops at your target angle within the allowed steady state error. This requires you to use the PID controller and also tune the PID constants.

DavisC 28-02-2012 21:04

Re: Use Gyro to turn 90 Degrees
 
What are the values of the gyro. Is it 0 - 359 or what?

mikets 28-02-2012 21:08

Re: Use Gyro to turn 90 Degrees
 
The gyro actually gives you angular velocity (turn rate), but the FPGA is doing integration for you. If you are calling the GetAngle() method, I believe you will get the accumulated angle. So if you turn several rounds, for example, you could get angle values > 360 or < -360. Remember, it is not a compass. It doesn't give you absolute heading. If only gives you current angle relative to your starting angle.

DavisC 28-02-2012 21:15

Re: Use Gyro to turn 90 Degrees
 
So another words, if you were heading a direction recording 0. Then you used "GetAngle() + 90" when the drive is correcting itself it will think the robot turned 90 degrees to the right and will try to correct itself by turning 90 degrees to the left?
(Assuming a positive value turns the robot right and a negative value turns the robot left).

I am trying to accomplish this program without using the Reset() because that causes unpredictable angles at which it reset itself.

mikets 28-02-2012 21:19

Re: Use Gyro to turn 90 Degrees
 
Yes, you can do the following, for eample:
Code:

float targetHeading = gyro->GetAngle() + 90.0;
while (gyro->GetAngle() < targetHeading)
{
    drive->ArcadeDrive(0.0, 0.5);
}


DavisC 28-02-2012 21:27

Re: Use Gyro to turn 90 Degrees
 
Alright I will be testing that tomorrow. I was curious about the value range because we had the bot set up with the following code:
Code:

float angle = gyro.GetAngle() + 90;
myRobot.Drive(-0.2, -angle / 30);

The robot went straight before the "+ 90"; the -0.2 is because a negative value is actually forward on the bot.

The result when running this was that the robot would turn right at a medium rate and continue turning after passing 90 degrees.

The other result that seemed odd was that changing "/ 30" to "/ 1" caused a slower turn rate while changing it to "/ 60" caused a faster turn rate.

mikets 28-02-2012 21:42

Re: Use Gyro to turn 90 Degrees
 
If you just want a simple turn, don't use myRobot.Drive, just use ArcadeDrive, like this:
Code:

#define TURN_TOLERANCE 0.5
void Turn(float angle)
{
    float targetHeading = gyro.GetAngle() + angle;
    while (fabs(targetHeading - gyro.GetAngle()) > TURN_TOLERANCE)
    {
        myRobot.ArcadeDrive(0.0, (angle < 0.0)? -0.5: 0.5);
    }
}

However, if you want to adjust the turn power according to how far you are from your target, then you need to use PID.
BTW, you should really fix your signs by calling myRobot->SetInvertedMotor(), you will benefit from it in the long run.


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

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