基于Nucleo F411RE和光流模块的通讯实验,串口1接收到数据之后解析,并从串口2输出光流模块计算结果
Fork of Nucleo_serial by
Revision 5:5fd053d79f06, committed 2018-06-22
- Comitter:
- adaphoto
- Date:
- Fri Jun 22 08:14:55 2018 +0000
- Parent:
- 4:5ac470115649
- Commit message:
- ??Nucleo F411RE??????????????????
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 5ac470115649 -r 5fd053d79f06 main.cpp --- a/main.cpp Sun Apr 08 14:49:52 2018 +0000 +++ b/main.cpp Fri Jun 22 08:14:55 2018 +0000 @@ -3,9 +3,24 @@ Serial pc(SERIAL_TX, SERIAL_RX); DigitalOut led01(LED1); +// 光流传感器接口,串口,波特率115200,每一帧数据20个byte +// IO口定义 +Serial Optical_Flow(PA_15,PB_7); +// 变量 +char *get_Optical_Flow_Cmd(); +// 数据处理函数 +void Optical_Flow_Process_Cmd(char *cmd); +// 线程实现函数 +void Optical_Flow_Call_Back(); +// 线程 +Thread Optical_Flow_Thread; + + + + int i; // for cycles - char cmd_on[] = "on"; - char cmd_off[] = "off"; +char cmd_on[] = "on"; +char cmd_off[] = "off"; char *getCmd(); void processCmd(char *cmd); @@ -24,6 +39,19 @@ } } } +//////////////////////////////////////////////////////////////////////////////// +void Optical_Flow_Call_Back() +{ + while(true) + { + if (Optical_Flow.readable()) // if there is an character to read from the device + { + char *SerialCommand = get_Optical_Flow_Cmd(); + Optical_Flow_Process_Cmd(SerialCommand); + free(SerialCommand); + } + } +} /////////////////////////////////////////////////////////////////////////////// char *getCmd() { @@ -59,6 +87,40 @@ } return NULL; } + +char *get_Optical_Flow_Cmd() +{ + char buff[128]; + unsigned char ch; + int buffIndex = 0; + memset(buff, 0, sizeof(buff)); // clean the buffer + + char *cmd = (char*)malloc(128); + if (!cmd) + return NULL; + do + { + ch = Optical_Flow.getc(); // read it + if (buffIndex < 128){ // just to avoid buffer overflow + buff[buffIndex] = ch; // put it into the value array and increment the index + buffIndex++; + } + } + while (ch != '\r' && ch != '\n' ); + buff[buffIndex]='\0'; // add the end-signalling char + + if(strlen(buff) != 0){ + + for (i = 0 ; i < sizeof(buff) ; i++){ + if (buff[i] == '\r' || buff[i] == '\n'){ + cmd[i] = '\0'; + return cmd; + } + cmd[i] = buff[i]; + } + } + return NULL; +} /////////////////////////////////////////////////////////////////////////////// void processCmd(char *cmd) { @@ -73,18 +135,31 @@ } } +void Optical_Flow_Process_Cmd(char *cmd) +{ + int8_t Delta_X,Delta_Y,Delta_Z; + + Delta_X = (int8_t)(cmd[6]); + Delta_Y = (int8_t)(cmd[7]); + Delta_Z = (int8_t)(cmd[5]); + //pc.printf("rec: \"%s\"\n\r", cmd); + pc.printf("\r\n X:%d, Y:%d, Z:%d",Delta_X,Delta_Y,cmd[5]); +} /////////////////////////////////////////////////////////////////////////////// int main() { - t1.start(tSerial_body); - - led01 = 1; + pc.baud(115200); + Optical_Flow.baud(115200); + //t1.start(tSerial_body); + Optical_Flow_Thread.start(Optical_Flow_Call_Back); + pc.printf("\r\n Optical Flow Test \r\n"); + //led01 = 1; - pc.printf("\n\r App started______Banik pyco\n\r"); - int counter = 1; + //pc.printf("\n\r App started______Banik pyco\n\r"); + //int counter = 1; while(1) { - pc.printf("seconds: %d\n\r", counter++); + //pc.printf("seconds: %d\n\r", counter++); wait(1); } } \ No newline at end of file