Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple messages to be scheduled with the same ID if the interval is set to 0 #30

Merged
merged 5 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ new call with different data is sent before the previous
interval is up. Sending a frame with an interval of -1
will cancel the repeat, and not send the frame. Sending with
an interval of 0 will schedule the new frame once, then stop
repeating.
repeating. Unlike with a positive interval, an interval of 0
guarantees that each new message will be sent.

## Build Requirements

Expand Down
26 changes: 19 additions & 7 deletions src/main/native/include/rev/Drivers/DriverDeviceThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ class DriverDeviceThread {
}
}

detail::CANThreadSendQueueElement* findFirstMatchingId(int targetId) {
detail::CANThreadSendQueueElement* findFirstMatchingIdWithNonZeroInterval(int targetId) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like findFirstMatchingId() can be deleted

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleted

for (auto& element : m_sendQueue) {
if (element.m_msg.GetMessageId() == targetId) {
if (element.m_msg.GetMessageId() == targetId && element.m_intervalMs > 0) {
return &element;
}
}
Expand All @@ -91,13 +91,25 @@ class DriverDeviceThread {
bool EnqueueMessage(const CANMessage& msg, int32_t timeIntervalMs) {
std::lock_guard<std::mutex> lock(m_writeMutex);

detail::CANThreadSendQueueElement* existing = findFirstMatchingId(msg.GetMessageId());
if(timeIntervalMs == 0) {
// If the time interval is 0, we want to allow duplicates.
m_sendQueue.push_back(detail::CANThreadSendQueueElement(msg, timeIntervalMs));

if(existing) {
existing->m_intervalMs = timeIntervalMs;
existing->m_msg = msg;
// Cancel existing repeating frame with same id
detail::CANThreadSendQueueElement* existing = findFirstMatchingIdWithNonZeroInterval(msg.GetMessageId());
if(existing) {
existing->m_intervalMs = -1;
}
} else {
m_sendQueue.push_back(detail::CANThreadSendQueueElement(msg, timeIntervalMs));
// We don't want to replace elements with zero as the interval. Those should be guaranteed to be sent
detail::CANThreadSendQueueElement* existing = findFirstMatchingIdWithNonZeroInterval(msg.GetMessageId());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After this change, calling with (data1, 0), then immediately calling with (data2, 5) will ensure that data1 is always sent once, then data2 is sent every 5 milliseconds.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, more practically, calling with (data_i, 0) repeatedly will ensure all the data actually gets sent.


if(existing) {
existing->m_intervalMs = timeIntervalMs;
existing->m_msg = msg;
} else {
m_sendQueue.push_back(detail::CANThreadSendQueueElement(msg, timeIntervalMs));
}
}

// TODO: Limit the max queue size
Expand Down
Loading