v7
Robot.cpp@8:9d0e8da27044, 2017-03-18 (annotated)
- Committer:
- fab16
- Date:
- Sat Mar 18 15:07:10 2017 +0000
- Revision:
- 8:9d0e8da27044
- Parent:
- 7:fa09588320d0
- Child:
- 9:aff70d8f388a
v6
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fab16 | 0:3cb651f7347b | 1 | #include "Robot.h" |
fab16 | 3:3f75a7741b8f | 2 | #include "m3pi.h" |
fab16 | 3:3f75a7741b8f | 3 | |
fab16 | 4:c8ae1b606d51 | 4 | m3pi m3piR; |
fab16 | 6:9d4190ab2739 | 5 | SRF05 srf(p21,p22); |
fab16 | 0:3cb651f7347b | 6 | |
fab16 | 1:b3c45f39e86e | 7 | Robot::Robot(){ |
fab16 | 2:c17925c0ce25 | 8 | obstacle = false; |
fab16 | 2:c17925c0ce25 | 9 | Affichage affichage; |
fab16 | 2:c17925c0ce25 | 10 | LED led; |
fab16 | 2:c17925c0ce25 | 11 | Deplacement deplacement; |
fab16 | 3:3f75a7741b8f | 12 | Pattern pattern; |
fab16 | 4:c8ae1b606d51 | 13 | |
fab16 | 5:152295068384 | 14 | /* for(int i=0; i<4; i++){ |
fab16 | 5:152295068384 | 15 | this->tabObstacle[i]=false; |
fab16 | 5:152295068384 | 16 | }*/ |
fab16 | 5:152295068384 | 17 | |
fab16 | 1:b3c45f39e86e | 18 | } |
fab16 | 1:b3c45f39e86e | 19 | |
fab16 | 1:b3c45f39e86e | 20 | Robot::~Robot(){ |
fab16 | 1:b3c45f39e86e | 21 | // TODO fermer connexion bluetooth |
fab16 | 1:b3c45f39e86e | 22 | } |
fab16 | 1:b3c45f39e86e | 23 | |
fab16 | 2:c17925c0ce25 | 24 | Affichage Robot::getAffichage(){ |
fab16 | 2:c17925c0ce25 | 25 | return affichage; |
fab16 | 2:c17925c0ce25 | 26 | } |
fab16 | 2:c17925c0ce25 | 27 | |
fab16 | 2:c17925c0ce25 | 28 | LED Robot::getLed(){ |
fab16 | 2:c17925c0ce25 | 29 | return led; |
fab16 | 2:c17925c0ce25 | 30 | } |
fab16 | 2:c17925c0ce25 | 31 | |
fab16 | 2:c17925c0ce25 | 32 | Deplacement Robot::getDeplacement(){ |
fab16 | 2:c17925c0ce25 | 33 | return deplacement; |
fab16 | 2:c17925c0ce25 | 34 | } |
fab16 | 3:3f75a7741b8f | 35 | |
fab16 | 3:3f75a7741b8f | 36 | Pattern Robot::getPattern(){ |
fab16 | 3:3f75a7741b8f | 37 | return pattern; |
fab16 | 3:3f75a7741b8f | 38 | } |
fab16 | 3:3f75a7741b8f | 39 | |
fab16 | 7:fa09588320d0 | 40 | void Robot::action(char idAction){ |
fab16 | 3:3f75a7741b8f | 41 | |
fab16 | 3:3f75a7741b8f | 42 | switch(idAction){ |
fab16 | 3:3f75a7741b8f | 43 | |
fab16 | 3:3f75a7741b8f | 44 | // deplacement simple |
fab16 | 7:fa09588320d0 | 45 | case 'a' : |
fab16 | 3:3f75a7741b8f | 46 | { |
fab16 | 5:152295068384 | 47 | if(obstacle==false){ |
fab16 | 5:152295068384 | 48 | this->deplacement.avancer(); |
fab16 | 5:152295068384 | 49 | break; |
fab16 | 5:152295068384 | 50 | } |
fab16 | 3:3f75a7741b8f | 51 | } |
fab16 | 3:3f75a7741b8f | 52 | |
fab16 | 7:fa09588320d0 | 53 | case 'b' : |
fab16 | 3:3f75a7741b8f | 54 | { |
fab16 | 3:3f75a7741b8f | 55 | this->deplacement.droite(); |
fab16 | 3:3f75a7741b8f | 56 | break; |
fab16 | 3:3f75a7741b8f | 57 | } |
fab16 | 3:3f75a7741b8f | 58 | |
fab16 | 7:fa09588320d0 | 59 | case 'c' : |
fab16 | 3:3f75a7741b8f | 60 | { |
fab16 | 3:3f75a7741b8f | 61 | this->deplacement.reculer(); |
fab16 | 3:3f75a7741b8f | 62 | break; |
fab16 | 3:3f75a7741b8f | 63 | } |
fab16 | 3:3f75a7741b8f | 64 | |
fab16 | 7:fa09588320d0 | 65 | case 'd' : |
fab16 | 3:3f75a7741b8f | 66 | { |
fab16 | 3:3f75a7741b8f | 67 | this->deplacement.gauche(); |
fab16 | 3:3f75a7741b8f | 68 | break; |
fab16 | 3:3f75a7741b8f | 69 | } |
fab16 | 3:3f75a7741b8f | 70 | |
fab16 | 7:fa09588320d0 | 71 | case 'e': |
fab16 | 3:3f75a7741b8f | 72 | { |
fab16 | 3:3f75a7741b8f | 73 | this->deplacement.tourner_gauche(); |
fab16 | 3:3f75a7741b8f | 74 | break; |
fab16 | 3:3f75a7741b8f | 75 | } |
fab16 | 3:3f75a7741b8f | 76 | |
fab16 | 7:fa09588320d0 | 77 | case 'f' : |
fab16 | 3:3f75a7741b8f | 78 | { |
fab16 | 3:3f75a7741b8f | 79 | this->deplacement.tourner_droite(); |
fab16 | 3:3f75a7741b8f | 80 | break; |
fab16 | 3:3f75a7741b8f | 81 | } |
fab16 | 3:3f75a7741b8f | 82 | |
fab16 | 3:3f75a7741b8f | 83 | // pattern |
fab16 | 3:3f75a7741b8f | 84 | |
fab16 | 7:fa09588320d0 | 85 | case 'g' : |
fab16 | 3:3f75a7741b8f | 86 | { |
fab16 | 5:152295068384 | 87 | if(obstacle==false){ |
fab16 | 5:152295068384 | 88 | this->pattern.carre(); |
fab16 | 5:152295068384 | 89 | break; |
fab16 | 5:152295068384 | 90 | } |
fab16 | 3:3f75a7741b8f | 91 | } |
fab16 | 3:3f75a7741b8f | 92 | |
fab16 | 7:fa09588320d0 | 93 | case 'h' : |
fab16 | 3:3f75a7741b8f | 94 | { |
fab16 | 5:152295068384 | 95 | if(obstacle==false){ |
fab16 | 5:152295068384 | 96 | this->pattern.triangle(); |
fab16 | 5:152295068384 | 97 | break; |
fab16 | 5:152295068384 | 98 | } |
fab16 | 3:3f75a7741b8f | 99 | } |
fab16 | 3:3f75a7741b8f | 100 | |
fab16 | 8:9d0e8da27044 | 101 | case 'i' : |
fab16 | 8:9d0e8da27044 | 102 | { |
fab16 | 8:9d0e8da27044 | 103 | this->pattern.rectangle(); |
fab16 | 8:9d0e8da27044 | 104 | break; |
fab16 | 8:9d0e8da27044 | 105 | } |
fab16 | 8:9d0e8da27044 | 106 | |
fab16 | 3:3f75a7741b8f | 107 | // led |
fab16 | 8:9d0e8da27044 | 108 | case 'j' : |
fab16 | 3:3f75a7741b8f | 109 | { |
fab16 | 3:3f75a7741b8f | 110 | this->led.K1000(1); |
fab16 | 3:3f75a7741b8f | 111 | break; |
fab16 | 3:3f75a7741b8f | 112 | } |
fab16 | 3:3f75a7741b8f | 113 | |
fab16 | 8:9d0e8da27044 | 114 | case 'k' : |
fab16 | 3:3f75a7741b8f | 115 | { |
fab16 | 3:3f75a7741b8f | 116 | this->led.K2000(1); |
fab16 | 3:3f75a7741b8f | 117 | break; |
fab16 | 3:3f75a7741b8f | 118 | } |
fab16 | 3:3f75a7741b8f | 119 | |
fab16 | 8:9d0e8da27044 | 120 | case 'l' : |
fab16 | 3:3f75a7741b8f | 121 | { |
fab16 | 3:3f75a7741b8f | 122 | this->led.K3000(1); |
fab16 | 3:3f75a7741b8f | 123 | break; |
fab16 | 3:3f75a7741b8f | 124 | } |
fab16 | 3:3f75a7741b8f | 125 | |
fab16 | 8:9d0e8da27044 | 126 | case 'm' : |
fab16 | 3:3f75a7741b8f | 127 | { |
fab16 | 3:3f75a7741b8f | 128 | this->led.K4000(1); |
fab16 | 3:3f75a7741b8f | 129 | break; |
fab16 | 3:3f75a7741b8f | 130 | } |
fab16 | 3:3f75a7741b8f | 131 | |
fab16 | 8:9d0e8da27044 | 132 | case 'n' : |
fab16 | 3:3f75a7741b8f | 133 | { |
fab16 | 3:3f75a7741b8f | 134 | this->led.LED_desynchrone(1); |
fab16 | 3:3f75a7741b8f | 135 | break; |
fab16 | 3:3f75a7741b8f | 136 | } |
fab16 | 3:3f75a7741b8f | 137 | |
fab16 | 8:9d0e8da27044 | 138 | case 'o' : |
fab16 | 3:3f75a7741b8f | 139 | { |
fab16 | 3:3f75a7741b8f | 140 | this->led.LED_Blinking(DigitalOut (LED1),1); |
fab16 | 3:3f75a7741b8f | 141 | break; |
fab16 | 3:3f75a7741b8f | 142 | } |
fab16 | 3:3f75a7741b8f | 143 | |
fab16 | 8:9d0e8da27044 | 144 | case 'p' : |
fab16 | 3:3f75a7741b8f | 145 | { |
fab16 | 3:3f75a7741b8f | 146 | this->led.LED_Blinking(DigitalOut (LED2),1); |
fab16 | 3:3f75a7741b8f | 147 | break; |
fab16 | 3:3f75a7741b8f | 148 | } |
fab16 | 3:3f75a7741b8f | 149 | |
fab16 | 8:9d0e8da27044 | 150 | case 'q' : |
fab16 | 3:3f75a7741b8f | 151 | { |
fab16 | 3:3f75a7741b8f | 152 | this->led.LED_Blinking(DigitalOut (LED3),1); |
fab16 | 3:3f75a7741b8f | 153 | break; |
fab16 | 3:3f75a7741b8f | 154 | } |
fab16 | 3:3f75a7741b8f | 155 | |
fab16 | 8:9d0e8da27044 | 156 | case 'r' : |
fab16 | 3:3f75a7741b8f | 157 | { |
fab16 | 3:3f75a7741b8f | 158 | this->led.LED_Blinking(DigitalOut (LED4),1); |
fab16 | 3:3f75a7741b8f | 159 | break; |
fab16 | 3:3f75a7741b8f | 160 | } |
fab16 | 3:3f75a7741b8f | 161 | |
fab16 | 3:3f75a7741b8f | 162 | |
fab16 | 3:3f75a7741b8f | 163 | // affichage |
fab16 | 3:3f75a7741b8f | 164 | |
fab16 | 8:9d0e8da27044 | 165 | case 's' : |
fab16 | 3:3f75a7741b8f | 166 | { |
fab16 | 3:3f75a7741b8f | 167 | this->affichage.bonjour(); |
fab16 | 3:3f75a7741b8f | 168 | break; |
fab16 | 3:3f75a7741b8f | 169 | } |
fab16 | 3:3f75a7741b8f | 170 | } |
fab16 | 3:3f75a7741b8f | 171 | |
fab16 | 3:3f75a7741b8f | 172 | } |
fab16 | 3:3f75a7741b8f | 173 | |
fab16 | 4:c8ae1b606d51 | 174 | void Robot::utiliserUltrason(){ |
fab16 | 4:c8ae1b606d51 | 175 | affichage.effacerTout(); |
fab16 | 4:c8ae1b606d51 | 176 | float result = srf.read(); |
fab16 | 4:c8ae1b606d51 | 177 | //m3piR.printf("%.1f",result); |
fab16 | 4:c8ae1b606d51 | 178 | affichage.affichageUltrason(result); |
fab16 | 5:152295068384 | 179 | |
fab16 | 5:152295068384 | 180 | if(result <= 8){ |
fab16 | 5:152295068384 | 181 | obstacle = true; |
fab16 | 5:152295068384 | 182 | } |
fab16 | 5:152295068384 | 183 | else{ |
fab16 | 5:152295068384 | 184 | obstacle = false; |
fab16 | 5:152295068384 | 185 | } |
fab16 | 5:152295068384 | 186 | |
fab16 | 5:152295068384 | 187 | affichage.afficherObstacle(obstacle); |
fab16 | 6:9d4190ab2739 | 188 | |
fab16 | 5:152295068384 | 189 | } |
fab16 | 5:152295068384 | 190 | |
fab16 | 5:152295068384 | 191 | void Robot::afficherObstacle(){ |
fab16 | 5:152295068384 | 192 | affichage.afficherObstacle(obstacle); |
fab16 | 3:3f75a7741b8f | 193 | } |
fab16 | 4:c8ae1b606d51 | 194 | |
fab16 | 5:152295068384 | 195 | void Robot::scanneEnvironement(){ |
fab16 | 5:152295068384 | 196 | |
fab16 | 5:152295068384 | 197 | for(int i=1; i<4; i++){ |
fab16 | 5:152295068384 | 198 | |
fab16 | 5:152295068384 | 199 | utiliserUltrason(); |
fab16 | 5:152295068384 | 200 | |
fab16 | 5:152295068384 | 201 | if(obstacle==true){ |
fab16 | 5:152295068384 | 202 | tabObstacle[i]=true; |
fab16 | 5:152295068384 | 203 | } |
fab16 | 5:152295068384 | 204 | else{ |
fab16 | 5:152295068384 | 205 | tabObstacle[i]=false; |
fab16 | 5:152295068384 | 206 | } |
fab16 | 5:152295068384 | 207 | |
fab16 | 5:152295068384 | 208 | obstacle=false; |
fab16 | 5:152295068384 | 209 | wait(1); |
fab16 | 5:152295068384 | 210 | deplacement.quartRotation(); |
fab16 | 5:152295068384 | 211 | wait(2); |
fab16 | 5:152295068384 | 212 | } |
fab16 | 5:152295068384 | 213 | deplacement.quartRotation(); |
fab16 | 6:9d4190ab2739 | 214 | |
fab16 | 5:152295068384 | 215 | } |