Skip to content

Commit 2f47968

Browse files
committed
bug: fixed initialization error, as well as unsigned data type error. feat: added unit test for rosflight_main loop
1 parent 70f5408 commit 2f47968

File tree

4 files changed

+75
-5
lines changed

4 files changed

+75
-5
lines changed

include/rosflight.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class ROSflight
9292
&comm_manager_, &command_manager_, &controller_, &estimator_, &mixer_, &rc_, &sensors_};
9393

9494
uint32_t loop_time_us_;
95-
uint64_t last_time_;
95+
int64_t last_time_;
9696
float dt_;
9797
};
9898

src/rosflight.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ ROSflight::ROSflight(Board & board, CommLinkInterface & comm_link)
4646
, rc_(*this)
4747
, sensors_(*this)
4848
, state_manager_(*this)
49-
, dt_(0)
50-
, last_time_(0)
5149
, loop_time_us_(0)
50+
, last_time_(0)
51+
, dt_(0)
5252
{
5353
comm_link.set_listener(&comm_manager_);
5454
params_.set_listeners(param_listeners_, num_param_listeners_);
@@ -112,7 +112,7 @@ void ROSflight::run()
112112

113113
got_flags got = sensors_.run(); // IMU, GNSS, Baro, Mag, Pitot, SONAR, Battery
114114

115-
if (got.imu && check_time_going_forwards()) {
115+
if (got.imu && check_time_going_forwards()) { // dt_ is computed by check_time_going_forwards
116116
uint64_t start = board_.clock_micros();
117117
estimator_.run(dt_);
118118
controller_.run(dt_);
@@ -152,7 +152,7 @@ uint32_t ROSflight::get_loop_time_us() { return loop_time_us_; }
152152
*/
153153
bool ROSflight::check_time_going_forwards()
154154
{
155-
const uint64_t now_us = sensors_.get_imu()->header.timestamp;
155+
const int64_t now_us = static_cast<int64_t>(sensors_.get_imu()->header.timestamp);
156156
if (last_time_ == 0) {
157157
last_time_ = now_us;
158158
return false;

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ add_executable(unit_tests
2323
command_manager_test.cpp
2424
estimator_test.cpp
2525
parameters_test.cpp
26+
rosflight_test.cpp
2627
)
2728
2829
target_include_directories(unit_tests PUBLIC .)

test/rosflight_test.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include "common.h"
2+
#include "mavlink.h"
3+
#include "test_board.h"
4+
#include "state_manager.h"
5+
6+
#include "rosflight.h"
7+
8+
9+
using namespace rosflight_firmware;
10+
11+
class ROSflightTest : public ::testing::Test
12+
{
13+
public:
14+
testBoard board;
15+
Mavlink mavlink;
16+
ROSflight rf;
17+
18+
ROSflightTest()
19+
: mavlink(board)
20+
, rf(board, mavlink)
21+
{}
22+
23+
void SetUp() override
24+
{
25+
// Initialize firmware with no errors
26+
board.backup_memory_clear();
27+
rf.init();
28+
rf.state_manager_.clear_error(
29+
rf.state_manager_.state().error_codes); // Clear All Errors to Start
30+
rf.params_.set_param_int(PARAM_PRIMARY_MIXER, 10);
31+
rf.params_.set_param_int(PARAM_CALIBRATE_GYRO_ON_ARM, false); // default to turning this off
32+
rf.params_.set_param_float(PARAM_FAILSAFE_THROTTLE, 0.0f);
33+
}
34+
};
35+
36+
TEST_F(ROSflightTest, MainLoopSetAndClearTimeGoingBackwards)
37+
{
38+
// Set initial time and step
39+
board.set_time(100);
40+
rf.sensors_.run();
41+
board.set_time(500);
42+
rf.run();
43+
44+
// Step time backward
45+
board.set_time(100);
46+
rf.run();
47+
48+
EXPECT_EQ(rf.state_manager_.state().error_codes, StateManager::ERROR_TIME_GOING_BACKWARDS);
49+
50+
// Step time forward
51+
board.set_time(500);
52+
rf.run();
53+
EXPECT_EQ(rf.state_manager_.state().error_codes, StateManager::ERROR_NONE);
54+
}
55+
56+
TEST_F(ROSflightTest, CheckTimeGoingForward)
57+
{
58+
board.set_time(10000);
59+
rf.sensors_.run();
60+
EXPECT_EQ(rf.sensors_.get_imu()->header.timestamp, 10000);
61+
rf.check_time_going_forwards();
62+
63+
board.set_time(1000);
64+
rf.sensors_.run();
65+
rf.check_time_going_forwards();
66+
67+
EXPECT_EQ(rf.sensors_.get_imu()->header.timestamp, 1000);
68+
EXPECT_EQ(rf.state_manager_.state().error_codes, StateManager::ERROR_TIME_GOING_BACKWARDS);
69+
}

0 commit comments

Comments
 (0)