Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Diff: fonction.cpp
- Revision:
- 1:cfb7e4122e21
- Parent:
- 0:c2a70ac44fd7
--- a/fonction.cpp Mon Jun 06 07:20:16 2022 +0000 +++ b/fonction.cpp Thu Jun 09 20:36:50 2022 +0000 @@ -1,217 +1,93 @@ #include "mbed.h" -#include <string> -#include <iostream> +#include "autre.h" #include "header.h" - -Serial pc (PA_2, PA_3); - -Serial husky (PA_0,PA_1); - -unsigned char reception_bidule[7]={0,0,0,0,0,0,'\n'}; +float distance_laser[8] = {0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}; +Timer temps_de_jeux; +Timer launch_time; +typedef enum {debut,chasse,ctourne_droite,ctourne_gauche,cvire_gauche,cvire_droite,cavance,vise,tire} type_statut; -unsigned char data_recept[50]; -unsigned char cpt=0,flag_printf=0,flag_tram=0; -unsigned char data_taille=0,cpt_taille=0,chksum=0; -unsigned char tableau_COMMAND_REQUEST[16]; - -unsigned short pos_x, pos_y; +static type_statut statut = debut; -bool flag_renvoi_pos = false; - -unsigned char COMMAND_REQUEST_KNOCK[6]={0x55,0xAA,0x11,0x00,0x2C,0x3C}; -unsigned char COMMAND_REQUEST_BLOCKS[6]={0x55,0xAA,0x11,0x00,0x21,0x31}; -unsigned char COMMAND_REQUEST[6]={0x55,0xAA,0x11,0x00,0x20,0x30}; -unsigned char COMMAND_REQUEST_ALGORITHM_BLOCK[8]={0x55,0xAA,0x11,0x02,0x2D,0x01,0x00,0x40}; -unsigned char COMMAND_REQUEST_ALGORITHM_COLOR[8]={0x55,0xAA,0x11,0x02,0x2D,0x04,0x00,0x43}; - -typedef enum +void cerveau(void) +{ + float x; +if(temps_de_jeux.read() < 88) +{ +switch (statut) { - HEADER1, - HEADER2, - ADRESSE, - TAILLE, - CHECKSUM, - DATA, - CMD -}T_etat; - -static T_etat etat = HEADER1; - -short xg_c,xd_c; -short xg_f,xd_f,xg_trig, xd_trig; -short angl; - -float calcul_angle( void ) -{ - xg_c = main_recept_droit(); - xd_c = 150; //main_recept_gauche(); - xg_f = xg_c; - xd_f = xd_c; + case debut : + init_moteur_propulsion(); + init_moteur_balle(); + avancer(1); + prise_avant(0.5); + if(distance_laser[2] < 0.05) statut = vise; + break; + + case chasse : + //priorité à droite + prise_avant(0.5); + vire_droite(1); + //si la distance détécté par le laser est bon => case adaptée + if (test_presence(1)) statut = cvire_gauche; + else if (test_presence(2) && test_presence(3)) statut = cavance; + else if (test_presence(4)) statut = cvire_droite; + break; + + case cvire_gauche : //trourne fortement à gauche + vire_gauche(1); + if(test_presence(1)) statut = ctourne_gauche; + break; + + case cvire_droite : //trourne fortement à droite + vire_droite(1); + if(test_presence(4)) statut = ctourne_droite; + break; - xg_trig = 0.185 * xg_f; - xd_trig = 60 - 0.185 * xd_f; + case cavance : + avancer(1); + if(!test_presence(2)) statut = ctourne_droite; + else if(!test_presence(3)) statut = ctourne_gauche; + else if(distance_laser[2] < 0.05) statut = vise; + break; + + case ctourne_droite : //tourne un peu à droite + tourner_droite(1); + if(test_presence(3)) statut = cavance; + else if(distance_laser[2] < 0.05) statut = vise; + break; - if ( xd_trig > xd_trig ) - { - angl = (xd_trig - 30)%360; + case ctourne_gauche : //tourne un peu à gauche + tourner_gauche(1); + if(test_presence(2)) statut = cavance; + else if(distance_laser[2] < 0.05) statut = vise; + break; + + case vise : + stop(); + prise_avant(0); + //calcule de math etc + x = 0.5; + //c'est calculé t'inquiète + statut = tire; + break; + + case tire : + launch_time.reset(); + prise_arriere(x); + if(launch_time.read() > 0.5 ) statut = chasse; + break; } - else - { - angl = 210 - 0.185* xd_f; - } - return angl; -} - -short main_recept_droit() - - { - husky.attach(&reception,Serial::RxIrq); //La fct réception est donc activé a chaque fois qu'une donnée passe dans le Rx - husky.baud(9600); - pc.printf("Debut du programme\n"); - - pc.printf("Envoie command_request_knock\n"); - - envoi_commande(6,COMMAND_REQUEST_KNOCK); //Test d'aqcuisiton avec commande_bidule - - bool flag_end = false; - - while(flag_end==false) - { - pc.printf("Check:%02X\n",chksum); - if(flag_printf) - { - affichage_buffer_reception(); //renvoie de données - flag_printf=0; - flag_end=true; - } - } - - flag_end=false; - - pc.printf("Reception de data, envoie Command_request_blocks\n"); - - flag_renvoi_pos = false; - while(1) - { - - envoi_commande(6,COMMAND_REQUEST_BLOCKS); - - for(int i=0;i<data_taille+6;i++) //header + header2 + adress+ datataille + comande + data + checksum - { - tableau_COMMAND_REQUEST[i] = data_recept[i]; - } - - flag_end=false; - - - for(int j=0;j<16;j++) //header + header2 + adress+ datataille + comande + data + checksum - { - pc.printf("%02x:",tableau_COMMAND_REQUEST[j]); - - } - pc.printf("\n"); - reception_position(); - - if(flag_renvoi_pos) - { - return pos_x; - } - flag_renvoi_pos = true; +} else +{ + stop(); + //creve_ballon(); } } -void reception_position(void) -{ - if(tableau_COMMAND_REQUEST[4] == 0x2A) //0x2A de [4] pour vérifier qu'ils s'agisse d'un block return_blocks - { - unsigned short high_x, low_x, high_y, low_y; - - high_x = tableau_COMMAND_REQUEST[6]; - low_x = tableau_COMMAND_REQUEST[5]; - - high_y = tableau_COMMAND_REQUEST[8]; - low_y = tableau_COMMAND_REQUEST[7]; - - pos_x = (high_x<<8) + low_x; - pos_y = (high_y<<8) + low_y; - } - else - { - pos_x = 0x0000; - pos_y = 0x0000; - } - pc.printf("%04d:",pos_x); - pc.printf("%04d:",pos_y); - pc.printf("\n"); -} - -void envoi_commande(unsigned char taille,unsigned char buffer[]) //Envoie la commande a la carte -{ - for(int i=0;i<taille;i++) - { - husky.putc(buffer[i]); - } -} - -void affichage_buffer_reception() //Acquiere la donnée revoyé par la carte ex : [0x55] -{ - for(int i=0;i<data_taille+6;i++) //header + header2 + adress+ datataille + comande + data + checksum - { - pc.printf("%02x:",data_recept[i]); - } - pc.printf("\n"); -} - -void reception(void) //Automate de réception des données -{ - data_recept[cpt] = husky.getc(); - //flag_recept=1; - switch(etat) - { - case HEADER1:chksum=0; - cpt=0; - if(data_recept[cpt]==0x55){ - etat=HEADER2; - } - break; - - case HEADER2 : if(data_recept[cpt]==0xAA) - etat=ADRESSE; - else - etat=HEADER1; - break; - - case ADRESSE : if(data_recept[cpt]==0x11) - etat=TAILLE; - else - etat=HEADER1; - break; - case TAILLE : data_taille=data_recept[cpt]; - etat=CMD; - break; - - case CMD: // utilisation de cmd??? - etat=DATA; - if(data_taille==0) - etat=CHECKSUM; - break; - - case DATA: cpt_taille++; - if(cpt_taille==data_taille) - etat=CHECKSUM; - break; - - case CHECKSUM : etat=HEADER1; - - if(data_recept[cpt]==chksum) - { - flag_tram=flag_tram+1; - flag_printf=1; - cpt_taille=0; - cpt=0; - } - break; - } - chksum=chksum+data_recept[cpt]; - cpt++; - -} + + bool test_presence( int num) + { + //calcule_coord laser(distance_laser[num]); + //if ((Xballe < 4000)|| (Xballe > 0) || (Yballe < 4000) || (Yballe > 0)) return 1; + //else return 0; + } + \ No newline at end of file