From 0e0b417b6f19e11e99c472f100e16117a25a2a6b Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 24 Nov 2022 17:08:44 +0100 Subject: [PATCH] capteurs ultrason --- CMakeLists.txt | 3 ++- HCSR04.h | 14 ++++++++++++++ main.cpp | 19 ++++++++++++++++++- parser.py | 9 ++++++++- projet-tuteure-3a | 2 +- 5 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 HCSR04.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 406c318..04e21a8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,5 +5,6 @@ project(smogogus) add_definitions("-x c++") include_directories(${PROJECT_SOURCE_DIR}) set_source_files_properties(projet-tuteure-3a/photores.ino PROPERTIES LANGUAGE CXX) -add_executable(app projet-tuteure-3a/motorController.cpp projet-tuteure-3a/photores.ino main.cpp) +set_source_files_properties(projet-tuteure-3a/principal_ultrason.ino PROPERTIES LANGUAGE CXX) +add_executable(app projet-tuteure-3a/motorController.cpp projet-tuteure-3a/principal_ultrason.ino main.cpp) add_definitions(-include ../adapter_ard.h) diff --git a/HCSR04.h b/HCSR04.h new file mode 100644 index 0000000..746d440 --- /dev/null +++ b/HCSR04.h @@ -0,0 +1,14 @@ +#include + +typedef uint8_t byte; + +class HCSR04_c{ + public: + void begin(byte trig, byte* echos, byte ecnt); + double* measureDistanceCm(); +}; + +extern HCSR04_c HCSR04; + +int couloir(void); +int rotCouloir(int dir); \ No newline at end of file diff --git a/main.cpp b/main.cpp index 04b3162..36eda96 100644 --- a/main.cpp +++ b/main.cpp @@ -1,4 +1,5 @@ #include "adapter_ard.h" +#include "HCSR04.h" #include #include @@ -7,9 +8,13 @@ #include serial_c Serial; + +HCSR04_c HCSR04; + struct timespec start, end; int pinState[16]; +double dists[2]; int main(int argc, char** argv){ clock_gettime(CLOCK_MONOTONIC_RAW, &start); @@ -28,7 +33,10 @@ int main(int argc, char** argv){ char* end_ptr; int pin = strtol(&cmd_buff[1], &end_ptr, 10); int level = strtol(&end_ptr[1], &end_ptr, 10); - pinState[pin] = level; + //printf("pin %d at %d\n", pin, level); + if(pin >= 0) + pinState[pin] = level; + else dists[-pin-1] = level/10.0; cmd_buff_ind = 0; } cmd_buff[cmd_buff_ind++] = c; @@ -94,4 +102,13 @@ void serial_c::print(int nb){ void serial_c::println(int nb){ this->print(nb); printf("\n"); +} + +void HCSR04_c::begin(byte trig, byte* echos, byte ecnt){ + //printf(""); +} + +double* HCSR04_c::measureDistanceCm(){ + //printf("dists %f %f", dists[0], dists[1]); + return dists; } \ No newline at end of file diff --git a/parser.py b/parser.py index fb2f6b5..622d932 100644 --- a/parser.py +++ b/parser.py @@ -59,4 +59,11 @@ class aip_parser: def send_pin_state(self): for ind, p in enumerate(self.robot.pins): - self.stdin.write(bytes(f"${ind},{p};", "utf-8")) \ No newline at end of file + self.stdin.write(bytes(f"${ind},{p};", "utf-8")) + for ind, p in enumerate(self.robot.dists): + if not p: + p = -10 + else: + p = int(1000*p) + print("#######", p) + self.stdin.write(bytes(f"${-ind-1},{p};", "utf-8")) \ No newline at end of file diff --git a/projet-tuteure-3a b/projet-tuteure-3a index 397f93f..a6e75ef 160000 --- a/projet-tuteure-3a +++ b/projet-tuteure-3a @@ -1 +1 @@ -Subproject commit 397f93f67d864dbe52095f9e91dd8bbad8364960 +Subproject commit a6e75ef26b8896385a994733c114694f14862e20