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