IMU-pressure-tempreture sensors

Dependencies:   CMSIS_DSP_401 DHT22 MPU9150_DMP QuaternionMath MODSERIAL mbed-src FATFileSystem111 SDFileSystem11 Camera_LS_Y201_CANSAT

Dependents:   combined_F

Revision:
4:dbb8e901826d
Parent:
1:339ebc8786ca
--- a/main.cpp	Tue Aug 18 00:42:31 2015 +0000
+++ b/main.cpp	Wed Sep 09 14:38:04 2015 +0000
@@ -3,28 +3,287 @@
 #include "BMP085.h"
 #include "DHT22.h"
 #include "main.h"
+
+   
+#include "Camera_LS_Y201.h"
+#include "SDFileSystem.h"
   char ns, ew, tf, status;
     int fq, nst, fix, date;                                     // fix quality, Number of satellites being tracked, 3D fix
     float latitude, longitude, timefix, speed, altitude;
-
+Serial xbee(p28,p27);
 
 
 DHT22 dht22(p23);
-BMP085 bmp085(p28, p27,BMP085_oss8);
-Serial xbee(p9,p10);
-DigitalOut rst(p11,PullUp);
-DigitalOut myled(LED1);
+BMP085 bmp085(p9, p10,BMP085_oss8);
+
+
+//DigitalOut myled(LED1);
 
 Serial debug(USBTX, USBRX);
