Joined: Tue May 10, 2011 3:00 pm
Hi I am a Vex programmer and I was wondering. If there is any VEX sample code for the Gyro.
The problem that I have with the gyro is that it has a max value of about 4095. So if I say add 500 to the current position, and the gyro is currently at the value 4000 then the robot will turn infinitely.
Here is my code:
#pragma config(Sensor, in1, guyo, sensorGyro)
#pragma config(Motor, port1, , tmotorNormal, openLoop)
#pragma config(Motor, port2, left, tmotorNormal, openLoop, reversed)
#pragma config(Motor, port3, right, tmotorNormal, openLoop)
#pragma config(Motor, port6, port6, tmotorNormal, openLoop, reversed)
#pragma config(Motor, port7, port7, tmotorNormal, openLoop, reversed)
#pragma config(Motor, port8, port8, tmotorNormal, openLoop)
#pragma config(Motor, port9, port9, tmotorNormal, openLoop)
#pragma config(Motor, port10, Claw, tmotorNormal, openLoop, reversed)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
task main ()
int current_pos = SensorValue[guyo];
int final = current_pos + 500;
while(SensorValue[guyo] <= final)
I also figured out that you can clear a gyro. But the gyro cannot have a negative value. So in my case I can only turn right.
Joined: Sun Nov 15, 2009 5:46 am
I know NXT but don't know anything about VEX. How gyro works should be the same on both though. Gyro gives you turn rate, not position. If you need to get position, you need to integrate the gyro reading against time. Your code is using the gyro reading as position which is wrong. Think of the gyro reading as the angular velocity (how fast it is turning). If you add 500 to it, you just tell it to turn faster.
Joined: Tue May 15, 2007 9:02 am
You could think of the gyro like a compass, that rolls over from 0 to 360 and vice versa. Instead of writing conditions that wait for the sensor value to be ">" or "<" as it turns, you could use "!=", so that it looks for a very specific number, regardless of the direction it's turning.
Also, if you want to increment your value, but you go past the sensor limits, you can write an equation to take of that. For example: position = (position + 500) % 4098
The % operator will give you the remainder of the equation.