Skip to content

Commit

Permalink
Add FMS info to snapshot names (PhotonVision#1460)
Browse files Browse the repository at this point in the history
Supersedes PhotonVision#464

Co-authored-by: Ofir Siboni <[email protected]>
  • Loading branch information
mcm001 and OfirSiboni authored Oct 13, 2024
1 parent 09b1bb9 commit 353a8ea
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ public void accept(NetworkTableEvent event) {
}
}

public NetworkTableInstance getNTInst() {
return ntInstance;
}

private void onFieldLayoutChanged(NetworkTableEvent event) {
var atfl_json = event.valueData.value.getString();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@

package org.photonvision.vision.frame.consumer;

import edu.wpi.first.math.MathUtil;
import edu.wpi.first.networktables.IntegerEntry;
import edu.wpi.first.networktables.IntegerSubscriber;
import edu.wpi.first.networktables.NetworkTable;
import edu.wpi.first.networktables.StringSubscriber;
import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
Expand All @@ -35,6 +38,9 @@
public class FileSaveFrameConsumer implements Consumer<CVMat> {
private final Logger logger = new Logger(FileSaveFrameConsumer.class, LogGroup.General);

// match type's values from the FMS.
private static final String[] matchTypes = {"N/A", "P", "Q", "E", "EV"};

// Formatters to generate unique, timestamped file names
private static final String FILE_PATH = ConfigManager.getInstance().getImageSavePath().toString();
private static final String FILE_EXTENSION = ".jpg";
Expand All @@ -48,6 +54,10 @@ public class FileSaveFrameConsumer implements Consumer<CVMat> {
private final String ntEntryName;
private IntegerEntry saveFrameEntry;

private StringSubscriber ntEventName;
private IntegerSubscriber ntMatchNum;
private IntegerSubscriber ntMatchType;

private final String cameraUniqueName;
private String cameraNickname;
private final String streamType;
Expand All @@ -61,6 +71,12 @@ public FileSaveFrameConsumer(String camNickname, String cameraUniqueName, String
this.streamType = streamPrefix;

this.rootTable = NetworkTablesManager.getInstance().kRootTable;

NetworkTable fmsTable = NetworkTablesManager.getInstance().getNTInst().getTable("FMSInfo");
this.ntEventName = fmsTable.getStringTopic("EventName").subscribe("UNKNOWN");
this.ntMatchNum = fmsTable.getIntegerTopic("MatchNumber").subscribe(-1);
this.ntMatchType = fmsTable.getIntegerTopic("MatchType").subscribe(0);

updateCameraNickname(camNickname);
}

Expand All @@ -75,7 +91,15 @@ public void accept(CVMat image) {
Date now = new Date();

String fileName =
cameraNickname + "_" + streamType + "_" + df.format(now) + "T" + tf.format(now);
cameraNickname
+ "_"
+ streamType
+ "_"
+ df.format(now)
+ "T"
+ tf.format(now)
+ "_"
+ getMatchData();

// Check if the Unique Camera directory exists and create it if it doesn't
String cameraPath = FILE_PATH + File.separator + this.cameraUniqueName;
Expand Down Expand Up @@ -119,4 +143,30 @@ public void overrideTakeSnapshot() {
// Simulate NT change
saveFrameEntry.set(saveFrameEntry.get() + 1);
}

/**
* Returns the match Data collected from the NT. eg : Q58 for qualfication match 58. If not in
* event, returns N/A-0-EVENTNAME
*/
private String getMatchData() {
var matchType = ntMatchType.getAtomic();
if (matchType.timestamp == 0) {
// no NT info yet
logger.warn("Did not recieve match type, defaulting to 0");
}

var matchNum = ntMatchNum.getAtomic();
if (matchNum.timestamp == 0) {
logger.warn("Did not recieve match num, defaulting to -1");
}

var eventName = ntEventName.getAtomic();
if (eventName.timestamp == 0) {
logger.warn("Did not recieve event name, defaulting to 'UNKNOWN'");
}

String matchTypeStr =
matchTypes[MathUtil.clamp((int) matchType.value, 0, matchTypes.length - 1)];
return matchTypeStr + "-" + matchNum.value + "-" + eventName.value;
}
}

0 comments on commit 353a8ea

Please sign in to comment.