//
you're reading...
How-to's, leJOS Features

Getting heading from a gyro

The EV3 comes with a gyro sensor. This sensor measures rate of turn or angle over the vertical axis. Rate of turn is the speed at which the sensor rotates. This is also known as angular speed and is expressed (in leJOS) in degrees per second. Angle is the change in position of the sensor since the sensor was first used. Or since the last time it was reset. Angle is very much like a compass. The difference is that a compass measures the angle from an absolute heading, north. The gyro in angle mode measures from a relative heading, it’s starting position. Angle is not measured directly by the sensor. The sensor can only measure rate of turn directly. Angle is calculated from rate of turn by integration. This calculation is very sensitive to small errors in rate of turn. Lego therefore advices to reset the sensor before every turn. During a reset the angle is reset to zero, but more important, the offset error of the sensor is recalculated. The offset error is the difference between the actual rate of turn and the rate of turn the sensor measures. If this error is known one can correct for it. To determine this error the sensor must be motionless. The offset error is not constant, it therefor must be recalculated regularly.

There are several third party gyro sensors available for the NXT and EV3. Some are able to measure rate of turn over three axes but none is able to provide angle. This is a clear advantage of the Lego sensor. With leJOS however it is very easy to calculate angle from rate of turn. All you need to do is to apply two standard filters, the OffsetCorrectionFilter and the IntegrationFilter. The OffsetCorrectionFilter is applied first. The IntegrationFilter comes second. The OffsetCorrectionFilter uses advanced statistics to continuously update the offset error. There is no need to instruct it to recalculate it. You only have to make sure that the sensor is motionless during the first few reads ( >15 ) from the sensor. Also the IntegrationFilter needs little care. It performs best when the sensor is queried often. But it is not necessary to read it a precise regular intervals. You can reset the accumulated angle every so often. If you happen to have another reference like a compass you can use the angle it provides in the reset. This gives your gyro generated angle an absolute reference.

test setupIt is interesting to know how a software solution performs compared to the Lego gyro sensor. I ran some simple tests to compare three different setups. A Dexter Industries dIMU sensor using the OffsetCorrection and Integraton filters, a EV3 gyro sensor in angle mode and the same EV3 gyro sensor in rate mode using the two filters. I tested the drift over a period of 5 minutes while the sensors were motionless. I tested the outcome of a ninety degree turn and back at high (90 degrees/s) and at slow (22 degrees/s) speed. The three setups were tested simultaneously, thus canceling out the effect of variation in test conditions. I turned the test device by hand.
Below are the test results. The figures in blue is the error from the expected value  (root mean squared error). The bigger this number the bigger the error.

 

 

 

Test dIMU EV3 angle EV3 rate
5 min Drift 2 0 20
. 0 0 36
. 1,0 0,0 20,6
90 degree turn 87 91 88
. 89 91 89
. 88 91 89
. 87 90 88
. 2,4 0,9 1,6
Back 0 1 2
. 0 2 4
. 0 1 2
. -1 0 1
. 0,5 1,2 2,5
slow 90 degree turn 89 87 79
. 87 92 87
. 87 92 86
. 89 90 85
. 2,2 2,1 6,5
slow Back -1 -4 -6
. -3 5 7
. -1 4 4
. 0 1 3
. 1,7 3,8 5,2

The results show that the software solution does not work very well on the EV3 gyro sensor. This setup gave the biggest errors. I think this is because the rate that this sensor returns to the brick is rounded (or truncated) to degrees. The resolution of the dIMU sensor is much better 1/70 of a degree. It gives much better results. The EV3 gyro sensor in angle mode gives best absolute results, although not much better then the software solution. It is interesesting to see that the dIMU performs best at the turn and turn back test. This makes me believe that the dIMU has a scaling error. It would take another filter to compensate for that.

The EV3 in angle mode seems not to drift at all. In rate mode the drift is huge, all on account of rounding off errors. the dIMU has very little drift. A test with really really slow turns showed that the angle given by the EV3 in angle mode remains zero, even after a ninety degree turn. So something in the sensor makes it ignore very small changes.

Overall both the software solution and the EV3 sensor in angle mode perform satisfactory. There is no clear advantage to one one of them. If one goes for the software solution one must use a sensor that has an good resolution.

The tests were run using leJOS 0.8.2 beta. This is not released at the time of writing. The OffsetCorrectionFilter of earlier releases does not perform well.

 

Advertisements

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

About leJOS News

leJOS News keeps you up-to-date with leJOS. It features latest news, explains cool features, shows advanced techniques and highlights amazing projects. Be sure to subscribe to leJOS News and never miss an article again. Best of all, subscription is free!
Follow leJOS News on WordPress.com
%d bloggers like this: