Class Navx

java.lang.Object
com.studica.frc.Navx
All Implemented Interfaces:
AutoCloseable

public class Navx extends Object implements AutoCloseable
Studica NavX3 IMU over CAN or USB.

Any number of Navx objects can be open, one per device. Data arrives in the background; getters return the latest value, or Optional.empty() when that message group isn't streaming or its data has gone stale.

If the device can't be opened, the error is reported to the Driver Station and the object stays inert: getters return empty and commands return Status.INVALID_HANDLE.

  • Constructor Details

    • Navx

      public Navx(int canId)
      Opens a NavX3 on CAN bus S0 at 100 Hz.
      Parameters:
      canId - CAN device ID set on the NavX3
    • Navx

      public Navx(int canId, org.wpilib.hardware.bus.CANPort bus)
      Opens a NavX3 on CAN at 100 Hz.
      Parameters:
      canId - CAN device ID set on the NavX3
      bus - CAN port it is wired to
    • Navx

      public Navx(int canId, org.wpilib.hardware.bus.CANPort bus, int updateRateHz)
      Opens a NavX3 on CAN.
      Parameters:
      canId - CAN device ID set on the NavX3
      bus - CAN port it is wired to
      updateRateHz - output data rate, 1-200 Hz
  • Method Details

    • usb

      public static Navx usb()
      Opens the NavX3 plugged into any USB port, at 100 Hz. It may be moved between USB ports while running. With more than one NavX3 on USB, use usb(UsbPort) for each instead.
      Returns:
      the Navx
    • usb

      public static Navx usb(int updateRateHz)
      Opens the NavX3 plugged into any USB port. It may be moved between USB ports while running.
      Parameters:
      updateRateHz - output data rate, 1-200 Hz
      Returns:
      the Navx
    • usb

      public static Navx usb(UsbPort port)
      Opens the NavX3 in a specific SystemCore USB port, at 100 Hz.
      Parameters:
      port - the USB port, as labelled on the SystemCore
      Returns:
      the Navx
    • usb

      public static Navx usb(UsbPort port, int updateRateHz)
      Opens the NavX3 in a specific SystemCore USB port.
      Parameters:
      port - the USB port, as labelled on the SystemCore
      updateRateHz - output data rate, 1-200 Hz
      Returns:
      the Navx
    • fromSerialDevice

      public static Navx fromSerialDevice(String devicePath, int updateRateHz)
      Opens a NavX3 on an explicit serial device.
      Parameters:
      devicePath - e.g. "/dev/ttyACM0"
      updateRateHz - output data rate, 1-200 Hz
      Returns:
      the Navx
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
    • isOpen

      public boolean isOpen()
      Returns whether the device was opened.
      Returns:
      false if the device could not be opened or has been closed
    • isConnected

      public boolean isConnected()
      Returns whether the device is talking.
      Returns:
      true if data arrived recently
    • getYaw

      public Optional<org.wpilib.units.measure.Angle> getYaw()
      Gets the heading.
      Returns:
      yaw, typically [-180, 180] degrees, or empty if not streaming
    • getPitch

      public Optional<org.wpilib.units.measure.Angle> getPitch()
      Gets the pitch.
      Returns:
      pitch, typically [-90, 90] degrees, or empty if not streaming
    • getRoll

      public Optional<org.wpilib.units.measure.Angle> getRoll()
      Gets the roll.
      Returns:
      roll, typically [-180, 180] degrees, or empty if not streaming
    • getAngle

      public Optional<org.wpilib.units.measure.Angle> getAngle()
      Gets the continuous heading, which does not wrap at +/-180 degrees.
      Returns:
      the angle, or empty if not streaming
    • getRotation2d

      public Optional<org.wpilib.math.geometry.Rotation2d> getRotation2d()
      Gets the heading as a Rotation2d, from getYaw().
      Returns:
      the heading, or empty if not streaming
    • getRotation3d

      public Optional<org.wpilib.math.geometry.Rotation3d> getRotation3d()
      Gets the orientation as a Rotation3d, from the 6-axis quaternion.
      Returns:
      the orientation, or empty if not streaming
    • getQuaternion6d

      public Optional<org.wpilib.math.geometry.Quaternion> getQuaternion6d()
      Gets the fused gyro + accelerometer orientation.
      Returns:
      the quaternion, or empty if not streaming
    • getQuaternion9d

      public Optional<org.wpilib.math.geometry.Quaternion> getQuaternion9d()
      Gets the fused gyro + accelerometer + magnetometer orientation.
      Returns:
      the quaternion, or empty if not streaming
    • getAngularVelocity

      public Optional<Navx.AngularVelocity3d> getAngularVelocity()
      Gets the angular velocity.
      Returns:
      angular velocity about each axis, or empty if not streaming
    • getLinearAcceleration

      public Optional<Navx.Acceleration3d> getLinearAcceleration()
      Gets the linear acceleration.
      Returns:
      acceleration along each axis, or empty if not streaming
    • getMagneticField

      public Optional<Navx.MagneticField> getMagneticField()
      Gets the magnetometer reading.
      Returns:
      the magnetic field, or empty if not streaming
    • getAlgorithmState

      public Optional<Navx.AlgorithmState> getAlgorithmState()
      Gets the sensor-fusion filter state, for diagnosing calibration.
      Returns:
      the filter state, or empty if not streaming
    • getTemperature

      public Optional<org.wpilib.units.measure.Temperature> getTemperature()
      Gets the gyroscope chip temperature.
      Returns:
      the temperature, or empty if not streaming
    • getTimestamp

      public Optional<org.wpilib.units.measure.Time> getTimestamp()
      Gets when the newest sample was taken, on the same clock as Timer.getTimestamp(), e.g. for a pose estimator. Over CAN FD and USB this is when the NavX3 measured it, once its clock is synced (see isTimeSynced()); otherwise it is when the sample arrived.
      Returns:
      the timestamp, or empty if not streaming
    • isTimeSynced

      public boolean isTimeSynced()
      Returns whether getTimestamp() is the measurement time. The NavX3's clock syncs within about a second of connecting over CAN FD or USB; classic CAN only has arrival times.
      Returns:
      true if the newest sample's timestamp is when it was measured
    • resetYaw

      public Status resetYaw()
      Resets yaw to zero.
      Returns:
      the result
    • setOutputRate

      public Status setOutputRate(int hz)
      Sets the output data rate.
      Parameters:
      hz - rate, 1-200 Hz
      Returns:
      the result
    • enableMessages

      public Status enableMessages(Navx.Message... messages)
      Chooses which message groups stream. Groups left out read as empty.
      Parameters:
      messages - the groups to stream
      Returns:
      the result
    • use9AxisYaw

      public Status use9AxisYaw(boolean enable)
      Chooses whether the yaw estimate uses the magnetometer.
      Parameters:
      enable - true for 9-axis yaw (with magnetometer), false for 6-axis
      Returns:
      the result
    • getFirmwareVersion

      public Optional<FirmwareVersion> getFirmwareVersion()
      Asks the device for its firmware version. Blocks until it answers.
      Returns:
      the version, or empty if the device did not answer
    • getSensorUuid

      public Optional<int[]> getSensorUuid()
      Asks the device for its unique ID. Blocks until it answers.
      Returns:
      the 96-bit ID as three words, or empty if the device did not answer
    • selfTest

      public Optional<Navx.SelfTestResult> selfTest()
      Runs the IMU self test. Blocks for the few seconds it takes; meanwhile the NavX3 sends no data, so the getters return empty. Keep the robot still.
      Returns:
      the result codes, or empty if unsupported or the device did not answer