1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
#define ServoPin 12 #define s0 4 #define s1 11 #define s2 3 #define s3 7 #define outPin 8 #define PIN_x 2 #define PIN_y A3
int colorflag=0; int leftmotor1=5; int leftmotor2=6; int rightmotor1=9; int rightmotor2=10; int sensor1=14; int sensor2=16; int sensor3=17; int leftread=0; int rightread=0; int middleleft=800; int middleright=800;
int pulseWidth;
double r_ratio = 3.45, g_ratio = 2.62, b_ratio = 2.98; int card=0; int balloon=0;
int red=0,green=0,blue=0;
void setup() { pinMode(leftmotor1,OUTPUT); pinMode(leftmotor2,OUTPUT); pinMode(rightmotor1,OUTPUT); pinMode(rightmotor2,OUTPUT); pinMode(ServoPin,OUTPUT); pinMode(sensor1,INPUT); pinMode(sensor2,INPUT); pinMode(sensor3,INPUT); xy_Init(); Serial.begin(9600);
digitalWrite(ServoPin, LOW); }
void loop() { walk_to_cross(); Serial.print(leftread); Serial.print("\t"); Serial.print(rightread); Serial.print("\n");
colorflag=color_analyse(); }
void walk_to_cross() { leftread=Good_read(sensor1,51); rightread=Good_read(sensor2,51); if(leftread<=middleleft && rightread<=middleright) { delay(1); if(leftread<=middleleft && rightread<=middleright) { STOP(); colorflag=color_analyse(); if(colorflag) { if(card==0) { deal_card(); } if(card) { delay(1); if(card) servo(); } } else { RUN(); delay(2000); } } } else if(leftread>middleleft && rightread>middleright) { delay(1); if(leftread>middleleft && rightread>middleright) { forward(); } } else if(leftread>middleleft && rightread<=middleright) { delay(1); if(leftread>middleleft && rightread<=middleright) { turn_left(); } } else if(leftread<=middleleft && rightread>middleright) { delay(1); if(leftread<=middleleft && rightread>middleright) { turn_right(); } } else forward(); }
void deal_card() { if(colorflag) { if(colorflag=='R')card=1; if(colorflag=='G')card=2; if(colorflag=='B')card=3; colorflag=0; } }
void servo() { if(colorflag=='R')balloon=1; else if(colorflag=='G')balloon=2; else if(colorflag=='B')balloon=3; if(balloon==card) { STOP(); ServoControl(180); delay(1500); } }
double StandardDeviation(int a[], int cnt) { double sum = 0; double ave = average(a, cnt); for (int i = 0; i < cnt; i++) sum += (1.0 * a[i] - ave) * (a[i] - ave); sum /= cnt; return sqrt(sum); } double average(int a[], int cnt) { double sum = 0; for (int i = 0; i < cnt; i++) sum += 1.0 * a[i]; sum /= cnt; return sum; } int judge_point(int a[], int cnt) { double ave = average(a, cnt); double stand = StandardDeviation(a, cnt); int cntt = cnt; for (int i = 0; i < cnt; i++) { if (!(a[i] >= ave - 2 * stand && a[i] <= ave + 2 * stand)) { cntt--; a[i] = 0; } } return average(a, cntt); } int Good_read(int PIN, int cnt) { int a[cnt]; for (int i = 0; i < cnt; i++) a[i] = analogRead (PIN); return judge_point(a, cnt); } float min1(float a, float b) { return a <= b ? a : b; }
void color() { if(color_analyse()=='R')colorflag=1; if(color_analyse()=='G')colorflag=2; if(color_analyse()=='B')colorflag=3; }
char color_analyse() { int cnt = 50; int rColor1[cnt] ; int gColor1[cnt] ; int bColor1[cnt] ; int rColor ; int gColor ; int bColor ; double r_ratio = 3.45, g_ratio = 2.62, b_ratio = 2.98; double r_sum = 0, g_sum = 0, b_sum = 0;
for (int i = 0; i < cnt; i++) { read_color(&rColor1[i], &gColor1[i], &bColor1[i]); } rColor = judge_point(rColor1, cnt); gColor = judge_point(gColor1, cnt); bColor = judge_point(bColor1, cnt); Serial.print(rColor * r_ratio); Serial.print(" "); Serial.print(gColor * g_ratio); Serial.print(" "); Serial.print(bColor * b_ratio); Serial.print(" "); if ( rColor * r_ratio < gColor * g_ratio && rColor * r_ratio < bColor * b_ratio) { Serial.println("R"); return 'R'; } else if (gColor * g_ratio < rColor * r_ratio && gColor * g_ratio < bColor * b_ratio) { Serial.println("G"); return 'G';
} else if (bColor * b_ratio < rColor * r_ratio && bColor * b_ratio < gColor * g_ratio) { Serial.println("B"); return 'B'; } }
void read_color(int *rColor, int *gColor, int *bColor) { int cnt = 1; int tmp_r = 0, tmp_g = 0, tmp_b = 0; for (int i = 0; i < cnt; i++) { digitalWrite(13, 1); delay(3); digitalWrite(s2, LOW); digitalWrite(s3, LOW); pulseWidth = pulseIn(outPin, LOW); tmp_r += pulseWidth; delay(3);
digitalWrite(s2, HIGH); digitalWrite(s3, HIGH); pulseWidth = pulseIn(outPin, LOW); tmp_g += pulseWidth; delay(3);
digitalWrite(s2, LOW); digitalWrite(s3, HIGH); pulseWidth = pulseIn(outPin, LOW); tmp_b += pulseWidth; digitalWrite(13, 0); delay(3); } *rColor = tmp_r / cnt; *gColor = tmp_g / cnt; *bColor = tmp_b / cnt; }
void forward(void){ analogWrite(leftmotor1,210); analogWrite(leftmotor2,0); analogWrite(rightmotor1,210); analogWrite(rightmotor2,0); }
void turn_right(void){ analogWrite(leftmotor1,0); analogWrite(leftmotor2,200); analogWrite(rightmotor1,255); analogWrite(rightmotor2,0); }
void turn_left(void){ analogWrite(leftmotor1,255); analogWrite(leftmotor2,0); analogWrite(rightmotor1,0); analogWrite(rightmotor2,200); }
void RUN(void){ analogWrite(leftmotor1,255); analogWrite(leftmotor2,0); analogWrite(rightmotor1,255); analogWrite(rightmotor2,0); }
void STOP(void){ analogWrite(leftmotor1,0); analogWrite(leftmotor2,0); analogWrite(rightmotor1,0); analogWrite(rightmotor2,0); }
int xy_Read() { int cnt = 50; int a[cnt]; for (int i = 0; i < cnt; i++) a[i] = Good_read(PIN_y, cnt); return StandardDeviation(a, cnt); }
void xy_Init() { pinMode(PIN_x, INPUT); pinMode(PIN_y, INPUT); }
void ServoControl(int servoAngle) { double thisAngle = map(servoAngle, 0, 270, 500, 2500); unsigned char i = 50; while (i--) { digitalWrite(ServoPin, HIGH); delayMicroseconds(thisAngle); digitalWrite(ServoPin, LOW); delayMicroseconds(20000 - thisAngle); } }
long map(long x,long in_min,long in_max,long out_min,long out_max) { return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; }
|