Skip to content

Commit

Permalink
add back old behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
mcm001 committed May 6, 2024
1 parent 79fdbe9 commit 9bc7316
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 36 deletions.
11 changes: 5 additions & 6 deletions photon-lib/src/main/java/org/photonvision/PhotonCamera.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,13 @@ public PhotonCamera(String cameraName) {
}

/**
* The list of pipeline results sent by PhotonVision since the last call to GetAllUnreadResult().
* The list of pipeline results sent by PhotonVision since the last call to getAllUnreadResults().
* Calling this function clears the internal FIFO queue, and multiple calls to
* GetAllUnreadResult() will return different (potentially empty) result arrays. Be careful to
* getAllUnreadResults() will return different (potentially empty) result arrays. Be careful to
* call this exactly ONCE per loop of your robot code! FIFO depth is limited to 20 changes, so
* make sure to call this frequently enough to avoid old results being discarded, too!
*/
List<PhotonPipelineResult> GetAllUnreadResult() {
List<PhotonPipelineResult> getAllUnreadResults() {
List<PhotonPipelineResult> ret = new ArrayList<>();

var changes = resultSubscriber.getAllChanges();
Expand All @@ -195,9 +195,8 @@ List<PhotonPipelineResult> GetAllUnreadResult() {
}

/**
* Returns the latest pipeline result.
*
* @return The latest pipeline result.
* Returns the latest pipeline result. This is simply the most recent result recieved via NT.
* Calling this multiple times will always return the most recent result.
*/
@Deprecated(since = "2024", forRemoval = true)
public PhotonPipelineResult getLatestResult() {
Expand Down
44 changes: 34 additions & 10 deletions photon-lib/src/main/native/cpp/photon/PhotonCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,34 @@ PhotonCamera::PhotonCamera(nt::NetworkTableInstance instance,
PhotonCamera::PhotonCamera(const std::string_view cameraName)
: PhotonCamera(nt::NetworkTableInstance::GetDefault(), cameraName) {}

PhotonPipelineResult PhotonCamera::GetLatestResult() {
if (test) {
if (testResult.size())
return testResult.back();
else
return PhotonPipelineResult{};
}

// Prints warning if not connected
VerifyVersion();

// Create the new result;
PhotonPipelineResult result;

// Fill the packet with latest data and populate result.
const auto value = rawBytesEntry.Get();
if (!value.size()) return result;

photon::Packet packet{value};

packet >> result;

result.SetTimestamp(units::microsecond_t(rawBytesEntry.GetLastChange()) -
result.GetLatency());

return result;
}

std::vector<PhotonPipelineResult> PhotonCamera::GetAllUnreadResults() {
if (test) {
return testResult;
Expand All @@ -110,28 +138,24 @@ std::vector<PhotonPipelineResult> PhotonCamera::GetAllUnreadResults() {
// Prints warning if not connected
VerifyVersion();

// Clear the current packet.
packet.Clear();
const auto changes = rawBytesEntry.ReadQueue();

// Create the new result;
std::vector<PhotonPipelineResult> ret;
// Create the new result list -- these will be updated in-place
std::vector<PhotonPipelineResult> ret(changes.size());

const auto changes = rawBytesEntry.ReadQueue();
ret.reserve(changes.size());
for (size_t i = 0; i < changes.size(); i++) {
const nt::Timestamped<std::vector<uint8_t>>& value = changes[i];

for (const nt::Timestamped<std::vector<uint8_t>>& value : changes) {
if (!value.value.size() || value.time == 0) {
continue;
}

// Fill the packet with latest data and populate result.
photon::Packet packet{value.value};

PhotonPipelineResult result;
PhotonPipelineResult& result = ret[i];
packet >> result;
result.SetTimestamp(units::microsecond_t(value.time) - result.GetLatency());

ret.emplace_back(std::move(result));
}

return ret;
Expand Down
11 changes: 2 additions & 9 deletions photon-lib/src/main/native/include/photon/PhotonCamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,8 @@ class PhotonCamera {
*/
std::vector<PhotonPipelineResult> GetAllUnreadResults();

[[deprecated(
"Replace with GetAllUnreadResults")]] inline const PhotonPipelineResult
GetLatestResult() {
const auto ret = GetAllUnreadResults();
if (!ret.size()) return PhotonPipelineResult();
return ret.back();
}
[[deprecated("Replace with GetAllUnreadResults")]]
PhotonPipelineResult GetLatestResult();

/**
* Toggles driver mode.
Expand Down Expand Up @@ -200,8 +195,6 @@ class PhotonCamera {
std::string path;
std::string m_cameraName;

mutable Packet packet;

private:
units::second_t lastVersionCheckTime = 0_s;
inline static bool VERSION_CHECK_ENABLED = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,17 @@ private PacketResult<T> parse(byte[] data, long timestamp) {
}

/**
* Get the latest value sent over NT. If no new value, a result with a timestamp of 0 and null
* value is provided.
* Get the latest value sent over NT. If the value has never been set, returns the provided
* default
*/
public PacketResult<T> get() {

// Get /all/ changes since last call to readQueue
var changes = subscriber.readQueue();

if (changes.length == 0) {
return new PacketResult<T>();
}

var data = changes[changes.length - 1];
var data = subscriber.getAtomic();

// No changes sent at all yet
// Topic has never been published to?
if (data.timestamp == 0) {
return new PacketResult<T>();
return new PacketResult<>();
}

return parse(data.value, data.timestamp);
Expand Down

0 comments on commit 9bc7316

Please sign in to comment.