Skip to content

Commit

Permalink
Updated test fixture and some bug fixes as well
Browse files Browse the repository at this point in the history
  • Loading branch information
avtoku committed Nov 19, 2024
1 parent 31c02fb commit 1287aeb
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 30 deletions.
2 changes: 1 addition & 1 deletion boards/varmint_h7
1 change: 0 additions & 1 deletion include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ class Board

// RC
virtual void rc_init(rc_type_t rc_type) = 0;
virtual bool rc_lost() = 0;
virtual bool rc_read(rosflight_firmware::RcStruct * rc) = 0;

// PWM
Expand Down
3 changes: 3 additions & 0 deletions include/rc.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ class RC : public ParamListenerInterface
float read_chan(uint8_t chan);
bool receive(void);

// _FAKE_ RC receive. Use set_chan for testing only!
uint16_t fake_rx(uint16_t *chan, uint16_t len, bool lost, bool failsafe);

private:
RcStruct rc_={};

Expand Down
14 changes: 13 additions & 1 deletion src/rc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ bool RC::check_rc_lost()
bool failsafe = false;

// If the board reports that we have lost RC, tell the state manager
if (RF_.board_.rc_lost()) {
if(rc_.frameLost | rc_.failsafeActivated) {
failsafe = true;
} else {
// go into failsafe if we get an invalid RC command for any channel
Expand Down Expand Up @@ -285,4 +285,16 @@ bool RC::new_command()
}
}

uint16_t RC::fake_rx(uint16_t *chan, uint16_t len, bool lost, bool failsafe) {
len = (len<RC_STRUCT_CHANNELS)?len:RC_STRUCT_CHANNELS;
for(int i=0;i<len;i++) rc_.chan[i] = (static_cast<float>(chan[i])-1000.)/1000.;
rc_.failsafeActivated = failsafe;
rc_.frameLost = lost;
rc_.nChan = RC_STRUCT_CHANNELS;
rc_.timestamp = RF_.board_.clock_micros();
run();
return len;
};


} // namespace rosflight_firmware
23 changes: 14 additions & 9 deletions test/command_manager_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class CommandManagerTest : public ::testing::Test
ROSflight rf;
float last_set_rc = 0;

bool rc_lost=false;
uint16_t rc_values[8];
float max_roll, max_pitch, max_yawrate;

