Envoi de la position OK

This commit is contained in:
Samuel 2026-05-19 22:24:25 +02:00
parent 38b847bf24
commit 3cb9cb3eed
7 changed files with 130 additions and 21 deletions

View File

@ -16,6 +16,11 @@
"servomoteur.h": "c", "servomoteur.h": "c",
"moteurs.h": "c", "moteurs.h": "c",
"messagerie_applicative.h": "c", "messagerie_applicative.h": "c",
"config_robot.h": "c" "config_robot.h": "c",
"wifi.h": "c",
"string.h": "c",
"lwip_nosys.h": "c",
"async_context.h": "c",
"wifi_settings.h": "c"
} }
} }

95
Wifi.c
View File

@ -1,4 +1,13 @@
#include "Wifi.h"
#include <string.h>
#include "pico/cyw43_arch.h" #include "pico/cyw43_arch.h"
#include "lwip/etharp.h"
#include "lwip/ethip6.h"
#include "lwip/dns.h"
#include "lwip/igmp.h"
#include "lwip/tcpip.h"
#include "netif/ethernet.h"
#include <pico/lwip_nosys.h>
// Si le fichier n'existe pas, créez-le à partir du modèle "wifi_settings.h.default" // Si le fichier n'existe pas, créez-le à partir du modèle "wifi_settings.h.default"
#include "wifi_settings.h" #include "wifi_settings.h"
@ -7,15 +16,40 @@
struct udp_pcb* pcb; struct udp_pcb* pcb;
struct udp_pcb * pcb_r; struct udp_pcb * pcb_r;
ip_addr_t addr; ip_addr_t my_ip_addr, host_ip_addr;
char udp_buffer_r[UDP_BUFFER_LENGTH];
unsigned int udp_buffer_r_index = 0;
void udp_receive_put_in_buffer(char c){
if(udp_buffer_r_index + 1< UDP_BUFFER_LENGTH){
udp_buffer_r[udp_buffer_r_index] = c;
udp_buffer_r_index++;
}
}
/// @brief Copie les données reçues par UDP dans le tampon et renvoie le nombre de données copiées
/// @param tampon qui va contenir les données
/// @return Nombre de données dans le tampon
int udp_receive_get_from_buffer(char* tampon){
unsigned int taille_tampon;
if( udp_buffer_r_index > 0){
taille_tampon = udp_buffer_r_index;
memcpy(tampon, udp_buffer_r, taille_tampon);
udp_buffer_r_index = 0;
return taille_tampon;
}
return 0;
}
void udp_receive_callback(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port){ void udp_receive_callback(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port){
printf("UDP callback !\n"); printf("UDP callback !\n");
printf("IP: %d.%d.%d.%d, port %d\n", (addr->addr >>24) & 0xFF, (addr->addr >>16) & 0xFF, (addr->addr >>8) & 0xFF, addr->addr & 0xFF, port); printf("IP: %d.%d.%d.%d, port %d\n", (addr->addr) & 0xFF, (addr->addr >>8) & 0xFF, (addr->addr >>16) & 0xFF, (addr->addr >>24)& 0xFF, port);
printf("Taille:%d\n", p->len); printf("Taille:%d\n", p->len);
printf("Contenu: "); printf("Contenu: ");
for(int i=0; i < p->len; i++){ for(int i=0; i < p->len; i++){
printf("%c", ((char*)p->payload)[i]); printf("%c", ((char*)p->payload)[i]);
udp_receive_put_in_buffer(((char*)p->payload)[i]);
} }
printf("\n"); printf("\n");
} }
@ -48,35 +82,74 @@ int Wifi_init(void){
cyw43_arch_enable_sta_mode(); cyw43_arch_enable_sta_mode();
printf("Connecting to Wi-Fi...\n"); 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)) { while (cyw43_arch_wifi_connect_timeout_ms(MY_WIFI_SSID, MY_WIFI_PASSWORD, CYW43_AUTH_WPA2_AES_PSK, 30000)) {
printf("failed to connect.\n"); printf("failed to connect.\n");
return 1;
} else {
printf("Connected.\n");
} }
printf("Connected.\n");
pcb = udp_new(); pcb = udp_new();
ipaddr_aton(BEACON_TARGET, &addr); ipaddr_aton(BEACON_TARGET, &my_ip_addr);
UDP_client_init(); UDP_client_init();
return 0; return 0;
} }
void Wifi_udp_send_string(char * message){ void Wifi_udp_send_data(char * message, unsigned int size){
static int counter = 0; static int counter = 0;
struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, BEACON_MSG_LEN_MAX+1, PBUF_RAM); struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, BEACON_MSG_LEN_MAX+1, PBUF_RAM);
char *req = (char *)p->payload; char *req = (char *)p->payload;
memset(req, 0, BEACON_MSG_LEN_MAX+1); memset(req, 0, BEACON_MSG_LEN_MAX+1);
snprintf(req, BEACON_MSG_LEN_MAX, "%s", message); if(size <BEACON_MSG_LEN_MAX){
memcpy(req, message, size);
}else{
printf("Error: size >= BEACON_MSG_LEN_MAX\n");
}
err_t er = udp_sendto(pcb, p, &addr, UDP_PORT); err_t er = udp_sendto(pcb, p, &my_ip_addr, 1234);
pbuf_free(p); pbuf_free(p);
if (er != ERR_OK) { if (er != ERR_OK) {
printf("Failed to send UDP packet! error=%d", er); printf("Failed to send UDP packet! error=%d", er);
} else { } else {
//printf("Sent packet %d\n", counter); 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 Wifi_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, &my_ip_addr, 1234);
//er |= udp_sendto(pcb, p, &my_ip_addr, 1234);
pbuf_free(p);
if (er != ERR_OK) {
printf("Failed to send UDP packet! error=%d", er);
} else {
printf("Sent packet %d\n", counter);
counter++; counter++;
} }

4
Wifi.h
View File

@ -1,2 +1,6 @@
#define UDP_BUFFER_LENGTH 1024
int Wifi_init(void); int Wifi_init(void);
void Wifi_udp_send_string(char * message); void Wifi_udp_send_string(char * message);
void Wifi_udp_send_data(char * message, unsigned int size);
int udp_receive_get_from_buffer(char * );

View File

@ -2,6 +2,7 @@
#include <stdio.h> #include <stdio.h>
#include "communication.h" #include "communication.h"
#include "messagerie.h" #include "messagerie.h"
#include "Wifi.h"
#include "tusb.h" #include "tusb.h"
#define TAMPON_TAILLE 1020 #define TAMPON_TAILLE 1020
@ -43,6 +44,7 @@ void augmente_index(unsigned int *index, unsigned int offset){
/// et analyse si un message valide a été reçu. /// et analyse si un message valide a été reçu.
void communication_reception_message(){ void communication_reception_message(){
int input_char; int input_char;
char tampon_reception_udp[UDP_BUFFER_LENGTH];
int chaîne_octets_reçus[TAMPON_TAILLE]; int chaîne_octets_reçus[TAMPON_TAILLE];
unsigned int index_chaine_recue; unsigned int index_chaine_recue;
unsigned int index_tampon; unsigned int index_tampon;
@ -66,8 +68,13 @@ void communication_reception_message(){
} }
}*/ }*/
input_char = stdio_getchar_timeout_us(0); // input_char = stdio_getchar_timeout_us(0);
while(input_char != PICO_ERROR_TIMEOUT){ int nb_char = udp_receive_get_from_buffer(tampon_reception_udp);
int current_char = nb_char;
while(current_char > 0){
input_char = tampon_reception_udp[nb_char - current_char];
current_char--;
printf("%c %d\n", input_char, input_char);
com_reception_buffer.index_tampon_ecriture++; com_reception_buffer.index_tampon_ecriture++;
if(com_reception_buffer.index_tampon_ecriture >= TAMPON_TAILLE){ if(com_reception_buffer.index_tampon_ecriture >= TAMPON_TAILLE){
com_reception_buffer.index_tampon_ecriture = 0; com_reception_buffer.index_tampon_ecriture = 0;
@ -75,7 +82,7 @@ void communication_reception_message(){
com_reception_buffer.tampon[com_reception_buffer.index_tampon_ecriture] = input_char; com_reception_buffer.tampon[com_reception_buffer.index_tampon_ecriture] = input_char;
// Caractère suivant ? // Caractère suivant ?
input_char = stdio_getchar_timeout_us(0); //input_char = stdio_getchar_timeout_us(0);
} }
// Copie du tampon tournant dans une chaine // Copie du tampon tournant dans une chaine

View File

@ -1,7 +1,7 @@
// Changer le define en fonction qu'on soit sur les PAMIs ou sur le robot principal // Changer le define en fonction qu'on soit sur les PAMIs ou sur le robot principal
#define ROBOT_PROPULSION_2026 //#define ROBOT_PROPULSION_2026
//#define ROBOT_TYPE_PAMI #define ROBOT_TYPE_PAMI
#ifndef ROBOT_PROPULSION_2026 #ifndef ROBOT_PROPULSION_2026
#ifndef ROBOT_TYPE_PAMI #ifndef ROBOT_TYPE_PAMI

21
main.c
View File

@ -82,7 +82,15 @@ void main(void)
sleep_ms(50); sleep_ms(50);
Wifi_udp_send_string("Demarrage:1\n"); Wifi_udp_send_string("Demarrage:1\n");
Wifi_udp_send_string("test:1\n"); Wifi_udp_send_string("test:1\n");
sleep_ms(50);
Wifi_udp_send_string("Demarrage:1\n");
Wifi_udp_send_string("test:1\n");
sleep_ms(50);
Wifi_udp_send_string("Demarrage:1\n");
Wifi_udp_send_string("test:1\n");
sleep_ms(50);
Wifi_udp_send_data("\x01\x02\x03\x04\x05", 5);
Wifi_udp_send_string("test:1\n");
//i2c_maitre_init(); //i2c_maitre_init();
//Servomoteur_Init(); //Servomoteur_Init();
@ -248,21 +256,30 @@ void main(void)
} }
break; break;
case 'd': case 'd':
//printf("Demande donnees\n"); char message_envoi_udp[500];
printf("Demande donnees\n");
// Demande de données // Demande de données
memcpy(&message_applicatif, message.donnees, message.taille_donnees); memcpy(&message_applicatif, message.donnees, message.taille_donnees);
// printf("id_carte:%c %d\n", message_applicatif.id_carte, message_applicatif.id_carte); // printf("id_carte:%c %d\n", message_applicatif.id_carte, message_applicatif.id_carte);
// printf("adresse registre:%d\n", message_applicatif.adresse_registre, message_applicatif.id_carte); // printf("adresse registre:%d\n", message_applicatif.adresse_registre, message_applicatif.id_carte);
// printf("taille:%d\n", message_applicatif.taille_donnees, message_applicatif.taille_donnees); // printf("taille:%d\n", message_applicatif.taille_donnees, message_applicatif.taille_donnees);
message_envoi_udp[0] = 0xFF;
message_envoi_udp[1] = 0xFF;
message_envoi_udp[2] = 'P';
message_envoi_udp[3] = message_applicatif.taille_donnees+1;
stdio_putchar_raw(0xFF); stdio_putchar_raw(0xFF);
stdio_putchar_raw(0xFF); stdio_putchar_raw(0xFF);
stdio_putchar_raw('P'); stdio_putchar_raw('P');
stdio_putchar_raw(message_applicatif.taille_donnees+1); stdio_putchar_raw(message_applicatif.taille_donnees+1);
for(int i=0; i<message_applicatif.taille_donnees; i++ ){ for(int i=0; i<message_applicatif.taille_donnees; i++ ){
message_envoi_udp[4+i] = memoire_echange[message_applicatif.adresse_registre + i];
stdio_putchar_raw(memoire_echange[message_applicatif.adresse_registre + i]); stdio_putchar_raw(memoire_echange[message_applicatif.adresse_registre + i]);
} }
message_envoi_udp[4+message_applicatif.taille_donnees] = 0x00;
stdio_putchar_raw(0x00); stdio_putchar_raw(0x00);
stdio_putchar_raw('\n'); stdio_putchar_raw('\n');
Wifi_udp_send_data(message_envoi_udp, 4+message_applicatif.taille_donnees + 1);
//Wifi_udp_send_string("Coucou:1\n");
break; break;
default: default:
//printf("Message inconnu: %d %c\n", message.donnees[0], message.donnees[0]); //printf("Message inconnu: %d %c\n", message.donnees[0], message.donnees[0]);

View File

@ -1,8 +1,11 @@
#define MY_WIFI_SSID "Poivron" #define MY_WIFI_SSID "Xperia 10 III"
#define MY_WIFI_PASSWORD "vfS7Kru219vYU3P61D" #define MY_WIFI_PASSWORD "keuronde"
#define UDP_PORT 47269 #define UDP_PORT 47269
#define BEACON_TARGET "192.168.1.58"
//#define BEACON_TARGET "192.168.1.58"
#define BEACON_TARGET "172.28.172.11"
/*#define MY_WIFI_SSID "Livebox-A040" /*#define MY_WIFI_SSID "Livebox-A040"
#define MY_WIFI_PASSWORD "QkqoXCPt6UgvaUAWmq"*/ #define MY_WIFI_PASSWORD "QkqoXCPt6UgvaUAWmq"*/