From 9f4a0b75235cc00d213ccb6f9a0c326edc07b588 Mon Sep 17 00:00:00 2001 From: Samuel Date: Thu, 25 Jul 2024 21:00:44 +0200 Subject: [PATCH] =?UTF-8?q?Permi=C3=A8re=20communication=20du=20PAMI=20en?= =?UTF-8?q?=20WiFi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/c_cpp_properties.json | 4 +- .vscode/tasks.json | 4 +- CMakeLists.txt | 4 +- Teleplot.c | 92 +++++++++++++++++++++++++++++++++++ Teleplot.h | 5 ++ VL53L8_2024.c | 4 +- lwipopts.h | 10 ++++ lwipopts_examples_common.h | 90 ++++++++++++++++++++++++++++++++++ main.c | 5 ++ wifi_settings.h.default | 9 ++++ 10 files changed, 221 insertions(+), 6 deletions(-) create mode 100644 Teleplot.c create mode 100644 Teleplot.h create mode 100644 lwipopts.h create mode 100644 lwipopts_examples_common.h create mode 100644 wifi_settings.h.default diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 05ca5f8..fef8b00 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -7,7 +7,9 @@ "${env:PICO_SDK_PATH}/src/common/pico_base/include", "${env:PICO_SDK_PATH}/build/generated/pico_base", "${env:PICO_SDK_PATH}/src/common/pico_base/include/pico", - "${env:PICO_SDK_PATH}/src/common/pico_stdlib/include" + "${env:PICO_SDK_PATH}/src/common/pico_stdlib/include", + "${env:PICO_SDK_PATH}/lib/cyw43-driver/src/", + "${env:PICO_SDK_PATH}/lib/**/include" ], "myCompilerPath": "/usr/bin/arm-none-eabi-gcc" }, diff --git a/.vscode/tasks.json b/.vscode/tasks.json index c98bf9b..3a0a9a8 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -2,7 +2,7 @@ "tasks": [ { "type": "shell", - "command": "cd build; cmake ../; make", + "command": "cd build; cmake ../ -DPICO_BOARD=pico_w; make", "label": "CMake in build/", "problemMatcher": [], "group": { @@ -12,7 +12,7 @@ }, { "type": "shell", - "command": "cd build; cmake ../; make Flash", + "command": "cd build; cmake ../ -DPICO_BOARD=pico_w; make Flash", "label": "CMake & Make & Flash", "problemMatcher": [], "group": { diff --git a/CMakeLists.txt b/CMakeLists.txt index f9f23d3..8c25102 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,7 @@ add_executable(Mon_Projet Localisation.c main.c QEI.c + Teleplot.c Temps.c Trajectoire_bezier.c Trajectoire_circulaire.c @@ -36,7 +37,7 @@ add_executable(Mon_Projet pico_generate_pio_header(Mon_Projet ${CMAKE_CURRENT_LIST_DIR}/quadrature_encoder.pio) -target_include_directories(Mon_Projet PRIVATE VL53L8CX_ULD_API/inc/) +target_include_directories(Mon_Projet PRIVATE VL53L8CX_ULD_API/inc/ ${CMAKE_CURRENT_LIST_DIR}) target_link_libraries(Mon_Projet hardware_adc @@ -45,6 +46,7 @@ target_link_libraries(Mon_Projet hardware_pio pico_stdlib pico_multicore + pico_cyw43_arch_lwip_poll ) pico_enable_stdio_usb(Mon_Projet 1) diff --git a/Teleplot.c b/Teleplot.c new file mode 100644 index 0000000..0d214ad --- /dev/null +++ b/Teleplot.c @@ -0,0 +1,92 @@ +#include "pico/cyw43_arch.h" +#include "Teleplot.h" + +// Si le fichier n'existe pas, créez-le à partir du modèle "wifi_settings.h.default" +#include "wifi_settings.h" + +#define BEACON_MSG_LEN_MAX 500 + +char teleplot_tampon[BEACON_MSG_LEN_MAX]=""; + +struct udp_pcb* pcb; +ip_addr_t addr; + +int Teleplot_init(void){ + if (cyw43_arch_init()) { + printf("failed to initialise\n"); + return 1; + } + + cyw43_arch_enable_sta_mode(); + + printf("Connecting to Wi-Fi...\n"); + if (cyw43_arch_wifi_connect_timeout_ms(MY_WIFI_SSID, MY_WIFI_PASSWORD, CYW43_AUTH_WPA2_AES_PSK, 30000)) { + printf("failed to connect.\n"); + return 1; + } else { + printf("Connected.\n"); + } + + pcb = udp_new(); + ipaddr_aton(BEACON_TARGET, &addr); + return 0; +} + +void teleplot_udp_send_string(char * message){ + static int counter = 0; + + struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, BEACON_MSG_LEN_MAX+1, PBUF_RAM); + char *req = (char *)p->payload; + memset(req, 0, BEACON_MSG_LEN_MAX+1); + + snprintf(req, BEACON_MSG_LEN_MAX, "%s", message); + + err_t er = udp_sendto(pcb, p, &addr, UDP_PORT); + + pbuf_free(p); + if (er != ERR_OK) { + printf("Failed to send UDP packet! error=%d", er); + } else { + printf("Sent packet %d\n", counter); + counter++; + } + + // Note in practice for this simple UDP transmitter, + // the end result for both background and poll is the same + +#if PICO_CYW43_ARCH_POLL + // if you are using pico_cyw43_arch_poll, then you must poll periodically from your + // main loop (not from a timer) to check for Wi-Fi driver or lwIP work that needs to be done. + cyw43_arch_poll(); +#else + // if you are not using pico_cyw43_arch_poll, then WiFI driver and lwIP work + // is done via interrupt in the background. This sleep is just an example of some (blocking) + // work you might be doing. + // sleep_ms(BEACON_INTERVAL_MS); +#endif +} + + +void Teleplot_ajout_ou_envoie_tampon(char * message){ + // Si le tampon ne peut pas accueillir le prochain message + // On envoie et on vide le tampon + if(strlen(message) + strlen(teleplot_tampon) > BEACON_MSG_LEN_MAX){ + teleplot_udp_send_string(teleplot_tampon); + teleplot_tampon[0]='\0'; // On "vide" le tampon + } + // On ajoute le message au tampon + strcat(teleplot_tampon, message); + +} + +void Teleplot_add_variable_float_2decimal(char * nom_variable, float valeur){ + char tampon[100]; + sprintf(tampon, "%s:%lu:%.2f\n", nom_variable, (long)time_us_64()/1000, valeur); + Teleplot_ajout_ou_envoie_tampon(tampon); +} + +void Teleplot_add_variable_int(char * nom_variable, int valeur){ + char tampon[100]; + sprintf(tampon, "%s:%lu:%d\n", nom_variable, (long)time_us_64()/1000, valeur); + Teleplot_ajout_ou_envoie_tampon(tampon); +} \ No newline at end of file diff --git a/Teleplot.h b/Teleplot.h new file mode 100644 index 0000000..5348d93 --- /dev/null +++ b/Teleplot.h @@ -0,0 +1,5 @@ +int Teleplot_init(void); +void teleplot_udp_send_string(char * message); +void Teleplot_add_variable_float_2decimal(char * nom_variable, float valeur); +void Teleplot_add_variable_int(char * nom_variable, int valeur); + diff --git a/VL53L8_2024.c b/VL53L8_2024.c index cdde1f5..b6b0e67 100644 --- a/VL53L8_2024.c +++ b/VL53L8_2024.c @@ -145,8 +145,8 @@ void VL53L8_lecture(VL53L8CX_Configuration * Dev, VL53L8CX_ResultsData * Results printf("%d,", Results->distance_mm[col+ 8*row]); } } - printf("\n"); - */ + printf("\n");*/ + } int VL53L8_min_distance(VL53L8CX_ResultsData Results, float *distance){ diff --git a/lwipopts.h b/lwipopts.h new file mode 100644 index 0000000..8571ed5 --- /dev/null +++ b/lwipopts.h @@ -0,0 +1,10 @@ +#ifndef _LWIPOPTS_H +#define _LWIPOPTS_H + +// Generally you would define your own explicit list of lwIP options +// (see https://www.nongnu.org/lwip/2_1_x/group__lwip__opts.html) +// +// This example uses a common include to avoid repetition +#include "lwipopts_examples_common.h" + +#endif diff --git a/lwipopts_examples_common.h b/lwipopts_examples_common.h new file mode 100644 index 0000000..217cb13 --- /dev/null +++ b/lwipopts_examples_common.h @@ -0,0 +1,90 @@ +#ifndef _LWIPOPTS_EXAMPLE_COMMONH_H +#define _LWIPOPTS_EXAMPLE_COMMONH_H + + +// Common settings used in most of the pico_w examples +// (see https://www.nongnu.org/lwip/2_1_x/group__lwip__opts.html for details) + +// allow override in some examples +#ifndef NO_SYS +#define NO_SYS 1 +#endif +// allow override in some examples +#ifndef LWIP_SOCKET +#define LWIP_SOCKET 0 +#endif +#if PICO_CYW43_ARCH_POLL +#define MEM_LIBC_MALLOC 1 +#else +// MEM_LIBC_MALLOC is incompatible with non polling versions +#define MEM_LIBC_MALLOC 0 +#endif +#define MEM_ALIGNMENT 4 +#define MEM_SIZE 4000 +#define MEMP_NUM_TCP_SEG 32 +#define MEMP_NUM_ARP_QUEUE 10 +#define PBUF_POOL_SIZE 24 +#define LWIP_ARP 1 +#define LWIP_ETHERNET 1 +#define LWIP_ICMP 1 +#define LWIP_RAW 1 +#define TCP_WND (8 * TCP_MSS) +#define TCP_MSS 1460 +#define TCP_SND_BUF (8 * TCP_MSS) +#define TCP_SND_QUEUELEN ((4 * (TCP_SND_BUF) + (TCP_MSS - 1)) / (TCP_MSS)) +#define LWIP_NETIF_STATUS_CALLBACK 1 +#define LWIP_NETIF_LINK_CALLBACK 1 +#define LWIP_NETIF_HOSTNAME 1 +#define LWIP_NETCONN 0 +#define MEM_STATS 0 +#define SYS_STATS 0 +#define MEMP_STATS 0 +#define LINK_STATS 0 +// #define ETH_PAD_SIZE 2 +#define LWIP_CHKSUM_ALGORITHM 3 +#define LWIP_DHCP 1 +#define LWIP_IPV4 1 +#define LWIP_TCP 1 +#define LWIP_UDP 1 +#define LWIP_DNS 1 +#define LWIP_TCP_KEEPALIVE 1 +#define LWIP_NETIF_TX_SINGLE_PBUF 1 +#define DHCP_DOES_ARP_CHECK 0 +#define LWIP_DHCP_DOES_ACD_CHECK 0 + +#ifndef NDEBUG +#define LWIP_DEBUG 1 +#define LWIP_STATS 1 +#define LWIP_STATS_DISPLAY 1 +#endif + +#define ETHARP_DEBUG LWIP_DBG_OFF +#define NETIF_DEBUG LWIP_DBG_OFF +#define PBUF_DEBUG LWIP_DBG_OFF +#define API_LIB_DEBUG LWIP_DBG_OFF +#define API_MSG_DEBUG LWIP_DBG_OFF +#define SOCKETS_DEBUG LWIP_DBG_OFF +#define ICMP_DEBUG LWIP_DBG_OFF +#define INET_DEBUG LWIP_DBG_OFF +#define IP_DEBUG LWIP_DBG_OFF +#define IP_REASS_DEBUG LWIP_DBG_OFF +#define RAW_DEBUG LWIP_DBG_OFF +#define MEM_DEBUG LWIP_DBG_OFF +#define MEMP_DEBUG LWIP_DBG_OFF +#define SYS_DEBUG LWIP_DBG_OFF +#define TCP_DEBUG LWIP_DBG_OFF +#define TCP_INPUT_DEBUG LWIP_DBG_OFF +#define TCP_OUTPUT_DEBUG LWIP_DBG_OFF +#define TCP_RTO_DEBUG LWIP_DBG_OFF +#define TCP_CWND_DEBUG LWIP_DBG_OFF +#define TCP_WND_DEBUG LWIP_DBG_OFF +#define TCP_FR_DEBUG LWIP_DBG_OFF +#define TCP_QLEN_DEBUG LWIP_DBG_OFF +#define TCP_RST_DEBUG LWIP_DBG_OFF +#define UDP_DEBUG LWIP_DBG_OFF +#define TCPIP_DEBUG LWIP_DBG_OFF +#define PPP_DEBUG LWIP_DBG_OFF +#define SLIP_DEBUG LWIP_DBG_OFF +#define DHCP_DEBUG LWIP_DBG_OFF + +#endif /* __LWIPOPTS_H__ */ diff --git a/main.c b/main.c index cedc1c1..409b198 100644 --- a/main.c +++ b/main.c @@ -13,6 +13,7 @@ #include "i2c_maitre.h" #include "Localisation.h" #include "Moteurs.h" +#include "Teleplot.h" #include "Temps.h" #include "Trajectoire.h" #include "Trajet.h" @@ -66,6 +67,7 @@ void main(void) identifiant_init(); Localisation_init(identifiant_lire()); Trajet_init(identifiant_lire()); + Teleplot_init(); i2c_maitre_init(); @@ -83,6 +85,7 @@ void main(void) //multicore_launch_core1(gestion_affichage); multicore_launch_core1(gestion_VL53L8CX); + sleep_ms(5000); printf("Demarrage...\n"); @@ -170,8 +173,10 @@ void affichage(void){ printf(">m1_c:%f\n>m2_c:%f\n", AsserMoteur_getConsigne_mm_s(MOTEUR_A), AsserMoteur_getConsigne_mm_s(MOTEUR_B) );*/ printf(">pos_x:%.1f\n>pos_y:%.1f\n>pos_angle:%.1f\n", Localisation_get().x_mm, Localisation_get().y_mm, Localisation_get().angle_radian); printf(">distance_obstacle:%f\n",Trajet_get_obstacle_mm()); + Teleplot_add_variable_float_2decimal("dist", Trajet_get_obstacle_mm()); printf(">abscisse:%f\n",abscisse); + Teleplot_add_variable_float_2decimal("abs", abscisse); struct position_t position_actuelle; position_actuelle = Localisation_get(); diff --git a/wifi_settings.h.default b/wifi_settings.h.default new file mode 100644 index 0000000..14ea373 --- /dev/null +++ b/wifi_settings.h.default @@ -0,0 +1,9 @@ +// Copier ce fichier en "wifi_settings.h" et renseignez vos identifiants WiFi. +// Le port est le port par défaut de Teleplot +// BEACON_TARGET est l'IP ou l'adresse du serveur Teleplot + +#define MY_WIFI_SSID "My_SSID" +#define MY_WIFI_PASSWORD "My_WiFi_Password" + +#define UDP_PORT 47269 +#define BEACON_TARGET "192.168.1.2" \ No newline at end of file