Gyroscopes

A gyroscopes is a device that can measure rate of rotation without the need of an external reference. Gyroscopes are commonly used to measure angles, which is done by adding up the gyroscope readings over time. The rate integration is quite simple:

ANGLE = ANGLE + RATE * PERIOD

The variable ANGLE can be initialized to a known starting angle (typically zero). Some sensors perform this integration internally while some of them add other methods to reduce errors (i.e. Kalman filters). I ran basic tests on two sensors that perform such integrations internally, the Lego Ev3 gyro and the Microinfinity XG1300L. The angle reported by these two sensors is displayed without any further process. The sensors are rigidly attached together so I can compare their performance side-by-side. I built a simple 90 degree angle frame from Lego parts used as reference for this test. The setup can be seen below:

GyroCompSetup

The following video shows a typical result of an experiment:

In the test, I am not exceeding the dynamic ranges of the sensors (which for the Microinfinity sensor is 150 deg/sec and for the Lego Gyro is 440 deg/sec). Short experiments like this ones emphasize the problems associated with (non-linear) scale factors. The inaccuracy shown by the Lego EV3 gyro makes it not very suitable for localization purposes, although it may still be adequate for some other tasks that require less accuracy or very short period stability.

Gyroscope Error Sources
There are many sources of error that can affect the gyroscopes. Some of them are deterministic and with proper procedures can be estimated and compensated. From this category we can list (in order of significance):
– Static bias drift
– Scale factors
– Non-linearity of the scale factor
– Acceleration sensitivity
– Effects of temperature in static bias and scale factor

Other errors occur randomly and cannot be eliminated, but their effect in the overall system performance can be estimated using stochastic propagation techniques. From this category we can list:
– Drift instability
– Effects of temperature in drift

Finally, there are some limitations of the sensor itself or the way it is used that can also affect its performance. For example:
– Bandwidth limitations
– Dynamic range
– Digital Resolution
– Electrical noise
– Vibration

A combination of all these sources of errors contribute to the overall sensor performance.

Source Code
Below is the code that I used to test this setup. I used the lmsasm tool from the EV3 source code to compile it. The program runs directly in the Lego Virtual Machine:

//READ LOOP FOR LEGO AND XGL GYROS USING MANUAL POLL
//THE FIRST ANGLE VALUES ARE USED AS ZERO ANGLE
//THE HANDLING OF CHANGES FROM -PI to PI IS NOT WORKING FOR XGL

define LEGO_PORT 1
define XGL_PORT 0
define READ_INTERVAL 330

vmthread MAIN
{
//Lego Variables
DATA16 LegoReading
DATA16 LegoLastReading
DATA16 LegoAngle

//XGL Variables
DATA16 AuxWord
DATA8 AuxByte
DATA16 IicCommand
DATA16 IicReading
DATA16 XglReading
DATA16 XglLastReading
DATA16 XglAngle

//Other Variables
DATA16 AngleDiff
DATA16 IsAngleReset
DATA32 ReadoutTimer
DATAF AuxFloat

// Clear display
UI_DRAW(FILLWINDOW,BG_COLOR,0,0)
// Initialize Arrays
ARRAY(CREATE8,0,IicCommand)
ARRAY(CREATE8,0,IicReading)
// Setup manual poll for I2C XGL device
ARRAY_WRITE(IicCommand,0,0x01)
ARRAY_WRITE(IicCommand,1,0x42)
MOVE16_16(0,IsAngleReset)

Loop:
// Send read command to I2C XGL device
INPUT_DEVICE(SETUP,0,XGL_PORT,1,0,2,@IicCommand,2,@IicReading)
// Move bytes to word variables
ARRAY_READ(IicReading,0,AuxByte)
MOVE8_16(AuxByte,XglReading)
ARRAY_READ(IicReading,1,AuxByte)
MOVE8_16(AuxByte,AuxWord)

//Assemble XGL packet
AND16(XglReading,0xFF,XglReading)
MUL16(XglReading,256,XglReading)
AND16(AuxWord,0xFF,AuxWord)
ADD16(XglReading,AuxWord,XglReading)

//Compute XGL Angle based on rates
SUB16(XglReading,XglLastReading,AngleDiff);
//Take care of angles, when it goes from -PI to PI and vice versa
//THIS PART DOES NOT WORK CORRECTLY
JR_LT16(AngleDiff,10000,XGL_DIFF_POS)
SUB16(AngleDiff,36000,AngleDiff);
XGL_DIFF_POS:

JR_GT16(AngleDiff,-10000,XGL_DIFF_NEG)
ADD16(AngleDiff,36000,AngleDiff);
XGL_DIFF_NEG:
ADD16(XglAngle,AngleDiff,XglAngle);
MOVE16_16(XglReading,XglLastReading);

//Read the LEGO Gyroscope
INPUT_DEVICE(READY_SI,0,LEGO_PORT,0,0,1,AuxFloat)
MOVEF_16(AuxFloat,LegoReading)
AND16(LegoReading,0xFF,LegoReading)
MOVE16_F(LegoReading,AuxFloat)

//Compute Lego Angle based on rates
SUB16(LegoReading,LegoLastReading,AngleDiff);

//Take care of angles, when it goes from 0 to -1 (255)
JR_LT16(AngleDiff,100,LEGO_DIFF_POS)
SUB16(AngleDiff,256,AngleDiff);
LEGO_DIFF_POS:

JR_GT16(AngleDiff,-100,LEGO_DIFF_NEG)
ADD16(AngleDiff,256,AngleDiff);
LEGO_DIFF_NEG:

ADD16(LegoAngle,AngleDiff,LegoAngle);
MOVE16_16(LegoReading,LegoLastReading);

//Reset angles after first reading
JR_GT16(IsAngleReset,0,NO_RESET_NEEDED)
MOVE16_16(0,XglAngle)
MOVE16_16(0,LegoAngle)
ADD16(IsAngleReset,1,IsAngleReset);
NO_RESET_NEEDED:

//Print result on display
UI_DRAW(SELECT_FONT,LARGE_FONT)
UI_DRAW(TEXT,FG_COLOR,10,25,'GYRO DEG')
// Write XGL Value to display
//In order to use the same units as the Lego Gyro, dive Xgl result by 100
MOVE16_F(XglAngle,AuxFloat)
DIVF(AuxFloat,100.0F,AuxFloat)
UI_DRAW(VALUE,FG_COLOR,40,50,AuxFloat,6,0)
UI_DRAW(TEXT,FG_COLOR,10,50,'MINF')
//Lego gyro value is displayed as is
MOVE16_F(LegoAngle,AuxFloat)
UI_DRAW(VALUE,FG_COLOR,40,75,AuxFloat,6,0)
UI_DRAW(TEXT,FG_COLOR,10,75,'LEGO')
UI_DRAW(UPDATE)
// Wait
TIMER_WAIT(READ_INTERVAL,ReadoutTimer)
TIMER_READY(ReadoutTimer)
JR(Loop)

// Clean up
ARRAY(DELETE,IicReading)
ARRAY(DELETE,IicCommand)
}

2 Responses to Gyroscopes
  1. Parth says:

    Since only Lego sensors can be used in competition, do you think that there would be a reliable way to detect the angle? Something like resetting after each measurement, so that it is always measuring from 0?

    • admin says:

      There are calibration procedures that can help reducing the deterministic errors. However for the random errors there is little you can do. You can recalculate (reset) some of the errors such as: static bias and some effects of temperature in bias, but whatever error you already have will remain unchanged.