-MPU9150 imu(p27, p28, p15);
+MPU9150 imu(p10, p9, p15);
+/////////////////////////////////////////////////////
+#define DEBMSG      debug.printf
+#define NEWLINE()   debug.printf("\r\n")
+
+#define USE_SDCARD 0
+
+#if USE_SDCARD
+#define FILENAME    "/sd/IMG_%04d.jpg"
+SDFileSystem fs(p5, p6, p7, p8, "sd");
+#else
+#define FILENAME    "/local/IMG_%04d.jpg"
+LocalFileSystem fs("local");
+#endif
+Camera_LS_Y201 cam1(p17, p18);
+
+int pow(int x,int y)
+{
+ int z=x;
+ for ( i=1;i<y;i++)
+ {
+   z *=x;   
+ }
+ 
+ return z;
+}
+typedef struct work {
+    FILE *fp;
+} work_t;
+
+work_t work;
+void converter(double x)
+{
+    int i;
+    x*=pow(10,6);
+     int z;
+     for(i=9;i>=0;i--)
+     {
+       if(i==5){
+           xbee.printf(".");
+           wait(0.02);
+           }
+       z = x/(pow(10,i));
+       x =x-(z*pow(10,i));
+       xbee.printf("%d",z);
+     wait(0.02);
+     }
+
+}
+  
+  
+    uint8_t send[16] = {
+        0x56,
+        0x00,
+        0x32,
+        0x0C,
+        0x00,
+        0x0A,
+        0x00,
+        0x00,
+        0x00, // MH
+        0x00, // ML
+        0x00,
+        0x00,
+        0x00, // KH
+        0x00, // KL
+        0x00, // XX
+        0x00  // XX
+    };
+   
+    uint16_t x = 10;    // Interval time. XX XX * 0.01m[sec]
+    bool end = true;
+    uint16_t m = 0; // Staring address.
+     /*
+     * Get the data size.
+     */
+uint8_t body[16000];
+uint16_t k = sizeof(body);   
+    int siz_done = 0;
+    int siz_total = 0;
+    int cnt = 0;
+    int z;
+//////////////////////////////////////////
+void callback_func(int done, int total, uint8_t *buf, size_t siz) {
+    
+   fwrite(buf, siz, 1, work.fp);
+
+//for(int i=0;i<siz;i++)
+//{
+//xbee.printf("%x",buf[i]);
+//fprintf(work.fp,"%c",buf[i]);
+ //}
+ 
+    static int n = 0;
+    int tmp = done * 100 / total;
+    if (n != tmp) {
+        n = tmp;
+        DEBMSG("Writ%3d%%", n);
+        NEWLINE();
+   }
+}
+
+
+
+////////////////////////////////////////////    
+
+
+
+
+   void readdJpegFileContent(void (*func)(int done, int total, uint8_t *buf, size_t siz)) {
+
+
+
+if(m==0)
+{
+z=sizeof(body); 
+ }  
+    
+    //if (r != NoError) {
+      //  return r;
+    //}
 
+    
+        send[8] = (m >> 8) & 0xff;
+        send[9] = (m >> 0) & 0xff;
+        send[12] = (k >> 8) & 0xff;
+        send[13] = (k >> 0) & 0xff;
+        send[14] = (x >> 8) & 0xff;
+        send[15] = (x >> 0) & 0xff;
+        /*
+         * Send a command.
+         */
+        cam1.sendBytes(send, sizeof(send), 200 * 1000) ;
+       // printf("sended header");
+        //    return SendError;
+        
+        /*
+         * Read the header of the response.
+         */
+        uint8_t header[5];
+        cam1.recvBytes(header, sizeof(header), 2 * 1000 * 1000);
+        //    return RecvError;
+        //printf("recved header");
+        /*
+         * Check the response and fetch an image data.
+         */
+         
+        if ((header[0] == 0x76)
+                && (header[1] == 0x00)
+                && (header[2] == 0x32)
+                && (header[3] == 0x00)
+                && (header[4] == 0x00)) {
+          
+            cam1.recvBytes(body, z, 2 * 1000 * 1000); //{
+                //return RecvError;
+           // }
+         //   printf("saved");
+            
+            siz_done += z;
+            
+            if (func != NULL) {
+                if (siz_done > siz_total) {
+                    z=sizeof(body)-siz_done+siz_total+3;
+                    siz_done = siz_total;
+                    
+                    
+                }
+                func(siz_done, siz_total, body, z);
+            }
+            for (int i = 1; i < z; i++) {
+                if ((body[i - 1] == 0xFF) && (body[i - 0] == 0xD9)) {
+                    end = true;
+                    //printf("saved%d",i);
+                }
+            }
+        } //else {
+           // return UnexpectedReply;
+        //}
+        /*
+         * Read the footer of the response.
+         */
+        uint8_t footer[5];
+        cam1.recvBytes(footer, sizeof(footer), 2 * 1000 * 1000);// {
+        //    return RecvError;
+        //}
+
+        m += z;
+    
+   // return NoError;
+}
+
+//////////////////
+/**
+ * Capture.
+ *
+ * @param cam A pointer to a camera object.
+ * @param filename The file name.
+ *
+ * @return Return 0 if it succeed.
+ */
+int capture(Camera_LS_Y201 *cam, char *filename) {
+    /*
+     * Take a picture.
+     */
+if(end==true)
+{
+    if (cam->takePicture() != 0) {
+        return -1;
+    }
+    cam1.readJpegFileSize(&siz_total);
+//    printf("%d",siz_total);
+    siz_done = 0;
+
+      
+    DEBMSG("Captured.");
+    NEWLINE();
+    work.fp = fopen(filename, "wb");
+    //p1= fopen("/sd/IMG_11111.txt", "wb");
+    if (work.fp == NULL) {
+        return -2;
+    }
+
+    /*
+     * Read the content.
+     */
+    DEBMSG("%s", filename);
+    NEWLINE();
+end=false;
+}
+    /*
+     * Open file.
+     */
+    
+    readdJpegFileContent(callback_func);
+       // fclose(work.fp);
+        //return -3;
+    
+    
+
+    /*
+     * Stop taking pictures.
+     */
+ if(end== true)
+ {
+    fclose(work.fp);
+    cam->stopTakingPictures();
+
+DEBMSG("[%04d]:OK.", cnt);
+            NEWLINE();
+m=0;
+cnt++;
+//wait(1);
+//xbee.printf("saved");
+}
+    return 0;
+}
+//////////////////////////////////////////////////////
 DigitalOut led(LED1);
 void Init()
 {
     gps.baud(9600);
-  //  pc.baud(115200);
+    debug.baud(115200);
 
-    xbee.printf("Init OK\n");
+  //  xbee.printf("Init OK\n");
     printf("Init OK\n");
 }
 
