Single Channels Flight Mode Slots - Adding New

#1

I am looking to add an additional flight mode slot using the single channel method and the COM_FLTMODE# parameters.

The code in rc_update.cpp line 407 to map the incoming rc channel to the flight mode slots is pretty complicated - I get that it allows for addition flight slots, but it is challenging to back out where the PWM cutoffs for each channel are since it is using casting to int to drop the decimals off a float value.

Would this be better than as if statements comparing rc values (-1,1 mapped from PWMs 1000-2000)? It might not be as flexible at adding a new flight mode but it would be much easier to add an addition case and remap the PWM cutoffs.

#2

I would propose replace the following in rc_update (line 407) which relies on casting floats to ints after mapping to get indices:

/* the number of valid slots equals the index of the max marker minus one */

const int num_slots = manual_control_setpoint_s::MODE_SLOT_MAX;

/* the half width of the range of a slot is the total range

         * divided by the number of slots, again divided by two

         */

const float slot_width_half = 2.0f / num_slots / 2.0f;

/* min is -1, max is +1, range is 2. We offset below min and max */

const float slot_min = -1.0f - 0.05f;

const float slot_max = 1.0f + 0.05f;

/* the slot gets mapped by first normalizing into a 0..1 interval using min

         * and max. Then the right slot is obtained by multiplying with the number of

         * slots. And finally we add half a slot width to ensure that integer rounding

         * will take us to the correct final index.

         */

        manual.mode_slot = (((((_rc.channels[_parameters.rc_map_flightmode - 1] - slot_min) * num_slots) + slot_width_half) /

             (slot_max - slot_min)) + (1.0f / num_slots));

With:

/* determine flight mode slot index from rc input */
float rc_channel_val = _rc.channels[_parameters.rc_map_flightmode-1];

/* each 0.1 represent 50us of PWM.  */
if(rc_channel_val >= -1.0f && rc_channel_val < -0.7f){ //PWM 1000 - 1150

// Mode Slot 1

          manual.mode_slot = 0;

        }

else if(rc_channel_val >= -0.7f && rc_channel_val < -0.4f){ //PWM 1150 - 1300

// Mode Slot 2

          manual.mode_slot = 1;

        }

else if(rc_channel_val >= -0.4f && rc_channel_val < -0.1f){ //PWM 1300 - 1450

// Mode Slot 3

          manual.mode_slot = 2;

        }

else if(rc_channel_val >= -0.1f && rc_channel_val < 0.2f){ // PWM 1450 - 1600

// Mode Slot 4

          manual.mode_slot = 3;

        }

else if(rc_channel_val >= 0.2f && rc_channel_val < 0.5f){ // PWM 1600 - 1750

// Mode Slot 5

          manual.mode_slot = 4;

        }

else if(rc_channel_val >= 0.5f && rc_channel_val < 1.00f){ // PWM 1750 - 2000

// Mode Slot 6

          manual.mode_slot = 5;

        }

else{

          manual.mode_slot = 0;

        }

Which would make it very simple to add another flight mode slot as well as change the min/max PWMs for each slot.