test
Dependencies: mbed Motordriver SRF05
main.cpp
- Committer:
- brian12858
- Date:
- 2016-12-08
- Revision:
- 1:4e8b56a7698e
- Parent:
- 0:6d65c70ef417
File content as of revision 1:4e8b56a7698e:
#include "mbed.h" #include <string> #include <cstring> #include "rtos.h" #include "motordriver.h" Serial bt(p28,p27);//tx,rx Serial TAG(p9,p10);//tx,rx Serial pc(USBTX,USBRX); using namespace std; void read_line(); void Rx_interrupt(); // Circular buffers for serial TX and RX data - used by interrupt routines const int buffer_size = 255; // might need to increase buffer size for high baud rates char rx_buffer[buffer_size+1]; volatile int rx_in=0; volatile int rx_out=0; char rx_line[80]; Motor R(p21, p22, p23, 1); // pwm, fwd, rev, can brake ,right Motor L(p26, p25, p24, 1); // pwm, fwd, rev, can brake ,left string tagStr1,tagStr2,tagStr3; bool flag1=false,flag2=false,flag3=false; string str; //接收tag資料與透過藍芽發送資料至pc端 void thread_tag(){ bt.baud(38400); pc.baud(9600); TAG.baud(115200); TAG.attach(&Rx_interrupt, Serial::RxIrq); while(1) { //若有tag資料傳入,則進行讀資料 //if(TAG.readable()){ //pc.printf("have data\n\r"); str=""; //讀入所有資料,並寫放入string中 //pc.printf("read data\n\r"); read_line(); // Read ASCII number from rx line buffer //sscanf(rx_line,"%d",&rx_i); pc.printf("tag thread."); switch(rx_line[1]){ case '0': flag1=true; tagStr1=&rx_line[2]; pc.printf("string1 = %s\n\r",tagStr1); break; case '1': flag2=true; tagStr2=&rx_line[2]; pc.printf("string2 = %s\n\r",tagStr2); break; case '2': flag3=true; tagStr3=&rx_line[2]; pc.printf("string3 = %s\n\r",tagStr3); break; } if(flag3==true&&flag2==true&&flag1==true){ flag1=false,flag2=false,flag3=false; str=tagStr1+","+tagStr2+","+tagStr3; bt.printf("%s\n",str); } } } bool mode_flag=true; int main(){ bt.baud(38400); //pc.baud(9600); //pc.printf("start"); Thread thread0(thread_tag); // 若收到藍牙模組的資料,則送到「序列埠監控視窗」 char ch[1]={'s'}; while(1) { //pc.printf("ch[0]= %s\n\r",ch); if(bt.readable ()){ ch[0]=bt.getc(); //pc.printf("string1 = %s\n\r",ch); } switch(ch[0]){ case 'w': //前進 R.speed(-0.9),L.speed(-1); break; case 'a': //左轉 R.speed(0),L.speed(-1); break; case 's': //停住 R.stop(0.3),L.stop(0.4); break; case 'd': //右轉 R.speed(-0.9),L.speed(0); break; case 'x': //backward R.speed(0.9),L.speed(1); break; case 'm': //自走車 break; } } } void auto_mode(){ //自走程式放入位置 } // Read a line from the large rx buffer from rx interrupt routine void read_line() { int i; i = 0; // Start Critical Section - don't interrupt while changing global buffer variables NVIC_DisableIRQ(UART1_IRQn); // Loop reading rx buffer characters until end of line character while ((i==0) || (rx_line[i-1] != '\r')) { // Wait if buffer empty if (rx_in == rx_out) { // End Critical Section - need to allow rx interrupt to get new characters for buffer NVIC_EnableIRQ(UART1_IRQn); while (rx_in == rx_out) { } // Start Critical Section - don't interrupt while changing global buffer variables NVIC_DisableIRQ(UART1_IRQn); } rx_line[i] = rx_buffer[rx_out]; i++; rx_out = (rx_out + 1) % buffer_size; } // End Critical Section NVIC_EnableIRQ(UART1_IRQn); rx_line[i-1] = 0; return; } // Interupt Routine to read in data from serial port void Rx_interrupt() { // Loop just in case more than one character is in UART's receive FIFO buffer // Stop if buffer full while ((TAG.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) { rx_buffer[rx_in] = TAG.getc(); // Uncomment to Echo to USB serial to watch data flow // monitor_TAG.putc(rx_buffer[rx_in]); rx_in = (rx_in + 1) % buffer_size; } return; }