How to Gyroscope?

What i mean by how to gyroscope, is that I’m wondering how to code a gyroscope, and where to place/build it onto a robot.
Right now I have this code:


void gyro(int angle) {
	angle = angle*10;
	//Specify the amount of acceptable error in the turn
	int error = 5;

	//While the absolute value of the gyro is less than the desired rotation - 100...
	while(abs(SensorValue[in3]) < angle - 100)
	{
		motor[FR] = 50;
		motor[FL] = -50;
		motor[BR] = 50;
		motor[BL] = -50;
	}
	//Brief brake to eliminate some drift
	motor[FR] = -5;
	motor[FL] = 5;
	motor[BR] = -5;
	motor[BL] = 5;
	wait1Msec(100);

	//Second while loop to move the robot more slowly to its goal, also setting up a range
	//for the amount of acceptable error in the system
	while(abs(SensorValue[in3]) > angle + error || abs(SensorValue[in3]) < angle - error)
	{
		if(abs(SensorValue[in3]) > angle)
		{
			motor[FR] = -30;
			motor[FL] = 30;
			motor[BR] = -30;
			motor[BL] = 30;
		}
		else
		{
			motor[FR] = 30;
			motor[FL] = -30;
			motor[BR] = 30;
			motor[BL] = -30;
		}
	}
	//Stop
	motor[FR] = 0;
	motor[FL] = 0;
	motor[BR] = 0;
	motor[BL] = 0;
	wait1Msec(250);
}

task autonomous()
	{
		//Completely clear out any previous sensor readings by setting the port to "sensorNone"
		SensorType[in3] = sensorNone;
		wait1Msec(1000);
		//Reconfigure Analog Port 3 as a Gyro sensor and allow time for ROBOTC to calibrate it
		SensorType[in3] = sensorGyro;
		wait1Msec(2000);
}

So let’s say i want the robot to go back, throw and star over, then go for the middle cube would this work?


task autonomous()
	{
		//Completely clear out any previous sensor readings by setting the port to "sensorNone"
		SensorType[in3] = sensorNone;
		wait1Msec(1000);
		//Reconfigure Analog Port 3 as a Gyro sensor and allow time for ROBOTC to calibrate it
		SensorType[in3] = sensorGyro;
		wait1Msec(2000);
                straight(-127); //robot drives backwards until it gets to wall
		wait1Msec(1600);
		straight(0);
		arm(127); // robot stops at fence and brings arm up to throw preload over
                waitUntil(SensorValue[P1] >= armFull);
                gyro(45); // <---- here is my main issue, assuming my gyro code above works, would calling the gyro function here rotate the robot 45 degrees exactly? and what if after calling gyro(45); i call gyro(90); would calling gyro(90); rotate the robot 90 degrees from where it is or from the starting point meaning it would be 90 degrees from where it started not 135 degree
}

I couldn’t find any good tutorials online, this is just what i gathered from the sample programs so I’m looking for someone to help! thank you!

The gyro is intended to establish a 0 angle when reset and always associate that direction with 0. In your example, if you call gyro(45) and gyro(90) your bot will turn a total of 90 degrees. However, your code needs to account for the best direction to turn to get to the desired angle. If I am reading your code correctly, it will only turn to the right.

Proportional control would be wise, and there are many examples of that on the forum.