# ROBOTC.net Blog

ROBOTC News

## A Teacher’s POV: Using the Gyro Sensor

with one comment

Programming your robot to make precise turns can be a source of frustration for some students as they begin to learn ROBOTC. Oftentimes, when students are just learning programming, all of the movements of their robots are based on timing. When programming a robot to move forward or backwards, a small error or a small amount of inconsistency can usually be overcome. With turning, however, inconsistencies and small errors can lead to larger errors and the frustration I mentioned earlier.

At this point, students learn that sensors can be used to improve the movement of their robots. With the VEX IQ, a Gyro Sensor is provided that eliminates any guesswork when it comes to programming your robot to turn.

The Gyro Sensor measures the rotational angle of the robot. If you look at the Gyro Sensor, you will see an arrow that points in a counter-clockwise direction. That is the default positive direction. Therefore, as long as the sensor is mounted flat on the robot it picks up the parallel angle to the ground. The sensor then registers the current position as a zero point. If the robot turns counter-clockwise, it registers as a positive value. If it turns clockwise, the sensor registers a negative value. We can see this applied with the following illustration:

We can program the Gyro Sensor using Natural Language or full ROBOTC. To use Natural Language, you just need to make sure that the Gyro Sensor is plugged into port 4. Let’s take a look at some ways to program the Gyro Sensor with Natural Language.

With this program, getGyroDegrees returns the current rotational value of the sensor in units of degrees. When making gyro-based turns, it is best to reset the gyro sensor before each turn, so the resetGyro command is utilized. With the example, we want the robot to turn until the getGyroDegrees command returns a value (from the Gyro Sensor) of 90 degrees. Therefore, we use the repeatUntil command. When we run this program, our robot should make a 90 degree left turn. Note that the robot may turn more than 90 degrees due to drift, which is caused by momentum. If this occurs, just slow down the speed of the motors. That should eliminate the drift.

We can apply the same commands to program our robot to make a right turn.

What I did when first showing the students the Gyro Sensor was to have them see the sensor work with the debugger screen. I used a sample program utilizing full ROBOTC with this activity. The sample program we used was in the Gyro Sensor Folder, and it is called Gyro Display Values. The students compiled and downloaded the program. They kept the USB cables plugged into their robots so they could see the values of the Gyro Sensor on the debugger screen. To access the debugger windows, go to the Robot menu, click on Debugger Windows, and then select Sensors.

The students can now run their program, physically move their robot, and see how the values of the Gyro Sensor change via the debugger screen.

The VEX IQ Gyro Sensor is extremely useful and easy to program, and the students have a lot of fun using this sensor.

– Jason McKenna

Written by Cara Friez-LeWinter

February 24th, 2014 at 1:16 pm

## Programming the VEX Gyro in ROBOTC

with one comment

The new VEX Yaw Rate Gyro is one of the more sophisticated sensors in the VEX Robotics System – but it’s also one of the most useful and powerful. Among other things, the Gyro allows your robot to make very precise, consistent turns.

Other sensors, like the Shaft Encoders, allow you to control your robot’s turns… but indirectly. The Shaft Encoders can measure the rotation of the axles (or shafts) of your robot’s drive train. By monitoring how much the axles spin, you can also monitor and control how much your robot moves forward or turns. (As a side note, some great tutorials on using the Shaft Encoders to do just that can be found in the Movement section of the VEX Cortex Video Trainer.)

The Gyro, on the other hand, actually measures the rotation of your robot. Factors such as wheel slippage and varying surface friction that cause inconsistencies with Shaft Encoder-based turns are far less problematic with the Gyro; your robot will turn until it reaches the target you set – no matter how much the wheels slip or how long it takes!

There are a number of different ways that electronic Gyros work. The VEX Gyro includes a sensing element kept in continuous oscillation which reacts when angular rate is applied, based on the “Coriolis effect”. The Coriolis effect is a deflection of moving objects when they are viewed in a rotating reference frame. In a reference frame with clockwise rotation, the deflection is to the left of the motion of the object; in one with counter-clockwise rotation, the deflection is to the right. This effect is caused by the rotation of the Earth and the inertia of the mass experiencing the effect.

