Expand your NXT experiences with the new NXT Compass Sensor and add accurate navigation to your Mindstorms® NXT projects. The NXT Compass Sensor is a digital compass that measures the earth's magnetic field and outputs a value representing the current heading. The magnetic heading is calculated to the nearest 1° and returned as a number from 0 to 359. The NXT Magnetic Compass Sensor updates the heading 100 times per second. The Compass Sensor operates in two modes, Read mode and Calibrate mode. In Read mode, the current heading is calculated and returned each time to the NXT program executes a read command. In Calibrate mode the compass can be calibrated to compensate for externally generated magnetic field anomalies such as those that surround motors and batteries, thereby maintaining maximum accuracy.
The NXT Compass Sensor contains a digital magnetic compass that measures the earth’s magnetic field and calculates a heading angle. The Compass Sensor connects to an NXT sensor port using a standard NXT wire and uses the digital I2C communications protocol. The current heading is calculated to the nearest 1° and refreshed 100 times per second.
The Compass Sensor is housed in a standard Mindstorms sensor housing to match the other Mindstorms elements.
To test your new sensor, plug it into port 2 of your NXT and select View > Ultrasonic cm > Port 2. As you swing the sensor around, you’ll notice that the readings will change from 1 – 179. (0 will display as ?????? while in View mode.)
Programming
Mindstorms NXT-G
The compass can be programmed using LEGO Mindstorms NXT Software Compass Block*. If the Compass Block is not available you may also use the standard Ultrasonic Block.
Ultrasonic Block
If using the Ultrasonic Block to program the compass, configure the block to centimeter mode as shown.
The Ultrasonic Block can only return values from 0 to 250 so in order to return the full range of headings, 0 to 360, the heading value returned when using the Ultrasonic Block will be the current heading divided by 2. For example, if the current heading is 220°, the value returned will be 110. North will return a value of 0, East, a value of 45, South, a value of 90 and West, a value of 135. To get the actual magnetic heading, simply take the number returned and multiply by 2.
Compass Sensor Block
The Compass Block is designed to support the HiTechnic Compass Sensor and supports all compass features.
1. This plug wires the number for which of your NXT’s ports are connected to the compass sensor.
2. This plug wires the number of the Action to be performed.
3. This plug wires the Target heading, see section on Relative Heading further down this page.
4. This plug wires the lower limit number for the Inside/Outside compare function.
5. This plug wires the upper limit number for the Inside/Outside compare function.
6. This plug outputs the current Absolute Heading number.
7. This plug outputs the current Relative Heading number, see section on Relative Heading further down this page.
8. This plug outputs the Inside/Outside logical value.
Compass Front Panel
Port allows you to select the NXT port the compass is plugged into.
Action
Action has three options. These are;
Absolute Reading: Allows you to read the Absolute Heading, a value from 0 to 359, corresponding to the current magnetic heading in degrees. (Plug 6 on the data hub)
Relative Reading: Allows you to set a Target and read the Relative Heading (Plug 3 and 7 on the data hub).
See Using Relative Reading below for more information.
Calibrate: Set the compass sensor into Calibrate mode allowing you to go through the calibrate sequence to minimize the influence of metal and other other sources of magnetic interference such as motors or batteries.
See Calibrating the Compass Sensor below for more information.
Compare
Compares the Absolute Heading to the range set and sets the logic value to show if the value is inside or outside the range.
See Using Range below for more information.
Using Relative Reading
Relative Reading makes it easy to stay on a selected heading. To use this feature select the Relative Reading action. Then enter a Target that is the heading you wish to maintain.
If you wanted to navigate South, you would enter a Target of 180 (South is 180 degrees). The Compass Sensor block would then calculate the difference between the Absolute Heading and your Target and return the difference as the Relative Heading. (plug 7).
For example, with your target set to 180, if the Absolute Heading were 175, then the Relative Heading would be 5 (Target – Absolute Heading or 180 – 175 = 5). This indicates a turn to the right or clockwise of 5 degrees is required to get back to the Target.
If the Absolute Heading were 185, then the Relative Heading would be -5 (Target – Absolute Heading or 180 – 185 = -5). This indicates a turn to the left or clockwise of 5 degrees is required to get back to the Target.
Calibrating the Compass Sensor
The HiTechnic compass sensor will be influenced by local magnetic interference in the same way as any other magnetic compass. Objects such as metal items, motors, batteries and wires can cause magnetic interference. Local magnetic interference may cause the compass to output a heading several degrees different than the actual magnetic heading. This is called compass deviation. To correct for deviation, the HiTechnic compass sensor has a built in calibration function that calculates correction offsets and stores them in the compass.
Calibration is optional and usually not required for normal operation. To minimize the need for calibration make sure you mount the compass at least 4 – 6 inches (10 – 15 cm) away from the NXT and NXT motors.
To calibrate the compass under program control, select calibration mode (value 2 wired to the Action plug input) and then program your robot to rotate (turn in a tight circle) between 1 ½ and 2 times (more than 360 degrees) so it completes a full turn in about 20 seconds. Once the turns have been completed, set the Action to a Read function (Absolute or Relative) and the compass will be calibrated. The compass will retain the calibration settings until another calibration is performed, even if it is unplugged.
Using Range
The Compare lets you set a range and easily determine if you are inside or outside the range.
The Components for using the Range function are;
Inside Range / Outside Range: Select if you want to test for an Absolute Heading inside the range or outside the range.
A: The lower limit of the range (0 – 359)
B: The upper limit of the range (0 – 359)
To use the Range function, set the lower and upper limit of your range and whether you want to test for inside or outside the range.
Examples showing the logical result.
A
|
B
|
Range
|
Absolute Heading
|
Logical Value
|
85
|
95
|
Inside Range
|
90
|
|
85
|
95
|
Inside Range
|
100
|
|
85
|
95
|
Outside Range
|
90
|
|
85
|
95
|
Outside Range
|
100
|
|
The heading is obtained by reading location 0x42 to obtain the two degree heading and 0x43 to obtain the one degree adder. The heading can then be computed as
Heading = (two degree heading *2) + one degree adder.
If the sign is a problem, then the conventional 16 bit value can be obtained from 0x44 (low byte) and 0x45 (high byte).
Locn 0x41 is the mode control. Defaults to 0, which is measurement mode. Change to 0x43 to set calibration mode. Must be changed back to 0 when calibration is completed. (If cal. fails, locn. 0x41 will be set to 2 after the host has set it to 0 at the end of the failed calibrate attempt.)