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;
|
|
|
|
|
|
2021-03-23 12:47:26 -04:00
|
|
|
import edu.wpi.first.networktables.NetworkTableEntry;
|
|
|
|
|
import edu.wpi.first.networktables.NetworkTableInstance;
|
2021-01-16 20:41:47 -08:00
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import org.photonvision.common.dataflow.structures.Packet;
|
|
|
|
|
import org.photonvision.targeting.PhotonPipelineResult;
|
|
|
|
|
import org.photonvision.targeting.PhotonTrackedTarget;
|
|
|
|
|
|
2021-03-23 12:47:26 -04:00
|
|
|
@SuppressWarnings("unused")
|
2021-01-16 20:41:47 -08:00
|
|
|
public class SimPhotonCamera extends PhotonCamera {
|
2021-03-23 12:47:26 -04:00
|
|
|
private final NetworkTableEntry latencyMillisEntry;
|
|
|
|
|
private final NetworkTableEntry hasTargetEntry;
|
|
|
|
|
private final NetworkTableEntry targetPitchEntry;
|
|
|
|
|
private final NetworkTableEntry targetYawEntry;
|
|
|
|
|
private final NetworkTableEntry targetAreaEntry;
|
|
|
|
|
private final NetworkTableEntry targetSkewEntry;
|
|
|
|
|
private final NetworkTableEntry targetPoseEntry;
|
2022-02-22 19:01:01 -06:00
|
|
|
private final NetworkTableEntry versionEntry;
|
2021-03-23 12:47:26 -04:00
|
|
|
|
2021-01-16 20:41:47 -08:00
|
|
|
/**
|
2022-01-10 11:56:45 -08:00
|
|
|
* Constructs a Simulated PhotonCamera from a root table.
|
|
|
|
|
*
|
2022-01-24 12:38:45 -05:00
|
|
|
* @param instance The NetworkTableInstance to pull data from. This can be a custom instance in
|
|
|
|
|
* simulation, but should *usually* be the default NTInstance from
|
|
|
|
|
* NetworkTableInstance::getDefault
|
|
|
|
|
* @param cameraName The name of the camera, as seen in the UI.
|
2022-01-10 11:56:45 -08:00
|
|
|
*/
|
2022-01-24 12:38:45 -05:00
|
|
|
public SimPhotonCamera(NetworkTableInstance instance, String cameraName) {
|
|
|
|
|
super(instance, cameraName);
|
2021-03-23 12:47:26 -04:00
|
|
|
|
|
|
|
|
latencyMillisEntry = rootTable.getEntry("latencyMillis");
|
|
|
|
|
hasTargetEntry = rootTable.getEntry("hasTargetEntry");
|
|
|
|
|
targetPitchEntry = rootTable.getEntry("targetPitchEntry");
|
|
|
|
|
targetYawEntry = rootTable.getEntry("targetYawEntry");
|
|
|
|
|
targetAreaEntry = rootTable.getEntry("targetAreaEntry");
|
|
|
|
|
targetSkewEntry = rootTable.getEntry("targetSkewEntry");
|
|
|
|
|
targetPoseEntry = rootTable.getEntry("targetPoseEntry");
|
2022-02-22 19:01:01 -06:00
|
|
|
versionEntry = rootTable.getEntry("versionEntry");
|
|
|
|
|
|
|
|
|
|
// Sets the version string so that it will always match the current version
|
|
|
|
|
versionEntry.setString(PhotonVersion.versionString);
|
2021-01-16 20:41:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-01-10 11:56:45 -08:00
|
|
|
* Constructs a Simulated 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 SimPhotonCamera(String cameraName) {
|
2022-01-24 12:38:45 -05:00
|
|
|
this(NetworkTableInstance.getDefault(), cameraName);
|
2021-01-16 20:41:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-01-10 11:56:45 -08:00
|
|
|
* Simulate one processed frame of vision data, putting one result to NT.
|
|
|
|
|
*
|
|
|
|
|
* @param latencyMillis Latency of the provided frame
|
|
|
|
|
* @param targets Each target detected
|
|
|
|
|
*/
|
2021-01-16 20:41:47 -08:00
|
|
|
public void submitProcessedFrame(double latencyMillis, PhotonTrackedTarget... targets) {
|
|
|
|
|
submitProcessedFrame(latencyMillis, Arrays.asList(targets));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-01-10 11:56:45 -08:00
|
|
|
* Simulate one processed frame of vision data, putting one result to NT.
|
|
|
|
|
*
|
|
|
|
|
* @param latencyMillis Latency of the provided frame
|
|
|
|
|
* @param sortMode Order in which to sort targets
|
|
|
|
|
* @param targets Each target detected
|
|
|
|
|
*/
|
2021-03-23 12:47:26 -04:00
|
|
|
public void submitProcessedFrame(
|
|
|
|
|
double latencyMillis, PhotonTargetSortMode sortMode, PhotonTrackedTarget... targets) {
|
|
|
|
|
submitProcessedFrame(latencyMillis, sortMode, Arrays.asList(targets));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-01-10 11:56:45 -08:00
|
|
|
* Simulate one processed frame of vision data, putting one result to NT.
|
|
|
|
|
*
|
|
|
|
|
* @param latencyMillis Latency of the provided frame
|
|
|
|
|
* @param targetList List of targets detected
|
|
|
|
|
*/
|
2021-03-23 12:47:26 -04:00
|
|
|
public void submitProcessedFrame(double latencyMillis, List<PhotonTrackedTarget> targetList) {
|
|
|
|
|
submitProcessedFrame(latencyMillis, null, targetList);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-01-10 11:56:45 -08:00
|
|
|
* Simulate one processed frame of vision data, putting one result to NT.
|
|
|
|
|
*
|
|
|
|
|
* @param latencyMillis Latency of the provided frame
|
|
|
|
|
* @param sortMode Order in which to sort targets
|
|
|
|
|
* @param targetList List of targets detected
|
|
|
|
|
*/
|
2021-03-23 12:47:26 -04:00
|
|
|
public void submitProcessedFrame(
|
|
|
|
|
double latencyMillis, PhotonTargetSortMode sortMode, List<PhotonTrackedTarget> targetList) {
|
|
|
|
|
latencyMillisEntry.setDouble(latencyMillis);
|
|
|
|
|
|
|
|
|
|
if (sortMode != null) {
|
|
|
|
|
targetList.sort(sortMode.getComparator());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PhotonPipelineResult newResult = new PhotonPipelineResult(latencyMillis, targetList);
|
|
|
|
|
var newPacket = new Packet(newResult.getPacketSize());
|
|
|
|
|
newResult.populatePacket(newPacket);
|
|
|
|
|
rawBytesEntry.setRaw(newPacket.getData());
|
|
|
|
|
|
|
|
|
|
boolean hasTargets = newResult.hasTargets();
|
|
|
|
|
hasTargetEntry.setBoolean(hasTargets);
|
|
|
|
|
if (!hasTargets) {
|
|
|
|
|
targetPitchEntry.setDouble(0.0);
|
|
|
|
|
targetYawEntry.setDouble(0.0);
|
|
|
|
|
targetAreaEntry.setDouble(0.0);
|
|
|
|
|
targetPoseEntry.setDoubleArray(new double[] {0.0, 0.0, 0.0});
|
|
|
|
|
targetSkewEntry.setDouble(0.0);
|
|
|
|
|
} else {
|
|
|
|
|
var bestTarget = newResult.getBestTarget();
|
|
|
|
|
|
|
|
|
|
targetPitchEntry.setDouble(bestTarget.getPitch());
|
|
|
|
|
targetYawEntry.setDouble(bestTarget.getYaw());
|
|
|
|
|
targetAreaEntry.setDouble(bestTarget.getArea());
|
|
|
|
|
targetSkewEntry.setDouble(bestTarget.getSkew());
|
|
|
|
|
|
|
|
|
|
var transform = bestTarget.getCameraToTarget();
|
|
|
|
|
double[] poseData = {
|
2022-09-28 18:21:41 -07:00
|
|
|
transform.getX(), transform.getY(), transform.getRotation().toRotation2d().getDegrees()
|
2021-03-23 12:47:26 -04:00
|
|
|
};
|
|
|
|
|
targetPoseEntry.setDoubleArray(poseData);
|
2021-01-16 20:41:47 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|