With this article, I will explain a few aspects of using a gyro and give some hints on how to start exploring it.
What is a Gyroscope
A gyro is an attitude indicator. It provides indication of yaw, pitch and roll of a vehicle. These are relative measurements, comparing the current orientation with the orientation at time of calibration of the gyro. The gyro can not be used to measure the absolute geographic orientation, like a magnetic compass. But the absolute orientation can be calculated, when the absolute orientation in space during calibration is known (not taking into account the drift here, for simplicity).
In general, a gyro is not a navigational device! It can detect the momentarily attitude of a device, but it can not be used to keep a given course or heading!
The MeGyro Module
The makeblock MeGyro module also contains an accelerometer and a gravitometer. It measures gravity and acceleration along three cartesional axes, and from these measurements it calculates the deflection of the current orientation of the module from its orientation at last calibration time.
How to Apply the MeGyro Module in Practice
When looking at the gyro module a small drawing is visible, indicating the coordinate system. The gyro delivers rotation angles along these three axes. It is important to mount the gyro so that the axes point to the right directions, in order to match the familiar coordinate systems.
The X-axis should represent the length axis of the vehicle. It will then indicate the “roll” attitude.
The Y-axis points through the sides of the vehicle. The returned values for Y represent the “pitch” attitude.
The Z-axis is the vertical axis, pointing through the vehicle. It provides the “yaw” attitude. The Z-Axis can point upwards or downwards, which will result in either positive or negative values for “right” and “left” yaw, or heading deviation.
The values returned by gyro.getAngleX/Y/Z, after gyro.update() are given in +/- degrees deviation from what has been measured/calculated at gyro.begin(), when X, Y, and Z are set to “0”. So, if Z = 3.0, it means that the vehicle has turned 3 degrees to the right (I prefer right to be positive), since the last calibration of the gyro.
Note, that values for pitch and roll (X and Y axes) are defined within a range of +/-90 deg, while the heading (Z-axis) is defined as 360 deg. When doing a looping, starting at horizontal orientation, the pitch will reach its maximum deflection angle at the vertical orientation, at 90 deg. from there onwards, the angle to the horizontal pane will decrease again, to 0. At the capsized horizontal position the pitch will be 0 again and increase towards the maximum of -90 deg, from where it increases again to 0 - the initial orientation. The same mechanism applies to the roll axis, when performing a roll.
The MeGyro Example Sketch
This sketch should be used to find out about the correct function and orientation of the module. It is important to use Makeblock Library V 3.X for this, as earlier libraries are buggy for the gyro. Just load and run the sketch and move the module, simulating pitch, yaw and roll. Observe the measurements, returned for the three angles by watching the output of the sketch in serial monitor. Once you know how to mount the module on your vehicle you are ready to go. For a start, you could write a sketch to let the module drive straight forward, self-correcting for deviations of the heading.
Heading (Z) Axis and Drift
The meGyro “gyro.getAngleZ” function delivers the heading as deflection since the last calibration (last call of gyro.begin()). However, if you turn the module several times by 360 deg you will notice that the returned values grow above 360. If you want to achieve headings between 0 and 360 deg, you could use the fmod function, like this:
fmod(gyro.getAngleZ(),360.0). This will always return values between 0 and 360 or -360.
To me, the behaviour of Z is strange, the values returned by gyro.getAngleZ() should always be in the range of +/-180, but I may be wrong. Anyways, while X and Z deliver rather stable values, Z is affected by a large drift. At my module, the value drifts by almost 1 deg/sec, which is way too much for a practical usage to keep the heading of a module straight. Commercial gyros can keep the Z-value much more constant, but they cost around 50,000$ +