基于Nucleo F411RE和光流模块的通讯实验,串口1接收到数据之后解析,并从串口2输出光流模块计算结果

Fork of Nucleo_serial by Tomas Hyhlík

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 Serial pc(SERIAL_TX, SERIAL_RX);
00004 DigitalOut led01(LED1);
00005 
00006 // 光流传感器接口,串口,波特率115200,每一帧数据20个byte
00007 // IO口定义
00008 Serial Optical_Flow(PA_15,PB_7);
00009 // 变量
00010 char *get_Optical_Flow_Cmd();
00011 // 数据处理函数
00012 void Optical_Flow_Process_Cmd(char *cmd);
00013 // 线程实现函数
00014 void Optical_Flow_Call_Back();
00015 // 线程
00016 Thread Optical_Flow_Thread;
00017 
00018 
00019 
00020 
00021 int i;          // for cycles
00022 char cmd_on[] = "on";
00023 char cmd_off[] = "off"; 
00024     
00025 char *getCmd();
00026 void processCmd(char *cmd);
00027 
00028 Thread t1;
00029 
00030  ///////////////////////////////////////////////////////////////////////////////
00031 void tSerial_body()
00032 {
00033     while(1){ 
00034         if (pc.readable())     // if there is an character to read from the device
00035         {
00036             char *SerialCommand = getCmd();
00037             processCmd(SerialCommand);
00038             free(SerialCommand);
00039         }   
00040     }    
00041 }
00042 ////////////////////////////////////////////////////////////////////////////////
00043 void Optical_Flow_Call_Back()
00044 {
00045     while(true)
00046     {
00047         if (Optical_Flow.readable()) // if there is an character to read from the device
00048         {
00049             char *SerialCommand = get_Optical_Flow_Cmd();
00050             Optical_Flow_Process_Cmd(SerialCommand);
00051             free(SerialCommand);
00052         }
00053     }
00054 }
00055  ///////////////////////////////////////////////////////////////////////////////
00056 char *getCmd()
00057 {
00058     char buff[128];
00059     unsigned char ch;
00060     int buffIndex = 0;
00061     memset(buff, 0, sizeof(buff));          // clean the buffer
00062 
00063 
00064     char *cmd = (char*)malloc(128);
00065     if (!cmd)
00066         return NULL;
00067     do 
00068     {
00069         ch = pc.getc();   // read it
00070         if (buffIndex < 128){               // just to avoid buffer overflow
00071             buff[buffIndex] = ch;  // put it into the value array and increment the index
00072             buffIndex++;
00073         }
00074      }
00075      while (ch != '\n' && ch != '\r' );
00076      buff[buffIndex]='\0';  // add the end-signalling char
00077              
00078     if(strlen(buff) != 0){
00079         
00080         for (i = 0 ; i < sizeof(buff) ; i++){
00081             if (buff[i] == '\n' || buff[i] == '\r'){
00082                 cmd[i] = '\0';
00083                 return cmd;
00084             }
00085             cmd[i] = buff[i];                
00086         }
00087     }  
00088     return NULL;  
00089 }
00090 
00091 char *get_Optical_Flow_Cmd()
00092 {
00093     char buff[128];
00094     unsigned char ch;
00095     int buffIndex = 0;
00096     memset(buff, 0, sizeof(buff));          // clean the buffer
00097     
00098     char *cmd = (char*)malloc(128);
00099     if (!cmd)
00100         return NULL;
00101     do 
00102     {
00103         ch = Optical_Flow.getc();           // read it
00104         if (buffIndex < 128){               // just to avoid buffer overflow
00105             buff[buffIndex] = ch;           // put it into the value array and increment the index
00106             buffIndex++;
00107         }
00108      }
00109      while (ch != '\r' && ch != '\n' );
00110      buff[buffIndex]='\0';                  // add the end-signalling char
00111     
00112     if(strlen(buff) != 0){
00113         
00114         for (i = 0 ; i < sizeof(buff) ; i++){
00115             if (buff[i] == '\r' || buff[i] == '\n'){
00116                 cmd[i] = '\0';
00117                 return cmd;
00118             }
00119             cmd[i] = buff[i];                
00120         }
00121     }  
00122     return NULL;
00123 }
00124  ///////////////////////////////////////////////////////////////////////////////
00125 void processCmd(char *cmd)
00126 {
00127     pc.printf("rec: \"%s\"\n\r", cmd);
00128     if(strcmp(cmd,cmd_on) == 0){
00129         led01 = 1;
00130         pc.printf("Turning the led on.\n\r");
00131     } else if (strcmp(cmd,cmd_off) == 0) {
00132         led01 = 0;    
00133         pc.printf("Turning the led off.\n\r");
00134     
00135     }
00136 }
00137 
00138 void Optical_Flow_Process_Cmd(char *cmd)
00139 {
00140     int8_t Delta_X,Delta_Y,Delta_Z;
00141     
00142     Delta_X = (int8_t)(cmd[6]);
00143     Delta_Y = (int8_t)(cmd[7]);
00144     Delta_Z = (int8_t)(cmd[5]);
00145     //pc.printf("rec: \"%s\"\n\r", cmd);
00146     pc.printf("\r\n X:%d, Y:%d, Z:%d",Delta_X,Delta_Y,cmd[5]);
00147 }
00148 ///////////////////////////////////////////////////////////////////////////////
00149 int main() 
00150 {
00151     pc.baud(115200);
00152     Optical_Flow.baud(115200);
00153     //t1.start(tSerial_body);
00154     Optical_Flow_Thread.start(Optical_Flow_Call_Back);
00155     pc.printf("\r\n Optical Flow Test \r\n");
00156     //led01 = 1;
00157     
00158     //pc.printf("\n\r App started______Banik pyco\n\r");
00159     //int counter = 1;
00160     while(1)
00161     {   
00162         //pc.printf("seconds: %d\n\r", counter++);
00163         wait(1);
00164     }
00165 }