18 #if defined(CONFIG_ARCH_BOARD_PX4_SITL) 19 #define TEST_DATA_PATH "./test_data/" 21 #define TEST_DATA_PATH "/fs/microsd" 33 bool dsmTest(
const char *filepath,
unsigned expected_chancount,
unsigned expected_dropcount,
unsigned chan0);
57 FILE *fp = fopen(filepath,
"rt");
62 const int line_size = 500;
64 bool has_decoded_values =
false;
65 const int max_channels = 16;
66 uint16_t rc_values[max_channels];
67 uint16_t num_values = 0;
70 while (fgets(line, line_size, fp) !=
nullptr) {
72 if (strncmp(line,
"INPUT ", 6) == 0) {
74 if (has_decoded_values) {
75 PX4_ERR(
"Parser decoded values that are not in the test file (line=%i)", line_counter);
80 const char *file_buffer = line + 6;
86 while (sscanf(file_buffer,
"%x, %n", &number, &offset) > 0) {
87 frame[frame_len++] = number;
88 file_buffer += offset;
94 bool result =
crsf_parse(now, frame, frame_len, rc_values, &num_values, max_channels);
97 has_decoded_values =
true;
100 }
else if (strncmp(line,
"DECODED ", 8) == 0) {
102 if (!has_decoded_values) {
103 PX4_ERR(
"Test file contains decoded values but the parser did not decode anything (line=%i)", line_counter);
108 const char *file_buffer = line + 8;
110 int expected_rc_value;
111 int expected_num_channels = 0;
113 while (sscanf(file_buffer,
"%x, %n", &expected_rc_value, &offset) > 0) {
116 if (
abs(expected_rc_value - (
int)rc_values[expected_num_channels]) > 10) {
117 PX4_ERR(
"File line: %i, channel: %i", line_counter, expected_num_channels);
118 ut_compare(
"Wrong decoded channel", expected_rc_value, rc_values[expected_num_channels]);
121 file_buffer += offset;
122 ++expected_num_channels;
125 if (expected_num_channels != num_values) {
126 PX4_ERR(
"File line: %d", line_counter);
127 ut_compare(
"Unexpected number of decoded channels", expected_num_channels, num_values);
130 has_decoded_values =
false;
149 bool RCTest::dsmTest(
const char *filepath,
unsigned expected_chancount,
unsigned expected_dropcount,
unsigned chan0)
153 fp = fopen(filepath,
"rt");
163 for (
unsigned i = 0; i < 20; i++) {
165 (void)fgets(buf,
sizeof(buf), fp);
170 uint16_t rc_values[18];
174 uint16_t max_channels =
sizeof(rc_values) /
sizeof(rc_values[0]);
176 int rate_limiter = 0;
177 unsigned last_drop = 0;
181 while (EOF != (ret = fscanf(fp,
"%f,%x,,", &f, &x))) {
192 bool result =
dsm_parse(f * 1e6f, &frame[0], len, rc_values, &num_values,
193 &dsm_11_bit, &dsm_frame_drops,
nullptr, max_channels);
196 ut_compare(
"num_values == expected_chancount", num_values, expected_chancount);
198 ut_test(
abs((
int)chan0 - (
int)rc_values[0]) < 30);
202 for (
unsigned i = 0; i < num_values; i++) {
207 if (last_drop != (dsm_frame_drops)) {
208 PX4_INFO(
"frame dropped, now #%d", (dsm_frame_drops));
218 PX4_INFO(
"drop: %d", (
int)last_drop);
219 ut_test(last_drop == expected_dropcount);
229 fp = fopen(filepath,
"rt");
239 for (
unsigned i = 0; i < 20; i++) {
241 (void)fgets(buf,
sizeof(buf), fp);
246 uint16_t rc_values[18];
249 unsigned sbus_frame_resets = 0;
251 bool sbus_frame_drop;
252 uint16_t max_channels =
sizeof(rc_values) /
sizeof(rc_values[0]);
254 int rate_limiter = 0;
255 unsigned last_drop = 0;
257 while (EOF != (ret = fscanf(fp,
"%f,%x,,", &f, &x))) {
271 bool result =
sbus_parse(now, &frame[0], len, rc_values, &num_values,
272 &sbus_failsafe, &sbus_frame_drop, &sbus_frame_drops, max_channels);
280 if (last_drop != (sbus_frame_drops + sbus_frame_resets)) {
281 PX4_WARN(
"frame dropped, now #%d", (sbus_frame_drops + sbus_frame_resets));
282 last_drop = sbus_frame_drops + sbus_frame_resets;
301 fp = fopen(filepath,
"rt");
309 for (
unsigned i = 0; i < 20; i++) {
311 (void)fgets(buf,
sizeof(buf), fp);
316 while (EOF != (ret = fscanf(fp,
"%f,%x,,", &f, &x))) {
323 if (((f - last_time) * 1000 * 1000) > 3000) {
327 uint8_t b =
static_cast<uint8_t
>(x);
336 uint16_t channel_count;
337 uint16_t channels[20];
339 if (!
st24_decode(b, &rssi, &rx_count, &channel_count, channels,
sizeof(channels) /
sizeof(channels[0]))) {
342 for (
unsigned i = 0; i < channel_count; i++) {
362 fp = fopen(filepath,
"rt");
370 for (
unsigned i = 0; i < 20; i++) {
372 (void)fgets(buf,
sizeof(buf), fp);
377 while (EOF != (ret = fscanf(fp,
"%f,%x,,", &f, &x))) {
384 if (((f - last_time) * 1000 * 1000) > 3000) {
388 uint8_t b =
static_cast<uint8_t
>(x);
397 uint16_t channel_count;
398 uint16_t channels[32];
402 if (!
sumd_decode(b, &rssi, &rx_count, &channel_count, channels, 32, &sumd_failsafe)) {
405 for (
unsigned i = 0; i < channel_count; i++) {
int sumd_decode(uint8_t byte, uint8_t *rssi, uint8_t *rx_count, uint16_t *channel_count, uint16_t *channels, uint16_t max_chan_count, bool *failsafe)
Decoder for SUMD/SUMH protocol.
RC protocol definition for Spektrum RC.
RC protocol definition for Graupner HoTT transmitter.
#define ut_declare_test_c(test_function, test_class)
bool dsmTest(const char *filepath, unsigned expected_chancount, unsigned expected_dropcount, unsigned chan0)
bool crsf_parse(const uint64_t now, const uint8_t *frame, unsigned len, uint16_t *values, uint16_t *num_values, uint16_t max_channels)
Parse the CRSF protocol and extract RC channel data.
Base class to be used for unit tests.
High-resolution timer with callouts and timekeeping.
int _tests_failed
The number of unit tests which failed.
#define ut_test(test)
Used to assert a value within a unit test.
RC protocol definition for Yuneec ST24 transmitter.
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
Simple error/warning functions, heavily inspired by the BSD functions of the same names...
bool dsm_parse(const uint64_t now, const uint8_t *frame, const unsigned len, uint16_t *values, uint16_t *num_values, bool *dsm_11_bit, unsigned *frame_drops, int8_t *rssi_percent, uint16_t max_channels)
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
static unsigned sbus_frame_drops
static unsigned dsm_frame_drops
Count of incomplete DSM frames.
int st24_decode(uint8_t byte, uint8_t *rssi, uint8_t *lost_count, uint16_t *channel_count, uint16_t *channels, uint16_t max_chan_count)
Decoder for ST24 protocol.
__EXPORT int rc_tests_main(int argc, char *argv[])
RC protocol definition for S.BUS.
#define ut_compare(message, v1, v2)
Used to compare two integer values within a unit test.
bool sbus_parse(uint64_t now, uint8_t *frame, unsigned len, uint16_t *values, uint16_t *num_values, bool *sbus_failsafe, bool *sbus_frame_drop, unsigned *frame_drops, uint16_t max_channels)
#define ut_run_test(test)
Runs a single unit test.
RC protocol definition for CSRF (TBS Crossfire).
Dual< Scalar, N > abs(const Dual< Scalar, N > &a)
bool run_tests() override
Override to run your unit tests.
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).