View unanswered posts | View active topics It is currently Sun Jul 24, 2016 4:48 am

 Page 1 of 1 [ 7 posts ]
 Print view Previous topic | Next topic
Where can i find out how to use the gyro sensor?
Author Message
Rookie

Joined: Mon Nov 03, 2008 1:11 am
Posts: 5
Where can i find out how to use the gyro sensor?
The title says it all..

So i found this code on the hitechnic website..

 Code:const tSensors GyroSensor          = (tSensors) S1;   //gyro sensor//#define offset 598       //offset value may vary depending on sensor & temperaturelong Gyro_value; task main(){   Gyro_value=SensorValue(GyroSensor)-offset;  //read the gyro sensor....}

So what is the offset value mean?
does the gyro sensor measure values in degrees?
If not what are the values of the gyro sensor (ie. is it -100 to 100, or something else?)

_________________
I am a Member of FTC team Flaming Cup noodles, team # 2869.

Sat Dec 27, 2008 4:23 pm
Novice

Joined: Thu Oct 09, 2008 7:58 pm
Posts: 79
Re: Where can i find out how to use the gyro sensor?
arkarind wrote:
The title says it all..

So i found this code on the hitechnic website..

 Code:const tSensors GyroSensor          = (tSensors) S1;   //gyro sensor//#define offset 598       //offset value may vary depending on sensor & temperaturelong Gyro_value; task main(){   Gyro_value=SensorValue(GyroSensor)-offset;  //read the gyro sensor....}

So what is the offset value mean?
does the gyro sensor measure values in degrees?
If not what are the values of the gyro sensor (ie. is it -100 to 100, or something else?)

. Similar to encoders, gyros measure the number of angles from the point where you zero the gyro. For example, if you zero a gyro, then turn clockwise, it will read 90 when you have turned 90 degrees. Similarly, if you want to make sure you have done 2 full rotations, then the degree count would be 720. Likewise, if you turn the other way, the numeral number would be the same, but the value would be negative ( -90 for 90 degrees in the other direction). That is how an encoder essentially works. Like an encoder but does it with angle of rotation.

Now, if I am wrong, please let me know on this, I believe an offset value is used so that no data is negative. I guess this creates a range of positive numbers, but for the purpose for FTC, I would think using an offset value might be confusing.

Sat Dec 27, 2008 9:07 pm
Rookie

Joined: Wed Jun 25, 2008 6:07 pm
Posts: 46
Re: Where can i find out how to use the gyro sensor?
Arkarind,

Have you already bought a gyro sensor? If so, the best place to start would be to try the "HiTechnic Gyro.c" sample program. To find it:

1) Open RobotC
2) Go to File -> Open Sample Program
3) Open the "3rd Party Samples" directory
4) You will find the "HiTechnic Gryo.c" file there

When you run the demo (which wants the sensor on port 3), you will notice that the program is able to display the difference between the angle of where the gyro is now, and where it was when the program started. In that regard, you can use it a little like a compass sensor, to determine which way your robot is pointing. Unlike the compass, though, you can use it along any rotational axis, not just the plane parallel to the floor.

It's important to realize, though, that the value you're seeing there is not the value that is being read directly from the gyro itself. In it's raw form, the gyro gives back sensor values to indicate the current angular velocity. What that means, is that if you hold the sensor steady, and read the raw values, you will get a value of 0, regardless of which direction the gyro is pointing. You would only see the value change while you are rotating. The value itself is measured in degrees/second. A value of 10, for example, should indicate that the sensor is currently rotating at about 10 degrees per second. (To experiment with the raw values, hook up the gyro and use the "try me" program for the light sensor. It will be able to show the raw gyro values and give you and idea of what's going on.)

