エレキジャック Web版 マイコン・カーを製作してみよう<21> マイコンカー制御プログラムです。http://www.eleki-jack.com/arm/2012/10/21.html このプログラムは次回の記事でアップされるパソコン用プログラムを使ってマイコンカーを制御するプログラムです。マイコンカーとパソコンの通信はXbeeを使っています。
Revision 1:8f2a9b48a9e0, committed 2012-05-05
- Comitter:
- sunifu
- Date:
- Sat May 05 15:04:33 2012 +0000
- Parent:
- 0:6eaa311b9d3b
- Child:
- 2:9758930faa7e
- Commit message:
- 0.0.2
Changed in this revision
--- a/CameraC1098.cpp Sat May 05 11:37:25 2012 +0000 +++ b/CameraC1098.cpp Sat May 05 15:04:33 2012 +0000 @@ -38,8 +38,6 @@ * Make a sync. for baud rate. */ CameraC1098::ErrorNumber CameraC1098::sync() { - WAITIDLE(); - for (int i = 0; i < SYNCMAX; i++) { if (NoError == sendSync()) { if (NoError == recvAckOrNck()) { @@ -67,23 +65,42 @@ * @param jr JPEG resolution. */ CameraC1098::ErrorNumber CameraC1098::init(Baud baud,JpegResolution jr) { + int i ; ErrorNumber en; WAITIDLE(); - for (int i = 2 ; i <= 7 ; i++) { + for ( i = 0x02 ; i <= 0x07 ; i++ ) { + sendAck(0x0D, 0x00); + en = sendInitial((Baud)i, jr); + wait_ms(50) ; + if (NoError == en) { + if (NoError == sendSync()) { + if (NoError == recvAckOrNck()) { + if (NoError == recvSync()) { + if (NoError == sendAck(0x0D, 0x00)) { + /* + * After synchronization, the camera needs a little time for AEC and AGC to be stable. + * Users should wait for 1-2 seconds before capturing the first picture. + */ + wait(2); + return NoError; + } + } + } + } + wait_ms(50); + } + } + /* + for (int i = 0; i < SYNCMAX; i++) { if (NoError == sendSync()) { - if (NoError == recvAckOrNck()) { + if (NoError == recvAckOrNck()) { if (NoError == recvSync()) { - if (NoError == sendAck(0x0D, 0x00)) { - en = sendInitial((Baud)i, jr); - if (NoError != en) { - return en; - } - WAITFUNC(); - en = recvAckOrNck(); - if (NoError != en) { - return en; - } - + if (NoError == sendAck(0x0D, 0x00)) { + WAITFUNC(); + en = recvAckOrNck(); + if (NoError != en) { + return en; + } static bool alreadySetupPackageSize = false; if (!alreadySetupPackageSize) { en = sendSetPackageSize(packageSize); @@ -97,15 +114,16 @@ } alreadySetupPackageSize = true; } - return (ErrorNumber)NoError; } - } + } } } wait_ms(50); } + */ return UnexpectedReply; + } @@ -224,7 +242,7 @@ CameraC1098::ErrorNumber CameraC1098::sendInitial(Baud baud, JpegResolution jr) { char send[COMMAND_LENGTH]; - + send[0] = 0xAA; send[1] = 0x01; send[2] = (int)baud; @@ -233,8 +251,10 @@ send[5] = (char)jr; if (!SENDFUNC(send, sizeof(send))) { + printf("SendInitial SendRegisterTimeout\r\n"); return (ErrorNumber)SendRegisterTimeout; } + printf("SendInitial NoError Baud[%X] Jr[%d]\r\n",baud,jr); return (ErrorNumber)NoError; } @@ -285,7 +305,29 @@ } void CameraC1098::setmbedBaud(Baud baud) { - serial.baud((int)baud); + switch((int)baud){ + case 2: + serial.baud(460800); + break; + case 3: + serial.baud(230400); + break; + case 4: + serial.baud(115200); + break; + case 5: + serial.baud(57600); + break; + case 6: + serial.baud(28800); + break; + case 7: + serial.baud(14400); + break; + default: + serial.baud(14400); + } + } @@ -330,20 +372,28 @@ send[3] = 0x00; send[4] = 0x00; send[5] = 0x00; + + if (!SENDFUNC(send, sizeof(send))) { + printf("SendSync Error\r\n"); return (ErrorNumber)SendRegisterTimeout; } + printf("SendSync NoError\r\n"); return (ErrorNumber)NoError; } CameraC1098::ErrorNumber CameraC1098::recvSync() { char recv[COMMAND_LENGTH]; + if (!RECVFUNC(recv, sizeof(recv))) { + printf("RecvSync UnexpectedReply [0]\r\n"); return (ErrorNumber)UnexpectedReply; } if ((0xAA != recv[0]) || (0x0D != recv[1])) { + printf("RecvSync UnexpectedReply [1]\r\n"); return (ErrorNumber)UnexpectedReply; } + printf("RecvSync NoError \r\n"); return (ErrorNumber)NoError; } @@ -355,6 +405,7 @@ */ CameraC1098::ErrorNumber CameraC1098::sendAck(uint8_t commandId, uint16_t packageId) { char send[COMMAND_LENGTH]; + printf("Send Ack\r\n"); send[0] = 0xAA; send[1] = 0x0E; send[2] = commandId; @@ -374,15 +425,20 @@ */ CameraC1098::ErrorNumber CameraC1098::recvAckOrNck() { char recv[COMMAND_LENGTH]; + if (!RECVFUNC(recv, sizeof(recv))) { + printf("RecvAckOrNck UnexpecterReply [0]\r\n"); return (ErrorNumber)UnexpectedReply; } if ((0xAA == recv[0]) && (0x0E == recv[1])) { + printf("RecvAckOrNck NoError\r\n"); return (ErrorNumber)NoError; } if ((0xAA == recv[0]) && (0x0F == recv[1])) { + printf("RecvAckOrNck ErrorCode[%d] \r\n",recv[4]); return (ErrorNumber)recv[4]; } + printf("RecvAckOrNck UnexpecterReply [1]\r\n"); return (ErrorNumber)UnexpectedReply; }
--- a/CameraC1098.h Sat May 05 11:37:25 2012 +0000 +++ b/CameraC1098.h Sat May 05 15:04:33 2012 +0000 @@ -48,12 +48,12 @@ * Baud rate. */ enum Baud { - Baud460800 = 460800, - Baud230400 = 230400, - Baud115200 = 115200, - Baud57600 = 57600, - Baud28800 = 28800, - Baud14400 = 14400 // Default. + Baud460800 = 0x02, + Baud230400 = 0x03, + Baud115200 = 0x04, + Baud57600 = 0x05, + Baud28800 = 0x06, + Baud14400 = 0x07 // Default. }; /**
--- a/main.cpp Sat May 05 11:37:25 2012 +0000 +++ b/main.cpp Sat May 05 15:04:33 2012 +0000 @@ -37,7 +37,7 @@ LocalFileSystem fs("fs"); #endif -CameraC1098 camera(p13, p14); +CameraC1098 camera(p9, p10); /** @@ -98,23 +98,20 @@ printf("CameraC1098\r\n"); printf("==========\r\n"); // Camera Reset - sync(); + //sync(); CameraC1098::ErrorNumber err = CameraC1098::NoError; - -#if USE_JPEG_HIGH_RESOLUTION - err = camera.init(CameraC1098::Baud460800, CameraC1098::JpegResolution640x480); -#else - err = camera.init(CameraC1098::Baud460800, CameraC1098::JpegResolution320x240); -#endif + camera.setmbedBaud(CameraC1098::Baud14400); + err = camera.init(CameraC1098::Baud14400, CameraC1098::JpegResolution640x480); + if (CameraC1098::NoError == err) { printf("[ OK ] : CameraC1098::init\r\n"); } else { printf("[FAIL] : CameraC1098::init (Error=%02X)\r\n", (int)err); } - camera.setmbedBaud(CameraC1098::Baud460800); + // - test_jpeg_snapshot_picture(); + //test_jpeg_snapshot_picture(); return 0; }