Initial commit

This commit is contained in:
Ulysse Cura 2026-01-31 19:52:19 +01:00
commit 264bb02e98
16 changed files with 3278 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
program/build

27
program/.vscode/c_cpp_properties.json vendored Normal file
View File

@ -0,0 +1,27 @@
{
"configurations": [
{
"name": "Linux",
"intelliSenseMode": "linux-gcc-arm",
"includePath": [
"${env:PICO_SDK_PATH}/src/**/include/",
"${env:PICO_SDK_PATH}/lib/**/include/",
"${env:PICO_SDK_PATH}/lib/**/src/",
"${workspaceFolder}/build/generated/pico_base/",
"${workspaceFolder}/src/wifi/headers/",
"${workspaceFolder}/src/"
],
"compilerPath": "/usr/bin/arm-none-eabi-gcc",
"cStandard": "c11",
"browse": {
"path": [
"${workspaceFolder}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
},
"configurationProvider": "ms-vscode.cmake-tools"
}
],
"version": 4
}

19
program/.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,19 @@
{
"files.associations": {
"*.md": "markdown",
"binary_info.h": "c",
"i2c.h": "c",
"stdlib.h": "c",
"stdint.h": "c",
"gyro.h": "c",
"motors.h": "c",
"motion_control.h": "c",
"i2c_master.h": "c",
"udp_client.h": "c",
"udp_payload.h": "c",
"udp.h": "c",
"opt.h": "c",
"cyw43_arch.h": "c",
"wifi_operator.h": "c"
}
}

37
program/.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,37 @@
{
"tasks": [
{
"label": "CMake & Make",
"type": "shell",
"group": "build",
"command": "mkdir -p build && cd build && cmake ../ && make",
"presentation": {
"echo": true,
"reveal": "always",
"focus": true,
"panel": "shared",
"showReuseMessage": true,
"clear": true
}
},
{
"label": "CMake & Make & Flash",
"type": "shell",
"group": "build",
"command": "mkdir -p build && cd build && cmake ../ && make Flash",
"presentation": {
"echo": true,
"reveal": "always",
"focus": true,
"panel": "shared",
"showReuseMessage": true,
"clear": true
}
}
],
"version": "2.0.0"
}

46
program/CMakeLists.txt Normal file
View File

@ -0,0 +1,46 @@
cmake_minimum_required(VERSION 3.13)
# Définir explicitement la carte comme Pico W
set(PICO_BOARD pico_w)
if(NOT DEFINED PICO_BOARD)
add_definitions(-DPICO_BOARD=${PICO_BOARD})
endif()
include(pico_sdk_import.cmake)
project(controller C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
pico_sdk_init()
add_executable(controller
src/main.c
src/controller.c
src/wifi/wifi_operator.c
src/wifi/udp_server.c
)
target_include_directories(controller PRIVATE
${CMAKE_CURRENT_LIST_DIR}/src/wifi/headers/
${CMAKE_CURRENT_LIST_DIR}/src/
)
target_link_libraries(controller
pico_stdlib
hardware_i2c
hardware_pwm
hardware_uart
pico_cyw43_arch_lwip_poll
)
pico_enable_stdio_usb(controller 1)
pico_enable_stdio_uart(controller 1)
pico_add_extra_outputs(controller)
add_custom_target(Flash
DEPENDS controller
COMMAND sudo picotool load -f ${PROJECT_BINARY_DIR}/controller.uf2
)

2565
program/Makefile Normal file

File diff suppressed because it is too large Load Diff

95
program/Readme.md Normal file
View File

@ -0,0 +1,95 @@
Motion controler code for the RPI Pico (RP2040)
===============================================
This project is the main controller firmware for the RPI Pico (RP2040), designed for the Eurobot 2025 Cup.
I2C description
-----------------------------------------------
The robots I2C communication works as follows:
* Send the device address + R/W bit (to select read or write mode).
* Send the target register address (to read from or write to).
* Read or write the register data. Multiple registers can be read/written sequentially, with the address auto-incrementing after each operation.
This code is designed to be the master in the i2c communication.
|Register |R/W|Description |Encoding |
|---------|:-:|-------------------------------|:-----------------:|
| 0x00 | W | Speed motor 1 |**-128** - **127** |
| 0x01 | W | Speed motor 2 |**-128** - **127** |
| 0x02 | W | Speed motor 3 |**-128** - **127** |
| 0x03 | W | Speed motor 4 |**-128** - **127** |
| 0x04 | W | Servo 1 position selection | **0** - **1** |
| 0x05 | W | Servo 2 position selection | **0** - **1** |
| 0x06 | W | Servo 3 position selection | **0** - **1** |
| 0x07 | W | Servo 4 position selection | **0** - **1** |
Motors communication description
-----------------------------------------------
Motors are «connected» to their respective I2C buffer address.
To control a motor you need to write data to its adress of the form :
>```C
>int8_t speed;
>```
Value goes from **-128** to **127**.
Servo motors communication description
-----------------------------------------------
Servo motors are «connected» to their respective I2C buffer address.
To control a servo motor you need to write data to its adress of the form :
>```C
>uint8_t close;
>```
Value is 0 or 1 for the open or the close pos.
Internet communication description
-----------------------------------------------
The robot main_controller is a client connected to the wireless controller which is an udp server host. A buffer is used to store data received from host.
Speed on X and Y axis are not depending of the robot orientation.
Servo motors keep the same byte address in i2c buffer and udp payload.
|Byte |Description |Encoding |
|---------|-------------------------------------------------|:-----------------:|
| 0x00-01 | Robot angle (0x00 is the last significant byte) |**-180** - **180** |
| 0x02 | Speed x axis |**-128** - **127** |
| 0x03 | Speed y axis |**-128** - **127** |
| 0x04 | Servo 1 position selection | **0** - **1** |
| 0x05 | Servo 2 position selection | **0** - **1** |
| 0x06 | Servo 3 position selection | **0** - **1** |
| 0x07 | Servo 4 position selection | **0** - **1** |
Pinout description
-----------------------------------------------
|Pin |Description |GPIO Type |
|----|----------------------------------|-----------|
| 4 | I2C Bus SDA | I2C |
| 5 | I2C Bus SCL | I2C |
Motors placement
-----------------------------------------------
,-~***~-,
/1 2\
| |
| |
| |
\3 4/
`-.....-'

View File

@ -0,0 +1,62 @@
# This is a copy of <PICO_SDK_PATH>/external/pico_sdk_import.cmake
# This can be dropped into an external project to help locate this SDK
# It should be include()ed prior to project()
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}')")
endif ()
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT))
set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT})
message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')")
endif ()
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH))
set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH})
message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')")
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")
if (NOT PICO_SDK_PATH)
if (PICO_SDK_FETCH_FROM_GIT)
include(FetchContent)
set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR})
if (PICO_SDK_FETCH_FROM_GIT_PATH)
get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}")
endif ()
FetchContent_Declare(
pico_sdk
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
GIT_TAG master
)
if (NOT pico_sdk)
message("Downloading Raspberry Pi Pico SDK")
FetchContent_Populate(pico_sdk)
set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR})
endif ()
set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE})
else ()
message(FATAL_ERROR
"SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git."
)
endif ()
endif ()
get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
if (NOT EXISTS ${PICO_SDK_PATH})
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found")
endif ()
set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake)
if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE})
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK")
endif ()
set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE)
include(${PICO_SDK_INIT_CMAKE_FILE})

99
program/src/controller.c Normal file
View File

@ -0,0 +1,99 @@
#include "headers/robot.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/wifi_operator.h"
auto_init_mutex(wifi_mutex);
void robot_init(void)
{
robot.is_running = true;
stdio_init_all();
if(cyw43_arch_init())
robot.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();
//mcp23017_init();
//gyro_init();
//gyro_calibrate();
//init_motion_control();
init_wifi_operator();
udp_client_init();
// 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);
led_state = !led_state;
}
}
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;
last_time = start_time;
static float elapsed_time = 0.0f;
elapsed_time += robot.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)
{
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)
{
udp_client_deinit();
//i2c_master_deinit();
}

View File

@ -0,0 +1,20 @@
#ifndef ROBOT_H
#define ROBOT_H
#include <stdbool.h>
typedef struct robot_t {
bool is_running;
float delta_time_ms;
} robot_t;
extern robot_t robot;
// Init all robot's components
void robot_init(void);
// Handle inputs and outputs
void robot_handle_inputs_outputs(void);
// Deinit all robot's components
void robot_deinit(void);
#endif // ROBOT_H

27
program/src/main.c Normal file
View File

@ -0,0 +1,27 @@
/* *\
Copyrights 2025
Riombotique
\* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* Code du RPI Pico principale gérant les differentes entrées-sorties. *
* Ce Pico est un maitre pilotant le gyroscope, l'internet et le motion controller.*
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "headers/robot.h"
robot_t robot;
int main(void)
{
robot_init();
while(robot.is_running)
{
robot_handle_inputs_outputs();
}
robot_deinit();
return 0;
}

View File

@ -0,0 +1,97 @@
#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
#ifndef MEM_SIZE
#define MEM_SIZE 32768 // Augmenté pour plus de mémoire disponible
#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 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_MSS 1460
#define TCP_SND_BUF (8 * TCP_MSS) // Augmenté pour de meilleures performances
#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 0
#define LWIP_DHCP_SERVER 0
#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
#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__ */

View File

@ -0,0 +1,28 @@
#ifndef UDP_CLIENT_H
#define UDP_CLIENT_H
#include <stdint.h>
#include <lwip/udp.h>
#define UDP_CLIENT_PORT 4243
#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;
// Init udp client, set callback to NULL for the default callback
void udp_client_init(void);
// Exit udp client
void udp_client_deinit(void);
#endif // UDP_CLIENT_H

View File

@ -0,0 +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"
#define WIFI_OPERATOR_PASSWORD "CDuKaka2000!"
void init_wifi_operator(void);
#endif // WIFI_OPERATOR_H

View File

@ -0,0 +1,70 @@
#include "headers/udp_server.h"
#include <stdio.h>
udp_client_t udp_client;
static inline void handle_receive(struct pbuf *p, const ip_addr_t *addr, u16_t port)
{
if(p->len >= 2)
{
uint8_t *payload = (uint8_t *)p->payload;
uint16_t len = p->len;
udp_client.message_callback(payload, len, addr, port);
}
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)
{
puts("Error creating UDP client");
return;
}
udp_recv(udp_client.pcb, udp_receive_callback, &udp_client);
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);
}
void udp_client_deinit(void)
{
if(udp_client.pcb)
{
udp_remove(udp_client.pcb);
udp_client.pcb = NULL;
}
}

View File

@ -0,0 +1,74 @@
#include "headers/wifi_operator.h"
#include <stdio.h>
#include <pico/cyw43_arch.h>
#include <lwip/netif.h>
#include <lwip/ip4_addr.h>
void 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;)
{
// 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);
}
}
// Configuration de l'interface réseau
if(netif_default == NULL)
{
puts("Error: WiFi interface isn't accessible");
return;
}
netif_set_up(netif_default);
netif_set_link_up(netif_default);
netif_set_addr(netif_default, &ip, &netmask, &gateway);
puts("Connexion successfully etablished !");
}