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

RC_Channel: Express option numbers in bits #19230

Conversation

muramura
Copy link
Contributor

I would change the number of arrays to the maximum value +1 to check for duplication of all channel options.

@@ -1261,7 +1261,7 @@ RC_Channel *RC_Channels::find_channel_for_option(const RC_Channel::aux_func_t op
// duplicate_options_exist - returns true if any options are duplicated
bool RC_Channels::duplicate_options_exist()
{
uint8_t auxsw_option_counts[256] = {};
uint8_t auxsw_option_counts[308] = {};
Copy link
Member

Choose a reason for hiding this comment

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

probably worth adding a 'AUX_FUNC_END` enum for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree with this idea. I will change it.

@muramura muramura force-pushed the AP_Maximize_the_number_of_duplicate_sequences_for_channel_options branch from 937696d to aa2655a Compare November 12, 2021 14:35
@muramura
Copy link
Contributor Author

@IamPete1 san. I've changed it.

Copy link
Contributor

@khancyr khancyr left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -1261,7 +1261,7 @@ RC_Channel *RC_Channels::find_channel_for_option(const RC_Channel::aux_func_t op
// duplicate_options_exist - returns true if any options are duplicated
bool RC_Channels::duplicate_options_exist()
{
uint8_t auxsw_option_counts[256] = {};
uint8_t auxsw_option_counts[uint32_t(RC_Channel::AUX_FUNC::AUX_FUNC_END)] = {};
Copy link
Contributor

@tridge tridge Nov 16, 2021

Choose a reason for hiding this comment

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

best to change this to a Bitmask object, which would reduce stack usage by 8x

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I change to check with a bit

@muramura muramura force-pushed the AP_Maximize_the_number_of_duplicate_sequences_for_channel_options branch 2 times, most recently from 28d78f2 to 91e096e Compare November 28, 2021 12:13
@muramura muramura changed the title RC_Channel: Maximize the number of duplicate sequences for channel options RC_Channel: Express option numbers in bits Nov 28, 2021
@muramura
Copy link
Contributor Author

I would change the option number to a bit representation.
I went from 308 bytes to 39 bytes by changing it to bits.
I got one loop process by changing the bit number.

Screenshot from 2021-11-28 20-56-30

@muramura muramura force-pushed the AP_Maximize_the_number_of_duplicate_sequences_for_channel_options branch from f4690c9 to f8fc592 Compare November 28, 2021 19:49
}

for (uint16_t i=0; i<sizeof(auxsw_option_counts); i++) {
if (i == 0) { // MAGIC VALUE! This is AUXSW_DO_NOTHING
Copy link
Contributor

Choose a reason for hiding this comment

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

This lost comment was useful

@@ -1269,29 +1269,24 @@ RC_Channel *RC_Channels::find_channel_for_option(const RC_Channel::aux_func_t op
// duplicate_options_exist - returns true if any options are duplicated
bool RC_Channels::duplicate_options_exist()
{
uint8_t auxsw_option_counts[256] = {};
uint8_t auxsw_option_counts[uint32_t(RC_Channel::AUX_FUNC::AUX_FUNC_END) / 8 + 1] = {};
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
uint8_t auxsw_option_counts[uint32_t(RC_Channel::AUX_FUNC::AUX_FUNC_END) / 8 + 1] = {};
uint8_t auxsw_option_counts[uint32_t((RC_Channel::AUX_FUNC::AUX_FUNC_END) +7 ) / 8] = {};

continue;
}
if (auxsw_option_counts[i] > 1) {
if ((auxsw_option_counts[option / 8] & (1 << (option % 8))) == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It's way too easy to get this wrong. Not saying you have - but I wouldn't trust myself with a quick review of this.

This is why we have the Bitmask class: https://github.com/ardupilot/ardupilot/master/libraries/AP_Common/Bitmask.h#L27

So instead of

    uint8_t auxsw_option_counts[uint32_t((RC_Channel::AUX_FUNC::AUX_FUNC_END) +7 ) / 8] = {};

you have

    Bitmask<RC_Channel::AUX_FUNC::AUX_FUNC_END> auxsw_option_used;

and then:

Suggested change
if ((auxsw_option_counts[option / 8] & (1 << (option % 8))) == 0) {
if (auxsw_option_used.get(option)) {

and then also use .set(option) as appropriate.

@peterbarker
Copy link
Contributor

I've reworked this over here: #27644

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants