Source-code for cansat implementation in Bibliotheca Alexandrina, Alexandria, Egypt.
Dependencies: JPEGCamera MODGPS RHT03 mbed
main.cpp
00001 #include "mbed.h" 00002 #include "JPEGCamera.h" 00003 #include "RHT03.h" 00004 #include "GPS.h" 00005 00006 00007 DigitalOut myled1(LED1); //show successful picture was taken 00008 DigitalOut myled2(LED2); //show end of sequence 00009 DigitalOut myled3(LED3); //show picture take failed 00010 DigitalOut myled4(LED4); //show camera is not ready 00011 00012 Serial pc(USBTX, USBRX); // tx, rx 00013 /*Xbee Start*/ 00014 00015 Serial XBee(p13, p14); 00016 00017 00018 // SET THIS. 00019 // Create an instance of the GPS object. You will need to 00020 // set p27 to whichever Serial RX pin you have connected 00021 // your GPS module to. 00022 GPS gps(NC,p27); 00023 00024 char rmc[GPS_BUFFER_LEN]; 00025 char gga[GPS_BUFFER_LEN]; 00026 char vtg[GPS_BUFFER_LEN]; 00027 char ukn[GPS_BUFFER_LEN]; 00028 00029 // 0.1 second flash of LED2 00030 DigitalOut led2(LED2); 00031 Timeout t2; 00032 void t2out(void) 00033 { 00034 led2 = 0; 00035 } 00036 void blip2(void) 00037 { 00038 led2 = 1; 00039 t2.attach(&t2out, 0.1); 00040 } 00041 00042 // 0.1 second flash of LED3 00043 DigitalOut led3(LED3); 00044 Timeout t3; 00045 void t3out(void) 00046 { 00047 led3 = 0; 00048 } 00049 void blip3(void) 00050 { 00051 led3 = 1; 00052 t3.attach(&t3out, 0.1); 00053 } 00054 00055 // 0.1 second flash of LED4 00056 DigitalOut led4(LED4); 00057 00058 Timeout t4; 00059 void t4out(void) 00060 { 00061 led4 = 0; 00062 } 00063 void blip4(void) 00064 { 00065 led4 = 1; 00066 t4.attach(&t4out, 0.1); 00067 } 00068 ///////////////////////////////////////////////////////////// 00069 00070 int main() 00071 { 00072 00073 GPS_Time q1; 00074 // SET THIS. 00075 // Ensure you set the baud rate to match your serial 00076 // communications to your PC/Max/Linux host so you 00077 // can read the messages. 00078 pc.printf("setting the PC baud rate"); 00079 pc.baud(9600); 00080 00081 00082 // Tell MODGPS "we want copies of the NMEA sentences". When a callback 00083 // is made our buffers will contain a copy of the last received sentence 00084 // before it was processed/destroyed. 00085 gps.setRmc(rmc); 00086 gps.setGga(gga); 00087 gps.setVtg(vtg); 00088 gps.setUkn(ukn); 00089 00090 00091 // SET THIS. 00092 // Most GPS modules use 9600,8,n,1 so that's what 00093 // we default to here. Ensure your GPS module matches 00094 // this, otherwise set it to match. 00095 pc.printf("setting the GPS baud rate"); 00096 gps.baud(9600); 00097 gps.format(8, GPS::None, 1); 00098 00099 // OPTIONAL 00100 // If you GPS has a 1 pulse per second output you can 00101 // connect it to an Mbed pin. Here you specify what pin 00102 // and on what "edge" teh signal is active. If your GPS 00103 // module has a rising edge at the one second point then 00104 // use GPS::ppsRise 00105 #ifdef PPSPIN 00106 gps.ppsAttach(PPSPIN, GPS::ppsFall); 00107 #endif 00108 00109 // Sample of a callback to a function when the 1PPS activates. 00110 // For this example, we flash LED2 for 0.1 second. 00111 gps.attach_pps(&blip2); 00112 00113 // Sample of a callback to a function when a NMEA GGA message is recieved. 00114 // For this example, we flash LED2 for 0.1 second. 00115 gps.attach_gga(&blip3); 00116 00117 // Sample of a callback to a function when a NMEA RMC message is recieved. 00118 // For this example, we flash LED2 for 0.1 second. 00119 gps.attach_rmc(&blip4); 00120 00121 00122 00123 // Camera Code 00124 pc.printf("Loading camera module"); 00125 JPEGCamera camera(p9, p10); // TX, RX 00126 LocalFileSystem local("local"); //save images on mbed 00127 camera.setPictureSize(JPEGCamera::SIZE320x240); 00128 pc.printf("getting ready for the while loop!"); 00129 int counter = 0; 00130 while(counter <1 ) { 00131 counter++; 00132 // GPS Code 00133 pc.printf("Turning inside the loop()"); 00134 // Every 3 seconds, flip LED1 and print the basic GPS info. 00135 pc.printf("hello GPS"); 00136 wait(3); 00137 myled1 = 1; 00138 00139 // Demonstrate how to find out the GPS location co-ords. 00140 pc.printf("Method 1. Lat = %.4f ", gps.latitude()); 00141 pc.printf("Lon = %.4f ", gps.longitude()); 00142 pc.printf("Alt = %.4f ", gps.altitude()); 00143 00144 // Gran a snapshot of the current time. 00145 gps.timeNow(&q1); 00146 pc.printf("%02d:%02d:%02d %02d/%02d/%04d\r\n", 00147 q1.hour, q1.minute, q1.second, q1.day, q1.month, q1.year); 00148 00149 // Alternative method that does the same thing. 00150 pc.printf("Method 2. Lat = %.4f ", gps.latitude()); 00151 pc.printf("Lon = %.4f ", gps.longitude()); 00152 pc.printf("Alt = %.4f ", gps.altitude()); 00153 00154 GPS_Time *q2 = gps.timeNow(); 00155 pc.printf("%02d:%02d:%02d %02d/%02d/%04d\r\n\n", 00156 q2->hour, q2->minute, q2->second, q2->day, q2->month, q2->year); 00157 delete(q2); 00158 myled1 = 0; 00159 00160 00161 //////////////////// 00162 00163 //Temperature Sensor Code 00164 pc.printf("hello TEMP"); 00165 int done=0; 00166 float temp,hum; 00167 00168 RHT03 humtemp(p24); //Initalise the RHT03 (change pin number to the pin its connected to) 00169 00170 while(!done) { //Loop keeps running until RHT03 is read succesfully 00171 wait(2); //Needed to make sure the sensor has time to initalise and so its not polled too quickly 00172 if(humtemp.readData() == RHT_ERROR_NONE) done=1; //Request data from the RHT03 00173 } 00174 00175 temp = humtemp.getTemperatureC(); //Gets the current temperature in centigrade 00176 hum = humtemp.getHumidity(); //Gets the current humidity in percentage 00177 00178 pc.printf("Temperature is %f C \r\n",temp); 00179 pc.printf("Humidity is %f % \r\n",hum); 00180 wait(3); 00181 ///////////////////////////////////////////////// 00182 00183 //camera Code 00184 Timer timer; 00185 pc.printf("starting timer..."); 00186 timer.start(); 00187 pc.printf("hello CAMERA"); 00188 for (int i = 1; i < 5; i++) { 00189 if (camera.isReady()) { 00190 char filename[32]; 00191 sprintf(filename, "/local/pict%03d.jpg", i); 00192 printf("Picture: %s ", filename); 00193 if (camera.takePicture(filename)) { 00194 while (camera.isProcessing()) { 00195 camera.processPicture(); 00196 } 00197 myled1 = 1; //show successful picture was taken 00198 wait(2.0); 00199 myled1 = 0; 00200 } else { 00201 printf("take picture failed\n"); 00202 myled3 = 1; //show picture take failed 00203 wait(2.0); 00204 myled3 = 0; 00205 } 00206 } else { 00207 printf("camera is not ready\n"); 00208 myled4 = 1; //show camera is not ready 00209 wait(2.0); 00210 myled4 = 0; 00211 } 00212 } 00213 myled2 = 1; //show end of sequence 00214 wait(2.0); 00215 myled2 = 0; 00216 printf("time = %f\n", timer.read()); 00217 //////////////////////////////////////////////////// 00218 00219 //Xbee Code 00220 00221 pc.printf("i'm Sending.."); 00222 XBee.printf("Hello"); 00223 wait(1.0); 00224 00225 ///////////////////////////////////////////// 00226 00227 00228 } 00229 }
Generated on Fri Jul 22 2022 03:16:51 by 1.7.2