Expand All @@ -44,8 +45,7 @@ class CommandManagerTest : public ::testing::Test
void SetUp() override
{
rf.init();
rf.state_manager_.clear_error(
rf.state_manager_.state().error_codes); // Clear All Errors to Start
rf.state_manager_.clear_error(rf.state_manager_.state().error_codes); // Clear All Errors to Start
rf.params_.set_param_int(PARAM_CALIBRATE_GYRO_ON_ARM, false);

for (int i = 0; i < 8; i++) { rc_values[i] = 1500; }
Expand All @@ -72,7 +72,7 @@ class CommandManagerTest : public ::testing::Test
while (board.clock_micros() < start_time_us + us) {
if (board.clock_millis() > last_set_rc + 20) {
last_set_rc = board.clock_millis();
board.set_rc(rc_values);
rf.rc_.fake_rx(rc_values,8, rc_lost, false); // the false is for failsafe.
}
board.set_imu(dummy_acc, dummy_gyro, board.clock_micros() + 1000);
rf.run();
Expand Down Expand Up @@ -240,7 +240,8 @@ TEST_F(CommandManagerTest, RCOutput)

TEST_F(CommandManagerTest, LoseRCDisarmed)
{
board.set_pwm_lost(true);

rc_lost = true;
stepFirmware(50000);

control_t output = rf.command_manager_.combined_control();
Expand All @@ -262,9 +263,10 @@ TEST_F(CommandManagerTest, LoseRCDisarmed)

TEST_F(CommandManagerTest, RegainRCDisarmed)
{
board.set_pwm_lost(true);
rc_lost = true;
stepFirmware(40000);
board.set_pwm_lost(false);

rc_lost = false;
stepFirmware(40000);

EXPECT_EQ(rf.state_manager_.state().error, false);
Expand All @@ -277,7 +279,8 @@ TEST_F(CommandManagerTest, LoseRCArmed)

rf.state_manager_.set_event(StateManager::EVENT_REQUEST_ARM);
EXPECT_EQ(rf.state_manager_.state().armed, true);
board.set_pwm_lost(true);

rc_lost = true;
stepFirmware(20000);

control_t output = rf.command_manager_.combined_control();
Expand All @@ -302,9 +305,11 @@ TEST_F(CommandManagerTest, RegainRCArmed)
stepFirmware(50000);
rf.state_manager_.set_event(StateManager::EVENT_REQUEST_ARM);

board.set_pwm_lost(true);

rc_lost = true;
stepFirmware(20000);
board.set_pwm_lost(false);

rc_lost = false;
stepFirmware(20000);

EXPECT_EQ(rf.state_manager_.state().armed, true);
Expand Down
11 changes: 7 additions & 4 deletions test/state_machine_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class StateMachineTest : public ::testing::Test
uint16_t rc_values[8];
for (int i = 0; i < 8; i++) { rc_values[i] = 1500; }
rc_values[2] = 1000;
board.set_rc(rc_values);
rf.rc_.fake_rx(rc_values,8,false,false);

stepFirmware(100000);
}
Expand Down Expand Up @@ -291,7 +291,8 @@ TEST_F(StateMachineTest, ArmIfThrottleLow)
uint16_t rc_values[8];
for (int i = 0; i < 8; i++) { rc_values[i] = (i > 3) ? 1000 : 1500; }
rc_values[2] = 1000;
board.set_rc(rc_values);
rf.rc_.fake_rx(rc_values,8,false,false);

step_firmware(rf, board, 100);
rf.state_manager_.set_event(StateManager::EVENT_REQUEST_ARM);
EXPECT_EQ(true, rf.state_manager_.state().armed);
Expand All @@ -303,7 +304,8 @@ TEST_F(StateMachineTest, ArmIfThrottleHighWithMinThrottle)
uint16_t rc_values[8];
for (int i = 0; i < 8; i++) { rc_values[i] = (i > 3) ? 1000 : 1500; }
rc_values[2] = 1500;
board.set_rc(rc_values);
rf.rc_.fake_rx(rc_values,8,false,false);

step_firmware(rf, board, 100000);

rf.state_manager_.set_event(StateManager::EVENT_REQUEST_ARM);
Expand All @@ -317,7 +319,8 @@ TEST_F(StateMachineTest, DontArmIfThrottleHighWithoutMinThrottle)
uint16_t rc_values[8];
for (int i = 0; i < 8; i++) { rc_values[i] = (i > 3) ? 1000 : 1500; }
rc_values[2] = 1500;
board.set_rc(rc_values);
rf.rc_.fake_rx(rc_values,8,false,false);

step_firmware(rf, board, 100000);

rf.state_manager_.set_event(StateManager::EVENT_REQUEST_ARM);
Expand Down
12 changes: 1 addition & 11 deletions test/test_board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,9 @@

namespace rosflight_firmware
{
void testBoard::set_rc(uint16_t * values)
{
for (int i = 0; i < 8; i++) { rc_values[i] = values[i]; }
new_rc_ = true;
}

void testBoard::set_time(uint64_t time_us) { time_us_ = time_us; }

void testBoard::set_pwm_lost(bool lost) { rc_lost_ = lost; }

void testBoard::set_imu(float * acc, float * gyro, uint64_t time_us)
{
time_us_ = time_us;
Expand Down Expand Up @@ -130,12 +123,9 @@ void testBoard::battery_current_set_multiplier(double multiplier) {}
// PWM
// TODO make these deal in normalized (-1 to 1 or 0 to 1) values (not pwm-specific)
void testBoard::rc_init(rc_type_t rc_type) {(void)rc_type;}
bool testBoard::rc_lost() { return rc_lost_; }

bool testBoard::rc_read(rosflight_firmware::RcStruct * rc_struct)
{
// ???? what goes here ???
return true;
return false;
}
void testBoard::pwm_write(float * value, uint32_t channels) {}
void testBoard::pwm_init(const float * rate, uint32_t channels) {}
Expand Down
3 changes: 0 additions & 3 deletions test/test_board.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class testBoard : public Board
uint16_t rc_values[8] = {1500, 1500, 1000, 1500, 1500, 1500, 1500, 1500};
bool new_rc_ = false;
uint64_t time_us_ = 0;
bool rc_lost_ = false;
float acc_[3] = {0, 0, 0};
float gyro_[3] = {0, 0, 0};
bool new_imu_ = false;
Expand Down Expand Up @@ -95,7 +94,6 @@ class testBoard : public Board

// RC
void rc_init(rc_type_t rc_type) override;
bool rc_lost() override;
bool rc_read(rosflight_firmware::RcStruct * rc) override;

// PWM
Expand Down Expand Up @@ -125,7 +123,6 @@ class testBoard : public Board
void backup_memory_clear();

void set_imu(float * acc, float * gyro, uint64_t time_us);
void set_rc(uint16_t * values);
void set_time(uint64_t time_us);
void set_pwm_lost(bool lost);
};
Expand Down

0 comments on commit 1287aeb

Please sign in to comment.