Class Parsec

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

public class Parsec extends Object implements AutoCloseable
Studica Parsec multi-zone time-of-flight distance sensor, over CAN or USB.

Any number of Parsec objects can be open, one per device. Frames arrive in the background; getScan() returns the newest one, or Optional.empty() when none has arrived recently.

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

    • Parsec

      public Parsec(int canId)
      Opens a Parsec on CAN bus S0.
      Parameters:
      canId - CAN device ID set on the Parsec
    • Parsec

      public Parsec(int canId, org.wpilib.hardware.bus.CANPort bus)
      Opens a Parsec on CAN.
      Parameters:
      canId - CAN device ID set on the Parsec
      bus - CAN port it is wired to
  • Method Details

    • usb

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

      public static Parsec usb(UsbPort port)
      Opens the Parsec in a specific SystemCore USB port.
      Parameters:
      port - the USB port, as labelled on the SystemCore
      Returns:
      the Parsec
    • fromSerialDevice

      public static Parsec fromSerialDevice(String devicePath)
      Opens a Parsec on an explicit serial device.
      Parameters:
      devicePath - e.g. "/dev/ttyACM0"
      Returns:
      the Parsec
    • close

      public void close()
      Closes the device. The object is inert afterwards.
      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 was closed
    • isConnected

      public boolean isConnected()
      Returns whether data arrived recently.
      Returns:
      true if connected
    • getScan

      public Optional<Parsec.Scan> getScan()
      Returns the newest frame.
      Returns:
      the frame, or empty if none arrived recently
    • getClosestDistance

      public Optional<org.wpilib.units.measure.Distance> getClosestDistance()
      Returns the nearest object in view: a shortcut for getScan() then Parsec.Scan.getClosest().
      Returns:
      the nearest distance, or empty if there's no recent frame or no valid reading
    • setOutputRate

      public Status setOutputRate(int hz)
      Sets how often frames are sent.
      Parameters:
      hz - 1-60 Hz, no faster than the ranging frequency
      Returns:
      the result
    • setResolution

      public Status setResolution(Parsec.Resolution resolution)
      Selects the zone grid. The sensor restarts at 15 Hz, so set the ranging frequency and output rate afterwards.
      Parameters:
      resolution - the grid
      Returns:
      the result
    • setRangingFrequency

      public Status setRangingFrequency(int hz)
      Sets how often the sensor measures.
      Parameters:
      hz - 1-60 Hz with 4x4 zones, 1-15 Hz with 8x8
      Returns:
      the result
    • setZoneMask

      public Status setZoneMask(long mask)
      Chooses which zones are measured: bit i enables zone i, counting row-major as Parsec.Scan.getDistance(int) does. Other zones read empty.
      Parameters:
      mask - the zones to measure; at least one bit must be set
      Returns:
      the result
    • getConfig

      public Optional<Parsec.Config> getConfig()
      Reads the device configuration. Blocks up to 500 ms (USB) or 200 ms (CAN).
      Returns:
      the configuration, or empty if the device didn't answer