HI
Dependencies: Motor TCS3472_I2C mbed
main.cpp@0:1cbed7d36912, 2015-04-24 (annotated)
- Committer:
- taylormooers
- Date:
- Fri Apr 24 00:53:50 2015 +0000
- Revision:
- 0:1cbed7d36912
- Child:
- 1:9eb8d4e6a719
GHFGH
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
taylormooers | 0:1cbed7d36912 | 1 | #include "mbed.h" //includes this library for all mbed functions |
taylormooers | 0:1cbed7d36912 | 2 | #include "TCS3472_I2C.h" //includes the library for the RGB sensor |
taylormooers | 0:1cbed7d36912 | 3 | #include "Motor.h" |
taylormooers | 0:1cbed7d36912 | 4 | Motor R(p24, p25, p26); |
taylormooers | 0:1cbed7d36912 | 5 | Motor L(p21, p22, p23); |
taylormooers | 0:1cbed7d36912 | 6 | |
taylormooers | 0:1cbed7d36912 | 7 | PwmOut RGBsensorLED(p22); //established pin to power the LED on the sensor |
taylormooers | 0:1cbed7d36912 | 8 | TCS3472_I2C RGBsensor(p9, p10); //establishes pins for the RGB sensor. |
taylormooers | 0:1cbed7d36912 | 9 | |
taylormooers | 0:1cbed7d36912 | 10 | |
taylormooers | 0:1cbed7d36912 | 11 | Serial pc(USBTX, USBRX); //establishes a serial port in order to communicate with TeraTerm and LATLAB |
taylormooers | 0:1cbed7d36912 | 12 | |
taylormooers | 0:1cbed7d36912 | 13 | int main() //starts the program |
taylormooers | 0:1cbed7d36912 | 14 | { |
taylormooers | 0:1cbed7d36912 | 15 | int RGB_data[4]; //declares an array to store the data read from the RGB sensor |
taylormooers | 0:1cbed7d36912 | 16 | float PWMbrightness = 1.0; //declares float for LED brightness |
taylormooers | 0:1cbed7d36912 | 17 | |
taylormooers | 0:1cbed7d36912 | 18 | RGBsensor.enablePowerAndRGBC(); //enables RGB sensor |
taylormooers | 0:1cbed7d36912 | 19 | RGBsensor.setIntegrationTime(100); //sets the intergration time of the RGB sensor |
taylormooers | 0:1cbed7d36912 | 20 | RGBsensorLED = PWMbrightness; //set brightness of sensor LED |
taylormooers | 0:1cbed7d36912 | 21 | pc.baud(921600); //sets the baud rate of the serial connection |
taylormooers | 0:1cbed7d36912 | 22 | |
taylormooers | 0:1cbed7d36912 | 23 | while (1) { //loops the program until is broken |
taylormooers | 0:1cbed7d36912 | 24 | |
taylormooers | 0:1cbed7d36912 | 25 | int colorcount[3] = {0,0,0}; //establishes an array for the possible regions the car is in |
taylormooers | 0:1cbed7d36912 | 26 | int scan = 0; //variable for for loop |
taylormooers | 0:1cbed7d36912 | 27 | |
taylormooers | 0:1cbed7d36912 | 28 | for (scan=0; scan<10; scan+=1) { |
taylormooers | 0:1cbed7d36912 | 29 | |
taylormooers | 0:1cbed7d36912 | 30 | RGBsensor.getAllColors(RGB_data); //read data from sensor for red, green, and blue along with magnitude |
taylormooers | 0:1cbed7d36912 | 31 | |
taylormooers | 0:1cbed7d36912 | 32 | int light = RGB_data[0]; //sets a variable for the light data so I don't have to type that every time |
taylormooers | 0:1cbed7d36912 | 33 | int red = RGB_data[1]; //sets a variable for the red data so I don't have to type that every time |
taylormooers | 0:1cbed7d36912 | 34 | int green = RGB_data[2]; //sets a variable for the blue data so I don't have to type that every time |
taylormooers | 0:1cbed7d36912 | 35 | int blue = RGB_data[3]; //sets a variable for the green data so I don't have to type that every time |
taylormooers | 0:1cbed7d36912 | 36 | |
taylormooers | 0:1cbed7d36912 | 37 | if ( (abs((1.6822*red + 15.609)-light) < 10) && (abs((4.2525*green - 37.993)-light) < 10) && (abs((6.5565*blue - 44.793 )-light) < 10) ) { |
taylormooers | 0:1cbed7d36912 | 38 | colorcount[0] += 1; //car is in red |
taylormooers | 0:1cbed7d36912 | 39 | L.speed(0.0); |
taylormooers | 0:1cbed7d36912 | 40 | R.speed(0.2); |
taylormooers | 0:1cbed7d36912 | 41 | } |
taylormooers | 0:1cbed7d36912 | 42 | |
taylormooers | 0:1cbed7d36912 | 43 | if ( (abs((3.7757*red - 134.86)-light) < 10) && (abs((3.4282*green - 8.791 )-light) < 10) && (abs((2.5685*blue + 63.205)-light) < 10) ) { |
taylormooers | 0:1cbed7d36912 | 44 | colorcount[1] += 1; //car is in blue |
taylormooers | 0:1cbed7d36912 | 45 | R.speed(0.5); |
taylormooers | 0:1cbed7d36912 | 46 | L.speed(0.5); |
taylormooers | 0:1cbed7d36912 | 47 | } |
taylormooers | 0:1cbed7d36912 | 48 | |
taylormooers | 0:1cbed7d36912 | 49 | if ( (abs((3.8225*red - 138.09)-light) < 10) && (abs((2.1032*green + 39.654)-light) < 10) && (abs((5.3619*blue - 2.7884)-light) < 10) ) { |
taylormooers | 0:1cbed7d36912 | 50 | colorcount[2] += 1; //car is in green |
taylormooers | 0:1cbed7d36912 | 51 | L.speed(0.2); |
taylormooers | 0:1cbed7d36912 | 52 | R.speed(0.0); |
taylormooers | 0:1cbed7d36912 | 53 | } |
taylormooers | 0:1cbed7d36912 | 54 | } |
taylormooers | 0:1cbed7d36912 | 55 | if (colorcount[0] >= 7) { //data reads in the red |
taylormooers | 0:1cbed7d36912 | 56 | pc.printf("Making a right turn. \n"); //print to tera term |
taylormooers | 0:1cbed7d36912 | 57 | |
taylormooers | 0:1cbed7d36912 | 58 | } |
taylormooers | 0:1cbed7d36912 | 59 | if (colorcount[1] >= 7) { //data reads in the blue |
taylormooers | 0:1cbed7d36912 | 60 | pc.printf("Driving straight. \n"); //print to tera term |
taylormooers | 0:1cbed7d36912 | 61 | |
taylormooers | 0:1cbed7d36912 | 62 | } |
taylormooers | 0:1cbed7d36912 | 63 | if (colorcount[2] >= 7) { //data reads in the green |
taylormooers | 0:1cbed7d36912 | 64 | pc.printf("Making a left turn. \n"); //print to tera term |
taylormooers | 0:1cbed7d36912 | 65 | |
taylormooers | 0:1cbed7d36912 | 66 | } |
taylormooers | 0:1cbed7d36912 | 67 | |
taylormooers | 0:1cbed7d36912 | 68 | |
taylormooers | 0:1cbed7d36912 | 69 | } |
taylormooers | 0:1cbed7d36912 | 70 | |
taylormooers | 0:1cbed7d36912 | 71 | } |