2021-01-16 20:41:47 -08:00
|
|
|
/*
|
2022-01-20 19:35:28 -08:00
|
|
|
* MIT License
|
2021-01-16 20:41:47 -08:00
|
|
|
*
|
2022-01-20 19:35:28 -08:00
|
|
|
* Copyright (c) 2022 PhotonVision
|
2021-01-16 20:41:47 -08:00
|
|
|
*
|
2022-01-20 19:35:28 -08:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2021-01-16 20:41:47 -08:00
|
|
|
*
|
2022-01-20 19:35:28 -08:00
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
2021-01-16 20:41:47 -08:00
|
|
|
*/
|
2022-01-20 19:35:28 -08:00
|
|
|
|
2021-01-16 20:41:47 -08:00
|
|
|
package org.photonvision;
|
|
|
|
|
|
|
|
|
|
import edu.wpi.first.networktables.NetworkTable;
|
|
|
|
|
import edu.wpi.first.networktables.NetworkTableEntry;
|
|
|
|
|
import edu.wpi.first.networktables.NetworkTableInstance;
|
2021-10-18 22:31:18 -04:00
|
|
|
import edu.wpi.first.wpilibj.DriverStation;
|
2021-01-16 20:41:47 -08:00
|
|
|
import org.photonvision.common.dataflow.structures.Packet;
|
|
|
|
|
import org.photonvision.common.hardware.VisionLEDMode;
|
|
|
|
|
import org.photonvision.targeting.PhotonPipelineResult;
|
|
|
|
|
|
|
|
|
|
/** Represents a camera that is connected to PhotonVision. */
|
|
|
|
|
public class PhotonCamera {
|
|
|
|
|
final NetworkTableEntry rawBytesEntry;
|
|
|
|
|
final NetworkTableEntry driverModeEntry;
|
|
|
|
|
final NetworkTableEntry inputSaveImgEntry;
|
|
|
|
|
final NetworkTableEntry outputSaveImgEntry;
|
|
|
|
|
final NetworkTableEntry pipelineIndexEntry;
|
|
|
|
|
final NetworkTableEntry ledModeEntry;
|
2021-10-18 22:31:18 -04:00
|
|
|
final NetworkTableEntry versionEntry;
|
2021-01-16 20:41:47 -08:00
|
|
|
|
|
|
|
|
final NetworkTable mainTable = NetworkTableInstance.getDefault().getTable("photonvision");
|
2021-10-18 22:31:18 -04:00
|
|
|
private final String path;
|
2021-01-16 20:41:47 -08:00
|
|
|
|
|
|
|
|
Packet packet = new Packet(1);
|
|
|
|
|
|
|
|
|
|
/**
|
2022-01-10 11:56:45 -08:00
|
|
|
* Constructs a PhotonCamera from a root table.
|
|
|
|
|
*
|
|
|
|
|
* @param rootTable The root table that the camera is broadcasting information over.
|
|
|
|
|
*/
|
2021-01-16 20:41:47 -08:00
|
|
|
public PhotonCamera(NetworkTable rootTable) {
|
2021-10-18 22:31:18 -04:00
|
|
|
path = rootTable.getPath();
|
2021-01-16 20:41:47 -08:00
|
|
|
rawBytesEntry = rootTable.getEntry("rawBytes");
|
|
|
|
|
driverModeEntry = rootTable.getEntry("driverMode");
|
|
|
|
|
inputSaveImgEntry = rootTable.getEntry("inputSaveImgCmd");
|
|
|
|
|
outputSaveImgEntry = rootTable.getEntry("outputSaveImgCmd");
|
|
|
|
|
pipelineIndexEntry = rootTable.getEntry("pipelineIndex");
|
|
|
|
|
ledModeEntry = mainTable.getEntry("ledMode");
|
2021-10-18 22:31:18 -04:00
|
|
|
versionEntry = mainTable.getEntry("version");
|
2021-01-16 20:41:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-01-10 11:56:45 -08:00
|
|
|
* Constructs a PhotonCamera from the name of the camera.
|
|
|
|
|
*
|
|
|
|
|
* @param cameraName The nickname of the camera (found in the PhotonVision UI).
|
|
|
|
|
*/
|
2021-01-16 20:41:47 -08:00
|
|
|
public PhotonCamera(String cameraName) {
|
|
|
|
|
this(NetworkTableInstance.getDefault().getTable("photonvision").getSubTable(cameraName));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-01-10 11:56:45 -08:00
|
|
|
* Returns the latest pipeline result.
|
|
|
|
|
*
|
|
|
|
|
* @return The latest pipeline result.
|
|
|
|
|
*/
|
2021-01-16 20:41:47 -08:00
|
|
|
public PhotonPipelineResult getLatestResult() {
|
2021-10-18 22:31:18 -04:00
|
|
|
verifyVersion();
|
|
|
|
|
|
2021-01-16 20:41:47 -08:00
|
|
|
// Clear the packet.
|
|
|
|
|
packet.clear();
|
|
|
|
|
|
|
|
|
|
// Create latest result.
|
|
|
|
|
var ret = new PhotonPipelineResult();
|
|
|
|
|
|
|
|
|
|
// Populate packet and create result.
|
|
|
|
|
packet.setData(rawBytesEntry.getRaw(new byte[] {}));
|
|
|
|
|
if (packet.getSize() < 1) return ret;
|
|
|
|
|
ret.createFromPacket(packet);
|
|
|
|
|
|
|
|
|
|
// Return result.
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-01-10 11:56:45 -08:00
|
|
|
* Returns whether the camera is in driver mode.
|
|
|
|
|
*
|
|
|
|
|
* @return Whether the camera is in driver mode.
|
|
|
|
|
*/
|
2021-01-16 20:41:47 -08:00
|
|
|
public boolean getDriverMode() {
|
2021-12-15 11:16:09 -06:00
|
|
|
return driverModeEntry.getBoolean(false);
|
2021-01-16 20:41:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-01-10 11:56:45 -08:00
|
|
|
* Toggles driver mode.
|
|
|
|
|
*
|
|
|
|
|
* @param driverMode Whether to set driver mode.
|
|
|
|
|
*/
|
2021-01-16 20:41:47 -08:00
|
|
|
public void setDriverMode(boolean driverMode) {
|
2021-12-15 11:16:09 -06:00
|
|
|
driverModeEntry.setBoolean(driverMode);
|
2021-01-16 20:41:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-01-10 11:56:45 -08:00
|
|
|
* Request the camera to save a new image file from the input camera stream with overlays. Images
|
|
|
|
|
* take up space in the filesystem of the PhotonCamera. Calling it frequently will fill up disk
|
|
|
|
|
* space and eventually cause the system to stop working. Clear out images in
|
|
|
|
|
* /opt/photonvision/photonvision_config/imgSaves frequently to prevent issues.
|
|
|
|
|
*/
|
2021-01-16 20:41:47 -08:00
|
|
|
public void takeInputSnapshot() {
|
|
|
|
|
inputSaveImgEntry.setBoolean(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-01-10 11:56:45 -08:00
|
|
|
* Request the camera to save a new image file from the output stream with overlays. Images take
|
|
|
|
|
* up space in the filesystem of the PhotonCamera. Calling it frequently will fill up disk space
|
|
|
|
|
* and eventually cause the system to stop working. Clear out images in
|
|
|
|
|
* /opt/photonvision/photonvision_config/imgSaves frequently to prevent issues.
|
|
|
|
|
*/
|
2021-01-16 20:41:47 -08:00
|
|
|
public void takeOutputSnapshot() {
|
|
|
|
|
outputSaveImgEntry.setBoolean(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-01-10 11:56:45 -08:00
|
|
|
* Returns the active pipeline index.
|
|
|
|
|
*
|
|
|
|
|
* @return The active pipeline index.
|
|
|
|
|
*/
|
2021-01-16 20:41:47 -08:00
|
|
|
public int getPipelineIndex() {
|
2021-12-15 11:16:09 -06:00
|
|
|
return pipelineIndexEntry.getNumber(0).intValue();
|
2021-01-16 20:41:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-01-10 11:56:45 -08:00
|
|
|
* Allows the user to select the active pipeline index.
|
|
|
|
|
*
|
|
|
|
|
* @param index The active pipeline index.
|
|
|
|
|
*/
|
2021-01-16 20:41:47 -08:00
|
|
|
public void setPipelineIndex(int index) {
|
2021-12-15 11:16:09 -06:00
|
|
|
pipelineIndexEntry.setNumber(index);
|
2021-01-16 20:41:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-01-10 11:56:45 -08:00
|
|
|
* Returns the current LED mode.
|
|
|
|
|
*
|
|
|
|
|
* @return The current LED mode.
|
|
|
|
|
*/
|
2021-01-16 20:41:47 -08:00
|
|
|
public VisionLEDMode getLEDMode() {
|
|
|
|
|
int value = ledModeEntry.getNumber(-1).intValue();
|
|
|
|
|
switch (value) {
|
|
|
|
|
case 0:
|
2021-12-15 11:16:09 -06:00
|
|
|
return VisionLEDMode.kOff;
|
2021-01-16 20:41:47 -08:00
|
|
|
case 1:
|
2021-12-15 11:16:09 -06:00
|
|
|
return VisionLEDMode.kOn;
|
2021-01-16 20:41:47 -08:00
|
|
|
case 2:
|
2021-12-15 11:16:09 -06:00
|
|
|
return VisionLEDMode.kBlink;
|
2021-01-16 20:41:47 -08:00
|
|
|
case -1:
|
|
|
|
|
default:
|
2021-12-15 11:16:09 -06:00
|
|
|
return VisionLEDMode.kDefault;
|
2021-01-16 20:41:47 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-01-10 11:56:45 -08:00
|
|
|
* Sets the LED mode.
|
|
|
|
|
*
|
|
|
|
|
* @param led The mode to set to.
|
|
|
|
|
*/
|
2021-01-16 20:41:47 -08:00
|
|
|
public void setLED(VisionLEDMode led) {
|
2021-12-15 11:16:09 -06:00
|
|
|
ledModeEntry.setNumber(led.value);
|
2021-01-16 20:41:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-01-10 11:56:45 -08:00
|
|
|
* Returns whether the latest target result has targets.
|
|
|
|
|
*
|
|
|
|
|
* <p>This method is deprecated; {@link PhotonPipelineResult#hasTargets()} should be used instead.
|
|
|
|
|
*
|
|
|
|
|
* @deprecated This method should be replaced with {@link PhotonPipelineResult#hasTargets()}
|
|
|
|
|
* @return Whether the latest target result has targets.
|
|
|
|
|
*/
|
2021-09-03 19:19:38 -07:00
|
|
|
@Deprecated
|
2021-01-16 20:41:47 -08:00
|
|
|
public boolean hasTargets() {
|
|
|
|
|
return getLatestResult().hasTargets();
|
|
|
|
|
}
|
2021-10-18 22:31:18 -04:00
|
|
|
|
|
|
|
|
private void verifyVersion() {
|
|
|
|
|
String versionString = versionEntry.getString("");
|
|
|
|
|
if (versionString.equals("")) {
|
|
|
|
|
DriverStation.reportError(
|
|
|
|
|
"PhotonVision coprocessor at path " + path + " not found on NetworkTables!", true);
|
|
|
|
|
} else if (!PhotonVersion.versionMatches(versionString)) {
|
|
|
|
|
DriverStation.reportError(
|
|
|
|
|
"Photon version "
|
|
|
|
|
+ PhotonVersion.versionString
|
|
|
|
|
+ " does not match coprocessor version "
|
|
|
|
|
+ versionString
|
|
|
|
|
+ "!",
|
|
|
|
|
true);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-16 20:41:47 -08:00
|
|
|
}
|