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.

    PropertyTypeValue/DescriptionDefaultRequired
    controllerStringBNO055, HMC6352, HMC5883L, MAG3110. The name of the controller to useyes
    gaussNumbercgs units. Set the scale gauss for compass readings.1.3no

Shape

Property NameDescriptionRead Only
headingThe current heading in degrees, 0-360°Yes
bearingAn object of properties whose values are relevant bearing information ★Yes
  • bearing

    Property NameDescriptionRead Only
    pointA cardinal direction, eg. "north", "south", "east", "west"No
    abbrAbbreviated point, eg. "N", "NE", "NEbE"No
    lowLow end of cardinal range in degreesNo
    midMiddle end of cardinal range in degreesNo
    highHigh end of cardinal range in degreesNo

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

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

Hi! The Johnny-Five community is building new projects every day. We made this newsletter to tell you about what's new, what's good, and what's next for Open Source robotics. Join us in exploring what we can make together.

Fork me on GitHub