Skip to content

FRC JAVA

Eric Sims edited this page Jan 21, 2020 · 11 revisions

Library Install

  1. Download the latest release https://github.com/LetsBuildRockets/ToF-CAN/releases
  2. Extract and copy the "org" folder into the project folder next to other packages (project > src > java) Your project should then look something like this:
exampleproject
├───.gradle
│   └───...
├───.settings
├───.vscode
├───.wpilib
├───bin
│   └───...
├───build
│   └───...
├───gradle
│   └───...
├───src
│   └───main
│       ├───deploy
│       └───java
│           ├───frc
│           │   └───robot
│           |       └───...
│           └───org
│               └───letsbuildrockets
│                   └───libs
│                       └───...
└───vendordeps

Now just import the TimeOfFlightSensor class and you're good to go!

import org.letsbuildrockets.libs.TimeOfFlightSensor;

TimeOfFlightSensor Methods

Constructor

Parameters:

  • int ID - ToF Sensor Hardware ID

Returns:

  • TimeOfFlightSensor

The TimeOfFlightSensor constructor initializes a new TimeOfFlightSensor object to communicate with a ToF sensor. The ID parameter must match the hardware ID on the sticker on the front of the ToF sensor.

//Example:
package frc.robot;

import org.letsbuildrockets.libs.TimeOfFlightSensor;
import edu.wpi.first.wpilibj.TimedRobot;

public class Robot extends TimedRobot {
  private TimeOfFlightSensor tofsensor;

  @Override
  public void robotInit() {
    tofsensor = new TimeOfFlightSensor(0x621);
  }

}

inRange

Parameters:

  • void

Returns:

  • boolean - in range

This method reports if the ToF sensor is detecting something in range of the sensor. It should ALWAYS be used to handle and out of range error before calling getDistance().

//Example:
if(tofsensor.inRange()) {
  System.out.println("distance: " + tofsensor.getDistance());
} else {
  System.out.println("out of range");
}

getDistance

Parameters:

  • void

Returns:

  • int - distance in mm

The getDistance method returns the distance detected by the ToF sensor in millimeters.

//Example:
if(tofsensor.inRange()) {
  int distance_mm = tofsensor.getDistance();
  float distance_ft = distance_mm / 304.8;
} else {
  System.out.println("out of range");
}

getError

Parameters:

  • void

Returns:

  • int - Error code

This method returns the current error code from the Tof Sensor

// Error codes
private static final byte ERROR_NONE = 0;
private static final byte ERROR_OUT_OF_RANGE = 1;
private static final byte ERROR_WRITING_TO_CAN = 2;
private static final byte ERROR_INIT_CAN = 3;
private static final byte ERROR_INIT_VL53L0X = 4;
private static final byte ERROR_BAD_CTRL_BYTE = 5;
private static final byte ERROR_NOT_ENOUGH_DATA_BYTES = 6;

// Example:
if(tofsensor.getError() == ERROR_WRITING_TO_CAN) {
  System.out.println("unable to write to CAN bus!");
}

getID

Parameters:

  • void

Returns:

  • int - sensor hardware ID

getFirwareVersion

Parameters:

  • void

Returns:

  • VersionNumber - VersionNumber object

The getFirwareVersion method returns the current firmware version on the ToF Sensor. The return object has two fields, .major and .minor, which store the major and minor firmware version. There is also a .toString() method that will convert the firmware version to a readable version number in the format: x.y

//Example:
System.out.println("ToF Sensor has firmware version: " + tofsensor.getFirwareVersion().toString());

setHardwareCANAddress

Parameters:

  • int newID

Returns:

  • void

This method programs a new hardware address onto the ToF sensor. Once you send this command, you will be unable to communicate with the sensor until you reinitialize with the correct hardware ID. This command is typically used just once, if you need to, for some reason, change the default hardware ID that the ToF sensor comes with. Note, the hardware ID will no longer match the hardware ID on the sticker, so be sure to relabel your sensor. The newID must be greater than, or equal to 0x0620 and less than or equal to 0x0FFF.

//Example:
setHarwareCANAddress(0x0623);