Hlimi Omar
/
biniou
TRR2018 omar
Fork of biniou by
stateMachines.cpp@41:4dd36f607279, 2018-09-19 (annotated)
- Committer:
- GaspardD
- Date:
- Wed Sep 19 13:42:04 2018 +0000
- Revision:
- 41:4dd36f607279
- Parent:
- 40:b9450d753782
- Child:
- 42:3f12252862b9
plus propre
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 | 41:4dd36f607279 | 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 | 41:4dd36f607279 | 229 | for(int h=0; h<NB_INTEGRAL_SAMPLES; h++) { |
GaspardD | 29:fc984fe08ca7 | 230 | lastDifferences90[h] = 0; |
GaspardD | 29:fc984fe08ca7 | 231 | } |
GaspardD | 29:fc984fe08ca7 | 232 | return; |
GaspardD | 29:fc984fe08ca7 | 233 | } |
GaspardD | 29:fc984fe08ca7 | 234 | |
GaspardD | 21:de7a0a47f8a3 | 235 | |
GaspardD | 29:fc984fe08ca7 | 236 | double getShortDistMoy(AnalogIn* p,double* tab,int size) |
GaspardD | 11:bc24b3ba51a9 | 237 | { |
GaspardD | 29:fc984fe08ca7 | 238 | tab[index_fifo_ir] = 3.3 * (double)p->read(); // on convertit directement en volts |
GaspardD | 29:fc984fe08ca7 | 239 | //tension proportionelle à l'inverse de la distance en dessous de 2V |
GaspardD | 41:4dd36f607279 | 240 | if(tab[index_fifo_ir]< 0.1034) { |
GaspardD | 41:4dd36f607279 | 241 | tab[index_fifo_ir]= 0.11; |
GaspardD | 41:4dd36f607279 | 242 | } |
GaspardD | 39:de3638276b7e | 243 | tab[index_fifo_ir] = (11.3531/(tab[index_fifo_ir]-0.1034))-0.42; |
GaspardD | 29:fc984fe08ca7 | 244 | |
GaspardD | 29:fc984fe08ca7 | 245 | int sumMoy = 0; |
GaspardD | 29:fc984fe08ca7 | 246 | for(int k=0; k<size; k++) { |
GaspardD | 29:fc984fe08ca7 | 247 | sumMoy+=tab[k]; |
GaspardD | 29:fc984fe08ca7 | 248 | } |
GaspardD | 29:fc984fe08ca7 | 249 | return sumMoy/size; |
GaspardD | 29:fc984fe08ca7 | 250 | } |
GaspardD | 29:fc984fe08ca7 | 251 | |
GaspardD | 29:fc984fe08ca7 | 252 | double getDistMoy(AnalogIn* p,double* tab,int size) |
GaspardD | 29:fc984fe08ca7 | 253 | { |
GaspardD | 29:fc984fe08ca7 | 254 | tab[index_fifo_ir] = 3.3 * (double)p->read(); // on convertit directement en volts |
GaspardD | 29:fc984fe08ca7 | 255 | //tension proportionelle à l'inverse de la distance |
ohlimi2 | 36:bccddd02966a | 256 | tab[index_fifo_ir] = 1.0/(0.0161 * (double)tab[index_fifo_ir]); |
GaspardD | 11:bc24b3ba51a9 | 257 | int sumMoy = 0; |
GaspardD | 19:771bf61be276 | 258 | for(int k=0; k<size; k++) { |
GaspardD | 11:bc24b3ba51a9 | 259 | sumMoy+=tab[k]; |
GaspardD | 11:bc24b3ba51a9 | 260 | } |
GaspardD | 11:bc24b3ba51a9 | 261 | return sumMoy/size; |
GaspardD | 11:bc24b3ba51a9 | 262 | } |
GaspardD | 39:de3638276b7e | 263 | void tachyCheck() |
GaspardD | 39:de3638276b7e | 264 | { |
GaspardD | 41:4dd36f607279 | 265 | if(timerSinceTachy.read_us() > 500000) { |
GaspardD | 39:de3638276b7e | 266 | tachySpeed_cmps = 0.0; |
GaspardD | 39:de3638276b7e | 267 | } |
GaspardD | 41:4dd36f607279 | 268 | return; |
GaspardD | 39:de3638276b7e | 269 | } |
GaspardD | 39:de3638276b7e | 270 | |
GaspardD | 2:fd0ffe46a87d | 271 | |
GaspardD | 12:51b1b40cc017 | 272 | void it4cm() |
GaspardD | 12:51b1b40cc017 | 273 | { |
GaspardD | 39:de3638276b7e | 274 | //le timer sert de flag sur le freinage: |
GaspardD | 39:de3638276b7e | 275 | //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 | 276 | //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 | 277 | |
ohlimi2 | 36:bccddd02966a | 278 | tachySectionDist_cm += TACHY_CM; |
ohlimi2 | 36:bccddd02966a | 279 | tachyTotalDist_cm += TACHY_CM; |
GaspardD | 39:de3638276b7e | 280 | 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 | 281 | #if DEBUG > 2 |
ohlimi2 | 36:bccddd02966a | 282 | pc.printf("IT: distance parcourue %.4lf , vitesse:%.4lf \r\n",tachyTotalDist_cm,tachySpeed_cmps); |
ohlimi2 | 36:bccddd02966a | 283 | #endif |
GaspardD | 39:de3638276b7e | 284 | timerSinceTachy.reset(); |
GaspardD | 39:de3638276b7e | 285 | timerSinceTachy.start(); |
ohlimi2 | 36:bccddd02966a | 286 | return; |
ohlimi2 | 36:bccddd02966a | 287 | |
GaspardD | 19:771bf61be276 | 288 | #if DEBUG > 0 |
GaspardD | 14:d471faa7d1a2 | 289 | pc.printf("IT tachy\r\n"); |
GaspardD | 14:d471faa7d1a2 | 290 | #endif |
GaspardD | 12:51b1b40cc017 | 291 | } |
GaspardD | 12:51b1b40cc017 | 292 | |
GaspardD | 12:51b1b40cc017 | 293 | void it_serial() |
GaspardD | 12:51b1b40cc017 | 294 | { |
GaspardD | 12:51b1b40cc017 | 295 | if(serialLidar.getc()==HEADER) { // determine data package frame header 0x59 |
GaspardD | 12:51b1b40cc017 | 296 | uart[0]=HEADER; |
GaspardD | 12:51b1b40cc017 | 297 | if(serialLidar.getc()==HEADER) { //determine data package frame header 0x59 |
GaspardD | 12:51b1b40cc017 | 298 | uart[1]=HEADER; |
GaspardD | 12:51b1b40cc017 | 299 | for(i=2; i<9; i++) { // store data to array |
GaspardD | 12:51b1b40cc017 | 300 | uart[i]=serialLidar.getc(); |
GaspardD | 12:51b1b40cc017 | 301 | } |
GaspardD | 12:51b1b40cc017 | 302 | check=uart[0]+uart[1]+uart[2]+uart[3]+uart[4]+uart[5]+uart[6]+uart[7]; |
GaspardD | 12:51b1b40cc017 | 303 | if(uart[8]==(check&0xff)) { // check the received data as per protocols |
GaspardD | 23:04d393220daa | 304 | distLidarPrev = distLidar; |
GaspardD | 12:51b1b40cc017 | 305 | distLidar=uart[2]+uart[3]*256;// calculate distance value |
GaspardD | 23:04d393220daa | 306 | strengthLidarPrev = strengthLidar; |
GaspardD | 12:51b1b40cc017 | 307 | strengthLidar=uart[4]+uart[5]*256;// calculate signal strength value |
GaspardD | 12:51b1b40cc017 | 308 | } |
GaspardD | 12:51b1b40cc017 | 309 | } |
GaspardD | 12:51b1b40cc017 | 310 | } |
GaspardD | 12:51b1b40cc017 | 311 | } |
GaspardD | 19:771bf61be276 | 312 | // +++++++++++++++++++++++++++++++++++++++++++ STATES MACHINES +++++++++++++++++++++++++++++++++++++++++++ |
GaspardD | 19:771bf61be276 | 313 | |
GaspardD | 8:1d8c3ca5e508 | 314 | //########## INIT STATES MACHINES ########## |
GaspardD | 11:bc24b3ba51a9 | 315 | void mursInit(void) |
GaspardD | 8:1d8c3ca5e508 | 316 | { |
GaspardD | 39:de3638276b7e | 317 | #if DEBUG > 0 |
GaspardD | 13:af9a59ccf60b | 318 | pc.printf("Init Murs\r\n"); |
GaspardD | 13:af9a59ccf60b | 319 | #endif |
GaspardD | 13:af9a59ccf60b | 320 | timeSinceStart.start(); |
GaspardD | 29:fc984fe08ca7 | 321 | st_murs=REF_BIDIR; |
GaspardD | 19:771bf61be276 | 322 | PwmDirection.period_us(SPEED_PERIOD_US); |
GaspardD | 17:8c465656eea4 | 323 | PwmDirection.pulsewidth_us(DIRECTION_PULSE_MIDDLE);// milieu |
GaspardD | 8:1d8c3ca5e508 | 324 | return; |
GaspardD | 8:1d8c3ca5e508 | 325 | } |
GaspardD | 11:bc24b3ba51a9 | 326 | #ifdef DLVV |
GaspardD | 11:bc24b3ba51a9 | 327 | void obstacleInit(void) |
GaspardD | 8:1d8c3ca5e508 | 328 | { |
GaspardD | 19:771bf61be276 | 329 | #if DEBUG > 0 |
GaspardD | 13:af9a59ccf60b | 330 | pc.printf("Init Obstacle\r\n"); |
GaspardD | 13:af9a59ccf60b | 331 | #endif |
GaspardD | 29:fc984fe08ca7 | 332 | st_speedLimit=ALL_CLEAR; |
GaspardD | 8:1d8c3ca5e508 | 333 | return; |
GaspardD | 8:1d8c3ca5e508 | 334 | } |
GaspardD | 11:bc24b3ba51a9 | 335 | #endif |
GaspardD | 11:bc24b3ba51a9 | 336 | void sectionInit(void) |
GaspardD | 2:fd0ffe46a87d | 337 | { |
GaspardD | 19:771bf61be276 | 338 | #if DEBUG > 0 |
GaspardD | 13:af9a59ccf60b | 339 | pc.printf("Init Section\r\n"); |
GaspardD | 13:af9a59ccf60b | 340 | #endif |
GaspardD | 11:bc24b3ba51a9 | 341 | st_currentSection=ARRET; |
GaspardD | 14:d471faa7d1a2 | 342 | p_sectionCourante=&p_section1; |
GaspardD | 12:51b1b40cc017 | 343 | it_tachymeter.fall(&it4cm); |
GaspardD | 39:de3638276b7e | 344 | timerSinceTachy.start(); |
GaspardD | 39:de3638276b7e | 345 | tachySectionDist_cm = 0; |
GaspardD | 39:de3638276b7e | 346 | tachyStepsRegister = 0; |
GaspardD | 11:bc24b3ba51a9 | 347 | |
GaspardD | 23:04d393220daa | 348 | //section de test 1 |
GaspardD | 23:04d393220daa | 349 | p_section1.nextSection =NULL;// &p_section2; |
GaspardD | 29:fc984fe08ca7 | 350 | p_section1.consigne_position = 75.0; |
GaspardD | 39:de3638276b7e | 351 | p_section1.targetSpeed_cmps = 400.0; |
GaspardD | 40:b9450d753782 | 352 | p_section1.targetSpeed_cmps = 500.0; |
GaspardD | 29:fc984fe08ca7 | 353 | p_section1.slowSpeed_cmps = 328.0; |
GaspardD | 21:de7a0a47f8a3 | 354 | p_section1.coef_p_speed = 1; |
GaspardD | 29:fc984fe08ca7 | 355 | p_section1.lidarWarningDist_cm = 120.0; |
GaspardD | 40:b9450d753782 | 356 | p_section1.lng_section_cm = 2000.0;//20m |
GaspardD | 40:b9450d753782 | 357 | p_section1.coef_p = 0.40; |
GaspardD | 40:b9450d753782 | 358 | p_section1.coef_d = 0.05; |
GaspardD | 40:b9450d753782 | 359 | p_section1.coef_i = 0.001; |
GaspardD | 39:de3638276b7e | 360 | |
GaspardD | 39:de3638276b7e | 361 | //section de test |
GaspardD | 39:de3638276b7e | 362 | p_section2.nextSection = NULL; |
GaspardD | 39:de3638276b7e | 363 | p_section2.consigne_position = 75.0; |
GaspardD | 39:de3638276b7e | 364 | p_section2.targetSpeed_cmps = 328.0; |
GaspardD | 39:de3638276b7e | 365 | p_section2.slowSpeed_cmps = 328.0; |
GaspardD | 39:de3638276b7e | 366 | p_section2.coef_p_speed = 1.0; |
GaspardD | 39:de3638276b7e | 367 | p_section2.lidarWarningDist_cm = 300.0; |
GaspardD | 39:de3638276b7e | 368 | p_section2.lng_section_cm = 200.0;//2m |
GaspardD | 39:de3638276b7e | 369 | p_section2.coef_p = 1; |
GaspardD | 39:de3638276b7e | 370 | p_section2.coef_i = 0.00001; |
GaspardD | 39:de3638276b7e | 371 | p_section2.coef_d = 0.00001; |
GaspardD | 23:04d393220daa | 372 | |
GaspardD | 8:1d8c3ca5e508 | 373 | return; |
GaspardD | 8:1d8c3ca5e508 | 374 | } |
GaspardD | 11:bc24b3ba51a9 | 375 | |
GaspardD | 11:bc24b3ba51a9 | 376 | void maxSpeedInit(void) |
GaspardD | 11:bc24b3ba51a9 | 377 | { |
GaspardD | 19:771bf61be276 | 378 | #if DEBUG > 0 |
GaspardD | 13:af9a59ccf60b | 379 | pc.printf("Init Max Speed\r\n"); |
GaspardD | 13:af9a59ccf60b | 380 | #endif |
GaspardD | 11:bc24b3ba51a9 | 381 | st_maxSpeed=SPEED_MAX; |
GaspardD | 11:bc24b3ba51a9 | 382 | maxSpeed_cmps= p_sectionCourante->targetSpeed_cmps; |
GaspardD | 11:bc24b3ba51a9 | 383 | serialLidar.baud(115200); |
GaspardD | 14:d471faa7d1a2 | 384 | serialLidar.attach(&it_serial); |
GaspardD | 11:bc24b3ba51a9 | 385 | return; |
GaspardD | 11:bc24b3ba51a9 | 386 | } |
GaspardD | 11:bc24b3ba51a9 | 387 | |
GaspardD | 11:bc24b3ba51a9 | 388 | void throttleInit(void) |
GaspardD | 8:1d8c3ca5e508 | 389 | { |
GaspardD | 19:771bf61be276 | 390 | #if DEBUG > 0 |
GaspardD | 13:af9a59ccf60b | 391 | pc.printf("Init Throttle\r\n"); |
GaspardD | 13:af9a59ccf60b | 392 | #endif |
GaspardD | 21:de7a0a47f8a3 | 393 | st_thro = REGULATION_SPEED; |
ohlimi2 | 36:bccddd02966a | 394 | PwmMotor.period_us(DIRECTION_PERIOD_MS); //20 ms is default |
GaspardD | 19:771bf61be276 | 395 | PwmMotor.pulsewidth_us(1000);//MIN |
GaspardD | 12:51b1b40cc017 | 396 | wait(3); |
GaspardD | 19:771bf61be276 | 397 | PwmMotor.pulsewidth_us(2000);//MAX |
GaspardD | 12:51b1b40cc017 | 398 | wait(1); |
GaspardD | 19:771bf61be276 | 399 | PwmMotor.pulsewidth_us(1500);//ZEROING |
GaspardD | 12:51b1b40cc017 | 400 | wait(1); |
GaspardD | 19:771bf61be276 | 401 | pulseSpeed_us = INITAL_PULSE_SPEED_US; |
GaspardD | 19:771bf61be276 | 402 | #if DEBUG > 0 |
ohlimi2 | 36:bccddd02966a | 403 | pc.printf("temps init: %.4lf micros\r\n",timeSinceStart.read_us()); |
ohlimi2 | 36:bccddd02966a | 404 | 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 | 405 | timeSinceStart.reset(); |
GaspardD | 13:af9a59ccf60b | 406 | timeSinceStart.start(); |
GaspardD | 13:af9a59ccf60b | 407 | #endif |
GaspardD | 23:04d393220daa | 408 | #ifdef DUMP_SAMPLIG_PERIOD |
GaspardD | 22:26fc6e6f7a55 | 409 | timerLog.start(); |
GaspardD | 22:26fc6e6f7a55 | 410 | #endif |
GaspardD | 22:26fc6e6f7a55 | 411 | |
GaspardD | 8:1d8c3ca5e508 | 412 | return; |
GaspardD | 8:1d8c3ca5e508 | 413 | } |
GaspardD | 2:fd0ffe46a87d | 414 | |
GaspardD | 8:1d8c3ca5e508 | 415 | |
GaspardD | 8:1d8c3ca5e508 | 416 | //########## UPDATE STATES ########## |
GaspardD | 11:bc24b3ba51a9 | 417 | void mursUpdate(void) |
GaspardD | 2:fd0ffe46a87d | 418 | { |
GaspardD | 14:d471faa7d1a2 | 419 | #if (DEBUG > 3) |
GaspardD | 14:d471faa7d1a2 | 420 | pc.printf("\r\nUpdate MURS\r\n"); |
GaspardD | 14:d471faa7d1a2 | 421 | #endif |
GaspardD | 11:bc24b3ba51a9 | 422 | //lectures |
GaspardD | 29:fc984fe08ca7 | 423 | |
GaspardD | 29:fc984fe08ca7 | 424 | distMurG45Moy = getDistMoy(&anaG45,distMurG45,NB_ECHANTILLONS_IR); |
GaspardD | 29:fc984fe08ca7 | 425 | distMurD45Moy = getDistMoy(&anaD45,distMurD45,NB_ECHANTILLONS_IR); |
GaspardD | 29:fc984fe08ca7 | 426 | distMurG90Moy = getDistMoy(&anaG90,distMurG90,NB_ECHANTILLONS_IR); |
ohlimi2 | 36:bccddd02966a | 427 | distMurD90Moy = getDistMoy(&anaD90,distMurD90,NB_ECHANTILLONS_IR); |
GaspardD | 39:de3638276b7e | 428 | shortDistMurG90Moy = getShortDistMoy(&anaShortG90,shortDistMurD90,NB_ECHANTILLONS_IR); |
GaspardD | 39:de3638276b7e | 429 | shortDistMurD90Moy = getShortDistMoy(&anaShortD90,shortDistMurD90,NB_ECHANTILLONS_IR); |
GaspardD | 11:bc24b3ba51a9 | 430 | index_fifo_ir = (index_fifo_ir+1)%NB_ECHANTILLONS_IR; |
GaspardD | 29:fc984fe08ca7 | 431 | |
ohlimi2 | 36:bccddd02966a | 432 | |
GaspardD | 41:4dd36f607279 | 433 | trueDistMurD90Moy = distMurD90Moy; |
GaspardD | 41:4dd36f607279 | 434 | trueDistMurG90Moy = distMurG90Moy; |
GaspardD | 39:de3638276b7e | 435 | |
GaspardD | 41:4dd36f607279 | 436 | if(shortDistMurG90Moy < DIST_MIN_LONG_CM) { |
GaspardD | 29:fc984fe08ca7 | 437 | trueDistMurG90Moy = shortDistMurG90Moy; |
GaspardD | 41:4dd36f607279 | 438 | } else { |
GaspardD | 29:fc984fe08ca7 | 439 | trueDistMurG90Moy = distMurG90Moy; |
GaspardD | 29:fc984fe08ca7 | 440 | } |
GaspardD | 41:4dd36f607279 | 441 | if(shortDistMurD90Moy < DIST_MIN_LONG_CM) { |
GaspardD | 29:fc984fe08ca7 | 442 | trueDistMurD90Moy = shortDistMurD90Moy; |
GaspardD | 41:4dd36f607279 | 443 | } else { |
GaspardD | 29:fc984fe08ca7 | 444 | trueDistMurD90Moy = distMurD90Moy; |
GaspardD | 39:de3638276b7e | 445 | } |
ohlimi2 | 36:bccddd02966a | 446 | |
GaspardD | 29:fc984fe08ca7 | 447 | |
GaspardD | 29:fc984fe08ca7 | 448 | #ifdef DLVV |
GaspardD | 29:fc984fe08ca7 | 449 | switch (st_obstacle) { |
GaspardD | 41:4dd36f607279 | 450 | case FRONT_OBSTRUCTED: |
GaspardD | 41:4dd36f607279 | 451 | st_tmpMurs = REF_A_GAUCHE; |
GaspardD | 41:4dd36f607279 | 452 | return; |
GaspardD | 41:4dd36f607279 | 453 | case RIGHT_OBSTRUCTED: |
GaspardD | 41:4dd36f607279 | 454 | st_tmpMurs = REF_A_GAUCHE; |
GaspardD | 41:4dd36f607279 | 455 | return; |
GaspardD | 41:4dd36f607279 | 456 | case LEFT_OBSTRUCTED: |
GaspardD | 41:4dd36f607279 | 457 | st_tmpMurs = REF_A_DROITE; |
GaspardD | 41:4dd36f607279 | 458 | return; |
GaspardD | 41:4dd36f607279 | 459 | default: |
GaspardD | 41:4dd36f607279 | 460 | break; |
GaspardD | 29:fc984fe08ca7 | 461 | } |
GaspardD | 29:fc984fe08ca7 | 462 | |
GaspardD | 29:fc984fe08ca7 | 463 | #endif |
GaspardD | 41:4dd36f607279 | 464 | st_tmpMurs = REF_BIDIR; |
GaspardD | 29:fc984fe08ca7 | 465 | |
GaspardD | 41:4dd36f607279 | 466 | st_murs = st_tmpMurs; |
GaspardD | 12:51b1b40cc017 | 467 | |
GaspardD | 8:1d8c3ca5e508 | 468 | return; |
GaspardD | 8:1d8c3ca5e508 | 469 | } |
GaspardD | 11:bc24b3ba51a9 | 470 | #ifdef DLVV |
GaspardD | 11:bc24b3ba51a9 | 471 | void obstacleUpdate(void) |
GaspardD | 8:1d8c3ca5e508 | 472 | { |
GaspardD | 8:1d8c3ca5e508 | 473 | return; |
GaspardD | 8:1d8c3ca5e508 | 474 | } |
GaspardD | 11:bc24b3ba51a9 | 475 | #endif |
GaspardD | 11:bc24b3ba51a9 | 476 | |
GaspardD | 11:bc24b3ba51a9 | 477 | void sectionUpdate(void) |
GaspardD | 8:1d8c3ca5e508 | 478 | { |
GaspardD | 14:d471faa7d1a2 | 479 | #if (DEBUG > 3) |
GaspardD | 14:d471faa7d1a2 | 480 | pc.printf("\r\nUpdate Section\r\n"); |
GaspardD | 14:d471faa7d1a2 | 481 | #endif |
GaspardD | 11:bc24b3ba51a9 | 482 | switch (st_currentSection) { |
GaspardD | 19:771bf61be276 | 483 | case RUNNING_SECTION: |
GaspardD | 19:771bf61be276 | 484 | if(tachySectionDist_cm > p_sectionCourante->lng_section_cm) { //on pourrait rajouter un test lidar |
GaspardD | 19:771bf61be276 | 485 | st_tmpSection = LOADING_SECTION; |
GaspardD | 19:771bf61be276 | 486 | } else { |
GaspardD | 19:771bf61be276 | 487 | return; |
GaspardD | 19:771bf61be276 | 488 | } |
GaspardD | 19:771bf61be276 | 489 | break; |
GaspardD | 19:771bf61be276 | 490 | case LOADING_SECTION: |
GaspardD | 19:771bf61be276 | 491 | if(p_sectionCourante != NULL) { //la section a ete chargee dans sectionOutput |
GaspardD | 19:771bf61be276 | 492 | st_tmpSection = RUNNING_SECTION; |
GaspardD | 19:771bf61be276 | 493 | } else { |
GaspardD | 19:771bf61be276 | 494 | st_tmpSection=ARRET; |
GaspardD | 19:771bf61be276 | 495 | } |
GaspardD | 19:771bf61be276 | 496 | break; |
GaspardD | 19:771bf61be276 | 497 | case ARRET: |
GaspardD | 19:771bf61be276 | 498 | if(p_sectionCourante != NULL) { |
GaspardD | 19:771bf61be276 | 499 | st_tmpSection = RUNNING_SECTION; |
GaspardD | 19:771bf61be276 | 500 | } else { |
GaspardD | 19:771bf61be276 | 501 | return; |
GaspardD | 19:771bf61be276 | 502 | } |
GaspardD | 19:771bf61be276 | 503 | break; |
GaspardD | 19:771bf61be276 | 504 | default: |
GaspardD | 19:771bf61be276 | 505 | break; |
GaspardD | 11:bc24b3ba51a9 | 506 | } |
GaspardD | 11:bc24b3ba51a9 | 507 | st_currentSection = st_tmpSection; |
GaspardD | 8:1d8c3ca5e508 | 508 | return; |
GaspardD | 8:1d8c3ca5e508 | 509 | } |
GaspardD | 8:1d8c3ca5e508 | 510 | |
GaspardD | 11:bc24b3ba51a9 | 511 | void maxSpeedUpdate(void) |
GaspardD | 8:1d8c3ca5e508 | 512 | { |
GaspardD | 14:d471faa7d1a2 | 513 | #if (DEBUG > 3) |
GaspardD | 14:d471faa7d1a2 | 514 | pc.printf("\r\nUpdate MaxSpeed\r\n"); |
GaspardD | 14:d471faa7d1a2 | 515 | #endif |
GaspardD | 11:bc24b3ba51a9 | 516 | |
GaspardD | 41:4dd36f607279 | 517 | if(st_maxSpeed == BLOCKED && strengthLidar > LIDAR_STRENGTH_THRESOLD && distLidar < p_sectionCourante->lidarWarningDist_cm) { |
GaspardD | 41:4dd36f607279 | 518 | st_tmpMaxSpeed = BLOCKED; |
GaspardD | 41:4dd36f607279 | 519 | } else 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 | 41:4dd36f607279 | 525 | distLidarPrev-distLidar > 3) { |
GaspardD | 21:de7a0a47f8a3 | 526 | st_tmpMaxSpeed = BLOCKED; |
GaspardD | 41:4dd36f607279 | 527 | } else { |
GaspardD | 24:698fefbbee00 | 528 | st_tmpMaxSpeed = SPEED_WARNING; |
GaspardD | 24:698fefbbee00 | 529 | } |
GaspardD | 21:de7a0a47f8a3 | 530 | } else { |
GaspardD | 11:bc24b3ba51a9 | 531 | st_tmpMaxSpeed = SPEED_MAX; |
GaspardD | 11:bc24b3ba51a9 | 532 | } |
GaspardD | 11:bc24b3ba51a9 | 533 | |
GaspardD | 11:bc24b3ba51a9 | 534 | st_maxSpeed = st_tmpMaxSpeed; |
GaspardD | 11:bc24b3ba51a9 | 535 | return; |
GaspardD | 11:bc24b3ba51a9 | 536 | } |
GaspardD | 11:bc24b3ba51a9 | 537 | |
GaspardD | 11:bc24b3ba51a9 | 538 | void throttleUpdate(void) |
GaspardD | 11:bc24b3ba51a9 | 539 | { |
GaspardD | 14:d471faa7d1a2 | 540 | #if (DEBUG > 3) |
GaspardD | 14:d471faa7d1a2 | 541 | pc.printf("\r\nUpdate Throttle\r\n"); |
GaspardD | 14:d471faa7d1a2 | 542 | #endif |
GaspardD | 11:bc24b3ba51a9 | 543 | switch (st_thro) { |
GaspardD | 21:de7a0a47f8a3 | 544 | case REGULATION_SPEED: |
GaspardD | 24:698fefbbee00 | 545 | if( st_currentSection == ARRET || |
GaspardD | 41:4dd36f607279 | 546 | st_maxSpeed == BLOCKED ) { |
GaspardD | 19:771bf61be276 | 547 | st_tmpThro = BRAKING; |
GaspardD | 29:fc984fe08ca7 | 548 | } |
GaspardD | 27:f8c3f1524a64 | 549 | #ifdef FREINAGE_ADAPTATIF |
GaspardD | 41:4dd36f607279 | 550 | else if(tachySpeed_cmps > (maxSpeed_cmps + SPEED_DELTA_CMPS)) { |
GaspardD | 24:698fefbbee00 | 551 | st_tmpThro = BRAKING; |
GaspardD | 29:fc984fe08ca7 | 552 | 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 | 553 | brakingTimer.reset(); |
GaspardD | 24:698fefbbee00 | 554 | brakingTimer.start(); |
GaspardD | 27:f8c3f1524a64 | 555 | } |
GaspardD | 27:f8c3f1524a64 | 556 | #endif |
GaspardD | 41:4dd36f607279 | 557 | else { |
GaspardD | 19:771bf61be276 | 558 | return; |
GaspardD | 19:771bf61be276 | 559 | } |
GaspardD | 19:771bf61be276 | 560 | break; |
GaspardD | 16:63690703b5b6 | 561 | case BRAKING: |
GaspardD | 29:fc984fe08ca7 | 562 | if( st_maxSpeed == BLOCKED |
GaspardD | 41:4dd36f607279 | 563 | #ifdef FREINAGE_ADAPTATIF |
GaspardD | 41:4dd36f607279 | 564 | || brakingTimer.read_us() < brakingDurationNeeded_us |
GaspardD | 41:4dd36f607279 | 565 | #endif |
GaspardD | 41:4dd36f607279 | 566 | ) { |
GaspardD | 23:04d393220daa | 567 | st_tmpThro = BRAKING; |
GaspardD | 41:4dd36f607279 | 568 | } else if(st_currentSection == ARRET) { |
GaspardD | 21:de7a0a47f8a3 | 569 | st_tmpThro = STOPPED; |
GaspardD | 41:4dd36f607279 | 570 | } else { |
GaspardD | 41:4dd36f607279 | 571 | #ifdef FREINAGE_ADAPTATIF |
GaspardD | 24:698fefbbee00 | 572 | brakingDurationNeeded_us = 0; |
GaspardD | 41:4dd36f607279 | 573 | #endif |
GaspardD | 21:de7a0a47f8a3 | 574 | st_tmpThro = REGULATION_SPEED; |
GaspardD | 21:de7a0a47f8a3 | 575 | } |
GaspardD | 19:771bf61be276 | 576 | break; |
GaspardD | 19:771bf61be276 | 577 | case STOPPED: |
GaspardD | 19:771bf61be276 | 578 | if(st_currentSection == RUNNING_SECTION) { |
GaspardD | 22:26fc6e6f7a55 | 579 | st_tmpThro = REGULATION_SPEED; |
GaspardD | 19:771bf61be276 | 580 | } else { |
GaspardD | 19:771bf61be276 | 581 | st_tmpThro = STOPPED; |
GaspardD | 16:63690703b5b6 | 582 | } |
GaspardD | 19:771bf61be276 | 583 | break; |
GaspardD | 19:771bf61be276 | 584 | default: |
GaspardD | 19:771bf61be276 | 585 | break; |
GaspardD | 11:bc24b3ba51a9 | 586 | } |
GaspardD | 11:bc24b3ba51a9 | 587 | st_thro = st_tmpThro; |
GaspardD | 8:1d8c3ca5e508 | 588 | return; |
GaspardD | 8:1d8c3ca5e508 | 589 | } |
GaspardD | 2:fd0ffe46a87d | 590 | |
GaspardD | 8:1d8c3ca5e508 | 591 | //########## OUTPUT STATES ########## |
GaspardD | 11:bc24b3ba51a9 | 592 | //updating output parameters |
GaspardD | 11:bc24b3ba51a9 | 593 | void mursOutput(void) |
GaspardD | 12:51b1b40cc017 | 594 | { |
GaspardD | 29:fc984fe08ca7 | 595 | //pour dériver |
GaspardD | 29:fc984fe08ca7 | 596 | positionSurPiste45Prev = positionSurPiste45; |
GaspardD | 29:fc984fe08ca7 | 597 | positionSurPiste90Prev = positionSurPiste90; |
GaspardD | 14:d471faa7d1a2 | 598 | #if (DEBUG > 3) |
GaspardD | 14:d471faa7d1a2 | 599 | pc.printf("\r\n Output MURS\r\n"); |
GaspardD | 14:d471faa7d1a2 | 600 | #endif |
GaspardD | 23:04d393220daa | 601 | |
GaspardD | 29:fc984fe08ca7 | 602 | switch (st_murs) { |
GaspardD | 41:4dd36f607279 | 603 | case REF_A_GAUCHE://par defaut, on compte à partir de la bordure gauche |
GaspardD | 41:4dd36f607279 | 604 | largeurPiste90 = 150.0; |
GaspardD | 41:4dd36f607279 | 605 | positionSurPiste90 = ( trueDistMurG90Moy + DEMI_LARGEUR_BINIOU_CM ); |
GaspardD | 41:4dd36f607279 | 606 | positionSurPiste45 = ( distMurG45Moy * 1.414214/2.0 ) + DEMI_LARGEUR_BINIOU_CM; |
GaspardD | 39:de3638276b7e | 607 | #ifdef OMAR |
GaspardD | 41:4dd36f607279 | 608 | pc.printf("REF_A_GAUCHE\r\n"); |
GaspardD | 39:de3638276b7e | 609 | #endif |
GaspardD | 41:4dd36f607279 | 610 | break; |
GaspardD | 41:4dd36f607279 | 611 | case REF_A_DROITE://par defaut, on compte à partir de la bordure gauche |
GaspardD | 41:4dd36f607279 | 612 | largeurPiste90 = 150.0; |
GaspardD | 41:4dd36f607279 | 613 | positionSurPiste90 = largeurPiste90 - ( DEMI_LARGEUR_BINIOU_CM + trueDistMurD90Moy ) ; |
GaspardD | 41:4dd36f607279 | 614 | positionSurPiste45 = largeurPiste90 - ( distMurD45Moy * (1.414214)/2 ) + DEMI_LARGEUR_BINIOU_CM; |
GaspardD | 39:de3638276b7e | 615 | #ifdef OMAR |
GaspardD | 41:4dd36f607279 | 616 | pc.printf("REF_A_DROITE\r\n"); |
GaspardD | 39:de3638276b7e | 617 | #endif |
GaspardD | 41:4dd36f607279 | 618 | break; |
GaspardD | 41:4dd36f607279 | 619 | default://REF_BIDIR |
GaspardD | 41:4dd36f607279 | 620 | largeurPiste90 = trueDistMurG90Moy + trueDistMurD90Moy ; |
GaspardD | 41:4dd36f607279 | 621 | largeurPiste45 = distMurG45Moy + distMurD45Moy ; |
GaspardD | 41:4dd36f607279 | 622 | positionSurPiste90 = (trueDistMurG90Moy ); |
GaspardD | 41:4dd36f607279 | 623 | positionSurPiste45 = (distMurG45Moy); |
GaspardD | 39:de3638276b7e | 624 | #ifdef OMAR |
GaspardD | 41:4dd36f607279 | 625 | pc.printf("REF_BIDIR\r\n"); |
GaspardD | 39:de3638276b7e | 626 | #endif |
GaspardD | 41:4dd36f607279 | 627 | break; |
GaspardD | 29:fc984fe08ca7 | 628 | } |
GaspardD | 23:04d393220daa | 629 | |
GaspardD | 22:26fc6e6f7a55 | 630 | |
GaspardD | 21:de7a0a47f8a3 | 631 | //deriv correction |
GaspardD | 29:fc984fe08ca7 | 632 | derive45 = positionSurPiste45 - positionSurPiste45Prev; |
GaspardD | 29:fc984fe08ca7 | 633 | derive90 = positionSurPiste90 - positionSurPiste90Prev; |
GaspardD | 39:de3638276b7e | 634 | |
GaspardD | 39:de3638276b7e | 635 | #ifdef OMAR/* |
GaspardD | 39:de3638276b7e | 636 | pc.printf("derive45 => %.4lf \r\n ",derive45); |
GaspardD | 39:de3638276b7e | 637 | pc.printf("derive90 => %.4lf \r\n ",derive90); |
GaspardD | 39:de3638276b7e | 638 | pc.printf("distMurG45Moy => %.4lf \r\n ",distMurG45Moy); |
GaspardD | 39:de3638276b7e | 639 | pc.printf("distMurD45Moy => %.4lf \r\n ",distMurD45Moy); |
GaspardD | 39:de3638276b7e | 640 | pc.printf("trueDistMurG90Moy => %.4lf \r\n ",trueDistMurG90Moy); |
GaspardD | 39:de3638276b7e | 641 | pc.printf("trueDistMurD90Moy => %.4lf \r\n ",trueDistMurD90Moy); |
GaspardD | 39:de3638276b7e | 642 | pc.printf("positionSurPiste90 => %.4lf \r\n ",positionSurPiste90); |
GaspardD | 39:de3638276b7e | 643 | pc.printf("positionSurPiste45 => %.4lf \r\n ",positionSurPiste45); |
GaspardD | 39:de3638276b7e | 644 | pc.printf("largeurPiste90 => %.4lf \r\n ",largeurPiste90);*/ |
GaspardD | 39:de3638276b7e | 645 | #endif |
GaspardD | 39:de3638276b7e | 646 | |
GaspardD | 41:4dd36f607279 | 647 | //integral correction |
GaspardD | 39:de3638276b7e | 648 | lastDifferences90[lastDifferenceIndex] = (p_sectionCourante->consigne_position - positionSurPiste90); |
GaspardD | 39:de3638276b7e | 649 | integralSum=0; |
GaspardD | 39:de3638276b7e | 650 | for(int f=0; f<NB_INTEGRAL_SAMPLES; f++) { |
GaspardD | 39:de3638276b7e | 651 | integralSum+=lastDifferences90[f]; |
GaspardD | 39:de3638276b7e | 652 | } |
GaspardD | 39:de3638276b7e | 653 | lastDifferenceIndex = (lastDifferenceIndex + 1)%NB_INTEGRAL_SAMPLES; |
GaspardD | 39:de3638276b7e | 654 | |
GaspardD | 41:4dd36f607279 | 655 | |
GaspardD | 41:4dd36f607279 | 656 | |
GaspardD | 41:4dd36f607279 | 657 | |
GaspardD | 21:de7a0a47f8a3 | 658 | //application des coefficients |
GaspardD | 41:4dd36f607279 | 659 | |
GaspardD | 41:4dd36f607279 | 660 | pulseDirection_us = (int)( |
GaspardD | 41:4dd36f607279 | 661 | ((positionSurPiste45 - (largeurPiste45/2.0)) * p_sectionCourante->coef_p) |
GaspardD | 41:4dd36f607279 | 662 | + ((positionSurPiste90 - (largeurPiste90/2.0)) * p_sectionCourante->coef_p) |
GaspardD | 41:4dd36f607279 | 663 | + ( -derive45 * p_sectionCourante->coef_d) |
GaspardD | 41:4dd36f607279 | 664 | + ( -derive90 * p_sectionCourante->coef_d) |
GaspardD | 41:4dd36f607279 | 665 | + ( integralSum * p_sectionCourante->coef_i) |
GaspardD | 41:4dd36f607279 | 666 | )* (300.0/(tachySpeed_cmps+1.0)) // faire du test reel pour afiné ou un calcul |
GaspardD | 41:4dd36f607279 | 667 | + DIRECTION_PULSE_MIDDLE; |
GaspardD | 41:4dd36f607279 | 668 | |
GaspardD | 41:4dd36f607279 | 669 | |
GaspardD | 39:de3638276b7e | 670 | #ifdef OMAR |
GaspardD | 41:4dd36f607279 | 671 | pc.printf("p_sectionCourante->consigne_position => %.4lf \r\n ",p_sectionCourante->consigne_position); |
GaspardD | 41:4dd36f607279 | 672 | pc.printf("positionSurPiste45 => %.4lf \r\n ",positionSurPiste45); |
GaspardD | 41:4dd36f607279 | 673 | pc.printf("p_sectionCourante->coef_p => %.4lf \r\n ",p_sectionCourante->coef_p); |
GaspardD | 39:de3638276b7e | 674 | #endif |
GaspardD | 21:de7a0a47f8a3 | 675 | //gestioon du dépassement |
GaspardD | 21:de7a0a47f8a3 | 676 | if(pulseDirection_us > DIRECTION_PULSE_MAX) { //POUR TOURNER A GAUCHE |
GaspardD | 19:771bf61be276 | 677 | #if DEBUG > 1 |
ohlimi2 | 36:bccddd02966a | 678 | pc.printf("!!! OVER PWM Direction pulse: %.4lf\r\n",pulseDirection_us); |
GaspardD | 21:de7a0a47f8a3 | 679 | #endif |
GaspardD | 21:de7a0a47f8a3 | 680 | pulseDirection_us = DIRECTION_PULSE_MAX; |
GaspardD | 21:de7a0a47f8a3 | 681 | } else if(pulseDirection_us < DIRECTION_PULSE_MIN ) { //POUR TOURNER A DROITE |
GaspardD | 21:de7a0a47f8a3 | 682 | #if DEBUG > 1 |
ohlimi2 | 36:bccddd02966a | 683 | pc.printf("!!! UNDER PWM Direction pulse: %.4lf\r\n",pulseDirection_us); |
GaspardD | 19:771bf61be276 | 684 | #endif |
GaspardD | 21:de7a0a47f8a3 | 685 | pulseDirection_us = DIRECTION_PULSE_MIN ; |
GaspardD | 21:de7a0a47f8a3 | 686 | } |
GaspardD | 21:de7a0a47f8a3 | 687 | #if DEBUG > 1 |
ohlimi2 | 36:bccddd02966a | 688 | pc.printf("PWM Direction pulse: %.4lf\r\n",pulseDirection_us); |
GaspardD | 21:de7a0a47f8a3 | 689 | #endif |
GaspardD | 21:de7a0a47f8a3 | 690 | PwmDirection.pulsewidth_us(pulseDirection_us); |
GaspardD | 8:1d8c3ca5e508 | 691 | return; |
GaspardD | 8:1d8c3ca5e508 | 692 | } |
GaspardD | 8:1d8c3ca5e508 | 693 | |
GaspardD | 11:bc24b3ba51a9 | 694 | #ifdef DLVV |
GaspardD | 11:bc24b3ba51a9 | 695 | void obstacleOutput(void) |
GaspardD | 8:1d8c3ca5e508 | 696 | { |
GaspardD | 8:1d8c3ca5e508 | 697 | return; |
GaspardD | 8:1d8c3ca5e508 | 698 | } |
GaspardD | 11:bc24b3ba51a9 | 699 | #endif |
GaspardD | 11:bc24b3ba51a9 | 700 | void sectionOutput(void) |
GaspardD | 8:1d8c3ca5e508 | 701 | { |
GaspardD | 14:d471faa7d1a2 | 702 | #if (DEBUG > 3) |
GaspardD | 14:d471faa7d1a2 | 703 | pc.printf("\r\n Output Section\r\n"); |
GaspardD | 14:d471faa7d1a2 | 704 | #endif |
GaspardD | 11:bc24b3ba51a9 | 705 | switch (st_currentSection) { |
GaspardD | 19:771bf61be276 | 706 | case RUNNING_SECTION: |
GaspardD | 19:771bf61be276 | 707 | break; |
GaspardD | 19:771bf61be276 | 708 | case LOADING_SECTION: |
GaspardD | 19:771bf61be276 | 709 | p_sectionCourante=p_sectionCourante->nextSection; |
GaspardD | 19:771bf61be276 | 710 | tachySectionDist_cm = 0; |
GaspardD | 19:771bf61be276 | 711 | break; |
GaspardD | 19:771bf61be276 | 712 | case ARRET: |
GaspardD | 19:771bf61be276 | 713 | //on est à l'arret |
GaspardD | 19:771bf61be276 | 714 | break; |
GaspardD | 19:771bf61be276 | 715 | default: |
GaspardD | 19:771bf61be276 | 716 | break; |
GaspardD | 11:bc24b3ba51a9 | 717 | } |
GaspardD | 8:1d8c3ca5e508 | 718 | return; |
GaspardD | 8:1d8c3ca5e508 | 719 | } |
GaspardD | 8:1d8c3ca5e508 | 720 | |
GaspardD | 11:bc24b3ba51a9 | 721 | |
GaspardD | 11:bc24b3ba51a9 | 722 | void maxSpeedOutput(void) |
GaspardD | 8:1d8c3ca5e508 | 723 | { |
GaspardD | 14:d471faa7d1a2 | 724 | #if (DEBUG > 3) |
GaspardD | 14:d471faa7d1a2 | 725 | pc.printf("\r\n Output MAX SPEED\r\n"); |
GaspardD | 14:d471faa7d1a2 | 726 | #endif |
GaspardD | 19:771bf61be276 | 727 | switch(st_maxSpeed) { |
GaspardD | 21:de7a0a47f8a3 | 728 | case BLOCKED: |
GaspardD | 41:4dd36f607279 | 729 | maxSpeed_cmps = 0; |
GaspardD | 19:771bf61be276 | 730 | break; |
GaspardD | 21:de7a0a47f8a3 | 731 | case SPEED_WARNING: |
GaspardD | 24:698fefbbee00 | 732 | maxSpeed_cmps = p_sectionCourante->slowSpeed_cmps; |
GaspardD | 19:771bf61be276 | 733 | break; |
GaspardD | 19:771bf61be276 | 734 | default: |
GaspardD | 19:771bf61be276 | 735 | maxSpeed_cmps = p_sectionCourante->targetSpeed_cmps; |
GaspardD | 19:771bf61be276 | 736 | break; |
GaspardD | 11:bc24b3ba51a9 | 737 | } |
GaspardD | 8:1d8c3ca5e508 | 738 | return; |
GaspardD | 8:1d8c3ca5e508 | 739 | } |
GaspardD | 11:bc24b3ba51a9 | 740 | |
GaspardD | 11:bc24b3ba51a9 | 741 | void throttleOutput(void) |
GaspardD | 11:bc24b3ba51a9 | 742 | { |
GaspardD | 14:d471faa7d1a2 | 743 | #if (DEBUG > 3) |
GaspardD | 14:d471faa7d1a2 | 744 | pc.printf("\r\n Output TROTTLE\r\n"); |
GaspardD | 14:d471faa7d1a2 | 745 | #endif |
GaspardD | 11:bc24b3ba51a9 | 746 | switch (st_thro) { |
GaspardD | 21:de7a0a47f8a3 | 747 | case REGULATION_SPEED: |
GaspardD | 22:26fc6e6f7a55 | 748 | pulseSpeed_us = maxSpeed_cmps * 279 / 2048 + 1558 ; |
GaspardD | 19:771bf61be276 | 749 | break; |
GaspardD | 19:771bf61be276 | 750 | case BRAKING: |
GaspardD | 16:63690703b5b6 | 751 | #if DEBUG > 2 |
GaspardD | 19:771bf61be276 | 752 | pc.printf("BRAKINGGGGGGGGGGGGGGGGGG !!! \r\n"); |
GaspardD | 19:771bf61be276 | 753 | #endif |
GaspardD | 19:771bf61be276 | 754 | pulseSpeed_us = BRAKING_PULSE_US; |
GaspardD | 19:771bf61be276 | 755 | break; |
GaspardD | 19:771bf61be276 | 756 | case STOPPED: |
GaspardD | 19:771bf61be276 | 757 | #if DEBUG > 2 |
GaspardD | 19:771bf61be276 | 758 | pc.printf("STOPPED\r\n"); |
GaspardD | 16:63690703b5b6 | 759 | #endif |
GaspardD | 19:771bf61be276 | 760 | pulseSpeed_us = ZERO_PULSE_SPEED_US; |
GaspardD | 19:771bf61be276 | 761 | break; |
GaspardD | 19:771bf61be276 | 762 | default: |
GaspardD | 19:771bf61be276 | 763 | break; |
GaspardD | 11:bc24b3ba51a9 | 764 | } |
GaspardD | 22:26fc6e6f7a55 | 765 | |
GaspardD | 19:771bf61be276 | 766 | PwmMotor.pulsewidth_us(pulseSpeed_us); |
GaspardD | 15:129f205ff030 | 767 | #if DEBUG > 1 |
ohlimi2 | 36:bccddd02966a | 768 | pc.printf("PWM Thro pulse: %.4lf micros\r\n",pulseSpeed_us); |
GaspardD | 13:af9a59ccf60b | 769 | #endif |
GaspardD | 11:bc24b3ba51a9 | 770 | return; |
GaspardD | 11:bc24b3ba51a9 | 771 | } |