View unanswered posts | View active topics It is currently Tue Feb 19, 2019 3:01 am

 Page 1 of 3 [ 42 posts ] Go to page 1, 2, 3  Next
 Print view Previous topic | Next topic
Gyro Senosr
Author Message
Rookie

Joined: Wed Dec 01, 2010 5:15 pm
Posts: 31
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

Wed Dec 01, 2010 5:17 pm
Guru

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
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.

Wed Dec 01, 2010 6:01 pm
Guru

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
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);}

Wed Dec 01, 2010 6:09 pm
Guru

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
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.

Wed Dec 01, 2010 6:23 pm
Guru

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
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);}

Sat Dec 04, 2010 6:50 am
Expert

Joined: Mon Oct 06, 2008 6:30 pm
Posts: 176
Location: Netherlands
Re: Gyro Senosr
I wrote a post about using gyro's. Check it out at http://nxttime.wordpress.com/

_________________
My most recent blog: A grain of sugar

Sat Dec 04, 2010 2:37 pm
Guru

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
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)

Sat Dec 04, 2010 3:02 pm

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Re: Gyro Senosr
MHTS,

It's on the TODO list of improvements

- Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]

Sat Dec 04, 2010 4:18 pm
Expert

Joined: Mon Oct 06, 2008 6:30 pm
Posts: 176
Location: Netherlands
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.

_________________
My most recent blog: A grain of sugar

Sat Dec 04, 2010 5:00 pm
Guru

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
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?)

Sat Dec 04, 2010 7:27 pm

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
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

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]

Sun Dec 05, 2010 2:16 am
Guru

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
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.

Sun Dec 05, 2010 2:24 am

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
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

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]

Sun Dec 05, 2010 3:54 am
Expert

Joined: Mon Oct 06, 2008 6:30 pm
Posts: 176
Location: Netherlands
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.

_________________
My most recent blog: A grain of sugar
Sun Dec 05, 2010 7:58 am
Guru

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
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.

Sun Dec 05, 2010 10:55 am
Display posts from previous:  Sort by
 Page 1 of 3 [ 42 posts ] Go to page 1, 2, 3  Next

#### 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