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

AP_Scripting: Add filtering of incoming frames #25700

Closed
wants to merge 1 commit into from

Conversation

EosBandi
Copy link
Contributor

@EosBandi EosBandi commented Dec 4, 2023

Add filters to the CAN buffer; mask is bitwise ANDed with the frame ID and compared to value if not match frame is not buffered
-- By default, no filters are added, and all frames are buffered, writing is not affected by filters
-- Maximum number of filters is 8

Usage : driver:add_filter(mask, value)

Why?

  • On a shared bus non, handled frames can be filtered out, making processing relevant frames easier and requiring less buffer and slower script update.
  • Can add script driver for composite DroneCan devices with custom or experimental messages without the need for processing the frames that a non-script driver already handles. (Processing a single 1Hz custom message from a GPS/MAG device with maximum buffer size requires 5ms update time without filtering. A slower update or smaller buffer causes drop of messages)

Comment on lines +32 to +42
// Check if frame matches any filters
bool find = false;
for (int i=0;i<MAX_FILTERS;i++) {
if (filter_mask[i] == UINT32_MAX) {
break;
}
if ((frame.id & filter_mask[i]) == filter_value[i]) {
find = true;
break;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

This should be down one level in the handle_frame method before we do buffer.push(frame);. Here it will filter everything so you could not have two different sets of filters on the same bus.

uint32_t filter_mask[MAX_FILTERS];
uint32_t filter_value[MAX_FILTERS];

void add_filter(uint32_t mask, uint32_t value) {
Copy link
Member

Choose a reason for hiding this comment

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

this should return false if there are no more filters left.

Comment on lines +44 to +45
filter_mask[i] = UINT32_MAX;
filter_value[i] = UINT32_MAX;
Copy link
Member

Choose a reason for hiding this comment

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

Surely the default value would be zeros? Anything & 0 == 0, so it would pass everything?

@IamPete1
Copy link
Member

IamPete1 commented Dec 4, 2023

Might also want a way to clear filters, although I guess you could just delete the object in the script and get a new one.

@EosBandi EosBandi closed this Dec 5, 2023
@EosBandi EosBandi deleted the script_can_filtering branch December 5, 2023 00:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants