natsu tsumagari
/
test_cadenceClass
回転数計のクラスです。
main.cpp@0:57308daf8dfe, 2016-11-23 (annotated)
- Committer:
- tsumagari
- Date:
- Wed Nov 23 04:40:06 2016 +0000
- Revision:
- 0:57308daf8dfe
- Child:
- 1:6bceac3446ff
Cadence class ?main????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tsumagari | 0:57308daf8dfe | 1 | #include "mbed.h" |
tsumagari | 0:57308daf8dfe | 2 | #include <string> |
tsumagari | 0:57308daf8dfe | 3 | //#include "Cadence.h" |
tsumagari | 0:57308daf8dfe | 4 | |
tsumagari | 0:57308daf8dfe | 5 | DigitalOut myled(LED1); |
tsumagari | 0:57308daf8dfe | 6 | Ticker cadenceTicker; |
tsumagari | 0:57308daf8dfe | 7 | Serial pc(USBTX,USBRX); //for debug |
tsumagari | 0:57308daf8dfe | 8 | |
tsumagari | 0:57308daf8dfe | 9 | class Cadence : public Serial{ |
tsumagari | 0:57308daf8dfe | 10 | private: |
tsumagari | 0:57308daf8dfe | 11 | static const int DATAS_NUM = 74; |
tsumagari | 0:57308daf8dfe | 12 | string strData; |
tsumagari | 0:57308daf8dfe | 13 | int gomi; |
tsumagari | 0:57308daf8dfe | 14 | |
tsumagari | 0:57308daf8dfe | 15 | protected: |
tsumagari | 0:57308daf8dfe | 16 | |
tsumagari | 0:57308daf8dfe | 17 | public: |
tsumagari | 0:57308daf8dfe | 18 | char data[DATAS_NUM]; |
tsumagari | 0:57308daf8dfe | 19 | int data_count, data_num; |
tsumagari | 0:57308daf8dfe | 20 | double cadence, cadenceV; |
tsumagari | 0:57308daf8dfe | 21 | string strV,strC; |
tsumagari | 0:57308daf8dfe | 22 | |
tsumagari | 0:57308daf8dfe | 23 | Cadence(PinName tx, PinName rx, const char* name = NULL) : Serial(tx, rx, NULL){ |
tsumagari | 0:57308daf8dfe | 24 | for(int i=0;i<DATAS_NUM;i++) data[i]= NULL; |
tsumagari | 0:57308daf8dfe | 25 | data_num=0; |
tsumagari | 0:57308daf8dfe | 26 | data_count=0; |
tsumagari | 0:57308daf8dfe | 27 | baud(115200); |
tsumagari | 0:57308daf8dfe | 28 | cadence=0; |
tsumagari | 0:57308daf8dfe | 29 | cadenceV=0; |
tsumagari | 0:57308daf8dfe | 30 | } |
tsumagari | 0:57308daf8dfe | 31 | // void Init(){ |
tsumagari | 0:57308daf8dfe | 32 | // for(int i=0;i<DATAS_NUM;i++) data[i]= NULL; |
tsumagari | 0:57308daf8dfe | 33 | // data_num=0; |
tsumagari | 0:57308daf8dfe | 34 | // data_count=0; |
tsumagari | 0:57308daf8dfe | 35 | // baud(115200); |
tsumagari | 0:57308daf8dfe | 36 | // } |
tsumagari | 0:57308daf8dfe | 37 | void cadenceRead(){ //Ticker で定期的に呼び出して値を更新 |
tsumagari | 0:57308daf8dfe | 38 | data_count=0; |
tsumagari | 0:57308daf8dfe | 39 | data_num=0; |
tsumagari | 0:57308daf8dfe | 40 | do{ |
tsumagari | 0:57308daf8dfe | 41 | if(readable()) { |
tsumagari | 0:57308daf8dfe | 42 | data[data_count] = getc(); |
tsumagari | 0:57308daf8dfe | 43 | data_count++; |
tsumagari | 0:57308daf8dfe | 44 | } |
tsumagari | 0:57308daf8dfe | 45 | }while(data[data_count-1] != '\r'); |
tsumagari | 0:57308daf8dfe | 46 | |
tsumagari | 0:57308daf8dfe | 47 | if(data_count > DATAS_NUM-4 ){ |
tsumagari | 0:57308daf8dfe | 48 | strData = data; |
tsumagari | 0:57308daf8dfe | 49 | switch (strData.length()){ |
tsumagari | 0:57308daf8dfe | 50 | case DATAS_NUM-3 : |
tsumagari | 0:57308daf8dfe | 51 | strData.erase(0,1); |
tsumagari | 0:57308daf8dfe | 52 | break; |
tsumagari | 0:57308daf8dfe | 53 | case DATAS_NUM-2 : |
tsumagari | 0:57308daf8dfe | 54 | strData.erase(0,2); |
tsumagari | 0:57308daf8dfe | 55 | break; |
tsumagari | 0:57308daf8dfe | 56 | case DATAS_NUM-1 : |
tsumagari | 0:57308daf8dfe | 57 | strData.erase(0,3); |
tsumagari | 0:57308daf8dfe | 58 | break; |
tsumagari | 0:57308daf8dfe | 59 | case DATAS_NUM : |
tsumagari | 0:57308daf8dfe | 60 | strData.erase(0,4); |
tsumagari | 0:57308daf8dfe | 61 | } |
tsumagari | 0:57308daf8dfe | 62 | strV = strData.substr(27,4); |
tsumagari | 0:57308daf8dfe | 63 | strC = strData.substr(64,4); |
tsumagari | 0:57308daf8dfe | 64 | sscanf(strC.c_str(),"%lf",&cadence); |
tsumagari | 0:57308daf8dfe | 65 | sscanf(strV.c_str(),"%lf",&cadenceV); |
tsumagari | 0:57308daf8dfe | 66 | cadence /= 6.0; |
tsumagari | 0:57308daf8dfe | 67 | cadenceV *=0.001; |
tsumagari | 0:57308daf8dfe | 68 | } |
tsumagari | 0:57308daf8dfe | 69 | } |
tsumagari | 0:57308daf8dfe | 70 | }; |
tsumagari | 0:57308daf8dfe | 71 | |
tsumagari | 0:57308daf8dfe | 72 | |
tsumagari | 0:57308daf8dfe | 73 | |
tsumagari | 0:57308daf8dfe | 74 | Cadence cadence(p13,p14); |
tsumagari | 0:57308daf8dfe | 75 | |
tsumagari | 0:57308daf8dfe | 76 | int main() { |
tsumagari | 0:57308daf8dfe | 77 | pc.baud(115200); |
tsumagari | 0:57308daf8dfe | 78 | while(1) { |
tsumagari | 0:57308daf8dfe | 79 | myled = 1; |
tsumagari | 0:57308daf8dfe | 80 | wait(0.2); |
tsumagari | 0:57308daf8dfe | 81 | myled = 0; |
tsumagari | 0:57308daf8dfe | 82 | wait(0.2); |
tsumagari | 0:57308daf8dfe | 83 | pc.printf("test\n\r"); |
tsumagari | 0:57308daf8dfe | 84 | pc.printf("cadence: %f\n\r",cadence.cadence); |
tsumagari | 0:57308daf8dfe | 85 | pc.printf("voltage: %f\n\r",cadence.cadenceV); |
tsumagari | 0:57308daf8dfe | 86 | pc.printf("%d,\n\r",cadence.data_count); |
tsumagari | 0:57308daf8dfe | 87 | cadence.cadenceRead(); |
tsumagari | 0:57308daf8dfe | 88 | } |
tsumagari | 0:57308daf8dfe | 89 | } |