@@ -32,16 +291,38 @@
 int e=6;
 int n=0;
 int main(){
+     xbee.baud(115200);
+    pc.baud(115200);
     
+DEBMSG("Camera module");
+    NEWLINE();
+    DEBMSG("Resetting...");
+    NEWLINE();
+    wait(1);
+
+    if (cam1.reset() == 0) {
+        DEBMSG("Reset OK.");
+        NEWLINE();
+    } else {
+        DEBMSG("Reset fail.");
+        NEWLINE();
+        error("Reset fail.");
+    }
+
+
+    wait(1);
+    cam1.baud();
+    int j=1;
+    int g=1;
 Init();
     char c;        
 //debug.baud(115200);
     
     if(imu.isReady()){
-        xbee.printf("MPU9150 is ready\r\n");
+        //xbee.printf("MPU9150 is ready\r\n");
         printf("MPU9150 is ready\r\n");
     } else {
-        xbee.printf("MPU9150 initialisation failure\r\n");
+        //xbee.printf("MPU9150 initialisation failure\r\n");
         printf("MPU9150 initialisation failure\r\n");
     }
     
@@ -59,19 +340,21 @@
   //  int g=10;
     
     while(true){
-      n++;  
+     Timer timer1;
+     timer1.start();
+     // n++;  
       // wait(0.5);   
      bmp085.update();
         float allltitude=bmp085.calcAltitude(bmp085.get_pressure()*100);
       //wait(0.2);
-       if(e==6)
-       {
+       //if(e==6)
+       //{
         dht22.sample() ;
          hum=dht22.getHumidity()/10.0;
       temp=dht22.getTemperature()/10.0;
 
-      e=1;
-      }
+      //e=1;
+      //}
           
 
         if(imu.getFifoCount() >= 48){
@@ -139,32 +422,130 @@
          }
         
         
-        if(n==10)
-        {
-        xbee.printf("w:%f, v.x:%f, v.y:%f, v.z:%f\r\n", q1.w, q1.v.x, q1.v.y, q1.v.z);
-          
+       // if(n==10)
+        //{
+ //       xbee.printf("w:%f, v.x:%f, v.y:%f, v.z:%f\r\n", q1.w, q1.v.x, q1.v.y, q1.v.z);
+xbee.printf(",");
+wait(0.02);
+xbee.printf("p");
+wait(0.02);
+float x =bmp085.get_pressure();
+converter( x);         
+wait(0.02);
+xbee.printf(",");
+wait(0.02);
+xbee.printf("t");
+wait(0.02);
+x =(bmp085.get_temperature()+temp)/2;
+
+
+converter( x);
+wait(0.02); 
+xbee.printf(",");
+wait(0.02);
+xbee.printf("h");
+wait(0.02);
+x=hum;
+converter( x);         
+wait(0.02);
+xbee.printf(",");
+wait(0.02);
+xbee.printf("A");  //A for altitude from pressure sensor
+wait(0.02);
+x =allltitude;
+
+converter( x);
+//imu variables q1.w, q1.v.x, q1.v.y, q1.v.z
+         
+wait(0.02);
+xbee.printf(",");
+wait(0.02);
+xbee.printf("w");  
+wait(0.02);
+x =q1.w;
 
-xbee.printf("p:%f hPa / t:%f / altitude=%f \n\r",bmp085.get_pressure() , bmp085.get_temperature(),allltitude);
+converter( x); 
+wait(0.02);
+xbee.printf(",");
+wait(0.02);
+xbee.printf("x"); 
+wait(0.02);
+x =q1.v.x;
+
 
-     xbee.printf("temp: %f  , hum:%f    \n\r",temp,hum);
+converter( x); 
+wait(0.02);
+xbee.printf(",");
+wait(0.02);
+xbee.printf("y");  
+wait(0.02);
+x =q1.v.y;
+converter(x);
+wait(0.02);
+xbee.printf(",");
+wait(0.02);
+xbee.printf("z");  
+wait(0.02);
+x =q1.v.z;
+converter(x);
+// gps variables latitude,longitude
+wait(0.02);
+xbee.printf(",");
+wait(0.02);
+xbee.printf("a");  
+wait(0.02);
+x = latitude;
+converter(x);
+wait(0.02);
+xbee.printf(",");
+wait(0.02);
+xbee.printf("o");  
+wait(0.02);
+x = longitude;
+converter(x);
+//xbee.printf("p:%f hPa / t:%f / altitude=%f \n\r",bmp085.get_pressure() , bmp085.get_temperature(),allltitude);
+
+  //   xbee.printf("temp: %f  , hum:%f    \n\r",temp,hum);
        
        
-       xbee.printf("GPGGA Fix taken at: %f, Latitude: %f %c, Longitude: %f %c, Fix quality: %d, Number of sat: %d, Altitude: %f M\n", timefix, latitude, ns, longitude, ew, fq, nst, altitude);
-     xbee.printf("GPGSA Type fix: %c, 3D fix: %d, number of sat: %d\r\n", tf, fix, nst);
-    xbee.printf("GPGLL Latitude: %f %c, Longitude: %f %c, Fix taken at: %f\n", latitude, ns, longitude, ew, timefix);
-   xbee.printf("GPRMC Fix taken at: %f, Status: %c, Latitude: %f %c, Longitude: %f %c, Speed: %f, Date: %d\n\n\r\n\n\n", timefix, status, latitude, ns, longitude, ew, speed, date); 
-n=1;
+    //   xbee.printf("a: %f, o: %f", latitude,longitude);
+      //////////////////////////////////////////////////////// 
+     //xbee.printf("GPGSA Type fix: %c, 3D fix: %d, number of sat: %d\r\n", tf, fix, nst);
+   // xbee.printf("GPGLL Latitude: %f %c, Longitude: %f %c, Fix taken at: %f\n", latitude, ns, longitude, ew, timefix);
+ //  xbee.printf("GPRMC Fix taken at: %f, Status: %c, Latitude: %f %c, Longitude: %f %c, Speed: %f, Date: %d\n\n\r\n\n\n", timefix, status, latitude, ns, longitude, ew, speed, date); 
+//n=1;
      
-  }
+  //}
+
             
       printf("w:%f, v.x:%f, v.y:%f, v.z:%f\r\n", q1.w, q1.v.x, q1.v.y, q1.v.z);
-printf("p:%f hPa / t:%f / altitude=%f \n\r",bmp085.get_pressure() , bmp085.get_temperature(),allltitude);
+    printf("p:%f hPa / t:%f / altitude=%f \n\r",x , bmp085.get_temperature(),allltitude);
      printf("temp: %f  , hum:%f    \n\r",temp,hum);
-        printf("GPGGA Fix taken at: %f, Latitude: %f %c, Longitude: %f %c, Fix quality: %d, Number of sat: %d, Altitude: %f M\n", timefix, latitude, ns, longitude, ew, fq, nst, altitude);
+  printf("GPGGA Fix taken at: %f, Latitude: %f %c, Longitude: %f %c, Fix quality: %d, Number of sat: %d, Altitude: %f M\n", timefix, latitude, ns, longitude, ew, fq, nst, altitude);
      printf("GPGSA Type fix: %c, 3D fix: %d, number of sat: %d\r\n", tf, fix, nst);
     printf("GPGLL Latitude: %f %c, Longitude: %f %c, Fix taken at: %f\n", latitude, ns, longitude, ew, timefix);
    printf("GPRMC Fix taken at: %f, Status: %c, Latitude: %f %c, Longitude: %f %c, Speed: %f, Date: %d\n\n\r\n\n\n", timefix, status, latitude, ns, longitude, ew, speed, date); 
-  
+
+    char fname[64];
+     //  xbee.printf("hello%d\n\r",g);
+       
+       if((end==true)||(j==1))
+       {
+        
+        snprintf(fname, sizeof(fname) - 1, FILENAME, cnt);
+        j=0;
+        }
+        capture(&cam1, fname);
+        //if (r == 0) {
+            
+        
+        //    }
+        //} else {
+            //DEBMSG("[%04d]:NG. (code=%d)", cnt, r);
+           // NEWLINE();
+           // error("Failure.");
+        //}
+        g++;
        
        
          
@@ -182,9 +563,18 @@
                 debug.putc((char)0);
             }
             */
-       }   
-    //wait(1);
-e++;
+       }
+       
+       while(1)
+       {
+           if(timer1.read_ms()>=2000)
+           {
+               timer1.reset();
+               break;   
+    }
+    }
+    
+//e++;
 //g++;
  //xbee.printf("this is the end");
     //rst=1;