Skip to content

Commit 5be9b8b

Browse files
Add PacketSerde interface and expand PacketUtils for more wpimath classes (#1058)
Follows a similar system to the current Protobuf implementation that helps make code more readable and expandable. Also wraps the NT topic to be more useful. Impl stuff is hidden so it can't be extended. Optimizes AT-specific classes by only serializing data when needed, won't save on size but will on time. closes #1003
1 parent 0356eee commit 5be9b8b

File tree

17 files changed

+794
-435
lines changed

17 files changed

+794
-435
lines changed

photon-core/src/main/java/org/photonvision/common/dataflow/networktables/NTDataPublisher.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.function.Consumer;
2424
import java.util.function.Supplier;
2525
import org.photonvision.common.dataflow.CVPipelineResultConsumer;
26-
import org.photonvision.common.dataflow.structures.Packet;
2726
import org.photonvision.common.logging.LogGroup;
2827
import org.photonvision.common.logging.Logger;
2928
import org.photonvision.common.networktables.NTTopicSet;
@@ -134,10 +133,8 @@ public void accept(CVPipelineResult result) {
134133
result.getLatencyMillis(),
135134
TrackedTarget.simpleFromTrackedTargets(result.targets),
136135
result.multiTagResult);
137-
Packet packet = new Packet(simplified.getPacketSize());
138-
simplified.populatePacket(packet);
139136

140-
ts.rawBytesEntry.set(packet.getData());
137+
ts.resultPublisher.accept(simplified, simplified.getPacketSize());
141138

142139
ts.pipelineIndexPublisher.set(pipelineIndexSupplier.get());
143140
ts.driverModePublisher.set(driverModeSupplier.getAsBoolean());

photon-lib/src/main/java/org/photonvision/PhotonCamera.java

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,13 @@
4242
import edu.wpi.first.networktables.NetworkTable;
4343
import edu.wpi.first.networktables.NetworkTableInstance;
4444
import edu.wpi.first.networktables.PubSubOption;
45-
import edu.wpi.first.networktables.RawSubscriber;
4645
import edu.wpi.first.networktables.StringSubscriber;
4746
import edu.wpi.first.wpilibj.DriverStation;
4847
import edu.wpi.first.wpilibj.Timer;
4948
import java.util.Optional;
5049
import java.util.Set;
51-
import org.photonvision.common.dataflow.structures.Packet;
5250
import org.photonvision.common.hardware.VisionLEDMode;
51+
import org.photonvision.common.networktables.PacketSubscriber;
5352
import org.photonvision.targeting.PhotonPipelineResult;
5453

5554
/** Represents a camera that is connected to PhotonVision. */
@@ -58,7 +57,7 @@ public class PhotonCamera implements AutoCloseable {
5857
public static final String kTableName = "photonvision";
5958

6059
private final NetworkTable cameraTable;
61-
RawSubscriber rawBytesEntry;
60+
PacketSubscriber<PhotonPipelineResult> resultSubscriber;
6261
BooleanPublisher driverModePublisher;
6362
BooleanSubscriber driverModeSubscriber;
6463
DoublePublisher latencyMillisEntry;
@@ -78,7 +77,7 @@ public class PhotonCamera implements AutoCloseable {
7877

7978
@Override
8079
public void close() {
81-
rawBytesEntry.close();
80+
resultSubscriber.close();
8281
driverModePublisher.close();
8382
driverModeSubscriber.close();
8483
latencyMillisEntry.close();
@@ -115,8 +114,6 @@ public static void setVersionCheckEnabled(boolean enabled) {
115114
VERSION_CHECK_ENABLED = enabled;
116115
}
117116

118-
Packet packet = new Packet(1);
119-
120117
/**
121118
* Constructs a PhotonCamera from a root table.
122119
*
@@ -130,11 +127,14 @@ public PhotonCamera(NetworkTableInstance instance, String cameraName) {
130127
var photonvision_root_table = instance.getTable(kTableName);
131128
this.cameraTable = photonvision_root_table.getSubTable(cameraName);
132129
path = cameraTable.getPath();
133-
rawBytesEntry =
130+
var rawBytesEntry =
134131
cameraTable
135132
.getRawTopic("rawBytes")
136133
.subscribe(
137134
"rawBytes", new byte[] {}, PubSubOption.periodic(0.01), PubSubOption.sendAll(true));
135+
resultSubscriber =
136+
new PacketSubscriber<>(
137+
rawBytesEntry, PhotonPipelineResult.serde, new PhotonPipelineResult());
138138
driverModePublisher = cameraTable.getBooleanTopic("driverModeRequest").publish();
139139
driverModeSubscriber = cameraTable.getBooleanTopic("driverMode").subscribe(false);
140140
inputSaveImgEntry = cameraTable.getIntegerTopic("inputSaveImgCmd").getEntry(0);
@@ -177,21 +177,12 @@ public PhotonCamera(String cameraName) {
177177
public PhotonPipelineResult getLatestResult() {
178178
verifyVersion();
179179

180-
// Clear the packet.
181-
packet.clear();
182-
183-
// Create latest result.
184-
var ret = new PhotonPipelineResult();
185-
186-
// Populate packet and create result.
187-
packet.setData(rawBytesEntry.get(new byte[] {}));
188-
189-
if (packet.getSize() < 1) return ret;
190-
ret.createFromPacket(packet);
180+
var ret = resultSubscriber.get();
191181

192182
// Set the timestamp of the result.
193183
// getLatestChange returns in microseconds, so we divide by 1e6 to convert to seconds.
194-
ret.setTimestampSeconds((rawBytesEntry.getLastChange() / 1e6) - ret.getLatencyMillis() / 1e3);
184+
ret.setTimestampSeconds(
185+
(resultSubscriber.subscriber.getLastChange() / 1e6) - ret.getLatencyMillis() / 1e3);
195186

196187
// Return result.
197188
return ret;

photon-lib/src/main/java/org/photonvision/simulation/PhotonCameraSim.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
import org.opencv.imgproc.Imgproc;
4848
import org.photonvision.PhotonCamera;
4949
import org.photonvision.PhotonTargetSortMode;
50-
import org.photonvision.common.dataflow.structures.Packet;
5150
import org.photonvision.common.networktables.NTTopicSet;
5251
import org.photonvision.estimation.CameraTargetRelation;
5352
import org.photonvision.estimation.OpenCVHelp;
@@ -564,9 +563,7 @@ public void submitProcessedFrame(PhotonPipelineResult result) {
564563
public void submitProcessedFrame(PhotonPipelineResult result, long receiveTimestamp) {
565564
ts.latencyMillisEntry.set(result.getLatencyMillis(), receiveTimestamp);
566565

567-
var newPacket = new Packet(result.getPacketSize());
568-
result.populatePacket(newPacket);
569-
ts.rawBytesEntry.set(newPacket.getData(), receiveTimestamp);
566+
ts.resultPublisher.accept(result, result.getPacketSize());
570567

571568
boolean hasTargets = result.hasTargets();
572569
ts.hasTargetEntry.set(hasTargets, receiveTimestamp);

photon-lib/src/main/java/org/photonvision/simulation/SimPhotonCamera.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import java.util.List;
3434
import org.photonvision.PhotonCamera;
3535
import org.photonvision.PhotonTargetSortMode;
36-
import org.photonvision.common.dataflow.structures.Packet;
3736
import org.photonvision.common.networktables.NTTopicSet;
3837
import org.photonvision.targeting.MultiTargetPNPResult;
3938
import org.photonvision.targeting.PhotonPipelineResult;
@@ -143,9 +142,8 @@ public void submitProcessedFrame(
143142

144143
PhotonPipelineResult newResult =
145144
new PhotonPipelineResult(latencyMillis, targetList, new MultiTargetPNPResult());
146-
var newPacket = new Packet(newResult.getPacketSize());
147-
newResult.populatePacket(newPacket);
148-
ts.rawBytesEntry.set(newPacket.getData());
145+
146+
ts.resultPublisher.accept(newResult, newResult.getPacketSize());
149147

150148
boolean hasTargets = newResult.hasTargets();
151149
ts.hasTargetEntry.set(hasTargets);

photon-lib/src/test/java/org/photonvision/PacketTest.java

Lines changed: 0 additions & 190 deletions
This file was deleted.

photon-lib/src/test/java/org/photonvision/PhotonCameraTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void testEmpty() {
4040
if (packet.getSize() < 1) {
4141
return;
4242
}
43-
ret.createFromPacket(packet);
43+
PhotonPipelineResult.serde.pack(packet, ret);
4444
});
4545
}
4646
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (C) Photon Vision.
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package org.photonvision.common.dataflow.structures;
19+
20+
public interface PacketSerde<T> {
21+
int getMaxByteSize();
22+
23+
void pack(Packet packet, T value);
24+
25+
T unpack(Packet packet);
26+
}

0 commit comments

Comments
 (0)