WiFi AP mode connecting

This commit is contained in:
Ulysse Cura 2026-02-04 22:19:54 +01:00
parent 264bb02e98
commit 16799fd960
10 changed files with 137 additions and 2767 deletions

File diff suppressed because it is too large Load Diff

View File

@ -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})

View File

@ -1,48 +1,33 @@
#include "headers/robot.h"
#include "headers/controller.h"
#include <pico/stdlib.h>
#include <pico/cyw43_arch.h>
#include <time.h>
#include <pico/mutex.h>
#include "i2c/headers/i2c_master.h"
#include "i2c/headers/mcp23017.h"
#include "wifi/headers/udp_client.h"
#include "wifi/headers/udp_server.h"
#include "wifi/headers/wifi_operator.h"
auto_init_mutex(wifi_mutex);
void robot_init(void)
void controller_init(void)
{
robot.is_running = true;
controller.is_running = true;
stdio_init_all();
if(cyw43_arch_init())
robot.is_running = false;
if(cyw43_arch_init_with_country(CYW43_COUNTRY_FRANCE))
controller.is_running = false;
mutex_enter_blocking(&wifi_mutex);
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, true);
mutex_exit(&wifi_mutex);
//i2c_master_init();
if(init_wifi_operator())
controller.is_running = false;
//mcp23017_init();
//gyro_init();
//gyro_calibrate();
//init_motion_control();
init_wifi_operator();
udp_client_init();
if(udp_server_init())
controller.is_running = false;
// Initialisation ended
for(uint i = 0, led_state = true; i < 5; i++)
{
mutex_enter_blocking(&wifi_mutex);
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, led_state);
mutex_exit(&wifi_mutex);
sleep_ms(100);
@ -54,46 +39,33 @@ static inline void update_time(void)
{
static float last_time = 0.0;
float start_time = (float)clock() * 1000.0f / (float)CLOCKS_PER_SEC;
robot.delta_time_ms = start_time - last_time;
controller.delta_time_ms = start_time - last_time;
last_time = start_time;
static float elapsed_time = 0.0f;
elapsed_time += robot.delta_time_ms;
elapsed_time += controller.delta_time_ms;
static bool led_state = false;
if(elapsed_time >= 1000.0f)
{
elapsed_time = 0.0f;
mutex_enter_blocking(&wifi_mutex);
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, led_state);
mutex_exit(&wifi_mutex);
led_state = !led_state;
}
}
void robot_handle_inputs_outputs(void)
void controller_handle_inputs_outputs(void)
{
mutex_enter_blocking(&wifi_mutex);
cyw43_arch_poll();
mutex_exit(&wifi_mutex);
update_time();
//gyro_update();
//i2c_update_motion_control();
//i2c_update_servo_motors();
//mcp23017_update();
tight_loop_contents();
}
void robot_deinit(void)
void controller_deinit(void)
{
udp_client_deinit();
//i2c_master_deinit();
udp_server_deinit();
}

View File

@ -3,18 +3,18 @@
#include <stdbool.h>
typedef struct robot_t {
typedef struct controller_t {
bool is_running;
float delta_time_ms;
} robot_t;
} controller_t;
extern robot_t robot;
extern controller_t controller;
// Init all robot's components
void robot_init(void);
// Init all controller's components
void controller_init(void);
// Handle inputs and outputs
void robot_handle_inputs_outputs(void);
// Deinit all robot's components
void robot_deinit(void);
void controller_handle_inputs_outputs(void);
// Deinit all controller's components
void controller_deinit(void);
#endif // ROBOT_H

View File

@ -8,20 +8,20 @@
* Ce Pico est un maitre pilotant le gyroscope, l'internet et le motion controller.*
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "headers/robot.h"
#include "headers/controller.h"
robot_t robot;
controller_t controller;
int main(void)
{
robot_init();
controller_init();
while(robot.is_running)
while(controller.is_running)
{
robot_handle_inputs_outputs();
controller_handle_inputs_outputs();
}
robot_deinit();
controller_deinit();
return 0;
}

View File

@ -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__ */
#endif /* __LWIPOPTS_H__ */

View File

@ -4,25 +4,17 @@
#include <stdint.h>
#include <lwip/udp.h>
#define UDP_CLIENT_PORT 4243
#define UDP_SERVER_PORT 4242
#define BUFFER_SIZE 1024
// Message callback deffinition
typedef void (*message_callback_t)(uint8_t *payload, uint16_t len, const ip_addr_t *addr, uint16_t port);
// Data in here is used by the SDK
typedef struct udp_client_t {
struct udp_pcb *pcb; // like this
ip_addr_t local_addr; // or this...
uint16_t local_port; // So don't remove them, even if they are not used explicitely in the program
uint8_t recv_buffer[BUFFER_SIZE]; // Please (Do not even change their position)
message_callback_t message_callback;
} udp_client_t;
typedef struct udp_server_t {
struct udp_pcb *pcb;
} udp_server_t;
// Init udp client, set callback to NULL for the default callback
void udp_client_init(void);
int udp_server_init(void);
// Send all data to robot
void udp_server_send(void);
// Exit udp client
void udp_client_deinit(void);
void udp_server_deinit(void);
#endif // UDP_CLIENT_H

View File

