|
| 1 | +/* |
| 2 | + * Copyright (C) 2008 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software< /span> |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#ifndef _ROBOTDULAB_H_ |
| 18 | +#define _ROBOTDULAB_H_ |
| 19 | + |
| 20 | +#if ARDUINO >= 100 |
| 21 | + #include "Arduino.h" |
| 22 | + #include "Print.h" |
| 23 | +#else |
| 24 | + #include "WProgram.h" |
| 25 | +#endif |
| 26 | + |
| 27 | +#include <Servo.h> |
| 28 | +#include <Adafruit_NeoPixel.h> |
| 29 | + |
| 30 | +/** Constants */ |
| 31 | + |
| 32 | +/** Couleurs */ |
| 33 | +typedef enum |
| 34 | +{ |
| 35 | + NOIR = (0), |
| 36 | + ROUGE = (1), |
| 37 | + VERT = (2), |
| 38 | + MAGENTA = (3), |
| 39 | + BLEU = (4), |
| 40 | + BLANC = (5), |
| 41 | + TURQUOISE= (6), |
| 42 | + JAUNE = (7), |
| 43 | + VIOLET = (8), |
| 44 | +} couleur_index_t; |
| 45 | + |
| 46 | +uint32_t couleurs[] = { |
| 47 | + Adafruit_NeoPixel::Color(0, 0, 0), |
| 48 | + Adafruit_NeoPixel::Color(255, 0, 0), |
| 49 | + Adafruit_NeoPixel::Color(0, 255, 0), |
| 50 | + Adafruit_NeoPixel::Color(139, 0, 139), |
| 51 | + Adafruit_NeoPixel::Color(0, 0, 255), |
| 52 | + Adafruit_NeoPixel::Color(127, 127, 127), |
| 53 | + Adafruit_NeoPixel::Color(0, 245, 255), |
| 54 | + Adafruit_NeoPixel::Color(255, 215, 0), |
| 55 | + Adafruit_NeoPixel::Color(208, 32, 114), |
| 56 | +}; |
| 57 | + |
| 58 | +/** Sens de rotation servomoteur */ |
| 59 | +typedef enum |
| 60 | +{ |
| 61 | + INVERSE = (0), |
| 62 | + ARRET = (90), |
| 63 | + NORMAL = (180), |
| 64 | +} sens_rotation_t; |
| 65 | + |
| 66 | +/** Gestion de l'anneau de LED */ |
| 67 | + |
| 68 | +const int NB_LED = 16; |
| 69 | + |
| 70 | +typedef enum |
| 71 | +{ |
| 72 | + COULEUR = (1), |
| 73 | + ARC_EN_CIEL = (2), |
| 74 | + ARC_EN_CIEL_CYCLIQUE = (3), |
| 75 | + COULEUR_CYCLIQUE = (4), |
| 76 | +} choix_animation_anneau_led_t; |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +class RobotDuLAB { |
| 81 | + const int triggerPin = 2; // broche trig du capteur US HC-SR04 |
| 82 | + const int echoPin = 4; // broche echo du capteur US HC-SR04 |
| 83 | + |
| 84 | + Servo roueDroite; |
| 85 | + Servo roueGauche; |
| 86 | + |
| 87 | + Adafruit_NeoPixel anneauDeLed; |
| 88 | + |
| 89 | + public: |
| 90 | + |
| 91 | + // Constructor(s) |
| 92 | + RobotDuLAB(const int triggerPin, const int echoPin, const int roueDroitePin, |
| 93 | + const int roueGauchePin, const int anneauDeLedPin):triggerPin(triggerPin), |
| 94 | + echoPin(echoPin), |
| 95 | + roueDroite(), |
| 96 | + roueGauche(), |
| 97 | + anneauDeLed(NB_LED, anneauDeLedPin, NEO_GRB + NEO_KHZ800) { |
| 98 | + // Initialisation du HC-SR04 |
| 99 | + pinMode (triggerPin,OUTPUT); |
| 100 | + digitalWrite(triggerPin, LOW); |
| 101 | + pinMode(echoPin, INPUT); |
| 102 | + |
| 103 | + // Initialisation servos |
| 104 | + roueGauche.attach(roueGauchePin); |
| 105 | + roueDroite.attach(roueDroitePin); |
| 106 | + |
| 107 | + //Initialisation Anneau de LED |
| 108 | + anneauDeLed.begin(); |
| 109 | + } |
| 110 | + |
| 111 | + virtual ~RobotDuLAB() {} |
| 112 | + |
| 113 | + void begin(){ |
| 114 | + |
| 115 | + } |
| 116 | + |
| 117 | + void Arreter() { |
| 118 | + roueDroite.write(ARRET); |
| 119 | + roueGauche.write(ARRET); |
| 120 | + } |
| 121 | + |
| 122 | + void Avancer() { |
| 123 | + roueDroite.write(NORMAL); |
| 124 | + roueGauche.write(INVERSE); |
| 125 | + } |
| 126 | + |
| 127 | + void Reculer() { |
| 128 | + roueDroite.write(INVERSE); |
| 129 | + roueGauche.write(ARRET); |
| 130 | + } |
| 131 | + |
| 132 | + void TournerAGauche() { |
| 133 | + roueDroite.write(NORMAL); |
| 134 | + roueGauche.write(NORMAL); |
| 135 | + } |
| 136 | + |
| 137 | + void TournerADroite() { |
| 138 | + roueDroite.write(INVERSE); |
| 139 | + roueGauche.write(INVERSE); |
| 140 | + } |
| 141 | + |
| 142 | + void AnimerAnneauLed(choix_animation_anneau_led_t choix, couleur_index_t couleur){ |
| 143 | + switch (choix){ |
| 144 | + case COULEUR : |
| 145 | + anneauDeLed.setPixelColor(14, couleurs[couleur]); |
| 146 | + anneauDeLed.setPixelColor(15, couleurs[couleur]); |
| 147 | + anneauDeLed.setPixelColor(0, couleurs[couleur]); |
| 148 | + break; |
| 149 | + case ARC_EN_CIEL : |
| 150 | + rainbow(20); |
| 151 | + break; |
| 152 | + case ARC_EN_CIEL_CYCLIQUE : |
| 153 | + rainbowCycle(20); |
| 154 | + break; |
| 155 | + case COULEUR_CYCLIQUE : |
| 156 | + theaterChase(couleurs[couleur], 50); |
| 157 | + break; |
| 158 | + } |
| 159 | + anneauDeLed.show(); |
| 160 | + } |
| 161 | + |
| 162 | + long getDistanceObstacle(){ |
| 163 | + digitalWrite(triggerPin, HIGH); |
| 164 | + delayMicroseconds(10); |
| 165 | + digitalWrite(triggerPin, LOW); |
| 166 | + |
| 167 | + long duree_echo = pulseIn(echoPin, HIGH); |
| 168 | + return microsecondsEnCentimetres(duree_echo); |
| 169 | + } |
| 170 | + |
| 171 | + private: |
| 172 | + long microsecondsEnCentimetres(long microseconds) |
| 173 | + { |
| 174 | + // The speed of sound is 340 m/s or 29 microseconds per centimeter. |
| 175 | + // The ping travels out and back, so to find the distance of the |
| 176 | + // object we take half of the distance travelled. |
| 177 | + return microseconds / 29 / 2; |
| 178 | + } |
| 179 | + |
| 180 | + // Fill the dots one after the other with a color |
| 181 | + void colorWipe(uint32_t c, uint8_t wait) { |
| 182 | + for(uint16_t i=0; i<anneauDeLed.numPixels(); i++) { |
| 183 | + anneauDeLed.setPixelColor(i, c); |
| 184 | + anneauDeLed.show(); |
| 185 | + delay(wait); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + void rainbow(uint8_t wait) { |
| 190 | + uint16_t i, j; |
| 191 | + |
| 192 | + for(j=0; j<256; j++) { |
| 193 | + for(i=0; i<anneauDeLed.numPixels(); i++) { |
| 194 | + anneauDeLed.setPixelColor(i, Wheel((i+j) & 255)); |
| 195 | + } |
| 196 | + anneauDeLed.show(); |
| 197 | + delay(wait); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + // Slightly different, this makes the rainbow equally distributed throughout |
| 202 | + void rainbowCycle(uint8_t wait) { |
| 203 | + uint16_t i, j; |
| 204 | + |
| 205 | + for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel |
| 206 | + for(i=0; i< anneauDeLed.numPixels(); i++) { |
| 207 | + anneauDeLed.setPixelColor(i, Wheel(((i * 256 / anneauDeLed.numPixels()) + j) & 255)); |
| 208 | + } |
| 209 | + anneauDeLed.show(); |
| 210 | + delay(wait); |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + //Theatre-style crawling lights. |
| 215 | + void theaterChase(uint32_t c, uint8_t wait) { |
| 216 | + for (int j=0; j<10; j++) { //do 10 cycles of chasing |
| 217 | + for (int q=0; q < 3; q++) { |
| 218 | + for (uint16_t i=0; i < anneauDeLed.numPixels(); i=i+3) { |
| 219 | + anneauDeLed.setPixelColor(i+q, c); //turn every third pixel on |
| 220 | + } |
| 221 | + anneauDeLed.show(); |
| 222 | + |
| 223 | + delay(wait); |
| 224 | + |
| 225 | + for (uint16_t i=0; i < anneauDeLed.numPixels(); i=i+3) { |
| 226 | + anneauDeLed.setPixelColor(i+q, 0); //turn every third pixel off |
| 227 | + } |
| 228 | + } |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + //Theatre-style crawling lights with rainbow effect |
| 233 | + void theaterChaseRainbow(uint8_t wait) { |
| 234 | + for (int j=0; j < 256; j++) { // cycle all 256 colors in the wheel |
| 235 | + for (int q=0; q < 3; q++) { |
| 236 | + for (uint16_t i=0; i < anneauDeLed.numPixels(); i=i+3) { |
| 237 | + anneauDeLed.setPixelColor(i+q, Wheel( (i+j) % 255)); //turn every third pixel on |
| 238 | + } |
| 239 | + anneauDeLed.show(); |
| 240 | + |
| 241 | + delay(wait); |
| 242 | + |
| 243 | + for (uint16_t i=0; i < anneauDeLed.numPixels(); i=i+3) { |
| 244 | + anneauDeLed.setPixelColor(i+q, 0); //turn every third pixel off |
| 245 | + } |
| 246 | + } |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + // Input a value 0 to 255 to get a color value. |
| 251 | + // The colours are a transition r - g - b - back to r. |
| 252 | + uint32_t Wheel(byte WheelPos) { |
| 253 | + WheelPos = 255 - WheelPos; |
| 254 | + if(WheelPos < 85) { |
| 255 | + return anneauDeLed.Color(255 - WheelPos * 3, 0, WheelPos * 3); |
| 256 | + } |
| 257 | + if(WheelPos < 170) { |
| 258 | + WheelPos -= 85; |
| 259 | + return anneauDeLed.Color(0, WheelPos * 3, 255 - WheelPos * 3); |
| 260 | + } |
| 261 | + WheelPos -= 170; |
| 262 | + return anneauDeLed.Color(WheelPos * 3, 255 - WheelPos * 3, 0); |
| 263 | + } |
| 264 | +}; |
| 265 | + |
| 266 | +#endif /* _ROBOTDULAB_H_ */ |
0 commit comments