This is the ServoCam Project, as shown here: http://youtu.be/riiqyXnckR4

Dependencies:   mbed NetEthApiLPC1768 NetServicesLPC1768 Camera_LS_Y201 Servo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "Camera_LS_Y201.h"
00003 #include "Servo.h"
00004 #include "EthernetNetIf.h"
00005 #include "HttpServer.h"
00006 
00007 Servo servo1(p21);
00008 Servo servo2(p22);
00009 DigitalOut myled(LED1);
00010 DigitalOut myled4(LED4);
00011 Camera_LS_Y201 cam1(p13, p14);
00012 Serial device(p28, p27);  // tx, rx
00013 LocalFileSystem local("local");               // Create the local filesystem under the name "local"
00014 LocalFileSystem fs("webfs");
00015 
00016 EthernetNetIf eth;  
00017 HttpServer svr;
00018 
00019 FILE *fp = NULL;
00020 int datcnt = 0;
00021 float ServoVal1[5] = {0,.25,.5,.75,1};
00022 //float ServoVal2[5] = {0,.25,.5,.75,1};
00023 float ServoVal2[5] = {1,.9,.5,.1,0};
00024 
00025 /**
00026  * Callback function for readJpegFileContent.
00027  *
00028  * @param buf A pointer to a buffer.
00029  * @param siz A size of the buffer.
00030  */
00031 void callback_func(int done, int total, uint8_t *buf, size_t siz) {
00032     fwrite(buf, siz, 1, fp);
00033 
00034     static int n = 0;
00035     int tmp = done * 100 / total;
00036     if (n != tmp) {
00037         n = tmp;
00038         // You can print the progress to LCD here.
00039     }
00040 }
00041 
00042 int capture(int n) {
00043     /*
00044      * Take a picture.(L)
00045      */
00046     if (cam1.takePicture() != 0) {
00047         return -1;
00048     }
00049    // lcd.locate(0, 1);
00050     device.printf("Captured.\r\n");
00051     
00052     char fname[64];
00053     
00054     /*
00055      * Open file.
00056      * Read the content.
00057      */
00058     snprintf(fname, sizeof(fname) - 1, "/local/IMG_%04d.jpg", n);
00059     fp = fopen(fname, "wb");
00060     if (fp == NULL) {
00061         return -2;
00062     }
00063     //lcd.locate(0, 0);
00064     device.printf("Capture: %04d\r\n", n);
00065     datcnt = 0;
00066     if (cam1.readJpegFileContent(callback_func) != 0) {
00067         fclose(fp);
00068         return -3;
00069     }
00070     fclose(fp);
00071 
00072     /*
00073      * Stop taking pictures.
00074      */
00075     cam1.stopTakingPictures();
00076 
00077     return 0;
00078 }
00079 
00080 
00081 int main() {
00082     device.baud(115200);
00083     device.printf("***********************************\r\n");
00084     device.printf("**** ServoCamera mbed Project  ****\r\n");
00085     device.printf("**** NXP mbed Design Challenge ****\r\n");
00086     device.printf("***********************************\r\n");
00087     wait(.25);
00088 
00089     if (cam1.reset() == 0) {
00090         device.printf("Camera Reset OK.\r\n ");
00091     }
00092     wait(.5);
00093 
00094     //device.printf("setImageSize=%d\n", cam1.setImageSize(LS_Y201::ImageSize640x480));
00095     //wait(1);
00096     device.printf("******************************\r\n");    
00097 /*    device.printf("** MOVE SERVO               **\r\n");    
00098     for(float p=0; p<1.0; p += 0.01) {
00099         servo2 = p;
00100         servo1 = p;
00101         wait(0.15);
00102     }
00103 */
00104     servo2 = 0.5;
00105     servo1 = 0.5;
00106     device.printf("** SERVOs CENTERED!           **\r\n");   
00107     device.printf("******************************\r\n");
00108      wait(2);
00109      int cnt = 0;
00110      int col = 0;
00111      int row = 0;
00112      servo2 = 0.5;
00113      servo1 = 0.3;
00114      wait(1);
00115      for (int j = 0; j < 3; j++) {
00116          col = j;
00117          servo1 = (ServoVal1[col+1]-0.15);  // Move servo to new position
00118          for (int i = 0; i < 3; i++) {      
00119             row = i;
00120             servo2 = ServoVal2[row+1];      // Move servo to new position
00121             wait(1);
00122             myled4 = 0;                     // turns LED ON
00123             int r = capture(cnt);           // capture image
00124             if (r == 0) {
00125                device.printf("[%04d]:OK.\r\n", cnt);
00126             } else {
00127                 device.printf("[%04d]:NG. (code=%d)\r\n", cnt, r);
00128             }
00129             cnt++;
00130             myled4 = 1;                     // turns LED OFF
00131           }     
00132     }
00133      servo2 = 0.5;
00134      servo1 = 0.3;
00135      //
00136  Base::add_rpc_class<DigitalOut>();
00137 
00138   device.printf("\r\nSetting up Ethernet...\r\n");
00139   EthernetErr ethErr = eth.setup();
00140   if(ethErr)
00141   {
00142    device.printf("Error %d in setup.\n", ethErr);
00143     return -1;
00144   }
00145   device.printf("\r\nSetup OK\r\n");
00146   
00147   svr.addHandler<SimpleHandler>("/hello");
00148   svr.addHandler<RpcHandler>("/rpc");
00149   svr.addHandler<FSHandler>(""); //Default handler
00150   //Example : Access to mbed.htm : http://a.b.c.d/webfs/mbed.htm
00151   
00152   svr.bind(80);
00153   
00154   device.printf("\r\nListening @port 80...\r\n");     
00155      
00156     //
00157     while (1) {
00158         Net::poll();
00159         myled = 1;
00160         wait(0.2);
00161         myled = 0;
00162         wait(0.2);
00163      }
00164    
00165 }