Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
uart.cpp@7:bfbbf605be43, 2018-11-24 (annotated)
- Committer:
- MasashiNomura
- Date:
- Sat Nov 24 11:49:51 2018 +0000
- Revision:
- 7:bfbbf605be43
- Parent:
- 1:15ab74f0d0f1
- Child:
- 8:1ca49cb18290
?????Z???????yaw,accZ,gyroZ?????
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| takeru0x1103 | 0:ecd925601fc6 | 1 | #include "typedef.h" |
| takeru0x1103 | 0:ecd925601fc6 | 2 | #include "uart.h" |
| takeru0x1103 | 1:15ab74f0d0f1 | 3 | #include "command.h" |
| takeru0x1103 | 0:ecd925601fc6 | 4 | |
| MasashiNomura | 7:bfbbf605be43 | 5 | #include <vector> |
| MasashiNomura | 7:bfbbf605be43 | 6 | #include <string> |
| MasashiNomura | 7:bfbbf605be43 | 7 | #include <sstream> |
| MasashiNomura | 7:bfbbf605be43 | 8 | using namespace std; |
| MasashiNomura | 7:bfbbf605be43 | 9 | |
| takeru0x1103 | 0:ecd925601fc6 | 10 | //#define WIREED_SERIAL |
| takeru0x1103 | 0:ecd925601fc6 | 11 | |
| takeru0x1103 | 0:ecd925601fc6 | 12 | //シリアルポートインスタンス |
| takeru0x1103 | 0:ecd925601fc6 | 13 | #ifdef WIREED_SERIAL |
| takeru0x1103 | 0:ecd925601fc6 | 14 | Serial sp(USBTX, USBRX); //有線のポート |
| takeru0x1103 | 0:ecd925601fc6 | 15 | #define BAUD_RATE 9600 |
| takeru0x1103 | 0:ecd925601fc6 | 16 | #else |
| takeru0x1103 | 0:ecd925601fc6 | 17 | Serial sp(p9, p10);//TWILITEをモジュールを接続したポート |
| takeru0x1103 | 0:ecd925601fc6 | 18 | #define BAUD_RATE 115200 |
| takeru0x1103 | 0:ecd925601fc6 | 19 | #endif |
| MasashiNomura | 7:bfbbf605be43 | 20 | |
| MasashiNomura | 7:bfbbf605be43 | 21 | Serial sp46Axis(p28,p27); |
| MasashiNomura | 7:bfbbf605be43 | 22 | #define BAUD_RATE4_6AXIS 115200 |
| MasashiNomura | 7:bfbbf605be43 | 23 | |
| takeru0x1103 | 0:ecd925601fc6 | 24 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| takeru0x1103 | 0:ecd925601fc6 | 25 | // UART 受信割り込みハンドラ |
| takeru0x1103 | 0:ecd925601fc6 | 26 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| takeru0x1103 | 0:ecd925601fc6 | 27 | void uartRxIntHndler(){ |
| takeru0x1103 | 0:ecd925601fc6 | 28 | //1Byte抜き出す |
| takeru0x1103 | 0:ecd925601fc6 | 29 | UCHAR buf = sp.getc(); |
| takeru0x1103 | 0:ecd925601fc6 | 30 | //エコーバック |
| takeru0x1103 | 0:ecd925601fc6 | 31 | sp.putc(buf); |
| takeru0x1103 | 0:ecd925601fc6 | 32 | //コマンドバッファに突っ込む |
| takeru0x1103 | 1:15ab74f0d0f1 | 33 | commandPush(buf); |
| takeru0x1103 | 0:ecd925601fc6 | 34 | } |
| takeru0x1103 | 0:ecd925601fc6 | 35 | |
| MasashiNomura | 7:bfbbf605be43 | 36 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| MasashiNomura | 7:bfbbf605be43 | 37 | // UART 受信割り込みハンドラ for Arduino UNO(Six Axis Sensor) |
| MasashiNomura | 7:bfbbf605be43 | 38 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| MasashiNomura | 7:bfbbf605be43 | 39 | void uartRxIntHndler46Axis(){ |
| MasashiNomura | 7:bfbbf605be43 | 40 | //1Byte抜き出す |
| MasashiNomura | 7:bfbbf605be43 | 41 | UCHAR buf = sp46Axis.getc(); |
| MasashiNomura | 7:bfbbf605be43 | 42 | //エコーバック |
| MasashiNomura | 7:bfbbf605be43 | 43 | sp46Axis.putc(buf); |
| MasashiNomura | 7:bfbbf605be43 | 44 | //スルーするためのバッファに突っ込む |
| MasashiNomura | 7:bfbbf605be43 | 45 | PushBuf(buf); |
| MasashiNomura | 7:bfbbf605be43 | 46 | } |
| MasashiNomura | 7:bfbbf605be43 | 47 | |
| takeru0x1103 | 0:ecd925601fc6 | 48 | //============================================================= |
| takeru0x1103 | 0:ecd925601fc6 | 49 | //初期設定 |
| takeru0x1103 | 0:ecd925601fc6 | 50 | //============================================================= |
| takeru0x1103 | 0:ecd925601fc6 | 51 | void uartInit(){ |
| takeru0x1103 | 0:ecd925601fc6 | 52 | //ボーレート設定 |
| takeru0x1103 | 0:ecd925601fc6 | 53 | sp.baud(BAUD_RATE); |
| takeru0x1103 | 0:ecd925601fc6 | 54 | |
| takeru0x1103 | 0:ecd925601fc6 | 55 | //受信割り込みハンドラ登録 |
| takeru0x1103 | 0:ecd925601fc6 | 56 | sp.attach |
| takeru0x1103 | 0:ecd925601fc6 | 57 | (uartRxIntHndler //登録するハンドラ |
| takeru0x1103 | 0:ecd925601fc6 | 58 | ,Serial::RxIrq //割り込み要因 |
| takeru0x1103 | 0:ecd925601fc6 | 59 | ); |
| takeru0x1103 | 0:ecd925601fc6 | 60 | |
| takeru0x1103 | 0:ecd925601fc6 | 61 | sp.printf("***********\r\n"); |
| takeru0x1103 | 0:ecd925601fc6 | 62 | sp.printf("UART open!!\r\n"); |
| takeru0x1103 | 0:ecd925601fc6 | 63 | sp.printf("-----------\r\n"); |
| takeru0x1103 | 0:ecd925601fc6 | 64 | sp.printf("short = %d\r\n" ,sizeof(unsigned short) ); |
| takeru0x1103 | 0:ecd925601fc6 | 65 | sp.printf("int = %d\r\n" ,sizeof(unsigned int) ); |
| takeru0x1103 | 0:ecd925601fc6 | 66 | sp.printf("long = %d\r\n" ,sizeof(unsigned long) ); |
| takeru0x1103 | 0:ecd925601fc6 | 67 | sp.printf("***********\r\n"); |
| takeru0x1103 | 0:ecd925601fc6 | 68 | |
| MasashiNomura | 7:bfbbf605be43 | 69 | //////////////////////////// |
| MasashiNomura | 7:bfbbf605be43 | 70 | // for 6Axis Sensor |
| MasashiNomura | 7:bfbbf605be43 | 71 | //ボーレート設定 |
| MasashiNomura | 7:bfbbf605be43 | 72 | sp46Axis.baud(BAUD_RATE4_6AXIS); |
| MasashiNomura | 7:bfbbf605be43 | 73 | |
| MasashiNomura | 7:bfbbf605be43 | 74 | //受信割り込みハンドラ登録 |
| MasashiNomura | 7:bfbbf605be43 | 75 | sp46Axis.attach |
| MasashiNomura | 7:bfbbf605be43 | 76 | (uartRxIntHndler46Axis //登録するハンドラ |
| MasashiNomura | 7:bfbbf605be43 | 77 | ,Serial::RxIrq //割り込み要因 |
| MasashiNomura | 7:bfbbf605be43 | 78 | ); |
| MasashiNomura | 7:bfbbf605be43 | 79 | |
| MasashiNomura | 7:bfbbf605be43 | 80 | sp46Axis.printf("***********\r\n"); |
| MasashiNomura | 7:bfbbf605be43 | 81 | sp46Axis.printf("UART open!!\r\n"); |
| MasashiNomura | 7:bfbbf605be43 | 82 | sp46Axis.printf("-----------\r\n"); |
| MasashiNomura | 7:bfbbf605be43 | 83 | sp46Axis.printf("short = %d\r\n" ,sizeof(unsigned short) ); |
| MasashiNomura | 7:bfbbf605be43 | 84 | sp46Axis.printf("int = %d\r\n" ,sizeof(unsigned int) ); |
| MasashiNomura | 7:bfbbf605be43 | 85 | sp46Axis.printf("long = %d\r\n" ,sizeof(unsigned long) ); |
| MasashiNomura | 7:bfbbf605be43 | 86 | sp46Axis.printf("***********\r\n"); |
| MasashiNomura | 7:bfbbf605be43 | 87 | |
| MasashiNomura | 7:bfbbf605be43 | 88 | // bufferのクリア |
| MasashiNomura | 7:bfbbf605be43 | 89 | InitBuf(); |
| MasashiNomura | 7:bfbbf605be43 | 90 | //////////////////////////// |
| takeru0x1103 | 0:ecd925601fc6 | 91 | } |
| MasashiNomura | 7:bfbbf605be43 | 92 | |
| MasashiNomura | 7:bfbbf605be43 | 93 | static std::vector<std::string> split(const std::string &str, char sep) |
| MasashiNomura | 7:bfbbf605be43 | 94 | { |
| MasashiNomura | 7:bfbbf605be43 | 95 | std::vector<std::string> v; |
| MasashiNomura | 7:bfbbf605be43 | 96 | std::stringstream ss(str); |
| MasashiNomura | 7:bfbbf605be43 | 97 | std::string buffer; |
| MasashiNomura | 7:bfbbf605be43 | 98 | while( std::getline(ss, buffer, sep) ) { |
| MasashiNomura | 7:bfbbf605be43 | 99 | v.push_back(buffer); |
| MasashiNomura | 7:bfbbf605be43 | 100 | } |
| MasashiNomura | 7:bfbbf605be43 | 101 | return v; |
| MasashiNomura | 7:bfbbf605be43 | 102 | } |
| MasashiNomura | 7:bfbbf605be43 | 103 | |
| MasashiNomura | 7:bfbbf605be43 | 104 | void InitBuf() |
| MasashiNomura | 7:bfbbf605be43 | 105 | { |
| MasashiNomura | 7:bfbbf605be43 | 106 | idxBufByG = 0; |
| MasashiNomura | 7:bfbbf605be43 | 107 | memset(bufDataByGyro, 0x00, sizeof(bufDataByGyro)); |
| MasashiNomura | 7:bfbbf605be43 | 108 | accZ = gyroZ = yaw = 0.0f; |
| MasashiNomura | 7:bfbbf605be43 | 109 | } |
| MasashiNomura | 7:bfbbf605be43 | 110 | |
| MasashiNomura | 7:bfbbf605be43 | 111 | void PushBuf(UCHAR rxChar) |
| MasashiNomura | 7:bfbbf605be43 | 112 | { |
| MasashiNomura | 7:bfbbf605be43 | 113 | bufDataByGyro[idxBufByG] = rxChar; |
| MasashiNomura | 7:bfbbf605be43 | 114 | ++idxBufByG; |
| MasashiNomura | 7:bfbbf605be43 | 115 | if(0x0A != rxChar){ |
| MasashiNomura | 7:bfbbf605be43 | 116 | return; |
| MasashiNomura | 7:bfbbf605be43 | 117 | } |
| MasashiNomura | 7:bfbbf605be43 | 118 | else |
| MasashiNomura | 7:bfbbf605be43 | 119 | { |
| MasashiNomura | 7:bfbbf605be43 | 120 | //std::size_t sz; |
| MasashiNomura | 7:bfbbf605be43 | 121 | std::string str((char*)bufDataByGyro); |
| MasashiNomura | 7:bfbbf605be43 | 122 | std::vector<std::string> list = split(str,0x09); |
| MasashiNomura | 7:bfbbf605be43 | 123 | if(list.size() > 0 && list.size() < 4){ |
| MasashiNomura | 7:bfbbf605be43 | 124 | yaw = atof(list.at(0).c_str()); |
| MasashiNomura | 7:bfbbf605be43 | 125 | accZ = atof(list.at(1).c_str()); |
| MasashiNomura | 7:bfbbf605be43 | 126 | gyroZ = atof(list.at(2).c_str()); |
| MasashiNomura | 7:bfbbf605be43 | 127 | sp.printf("yaw = %f " ,yaw ); |
| MasashiNomura | 7:bfbbf605be43 | 128 | sp.printf("acc = %f" ,accZ ); |
| MasashiNomura | 7:bfbbf605be43 | 129 | sp.printf("gyro = %f\r\n" ,gyroZ ); |
| MasashiNomura | 7:bfbbf605be43 | 130 | } |
| MasashiNomura | 7:bfbbf605be43 | 131 | idxBufByG = 0; |
| MasashiNomura | 7:bfbbf605be43 | 132 | memset(bufDataByGyro, 0x00, sizeof(bufDataByGyro)); |
| MasashiNomura | 7:bfbbf605be43 | 133 | } |
| MasashiNomura | 7:bfbbf605be43 | 134 | } |