Thus, the Gyro is able to detect whether the robot is turning clockwise or counter-clockwise and provide different sensor values based on how much it’s been rotated. For a more thorough explanation of the Coriolis effect, visit this Wikipedia article. For more technical information on the on the internals of the VEX Gyro, you can check out its datasheet.For this tutorial, we mounted the VEX Gyro on our Swervebot robot model and connected it to Analog port 8 on the Cortex.

ROBOTC includes a number of sample programs for the Gyro. You can open the sample program below by going to File > Open Sample Program > Gyroscope > “Gyro Based Turns – Basic.c”.

• Typically all sensors are configured in ROBOTC using the Motors and Sensors Setup window. This can still be done with the Gyro, but lines 23-28 do so manually. The Gyro is an extremely sensitive sensor – it’s readings are affected by heat, fluctuations in voltage, and how long the sensor has been in use. These factors cause the sensor to accumulate an error, or drift, over time. By setting Analog Port 8 to “sensorNone” on line 24, all of the drift is wiped out from previous runs. The sensor is then reconfigured as a “sensorGyro” on line 27.
• After the Gyro is configured on line 27, it’s very important that your robot is kept completely still for at least 1.1 seconds. During this time, ROBOTC and the Gyro perform a calibration process. The 2-second wait on line 28 is more than enough time for this process to occur.
• There is a small amount of variance between each Gyro. ROBOTC has default values used for scaling the readings from the VEX Gyro into useful SensorValues, but you may need to make a slight adjustment for your sensor. The “SensorScale[]” command (commented out on line 31) allows you to do just that. If you find that your robot is turning too far or too little, un-comment line 31 and adjust the “260” value as needed.
• As the Gyro is turned, the values it returns are in tenths of degrees, positive and negative. Thus, a SensorValue of 3600 equals 360 degrees, or one full rotation. When the sensor is mounted horizontally, counter-clockwise movements will return values from 0 to -3600; clockwise movements will return values from 0 to 3600. Once the gyro completes one full revolution, the sensor value will “roll-over” to 0 by default (for example: …3597, 3598, 3599, 3600, 0, 1, 2, 3,…). To change the “roll-over” point, un-comment line 33 and change the value of “SensorFullCount” from 3600 to the desired value (7200, 18000, ect)
• For a much more thorough explanation of the program, check out the tutorial video we produced, here.
```#pragma config(Motor, port2, rightMotor, tmotorNormal, openLoop)
#pragma config(Motor, port3, leftMotor, tmotorNormal, openLoop, reversed)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

/*+++++++++++++++++++++++++++++++++++++++++++++| Notes |++++++++++++++++++++++++++++++++++++++++++++
Gyro Based Turns - Basic
-This program instructs your robot to turn for the specified number of degrees in "degrees10".
-For best sensor results, clear out the gyro and manually configure it at the begging of the code.
-The Gyro is configured by default to provide values from 0 to -3600 for clockwise rotation,
and 0 to 3600 for counter-clockwise rotation

Robot Model(s): Swervebot

[I/O Port] [Name] [Type] [Description]
Motor Port 2 rightMotor VEX Motor Right side motor
Motor Port 3 leftMotor VEX Motor Left side motor
Analog Port 8 in8 VEX Gyro Top-center mounted,
away from the Cortex
--------------------------------------------------------------------------------------------------*/

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

//SensorScale[in8] = 260;
//Adjust SensorFullCount to set the "rollover" point. 3600 sets the rollover point to +/-3600
//SensorFullCount[in8] = 3600;

//Specify the number of degrees for the robot to turn (1 degree = 10, or 900 = 90 degrees)
int degrees10 = 900;

//While the absolute value of the gyro is less than the desired rotation...
while(abs(SensorValue[in8]) < degrees10)
{
//...continue turning
motor[rightMotor] = 25;
motor[leftMotor] = -25;
}

//Brief brake to stop some drift
motor[rightMotor] = -5;
motor[leftMotor] = 5;
wait1Msec(250);
}```

Here’s a video of the robot moving, based on the code:

And again, just in case you missed it, a more detailed explanation of the program can be found in this tutorial video:

The VEX Gyro can be purchased from the VEX Robotics store.

Written by Jesse Flot

October 13th, 2011 at 5:08 pm

Posted in Cortex,General News,VEX

Tagged with , , ,