Using the package org.pi4.loclib.f350compassfd

You might have noticed that the Location API does not provide an interface for the use of a digital compass. That means that data provided by a compass can not be accessed through the Location API. Below you will find instructions on how to use our communication protocol for the FD-350 compass reference board along with your own software.

General

The front-ending provided by the compass package is basically represented by the singleton class InputOutputHandler in combination with the interface CompassListener and the class CompassBoardReply that represents the actual data and provides methods for extracting it.
InputOutputHandler can be instantiated through the methods getInstance(int) and getInstance(File, boolean).

The package offers three sub-classes of InputOutputHandler of which instances are generated depending on which getInstance method is used and what arguments are specified. The sub classes are:

  • SerialPortInputOutputHandler: reads data from a serial port.
    This class is instantiated when getInstance(int) is called.
  • SerialFileInputOutputHandler: reads data from a linux device (e.g. dev/ttyUSB0).
    This class is instantiated when getInstance(File, boolean) is called, where the boolean argument is false.
  • FileInputOutputHandler: reads data from a file (mainly for testing purposes).
    This class is instantiated when getInstance(File, boolean) is called where the boolean argument is true.

Finally, a CompassListener object can be added to the InputOutputHandler by using the method addCompassListener(CompassListener).

The CompassListener object offers a method handleCompassBoardReply(CompassBoardReply) that is called everytime a data block is received from the compass. As already mentioned above, the data is stored in objects of the type CompassBoardReply.

The container class CompassBoardReply offers several methods with which the data can be extracted. The methods are as following:

  • getDirectionDegrees() : int
  • getDirectionMinutes() : int
  • getTemperatureDegreesCelsius() : float
  • getTemperatureDegreesFahrenheit() : float
  • getInclinationX() : int
  • getInclinationY() : int
  • getBatteryStatus() : int
  • getCalibrationStatus() : int
  • dump() : String

Additional information is provided by the corresponding javadoc.

Example code

        import org.pi4.loclib.f350compassfd;

        public class CompassExample implements CompassListener {
          
          private InputOutputHandler ioHandler;

          public CompassExample() {
            ioHandler = InputOutputHandler.getInstance(3);
            ioHandler.addCompassListener(this);
          }

          // Inherited by the CompassListener interface
          public void handleCompassBoardReply(CompassBoardReply reply) {
            System.out.println(reply.dump());
          }

          public static void main(String[] args) {
            new CompassTest();
          }
        }