![](/media/cache/profiles/f04cd7399b2b0128970efb6d20b5c551.50x50_q85.jpg)
This is the ServoCam Project, as shown here: http://youtu.be/riiqyXnckR4
Dependencies: mbed NetEthApiLPC1768 NetServicesLPC1768 Camera_LS_Y201 Servo
main.cpp@0:13c861a5ed0f, 2011-06-03 (annotated)
- Committer:
- tbaumg
- Date:
- Fri Jun 03 19:12:28 2011 +0000
- Revision:
- 0:13c861a5ed0f
V1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tbaumg | 0:13c861a5ed0f | 1 | #include "mbed.h" |
tbaumg | 0:13c861a5ed0f | 2 | #include "Camera_LS_Y201.h" |
tbaumg | 0:13c861a5ed0f | 3 | #include "Servo.h" |
tbaumg | 0:13c861a5ed0f | 4 | #include "EthernetNetIf.h" |
tbaumg | 0:13c861a5ed0f | 5 | #include "HttpServer.h" |
tbaumg | 0:13c861a5ed0f | 6 | |
tbaumg | 0:13c861a5ed0f | 7 | Servo servo1(p21); |
tbaumg | 0:13c861a5ed0f | 8 | Servo servo2(p22); |
tbaumg | 0:13c861a5ed0f | 9 | DigitalOut myled(LED1); |
tbaumg | 0:13c861a5ed0f | 10 | DigitalOut myled4(LED4); |
tbaumg | 0:13c861a5ed0f | 11 | Camera_LS_Y201 cam1(p13, p14); |
tbaumg | 0:13c861a5ed0f | 12 | Serial device(p28, p27); // tx, rx |
tbaumg | 0:13c861a5ed0f | 13 | LocalFileSystem local("local"); // Create the local filesystem under the name "local" |
tbaumg | 0:13c861a5ed0f | 14 | LocalFileSystem fs("webfs"); |
tbaumg | 0:13c861a5ed0f | 15 | |
tbaumg | 0:13c861a5ed0f | 16 | EthernetNetIf eth; |
tbaumg | 0:13c861a5ed0f | 17 | HttpServer svr; |
tbaumg | 0:13c861a5ed0f | 18 | |
tbaumg | 0:13c861a5ed0f | 19 | FILE *fp = NULL; |
tbaumg | 0:13c861a5ed0f | 20 | int datcnt = 0; |
tbaumg | 0:13c861a5ed0f | 21 | float ServoVal1[5] = {0,.25,.5,.75,1}; |
tbaumg | 0:13c861a5ed0f | 22 | //float ServoVal2[5] = {0,.25,.5,.75,1}; |
tbaumg | 0:13c861a5ed0f | 23 | float ServoVal2[5] = {1,.9,.5,.1,0}; |
tbaumg | 0:13c861a5ed0f | 24 | |
tbaumg | 0:13c861a5ed0f | 25 | /** |
tbaumg | 0:13c861a5ed0f | 26 | * Callback function for readJpegFileContent. |
tbaumg | 0:13c861a5ed0f | 27 | * |
tbaumg | 0:13c861a5ed0f | 28 | * @param buf A pointer to a buffer. |
tbaumg | 0:13c861a5ed0f | 29 | * @param siz A size of the buffer. |
tbaumg | 0:13c861a5ed0f | 30 | */ |
tbaumg | 0:13c861a5ed0f | 31 | void callback_func(int done, int total, uint8_t *buf, size_t siz) { |
tbaumg | 0:13c861a5ed0f | 32 | fwrite(buf, siz, 1, fp); |
tbaumg | 0:13c861a5ed0f | 33 | |
tbaumg | 0:13c861a5ed0f | 34 | static int n = 0; |
tbaumg | 0:13c861a5ed0f | 35 | int tmp = done * 100 / total; |
tbaumg | 0:13c861a5ed0f | 36 | if (n != tmp) { |
tbaumg | 0:13c861a5ed0f | 37 | n = tmp; |
tbaumg | 0:13c861a5ed0f | 38 | // You can print the progress to LCD here. |
tbaumg | 0:13c861a5ed0f | 39 | } |
tbaumg | 0:13c861a5ed0f | 40 | } |
tbaumg | 0:13c861a5ed0f | 41 | |
tbaumg | 0:13c861a5ed0f | 42 | int capture(int n) { |
tbaumg | 0:13c861a5ed0f | 43 | /* |
tbaumg | 0:13c861a5ed0f | 44 | * Take a picture.(L) |
tbaumg | 0:13c861a5ed0f | 45 | */ |
tbaumg | 0:13c861a5ed0f | 46 | if (cam1.takePicture() != 0) { |
tbaumg | 0:13c861a5ed0f | 47 | return -1; |
tbaumg | 0:13c861a5ed0f | 48 | } |
tbaumg | 0:13c861a5ed0f | 49 | // lcd.locate(0, 1); |
tbaumg | 0:13c861a5ed0f | 50 | device.printf("Captured.\r\n"); |
tbaumg | 0:13c861a5ed0f | 51 | |
tbaumg | 0:13c861a5ed0f | 52 | char fname[64]; |
tbaumg | 0:13c861a5ed0f | 53 | |
tbaumg | 0:13c861a5ed0f | 54 | /* |
tbaumg | 0:13c861a5ed0f | 55 | * Open file. |
tbaumg | 0:13c861a5ed0f | 56 | * Read the content. |
tbaumg | 0:13c861a5ed0f | 57 | */ |
tbaumg | 0:13c861a5ed0f | 58 | snprintf(fname, sizeof(fname) - 1, "/local/IMG_%04d.jpg", n); |
tbaumg | 0:13c861a5ed0f | 59 | fp = fopen(fname, "wb"); |
tbaumg | 0:13c861a5ed0f | 60 | if (fp == NULL) { |
tbaumg | 0:13c861a5ed0f | 61 | return -2; |
tbaumg | 0:13c861a5ed0f | 62 | } |
tbaumg | 0:13c861a5ed0f | 63 | //lcd.locate(0, 0); |
tbaumg | 0:13c861a5ed0f | 64 | device.printf("Capture: %04d\r\n", n); |
tbaumg | 0:13c861a5ed0f | 65 | datcnt = 0; |
tbaumg | 0:13c861a5ed0f | 66 | if (cam1.readJpegFileContent(callback_func) != 0) { |
tbaumg | 0:13c861a5ed0f | 67 | fclose(fp); |
tbaumg | 0:13c861a5ed0f | 68 | return -3; |
tbaumg | 0:13c861a5ed0f | 69 | } |
tbaumg | 0:13c861a5ed0f | 70 | fclose(fp); |
tbaumg | 0:13c861a5ed0f | 71 | |
tbaumg | 0:13c861a5ed0f | 72 | /* |
tbaumg | 0:13c861a5ed0f | 73 | * Stop taking pictures. |
tbaumg | 0:13c861a5ed0f | 74 | */ |
tbaumg | 0:13c861a5ed0f | 75 | cam1.stopTakingPictures(); |
tbaumg | 0:13c861a5ed0f | 76 | |
tbaumg | 0:13c861a5ed0f | 77 | return 0; |
tbaumg | 0:13c861a5ed0f | 78 | } |
tbaumg | 0:13c861a5ed0f | 79 | |
tbaumg | 0:13c861a5ed0f | 80 | |
tbaumg | 0:13c861a5ed0f | 81 | int main() { |
tbaumg | 0:13c861a5ed0f | 82 | device.baud(115200); |
tbaumg | 0:13c861a5ed0f | 83 | device.printf("***********************************\r\n"); |
tbaumg | 0:13c861a5ed0f | 84 | device.printf("**** ServoCamera mbed Project ****\r\n"); |
tbaumg | 0:13c861a5ed0f | 85 | device.printf("**** NXP mbed Design Challenge ****\r\n"); |
tbaumg | 0:13c861a5ed0f | 86 | device.printf("***********************************\r\n"); |
tbaumg | 0:13c861a5ed0f | 87 | wait(.25); |
tbaumg | 0:13c861a5ed0f | 88 | |
tbaumg | 0:13c861a5ed0f | 89 | if (cam1.reset() == 0) { |
tbaumg | 0:13c861a5ed0f | 90 | device.printf("Camera Reset OK.\r\n "); |
tbaumg | 0:13c861a5ed0f | 91 | } |
tbaumg | 0:13c861a5ed0f | 92 | wait(.5); |
tbaumg | 0:13c861a5ed0f | 93 | |
tbaumg | 0:13c861a5ed0f | 94 | //device.printf("setImageSize=%d\n", cam1.setImageSize(LS_Y201::ImageSize640x480)); |
tbaumg | 0:13c861a5ed0f | 95 | //wait(1); |
tbaumg | 0:13c861a5ed0f | 96 | device.printf("******************************\r\n"); |
tbaumg | 0:13c861a5ed0f | 97 | /* device.printf("** MOVE SERVO **\r\n"); |
tbaumg | 0:13c861a5ed0f | 98 | for(float p=0; p<1.0; p += 0.01) { |
tbaumg | 0:13c861a5ed0f | 99 | servo2 = p; |
tbaumg | 0:13c861a5ed0f | 100 | servo1 = p; |
tbaumg | 0:13c861a5ed0f | 101 | wait(0.15); |
tbaumg | 0:13c861a5ed0f | 102 | } |
tbaumg | 0:13c861a5ed0f | 103 | */ |
tbaumg | 0:13c861a5ed0f | 104 | servo2 = 0.5; |
tbaumg | 0:13c861a5ed0f | 105 | servo1 = 0.5; |
tbaumg | 0:13c861a5ed0f | 106 | device.printf("** SERVOs CENTERED! **\r\n"); |
tbaumg | 0:13c861a5ed0f | 107 | device.printf("******************************\r\n"); |
tbaumg | 0:13c861a5ed0f | 108 | wait(2); |
tbaumg | 0:13c861a5ed0f | 109 | int cnt = 0; |
tbaumg | 0:13c861a5ed0f | 110 | int col = 0; |
tbaumg | 0:13c861a5ed0f | 111 | int row = 0; |
tbaumg | 0:13c861a5ed0f | 112 | servo2 = 0.5; |
tbaumg | 0:13c861a5ed0f | 113 | servo1 = 0.3; |
tbaumg | 0:13c861a5ed0f | 114 | wait(1); |
tbaumg | 0:13c861a5ed0f | 115 | for (int j = 0; j < 3; j++) { |
tbaumg | 0:13c861a5ed0f | 116 | col = j; |
tbaumg | 0:13c861a5ed0f | 117 | servo1 = (ServoVal1[col+1]-0.15); // Move servo to new position |
tbaumg | 0:13c861a5ed0f | 118 | for (int i = 0; i < 3; i++) { |
tbaumg | 0:13c861a5ed0f | 119 | row = i; |
tbaumg | 0:13c861a5ed0f | 120 | servo2 = ServoVal2[row+1]; // Move servo to new position |
tbaumg | 0:13c861a5ed0f | 121 | wait(1); |
tbaumg | 0:13c861a5ed0f | 122 | myled4 = 0; // turns LED ON |
tbaumg | 0:13c861a5ed0f | 123 | int r = capture(cnt); // capture image |
tbaumg | 0:13c861a5ed0f | 124 | if (r == 0) { |
tbaumg | 0:13c861a5ed0f | 125 | device.printf("[%04d]:OK.\r\n", cnt); |
tbaumg | 0:13c861a5ed0f | 126 | } else { |
tbaumg | 0:13c861a5ed0f | 127 | device.printf("[%04d]:NG. (code=%d)\r\n", cnt, r); |
tbaumg | 0:13c861a5ed0f | 128 | } |
tbaumg | 0:13c861a5ed0f | 129 | cnt++; |
tbaumg | 0:13c861a5ed0f | 130 | myled4 = 1; // turns LED OFF |
tbaumg | 0:13c861a5ed0f | 131 | } |
tbaumg | 0:13c861a5ed0f | 132 | } |
tbaumg | 0:13c861a5ed0f | 133 | servo2 = 0.5; |
tbaumg | 0:13c861a5ed0f | 134 | servo1 = 0.3; |
tbaumg | 0:13c861a5ed0f | 135 | // |
tbaumg | 0:13c861a5ed0f | 136 | Base::add_rpc_class<DigitalOut>(); |
tbaumg | 0:13c861a5ed0f | 137 | |
tbaumg | 0:13c861a5ed0f | 138 | device.printf("\r\nSetting up Ethernet...\r\n"); |
tbaumg | 0:13c861a5ed0f | 139 | EthernetErr ethErr = eth.setup(); |
tbaumg | 0:13c861a5ed0f | 140 | if(ethErr) |
tbaumg | 0:13c861a5ed0f | 141 | { |
tbaumg | 0:13c861a5ed0f | 142 | device.printf("Error %d in setup.\n", ethErr); |
tbaumg | 0:13c861a5ed0f | 143 | return -1; |
tbaumg | 0:13c861a5ed0f | 144 | } |
tbaumg | 0:13c861a5ed0f | 145 | device.printf("\r\nSetup OK\r\n"); |
tbaumg | 0:13c861a5ed0f | 146 | |
tbaumg | 0:13c861a5ed0f | 147 | svr.addHandler<SimpleHandler>("/hello"); |
tbaumg | 0:13c861a5ed0f | 148 | svr.addHandler<RpcHandler>("/rpc"); |
tbaumg | 0:13c861a5ed0f | 149 | svr.addHandler<FSHandler>(""); //Default handler |
tbaumg | 0:13c861a5ed0f | 150 | //Example : Access to mbed.htm : http://a.b.c.d/webfs/mbed.htm |
tbaumg | 0:13c861a5ed0f | 151 | |
tbaumg | 0:13c861a5ed0f | 152 | svr.bind(80); |
tbaumg | 0:13c861a5ed0f | 153 | |
tbaumg | 0:13c861a5ed0f | 154 | device.printf("\r\nListening @port 80...\r\n"); |
tbaumg | 0:13c861a5ed0f | 155 | |
tbaumg | 0:13c861a5ed0f | 156 | // |
tbaumg | 0:13c861a5ed0f | 157 | while (1) { |
tbaumg | 0:13c861a5ed0f | 158 | Net::poll(); |
tbaumg | 0:13c861a5ed0f | 159 | myled = 1; |
tbaumg | 0:13c861a5ed0f | 160 | wait(0.2); |
tbaumg | 0:13c861a5ed0f | 161 | myled = 0; |
tbaumg | 0:13c861a5ed0f | 162 | wait(0.2); |
tbaumg | 0:13c861a5ed0f | 163 | } |
tbaumg | 0:13c861a5ed0f | 164 | |
tbaumg | 0:13c861a5ed0f | 165 | } |