ROBOTC.net forumshttp://www.robotc.net/forums/ Gyro Senosrhttp://www.robotc.net/forums/viewtopic.php?f=52&t=2804 Page 1 of 3

 Author: maths222 [ Wed Dec 01, 2010 5:17 pm ] Post subject: Gyro Senosr Our team would like to use a gyro sensor to measure the angle our robot turns. Does anyone have any ideas for how to code it.Team 4442

Author:  MHTS [ Wed Dec 01, 2010 6:01 pm ]
Post subject:  Re: Gyro Senosr

Our team does not use the gyro this year. We use the encoders to keep track of the robot's rotation. We have used other gyros for the FRC but I am not very familiar with the HiTechnic gyro. If it works the same, I believe gyro gives you a reading of anglar velocity. In fact, I just check the HiTechnic site for the gyro info and got the following:
 Quote:The NXT Gyro Sensor contains a single axis gyroscopic sensor that detects rotation and returns a value representing the number of degrees per second of rotation. The Gyro Sensor can measure up to +/- 360° per second of rotation.

In other words, it will give you a value proportional to how fast you are turning. In order to keep track of your heading, you need to integrate this value to cumulate the angle you have traveled. In order to do that, you need a consistent timed main loop. The typical main loop such as the one below will not work because the loop time is 10 msec + whatever time it needs to do "other things" which is unpredictable and inconsistent for every loop.
 Code:int heading = 0;while (true){    heading += HTGYROreadRot(gyro)/100;    //make it per 10 msec    //do some other things here.    wait1Msec(10);}

There are two ways to do this: create a constant timed loop or measure the actual loop time in each loop. The following code shows how to measure the actual loop time.
 Code:int heading = 0;long currTime;long prevTime = nPgmTime;while (true){    currTime = nPgmTime;    heading += HTGYROreadRot(gyro)*(currTime - prevTime)/1000;    prevTime = currTime;    // Do something else here.    wait1Msec(10);}

Hope it helps.

Author:  MHTS [ Wed Dec 01, 2010 6:09 pm ]
Post subject:  Re: Gyro Senosr

Oh I forgot one thing: gyro has a bias. In other words, it reads a small value even if it is not moving. So you need to subtract the bias from it. So the code should be:
 Code:int gyroOffset = HTGYROstartCal(gyro);int heading = 0;long currTime;long prevTime = nPgmTime;while (true){    currTime = nPgmTime;    heading += (HTGYROreadRot(gyro) - gyroOffset)*(currTime - prevTime)/1000;    prevTime = currTime;    // Do something else here.    wait1Msec(10);}

 Author: MHTS [ Wed Dec 01, 2010 6:23 pm ] Post subject: Re: Gyro Senosr BTW, if you want a more accurate heading, you may want to integrate at a higher frequency. But then you may not want the main robot code to execute at the same frequency. So you can either do the integration in a separate task but then you have to be careful on multi-threaded access to a shared variable (must use hogCPU and releaseCPU) or you may execute the main robot code only once for every N loops. If you haven't dealt with multi-threaded programming before, you may want to do the latter.

Author:  MHTS [ Sat Dec 04, 2010 6:50 am ]
Post subject:  Re: Gyro Senosr

Just for the fun of it, I coded up a gyro module and use it to drive the turning of the robot instead of the encoder. I learned several things. I thought the gyro may give me more accurate turning than the encoder, it did not. Because of the "noise" of the gyro, the integration of this noise over time even if the robot is not moving resulted in a slow change in heading. Since my PID algorithm allows me to keep the robot straight when driving forward. Because of the drift of the gyro sensor, the robot is slowly turning instead of going straight. So after some experimentation, I changed the turn algorihtm back to use the encoder instead of the gyro. Secondly, I also learned that HTGYROreadRot is already subtracting the offset after calling HTGYROstartCal. So the code I posted previously was incorrect. Also, heading must be float because the delta time per loop is less than one (after dividing 1000) so integer will truncate it to zero. So I must use float to not lose accuracy. The corrected integration code should be:
 Code:float heading = 0.0;long currTime;long prevTime = nPgmTime;HTGYROstartCal(gyro);while (true){    currTime = nPgmTime;    heading += (float)HTGYROreadRot(gyro))*(currTime - prevTime)/1000;    prevTime = currTime;    // Do something else here.    wait1Msec(10);}

 Author: Aswin [ Sat Dec 04, 2010 2:37 pm ] Post subject: Re: Gyro Senosr I wrote a post about using gyro's. Check it out at http://nxttime.wordpress.com/

 Author: MHTS [ Sat Dec 04, 2010 3:02 pm ] Post subject: Re: Gyro Senosr Aswin,Thanks for the excellent info. This would mean that I cannot use Xander's HTGYRO drive because it averages the offset to an integer which loses accuracy. May be Xander can improve it the next time Even if we use float to minimize the loss of accuracy, the drift problem still exists. So one has to be careful in choosing what sensor to use to solve a particular problem. I still think if one needs to know the heading of the robot, gyro may not be a good choice since it involves integration and drifting causes cumulative error in the integration process. I am pretty happy with the accuracy of the encoders keeping track of the heading. Even the encoders would have errors in the long run since it gives you relative heading. The ultimate thing is use an absolute heading device such as the compass. I haven't tackle that sensor yet. I wonder what kind of issues it may have. Several came to mind: magnetic interference and noise. Well, I will tackle that one someday (one sensor at a time)

 Author: mightor [ Sat Dec 04, 2010 4:18 pm ] Post subject: Re: Gyro Senosr MHTS,It's on the TODO list of improvements - Xander

 Author: Aswin [ Sat Dec 04, 2010 5:00 pm ] Post subject: Re: Gyro Senosr The gyro can be used for driving straight. The drift rate is small, a few degrees a minute. By that time most robots have reached the other side of the room. It cannot be used by itself for absolute heading. But that is another discussion.The compass is useless indoors when your home has concrete walls or floors. Disturbances can have a very big effect, 20 degrees is not uncommon in my house.

 Author: MHTS [ Sat Dec 04, 2010 7:27 pm ] Post subject: Re: Gyro Senosr The heading drift I observed is larger than that. May be 1 degree for every two seconds or so. I probably could improve it a little by implementing my own calibration averaging more data points and using float to calculate the offset. The HTGYRO driver averages only 5 data points, so that could be the reason I have a larger drift. Can you read raw A/D data from the gyro instead of in integer units of degrees/sec? (i.e. can you set the gyro in raw mode?)

 Author: mightor [ Sun Dec 05, 2010 2:16 am ] Post subject: Re: Gyro Senosr No, there is no raw mode, at least not in the documentation provided to me by HiTechnic (which tends to have more details than I am able to share). The sensor is already "raw" as it's analogue - Xander

 Author: MHTS [ Sun Dec 05, 2010 2:24 am ] Post subject: Re: Gyro Senosr Normally, a gyro will return an analog signal of 0-5V (or 0-3.3V whichever voltage system the sensor is designed for) and the spec would specify about volt/degree/sec. So the A/D will convert the voltage range into digital value of 0-1023 (10-bit A/D). I assume the gyro firmware is doing that conversion of the raw value into degree/sec. But since it is using integer, it caused conversion error that led to accuracy loss. If there is a raw mode that will give me the 0-1023 raw value, I can do my own conversion using float. That's what I was talking about.

 Author: mightor [ Sun Dec 05, 2010 3:54 am ] Post subject: Re: Gyro Senosr There is no firmware on the gyro. The only chip on there is the actual sensor, nothing more You can see the pictures I took here: [LINK]. There is no accuracy loss, what you get is what the gyro sees.- Xander

Author:  Aswin [ Sun Dec 05, 2010 7:58 am ]
Post subject:  Re: Gyro Senosr

Well, there is loss of accuracy when converting the analogue gyro signal to a digital value. But the conversion is done by the D/A converter in the NXT. There is no way to get a better accuracy from the D/A converter. This however, is not a real problem.

Attached you will find my Gyro driver and some helper files. You can try this to get better gyro information. The driver uses a voltage compensated offset calculated from 500 samples. It gives a new value every 20 msec. This value is offset compensated and is the average of 4 raw samples.

The driver is supplied as-is. You'll have to figure out for yourself how it works. Some directions though:
- include the gyro.c in your main program.
- gyro.c will also include sensor.c and tlist.c.
- the driver will detect the gyro automaticly as long as it is defined by the robotc sensor utility.
- gyro will start a task automaticly. This task will take 2 seconds to calibrate the gyro. The robot must be motionless during this time.
- A struct called Pgyro will be created. This struct contains all kind of usefull information, like turnrate, cumulative turn (in degrees) and heading (as opposed to starting position).

If you are, during execution of your program, quite sure that your robot is motionless you can call the function updateGyroOffset to compensate for drift.

 Author: MHTS [ Sun Dec 05, 2010 10:55 am ] Post subject: Re: Gyro Senosr I haven't opened up a HiTechnic gyro sensor but isn't the interface to NXT is I2C? If so, the A/D must be done in the gyro itself not on the NXT. Unless the RJ connector contains some analog signal pins.

 Page 1 of 3 All times are UTC - 5 hours [ DST ] Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Grouphttp://www.phpbb.com/