|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
[FTC]: I really need help on gyro!!!
so my team is trying to figure out how to use a gyro sensor. i am a new Programmer and i have no idea what im doing with this! can someone please give me some guidance? ASAP!!
|
|
#2
|
||||
|
||||
|
Re: [FTC]: I really need help on gyro!!!
They gyro sensor reads the CHANGE in degrees of the rotation of your sensor. in order to get the number of degrees your robot has traveled, you need to integrate it over the number of cycles.
Start first by calibrating your gyro sensor to the current heading. My Gyro sensor's name is HTGYRO Code:
HTGYROstartCal(HTGYRO); now we need to create variables for the current heading and the elapsed time Code:
float heading = 0.0; long currTime; long prevTime = nPgmTime; Code:
while(true)
{
currTime = nPgmTime;
heading += ((float)HTGYROreadRot(HTGYRO))*(currTime-prevTime)/1000;
Then we take the heading (currently 0) and add the degrees rotated by the change in time (comes in as milliseconds, we divide by 1000 to change to seconds). Code:
prevTime = currTime; Then we put in the logic for looking at the direction to turn. Code:
if(turningRight) //If you want to turn to the right
{
if(heading < (target - buffer)) // If the # of degrees turned is less than the target value...
{
turnRight(drivePower); //Turn Right
}else if(heading > (target + buffer)) // If the # of degrees turned is greater than the target value...
{
turnLeft(drivePower); // Turn to the left
}else if(heading < (target + buffer) && heding > (target - buffer)) // If in the "buffer zone"...
{
stopMotors(); // Stop the robot
wait1Msec(waitTime);
return;
}
}else if(direction == 2) // If turning to the left...
{
if(heading < (target - buffer)) // If # of degrees turned is less than target...
{
turnLeft(drivePower); // Turn to the left
}else if(heading > (target + buffer)) // If # of degrees turned is greater than target...
{
turnRight(drivePower); // Turn to the right
}else if(heading < (target + buffer) && heading > (target - buffer)) // If in "buffer zone"
{
stopMotors(); // Stop the robot
wait1Msec(waitTime);
return;
}
}
The buffer values are put in to ensure you dont have the "jiggle" that often happens because the robot shoots past the target while turning then has to compensate backwards again. This is essentially a "dead-zone" where the robot will stop moving if you are within a few degrees of the target. So, the entire function I have written is as follows: Code:
void gyroTurn(int target, int buffer, int drivePower, bool turningRight, int waitTime)
{
stopMotors();
wait1Msec(waitTime);
HTGYROstartCal(HTGYRO);
float heading = 0.0;
long currTime;
long prevTime = nPgmTime;
while(true)
{
currTime = nPgmTime;
heading += ((float)HTGYROreadRot(HTGYRO))*(currTime-prevTime)/1000;
prevTime = currTime;
if(direction == 1)
{
if(heading < (target - buffer))
{
turnRight(drivePower);
}else if(heading > (target + buffer))
{
turnLeft(drivePower);
}else if(heading < (target + buffer) && heading > (target - buffer))
{
stopMotors();
wait1Msec(waitTime);
return;
}
}else if(direction == 2)
{
if(heading < (target - buffer))
{
turnLeft(drivePower);
}else if(heading > (target + buffer))
{
turnRight(drivePower);
}else if(heading < (target + buffer) && heading > (target - buffer))
{
stopMotors();
wait1Msec(waitTime);
return;
}
}
}
}
Code:
turnLeft(drivePower); Code:
turnRight(drivePower); |
|
#3
|
||||
|
||||
|
Re: [FTC]: I really need help on gyro!!!
I realize that I forgot a few things and for some reason I cannot edit my OP...
at the beginning of your code, you will need to import the HTGYRO driver written by Xander Soldaat, the writer of all the RobotC 3rd Party Sensor Drivers you can find this file in the Sample Programs->3rd Party Sensor Drivers->drivers folder and can import it using: #import "HYGYRO-driver.h"; I also have one error in my code... where it says "direction == 1" and "direction == 2" it should be "turningRight" and "turningRight == false" If you have any other question be sure to let me know (Sorry mods for the DP but I couldnt edit my original post :/ ) |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|