cansat

Dependencies:   Servo mbed-src FATFileSystem1515 SDFileSystem1515 Camera_LS_Y201_CANSAT

Fork of Camera_LS_Y201_TestProgram by Shinichiro Nakamura

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main2.cpp Source File

main2.cpp

00001   #include "mbed.h"
00002 #include "Camera_LS_Y201.h"
00003 #include "SDFileSystem.h"
00004 #include "Servo.h"
00005 
00006 Servo myservo(p23);
00007 //#include "FATFileSystem.h"
00008 Serial xbee(p28,p27);
00009 Serial pc(USBTX,USBRX);
00010 //FILE *p1;
00011  
00012   #define DEBMSG      pc.printf
00013 #define NEWLINE()   pc.printf("\r\n")
00014 
00015 #define USE_SDCARD 0
00016 
00017 #if USE_SDCARD
00018 #define FILENAME    "/sd/IMG_%04d.jpg"
00019 SDFileSystem fs(p5, p6, p7, p8, "sd");
00020 #else
00021 #define FILENAME    "/local/IMG_%04d.jpg"
00022 LocalFileSystem fs("local");
00023 #endif
00024 Camera_LS_Y201 cam1(p17, p18);
00025 
00026 typedef struct work {
00027     FILE *fp;
00028 } work_t;
00029 
00030 work_t work;
00031 
00032   
00033   
00034     uint8_t send[16] = {
00035         0x56,
00036         0x00,
00037         0x32,
00038         0x0C,
00039         0x00,
00040         0x0A,
00041         0x00,
00042         0x00,
00043         0x00, // MH
00044         0x00, // ML
00045         0x00,
00046         0x00,
00047         0x00, // KH
00048         0x00, // KL
00049         0x00, // XX
00050         0x00  // XX
00051     };
00052    
00053     uint16_t x = 10;    // Interval time. XX XX * 0.01m[sec]
00054     bool end = true;
00055     uint16_t m = 0; // Staring address.
00056      /*
00057      * Get the data size.
00058      */
00059 uint8_t body[16000];
00060 uint16_t k = sizeof(body);   
00061     int siz_done = 0;
00062     int siz_total = 0;
00063     int cnt = 0;
00064     int z;
00065 //////////////////////////////////////////
00066 void callback_func(int done, int total, uint8_t *buf, size_t siz) {
00067     
00068    // fwrite(buf, siz, 1, work.fp);
00069 
00070 for(int i=0;i<siz;i++)
00071 {
00072 //xbee.printf("%x",buf[i]);
00073 fprintf(work.fp,"%c",buf[i]);
00074  }
00075  
00076     static int n = 0;
00077     int tmp = done * 100 / total;
00078     if (n != tmp) {
00079         n = tmp;
00080         DEBMSG("Writ%3d%%", n);
00081         NEWLINE();
00082    }
00083 }
00084 
00085 
00086 
00087 ////////////////////////////////////////////    
00088 
00089 
00090 
00091 
00092    void readdJpegFileContent(void (*func)(int done, int total, uint8_t *buf, size_t siz)) {
00093 
00094 
00095 
00096 if(m==0)
00097 {
00098 z=sizeof(body); 
00099  }  
00100     
00101     //if (r != NoError) {
00102       //  return r;
00103     //}
00104 
00105     
00106         send[8] = (m >> 8) & 0xff;
00107         send[9] = (m >> 0) & 0xff;
00108         send[12] = (k >> 8) & 0xff;
00109         send[13] = (k >> 0) & 0xff;
00110         send[14] = (x >> 8) & 0xff;
00111         send[15] = (x >> 0) & 0xff;
00112         /*
00113          * Send a command.
00114          */
00115         cam1.sendBytes(send, sizeof(send), 200 * 1000) ;
00116        // printf("sended header");
00117         //    return SendError;
00118         
00119         /*
00120          * Read the header of the response.
00121          */
00122         uint8_t header[5];
00123         cam1.recvBytes(header, sizeof(header), 2 * 1000 * 1000);
00124         //    return RecvError;
00125         //printf("recved header");
00126         /*
00127          * Check the response and fetch an image data.
00128          */
00129          
00130         if ((header[0] == 0x76)
00131                 && (header[1] == 0x00)
00132                 && (header[2] == 0x32)
00133                 && (header[3] == 0x00)
00134                 && (header[4] == 0x00)) {
00135           
00136             cam1.recvBytes(body, z, 2 * 1000 * 1000); //{
00137                 //return RecvError;
00138            // }
00139          //   printf("saved");
00140             
00141             siz_done += z;
00142             
00143             if (func != NULL) {
00144                 if (siz_done > siz_total) {
00145                     z=sizeof(body)-siz_done+siz_total+3;
00146                     siz_done = siz_total;
00147                     
00148                     
00149                 }
00150                 func(siz_done, siz_total, body, z);
00151             }
00152             for (int i = 1; i < z; i++) {
00153                 if ((body[i - 1] == 0xFF) && (body[i - 0] == 0xD9)) {
00154                     end = true;
00155                     //printf("saved%d",i);
00156                 }
00157             }
00158         } //else {
00159            // return UnexpectedReply;
00160         //}
00161         /*
00162          * Read the footer of the response.
00163          */
00164         uint8_t footer[5];
00165         cam1.recvBytes(footer, sizeof(footer), 2 * 1000 * 1000);// {
00166         //    return RecvError;
00167         //}
00168 
00169         m += z;
00170     
00171    // return NoError;
00172 }
00173 
00174 //////////////////
00175 /**
00176  * Capture.
00177  *
00178  * @param cam A pointer to a camera object.
00179  * @param filename The file name.
00180  *
00181  * @return Return 0 if it succeed.
00182  */
00183  float p=0.1;//////////////servo
00184  int flag=1;///////////////servo
00185 int capture(Camera_LS_Y201 *cam, char *filename) {
00186     /*
00187      * Take a picture.
00188      */
00189 if(end==true)
00190 {
00191     ///////////////////////////////////////////servo
00192      myservo = p;
00193      //pc.printf("hhhhhhh%fhhhh",p);
00194     if (flag==1)        
00195     {
00196         p+=0.1;
00197     }
00198     else
00199     {
00200         p-=0.1;
00201         }
00202     if(p>0.9)
00203     
00204     {
00205     flag=0;
00206     }
00207     
00208     if(p<0.1)
00209     {
00210     flag=1;
00211      }
00212     
00213     //////////////////////////////////////////servo
00214     if (cam->takePicture() != 0) {
00215         return -1;
00216     }
00217     cam1.readJpegFileSize(&siz_total);
00218 //    printf("%d",siz_total);
00219     siz_done = 0;
00220 
00221       
00222     DEBMSG("Captured.");
00223     NEWLINE();
00224     work.fp = fopen(filename, "wb");
00225     //p1= fopen("/sd/IMG_11111.txt", "wb");
00226     if (work.fp == NULL) {
00227         return -2;
00228     }
00229 
00230     /*
00231      * Read the content.
00232      */
00233     DEBMSG("%s", filename);
00234     NEWLINE();
00235 end=false;
00236 }
00237     /*
00238      * Open file.
00239      */
00240     
00241     readdJpegFileContent(callback_func);
00242        // fclose(work.fp);
00243         //return -3;
00244     
00245     
00246 
00247     /*
00248      * Stop taking pictures.
00249      */
00250  if(end== true)
00251  {
00252     fclose(work.fp);
00253     cam->stopTakingPictures();
00254 
00255 DEBMSG("[%04d]:OK.", cnt);
00256             NEWLINE();
00257 m=0;
00258 cnt++;
00259 //wait(1);
00260 //xbee.printf("saved");
00261 }
00262     return 0;
00263 }
00264 ////////////////////////////
00265 /////////////////////////
00266 
00267 /////////////////////////////////////////////////////
00268 int main(void) {
00269     
00270     xbee.baud(115200);
00271     pc.baud(115200);
00272     
00273 DEBMSG("Camera module");
00274     NEWLINE();
00275     DEBMSG("Resetting...");
00276     NEWLINE();
00277     wait(1);
00278 
00279     if (cam1.reset() == 0) {
00280         DEBMSG("Reset OK.");
00281         NEWLINE();
00282     } else {
00283         DEBMSG("Reset fail.");
00284         NEWLINE();
00285         error("Reset fail.");
00286     }
00287 
00288 
00289     wait(1);
00290     cam1.baud();
00291     int j=1;
00292     int g=1;
00293     while (1) {
00294         
00295     char fname[64];
00296      //  xbee.printf("hello%d\n\r",g);
00297        
00298        if((end==true)||(j==1))
00299        {
00300         
00301         snprintf(fname, sizeof(fname) - 1, FILENAME, cnt);
00302         j=0;
00303         }
00304         capture(&cam1, fname);
00305         //if (r == 0) {
00306             
00307         
00308         //    }
00309         //} else {
00310             //DEBMSG("[%04d]:NG. (code=%d)", cnt, r);
00311            // NEWLINE();
00312            // error("Failure.");
00313         //}
00314         g++;
00315     }
00316 }
00317     
00318     
00319        
00320     
00321