@ -1,11 +1,10 @@
#ifndef WIFI_OPERATOR_H
#define WIFI_OPERATOR_H
//#define WIFI_OPERATOR_SSID "RiombotiqueAP"
//#define WIFI_OPERATOR_PASSWORD "x4ptSLpPuJFcpzbLEhDoZ5J7dz"
#define WIFI_OPERATOR_SSID "thinkpad"
#define WIFI_OPERATOR_PASSWORD "CDuKaka2000!"
#define WIFI_OPERATOR_SSID "RiombotiqueAP"
#define WIFI_OPERATOR_PASSWORD "x4ptSLpPuJFcpzbLEhDoZ5J7dz"
#define WIFI_OPERATOR_CHANNEL 3
void init_wifi_operator(void);
int init_wifi_operator(void);
#endif // WIFI_OPERATOR_H

View File

@ -2,69 +2,38 @@
#include <stdio.h>
udp_client_t udp_client;
udp_server_t udp_server;
static inline void handle_receive(struct pbuf *p, const ip_addr_t *addr, u16_t port)
int udp_server_init(void)
{
if(p->len >= 2)
udp_server.pcb = udp_new();
if(udp_server.pcb == NULL)
{
uint8_t *payload = (uint8_t *)p->payload;
uint16_t len = p->len;
udp_client.message_callback(payload, len, addr, port);
puts("Error creating UDP server");
return -1;
}
pbuf_free(p);
}
static void __not_in_flash_func(udp_receive_callback)(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
{
udp_client_t *udp_client_received_data = (udp_client_t *)arg;
handle_receive(p, addr, port);
}
// Default callback func
static void __not_in_flash_func(default_message_callback)(uint8_t *payload, uint16_t len, const ip_addr_t *addr, uint16_t port)
{
printf("Received: len=%d from %s:%d\n", len, ipaddr_ntoa(addr), port);
for(uint i = 0; i < len; i++) printf("payload[%d]=%d | ", i, payload[i]);
puts("\n");
//printf(">Robot angle : %d\n", (int16_t)((payload[UDP_PAYLOAD_ANGLE_H_BYTE] << 8) | payload[UDP_PAYLOAD_ANGLE_L_BYTE]));
//printf(">Robot x speed : %d\n", (int8_t)payload[UDP_PAYLOAD_X_AXIS_SPEED_BYTE]);
//printf(">Robot y speed : %d\n", (int8_t)payload[UDP_PAYLOAD_Y_AXIS_SPEED_BYTE]);
}
void udp_client_init(void)
{
//udp_client.message_callback = udp_client_message_handler;
udp_client.message_callback = default_message_callback;
udp_client.pcb = udp_new();
if(udp_client.pcb == NULL)
if(udp_bind(udp_server.pcb, IP_ADDR_ANY, UDP_SERVER_PORT))
{
puts("Error creating UDP client");
return;
printf("Error bind UDP server");
return -1;
}
udp_recv(udp_client.pcb, udp_receive_callback, &udp_client);
puts("UDP client started");
err_t err = udp_bind(udp_client.pcb, IP_ADDR_ANY, UDP_CLIENT_PORT);
if(err != ERR_OK)
{
printf("Error bind UDP client: %d\n", err);
return;
}
printf("UDP client started on port %d\n", UDP_CLIENT_PORT);
return 0;
}
void udp_client_deinit(void)
void udp_server_send(void)
{
if(udp_client.pcb)
}
void udp_server_deinit(void)
{
if(udp_server.pcb)
{
udp_remove(udp_client.pcb);
udp_client.pcb = NULL;
udp_remove(udp_server.pcb);
udp_server.pcb = NULL;
}
}

View File

@ -5,70 +5,19 @@
#include <lwip/netif.h>
#include <lwip/ip4_addr.h>
void init_wifi_operator(void)
int init_wifi_operator(void)
{
// Mode client
cyw43_arch_enable_sta_mode();
// Désactiver le mode d'économie d'énergie
cyw43_wifi_pm(&cyw43_state, CYW43_NO_POWERSAVE_MODE);
// Configuration IP
ip4_addr_t ip, netmask, gateway;
IP4_ADDR(&ip, 192, 168, 128, 2);
IP4_ADDR(&netmask, 255, 255, 255, 0);
IP4_ADDR(&gateway, 192, 168, 128, 1);
// Configuration réseau avant connexion
netif_set_up(netif_default);
netif_set_addr(netif_default, &ip, &netmask, &gateway);
puts("IP config done");
sleep_ms(300); // Wait for wifi to be initialized
// Tentativs de connexion
for(int error_code; !error_code;)
if(cyw43_wifi_pm(&cyw43_state, CYW43_NO_POWERSAVE_MODE))
{
// Afficher les paramètres de connexion
printf("Trying to connect 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);
if(error_code)
{
const char *error_description;
switch(error_code)
{
case -1:
error_description = "Error Generic";
break;
case -2:
error_description = "Acces point not found";
break;
case -3:
error_description = "Incorrect password";
break;
default:
error_description = "Unknow error";
}
printf("Error: WiFi can't be connected - Error code: %d - %s\n", error_code, error_description);
}
puts("CYW43 no powersave mode failed");
return -1;
}
// Configuration de l'interface réseau
if(netif_default == NULL)
{
puts("Error: WiFi interface isn't accessible");
return;
}
cyw43_arch_enable_ap_mode(WIFI_OPERATOR_SSID, WIFI_OPERATOR_PASSWORD, CYW43_AUTH_WPA2_AES_PSK);
netif_set_up(netif_default);
netif_set_link_up(netif_default);
netif_set_addr(netif_default, &ip, &netmask, &gateway);
cyw43_wifi_ap_set_channel(&cyw43_state, WIFI_OPERATOR_CHANNEL);
puts("Connexion successfully etablished !");
puts("AP mode enabled");
return 0;
}