![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
TRR2018 omar
Fork of biniou by
stateMachines.cpp@42:3f12252862b9, 2018-09-19 (annotated)
- Committer:
- GaspardD
- Date:
- Wed Sep 19 20:44:07 2018 +0000
- Revision:
- 42:3f12252862b9
- Parent:
- 41:4dd36f607279
- Child:
- 43:d2164ee3336a
si y a un obstacle ? moins de 30 cm et que le lidar dit rien: on stoppe tout
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
GaspardD | 12:51b1b40cc017 | 1 | #include "stateMachines.h" |
ohlimi2 | 36:bccddd02966a | 2 | //#if DEBUG >0 |
GaspardD | 13:af9a59ccf60b | 3 | Serial pc(USBTX, USBRX); // tx, rx |
ohlimi2 | 36:bccddd02966a | 4 | //#endif |
GaspardD | 29:fc984fe08ca7 | 5 | |
GaspardD | 29:fc984fe08ca7 | 6 | //*************** declarations *************** |
GaspardD | 33:88d8f254c0b7 | 7 | |
GaspardD | 33:88d8f254c0b7 | 8 | //time monitoring |
GaspardD | 23:04d393220daa | 9 | #ifdef DUMP_SAMPLIG_PERIOD |
GaspardD | 22:26fc6e6f7a55 | 10 | Timer timerLog; |
GaspardD | 19:771bf61be276 | 11 | #endif |
GaspardD | 19:771bf61be276 | 12 | |
GaspardD | 33:88d8f254c0b7 | 13 | Timer timeSinceStart;// temps.start()/stop()/sec: read()/ms: read_ms()/µs: read_us() |
GaspardD | 39:de3638276b7e | 14 | Timer timerSinceTachy; |
GaspardD | 42:3f12252862b9 | 15 | Timer timersinceSerial; |
GaspardD | 33:88d8f254c0b7 | 16 | |
GaspardD | 29:fc984fe08ca7 | 17 | double distMurG90[NB_ECHANTILLONS_IR];//buffer tournant ir coté gauche pour moyenne |
GaspardD | 29:fc984fe08ca7 | 18 | double distMurD90[NB_ECHANTILLONS_IR];//buffer tournant ir coté droit pour moyenne |
GaspardD | 29:fc984fe08ca7 | 19 | double shortDistMurG90[NB_ECHANTILLONS_IR];//buffer tournant ir coté gauche petites distances pour moyenne |
GaspardD | 29:fc984fe08ca7 | 20 | double shortDistMurD90[NB_ECHANTILLONS_IR];//buffer tournant ir coté droit petites distances pour moyenne |
GaspardD | 29:fc984fe08ca7 | 21 | double distMurG45[NB_ECHANTILLONS_IR];//buffer tournant ir avant gauche 45deg pour moyenne |
GaspardD | 29:fc984fe08ca7 | 22 | double distMurD45[NB_ECHANTILLONS_IR];//buffer tournant ir avant droit 45deg pour moyenne |
GaspardD | 11:bc24b3ba51a9 | 23 | #ifdef DLVV |
GaspardD | 33:88d8f254c0b7 | 24 | double distMurG10[NB_ECHANTILLONS_IR];//buffer tournant ir avant gauche 10deg pour moyenne |
GaspardD | 33:88d8f254c0b7 | 25 | double distMurD10[NB_ECHANTILLONS_IR];//buffer tournant ir coté droit 10deg pour moyenne |
GaspardD | 33:88d8f254c0b7 | 26 | double distMurFront[NB_ECHANTILLONS_IR];//buffer tournant ir front |
GaspardD | 11:bc24b3ba51a9 | 27 | #endif |
GaspardD | 29:fc984fe08ca7 | 28 | double distMurG90Moy; |
GaspardD | 29:fc984fe08ca7 | 29 | double distMurD90Moy; |
GaspardD | 29:fc984fe08ca7 | 30 | double shortDistMurG90Moy; |
GaspardD | 29:fc984fe08ca7 | 31 | double shortDistMurD90Moy; |
GaspardD | 29:fc984fe08ca7 | 32 | double trueDistMurG90Moy; |
GaspardD | 29:fc984fe08ca7 | 33 | double trueDistMurD90Moy; |
GaspardD | 29:fc984fe08ca7 | 34 | double distMurG45Moy; |
GaspardD | 29:fc984fe08ca7 | 35 | double distMurD45Moy; |
GaspardD | 23:04d393220daa | 36 | |
GaspardD | 12:51b1b40cc017 | 37 | #ifdef DLVV |
GaspardD | 29:fc984fe08ca7 | 38 | double distMurG10Moy; |
GaspardD | 29:fc984fe08ca7 | 39 | double distMurD10Moy; |
GaspardD | 29:fc984fe08ca7 | 40 | double distMurFrontMoy |
GaspardD | 12:51b1b40cc017 | 41 | #endif |
GaspardD | 8:1d8c3ca5e508 | 42 | int index_fifo_ir = 0;//pour géreer le buffer tournant |
GaspardD | 8:1d8c3ca5e508 | 43 | int index_fifo_lidar = 0; |
GaspardD | 11:bc24b3ba51a9 | 44 | //sections |
GaspardD | 11:bc24b3ba51a9 | 45 | s_Section p_section1; |
GaspardD | 39:de3638276b7e | 46 | s_Section p_section2; |
GaspardD | 2:fd0ffe46a87d | 47 | |
GaspardD | 15:129f205ff030 | 48 | //PWM Controls |
GaspardD | 12:51b1b40cc017 | 49 | PwmOut PwmMotor(PB_6); // PWM4 ch1 TIM4 |
GaspardD | 12:51b1b40cc017 | 50 | PwmOut PwmDirection(PB_5); // PWM3 ch2 TIM3 |
GaspardD | 8:1d8c3ca5e508 | 51 | |
GaspardD | 21:de7a0a47f8a3 | 52 | int pulseDirection_us = DIRECTION_PULSE_MIDDLE; |
ohlimi2 | 36:bccddd02966a | 53 | double pulseDirection_us_temp ; |
GaspardD | 19:771bf61be276 | 54 | int pulseSpeed_us = INITAL_PULSE_SPEED_US; |
GaspardD | 12:51b1b40cc017 | 55 | //Capteurs direction |
GaspardD | 22:26fc6e6f7a55 | 56 | AnalogIn anaG90(CAPT_90_GAUCHE);//capteur ir coté gauche |
GaspardD | 22:26fc6e6f7a55 | 57 | AnalogIn anaD90(CAPT_90_DROITE);//capteur ir coté droit |
GaspardD | 29:fc984fe08ca7 | 58 | AnalogIn anaShortG90(CAPT_90_GAUCHE_SHORT);//capteur ir coté gauche short |
GaspardD | 29:fc984fe08ca7 | 59 | AnalogIn anaShortD90(CAPT_90_DROITE_SHORT);//capteur ir coté droit short |
GaspardD | 22:26fc6e6f7a55 | 60 | AnalogIn anaG45(CAPT_45_GAUCHE);//capteur ir avant gauche 45 deg |
GaspardD | 22:26fc6e6f7a55 | 61 | AnalogIn anaD45(CAPT_45_DROITE);//capteur ir avant droit 45deg |
GaspardD | 11:bc24b3ba51a9 | 62 | #ifdef DLVV |
GaspardD | 22:26fc6e6f7a55 | 63 | AnalogIn anaDlvvG(CAPT_10_GAUCHE);//capteur ir avant droit 10 deg |
GaspardD | 22:26fc6e6f7a55 | 64 | AnalogIn anaDlvvD(CAPT_10_DROITE);//capteur ir coté droit 10 deg |
GaspardD | 22:26fc6e6f7a55 | 65 | AnalogIn anaDlvvFront(CAPT_DEVANT);//capteur ir avant |
GaspardD | 11:bc24b3ba51a9 | 66 | #endif |
GaspardD | 12:51b1b40cc017 | 67 | |
GaspardD | 29:fc984fe08ca7 | 68 | //piste |
ohlimi2 | 36:bccddd02966a | 69 | double largeurPiste90 = 150.0; |
ohlimi2 | 36:bccddd02966a | 70 | double largeurPiste45 = 150.0; |
GaspardD | 29:fc984fe08ca7 | 71 | double positionSurPiste90 = 75.0; |
GaspardD | 29:fc984fe08ca7 | 72 | double positionSurPiste90Prev = positionSurPiste90; |
GaspardD | 29:fc984fe08ca7 | 73 | double positionSurPiste45 = 75.0; |
GaspardD | 29:fc984fe08ca7 | 74 | double positionSurPiste45Prev = positionSurPiste45; |
GaspardD | 29:fc984fe08ca7 | 75 | |
ohlimi2 | 36:bccddd02966a | 76 | double derive45,derive90; |
GaspardD | 21:de7a0a47f8a3 | 77 | int lastDifferences90[NB_INTEGRAL_SAMPLES] = {0};//for integral correction |
GaspardD | 21:de7a0a47f8a3 | 78 | int lastDifferenceIndex = 0; |
GaspardD | 21:de7a0a47f8a3 | 79 | int integralSum; |
GaspardD | 12:51b1b40cc017 | 80 | |
GaspardD | 12:51b1b40cc017 | 81 | //Capteur vitesse |
GaspardD | 12:51b1b40cc017 | 82 | InterruptIn it_tachymeter(PA_11); |
GaspardD | 2:fd0ffe46a87d | 83 | |
GaspardD | 11:bc24b3ba51a9 | 84 | |
GaspardD | 11:bc24b3ba51a9 | 85 | //LIDAR |
GaspardD | 12:51b1b40cc017 | 86 | Serial serialLidar(PC_10,PC_11); // tx, rx |
GaspardD | 11:bc24b3ba51a9 | 87 | |
GaspardD | 11:bc24b3ba51a9 | 88 | int distLidar;// LiDAR actually measured distance value |
GaspardD | 23:04d393220daa | 89 | int distLidarPrev; |
GaspardD | 11:bc24b3ba51a9 | 90 | int strengthLidar;// LiDAR signal strength |
GaspardD | 23:04d393220daa | 91 | int strengthLidarPrev; |
GaspardD | 11:bc24b3ba51a9 | 92 | int check;// check numerical value storage |
GaspardD | 12:51b1b40cc017 | 93 | int i; |
GaspardD | 11:bc24b3ba51a9 | 94 | int uart[9];// store data measured by LiDAR |
GaspardD | 11:bc24b3ba51a9 | 95 | const int HEADER=0x59;// data package frame header |
GaspardD | 11:bc24b3ba51a9 | 96 | |
GaspardD | 11:bc24b3ba51a9 | 97 | //SPEED |
GaspardD | 29:fc984fe08ca7 | 98 | double maxSpeed_cmps = 0; |
ohlimi2 | 36:bccddd02966a | 99 | double tachySpeed_cmps = 0; //en cm/s |
GaspardD | 39:de3638276b7e | 100 | double tachyStepsRegister = 0; |
GaspardD | 29:fc984fe08ca7 | 101 | double tachySectionDist_cm = 0; |
ohlimi2 | 36:bccddd02966a | 102 | double tachyTotalDist_cm = 0.0; |
GaspardD | 8:1d8c3ca5e508 | 103 | |
GaspardD | 27:f8c3f1524a64 | 104 | #ifdef FREINAGE_ADAPTATIF |
GaspardD | 24:698fefbbee00 | 105 | Timer brakingTimer; |
GaspardD | 24:698fefbbee00 | 106 | int brakingDurationNeeded_us = 0; |
GaspardD | 27:f8c3f1524a64 | 107 | #endif |
GaspardD | 24:698fefbbee00 | 108 | |
GaspardD | 8:1d8c3ca5e508 | 109 | //Etats |
GaspardD | 8:1d8c3ca5e508 | 110 | MUR_ST st_murs; |
GaspardD | 21:de7a0a47f8a3 | 111 | MUR_ST st_tmpMurs; |
GaspardD | 11:bc24b3ba51a9 | 112 | SECTION_ST st_currentSection; |
GaspardD | 21:de7a0a47f8a3 | 113 | SECTION_ST st_tmpSection; |
GaspardD | 11:bc24b3ba51a9 | 114 | MAX_SPEED_ST st_maxSpeed; |
GaspardD | 21:de7a0a47f8a3 | 115 | MAX_SPEED_ST st_tmpMaxSpeed; |
GaspardD | 8:1d8c3ca5e508 | 116 | THROTTLE_ST st_thro; |
GaspardD | 21:de7a0a47f8a3 | 117 | THROTTLE_ST st_tmpThro; |
GaspardD | 11:bc24b3ba51a9 | 118 | #ifdef DLVV |
GaspardD | 11:bc24b3ba51a9 | 119 | OBSTACLE_ST st_obstacle; |
GaspardD | 11:bc24b3ba51a9 | 120 | #endif |
GaspardD | 11:bc24b3ba51a9 | 121 | |
GaspardD | 11:bc24b3ba51a9 | 122 | s_Section* p_sectionCourante = NULL; |
GaspardD | 8:1d8c3ca5e508 | 123 | |
GaspardD | 19:771bf61be276 | 124 | |
GaspardD | 19:771bf61be276 | 125 | // +++++++++++++++++++++++++++++++++++++++++++ SAMPLING +++++++++++++++++++++++++++++++++++++++++++ |
GaspardD | 19:771bf61be276 | 126 | #ifdef SAMPLING |
GaspardD | 19:771bf61be276 | 127 | s_Sample history[TAILLE_SAMPLES]; |
GaspardD | 19:771bf61be276 | 128 | int indexSample = 0; |
GaspardD | 19:771bf61be276 | 129 | void initSamples(void) |
GaspardD | 19:771bf61be276 | 130 | { |
GaspardD | 19:771bf61be276 | 131 | for(int m=0; m<TAILLE_SAMPLES; m++) { |
GaspardD | 19:771bf61be276 | 132 | history[m].states.murs_dlvv = '0'; |
GaspardD | 19:771bf61be276 | 133 | history[m].states.section = '0'; |
GaspardD | 19:771bf61be276 | 134 | history[m].states.maxSpeed = '0'; |
GaspardD | 19:771bf61be276 | 135 | history[m].states.throttle = '0'; |
GaspardD | 19:771bf61be276 | 136 | history[m].time = 0; |
GaspardD | 29:fc984fe08ca7 | 137 | history[m].position45 = 0.0; |
GaspardD | 29:fc984fe08ca7 | 138 | history[m].position90 = 0.0; |
GaspardD | 29:fc984fe08ca7 | 139 | history[m].largeurPiste = 0.0; |
GaspardD | 29:fc984fe08ca7 | 140 | history[m].dist = 0.0; |
GaspardD | 21:de7a0a47f8a3 | 141 | history[m].pwm_thro_us = 0; |
GaspardD | 21:de7a0a47f8a3 | 142 | history[m].pwm_dir_us = 0; |
GaspardD | 23:04d393220daa | 143 | history[m].distLidar = 0; |
GaspardD | 23:04d393220daa | 144 | history[m].strLidar = 0; |
GaspardD | 19:771bf61be276 | 145 | } |
GaspardD | 21:de7a0a47f8a3 | 146 | #if DEBUG > 0 |
GaspardD | 19:771bf61be276 | 147 | pc.printf("[INIT SAMPLE DONE]\r\n"); |
GaspardD | 21:de7a0a47f8a3 | 148 | #endif |
GaspardD | 19:771bf61be276 | 149 | return; |
GaspardD | 19:771bf61be276 | 150 | } |
GaspardD | 19:771bf61be276 | 151 | void sampleLog(void) |
GaspardD | 19:771bf61be276 | 152 | { |
GaspardD | 23:04d393220daa | 153 | #ifdef DUMP_SAMPLIG_PERIOD |
GaspardD | 23:04d393220daa | 154 | if(timerLog.read_us() > DUMP_SAMPLIG_PERIOD) { |
GaspardD | 22:26fc6e6f7a55 | 155 | timerLog.reset(); |
GaspardD | 22:26fc6e6f7a55 | 156 | timerLog.start(); |
GaspardD | 23:04d393220daa | 157 | #endif |
GaspardD | 22:26fc6e6f7a55 | 158 | if(indexSample < TAILLE_SAMPLES) { |
GaspardD | 19:771bf61be276 | 159 | #ifdef DLVV |
GaspardD | 22:26fc6e6f7a55 | 160 | history[indexSample].states.murs_dlvv = (char)st_obstacle; |
GaspardD | 19:771bf61be276 | 161 | #else |
GaspardD | 22:26fc6e6f7a55 | 162 | history[indexSample].states.murs_dlvv = (char)st_murs; |
GaspardD | 19:771bf61be276 | 163 | #endif |
GaspardD | 22:26fc6e6f7a55 | 164 | history[indexSample].states.section = (char)st_currentSection; |
GaspardD | 22:26fc6e6f7a55 | 165 | history[indexSample].states.maxSpeed = (char)st_maxSpeed; |
GaspardD | 22:26fc6e6f7a55 | 166 | history[indexSample].states.throttle = (char)st_thro; |
GaspardD | 22:26fc6e6f7a55 | 167 | history[indexSample].time = timeSinceStart.read_us() ; |
GaspardD | 29:fc984fe08ca7 | 168 | history[indexSample].position45 = positionSurPiste45; |
GaspardD | 29:fc984fe08ca7 | 169 | history[indexSample].position90 = positionSurPiste90; |
ohlimi2 | 36:bccddd02966a | 170 | history[indexSample].largeurPiste = largeurPiste90; |
GaspardD | 22:26fc6e6f7a55 | 171 | history[indexSample].pwm_thro_us = pulseSpeed_us; |
GaspardD | 22:26fc6e6f7a55 | 172 | history[indexSample].pwm_dir_us = pulseDirection_us; |
GaspardD | 22:26fc6e6f7a55 | 173 | history[indexSample].dist = tachySectionDist_cm, |
GaspardD | 41:4dd36f607279 | 174 | history[indexSample].distLidar = distLidar; |
GaspardD | 23:04d393220daa | 175 | history[indexSample].strLidar = strengthLidar; |
GaspardD | 23:04d393220daa | 176 | indexSample++; |
GaspardD | 19:771bf61be276 | 177 | #if DEBUG > 0 |
ohlimi2 | 36:bccddd02966a | 178 | pc.printf("\r\nodo:%d dist = %.4lf \tstrength = %.4ld \tC45D: %.4lf C45G: %.4lf C90D: %.4lf C90G: %.4lf looptime: %.4lf micros",tachySectionDist_cm,distLidar,strengthLidar,distMurD45Moy,distMurG45Moy,distMurD90Moy,distMurG90Moy,timeSinceStart.read_us());// output signal strength value |
ohlimi2 | 36:bccddd02966a | 179 | pc.printf("\r\nstate Murs: %.4lf, state Section %.4lf, state MaxSpeed %.4lf, state Throttle %.4lf\r\n",st_murs,st_currentSection,st_maxSpeed,st_thro); |
ohlimi2 | 34:c98fd59c690a | 180 | //wait(2); |
GaspardD | 22:26fc6e6f7a55 | 181 | timeSinceStart.reset(); |
GaspardD | 22:26fc6e6f7a55 | 182 | timeSinceStart.start(); |
GaspardD | 19:771bf61be276 | 183 | #endif |
GaspardD | 22:26fc6e6f7a55 | 184 | } |
GaspardD | 22:26fc6e6f7a55 | 185 | return; |
GaspardD | 23:04d393220daa | 186 | #ifdef DUMP_SAMPLIG_PERIOD |
GaspardD | 19:771bf61be276 | 187 | } |
GaspardD | 23:04d393220daa | 188 | #endif |
GaspardD | 19:771bf61be276 | 189 | } |
GaspardD | 19:771bf61be276 | 190 | void transmitData(void) |
GaspardD | 19:771bf61be276 | 191 | { |
GaspardD | 21:de7a0a47f8a3 | 192 | #if DEBUG > 0 |
GaspardD | 19:771bf61be276 | 193 | pc.printf("[START TO TRANSMIT]\r\n"); |
GaspardD | 21:de7a0a47f8a3 | 194 | #endif |
GaspardD | 39:de3638276b7e | 195 | serialLidar.printf("time,position 45,position 90,largeur piste,pwm_thro_us,pwm_dir_us,dist,murs/dlvv,section,maxSpeed,throttle\r\n"); |
GaspardD | 19:771bf61be276 | 196 | for(int p=0; p<indexSample; p++) { |
GaspardD | 39:de3638276b7e | 197 | serialLidar.printf("%d,%.5f,%.5f,%.5f,%d,%d,%.5f,%d,%d,%d,%d\r\n", |
GaspardD | 21:de7a0a47f8a3 | 198 | history[p].time, |
GaspardD | 29:fc984fe08ca7 | 199 | history[p].position45, |
GaspardD | 29:fc984fe08ca7 | 200 | history[p].position90, |
GaspardD | 39:de3638276b7e | 201 | history[p].largeurPiste, |
GaspardD | 21:de7a0a47f8a3 | 202 | history[p].pwm_thro_us, |
GaspardD | 21:de7a0a47f8a3 | 203 | history[p].pwm_dir_us, |
GaspardD | 21:de7a0a47f8a3 | 204 | history[p].dist, |
GaspardD | 21:de7a0a47f8a3 | 205 | history[p].states.murs_dlvv, |
GaspardD | 21:de7a0a47f8a3 | 206 | history[p].states.section, |
GaspardD | 21:de7a0a47f8a3 | 207 | history[p].states.maxSpeed, |
GaspardD | 21:de7a0a47f8a3 | 208 | history[p].states.throttle); |
GaspardD | 19:771bf61be276 | 209 | } |
GaspardD | 19:771bf61be276 | 210 | return; |
GaspardD | 19:771bf61be276 | 211 | } |
GaspardD | 19:771bf61be276 | 212 | |
GaspardD | 19:771bf61be276 | 213 | #endif |
GaspardD | 19:771bf61be276 | 214 | |
GaspardD | 19:771bf61be276 | 215 | // +++++++++++++++++++++++++++++++++++++++++++ FUNCTION UTILS +++++++++++++++++++++++++++++++++++++++++++ |
GaspardD | 19:771bf61be276 | 216 | |
GaspardD | 19:771bf61be276 | 217 | #if DEBUG >= -1 |
GaspardD | 19:771bf61be276 | 218 | void pressed(void) |
GaspardD | 19:771bf61be276 | 219 | { |
GaspardD | 21:de7a0a47f8a3 | 220 | #if DEBUG > 0 |
GaspardD | 19:771bf61be276 | 221 | pc.printf("[BTN PRESSED]\r\n"); |
GaspardD | 21:de7a0a47f8a3 | 222 | #endif |
GaspardD | 19:771bf61be276 | 223 | //p_sectionCourante = &p_section1; |
GaspardD | 19:771bf61be276 | 224 | transmitData(); |
GaspardD | 19:771bf61be276 | 225 | } |
GaspardD | 19:771bf61be276 | 226 | #endif |
GaspardD | 19:771bf61be276 | 227 | |
GaspardD | 29:fc984fe08ca7 | 228 | void initIntegrationTable() |
GaspardD | 29:fc984fe08ca7 | 229 | { |
GaspardD | 41:4dd36f607279 | 230 | for(int h=0; h<NB_INTEGRAL_SAMPLES; h++) { |
GaspardD | 29:fc984fe08ca7 | 231 | lastDifferences90[h] = 0; |
GaspardD | 29:fc984fe08ca7 | 232 | } |
GaspardD | 29:fc984fe08ca7 | 233 | return; |
GaspardD | 29:fc984fe08ca7 | 234 | } |
GaspardD | 29:fc984fe08ca7 | 235 | |
GaspardD | 21:de7a0a47f8a3 | 236 | |
GaspardD | 29:fc984fe08ca7 | 237 | double getShortDistMoy(AnalogIn* p,double* tab,int size) |
GaspardD | 11:bc24b3ba51a9 | 238 | { |
GaspardD | 29:fc984fe08ca7 | 239 | tab[index_fifo_ir] = 3.3 * (double)p->read(); // on convertit directement en volts |
GaspardD | 29:fc984fe08ca7 | 240 | //tension proportionelle à l'inverse de la distance en dessous de 2V |
GaspardD | 41:4dd36f607279 | 241 | if(tab[index_fifo_ir]< 0.1034) { |
GaspardD | 41:4dd36f607279 | 242 | tab[index_fifo_ir]= 0.11; |
GaspardD | 41:4dd36f607279 | 243 | } |
GaspardD | 39:de3638276b7e | 244 | tab[index_fifo_ir] = (11.3531/(tab[index_fifo_ir]-0.1034))-0.42; |
GaspardD | 29:fc984fe08ca7 | 245 | |
GaspardD | 29:fc984fe08ca7 | 246 | int sumMoy = 0; |
GaspardD | 29:fc984fe08ca7 | 247 | for(int k=0; k<size; k++) { |
GaspardD | 29:fc984fe08ca7 | 248 | sumMoy+=tab[k]; |
GaspardD | 29:fc984fe08ca7 | 249 | } |
GaspardD | 29:fc984fe08ca7 | 250 | return sumMoy/size; |
GaspardD | 29:fc984fe08ca7 | 251 | } |
GaspardD | 29:fc984fe08ca7 | 252 | |
GaspardD | 29:fc984fe08ca7 | 253 | double getDistMoy(AnalogIn* p,double* tab,int size) |
GaspardD | 29:fc984fe08ca7 | 254 | { |
GaspardD | 29:fc984fe08ca7 | 255 | tab[index_fifo_ir] = 3.3 * (double)p->read(); // on convertit directement en volts |
GaspardD | 29:fc984fe08ca7 | 256 | //tension proportionelle à l'inverse de la distance |
ohlimi2 | 36:bccddd02966a | 257 | tab[index_fifo_ir] = 1.0/(0.0161 * (double)tab[index_fifo_ir]); |
GaspardD | 11:bc24b3ba51a9 | 258 | int sumMoy = 0; |
GaspardD | 19:771bf61be276 | 259 | for(int k=0; k<size; k++) { |
GaspardD | 11:bc24b3ba51a9 | 260 | sumMoy+=tab[k]; |
GaspardD | 11:bc24b3ba51a9 | 261 | } |
GaspardD | 11:bc24b3ba51a9 | 262 | return sumMoy/size; |
GaspardD | 11:bc24b3ba51a9 | 263 | } |
GaspardD | 39:de3638276b7e | 264 | void tachyCheck() |
GaspardD | 39:de3638276b7e | 265 | { |
GaspardD | 41:4dd36f607279 | 266 | if(timerSinceTachy.read_us() > 500000) { |
GaspardD | 39:de3638276b7e | 267 | tachySpeed_cmps = 0.0; |
GaspardD | 39:de3638276b7e | 268 | } |
GaspardD | 41:4dd36f607279 | 269 | return; |
GaspardD | 39:de3638276b7e | 270 | } |
GaspardD | 39:de3638276b7e | 271 | |
GaspardD | 2:fd0ffe46a87d | 272 | |
GaspardD | 12:51b1b40cc017 | 273 | void it4cm() |
GaspardD | 12:51b1b40cc017 | 274 | { |
GaspardD | 39:de3638276b7e | 275 | //le timer sert de flag sur le freinage: |
GaspardD | 39:de3638276b7e | 276 | //si le biniou ne bouge plus, il n'essaye plus de freiner tant qu'il n'y a pas un nouveau calcul de la vitesse |
GaspardD | 39:de3638276b7e | 277 | //dans la fonction tachyCheck() >> quand il n'y a pas eu d'IT en 500 ms si la vitesse est inférieure à 0.16 m/s, elle sera considérée comme nulle. |
GaspardD | 39:de3638276b7e | 278 | |
ohlimi2 | 36:bccddd02966a | 279 | tachySectionDist_cm += TACHY_CM; |
ohlimi2 | 36:bccddd02966a | 280 | tachyTotalDist_cm += TACHY_CM; |
GaspardD | 39:de3638276b7e | 281 | tachySpeed_cmps = (TACHY_CM * 1000000.0)/(double)timerSinceTachy.read_us(); // a chaque IT on a parcouru 8 cm soit (8*1000000)/durée |
ohlimi2 | 36:bccddd02966a | 282 | #if DEBUG > 2 |
ohlimi2 | 36:bccddd02966a | 283 | pc.printf("IT: distance parcourue %.4lf , vitesse:%.4lf \r\n",tachyTotalDist_cm,tachySpeed_cmps); |
ohlimi2 | 36:bccddd02966a | 284 | #endif |
GaspardD | 39:de3638276b7e | 285 | timerSinceTachy.reset(); |
GaspardD | 39:de3638276b7e | 286 | timerSinceTachy.start(); |
ohlimi2 | 36:bccddd02966a | 287 | return; |
ohlimi2 | 36:bccddd02966a | 288 | |
GaspardD | 19:771bf61be276 | 289 | #if DEBUG > 0 |
GaspardD | 14:d471faa7d1a2 | 290 | pc.printf("IT tachy\r\n"); |
GaspardD | 14:d471faa7d1a2 | 291 | #endif |
GaspardD | 12:51b1b40cc017 | 292 | } |
GaspardD | 12:51b1b40cc017 | 293 | |
GaspardD | 12:51b1b40cc017 | 294 | void it_serial() |
GaspardD | 12:51b1b40cc017 | 295 | { |
GaspardD | 12:51b1b40cc017 | 296 | if(serialLidar.getc()==HEADER) { // determine data package frame header 0x59 |
GaspardD | 12:51b1b40cc017 | 297 | uart[0]=HEADER; |
GaspardD | 12:51b1b40cc017 | 298 | if(serialLidar.getc()==HEADER) { //determine data package frame header 0x59 |
GaspardD | 12:51b1b40cc017 | 299 | uart[1]=HEADER; |
GaspardD | 12:51b1b40cc017 | 300 | for(i=2; i<9; i++) { // store data to array |
GaspardD | 12:51b1b40cc017 | 301 | uart[i]=serialLidar.getc(); |
GaspardD | 12:51b1b40cc017 | 302 | } |
GaspardD | 12:51b1b40cc017 | 303 | check=uart[0]+uart[1]+uart[2]+uart[3]+uart[4]+uart[5]+uart[6]+uart[7]; |
GaspardD | 12:51b1b40cc017 | 304 | if(uart[8]==(check&0xff)) { // check the received data as per protocols |
GaspardD | 23:04d393220daa | 305 | distLidarPrev = distLidar; |
GaspardD | 12:51b1b40cc017 | 306 | distLidar=uart[2]+uart[3]*256;// calculate distance value |
GaspardD | 23:04d393220daa | 307 | strengthLidarPrev = strengthLidar; |
GaspardD | 12:51b1b40cc017 | 308 | strengthLidar=uart[4]+uart[5]*256;// calculate signal strength value |
GaspardD | 42:3f12252862b9 | 309 | timersinceSerial.reset(); |
GaspardD | 42:3f12252862b9 | 310 | timersinceSerial.start(); |
GaspardD | 12:51b1b40cc017 | 311 | } |
GaspardD | 12:51b1b40cc017 | 312 | } |
GaspardD | 12:51b1b40cc017 | 313 | } |
GaspardD | 12:51b1b40cc017 | 314 | } |
GaspardD | 19:771bf61be276 | 315 | // +++++++++++++++++++++++++++++++++++++++++++ STATES MACHINES +++++++++++++++++++++++++++++++++++++++++++ |
GaspardD | 19:771bf61be276 | 316 | |
GaspardD | 8:1d8c3ca5e508 | 317 | //########## INIT STATES MACHINES ########## |
GaspardD | 11:bc24b3ba51a9 | 318 | void mursInit(void) |
GaspardD | 8:1d8c3ca5e508 | 319 | { |
GaspardD | 39:de3638276b7e | 320 | #if DEBUG > 0 |
GaspardD | 13:af9a59ccf60b | 321 | pc.printf("Init Murs\r\n"); |
GaspardD | 13:af9a59ccf60b | 322 | #endif |
GaspardD | 13:af9a59ccf60b | 323 | timeSinceStart.start(); |
GaspardD | 29:fc984fe08ca7 | 324 | st_murs=REF_BIDIR; |
GaspardD | 19:771bf61be276 | 325 | PwmDirection.period_us(SPEED_PERIOD_US); |
GaspardD | 17:8c465656eea4 | 326 | PwmDirection.pulsewidth_us(DIRECTION_PULSE_MIDDLE);// milieu |
GaspardD | 8:1d8c3ca5e508 | 327 | return; |
GaspardD | 8:1d8c3ca5e508 | 328 | } |
GaspardD | 11:bc24b3ba51a9 | 329 | #ifdef DLVV |
GaspardD | 11:bc24b3ba51a9 | 330 | void obstacleInit(void) |
GaspardD | 8:1d8c3ca5e508 | 331 | { |
GaspardD | 19:771bf61be276 | 332 | #if DEBUG > 0 |
GaspardD | 13:af9a59ccf60b | 333 | pc.printf("Init Obstacle\r\n"); |
GaspardD | 13:af9a59ccf60b | 334 | #endif |
GaspardD | 29:fc984fe08ca7 | 335 | st_speedLimit=ALL_CLEAR; |
GaspardD | 8:1d8c3ca5e508 | 336 | return; |
GaspardD | 8:1d8c3ca5e508 | 337 | } |
GaspardD | 11:bc24b3ba51a9 | 338 | #endif |
GaspardD | 11:bc24b3ba51a9 | 339 | void sectionInit(void) |
GaspardD | 2:fd0ffe46a87d | 340 | { |
GaspardD | 19:771bf61be276 | 341 | #if DEBUG > 0 |
GaspardD | 13:af9a59ccf60b | 342 | pc.printf("Init Section\r\n"); |
GaspardD | 13:af9a59ccf60b | 343 | #endif |
GaspardD | 11:bc24b3ba51a9 | 344 | st_currentSection=ARRET; |
GaspardD | 14:d471faa7d1a2 | 345 | p_sectionCourante=&p_section1; |
GaspardD | 12:51b1b40cc017 | 346 | it_tachymeter.fall(&it4cm); |
GaspardD | 39:de3638276b7e | 347 | timerSinceTachy.start(); |
GaspardD | 39:de3638276b7e | 348 | tachySectionDist_cm = 0; |
GaspardD | 39:de3638276b7e | 349 | tachyStepsRegister = 0; |
GaspardD | 11:bc24b3ba51a9 | 350 | |
GaspardD | 23:04d393220daa | 351 | //section de test 1 |
GaspardD | 23:04d393220daa | 352 | p_section1.nextSection =NULL;// &p_section2; |
GaspardD | 29:fc984fe08ca7 | 353 | p_section1.consigne_position = 75.0; |
GaspardD | 39:de3638276b7e | 354 | p_section1.targetSpeed_cmps = 400.0; |
GaspardD | 40:b9450d753782 | 355 | p_section1.targetSpeed_cmps = 500.0; |
GaspardD | 29:fc984fe08ca7 | 356 | p_section1.slowSpeed_cmps = 328.0; |
GaspardD | 21:de7a0a47f8a3 | 357 | p_section1.coef_p_speed = 1; |
GaspardD | 29:fc984fe08ca7 | 358 | p_section1.lidarWarningDist_cm = 120.0; |
GaspardD | 40:b9450d753782 | 359 | p_section1.lng_section_cm = 2000.0;//20m |
GaspardD | 40:b9450d753782 | 360 | p_section1.coef_p = 0.40; |
GaspardD | 40:b9450d753782 | 361 | p_section1.coef_d = 0.05; |
GaspardD | 40:b9450d753782 | 362 | p_section1.coef_i = 0.001; |
GaspardD | 39:de3638276b7e | 363 | |
GaspardD | 39:de3638276b7e | 364 | //section de test |
GaspardD | 39:de3638276b7e | 365 | p_section2.nextSection = NULL; |
GaspardD | 39:de3638276b7e | 366 | p_section2.consigne_position = 75.0; |
GaspardD | 39:de3638276b7e | 367 | p_section2.targetSpeed_cmps = 328.0; |
GaspardD | 39:de3638276b7e | 368 | p_section2.slowSpeed_cmps = 328.0; |
GaspardD | 39:de3638276b7e | 369 | p_section2.coef_p_speed = 1.0; |
GaspardD | 39:de3638276b7e | 370 | p_section2.lidarWarningDist_cm = 300.0; |
GaspardD | 39:de3638276b7e | 371 | p_section2.lng_section_cm = 200.0;//2m |
GaspardD | 39:de3638276b7e | 372 | p_section2.coef_p = 1; |
GaspardD | 39:de3638276b7e | 373 | p_section2.coef_i = 0.00001; |
GaspardD | 39:de3638276b7e | 374 | p_section2.coef_d = 0.00001; |
GaspardD | 23:04d393220daa | 375 | |
GaspardD | 8:1d8c3ca5e508 | 376 | return; |
GaspardD | 8:1d8c3ca5e508 | 377 | } |
GaspardD | 11:bc24b3ba51a9 | 378 | |
GaspardD | 11:bc24b3ba51a9 | 379 | void maxSpeedInit(void) |
GaspardD | 11:bc24b3ba51a9 | 380 | { |
GaspardD | 19:771bf61be276 | 381 | #if DEBUG > 0 |
GaspardD | 13:af9a59ccf60b | 382 | pc.printf("Init Max Speed\r\n"); |
GaspardD | 13:af9a59ccf60b | 383 | #endif |
GaspardD | 11:bc24b3ba51a9 | 384 | st_maxSpeed=SPEED_MAX; |
GaspardD | 11:bc24b3ba51a9 | 385 | maxSpeed_cmps= p_sectionCourante->targetSpeed_cmps; |
GaspardD | 11:bc24b3ba51a9 | 386 | serialLidar.baud(115200); |
GaspardD | 14:d471faa7d1a2 | 387 | serialLidar.attach(&it_serial); |
GaspardD | 11:bc24b3ba51a9 | 388 | return; |
GaspardD | 11:bc24b3ba51a9 | 389 | } |
GaspardD | 11:bc24b3ba51a9 | 390 | |
GaspardD | 11:bc24b3ba51a9 | 391 | void throttleInit(void) |
GaspardD | 8:1d8c3ca5e508 | 392 | { |
GaspardD | 19:771bf61be276 | 393 | #if DEBUG > 0 |
GaspardD | 13:af9a59ccf60b | 394 | pc.printf("Init Throttle\r\n"); |
GaspardD | 13:af9a59ccf60b | 395 | #endif |
GaspardD | 21:de7a0a47f8a3 | 396 | st_thro = REGULATION_SPEED; |
ohlimi2 | 36:bccddd02966a | 397 | PwmMotor.period_us(DIRECTION_PERIOD_MS); //20 ms is default |
GaspardD | 19:771bf61be276 | 398 | PwmMotor.pulsewidth_us(1000);//MIN |
GaspardD | 12:51b1b40cc017 | 399 | wait(3); |
GaspardD | 19:771bf61be276 | 400 | PwmMotor.pulsewidth_us(2000);//MAX |
GaspardD | 12:51b1b40cc017 | 401 | wait(1); |
GaspardD | 19:771bf61be276 | 402 | PwmMotor.pulsewidth_us(1500);//ZEROING |
GaspardD | 12:51b1b40cc017 | 403 | wait(1); |
GaspardD | 19:771bf61be276 | 404 | pulseSpeed_us = INITAL_PULSE_SPEED_US; |
GaspardD | 19:771bf61be276 | 405 | #if DEBUG > 0 |
ohlimi2 | 36:bccddd02966a | 406 | pc.printf("temps init: %.4lf micros\r\n",timeSinceStart.read_us()); |
ohlimi2 | 36:bccddd02966a | 407 | pc.printf("\r\nStates INIT: state Murs: %.4lf, state Section %.4lf, state MaxSpeed %.4lf, state Throttle %.4lf\r\n",st_murs,st_currentSection,st_maxSpeed,st_thro); |
GaspardD | 13:af9a59ccf60b | 408 | timeSinceStart.reset(); |
GaspardD | 13:af9a59ccf60b | 409 | timeSinceStart.start(); |
GaspardD | 13:af9a59ccf60b | 410 | #endif |
GaspardD | 23:04d393220daa | 411 | #ifdef DUMP_SAMPLIG_PERIOD |
GaspardD | 22:26fc6e6f7a55 | 412 | timerLog.start(); |
GaspardD | 22:26fc6e6f7a55 | 413 | #endif |
GaspardD | 22:26fc6e6f7a55 | 414 | |
GaspardD | 8:1d8c3ca5e508 | 415 | return; |
GaspardD | 8:1d8c3ca5e508 | 416 | } |
GaspardD | 2:fd0ffe46a87d | 417 | |
GaspardD | 8:1d8c3ca5e508 | 418 | |
GaspardD | 8:1d8c3ca5e508 | 419 | //########## UPDATE STATES ########## |
GaspardD | 11:bc24b3ba51a9 | 420 | void mursUpdate(void) |
GaspardD | 2:fd0ffe46a87d | 421 | { |
GaspardD | 14:d471faa7d1a2 | 422 | #if (DEBUG > 3) |
GaspardD | 14:d471faa7d1a2 | 423 | pc.printf("\r\nUpdate MURS\r\n"); |
GaspardD | 14:d471faa7d1a2 | 424 | #endif |
GaspardD | 11:bc24b3ba51a9 | 425 | //lectures |
GaspardD | 29:fc984fe08ca7 | 426 | |
GaspardD | 29:fc984fe08ca7 | 427 | distMurG45Moy = getDistMoy(&anaG45,distMurG45,NB_ECHANTILLONS_IR); |
GaspardD | 29:fc984fe08ca7 | 428 | distMurD45Moy = getDistMoy(&anaD45,distMurD45,NB_ECHANTILLONS_IR); |
GaspardD | 29:fc984fe08ca7 | 429 | distMurG90Moy = getDistMoy(&anaG90,distMurG90,NB_ECHANTILLONS_IR); |
ohlimi2 | 36:bccddd02966a | 430 | distMurD90Moy = getDistMoy(&anaD90,distMurD90,NB_ECHANTILLONS_IR); |
GaspardD | 39:de3638276b7e | 431 | shortDistMurG90Moy = getShortDistMoy(&anaShortG90,shortDistMurD90,NB_ECHANTILLONS_IR); |
GaspardD | 39:de3638276b7e | 432 | shortDistMurD90Moy = getShortDistMoy(&anaShortD90,shortDistMurD90,NB_ECHANTILLONS_IR); |
GaspardD | 11:bc24b3ba51a9 | 433 | index_fifo_ir = (index_fifo_ir+1)%NB_ECHANTILLONS_IR; |
GaspardD | 29:fc984fe08ca7 | 434 | |
ohlimi2 | 36:bccddd02966a | 435 | |
GaspardD | 41:4dd36f607279 | 436 | trueDistMurD90Moy = distMurD90Moy; |
GaspardD | 41:4dd36f607279 | 437 | trueDistMurG90Moy = distMurG90Moy; |
GaspardD | 39:de3638276b7e | 438 | |
GaspardD | 41:4dd36f607279 | 439 | if(shortDistMurG90Moy < DIST_MIN_LONG_CM) { |
GaspardD | 29:fc984fe08ca7 | 440 | trueDistMurG90Moy = shortDistMurG90Moy; |
GaspardD | 41:4dd36f607279 | 441 | } else { |
GaspardD | 29:fc984fe08ca7 | 442 | trueDistMurG90Moy = distMurG90Moy; |
GaspardD | 29:fc984fe08ca7 | 443 | } |
GaspardD | 41:4dd36f607279 | 444 | if(shortDistMurD90Moy < DIST_MIN_LONG_CM) { |
GaspardD | 29:fc984fe08ca7 | 445 | trueDistMurD90Moy = shortDistMurD90Moy; |
GaspardD | 41:4dd36f607279 | 446 | } else { |
GaspardD | 29:fc984fe08ca7 | 447 | trueDistMurD90Moy = distMurD90Moy; |
GaspardD | 39:de3638276b7e | 448 | } |
ohlimi2 | 36:bccddd02966a | 449 | |
GaspardD | 29:fc984fe08ca7 | 450 | |
GaspardD | 29:fc984fe08ca7 | 451 | #ifdef DLVV |
GaspardD | 29:fc984fe08ca7 | 452 | switch (st_obstacle) { |
GaspardD | 41:4dd36f607279 | 453 | case FRONT_OBSTRUCTED: |
GaspardD | 41:4dd36f607279 | 454 | st_tmpMurs = REF_A_GAUCHE; |
GaspardD | 41:4dd36f607279 | 455 | return; |
GaspardD | 41:4dd36f607279 | 456 | case RIGHT_OBSTRUCTED: |
GaspardD | 41:4dd36f607279 | 457 | st_tmpMurs = REF_A_GAUCHE; |
GaspardD | 41:4dd36f607279 | 458 | return; |
GaspardD | 41:4dd36f607279 | 459 | case LEFT_OBSTRUCTED: |
GaspardD | 41:4dd36f607279 | 460 | st_tmpMurs = REF_A_DROITE; |
GaspardD | 41:4dd36f607279 | 461 | return; |
GaspardD | 41:4dd36f607279 | 462 | default: |
GaspardD | 41:4dd36f607279 | 463 | break; |
GaspardD | 29:fc984fe08ca7 | 464 | } |
GaspardD | 29:fc984fe08ca7 | 465 | |
GaspardD | 29:fc984fe08ca7 | 466 | #endif |
GaspardD | 41:4dd36f607279 | 467 | st_tmpMurs = REF_BIDIR; |
GaspardD | 29:fc984fe08ca7 | 468 | |
GaspardD | 41:4dd36f607279 | 469 | st_murs = st_tmpMurs; |
GaspardD | 12:51b1b40cc017 | 470 | |
GaspardD | 8:1d8c3ca5e508 | 471 | return; |
GaspardD | 8:1d8c3ca5e508 | 472 | } |
GaspardD | 11:bc24b3ba51a9 | 473 | #ifdef DLVV |
GaspardD | 11:bc24b3ba51a9 | 474 | void obstacleUpdate(void) |
GaspardD | 8:1d8c3ca5e508 | 475 | { |
GaspardD | 8:1d8c3ca5e508 | 476 | return; |
GaspardD | 8:1d8c3ca5e508 | 477 | } |
GaspardD | 11:bc24b3ba51a9 | 478 | #endif |
GaspardD | 11:bc24b3ba51a9 | 479 | |
GaspardD | 11:bc24b3ba51a9 | 480 | void sectionUpdate(void) |
GaspardD | 8:1d8c3ca5e508 | 481 | { |
GaspardD | 14:d471faa7d1a2 | 482 | #if (DEBUG > 3) |
GaspardD | 14:d471faa7d1a2 | 483 | pc.printf("\r\nUpdate Section\r\n"); |
GaspardD | 14:d471faa7d1a2 | 484 | #endif |
GaspardD | 11:bc24b3ba51a9 | 485 | switch (st_currentSection) { |
GaspardD | 19:771bf61be276 | 486 | case RUNNING_SECTION: |
GaspardD | 19:771bf61be276 | 487 | if(tachySectionDist_cm > p_sectionCourante->lng_section_cm) { //on pourrait rajouter un test lidar |
GaspardD | 19:771bf61be276 | 488 | st_tmpSection = LOADING_SECTION; |
GaspardD | 19:771bf61be276 | 489 | } else { |
GaspardD | 19:771bf61be276 | 490 | return; |
GaspardD | 19:771bf61be276 | 491 | } |
GaspardD | 19:771bf61be276 | 492 | break; |
GaspardD | 19:771bf61be276 | 493 | case LOADING_SECTION: |
GaspardD | 19:771bf61be276 | 494 | if(p_sectionCourante != NULL) { //la section a ete chargee dans sectionOutput |
GaspardD | 19:771bf61be276 | 495 | st_tmpSection = RUNNING_SECTION; |
GaspardD | 19:771bf61be276 | 496 | } else { |
GaspardD | 19:771bf61be276 | 497 | st_tmpSection=ARRET; |
GaspardD | 19:771bf61be276 | 498 | } |
GaspardD | 19:771bf61be276 | 499 | break; |
GaspardD | 19:771bf61be276 | 500 | case ARRET: |
GaspardD | 19:771bf61be276 | 501 | if(p_sectionCourante != NULL) { |
GaspardD | 19:771bf61be276 | 502 | st_tmpSection = RUNNING_SECTION; |
GaspardD | 19:771bf61be276 | 503 | } else { |
GaspardD | 19:771bf61be276 | 504 | return; |
GaspardD | 19:771bf61be276 | 505 | } |
GaspardD | 19:771bf61be276 | 506 | break; |
GaspardD | 19:771bf61be276 | 507 | default: |
GaspardD | 19:771bf61be276 | 508 | break; |
GaspardD | 11:bc24b3ba51a9 | 509 | } |
GaspardD | 11:bc24b3ba51a9 | 510 | st_currentSection = st_tmpSection; |
GaspardD | 8:1d8c3ca5e508 | 511 | return; |
GaspardD | 8:1d8c3ca5e508 | 512 | } |
GaspardD | 8:1d8c3ca5e508 | 513 | |
GaspardD | 11:bc24b3ba51a9 | 514 | void maxSpeedUpdate(void) |
GaspardD | 8:1d8c3ca5e508 | 515 | { |
GaspardD | 14:d471faa7d1a2 | 516 | #if (DEBUG > 3) |
GaspardD | 14:d471faa7d1a2 | 517 | pc.printf("\r\nUpdate MaxSpeed\r\n"); |
GaspardD | 14:d471faa7d1a2 | 518 | #endif |
GaspardD | 11:bc24b3ba51a9 | 519 | |
GaspardD | 41:4dd36f607279 | 520 | if(st_maxSpeed == BLOCKED && strengthLidar > LIDAR_STRENGTH_THRESOLD && distLidar < p_sectionCourante->lidarWarningDist_cm) { |
GaspardD | 41:4dd36f607279 | 521 | st_tmpMaxSpeed = BLOCKED; |
GaspardD | 41:4dd36f607279 | 522 | } else if( strengthLidar > LIDAR_STRENGTH_THRESOLD ) { |
GaspardD | 23:04d393220daa | 523 | if( distLidar > p_sectionCourante->lidarWarningDist_cm ) { |
GaspardD | 19:771bf61be276 | 524 | st_tmpMaxSpeed = SPEED_MAX; |
GaspardD | 29:fc984fe08ca7 | 525 | } else if( strengthLidar > LIDAR_STRENGTH_THRESOLD && |
GaspardD | 24:698fefbbee00 | 526 | strengthLidarPrev > LIDAR_STRENGTH_THRESOLD && |
GaspardD | 24:698fefbbee00 | 527 | distLidar < p_sectionCourante->lidarWarningDist_cm && |
GaspardD | 41:4dd36f607279 | 528 | distLidarPrev-distLidar > 3) { |
GaspardD | 21:de7a0a47f8a3 | 529 | st_tmpMaxSpeed = BLOCKED; |
GaspardD | 41:4dd36f607279 | 530 | } else { |
GaspardD | 24:698fefbbee00 | 531 | st_tmpMaxSpeed = SPEED_WARNING; |
GaspardD | 24:698fefbbee00 | 532 | } |
GaspardD | 21:de7a0a47f8a3 | 533 | } else { |
GaspardD | 11:bc24b3ba51a9 | 534 | st_tmpMaxSpeed = SPEED_MAX; |
GaspardD | 11:bc24b3ba51a9 | 535 | } |
GaspardD | 11:bc24b3ba51a9 | 536 | |
GaspardD | 11:bc24b3ba51a9 | 537 | st_maxSpeed = st_tmpMaxSpeed; |
GaspardD | 11:bc24b3ba51a9 | 538 | return; |
GaspardD | 11:bc24b3ba51a9 | 539 | } |
GaspardD | 11:bc24b3ba51a9 | 540 | |
GaspardD | 11:bc24b3ba51a9 | 541 | void throttleUpdate(void) |
GaspardD | 11:bc24b3ba51a9 | 542 | { |
GaspardD | 14:d471faa7d1a2 | 543 | #if (DEBUG > 3) |
GaspardD | 14:d471faa7d1a2 | 544 | pc.printf("\r\nUpdate Throttle\r\n"); |
GaspardD | 14:d471faa7d1a2 | 545 | #endif |
GaspardD | 11:bc24b3ba51a9 | 546 | switch (st_thro) { |
GaspardD | 21:de7a0a47f8a3 | 547 | case REGULATION_SPEED: |
GaspardD | 24:698fefbbee00 | 548 | if( st_currentSection == ARRET || |
GaspardD | 41:4dd36f607279 | 549 | st_maxSpeed == BLOCKED ) { |
GaspardD | 19:771bf61be276 | 550 | st_tmpThro = BRAKING; |
GaspardD | 29:fc984fe08ca7 | 551 | } |
GaspardD | 27:f8c3f1524a64 | 552 | #ifdef FREINAGE_ADAPTATIF |
GaspardD | 41:4dd36f607279 | 553 | else if(tachySpeed_cmps > (maxSpeed_cmps + SPEED_DELTA_CMPS)) { |
GaspardD | 24:698fefbbee00 | 554 | st_tmpThro = BRAKING; |
GaspardD | 29:fc984fe08ca7 | 555 | brakingDurationNeeded_us = 10000.0 * MASSE_BINIOU_KG *((tachySpeed_cmps-maxSpeed_cmps)*(tachySpeed_cmps+maxSpeed_cmps)) / (PUISSANCE_FREINAGE_W *2) ;//vitesse en m/s et temps en us |
GaspardD | 24:698fefbbee00 | 556 | brakingTimer.reset(); |
GaspardD | 24:698fefbbee00 | 557 | brakingTimer.start(); |
GaspardD | 27:f8c3f1524a64 | 558 | } |
GaspardD | 27:f8c3f1524a64 | 559 | #endif |
GaspardD | 41:4dd36f607279 | 560 | else { |
GaspardD | 19:771bf61be276 | 561 | return; |
GaspardD | 19:771bf61be276 | 562 | } |
GaspardD | 19:771bf61be276 | 563 | break; |
GaspardD | 16:63690703b5b6 | 564 | case BRAKING: |
GaspardD | 42:3f12252862b9 | 565 | if(st_maxSpeed == BLOCKED && timersinceSerial.read_us() > 200000) { |
GaspardD | 42:3f12252862b9 | 566 | p_sectionCourante= NULL; |
GaspardD | 42:3f12252862b9 | 567 | st_tmpThro = STOPPED; |
GaspardD | 42:3f12252862b9 | 568 | } else if( st_maxSpeed == BLOCKED |
GaspardD | 41:4dd36f607279 | 569 | #ifdef FREINAGE_ADAPTATIF |
GaspardD | 42:3f12252862b9 | 570 | || brakingTimer.read_us() < brakingDurationNeeded_us |
GaspardD | 41:4dd36f607279 | 571 | #endif |
GaspardD | 42:3f12252862b9 | 572 | ) { |
GaspardD | 23:04d393220daa | 573 | st_tmpThro = BRAKING; |
GaspardD | 41:4dd36f607279 | 574 | } else if(st_currentSection == ARRET) { |
GaspardD | 21:de7a0a47f8a3 | 575 | st_tmpThro = STOPPED; |
GaspardD | 41:4dd36f607279 | 576 | } else { |
GaspardD | 41:4dd36f607279 | 577 | #ifdef FREINAGE_ADAPTATIF |
GaspardD | 24:698fefbbee00 | 578 | brakingDurationNeeded_us = 0; |
GaspardD | 41:4dd36f607279 | 579 | #endif |
GaspardD | 21:de7a0a47f8a3 | 580 | st_tmpThro = REGULATION_SPEED; |
GaspardD | 21:de7a0a47f8a3 | 581 | } |
GaspardD | 19:771bf61be276 | 582 | break; |
GaspardD | 19:771bf61be276 | 583 | case STOPPED: |
GaspardD | 19:771bf61be276 | 584 | if(st_currentSection == RUNNING_SECTION) { |
GaspardD | 22:26fc6e6f7a55 | 585 | st_tmpThro = REGULATION_SPEED; |
GaspardD | 19:771bf61be276 | 586 | } else { |
GaspardD | 19:771bf61be276 | 587 | st_tmpThro = STOPPED; |
GaspardD | 16:63690703b5b6 | 588 | } |
GaspardD | 19:771bf61be276 | 589 | break; |
GaspardD | 19:771bf61be276 | 590 | default: |
GaspardD | 19:771bf61be276 | 591 | break; |
GaspardD | 11:bc24b3ba51a9 | 592 | } |
GaspardD | 11:bc24b3ba51a9 | 593 | st_thro = st_tmpThro; |
GaspardD | 8:1d8c3ca5e508 | 594 | return; |
GaspardD | 8:1d8c3ca5e508 | 595 | } |
GaspardD | 2:fd0ffe46a87d | 596 | |
GaspardD | 8:1d8c3ca5e508 | 597 | //########## OUTPUT STATES ########## |
GaspardD | 11:bc24b3ba51a9 | 598 | //updating output parameters |
GaspardD | 11:bc24b3ba51a9 | 599 | void mursOutput(void) |
GaspardD | 12:51b1b40cc017 | 600 | { |
GaspardD | 29:fc984fe08ca7 | 601 | //pour dériver |
GaspardD | 29:fc984fe08ca7 | 602 | positionSurPiste45Prev = positionSurPiste45; |
GaspardD | 29:fc984fe08ca7 | 603 | positionSurPiste90Prev = positionSurPiste90; |
GaspardD | 14:d471faa7d1a2 | 604 | #if (DEBUG > 3) |
GaspardD | 14:d471faa7d1a2 | 605 | pc.printf("\r\n Output MURS\r\n"); |
GaspardD | 14:d471faa7d1a2 | 606 | #endif |
GaspardD | 23:04d393220daa | 607 | |
GaspardD | 29:fc984fe08ca7 | 608 | switch (st_murs) { |
GaspardD | 41:4dd36f607279 | 609 | case REF_A_GAUCHE://par defaut, on compte à partir de la bordure gauche |
GaspardD | 41:4dd36f607279 | 610 | largeurPiste90 = 150.0; |
GaspardD | 41:4dd36f607279 | 611 | positionSurPiste90 = ( trueDistMurG90Moy + DEMI_LARGEUR_BINIOU_CM ); |
GaspardD | 41:4dd36f607279 | 612 | positionSurPiste45 = ( distMurG45Moy * 1.414214/2.0 ) + DEMI_LARGEUR_BINIOU_CM; |
GaspardD | 39:de3638276b7e | 613 | #ifdef OMAR |
GaspardD | 41:4dd36f607279 | 614 | pc.printf("REF_A_GAUCHE\r\n"); |
GaspardD | 39:de3638276b7e | 615 | #endif |
GaspardD | 41:4dd36f607279 | 616 | break; |
GaspardD | 41:4dd36f607279 | 617 | case REF_A_DROITE://par defaut, on compte à partir de la bordure gauche |
GaspardD | 41:4dd36f607279 | 618 | largeurPiste90 = 150.0; |
GaspardD | 41:4dd36f607279 | 619 | positionSurPiste90 = largeurPiste90 - ( DEMI_LARGEUR_BINIOU_CM + trueDistMurD90Moy ) ; |
GaspardD | 41:4dd36f607279 | 620 | positionSurPiste45 = largeurPiste90 - ( distMurD45Moy * (1.414214)/2 ) + DEMI_LARGEUR_BINIOU_CM; |
GaspardD | 39:de3638276b7e | 621 | #ifdef OMAR |
GaspardD | 41:4dd36f607279 | 622 | pc.printf("REF_A_DROITE\r\n"); |
GaspardD | 39:de3638276b7e | 623 | #endif |
GaspardD | 41:4dd36f607279 | 624 | break; |
GaspardD | 41:4dd36f607279 | 625 | default://REF_BIDIR |
GaspardD | 41:4dd36f607279 | 626 | largeurPiste90 = trueDistMurG90Moy + trueDistMurD90Moy ; |
GaspardD | 41:4dd36f607279 | 627 | largeurPiste45 = distMurG45Moy + distMurD45Moy ; |
GaspardD | 41:4dd36f607279 | 628 | positionSurPiste90 = (trueDistMurG90Moy ); |
GaspardD | 41:4dd36f607279 | 629 | positionSurPiste45 = (distMurG45Moy); |
GaspardD | 39:de3638276b7e | 630 | #ifdef OMAR |
GaspardD | 41:4dd36f607279 | 631 | pc.printf("REF_BIDIR\r\n"); |
GaspardD | 39:de3638276b7e | 632 | #endif |
GaspardD | 41:4dd36f607279 | 633 | break; |
GaspardD | 29:fc984fe08ca7 | 634 | } |
GaspardD | 23:04d393220daa | 635 | |
GaspardD | 22:26fc6e6f7a55 | 636 | |
GaspardD | 21:de7a0a47f8a3 | 637 | //deriv correction |
GaspardD | 29:fc984fe08ca7 | 638 | derive45 = positionSurPiste45 - positionSurPiste45Prev; |
GaspardD | 29:fc984fe08ca7 | 639 | derive90 = positionSurPiste90 - positionSurPiste90Prev; |
GaspardD | 39:de3638276b7e | 640 | |
GaspardD | 39:de3638276b7e | 641 | #ifdef OMAR/* |
GaspardD | 39:de3638276b7e | 642 | pc.printf("derive45 => %.4lf \r\n ",derive45); |
GaspardD | 39:de3638276b7e | 643 | pc.printf("derive90 => %.4lf \r\n ",derive90); |
GaspardD | 39:de3638276b7e | 644 | pc.printf("distMurG45Moy => %.4lf \r\n ",distMurG45Moy); |
GaspardD | 39:de3638276b7e | 645 | pc.printf("distMurD45Moy => %.4lf \r\n ",distMurD45Moy); |
GaspardD | 39:de3638276b7e | 646 | pc.printf("trueDistMurG90Moy => %.4lf \r\n ",trueDistMurG90Moy); |
GaspardD | 39:de3638276b7e | 647 | pc.printf("trueDistMurD90Moy => %.4lf \r\n ",trueDistMurD90Moy); |
GaspardD | 39:de3638276b7e | 648 | pc.printf("positionSurPiste90 => %.4lf \r\n ",positionSurPiste90); |
GaspardD | 39:de3638276b7e | 649 | pc.printf("positionSurPiste45 => %.4lf \r\n ",positionSurPiste45); |
GaspardD | 39:de3638276b7e | 650 | pc.printf("largeurPiste90 => %.4lf \r\n ",largeurPiste90);*/ |
GaspardD | 39:de3638276b7e | 651 | #endif |
GaspardD | 39:de3638276b7e | 652 | |
GaspardD | 41:4dd36f607279 | 653 | //integral correction |
GaspardD | 39:de3638276b7e | 654 | lastDifferences90[lastDifferenceIndex] = (p_sectionCourante->consigne_position - positionSurPiste90); |
GaspardD | 39:de3638276b7e | 655 | integralSum=0; |
GaspardD | 39:de3638276b7e | 656 | for(int f=0; f<NB_INTEGRAL_SAMPLES; f++) { |
GaspardD | 39:de3638276b7e | 657 | integralSum+=lastDifferences90[f]; |
GaspardD | 39:de3638276b7e | 658 | } |
GaspardD | 39:de3638276b7e | 659 | lastDifferenceIndex = (lastDifferenceIndex + 1)%NB_INTEGRAL_SAMPLES; |
GaspardD | 39:de3638276b7e | 660 | |
GaspardD | 41:4dd36f607279 | 661 | |
GaspardD | 41:4dd36f607279 | 662 | |
GaspardD | 41:4dd36f607279 | 663 | |
GaspardD | 21:de7a0a47f8a3 | 664 | //application des coefficients |
GaspardD | 41:4dd36f607279 | 665 | |
GaspardD | 41:4dd36f607279 | 666 | pulseDirection_us = (int)( |
GaspardD | 41:4dd36f607279 | 667 | ((positionSurPiste45 - (largeurPiste45/2.0)) * p_sectionCourante->coef_p) |
GaspardD | 41:4dd36f607279 | 668 | + ((positionSurPiste90 - (largeurPiste90/2.0)) * p_sectionCourante->coef_p) |
GaspardD | 41:4dd36f607279 | 669 | + ( -derive45 * p_sectionCourante->coef_d) |
GaspardD | 41:4dd36f607279 | 670 | + ( -derive90 * p_sectionCourante->coef_d) |
GaspardD | 41:4dd36f607279 | 671 | + ( integralSum * p_sectionCourante->coef_i) |
GaspardD | 41:4dd36f607279 | 672 | )* (300.0/(tachySpeed_cmps+1.0)) // faire du test reel pour afiné ou un calcul |
GaspardD | 41:4dd36f607279 | 673 | + DIRECTION_PULSE_MIDDLE; |
GaspardD | 41:4dd36f607279 | 674 | |
GaspardD | 41:4dd36f607279 | 675 | |
GaspardD | 39:de3638276b7e | 676 | #ifdef OMAR |
GaspardD | 41:4dd36f607279 | 677 | pc.printf("p_sectionCourante->consigne_position => %.4lf \r\n ",p_sectionCourante->consigne_position); |
GaspardD | 41:4dd36f607279 | 678 | pc.printf("positionSurPiste45 => %.4lf \r\n ",positionSurPiste45); |
GaspardD | 41:4dd36f607279 | 679 | pc.printf("p_sectionCourante->coef_p => %.4lf \r\n ",p_sectionCourante->coef_p); |
GaspardD | 39:de3638276b7e | 680 | #endif |
GaspardD | 21:de7a0a47f8a3 | 681 | //gestioon du dépassement |
GaspardD | 21:de7a0a47f8a3 | 682 | if(pulseDirection_us > DIRECTION_PULSE_MAX) { //POUR TOURNER A GAUCHE |
GaspardD | 19:771bf61be276 | 683 | #if DEBUG > 1 |
ohlimi2 | 36:bccddd02966a | 684 | pc.printf("!!! OVER PWM Direction pulse: %.4lf\r\n",pulseDirection_us); |
GaspardD | 21:de7a0a47f8a3 | 685 | #endif |
GaspardD | 21:de7a0a47f8a3 | 686 | pulseDirection_us = DIRECTION_PULSE_MAX; |
GaspardD | 21:de7a0a47f8a3 | 687 | } else if(pulseDirection_us < DIRECTION_PULSE_MIN ) { //POUR TOURNER A DROITE |
GaspardD | 21:de7a0a47f8a3 | 688 | #if DEBUG > 1 |
ohlimi2 | 36:bccddd02966a | 689 | pc.printf("!!! UNDER PWM Direction pulse: %.4lf\r\n",pulseDirection_us); |
GaspardD | 19:771bf61be276 | 690 | #endif |
GaspardD | 21:de7a0a47f8a3 | 691 | pulseDirection_us = DIRECTION_PULSE_MIN ; |
GaspardD | 21:de7a0a47f8a3 | 692 | } |
GaspardD | 21:de7a0a47f8a3 | 693 | #if DEBUG > 1 |
ohlimi2 | 36:bccddd02966a | 694 | pc.printf("PWM Direction pulse: %.4lf\r\n",pulseDirection_us); |
GaspardD | 21:de7a0a47f8a3 | 695 | #endif |
GaspardD | 21:de7a0a47f8a3 | 696 | PwmDirection.pulsewidth_us(pulseDirection_us); |
GaspardD | 8:1d8c3ca5e508 | 697 | return; |
GaspardD | 8:1d8c3ca5e508 | 698 | } |
GaspardD | 8:1d8c3ca5e508 | 699 | |
GaspardD | 11:bc24b3ba51a9 | 700 | #ifdef DLVV |
GaspardD | 11:bc24b3ba51a9 | 701 | void obstacleOutput(void) |
GaspardD | 8:1d8c3ca5e508 | 702 | { |
GaspardD | 8:1d8c3ca5e508 | 703 | return; |
GaspardD | 8:1d8c3ca5e508 | 704 | } |
GaspardD | 11:bc24b3ba51a9 | 705 | #endif |
GaspardD | 11:bc24b3ba51a9 | 706 | void sectionOutput(void) |
GaspardD | 8:1d8c3ca5e508 | 707 | { |
GaspardD | 14:d471faa7d1a2 | 708 | #if (DEBUG > 3) |
GaspardD | 14:d471faa7d1a2 | 709 | pc.printf("\r\n Output Section\r\n"); |
GaspardD | 14:d471faa7d1a2 | 710 | #endif |
GaspardD | 11:bc24b3ba51a9 | 711 | switch (st_currentSection) { |
GaspardD | 19:771bf61be276 | 712 | case RUNNING_SECTION: |
GaspardD | 19:771bf61be276 | 713 | break; |
GaspardD | 19:771bf61be276 | 714 | case LOADING_SECTION: |
GaspardD | 19:771bf61be276 | 715 | p_sectionCourante=p_sectionCourante->nextSection; |
GaspardD | 19:771bf61be276 | 716 | tachySectionDist_cm = 0; |
GaspardD | 19:771bf61be276 | 717 | break; |
GaspardD | 19:771bf61be276 | 718 | case ARRET: |
GaspardD | 19:771bf61be276 | 719 | //on est à l'arret |
GaspardD | 19:771bf61be276 | 720 | break; |
GaspardD | 19:771bf61be276 | 721 | default: |
GaspardD | 19:771bf61be276 | 722 | break; |
GaspardD | 11:bc24b3ba51a9 | 723 | } |
GaspardD | 8:1d8c3ca5e508 | 724 | return; |
GaspardD | 8:1d8c3ca5e508 | 725 | } |
GaspardD | 8:1d8c3ca5e508 | 726 | |
GaspardD | 11:bc24b3ba51a9 | 727 | |
GaspardD | 11:bc24b3ba51a9 | 728 | void maxSpeedOutput(void) |
GaspardD | 8:1d8c3ca5e508 | 729 | { |
GaspardD | 14:d471faa7d1a2 | 730 | #if (DEBUG > 3) |
GaspardD | 14:d471faa7d1a2 | 731 | pc.printf("\r\n Output MAX SPEED\r\n"); |
GaspardD | 14:d471faa7d1a2 | 732 | #endif |
GaspardD | 19:771bf61be276 | 733 | switch(st_maxSpeed) { |
GaspardD | 21:de7a0a47f8a3 | 734 | case BLOCKED: |
GaspardD | 41:4dd36f607279 | 735 | maxSpeed_cmps = 0; |
GaspardD | 19:771bf61be276 | 736 | break; |
GaspardD | 21:de7a0a47f8a3 | 737 | case SPEED_WARNING: |
GaspardD | 24:698fefbbee00 | 738 | maxSpeed_cmps = p_sectionCourante->slowSpeed_cmps; |
GaspardD | 19:771bf61be276 | 739 | break; |
GaspardD | 19:771bf61be276 | 740 | default: |
GaspardD | 19:771bf61be276 | 741 | maxSpeed_cmps = p_sectionCourante->targetSpeed_cmps; |
GaspardD | 19:771bf61be276 | 742 | break; |
GaspardD | 11:bc24b3ba51a9 | 743 | } |
GaspardD | 8:1d8c3ca5e508 | 744 | return; |
GaspardD | 8:1d8c3ca5e508 | 745 | } |
GaspardD | 11:bc24b3ba51a9 | 746 | |
GaspardD | 11:bc24b3ba51a9 | 747 | void throttleOutput(void) |
GaspardD | 11:bc24b3ba51a9 | 748 | { |
GaspardD | 14:d471faa7d1a2 | 749 | #if (DEBUG > 3) |
GaspardD | 14:d471faa7d1a2 | 750 | pc.printf("\r\n Output TROTTLE\r\n"); |
GaspardD | 14:d471faa7d1a2 | 751 | #endif |
GaspardD | 11:bc24b3ba51a9 | 752 | switch (st_thro) { |
GaspardD | 21:de7a0a47f8a3 | 753 | case REGULATION_SPEED: |
GaspardD | 22:26fc6e6f7a55 | 754 | pulseSpeed_us = maxSpeed_cmps * 279 / 2048 + 1558 ; |
GaspardD | 19:771bf61be276 | 755 | break; |
GaspardD | 19:771bf61be276 | 756 | case BRAKING: |
GaspardD | 16:63690703b5b6 | 757 | #if DEBUG > 2 |
GaspardD | 19:771bf61be276 | 758 | pc.printf("BRAKINGGGGGGGGGGGGGGGGGG !!! \r\n"); |
GaspardD | 19:771bf61be276 | 759 | #endif |
GaspardD | 19:771bf61be276 | 760 | pulseSpeed_us = BRAKING_PULSE_US; |
GaspardD | 19:771bf61be276 | 761 | break; |
GaspardD | 19:771bf61be276 | 762 | case STOPPED: |
GaspardD | 19:771bf61be276 | 763 | #if DEBUG > 2 |
GaspardD | 19:771bf61be276 | 764 | pc.printf("STOPPED\r\n"); |
GaspardD | 16:63690703b5b6 | 765 | #endif |
GaspardD | 19:771bf61be276 | 766 | pulseSpeed_us = ZERO_PULSE_SPEED_US; |
GaspardD | 19:771bf61be276 | 767 | break; |
GaspardD | 19:771bf61be276 | 768 | default: |
GaspardD | 19:771bf61be276 | 769 | break; |
GaspardD | 11:bc24b3ba51a9 | 770 | } |
GaspardD | 22:26fc6e6f7a55 | 771 | |
GaspardD | 19:771bf61be276 | 772 | PwmMotor.pulsewidth_us(pulseSpeed_us); |
GaspardD | 15:129f205ff030 | 773 | #if DEBUG > 1 |
ohlimi2 | 36:bccddd02966a | 774 | pc.printf("PWM Thro pulse: %.4lf micros\r\n",pulseSpeed_us); |
GaspardD | 13:af9a59ccf60b | 775 | #endif |
GaspardD | 11:bc24b3ba51a9 | 776 | return; |
GaspardD | 11:bc24b3ba51a9 | 777 | } |