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
Changes from 2 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
31 changes: 26 additions & 5 deletions src/main/native/include/rev/Drivers/DriverDeviceThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,41 @@ class DriverDeviceThread {
return nullptr; // If no matching element found
}

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 && element.m_intervalMs > 0) {
return &element;
}
}
return nullptr; // If no matching element found
}

void removeElementsWithId(int targetId) {
m_sendQueue.erase(std::remove_if(m_sendQueue.begin(), m_sendQueue.end(), [targetId](detail::CANThreadSendQueueElement element) { return element.m_msg.GetMessageId() == targetId; }), m_sendQueue.end());
}

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