Skip to content

Commit

Permalink
rc_update: switch back to protected scope and use fixture for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
MaEtUgR committed Jul 11, 2023
1 parent 81764c4 commit 87c697a
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 57 deletions.
136 changes: 79 additions & 57 deletions src/modules/rc_update/RCUpdateTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,87 @@

using namespace rc_update;

TEST(RCUpdateTest, ModeSlotUnassigned)
class TestRCUpdate : public RCUpdate
{
public:
void UpdateManualSwitches(const hrt_abstime &timestamp_sample) { RCUpdate::UpdateManualSwitches(timestamp_sample); }
void update_rc_functions() { RCUpdate::update_rc_functions(); }
void setChannel(size_t index, float channel_value) { _rc.channels[index] = channel_value; }
};

class RCUpdateTest : public ::testing::Test, ModuleParams
{
public:
RCUpdateTest() : ModuleParams(nullptr) {}

void SetUp() override
{
// Disable autosaving parameters to avoid busy loop in param_set()
param_control_autosave(false);
}

void checkModeSlotSwitch(float channel_value, uint8_t expected_slot)
{
// GIVEN: First channel is configured as mode switch
_param_rc_map_fltmode.set(1);
EXPECT_EQ(_param_rc_map_fltmode.get(), 1);
// 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().mode_slot, expected_slot);
}

void checkModeSlotButton(uint8_t button_configuration, uint8_t channel, float channel_value, uint8_t expected_slot)
{
// GIVEN: Buttons are configured
_param_rc_map_fltm_btn.set(button_configuration);
EXPECT_EQ(_param_rc_map_fltm_btn.get(), button_configuration);
// GIVEN: buttons are mapped
_rc_update.update_rc_functions();
// GIVEN: First channel has some value
_rc_update.setChannel(channel - 1, channel_value);

// WHEN: we update the switches 4 times:
// - initiate the button press
// - keep the same button pressed
// - hold the button for 50ms
// - pass the simple outlier protection
_rc_update.UpdateManualSwitches(0);
_rc_update.UpdateManualSwitches(0);
_rc_update.UpdateManualSwitches(51_ms);
_rc_update.UpdateManualSwitches(51_ms);

// 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().mode_slot, expected_slot);
}

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
)
};

TEST_F(RCUpdateTest, ModeSlotUnassigned)
{
RCUpdate rc_update;
// GIVEN: Default configuration with no assigned mode switch
EXPECT_EQ(rc_update._param_rc_map_fltmode.get(), 0);
EXPECT_EQ(_param_rc_map_fltmode.get(), 0);

// WHEN: we update the switches two times to pass the simple outlier protection
rc_update.UpdateManualSwitches(0);
rc_update.UpdateManualSwitches(0);
_rc_update.UpdateManualSwitches(0);
_rc_update.UpdateManualSwitches(0);

// THEN: we receive no mode slot
uORB::SubscriptionData<manual_control_switches_s> manual_control_switches_sub{ORB_ID(manual_control_switches)};
Expand All @@ -55,28 +127,7 @@ TEST(RCUpdateTest, ModeSlotUnassigned)
EXPECT_EQ(manual_control_switches_sub.get().mode_slot, 0); // manual_control_switches_s::MODE_SLOT_NONE
}

void checkModeSlotSwitch(float channel_value, uint8_t expected_slot)
{
RCUpdate rc_update;

// GIVEN: First channel is configured as mode switch
rc_update._param_rc_map_fltmode.set(1);
EXPECT_EQ(rc_update._param_rc_map_fltmode.get(), 1);
// GIVEN: First channel has some value
rc_update._rc.channels[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().mode_slot, expected_slot);
}

TEST(RCUpdateTest, ModeSlotSwitchAllValues)
TEST_F(RCUpdateTest, ModeSlotSwitchAllValues)
{
checkModeSlotSwitch(-1.f, 1); // manual_control_switches_s::MODE_SLOT_1
checkModeSlotSwitch(-.5f, 2); // manual_control_switches_s::MODE_SLOT_2
Expand All @@ -86,36 +137,7 @@ TEST(RCUpdateTest, ModeSlotSwitchAllValues)
checkModeSlotSwitch(1.f, 6); // manual_control_switches_s::MODE_SLOT_6
}

void checkModeSlotButton(uint8_t button_configuration, uint8_t channel, float channel_value, uint8_t expected_slot)
{
RCUpdate rc_update;

// GIVEN: Buttons are configured
rc_update._param_rc_map_fltm_btn.set(button_configuration);
EXPECT_EQ(rc_update._param_rc_map_fltm_btn.get(), button_configuration);
// GIVEN: buttons are mapped
rc_update.update_rc_functions();
// GIVEN: First channel has some value
rc_update._rc.channels[channel - 1] = channel_value;

// WHEN: we update the switches 4 times:
// - initiate the button press
// - keep the same button pressed
// - hold the button for 50ms
// - pass the simple outlier protection
rc_update.UpdateManualSwitches(0);
rc_update.UpdateManualSwitches(0);
rc_update.UpdateManualSwitches(51_ms);
rc_update.UpdateManualSwitches(51_ms);

// 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().mode_slot, expected_slot);
}

TEST(RCUpdateTest, ModeSlotButtonAllValues)
TEST_F(RCUpdateTest, ModeSlotButtonAllValues)
{
checkModeSlotButton(1, 1, -1.f, 0); // button not pressed -> manual_control_switches_s::MODE_SLOT_NONE
checkModeSlotButton(1, 1, 0.f, 0); // button not pressed over threshold -> manual_control_switches_s::MODE_SLOT_NONE
Expand Down
1 change: 1 addition & 0 deletions src/modules/rc_update/rc_update.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class RCUpdate : public ModuleBase<RCUpdate>, public ModuleParams, public px4::W

int print_status() override;

protected:
static constexpr uint64_t VALID_DATA_MIN_INTERVAL_US{1_s / 3}; // assume valid RC input is at least 3 Hz

void Run() override;
Expand Down

0 comments on commit 87c697a

Please sign in to comment.