Icon of block

Compass Sensor Block

This block measures the earth's magnetic field to determine which way you robot is heading. It returns the heading as a number ranging from 0 to 359 degrees. Using data wires, it can send out the current heading and a logic signal (true/false) based on whether the current heading falls inside or outside a trigger range.

A trigger range consists of upper and lower limit values in a range of numbers where a change in the reading results in the condition of being either inside or outside the range. For example, you might program your robot to head south so you could set the trigger range to be from 170 to 190. As long as your robot is headed in a direction between these two numbers, the condition will be true.

Specify the trigger points by using the slider or by typing the values into the input boxes. To specify the portion of the compass sensor’s range (inside or outside the trigger range) that will generate the “true” signal, use the radio buttons or the pull-down menu. The “true” portion of the range will be in color; the “false” portion will be gray.

The default setting for the compass sensor block is for headings that are greater than 90 degrees and less than 270 degrees to generate a “true” signal. The compare function above the range allows you to switch the “true” portion of the range to be either inside or outside the range selected.

You must drag at least one output data wire from this block’s data hub to another block for any information to be sent.

Display Settings

Image of block

  1. The number shows which of your NXT’s ports are connected to the compass sensor. You can change this number in the configuration panel if you need to.
  2. The block’s data hub will open automatically when the block is placed in the work area. At least one data wire must be dragged from the block’s output plug to another block’s data hub. The trigger range and other features can be set dynamically by connecting an input data wire. (See the Data Hub section below for more information.)

Configuring the Compass Sensor Block

Image of configuration panel

The sensor may be connected directly to an NXT sensor port or via the HiTechnic Sensor Multiplexer (MUX).  When configuring the sensor via the multiplexer, there are two port selections to make. The first is the NXT to MUX connection and the second, the MUX to SENSOR connection.

Connecting Directly to the NXT

When connecting the sensor directly to the NXT, choose the port the sensor is connected to.

Connecting via the HiTechnic Sensor Multiplexer

To connect the sensor via the multiplexer, click the right arrow symbol  to display the port options.

Click the check box to select that the sensor will be connected vial the MUX.  Configure the MUX to Sensor connection by selecting the MUX port (1 - 4) that is connected to the sensor. 

Click on the right arrow symbol again, to display the NXT to MUX port selection.

Select the NXT port that is connected to the Sensor Multiplexer. 

Configuring Action and Compare

Ensure you select Absolute Reading to operate in this mode. For details about Relative Heading mode, look further down this page. 

If you choose the Compare function Inside Range above the slider, the block will be triggered when the heading is between the two trigger values; select Outside Range to trigger the block when the heading is outside the two trigger values. Use the slider to set the trigger values or type them directly into the input boxes (0–359). 

Note: The compass measures the very weak earth's magnetic field. Ensure that is mounted at least 4 inches or 10 centimeters away from the NXT and motors. Also ensure that it is mounted horizontally as the heading will not be accurate if the compass isn't level.

Configuring the Compass Sensor block’s Data Hub

You can control the Compass Sensor block dynamically by connecting data wires (from other blocks’ data hubs) to the Compass Sensor block’s data hub.

Open a block’s data hub by clicking the tab at the lower left edge of the block after it has been placed on the work area.

Image showing unopened tab on a generic block

Data wires carrying input information to a block are connected to the plugs on the left side of its data hub. Data wires carrying output information are connected to the plugs on the right side.

Image of data wires

[A] Input plug
[B] Output plug
[C] Number data wire (yellow)
[D] Logic data wire (green)
[E] Text data wire (orange)
[F] Broken data wire (gray)

Passing data from the input plug to the output plug

If an input plug has a corresponding output plug (see A above), the input data will pass through from the input plug to the output plug without being changed. In this case, you can only use the output plug if the input plug is connected to an input data wire; connecting an output data wire to such an output plug without a connected input data wire will cause the output data wire to be “broken” (and colored gray).

Data wires carry specific types of data

Each data wire carries a specific type of data between blocks. For example, if a data wire is dragged from a logic plug on a block’s data hub, it can only be connected to a logic plug on another block’s data hub. The chart below shows what kind of data each plug can accept or send out.

Data wire colors

Data wires are identified with specific colors: wires carrying number data are colored yellow, wires carrying logic data are colored green, and wires carrying text data are colored orange.

“Broken” data wires

If you try to connect a data wire to a plug of the wrong data type, the data wire will be broken (and colored gray). You will not be able to download your program if a data wire is broken.

If you click a broken wire you can read why it is broken in the small help window in the lower right corner of the work area.

Data must be within the possible range of the plug

If an input data wire transmits a value outside the possible range of the plug it is connected to, the block will either ignore the value or change it to a value within its range. For plugs that allow just a few input values (example: just 0, 1, or 2), the plug will ignore the input if a value arrives outside its range.

For plugs that accept larger input ranges (example: 0 – 100), the plug will force any input outside its range to fit. For example, if a Move block’s Power plug receives an input value of 150, the block will change the input value to 100 (i.e., a number within the Power plug’s range).

Compass Sensor block Data Hub plugs

You must click on the Data Hub to open it as shown.

Image of block

 

  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 Range selection for inside/outside range function.
  5. This plug wires the lower limit number for the Inside/Outside compare function.
  6. This plug wires the upper limit number for the Inside/Outside compare function.
  7. This plug outputs the current Absolute Heading number.
  8. This plug outputs the Inside/Outside logical value.
  9. This plug outputs the current Relative Heading number, see section on Relative Heading further down this page.
  10. This plug is set to true if the sensor is connected via the HiTechnic sensor multiplexer.
  11. this plug wires the number of the multiplexer port connected to the sensor .

Configuring the Compass Sensor Block for Relative Heading

Image of configuration panel

The Compass Block may be operated in Relative Heading mode. This is provided to make it easier to create a program which allows your robot to travel in a particular direction. You can select this mode by changing the Action to Relative Reading.

After you have selected Relative Reading, you'll notice that the Target box becomes enabled and the compare slider limits change to -90 and 90.

In Relative Heading mode, the Target heading is compared with the current compass heading and the difference is used instead of the absolute heading. This makes it much easier to control the steering of your robot. Simply set the Target heading to the direction you want your robot to go in and then set the compare range from -90 to 0. This will make it trigger if you need to turn left else you need to turn right.

Calibrating the Compass Sensor

Image of configuration panel

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 the Calibrate action (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 that it completes a full turn in about 20 seconds. Once the turns have been completed, use another Compass Sensor block with the Action set to either Absolute or Relative Reading to bring the sensor out of calibration mode. If the calibration failed, the Absolute Heading returned by this block will be -1. To test if the calibration was successful with this block, set the action to test the Absolute Heading for Inside Range 0 to 360; then the Yes/No output will be True only if the sensor calibration was successful. 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