RCUpdateTest: add unit test for return switch

This commit is contained in:
Matthias Grob 2023-07-03 17:22:45 +02:00
parent 392d445783
commit 1fa5136e30
1 changed files with 64 additions and 1 deletions

View File

@ -110,11 +110,37 @@ public:
_rc_update.setChannel(channel - 1, 0.f);
}
void checkReturnSwitch(float channel_value, float threshold, uint8_t expected_position)
{
// GIVEN: First channel is configured as return switch
_param_rc_map_return_sw.set(1);
_param_rc_map_return_sw.commit();
_param_rc_return_th.set(threshold);
_param_rc_return_th.commit();
_rc_update.updateParams();
EXPECT_EQ(_param_rc_map_return_sw.get(), 1);
EXPECT_FLOAT_EQ(_param_rc_return_th.get(), threshold);
// GIVEN: First channel has some value
_rc_update.setChannel(0, channel_value);
// WHEN: we update the switches two times to pass the simple outlier protection
_rc_update.UpdateManualSwitches(0);
_rc_update.UpdateManualSwitches(0);
// THEN: we receive the expected mode slot
uORB::SubscriptionData<manual_control_switches_s> manual_control_switches_sub{ORB_ID(manual_control_switches)};
manual_control_switches_sub.update();
EXPECT_EQ(manual_control_switches_sub.get().return_switch, expected_position);
}
TestRCUpdate _rc_update;
DEFINE_PARAMETERS(
(ParamInt<px4::params::RC_MAP_FLTMODE>) _param_rc_map_fltmode,
(ParamInt<px4::params::RC_MAP_FLTM_BTN>) _param_rc_map_fltm_btn
(ParamInt<px4::params::RC_MAP_FLTM_BTN>) _param_rc_map_fltm_btn,
(ParamInt<px4::params::RC_MAP_RETURN_SW>) _param_rc_map_return_sw,
(ParamFloat<px4::params::RC_RETURN_TH>) _param_rc_return_th
)
};
@ -160,3 +186,40 @@ TEST_F(RCUpdateTest, ModeSlotButtonAllValues)
checkModeSlotButton(31, 6, 1.f, 0); // button 6 pressed but not configured -> manual_control_switches_s::MODE_SLOT_NONE
checkModeSlotButton(63, 6, 1.f, 6); // button 6 pressed -> manual_control_switches_s::MODE_SLOT_6
}
TEST_F(RCUpdateTest, ReturnSwitchUnassigned)
{
// GIVEN: Default configuration with no assigned return switch
EXPECT_EQ(_param_rc_map_return_sw.get(), 0);
// WHEN: we update the switches two times to pass the simple outlier protection
_rc_update.UpdateManualSwitches(0);
_rc_update.UpdateManualSwitches(0);
// THEN: we receive an unmapped return switch state
uORB::SubscriptionData<manual_control_switches_s> manual_control_switches_sub{ORB_ID(manual_control_switches)};
manual_control_switches_sub.update();
EXPECT_EQ(manual_control_switches_sub.get().return_switch, 0); // manual_control_switches_s::SWITCH_POS_NONE
}
TEST_F(RCUpdateTest, ReturnSwitchPositiveThresholds)
{
checkReturnSwitch(-1.f, 0.5f, 3); // Below threshold -> SWITCH_POS_OFF
checkReturnSwitch(0.f, 0.5f, 3); // On threshold -> SWITCH_POS_OFF
checkReturnSwitch(.001f, 0.5f, 1); // Slightly above threshold -> SWITCH_POS_ON
checkReturnSwitch(1.f, 0.5f, 1); // Above threshold -> SWITCH_POS_ON
checkReturnSwitch(-1.f, 0.75f, 3); // Below threshold -> SWITCH_POS_OFF
checkReturnSwitch(0.f, 0.75f, 3); // Below threshold -> SWITCH_POS_OFF
checkReturnSwitch(.5f, 0.75f, 3); // On threshold -> SWITCH_POS_OFF
checkReturnSwitch(.501f, 0.75f, 1); // Slightly above threshold -> SWITCH_POS_ON
checkReturnSwitch(1.f, 0.75f, 1); // Above threshold -> SWITCH_POS_ON
checkReturnSwitch(-1.f, 0.f, 3); // On minimum threshold -> SWITCH_POS_OFF
checkReturnSwitch(-.999f, 0.f, 1); // Slightly above minimum threshold -> SWITCH_POS_ON
checkReturnSwitch(1.f, 0.f, 1); // Above minimum threshold -> SWITCH_POS_ON
checkReturnSwitch(-1.f, 1.f, 3); // Below maximum threshold -> SWITCH_POS_OFF
checkReturnSwitch(1.f, 1.f, 3); // On maximum threshold -> SWITCH_POS_OFF
}