If you play with the raw values, you'll notice that even when you hold the gyro steady, you don't actually get values of 0. I just tried it with my gyro, and was getting values of about 40. That's why the code recommends you subtract off an offset from the value read. In my case, I would want the offset to be 40, to accommodate for the bias being introduced by the sensor. As another note, it's important to realize that the offset value changes over time, and may even change while your robot is running its program. (That's just how gyros are.)

The reason the "HiTechnic Gyro.c" file is able to show the current angle, rather than just the rotational velocity, is that it is using an underlying GyroDriver.c file (also in the same directory) that handles the math of converting from angular velocity to angular position for you. The GyroDriver.c file, which is included by the demo program, creates a secondary task that continuously polls the raw values and integrates them into position value. It's really a convenient bit of work that has been done for you. (Thanks, RobotC team!)

If you're using the GyroDriver.c file in your own program to get position values, rather than velocity values, then what Sunny1261 said is correct. You can use the gyro pretty much like you would use the motor encoders. The 'offset' you asked about, though, isn't really meant to keep you from getting negative values. Negative values are fine. (They just indicate that you turned counter-clockwise past the point where the driver was initialized.) The 'offset' in the original code is related to the actual sensor bias which is hidden for you by the GyroDriver file.

Good luck with the gyro! It's a really neat sensor.

Sat Dec 27, 2008 11:54 pm
Rookie

Joined: Thu Mar 20, 2008 6:17 am
Posts: 1
Re: Where can i find out how to use the gyro sensor?
There are a couple of things to consider when using the gyro. The output of the gyro is a raw value from 0 - 1023 and will give you the rate of rotation in degrees / second. To get this simply read either the “SensorRaw[xx]” or “SensorValue[xx]”.

At rest you should see a value of ~ 600 which is the offset so if you subtract this value from the raw value it should give you 0 at rest. The offset may vary a little with temerature.

In order to dertermine an accurate angle of rotation you would have to include a calibration factor in the calculation and this can vary with each sensor. The GyroDriver.c does not do this so the result can be quite inaccurate. To determine the accuracy of your gyro without calibration, mount your gyro on a wheel or other fixture so it can be accurately turned through 90 or 180 degrees and run the HiTechnic Gyro.c example to see how close the result is to the actual angle turned.

Steve
HiTechnic Support

_________________
Steve
HiTechnic

Fri Jan 16, 2009 5:10 pm
Novice

Joined: Thu Oct 09, 2008 7:58 pm
Posts: 79
Re: Where can i find out how to use the gyro sensor?
is fGyroAngle the variable we use to call to check the value of the gyro?

Sat Jan 31, 2009 10:04 pm
Novice

Joined: Thu Oct 09, 2008 7:58 pm
Posts: 79
Re: Where can i find out how to use the gyro sensor?
Sorry for the double post, but when I went back to code the Gyro, RobotC gave me the error that it cannot find the GyroDriver.c include file. I went snooping and i found that the GyroDriver.c file was not in the includes folder. Yet, when i download the sample code, it runs fine, but when I put it into my code...it doesn't recognize it. The code is.

const tSensors kGyroSensor = S3;

#include "JoystickDriver.c" //Include file to "handle" the Bluetooth messages.
#include "GyroDriver.c" //Include file to help code the Gyro.

Sat Jan 31, 2009 10:13 pm
Novice

Joined: Fri Oct 24, 2008 8:58 am
Posts: 87
Re: Where can i find out how to use the gyro sensor?
here's my gyro code if it helps. This is working pretty good with only about 1-2 deg of drift every 10-15 seconds.

First make sure the robot is not moving and run this code:
 Code:// Measure gyro bias - 20 samples every 5 msec = 100 msec;  stempA = 0;  for (stempI=1;stempI<20;stempI++)  {    stempB = SensorValue[GyroSensor];    stempB *= 1000;    stempB -= stempA;    stempB /= stempI;    stempA += stempB;    wait1Msec(5);  }   // Initialize gyro parameters  tempSensors.gyro.angle = 0;  tempSensors.gyro.total = 0;  tempSensors.gyro.dt    = 0;  tempSensors.gyro.q_err = 0;  tempSensors.gyro.zero  = 0;  tempSensors.gyro.bias  = stempA;  tempSensors.gyro.rate  = SensorValue[GyroSensor];   // calculate the t0 rate so the first  tempSensors.gyro.rate *= 1000;                      // average is good  tempSensors.gyro.rate -= tempSensors.gyro.bias;  time1[GyroTimer] = 0;                                 // Use a timer to calculate dt

That'll recalculate a bias value you can use. I've found this can't be precalculated...it must be done as close to when you want to use the gyro as possible. The cheapo sensor in the Hitechnic gyros is very very very sensistive to temp, angle of the floor, phase of the moon

Now periodically in your code call the following. Personally, I do it in a dedicated task that processes all the sensors every 10 msec.

 Code:  stempA = SensorValue[GyroSensor];  tempSensors.gyro.dt = time1[GyroTimer];  time1[GyroTimer] = 0; // Update the gyro heading  stempA *= 1000;                              // convert to 1/1000th deg / sec  stempA -= tempSensors.gyro.bias;           // remove the stored biasing value  stempB = tempSensors.gyro.rate;            // retrieve last value  tempSensors.gyro.rate = stempA;            // save the new value  // Verify stempA <= stempB  if (stempB < stempA)  {    stempC = stempA;        // swap A <-> B    stempA = stempB;    stempB = stempC;  }  // Find change in angle by integration (area under curve...well actually the trapezoid since we only have 2 points)  stempC = stempB - stempA;                      // (1/2 (B-A) + A ) * dt  stempC /= 2;  stempC += stempA;  stempC *= tempSensors.gyro.dt;             // * dt (msec)  stempC += tempSensors.gyro.q_err;          // add back the quantization error from  stempA = stempC;                           // the last calculation  stempA /= 1000;                            // convert msec to sec  stempB = stempA;                           // calculate the remainder  stempB *= 1000;  stempC -= stempB;  tempSensors.gyro.q_err = stempC;           // the remainder is a quantization error that                                             // we should keep for the next calculation  stempB = tempSensors.gyro.total;           // update the integrated angle value  stempB += stempA;  tempSensors.gyro.total = stempB;  stempB /= 1000;                            // convert from 1/1000th degree to degrees

You'll have to use your own variables as the example uses some global variable structures that I can access from anywhere in the program.

Also, some of the complexity above is because I use integer math. Mostly because I'm paranoid about the RobotC software float routines. Probably totally unfounded but I've found I don't ever have problems when I do it fixed-precision (and its faster this way)

Hope it helps.

BTW, this will still not be good enough if you let the robot sit on the floor for 5-10 min before starting a match (like when the FMS is struggling to register everyone else with 100s of bluetooth devices in the room)
We learned this the hard way...so wait to bias and zero the gyro until autonomous actually starts. Also I make sure we're last in line to register... I'd rather wait with the robot off.

Don't forget to keep the robot still while you bias.

I'm hoping to write a kalman filter that uses a gyro and accelerometer to kill this bias/drift issue once and for all.

Mon Feb 02, 2009 10:22 pm
Display posts from previous:  Sort by
 Page 1 of 1 [ 7 posts ]

#### Who is online

Users browsing this forum: No registered users and 1 guest

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ ROBOTC Applications    ROBOTC for LEGO MINDSTORMS       Third-party sensors    ROBOTC for CORTEX & PIC    ROBOTC for VEX IQ    ROBOTC for Arduino    Robot Virtual Worlds    Multi-Robot Communications    Issues and Bugs Competitions & Partners    Mini Urban Challenge    CS2N Robot Virtual Worlds Competitions       VEX Skyrise Competition 2014-2015       VEX Toss Up 2013-2014       FTC Block Party! 2013-2014    Competitions using VEX - BEST, TSA, VEX, and RoboFest!    FTC Programming    RoboCup Junior and Other ROBOT Competitions Virtual Brick Robotics Discussions    General Discussions    Project Discussions Off-Topic ROBOTC Forum & ROBOTC.net Suggestions/Feedback    ROBOTC Forums Suggestions/Comments    ROBOTC.net Suggestions/Comments       NXT Programming: Tips for Beginning with ROBOTC       VEX Programming: Tips for Beginning with ROBOTC    2013 Robotics Summer Of Learning       VEX Toss Up Programming Challenge       FTC Ring It Up! Programming Challenge    International Forums       Spanish Forums          ROBOTC for MINDSTORMS          ROBOTC for VEX       French Forums          ROBOTC pour Mindstorms          ROBOTC pour IFI VEX       Japanese Forums （日本語のフォーラム）       German Forums    2015 Spring Carnival Event    PLTW (Project Lead The Way)    Robotics Merit Badge    2014 Robotics Academy Summer of Learning