From 7dd81c8cb2a05321bcb28819d6eb3aec67052517 Mon Sep 17 00:00:00 2001 From: Lorenz Meier Date: Sat, 27 Sep 2014 16:21:25 +0200 Subject: [PATCH] WIP on laser driver unit test --- Tools/tests-host/Makefile | 11 ++- Tools/tests-host/sf0x_data.txt | 33 +++++++ Tools/tests-host/sf0x_test | Bin 0 -> 9228 bytes Tools/tests-host/sf0x_test.cpp | 157 +++++++++++++++++++++++++++++++++ 4 files changed, 199 insertions(+), 2 deletions(-) create mode 100644 Tools/tests-host/sf0x_data.txt create mode 100755 Tools/tests-host/sf0x_test create mode 100644 Tools/tests-host/sf0x_test.cpp diff --git a/Tools/tests-host/Makefile b/Tools/tests-host/Makefile index f0737ef88c..b1547c23f0 100644 --- a/Tools/tests-host/Makefile +++ b/Tools/tests-host/Makefile @@ -3,7 +3,7 @@ CC=g++ CFLAGS=-I. -I../../src/modules -I ../../src/include -I../../src/drivers \ -I../../src -I../../src/lib -D__EXPORT="" -Dnullptr="0" -lm -all: mixer_test sbus2_test autodeclination_test +all: mixer_test sbus2_test autodeclination_test sf0x_test MIXER_FILES=../../src/systemcmds/tests/test_mixer.cpp \ ../../src/systemcmds/tests/test_conv.cpp \ @@ -20,6 +20,10 @@ SBUS2_FILES=../../src/modules/px4iofirmware/sbus.c \ hrt.cpp \ sbus2_test.cpp +SF0X_FILES= \ + hrt.cpp \ + sf0x_test.cpp + AUTODECLINATION_FILES= ../../src/lib/geo/geo_mag_declination.c \ hrt.cpp \ autodeclination_test.cpp @@ -30,10 +34,13 @@ mixer_test: $(MIXER_FILES) sbus2_test: $(SBUS2_FILES) $(CC) -o sbus2_test $(SBUS2_FILES) $(CFLAGS) +sf0x_test: $(SF0X_FILES) + $(CC) -o sf0x_test $(SF0X_FILES) $(CFLAGS) + autodeclination_test: $(SBUS2_FILES) $(CC) -o autodeclination_test $(AUTODECLINATION_FILES) $(CFLAGS) .PHONY: clean clean: - rm -f $(ODIR)/*.o *~ core $(INCDIR)/*~ mixer_test sbus2_test autodeclination_test \ No newline at end of file + rm -f $(ODIR)/*.o *~ core $(INCDIR)/*~ mixer_test sbus2_test autodeclination_test sf0x_test \ No newline at end of file diff --git a/Tools/tests-host/sf0x_data.txt b/Tools/tests-host/sf0x_data.txt new file mode 100644 index 0000000000..f61f59523e --- /dev/null +++ b/Tools/tests-host/sf0x_data.txt @@ -0,0 +1,33 @@ +0.02 +0.25 +0.03 +0.02 +0.25 +0.03 +0.02 +0.25 +0.03 +0.02 +0.25 +0.03 +0.25 +0.03 +0.02 +0.25 +0.03 +0.08 +0.40 +0.44 + + +0 +. +0 +1 + +0 +0. +.0 +.01 +01 +1 \ No newline at end of file diff --git a/Tools/tests-host/sf0x_test b/Tools/tests-host/sf0x_test new file mode 100755 index 0000000000000000000000000000000000000000..e56c35465423dc3abd2cccd3fdc118953c6a2cac GIT binary patch literal 9228 zcmeHNQEVJX8J>%4;-oHn79|)#VM*i4O(N%vC^EDGJI%sY#37ASltzsvYu{e(+VkD9 zw@1&Y7aXOQI2|r7v{i)=vUotE0%SoSQh`!SL9xpVhBri1`JqK;2_(xw!sMam`(|g8 zyEQ4iz|;QH%>RG?{PWL0|IVCuXZ-t{e|vAI5Qz>UrgsP-dQet7g_sd0Bq2VEVxvfT zV&u!#8SCU%scol3JN&nZeySKi%2U?qQ|%_If3jUCG)8O!;Rte)l)hW_13AF${?6Tv z$)Mr8oD7Kgu0RaxyTo)Wl(Ots#>=-<(fHQy3H*IlTZB05(53x-Kq~@&GVA6`t_NMz zU;hUKe@|q92xMA`Rph4Wew^|zw^{YtkJ zw^CcIXppi}cs5r^%UmH-RJi?lTrz_Bat)D)-;bnpvohm3lWt(OEk5lpobL)S*6tmN z>uv}OLdv5fr$$=Lu)f;XonCLP>yCay-%=LxvOG08Ud+pq@7>W~=!Sm6z8sb7V;2QY zQl2l~eyZE!OX~Q#v^{auAC~qlk}~hSUwpmVU-%q|Tm5l=b>H&Vy6=xYcKj>W(Z|9) zYew=0OzPH)j(${Ll~#zo_+3d+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f7e54a6 --- /dev/null +++ b/Tools/tests-host/sf0x_test.cpp @@ -0,0 +1,157 @@ + +#include +#include +#include +#include +#include +#include + +enum SF0X_PARSE_STATE { + SF0X_PARSE_STATE0_UNSYNC = 0, + SF0X_PARSE_STATE1_SYNC, + SF0X_PARSE_STATE2_GOT_DIGIT0, + SF0X_PARSE_STATE3_GOT_DOT, + SF0X_PARSE_STATE4_GOT_DIGIT1, + SF0X_PARSE_STATE5_GOT_DIGIT2, + SF0X_PARSE_STATE6_GOT_CARRIAGE_RETURN +}; + +int sf0x_parser(char c, char *parserbuf, unsigned *parserbuf_index, enum *SF0X_PARSE_STATE state, float *dist) +{ + int ret = -1; + + unsigned len = strlen(parserbuf); + switch (state) { + case SF0X_PARSE_STATE0_UNSYNC: + if (c == '\n') { + *state = SF0X_PARSE_STATE1_SYNC; + *parserbuf_index = 0; + } + break; + + case SF0X_PARSE_STATE1_SYNC: + if (c >= '0' && c <= '9') { + *state = SF0X_PARSE_STATE2_GOT_DIGIT0; + parserbuf[parserbuf_index] = c; + parserbuf_index++; + } + break; + + case SF0X_PARSE_STATE2_GOT_DIGIT0: + if (c >= '0' && c <= '9') { + *state = SF0X_PARSE_STATE2_GOT_DIGIT0; + parserbuf[parserbuf_index] = c; + parserbuf_index++; + } else if (c == '.') { + *state = SF0X_PARSE_STATE3_GOT_DOT; + parserbuf[parserbuf_index] = c; + parserbuf_index++; + } else { + *state = SF0X_PARSE_STATE0_UNSYNC; + } + break; + + case SF0X_PARSE_STATE3_GOT_DOT: + if (c >= '0' && c <= '9') { + *state = SF0X_PARSE_STATE4_GOT_DIGIT1; + parserbuf[parserbuf_index] = c; + parserbuf_index++; + } else { + *state = SF0X_PARSE_STATE0_UNSYNC; + } + break; + + case SF0X_PARSE_STATE4_GOT_DIGIT1: + if (c >= '0' && c <= '9') { + *state = SF0X_PARSE_STATE4_GOT_DIGIT2; + parserbuf[parserbuf_index] = c; + parserbuf_index++; + } else { + *state = SF0X_PARSE_STATE0_UNSYNC; + } + break; + + case SF0X_PARSE_STATE5_GOT_DIGIT2: + if (c == '\r') { + *state = SF0X_PARSE_STATE6_GOT_CARRIAGE_RETURN; + } else { + *state = SF0X_PARSE_STATE0_UNSYNC; + } + break; + + case SF0X_PARSE_STATE6_GOT_CARRIAGE_RETURN: + if (c == '\n') { + parserbuf[parserbuf_index] = '\0'; + *dist = strtod(linebuf, &end); + *state = SF0X_PARSE_STATE0_SYNC; + *parserbuf_index = 0; + ret = 0; + } else { + *state = SF0X_PARSE_STATE0_UNSYNC; + } + break; + } + + + + return ret; +} + +int main(int argc, char *argv[]) { + warnx("SF0X test started"); + + if (argc < 2) + errx(1, "Need a filename for the input file"); + + warnx("loading data from: %s", argv[1]); + + FILE *fp; + + fp = fopen(argv[1],"rt"); + + if (!fp) + errx(1, "failed opening file"); + + int ret = 0; + + const char LINE_MAX = 20; + char _linebuf[LINE_MAX]; + _linebuf[0] = '\0'; + + char *end; + + enum SF0X_PARSE_STATE state = SF0X_PARSE_STATE0_UNSYNC; + float dist_m; + char _parserbuf[LINE_MAX]; + unsigned _parsebuf_index = 0; + + while (fgets(_linebuf, LINE_MAX, fp) != NULL) { + + printf("\n%s", _linebuf); + + int parse_ret; + + for (int i = 0; i < strlen(_linebuf); i++) + { + printf("%0x ", _linebuf[i]); + parse_ret = sf0x_parser(_linebuf[i], _parserbuf, &_parsebuf_index, &state, &dist_m); + + if (parse_ret == 0) { + printf("PARSED!"); + } + } + + printf("\nparsed: %f %s\n", dist_m, (parse_ret == 0) ? "OK" : ""); + } + + // Init the parser + + + if (ret == EOF) { + warnx("Test finished, reached end of file"); + } else { + warnx("Test aborted, errno: %d", ret); + } + + return ret; +}