Skip to content
Rick Waldron edited this page Mar 22, 2023 · 34 revisions

The Compass class constructs an object that represents a single Compass or Magnetometer.

Supported Compass/Magnetometer:

This list will continue to be updated as more component support is implemented.

Parameters

  • Options An object of property parameters.

    Property Type Value/Description Default Required
    controller String BNO055, HMC6352, HMC5883L, MAG3110, LSM303C. The name of the controller to use yes
    gauss Number cgs units. Set the scale gauss for compass readings. 1.3 no

Shape

Property Name Description Read Only
raw The raw x/y/z values. Yes
heading The current heading in degrees, 0-360° Yes
bearing An object of properties whose values are relevant bearing information ★ Yes
  • bearing

    Property Name Description Read Only
    point A cardinal direction, eg. "north", "south", "east", "west" No
    abbr Abbreviated point, eg. "N", "NE", "NEbE" No
    low Low end of cardinal range in degrees No
    mid Middle end of cardinal range in degrees No
    high High end of cardinal range in degrees No

Component Initialization

BNO055

new five.Compass({
  controller: "BNO055"
});

BNO055

HMC6352

new five.Compass({
  controller: "HMC6352"
});

HMC6352

HMC5883L

new five.Compass({
  controller: "HMC5883L"
});

HMC5883L

MAG3110

new five.Compass({
  controller: "MAG3110"
});

MAG3110

LSM303C

// Create an LSM303C Compass/Magnetometer object:
//
//  - attach SDA and SCL to the I2C pins on your board (A4 and A5 for the Uno)
//  - specify the LSM303C controller
new five.Compass({
  controller: "LSM303C"
});
// Or...
new five.Magnetometer({
  controller: "LSM303C"
});

imu-lsm303c.png

Usage

var five = require("johnny-five");
var board = new five.Board();

board.on("ready", function() {

  var compass = new five.Compass({
    controller: "HMC6352"
  });

  compass.on("change", function() {
    console.log("change");
    console.log("  heading : ", Math.floor(this.heading));
    console.log("  bearing : ", this.bearing.point);
    console.log("--------------------------------------");
  });

  compass.on("data", function() {
    console.log("  heading : ", Math.floor(this.heading));
    console.log("  bearing : ", this.bearing.point);
    console.log("--------------------------------------");
  });
});
var five = require("johnny-five");
var board = new five.Board();

board.on("ready", function() {

  var compass = new five.Compass({
    controller: "HMC5883L"
  });

  compass.on("change", function() {
    console.log("change");
    console.log("  heading : ", Math.floor(this.heading));
    console.log("  bearing : ", this.bearing.point);
    console.log("--------------------------------------");
  });

  compass.on("data", function() {
    console.log("  heading : ", Math.floor(this.heading));
    console.log("  bearing : ", this.bearing.point);
    console.log("--------------------------------------");
  });
});

API

There are no special API functions for this class.

Events

  • change The "change" event is emitted whenever the heading of the compass has changed from it's last position
  • data The "data" event is fired as frequently as the user defined freq will allow in milliseconds.

Examples

Clone this wiki locally