Ok

Dependencies:   mbed_rtos_types Mutex mbed_rtos_storage mbed Semaphore

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "TCS34725.h"
00004 #include "MBed_Adafruit_GPS.h"
00005 #include "Datas.h"
00006 
00007 
00008 Serial pc(USBTX, USBRX, 115200); //115200 bauds
00009 DigitalOut ledR(PH_0);// //RGB led - red light
00010 DigitalOut ledG(PH_1);  //RGB led - green light
00011 DigitalOut ledB(PB_13);  //RGB led - blue light
00012 Serial gps(PA_9,PA_10);//
00013 extern AnalogIn soil_moist; //Analog soil moisture sensor
00014 extern AnalogIn light_sensor; //Analog light sensor
00015 extern I2C RGB; // RGB sensor
00016 extern DigitalOut ledColour;//Colour sensor led
00017 
00018 
00019 InterruptIn button(PB_2); // Interruption button
00020 DigitalOut myled1(LED1, PullDown);
00021 DigitalOut myled2(LED2, PullDown);
00022 DigitalOut myled3(LED3, PullDown);
00023 DigitalOut myled4(LED4, PullDown);
00024 
00025 
00026 
00027 
00028 Thread ThreadI2c(osPriorityNormal, 512); //I2c thread
00029 Thread ThreadAnalog(osPriorityNormal, 512); //Analog readings thread
00030 
00031 
00032 
00033 Ticker t;// This ticker calls the printing methods in the main thread
00034 bool normal=false; //True while in normal mode
00035 int iteration=0;// Number of iteration (from 0 to 500 in TEST mode; from 0 to 119 in NORMAL mode)
00036 int analog_timer=2000; // Timer of the analog thread (Starts in 2s: TEST mode) 
00037 int I2c_timer=2000; // Timer of the I2c thread
00038 bool readData =  false; // set to true when ticker t
00039 bool hour_past = false; // set to true when normal==1 & iteration==120 (an hour has past)
00040 extern uint8_t dominant_color[]; //Array that keeps count of how many times each color was dominant in the reading
00041 
00042 void read_data(void) // Ticker method
00043 {
00044     readData =  true;
00045     if(iteration == 4){
00046         if(normal==true){
00047         hour_past = true; //An hour has past
00048         }
00049      }
00050 }
00051 //Interrupt method. Changes between TEST & NORMAL modes
00052 void switch_mode(){
00053  if(normal==true){
00054      t.attach(&read_data, 2.0);
00055      analog_timer=2000;
00056      I2c_timer=2000;
00057      //Cleans the array so there are no old readings from previous NORMAL mode sessions
00058      dominant_color[0]=0;
00059      dominant_color[1]=0;
00060      dominant_color[2]=0;
00061      }
00062  else{
00063      //Changes the timing of operations
00064      t.attach(&read_data,2);
00065      analog_timer=2000;
00066      I2c_timer=2000;
00067      ledR=1;
00068      ledB=1;
00069      ledG=1;
00070      }
00071  iteration=0; //resets iteration
00072  myled1=!myled1;
00073  myled2=!myled2;   
00074  normal = !normal;   
00075  }
00076 
00077 
00078 extern int clear_value, green_value, red_value, blue_value; //Colour sensor values
00079 extern float temp_value, hum_value; //AnalogIn values in % 
00080 extern float x,y,z;//Accelerometer values (X, Y and Z)
00081 
00082 extern float soil;//Soil moisture
00083 extern float light;//Light intensity
00084 extern char getMax(int r, int g, int b);//Method to get the colour of most intensity
00085 char c;//Latest GPS character received
00086 char colour_max; //Colour of most intensity
00087 
00088 extern void analogThread();//Method of the analog thread
00089 extern void I2cThread();//Method of the I2c thread
00090 extern void gpsParse();//Parse and print the GPS values
00091 extern void accMinMax(float a,float b, float c);//Keeps track of the minimum and maximum values of each axis of the accelerometer
00092 extern void printAccMinMax();//Prints accelerometer maximum and minumim of each axis
00093 extern void setLedColour();//Sets the colour of the RGB led to the dominant color of the latest reading
00094 extern void addDominantColor();//Keeps track of how many times every colour is dominant
00095 extern void dominantColor();//Checks the dominant (or dominants, in case of a tie) colour and prints it
00096 extern void thresshold();//Checks if the received values exceed any thresshold and sets the corresponding RGB led colour light.
00097 
00098           
00099 //Prints analog reading values          
00100 void printAnalog(){
00101 pc.printf("Soil Moisture: %.0f %% \n\rLight: %f %%\n\r", soil, light );
00102 }
00103 //Prints RGB sensor values
00104  void printRGB(){
00105 // Print RGB sensor readings
00106 pc.printf("Clear (%d), Red (%d), Green (%d), Blue (%d)\n\r", clear_value, red_value, green_value, blue_value);
00107 }
00108  //Prints temperature and humidity values
00109    void printTempHum(){
00110        //print TemHum readings
00111        pc.printf("Temperature: %.0f C \n\rHumidity: %.0f %%\n\r", temp_value, hum_value);
00112     }
00113     //Prints accelerometer values
00114     void printAcc(){
00115         pc.printf("AccX = %f    AccY = %f    AccZ = %f\n\r", x, y, z);
00116         }
00117    
00118 int main()
00119 {       
00120     button.rise(switch_mode);
00121     //Objects of Data Class
00122     Datas hum ("Humidity"); 
00123     Datas temp ("Temperature"); 
00124     Datas light_v("Light");
00125     Datas soil_m ("Soil Moisture");
00126     //Starts in TEST mode; Turn on LED1; Timer = 2s
00127     myled1=1;
00128     myled2=0;
00129     myled3=0;
00130     myled4=0;
00131     
00132     t.attach(&read_data, 2);
00133     ThreadI2c.start(I2cThread);
00134     ThreadAnalog.start(analogThread);
00135   
00136   //Sets up the GPS
00137    Adafruit_GPS myGPS(&gps);
00138    myGPS.begin(9600);
00139 
00140    myGPS.sendCommand("$PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0*28\r\n"); // GGCA and RMC messages ONLY 
00141    wait(0.2); 
00142    myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); //Set update rate
00143     wait(0.2); 
00144    //myGPS.sendCommand(PMKT_SET_BAUD_115200); 
00145     wait(0.2); 
00146    myGPS.sendCommand(PGCMD_NOANTENNA); //Toggle GTOP antenna messages
00147         
00148         
00149     // Initialize color sensor
00150 
00151     // Timing register address 0x01 (0000 0001). We set 1st bit to 1 -> 1000 0001
00152     char timing_register[2] = {0x81,0x50}; //0x50 ~ 400ms
00153     RGB.write(RGB_ADDR,timing_register,2,false);
00154 
00155     // Control register address 0x0F (0000 1111). We set 1st bit to 1 -> 1000 1111
00156     char control_register[2] = {0x8F,0}; //{0x8F, 0x00}, {1000 1111, 0000 0000} -> 1x gain
00157     RGB.write(RGB_ADDR,control_register,2,false);
00158 
00159     // Enable register address 0x00 (0000 0000). We set 1st bit to 1 -> 1000 0000
00160     char enable_register[2] = {0x80,0x03}; //{0x80, 0x03}, {1000 0000, 0000 0011} -> AEN = PON = 1
00161     RGB.write(RGB_ADDR,enable_register,2,false);
00162     
00163     while(1){
00164        if (hour_past){ //An hour has past. Corresponding values are sent to the PC
00165            hour_past=false;
00166            hum.normalizeMinMaxAvg(iteration);
00167            temp.normalizeMinMaxAvg(iteration);
00168            soil_m.normalizeMinMaxAvg(iteration);
00169            light_v.normalizeMinMaxAvg(iteration);
00170            dominantColor();
00171            printAccMinMax();
00172           pc.printf("\n\r\n\rHOURLY SUMMARY\n\r\n\r");
00173           iteration=0;
00174           
00175      }      
00176            
00177            
00178            
00179            
00180       if (readData){
00181                  
00182         //Obtains which one is the greatest - red, green or blue
00183            colour_max = getMax(red_value, green_value, blue_value);
00184           pc.printf("Measure # %i\n\r", iteration);
00185            
00186           //NORMAL MODE 
00187                  if(normal){ 
00188                      pc.printf("NORMAL MODE\n\r");
00189                  temp.data_set[iteration]=temp_value;
00190                  hum.data_set[iteration]=hum_value;
00191                  soil_m.data_set[iteration]=soil;
00192                  light_v.data_set[iteration]=light;
00193                  accMinMax(x,y,z);
00194                  addDominantColor();
00195                  thresshold();
00196                  
00197           //TEST MODE     
00198                }
00199               else{
00200                      if (iteration==500){
00201                          iteration=0;
00202                          }
00203                      pc.printf("TEST MODE\n\r");
00204                      
00205                setLedColour();
00206                  
00207       
00208             }
00209   
00210   
00211      
00212   
00213     
00214 //Prints RGB values
00215   printRGB();
00216            
00217 //Prints Temp & RH values
00218   printTempHum();
00219 
00220 //Prints Acc values
00221  printAcc();
00222  
00223  //Prints Analog In values
00224  printAnalog();
00225  
00226     
00227        //check if we recieved a new message from GPS, if so, attempt to parse it,
00228        if ( myGPS.newNMEAreceived() ) {
00229            if ( myGPS.parse(myGPS.lastNMEA()) ) {
00230               //Prints the time in Madrid and other location values if FIX==1
00231            pc.printf("Time: %d:%d:%d.%u\n\r", myGPS.hour, myGPS.minute, myGPS.seconds, myGPS.milliseconds); 
00232            pc.printf("Fix: %d\n\r", (int) myGPS.fix);
00233            pc.printf("Quality: %d\n\r", (int) myGPS.fixquality);
00234            
00235            if (myGPS.fix) {
00236                pc.printf("Location: %5.2f%c, %5.2f%c\n\r", myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon);
00237                pc.printf("Altitude: %5.2f\n\r", myGPS.altitude);
00238                pc.printf("Satellites: %d\n\r", myGPS.satellites);
00239           }    
00240        }
00241        }
00242      
00243 
00244       
00245  pc.printf("\n\r\n\r");
00246  readData= false; //resets readData until next "tick"
00247  iteration++; //increases iteratios
00248   }
00249     c = myGPS.read();   //queries the GPS
00250   
00251        
00252 
00253        
00254 
00255        
00256        
00257 }
00258   
00259 }  
00260 
00261