From 0ad8474d941183a973037d051c38b7097e2b2060 Mon Sep 17 00:00:00 2001 From: Samuel Date: Mon, 24 Feb 2025 22:45:57 +0100 Subject: [PATCH] =?UTF-8?q?Code=20refactoris=C3=A9=20-=20d=C3=A9placements?= =?UTF-8?q?=20relatifs=20OK?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cerveau/Cerveau.ino | 249 ++++++++++-------------------- Cerveau/Communication_chassis.h | 20 +++ Cerveau/Communication_chassis.ino | 61 ++++++++ Cerveau/ServerWeb.h | 9 ++ Cerveau/ServerWeb.ino | 101 ++++++++++++ Readme.md | 5 + 6 files changed, 275 insertions(+), 170 deletions(-) create mode 100644 Cerveau/Communication_chassis.h create mode 100644 Cerveau/Communication_chassis.ino create mode 100644 Cerveau/ServerWeb.h create mode 100644 Cerveau/ServerWeb.ino diff --git a/Cerveau/Cerveau.ino b/Cerveau/Cerveau.ino index 68c6dc3..0ffd0fb 100644 --- a/Cerveau/Cerveau.ino +++ b/Cerveau/Cerveau.ino @@ -1,9 +1,11 @@ #include #include #include -#include #include +#include "Communication_chassis.h" +#include "ServerWeb.h" + #define WIRE Wire @@ -24,10 +26,10 @@ IPAddress subnet(255, 255, 255, 0); // IPAddress local_IP(192, 168, 1, 99); // IPAddress gateway(192, 168, 1, 1); // IPAddress subnet(255, 255, 255, 0); -WebServer server(80); + + const int16_t I2C_MASTER = 0x42; -const int16_t I2C_SLAVE_chassi = 0x55; const int16_t I2C_SLAVE_trian = 0x30; uint8_t test = 32; uint32_t i = 0; @@ -82,78 +84,6 @@ char* tableau[] = {"Lecture serveur", "Prise position", "Verif mvmt end ou cmd", char* statu[] = {"/..","./.","../"}; int index_statu=0; -struct chassis_reception_t { - unsigned char status; -}; - -struct chassis_emission_t { - unsigned char status; - int translation_x, translation_y, rotation_y; -}; - -void Scan_chassis(struct chassis_reception_t * chassis_reception); - -#define FORM \ - "\n" \ - "\n" \ - "\n" \ - "\n" \ - "A simple form\n" \ - "\n" \ - "\n" \ - "
\n" \ - "
" \ - "\n" \ - "
" \ - "\n" \ - "
" \ - "\n" \ - "
" \ - "\n" \ - "
" \ - "\n" \ - "
" \ - "\n" \ - "
" \ - "\n" \ - "
" \ - "\n" \ - "
" \ - "
" \ - "\n" \ - "
" \ - "\n" \ - "
" \ - "
" \ - "\n" \ - "
" \ - "
" \ - "\n" \ - "
" \ - "
" \ - "\n" \ - "
\n" \ - "\n" \ - "\n" - -void handleForm() { - String message; - message += FORM; - server.send(200, "text/html", message); - String myString0 = server.arg("X"); //positon de cmd en X mm - // x= myString0.toInt() * coef_mvt/10; - xA= myString0.toInt(); - String myString1 = server.arg("Y"); //positon de cmd en Y mm - // y= myString1.toInt() * coef_mvt/10; - yA= myString1.toInt(); - String myString2 = server.arg("R"); //positon de cmd en Rotation Deg ° - rot= myString2.toInt() * 13.88888; - String myString3 = server.arg("V"); // Vitesse de cmd en - vit= myString3.toInt(); - String myString4 = server.arg("A"); // Acceleration de cmd - acc= myString4.toInt(); -} - void setup() { // put your setup code here, to run once: @@ -162,7 +92,7 @@ void setup() { M5.Lcd.setTextSize(2); M5.Lcd.print("Bonjour\nEcran\n\nRecherche Wifi"); Initialisation_wifi(); - server.on("/form", handleForm); + Web_init(); WIRE.begin(); M5.Lcd.clear(); @@ -177,20 +107,8 @@ void setup() { //} // MemPosition_X = 800; // MemPosition_Y = 800; - M5.Lcd.clear(); - M5.Lcd.setCursor(0, 0);M5.Lcd.print("Pos");M5.Lcd.setCursor(50, 0);M5.Lcd.print("X :");M5.Lcd.setCursor(150, 0);M5.Lcd.print("|Xp:"); - M5.Lcd.setCursor(0, 20);M5.Lcd.print("n-1"); - M5.Lcd.setCursor(50, 20);M5.Lcd.print("Y :");M5.Lcd.setCursor(150, 20);M5.Lcd.print("|Yp:"); - M5.Lcd.setCursor(0, 40);M5.Lcd.print("Pos"); - M5.Lcd.setCursor(50, 40);M5.Lcd.print("X :"); - M5.Lcd.setCursor(0, 60);M5.Lcd.print("Act"); - M5.Lcd.setCursor(50, 60);M5.Lcd.print("Y :"); - M5.Lcd.setCursor(10, 80);M5.Lcd.print("Trans I2C"); - M5.Lcd.setCursor(10, 100);M5.Lcd.print("Triangulation "); - M5.Lcd.setCursor(10, 120);M5.Lcd.print("AngleRad"); - M5.Lcd.setCursor(10, 140);M5.Lcd.print("Tol x:"); - M5.Lcd.setCursor(10, 160);M5.Lcd.print("Tol y:"); - M5.Lcd.setCursor(10, 180);M5.Lcd.print("Case"); + affichage_standard_init(); + //M5.Lcd.setCursor(10, 200);M5.Lcd.print("cmd :"); } @@ -202,7 +120,7 @@ void loop() { strategie(); affichage_resultats(); - delay(500); + delay(10); } void affichage_resultats() { @@ -237,7 +155,7 @@ void Initialisation_wifi(){ if (test_wifi > 10) break; } if (WiFi.status() == WL_CONNECTED) { - server.begin(); + Serial.println("Server started"); delay(500); M5.Lcd.clear(); @@ -254,7 +172,6 @@ void Initialisation_wifi(){ while(1); } IPAddress myIP = WiFi.softAPIP(); - server.begin(); M5.Lcd.clear(); M5.Lcd.setCursor(10,10); M5.Lcd.print("Creation Hotspot ;-)"); @@ -266,6 +183,23 @@ void Initialisation_wifi(){ delay(1500); } +void affichage_standard_init(){ + M5.Lcd.clear(); + M5.Lcd.setCursor(0, 0);M5.Lcd.print("Pos");M5.Lcd.setCursor(50, 0);M5.Lcd.print("X :");M5.Lcd.setCursor(150, 0);M5.Lcd.print("|Xp:"); + M5.Lcd.setCursor(0, 20);M5.Lcd.print("n-1"); + M5.Lcd.setCursor(50, 20);M5.Lcd.print("Y :");M5.Lcd.setCursor(150, 20);M5.Lcd.print("|Yp:"); + M5.Lcd.setCursor(0, 40);M5.Lcd.print("Pos"); + M5.Lcd.setCursor(50, 40);M5.Lcd.print("X :"); + M5.Lcd.setCursor(0, 60);M5.Lcd.print("Act"); + M5.Lcd.setCursor(50, 60);M5.Lcd.print("Y :"); + M5.Lcd.setCursor(10, 80);M5.Lcd.print("Trans I2C"); + M5.Lcd.setCursor(10, 100);M5.Lcd.print("Triangulation "); + M5.Lcd.setCursor(10, 120);M5.Lcd.print("AngleRad"); + M5.Lcd.setCursor(10, 140);M5.Lcd.print("Tol x:"); + M5.Lcd.setCursor(10, 160);M5.Lcd.print("Tol y:"); + M5.Lcd.setCursor(10, 180);M5.Lcd.print("Case"); +} + void affiche_erreur(char * chaine1, char * chaine2){ M5.Lcd.clear(); M5.Lcd.setCursor(10,10); @@ -290,10 +224,19 @@ void affiche_msg(char * chaine1, char * chaine2){ void strategie(){ struct chassis_reception_t chassis_reception; + struct chassis_emission_t chassis_emission; switch(index_Maitre){ case 0 : //--------------------------------------------------------------------------------------------------------- - server.handleClient(); //************* Lecture des données recu pour coordoné (X,Y,rotation) - if(vit!=0 && !corrige){ + /// Lecture des commandes venant du serveur web et envoi des commandes au chassis + Web_gestion(); + M5.update(); + /// Si on a une réponse du client + /// On lit la réponse + if(Web_nouveau_message()){ + if(Web_type_requete() == WEB_DEPLACEMENT_RELATIF){ + chassis_emission = Web_get_donnees(); + send_Chassis(&chassis_emission); + } Serial.printf("vit:%d, corrige: %d\n", vit, corrige); MemCmd_X = xA; //Memorisation de la comande pour comparaison avec arrivé MemCmd_Y = yA; @@ -301,6 +244,41 @@ void strategie(){ //index_Maitre = 1; // Bloc le serveur WEB si on n'a pas la triangulation index_Maitre = 2; } + if(M5.BtnA.read() == 1){ + // Déplacement en X + chassis_emission.translation_x_mm = 2000; + chassis_emission.translation_y_mm = 0; + chassis_emission.rotation_z_rad = 0; + chassis_emission.vitesse = 2000; + chassis_emission.acceleration = 1500; + + send_Chassis(&chassis_emission); + index_Maitre = 2; + } + if(M5.BtnB.read() == 1){ + // Déplacement en Y + chassis_emission.translation_x_mm = 0; + chassis_emission.translation_y_mm = 2000; + chassis_emission.rotation_z_rad = 0; + chassis_emission.vitesse = 2000; + chassis_emission.acceleration = 1500; + + send_Chassis(&chassis_emission); + index_Maitre = 2; + + } + if(M5.BtnC.read() == 1){ + // Rotation + chassis_emission.translation_x_mm = 0; + chassis_emission.translation_y_mm = 0; + chassis_emission.rotation_z_rad = 100; + chassis_emission.vitesse = 2000; + chassis_emission.acceleration = 1500; + + send_Chassis(&chassis_emission); + index_Maitre = 2; + + } break; case 1 : //--------------------------------------------------------------------------------------------------------- @@ -315,8 +293,9 @@ void strategie(){ //Si position Pas OK ****************** break; - case 2 : //--------------------------------------------------------------------------------------------------------- + case 2 : // Deplacement relatif en cours Scan_chassis(&chassis_reception); //Verif de la fin de mouvement remonté par le chassis + // Pour l'instant, Scan_chassis bloque le programme en cas de problème de communication if(Err_Chassi_com !=0){ for(int j=0; j<10; j++){ affichage_resultats(); @@ -325,25 +304,13 @@ void strategie(){ } // ********************************* ERREUR DE COM FATAL *********************** } - if(Mvt_finit==0){ - cmd_chassi_x = xA; - cmd_chassi_y = yA; - Cmd_Angle = rot; - send_Chassis(); //Envoie des coordonées à atteindre - while(Mvt_finit!=1){ - Serial.println("while Mvt_finit"); - Scan_chassis(&chassis_reception); //Verif de la fin de mouvement remonté par le chassis (time out ?**********) - delay(50); - } - Scan_chassis(&chassis_reception); //Verif de la fin de mouvement remonté par le chassis (time out ?**********) + if(chassis_reception.status != MOUVEMENT_FINI){ + }else{ - send_Chassis_RAZ(); //Envoie des coordonées à atteindre + send_Chassis_RAZ(); // Immobilisation du robot + index_Maitre = 0; // On attend l'ordre suivant } - //M5.Lcd.clear(); - Serial.println("case 2 fin1/2"); - index_Maitre = 0; - Serial.println("case 2 fin2/2"); break; } } @@ -390,34 +357,6 @@ void compar_cinematique(){ } } -/// @brief Lit l'état du chassis en I2C -void Scan_chassis(struct chassis_reception_t * chassis_reception){ - unsigned char tampon2[14]; - //(Adresse I2c, Adresse dans le registre, tampon, longueur de donnée) - Serial.println("avant lire registre"); - error = I2C_lire_registre(I2C_SLAVE_chassi, 0, tampon2, 12); - Serial.println("apres lire registre"); - if (error !=0){ - Serial.println("Erreur I2C"); - Err_Chassi_com =1;IndexErr = 2; - affiche_erreur("Scan_Chassi", "Erreur I2C"); - while(1); - }else{ - Serial.println("I2C OK"); - Err_Chassi_com =0; - IndexErr = 0; - int valeur; - - Mvt_finit = (MOUVEMENT_FINI == tampon2[0]); - char chaine[100]; - sprintf(chaine, "tampon2: %x %x %x %x %x %x %x %x %x %x %x %x %x %x\n", tampon2[0], tampon2[1], tampon2[2], tampon2[3], tampon2[4], - tampon2[5], tampon2[6], tampon2[7], tampon2[8], tampon2[9], tampon2[10], tampon2[11], tampon2[12], tampon2[13]); - affiche_msg("Scan_Chassi", chaine); - Serial.println("Affect struct"); - chassis_reception->status = tampon2[0]; - Serial.println("apres Affect struct"); - } -} /// @brief Récupère position (X, Y) et l'orientation du robot void Scan_Triangulation(){ @@ -448,37 +387,7 @@ void Scan_Triangulation(){ } } -void send_Chassis_RAZ(){ - uint8_t RAZ_Message[11]={0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - error = I2C_ecrire_registre(I2C_SLAVE_chassi, 0, RAZ_Message, 11); -} - -void send_Chassis(){ - //if(nbr_essai<=10){ - // Prévient le chassis d'un nouveau mouvement avec le 2eme bit du premier Octet - Mot[0] = MOUVEMENT_EN_COURS; - //y*=-1; - //y = y*direction; - Mot[1] = cmd_chassi_x >>8; Mot[2] = cmd_chassi_x; - Mot[3] = cmd_chassi_y >>8; Mot[4] = cmd_chassi_y; - //Serial.println(y); - Mot[5] = Cmd_Angle >>8; Mot[6] = Cmd_Angle; - Mot[7] = vit >>8; Mot[8] = vit; - Mot[9] = acc >>8; Mot[10] = acc; - - error = I2C_ecrire_registre(I2C_SLAVE_chassi, 0, Mot, 11); - if (error !=0){Err_Cha_send =1; IndexErr = 5;} - else{Err_Cha_send =0;IndexErr = 0;} - if(error==0){ //si pas d'erreur de transmission alors RAZ des valeurs - nbr_essai ++; - cmd_chassi_x = 0; - cmd_chassi_y = 0; - Cmd_Angle = 0; - vit = 0; - acc = 0; - } -} void scan_I2C_bus(){ char error, address; diff --git a/Cerveau/Communication_chassis.h b/Cerveau/Communication_chassis.h new file mode 100644 index 0000000..5b55d85 --- /dev/null +++ b/Cerveau/Communication_chassis.h @@ -0,0 +1,20 @@ +#ifndef CHASSIS_H +#define CHASSSI_H + +#define I2C_SLAVE_chassi 0x55 + +struct chassis_reception_t { + unsigned char status; +}; + +struct chassis_emission_t { + unsigned char status; + int translation_x_mm, translation_y_mm, rotation_z_rad; + int vitesse, acceleration; +}; + +void Scan_chassis(struct chassis_reception_t * chassis_reception); +void send_Chassis(struct chassis_emission_t * chassis_emission); +void send_Chassis_RAZ(void); + +#endif diff --git a/Cerveau/Communication_chassis.ino b/Cerveau/Communication_chassis.ino new file mode 100644 index 0000000..c525e67 --- /dev/null +++ b/Cerveau/Communication_chassis.ino @@ -0,0 +1,61 @@ +//#include "Chassis.h" +#include +#include + + +/// @brief Lit l'état du chassis en I2C +void Scan_chassis(struct chassis_reception_t * chassis_reception){ + unsigned char tampon2[14]; + //(Adresse I2c, Adresse dans le registre, tampon, longueur de donnée) + error = I2C_lire_registre(I2C_SLAVE_chassi, 0, tampon2, 12); + if (error !=0){ + Err_Chassi_com =1;IndexErr = 2; + affiche_erreur("Scan_Chassi", "Erreur I2C"); + while(1); + }else{ + Serial.println("I2C OK"); + Err_Chassi_com =0; + IndexErr = 0; + + Mvt_finit = (MOUVEMENT_FINI == tampon2[0]); + chassis_reception->status = tampon2[0]; + } +} + +void send_Chassis(struct chassis_emission_t * chassis_emission){ + //if(nbr_essai<=10){ + // Prévient le chassis d'un nouveau mouvement avec le 2eme bit du premier Octet + Mot[0] = MOUVEMENT_EN_COURS; + //y*=-1; + //y = y*direction; + Mot[1] = chassis_emission->translation_x_mm >>8; + Mot[2] = chassis_emission->translation_x_mm; + Mot[3] = chassis_emission->translation_y_mm >>8; + Mot[4] = chassis_emission->translation_y_mm; + //Serial.println(y); + Mot[5] = chassis_emission->rotation_z_rad >>8; + Mot[6] = chassis_emission->rotation_z_rad; + Mot[7] = chassis_emission->vitesse >>8; Mot[8] = chassis_emission->vitesse; + Mot[9] = chassis_emission->acceleration >>8; + Mot[10] = chassis_emission->acceleration; + + error = I2C_ecrire_registre(I2C_SLAVE_chassi, 0, Mot, 11); + if (error !=0){Err_Cha_send =1; IndexErr = 5;} + else{Err_Cha_send =0;IndexErr = 0;} + if(error==0){ //si pas d'erreur de transmission alors RAZ des valeurs + nbr_essai ++; + cmd_chassi_x = 0; + cmd_chassi_y = 0; + Cmd_Angle = 0; + vit = 0; + acc = 0; + } +} + + + +void send_Chassis_RAZ(){ + uint8_t RAZ_Message[11]={0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + + error = I2C_ecrire_registre(I2C_SLAVE_chassi, 0, RAZ_Message, 11); +} diff --git a/Cerveau/ServerWeb.h b/Cerveau/ServerWeb.h new file mode 100644 index 0000000..f227ed6 --- /dev/null +++ b/Cerveau/ServerWeb.h @@ -0,0 +1,9 @@ +#define WEB_DEPLACEMENT_RELATIF 1 +#define WEB_DEPLACEMENT_ABSOLU 2 + +void Web_init(void); +bool Web_nouveau_message(void); +int Web_type_requete(void); +void Web_gestion(void); +struct chassis_emission_t Web_get_donnees(void); + diff --git a/Cerveau/ServerWeb.ino b/Cerveau/ServerWeb.ino new file mode 100644 index 0000000..31934d6 --- /dev/null +++ b/Cerveau/ServerWeb.ino @@ -0,0 +1,101 @@ +#include +#include + +//#include "Chassis.h" +//#include "ServerWeb.h" + +int Web_nouvelle_entree; +int type_requete; + +struct chassis_emission_t chassis_emission_web; + +#define FORM \ + "\n" \ + "\n" \ + "\n" \ + "\n" \ + "A simple form\n" \ + "\n" \ + "\n" \ + "
\n" \ + "
" \ + "\n" \ + "
" \ + "\n" \ + "
" \ + "\n" \ + "
" \ + "\n" \ + "
" \ + "\n" \ + "
" \ + "\n" \ + "
" \ + "\n" \ + "
" \ + "\n" \ + "
" \ + "
" \ + "\n" \ + "
" \ + "\n" \ + "
" \ + "
" \ + "\n" \ + "
" \ + "
" \ + "\n" \ + "
" \ + "
" \ + "\n" \ + "
\n" \ + "\n" \ + "\n" + +WebServer server(80); + +void Web_init(){ + server.begin(); + server.on("/form", handleForm); +} + +bool Web_nouveau_message(){ + if(Web_nouvelle_entree){ + Web_nouvelle_entree = 0; + return 1; + } + return 0; +} + +int Web_type_requete(){ + return type_requete; +} + +void Web_gestion(){ + server.handleClient(); +} + +struct chassis_emission_t Web_get_donnees(){ + return chassis_emission_web; +} + +void handleForm() { + String message; + type_requete = WEB_DEPLACEMENT_RELATIF; + message += FORM; + server.send(200, "text/html", message); + String myString0 = server.arg("X"); //positon de cmd en X mm + // x= myString0.toInt() * coef_mvt/10; + chassis_emission_web.translation_x_mm = myString0.toInt(); + String myString1 = server.arg("Y"); //positon de cmd en Y mm + // y= myString1.toInt() * coef_mvt/10; + chassis_emission_web.translation_y_mm = myString1.toInt(); + String myString2 = server.arg("R"); //positon de cmd en Rotation Deg ° + chassis_emission_web.rotation_z_rad = myString2.toInt() * 13.88888; + String myString3 = server.arg("V"); // Vitesse de cmd en + chassis_emission_web.vitesse = myString3.toInt(); + String myString4 = server.arg("A"); // Acceleration de cmd + chassis_emission_web.acceleration = myString4.toInt(); + Web_nouvelle_entree=1; +} + diff --git a/Readme.md b/Readme.md index f99cf83..76c1a0b 100644 --- a/Readme.md +++ b/Readme.md @@ -4,4 +4,9 @@ Déplacement fonctionnel Côté cerveau, un serveur web qui permet d'envoyer un ordre de déplacement relatif au châssis. Calibration des sens des moteurs pour les translations en X, Y et pour la rotation. +Calibration des translations non effectuées. +Vidéo réalisée le 24 février 2024, paramètres : +- vitesse : 2000 +- acceleration : 1500 +