Skip to content

Commit

Permalink
bfw but missing python
Browse files Browse the repository at this point in the history
  • Loading branch information
mcm001 committed Aug 4, 2024
1 parent d93c6fc commit eb40c0b
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 32 deletions.
21 changes: 19 additions & 2 deletions photon-lib/src/main/java/org/photonvision/PhotonCamera.java
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,20 @@ else if (!isConnected()) {
"PhotonVision coprocessor at path " + path + " is not sending new data.", true);
}

// Check for version. Warn if the versions aren't aligned.
String versionString = versionEntry.get("");
if (!versionString.isEmpty() && !PhotonVersion.versionMatches(versionString)) {

// Check mdef UUID
String local_uuid = PhotonPipelineResult.photonStruct.getInterfaceUUID();
String remote_uuid = resultSubscriber.getInterfaceUUID();

if (remote_uuid == null || remote_uuid.isEmpty()) {
// not connected yet?
DriverStation.reportWarning(
"PhotonVision coprocessor at path "
+ path
+ " has note reported a message interface UUID - is your coprocessor's camera started?",
true);
} else if (!local_uuid.equals(remote_uuid)) {
// Error on a verified version mismatch
// But stay silent otherwise

Expand All @@ -406,8 +417,14 @@ else if (!isConnected()) {
var versionMismatchMessage =
"Photon version "
+ PhotonVersion.versionString
+ " (message definition version "
+ local_uuid
+ ")"
+ " does not match coprocessor version "
+ versionString
+ " (message definition version "
+ remote_uuid
+ ")"
+ "!";
DriverStation.reportError(versionMismatchMessage, false);
throw new UnsupportedOperationException(versionMismatchMessage);
Expand Down
38 changes: 15 additions & 23 deletions photon-lib/src/main/native/cpp/photon/PhotonCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <frc/Timer.h>
#include <opencv2/core.hpp>
#include <opencv2/core/mat.hpp>
#include <wpi/json.h>

#include "PhotonVersion.h"
#include "photon/dataflow/structures/Packet.h"
Expand Down Expand Up @@ -192,22 +193,6 @@ std::optional<PhotonCamera::DistortionMatrix> PhotonCamera::GetDistCoeffs() {
return std::nullopt;
}

static bool VersionMatches(std::string them_str) {
std::smatch match;
std::regex versionPattern{"v[0-9]+.[0-9]+.[0-9]+"};

std::string us_str = PhotonVersion::versionString;

// Check that both versions are in the right format
if (std::regex_search(us_str, match, versionPattern) &&
std::regex_search(them_str, match, versionPattern)) {
// If they are, check string equality
return (us_str == them_str);
} else {
return false;
}
}

void PhotonCamera::VerifyVersion() {
if (!PhotonCamera::VERSION_CHECK_ENABLED) {
return;
Expand Down Expand Up @@ -244,13 +229,20 @@ void PhotonCamera::VerifyVersion() {
"Found the following PhotonVision cameras on NetworkTables:{}",
cameraNameOutString);
}
} else if (!VersionMatches(versionString)) {
FRC_ReportError(frc::warn::Warning, bfw);
std::string error_str = fmt::format(
"Photonlib version {} does not match coprocessor version {}!",
PhotonVersion::versionString, versionString);
FRC_ReportError(frc::err::Error, "{}", error_str);
throw std::runtime_error(error_str);
} else {
std::string local_uuid{SerdeType<PhotonPipelineResult>::GetSchemaHash()};
std::string remote_uuid =
rawBytesEntry.GetTopic().GetProperty("message_uuid");

if (local_uuid != remote_uuid) {
FRC_ReportError(frc::warn::Warning, bfw);
std::string error_str = fmt::format(
"Photonlib version {} (message definition version {}) does not match "
"coprocessor version {} (message definition version {})!",
PhotonVersion::versionString, local_uuid, versionString, remote_uuid);
FRC_ReportError(frc::err::Error, "{}", error_str);
throw std::runtime_error(error_str);
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions photon-targeting/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ apply from: "${rootDir}/shared/javacpp/publish.gradle"

// Add photon serde headers to our published sources
cppHeadersZip {
from('src/generated/main/native/include') {
from('src/generated/main/native/include') {
into '/'
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,9 @@ public T get() {
public void close() {
subscriber.close();
}

// TODO - i can see an argument for moving this logic all here instead of keeping in photoncamera
public String getInterfaceUUID() {
return subscriber.getTopic().getProperty("message_uuid");
}
}
7 changes: 5 additions & 2 deletions photonlib-cpp-examples/aimattarget/src/main/cpp/Robot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
#include "Robot.h"

#include <photon/PhotonUtils.h>
#include <units/time.h>

#include <frc/Timer.h>
#include <frc/smartdashboard/SmartDashboard.h>
#include <units/time.h>

void Robot::RobotPeriodic() {
photon::PhotonCamera::SetVersionCheckEnabled(false);
Expand All @@ -36,7 +37,9 @@ void Robot::RobotPeriodic() {
photon::PhotonPipelineResult result = camera.GetLatestResult();
auto end = frc::Timer::GetFPGATimestamp();

printf("DT is %iuS for %i targets\n",(int)units::microsecond_t(end - start).to<double>(), result.GetTargets().size());
std::printf("DT is %iuS for %i targets\n",
(int)units::microsecond_t(end - start).to<double>(),
result.GetTargets().size());
}

void Robot::TeleopPeriodic() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import edu.wpi.first.wpilibj.drive.DifferentialDrive;
import edu.wpi.first.wpilibj.motorcontrol.PWMVictorSPX;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;

import org.photonvision.PhotonCamera;

/**
Expand Down Expand Up @@ -75,8 +74,9 @@ public void robotPeriodic() {
var start = Timer.getFPGATimestamp();
var res = camera.getLatestResult();
var end = Timer.getFPGATimestamp();
System.out.println("dt: " + (int)((end - start)*1e6) + "uS for targets: " + res.getTargets().size());
SmartDashboard.putNumber("decodeTime", (int)((end-start)*1e6));
System.out.println(
"dt: " + (int) ((end - start) * 1e6) + "uS for targets: " + res.getTargets().size());
SmartDashboard.putNumber("decodeTime", (int) ((end - start) * 1e6));
}

@Override
Expand Down

0 comments on commit eb40c0b

Please sign in to comment.