From 77eae511b2aa4afd9b59897566d1cc7509af31fc Mon Sep 17 00:00:00 2001 From: Ulysse Cura Date: Wed, 4 Feb 2026 22:19:30 +0100 Subject: [PATCH] Updated pico files and udp communication --- program/main_controller/.vscode/tasks.json | 4 +- program/main_controller/CMakeLists.txt | 2 - program/main_controller/pico_sdk_import.cmake | 63 ++++++++++++++++++- program/main_controller/src/robot.c | 11 ++-- .../src/wifi/headers/lwipopts.h | 17 ++--- .../src/wifi/headers/udp_payload.h | 11 ---- .../src/wifi/headers/wifi_operator.h | 10 +-- program/main_controller/src/wifi/udp_client.c | 10 ++- .../main_controller/src/wifi/udp_payload.c | 26 -------- .../main_controller/src/wifi/wifi_operator.c | 18 +++--- 10 files changed, 97 insertions(+), 75 deletions(-) delete mode 100644 program/main_controller/src/wifi/headers/udp_payload.h delete mode 100644 program/main_controller/src/wifi/udp_payload.c diff --git a/program/main_controller/.vscode/tasks.json b/program/main_controller/.vscode/tasks.json index e70a949..f46abdd 100644 --- a/program/main_controller/.vscode/tasks.json +++ b/program/main_controller/.vscode/tasks.json @@ -5,7 +5,7 @@ "type": "shell", "group": "build", - "command": "mkdir -p build && cd build && cmake ../ && make", + "command": "mkdir -p build && cd build && cmake ../ && make -j $(nproc)", "presentation": { "echo": true, @@ -21,7 +21,7 @@ "type": "shell", "group": "build", - "command": "mkdir -p build && cd build && cmake ../ && make Flash", + "command": "mkdir -p build && cd build && cmake ../ && make -j $(nproc) Flash", "presentation": { "echo": true, diff --git a/program/main_controller/CMakeLists.txt b/program/main_controller/CMakeLists.txt index c7782a1..077f920 100644 --- a/program/main_controller/CMakeLists.txt +++ b/program/main_controller/CMakeLists.txt @@ -24,7 +24,6 @@ add_executable(main_controller src/i2c/mcp23017.c src/wifi/wifi_operator.c src/wifi/udp_client.c - src/wifi/udp_payload.c ) target_include_directories(main_controller PRIVATE @@ -36,7 +35,6 @@ target_link_libraries(main_controller pico_stdlib hardware_i2c hardware_pwm - hardware_uart pico_cyw43_arch_lwip_poll ) diff --git a/program/main_controller/pico_sdk_import.cmake b/program/main_controller/pico_sdk_import.cmake index 28efe9e..d493cc2 100644 --- a/program/main_controller/pico_sdk_import.cmake +++ b/program/main_controller/pico_sdk_import.cmake @@ -3,6 +3,28 @@ # This can be dropped into an external project to help locate this SDK # It should be include()ed prior to project() +# Copyright 2020 (c) 2020 Raspberry Pi (Trading) Ltd. +# +# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the +# following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following +# disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following +# disclaimer in the documentation and/or other materials provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, +# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH)) set(PICO_SDK_PATH $ENV{PICO_SDK_PATH}) message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')") @@ -18,9 +40,20 @@ if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_P message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')") endif () +if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_TAG} AND (NOT PICO_SDK_FETCH_FROM_GIT_TAG)) + set(PICO_SDK_FETCH_FROM_GIT_TAG $ENV{PICO_SDK_FETCH_FROM_GIT_TAG}) + message("Using PICO_SDK_FETCH_FROM_GIT_TAG from environment ('${PICO_SDK_FETCH_FROM_GIT_TAG}')") +endif () + +if (PICO_SDK_FETCH_FROM_GIT AND NOT PICO_SDK_FETCH_FROM_GIT_TAG) + set(PICO_SDK_FETCH_FROM_GIT_TAG "master") + message("Using master as default value for PICO_SDK_FETCH_FROM_GIT_TAG") +endif() + set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK") set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of SDK from git if not otherwise locatable") set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK") +set(PICO_SDK_FETCH_FROM_GIT_TAG "${PICO_SDK_FETCH_FROM_GIT_TAG}" CACHE FILEPATH "release tag for SDK") if (NOT PICO_SDK_PATH) if (PICO_SDK_FETCH_FROM_GIT) @@ -32,11 +65,37 @@ if (NOT PICO_SDK_PATH) FetchContent_Declare( pico_sdk GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk - GIT_TAG master + GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG} ) + if (NOT pico_sdk) message("Downloading Raspberry Pi Pico SDK") - FetchContent_Populate(pico_sdk) + # GIT_SUBMODULES_RECURSE was added in 3.17 + if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.17.0") + FetchContent_Populate( + pico_sdk + QUIET + GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk + GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG} + GIT_SUBMODULES_RECURSE FALSE + + SOURCE_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-src + BINARY_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-build + SUBBUILD_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-subbuild + ) + else () + FetchContent_Populate( + pico_sdk + QUIET + GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk + GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG} + + SOURCE_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-src + BINARY_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-build + SUBBUILD_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-subbuild + ) + endif () + set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR}) endif () set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE}) diff --git a/program/main_controller/src/robot.c b/program/main_controller/src/robot.c index e16fcc8..6d829d9 100644 --- a/program/main_controller/src/robot.c +++ b/program/main_controller/src/robot.c @@ -14,22 +14,23 @@ void robot_init(void) stdio_init_all(); - if(cyw43_arch_init()) + if(cyw43_arch_init_with_country(CYW43_COUNTRY_FRANCE)) robot.is_running = false; cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, true); - i2c_master_init(); + //i2c_master_init(); - mcp23017_init(); + //mcp23017_init(); - gyro_init(); + //gyro_init(); //gyro_calibrate(); //motion_control_init(); - wifi_operator_init(); + if(wifi_operator_init()) + robot.is_running = false; if(udp_client_init()) robot.is_running = false; diff --git a/program/main_controller/src/wifi/headers/lwipopts.h b/program/main_controller/src/wifi/headers/lwipopts.h index 91c3b05..3cc083c 100644 --- a/program/main_controller/src/wifi/headers/lwipopts.h +++ b/program/main_controller/src/wifi/headers/lwipopts.h @@ -21,18 +21,18 @@ #endif #define MEM_ALIGNMENT 4 #ifndef MEM_SIZE -#define MEM_SIZE 32768 // Augmenté pour plus de mémoire disponible +#define MEM_SIZE 4000 #endif #define MEMP_NUM_TCP_SEG 32 #define MEMP_NUM_ARP_QUEUE 10 -#define PBUF_POOL_SIZE 32 // Augmenté pour réduire les allocations +#define PBUF_POOL_SIZE 24 #define LWIP_ARP 1 #define LWIP_ETHERNET 1 #define LWIP_ICMP 1 #define LWIP_RAW 1 -#define TCP_WND (16 * TCP_MSS) // Augmenté pour de meilleures performances +#define TCP_WND (8 * TCP_MSS) #define TCP_MSS 1460 -#define TCP_SND_BUF (8 * TCP_MSS) // Augmenté pour de meilleures performances +#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 @@ -44,8 +44,7 @@ #define LINK_STATS 0 // #define ETH_PAD_SIZE 2 #define LWIP_CHKSUM_ALGORITHM 3 -#define LWIP_DHCP 0 -#define LWIP_DHCP_SERVER 0 +#define LWIP_DHCP 1 #define LWIP_IPV4 1 #define LWIP_TCP 1 #define LWIP_UDP 1 @@ -90,8 +89,4 @@ #define SLIP_DEBUG LWIP_DBG_OFF #define DHCP_DEBUG LWIP_DBG_OFF -#define SYS_LIGHTWEIGHT_PROT 1 // Protection pour le multicore -#define MEMP_NUM_PBUF 32 // Augmenté pour les buffers -#define ICMP_TTL 255 // Augmenté pour la fiabilité - -#endif /* __LWIPOPTS_H__ */ \ No newline at end of file +#endif /* __LWIPOPTS_H__ */ diff --git a/program/main_controller/src/wifi/headers/udp_payload.h b/program/main_controller/src/wifi/headers/udp_payload.h deleted file mode 100644 index 5bc1b4b..0000000 --- a/program/main_controller/src/wifi/headers/udp_payload.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef UDP_BUFFER_H -#define UDP_BUFFER_H - -#include -#include -#include - -// Callback function for writing data when udp package received -void __not_in_flash_func(udp_client_message_handler)(uint8_t *payload, uint16_t len, const ip_addr_t *addr, u16_t port); - -#endif // UDP_BUFFER_H \ No newline at end of file diff --git a/program/main_controller/src/wifi/headers/wifi_operator.h b/program/main_controller/src/wifi/headers/wifi_operator.h index 3b49785..89fcc3c 100644 --- a/program/main_controller/src/wifi/headers/wifi_operator.h +++ b/program/main_controller/src/wifi/headers/wifi_operator.h @@ -1,11 +1,11 @@ #ifndef WIFI_OPERATOR_H #define WIFI_OPERATOR_H -//#define WIFI_OPERATOR_SSID "RiombotiqueAP" -//#define WIFI_OPERATOR_PASSWORD "x4ptSLpPuJFcpzbLEhDoZ5J7dz" -#define WIFI_OPERATOR_SSID "thinkpad-T440p" -#define WIFI_OPERATOR_PASSWORD "CDuKaka2000!" +#define WIFI_OPERATOR_SSID "RiombotiqueAP" +#define WIFI_OPERATOR_PASSWORD "x4ptSLpPuJFcpzbLEhDoZ5J7dz" +//#define WIFI_OPERATOR_SSID "thinkpad-T440p" +//#define WIFI_OPERATOR_PASSWORD "CDuKaka2000!" -void wifi_operator_init(void); +int wifi_operator_init(void); #endif // WIFI_OPERATOR_H \ No newline at end of file diff --git a/program/main_controller/src/wifi/udp_client.c b/program/main_controller/src/wifi/udp_client.c index 7cf2527..f52fbf1 100644 --- a/program/main_controller/src/wifi/udp_client.c +++ b/program/main_controller/src/wifi/udp_client.c @@ -1,14 +1,18 @@ #include "headers/udp_client.h" #include -#include "headers/udp_payload.h" #include "headers/robot.h" +void __not_in_flash_func(message_callback)(uint8_t *payload, uint16_t len, const ip_addr_t *addr) +{ + // RECEIVE ALL CONTROLLER DATA +} + // Default callback func -static void __not_in_flash_func(default_message_callback)(uint8_t *payload, uint16_t len) +static inline void default_message_callback(uint8_t *payload, uint16_t len) { for(uint i = 0; i < len; i++) - printf("payload[%d]=%d | ", i, payload[i]); + printf("payload[%d]=%d%s", i, payload[i], ((i + 1) == len) ? "" : " | "); puts("\n"); } diff --git a/program/main_controller/src/wifi/udp_payload.c b/program/main_controller/src/wifi/udp_payload.c deleted file mode 100644 index 3620871..0000000 --- a/program/main_controller/src/wifi/udp_payload.c +++ /dev/null @@ -1,26 +0,0 @@ -#include "headers/udp_payload.h" - -#include "i2c/headers/motors.h" -#include "headers/robot.h" - -#define UDP_PAYLOAD_ANGLE_L_BYTE 0x00 -#define UDP_PAYLOAD_ANGLE_H_BYTE 0x01 -#define UDP_PAYLOAD_X_AXIS_SPEED_BYTE 0x02 -#define UDP_PAYLOAD_Y_AXIS_SPEED_BYTE 0x03 - -void __not_in_flash_func(udp_client_message_handler)(uint8_t *payload, uint16_t len, const ip_addr_t *addr, u16_t port) -{ - if(len != 16) return; - - robot.motion_control_data.angle = ((payload[UDP_PAYLOAD_ANGLE_H_BYTE] << 8) | payload[UDP_PAYLOAD_ANGLE_L_BYTE]); - - robot.motion_control_data.x_axis_speed = payload[UDP_PAYLOAD_X_AXIS_SPEED_BYTE]; - robot.motion_control_data.y_axis_speed = payload[UDP_PAYLOAD_Y_AXIS_SPEED_BYTE]; -/* - for(servo_motors_enum_t actual_servo_motor = SERVO_MOTOR1; actual_servo_motor < NB_SERVO_MOTORS; actual_servo_motor++) - { - const servo_motor_def_t *SERVO_MOTOR_DEF = &SERVO_MOTORS_DEFS[actual_servo_motor]; - - robot.motion_control_data.servo_motors_pos[actual_servo_motor] = payload[SERVO_MOTOR_DEF->buffer_reg_and_payload_byte]; - }*/ -} diff --git a/program/main_controller/src/wifi/wifi_operator.c b/program/main_controller/src/wifi/wifi_operator.c index d8c74dd..185cfd2 100644 --- a/program/main_controller/src/wifi/wifi_operator.c +++ b/program/main_controller/src/wifi/wifi_operator.c @@ -5,18 +5,20 @@ #include #include -void wifi_operator_init(void) +int wifi_operator_init(void) { - // Mode client + if(cyw43_wifi_pm(&cyw43_state, CYW43_NO_POWERSAVE_MODE)) + { + puts("CYW43 no powersave mode failed"); + return -1; + } + + // Station mode cyw43_arch_enable_sta_mode(); - // Désactiver le mode d'économie d'énergie - cyw43_wifi_pm(&cyw43_state, CYW43_NO_POWERSAVE_MODE); - - // Tentativs de connexion for(int error_code = 1; error_code; ) { - puts("WiFi connection attempt"); + printf("WiFi connection attempt to \"%s\"\n", WIFI_OPERATOR_SSID); error_code = cyw43_arch_wifi_connect_timeout_ms(WIFI_OPERATOR_SSID, WIFI_OPERATOR_PASSWORD, CYW43_AUTH_WPA2_AES_PSK, 10000); @@ -43,5 +45,5 @@ void wifi_operator_init(void) } } - puts("Connexion successfully etablished !"); + puts("STA mode activated"); }