![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
TRR2018 omar
Fork of biniou by
stateMachines.cpp
- Committer:
- GaspardD
- Date:
- 2018-09-10
- Revision:
- 12:51b1b40cc017
- Parent:
- 11:bc24b3ba51a9
- Child:
- 13:af9a59ccf60b
File content as of revision 12:51b1b40cc017:
#include "stateMachines.h" uint16_t distMurG90[NB_ECHANTILLONS_IR];//buffer tournant ir coté gauche pour moyenne uint16_t distMurD90[NB_ECHANTILLONS_IR];//buffer tournant ir coté droit pour moyenne uint16_t distMurG45[NB_ECHANTILLONS_IR];//buffer tournant ir avant gauche 45deg pour moyenne uint16_t distMurD45[NB_ECHANTILLONS_IR];//buffer tournant ir avant droit 45deg pour moyenne #ifdef DLVV uint16_t distMurG10[NB_ECHANTILLONS_IR];//buffer tournant ir avant gauche 10deg pour moyenne uint16_t distMurD10[NB_ECHANTILLONS_IR];//buffer tournant ir coté droit 10deg pour moyenne uint16_t distMurFront[NB_ECHANTILLONS_IR];//buffer tournant ir front #endif uint16_t distMurG90Moy; uint16_t distMurD90Moy; uint16_t distMurG45Moy; uint16_t distMurD45Moy; #ifdef DLVV uint16_t distMurG10Moy; uint16_t distMurD10Moy; uint16_t distMurFrontMoy #endif int dist_obstacle_lidar[NB_ECHANTILLONS_LIDAR];//buffer tournant int index_fifo_ir = 0;//pour géreer le buffer tournant int index_fifo_lidar = 0; //sections s_Section p_section1; //Controls PwmOut PwmMotor(PB_6); // PWM4 ch1 TIM4 PwmOut PwmDirection(PB_5); // PWM3 ch2 TIM3 float pulseDirection = 0.0015; float pulseSpeed = 0.015; //Capteurs direction AnalogIn anaG90(PC_1);//capteur ir coté gauche AnalogIn anaD90(PA_1);//capteur ir coté droit AnalogIn anaG45(PC_0);//capteur ir avant gauche 45 deg AnalogIn anaD45(PC_2);//capteur ir avant droit 45deg #ifdef DLVV AnalogIn anaDlvvG(PB_0);//capteur ir avant droit 10 deg AnalogIn anaDlvvD(PC_3);//capteur ir coté droit 10 deg AnalogIn anaDlvvFront(PA_4);//capteur ir avant #endif int differenceGD45; //Capteur vitesse InterruptIn it_tachymeter(PA_11); //LIDAR Serial serialLidar(PC_10,PC_11); // tx, rx int distLidar;// LiDAR actually measured distance value int strengthLidar;// LiDAR signal strength int check;// check numerical value storage int i; int uart[9];// store data measured by LiDAR const int HEADER=0x59;// data package frame header //SPEED int maxSpeed_cmps = 0; int tachySpeed_cmps = 0; int tachyStepsRegister = 0; int tachySectionDist_cm = 0; float ms_pwmSpeedPulse = 0.0015;//IDLE //Etats MUR_ST st_murs; SECTION_ST st_currentSection; MAX_SPEED_ST st_maxSpeed; THROTTLE_ST st_thro; #ifdef DLVV OBSTACLE_ST st_obstacle; #endif s_Section* p_sectionCourante = NULL; //time monitoring Timer timeSinceStart;// temps.start()/stop()/sec: read()/ms: read_ms()/µs: read_us() Timer timerSinceGetTachy; void getTachySpeed() { //tachySteps = VALEUR_DU_PIN; //poour gérer les vitesses lentes if(tachyStepsRegister == 0 && timerSinceGetTachy.read_us() < 500000) { return;//on attend encore un peu l'aquisition de la vitesse } tachySectionDist_cm += tachyStepsRegister * 4; tachySpeed_cmps = (tachyStepsRegister * 40000)/timerSinceGetTachy.read_us(); tachyStepsRegister=0; timerSinceGetTachy.reset(); timerSinceGetTachy.start(); return; } uint16_t getDistMoy(uint16_t* tab,int size) { int sumMoy = 0; for(int k=0; k<size;k++) { sumMoy+=tab[k]; } #if (NB_ECHANTILLONS_IR == 4 && NB_ECHANTILLONS_LIDAR == 4) return (uint16_t)sumMoy >> 2; #else return sumMoy/size; #endif } void it4cm() { tachyStepsRegister+=4; } void it_serial() { if(serialLidar.getc()==HEADER) { // determine data package frame header 0x59 uart[0]=HEADER; if(serialLidar.getc()==HEADER) { //determine data package frame header 0x59 uart[1]=HEADER; for(i=2; i<9; i++) { // store data to array uart[i]=serialLidar.getc(); } check=uart[0]+uart[1]+uart[2]+uart[3]+uart[4]+uart[5]+uart[6]+uart[7]; if(uart[8]==(check&0xff)) { // check the received data as per protocols distLidar=uart[2]+uart[3]*256;// calculate distance value strengthLidar=uart[4]+uart[5]*256;// calculate signal strength value } } } } //########## INIT STATES MACHINES ########## void mursInit(void) { st_murs=EQUILIBRAGE_REPULSIF; PwmDirection.period(0.010); //20 ms is default PwmDirection.pulsewidth(0.0015); return; } #ifdef DLVV void obstacleInit(void) { st_speedLimit=FRONT_CLEAR; return; } #endif void sectionInit(void) { timeSinceStart.reset(); timeSinceStart.start(); st_currentSection=ARRET; p_sectionCourante->nextSection=&p_section1; it_tachymeter.fall(&it4cm); timerSinceGetTachy.start(); getTachySpeed();//to reset tachySectionDist_cm = 0; tachyStepsRegister = 0; //section de test p_section1.nextSection = NULL; p_section1.targetSpeed_cmps = 50; p_section1.slowSpeed_cmps = 15; p_section1.brakingCoefficient = 20; // application de la formule p_section1.ms_accel = 0.00005; p_section1.ms_decel = 0.0001; p_section1.lidarWarningDist_cm = 200; p_section1.lng_section_cm = 1000;//10m p_section1.coef_p = 93400000.0; p_section1.coef_i = 0.0; p_section1.coef_d = 0.0; return; } void maxSpeedInit(void) { st_maxSpeed=SPEED_MAX; maxSpeed_cmps= p_sectionCourante->targetSpeed_cmps; serialLidar.baud(115200); return; } void throttleInit(void) { st_thro = AT_SPEED; PwmMotor.period(0.020); //20 ms is default PwmMotor.pulsewidth(0.0010); wait(3); PwmMotor.pulsewidth(0.0020); wait(1); PwmMotor.pulsewidth(0.0015); wait(1); return; } //########## UPDATE STATES ########## void mursUpdate(void) { MUR_ST st_tmpMurs; //lectures distMurG90[index_fifo_ir]=anaG90.read_u16(); distMurD90[index_fifo_ir]=anaD90.read_u16(); distMurG45[index_fifo_ir]=anaG45.read_u16(); distMurD45[index_fifo_ir]=anaD45.read_u16(); index_fifo_ir = (index_fifo_ir+1)%NB_ECHANTILLONS_IR; distMurG45Moy = getDistMoy(distMurG45,NB_ECHANTILLONS_IR); distMurD45Moy = getDistMoy(distMurD45,NB_ECHANTILLONS_IR); switch (st_murs) { case EQUILIBRAGE_REPULSIF: distMurG90Moy = getDistMoy(distMurG90,NB_ECHANTILLONS_IR); distMurD90Moy = getDistMoy(distMurD90,NB_ECHANTILLONS_IR); if( distMurG90Moy <= IR_DEADZONE_U16_22cm) { st_tmpMurs = ATTRACTIF_D; }else if(distMurD90Moy <= IR_DEADZONE_U16_22cm) { st_tmpMurs = ATTRACTIF_G; }else { st_tmpMurs = EQUILIBRAGE_REPULSIF; } break; case ATTRACTIF_D: distMurD90Moy= getDistMoy(distMurD90,NB_ECHANTILLONS_IR); if( distMurD90Moy >= IR_FAR_U16_105cm) { st_tmpMurs = ATTRACTIF_D; }else { st_tmpMurs = EQUILIBRAGE_REPULSIF; } break; case ATTRACTIF_G: distMurG90Moy= getDistMoy(distMurG90,NB_ECHANTILLONS_IR); if(distMurG90Moy >= IR_FAR_U16_105cm) { st_tmpMurs = ATTRACTIF_G; }else { st_tmpMurs = EQUILIBRAGE_REPULSIF; } break; default: return; } st_murs = st_tmpMurs; return; } #ifdef DLVV void obstacleUpdate(void) { return; } #endif void sectionUpdate(void) { SECTION_ST st_tmpSection; switch (st_currentSection) { case RUNNING_SECTION: if(tachySectionDist_cm > p_sectionCourante->lng_section_cm)//on pourrait rajouter un test lidar { st_tmpSection = LOADING_SECTION; } break; case LOADING_SECTION: if(p_sectionCourante->nextSection != NULL) { st_tmpSection = RUNNING_SECTION; }else { st_tmpSection=ARRET; } break; case ARRET: if(p_sectionCourante != NULL) { st_tmpSection = RUNNING_SECTION; } else { return; } break; default: break; } st_currentSection = st_tmpSection; return; } void maxSpeedUpdate(void) { MAX_SPEED_ST st_tmpMaxSpeed; i=0; if( strengthLidar > (LIDAR_STRENGTH_THRESOLD + LIDAR_STRENGH_DELTA) ) { if(distLidar > 30 && distLidar < p_sectionCourante->lidarWarningDist_cm) { st_tmpMaxSpeed = SPEED_VARIABLE; }else { st_tmpMaxSpeed = SPEED_LIMITED; } }else if(strengthLidar < (LIDAR_STRENGTH_THRESOLD - LIDAR_STRENGH_DELTA)) { st_tmpMaxSpeed = SPEED_MAX; } else{ return;//pour ne pas changer sans arret d'etats lorsque strengthLidar est à la limite du seuil } st_maxSpeed = st_tmpMaxSpeed; return; } void throttleUpdate(void) { THROTTLE_ST st_tmpThro; switch (st_thro) { case UNDER_SPEED: if( tachySpeed_cmps > maxSpeed_cmps ) { st_tmpThro = AT_SPEED; }else { return; } break; case OVER_SPEED: if( tachySpeed_cmps < maxSpeed_cmps ) { st_tmpThro = AT_SPEED; }else { return; } break; case AT_SPEED: if(tachySpeed_cmps > (maxSpeed_cmps + SPEED_DELTA_CMPS) ) { st_tmpThro = OVER_SPEED; } else if( tachySpeed_cmps < (maxSpeed_cmps - SPEED_DELTA_CMPS) ) { st_tmpThro = UNDER_SPEED; }else { return; } break; default: break; } st_thro = st_tmpThro; return; } //########## OUTPUT STATES ########## //updating output parameters void mursOutput(void) { switch (st_murs) { case EQUILIBRAGE_REPULSIF://TO DO TODO case ATTRACTIF_G: case ATTRACTIF_D: differenceGD45 = distMurD45Moy - distMurG45Moy; pulseDirection = (double) ((double)differenceGD45 / (double)p_sectionCourante->coef_p) + 0.0015; default: break; } PwmDirection.pulsewidth(pulseDirection); return; } #ifdef DLVV void obstacleOutput(void) { return; } #endif void sectionOutput(void) { switch (st_currentSection) { case RUNNING_SECTION: break; case LOADING_SECTION: p_sectionCourante=p_sectionCourante->nextSection; tachySectionDist_cm = 0; break; case ARRET: wait(10);//on est à l'arret break; default: break; } return; } void maxSpeedOutput(void) { switch(st_maxSpeed) { case SPEED_LIMITED: maxSpeed_cmps = p_sectionCourante->slowSpeed_cmps; break; case SPEED_VARIABLE: maxSpeed_cmps = p_sectionCourante->targetSpeed_cmps * (distLidar/(distLidar+p_sectionCourante->brakingCoefficient)); break; default: maxSpeed_cmps = p_sectionCourante->targetSpeed_cmps; break; } return; } void throttleOutput(void) { switch (st_thro) { case UNDER_SPEED: ms_pwmSpeedPulse += p_sectionCourante->ms_accel; break; case OVER_SPEED: ms_pwmSpeedPulse -= p_sectionCourante->ms_decel; break; case AT_SPEED: default: break; } return; }