In this example we will be using the IMU magnetometer make a compass.
The magnetometer in the IMU will measure the strength of the magnetic field. This is what allows it to pick up the earth's magnetic field, however at the same time it will also pick up external magnetic influences.
Before creating your compass you will need to remove the offsets caused by Hard-iron distortion. In the case of the blocks, the magnets in the blocks will cause changes in the readings. You'll notice this disturbance in the magnetometer readings when removing and adding blocks on top of the IMU.
We will be removing these offsets by creating some calibration logic in the Logic Maker.
- First go into the Logic Maker of the IMU.
- Insert the following Logic Gates and link them as shown below.
>DISPLAY INPUT > MAX & MIN >FORMULA >DISPLAY >DISPLAY
- Select "my" from the INPUT Gate.
- In the FORMULA Gate type "(a+b)/2"
- Create a copy of the same logic, and select "mx" instead.
- If you find that your compass is not working correctly you may need to re-calibrate it. To do this insert a PLAY Gate to reset the MIN & MAX Gates.
- Move the stack around in all directions to find the offsets of the magnetometer. You will see that your magnetometer is fully calibrated once the min & max values no longer change.
Creating the LED Matrix patterns
To create the compass you will also need to create patterns in the LED Matrix.
- First link the LED Matrix Block and IMU Block together in the Workspace.
- Create arrows for each of the directions required. You can follow the guide arrows below or create your own.
- Once finished you should have 8 patterns saved. Each patter should point each of the directions required on a compass.
Calculating the angle from north
- To create these you will first need to remove the offset created previously. To do this use the shortcut.
By right clicking the formula gate you will be able to see a Shortcut option. Create a shortcut for each of the FORMULA Gates.
- With your newly created Shortcuts of the FORMULA Gates create the logic shown below.
When connecting up make sure that you the shortcut you created to the offset value matches with the input source. The corresponding shortcuts are highlighted in the same colour in the image below.
FORMULA (shortcut mx) > FORMULA INPUT (mx) > > FORMULA >PARSER >DISPLAY INPUT (my) > FORMULA > >LED ARROW FORMULA (shortcut my) >
HOW IT WORKS:
The first step is to remove the offsets with the FORMULA GATE. With the offsets removed you now have isolated the magnetometer readings of the earths magnetic field.
Next is to calculate the angle from north. With the two magnitudes from the mx and my you are able to use the atan2(a,b) function to find the angle from north in radians.
Next another FORMULA Gate is used to convert the radians to degrees.
Finally a parser gate is used to convert the number to an integer and the angle is displayed in a DISPLAY Gate.
- Within the LED ARROW GROUP each of the LED Matrix Patterns are chosen depending on the angle calculated by the compass.
The image on the right shows the corresponding directions for each of the angle segments.
NOTE: Since you want your arrow to always point north, you will flip the LED Pattern along the north axis in the image on the right. So when the compass is facing east, you will show the west LED pattern.
You should now have a working compass that points north no matter which way you spin the blocks.