Hayato Kikuchi
/
MonitoringTest
Communication Class and Controller Class (ControllerForMbed Class)
main.cpp@2:fd0c21600586, 2021-12-23 (annotated)
- Committer:
- kikuchi8810
- Date:
- Thu Dec 23 08:56:26 2021 +0000
- Revision:
- 2:fd0c21600586
- Parent:
- 1:6633661058ec
modified
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kikuchi8810 | 0:a33375289d79 | 1 | //CommunicationMonitoringクラスの動作確認用プログラム |
kikuchi8810 | 0:a33375289d79 | 2 | //宇井コンのmbed環境での動作確認をするプログラム(ControllerForMbed内で実装し,controllerクラスと区別している) |
kikuchi8810 | 0:a33375289d79 | 3 | //contoller(conrollerForMbed)クラス内のタイムアウト機能を使わないようにしている |
kikuchi8810 | 0:a33375289d79 | 4 | |
kikuchi8810 | 0:a33375289d79 | 5 | #include "mbed.h" |
kikuchi8810 | 2:fd0c21600586 | 6 | #include "SoftPWM.h" //ソフトフェアでPWM出力を行うクラス |
kikuchi8810 | 2:fd0c21600586 | 7 | #include "Button.h" |
kikuchi8810 | 1:6633661058ec | 8 | #include "Controller.h"// DS4のみ通信が確認できているバージョンで,プロジェクトに公開しているクラス |
kikuchi8810 | 1:6633661058ec | 9 | #include "ControllerForMbed.h"//宇井コンとの通信をmbedで行えるように変更を加えたクラスでプロジェクトに公開していない. |
kikuchi8810 | 1:6633661058ec | 10 | #include "CommunicationMonitoring.h" //複数のデバイスの通信状況を確認するクラス(通信監視クラス) |
kikuchi8810 | 1:6633661058ec | 11 | #include "define.h" //コントローラクラスにのみ対応 |
kikuchi8810 | 0:a33375289d79 | 12 | |
kikuchi8810 | 0:a33375289d79 | 13 | #define INT_TIME 0.001 |
kikuchi8810 | 2:fd0c21600586 | 14 | #define CONTROL_PERIOD INT_TIME*10.0 |
kikuchi8810 | 0:a33375289d79 | 15 | #define INT_TIME_MS INT_TIME*1000 |
kikuchi8810 | 0:a33375289d79 | 16 | |
kikuchi8810 | 0:a33375289d79 | 17 | Serial pc(USBTX, USBRX); |
kikuchi8810 | 0:a33375289d79 | 18 | |
kikuchi8810 | 0:a33375289d79 | 19 | #if CON_TYPE == CON_DS4 |
kikuchi8810 | 0:a33375289d79 | 20 | Controller con(P7_4,P7_5, 115200); //TXpin, RXpin, baudrateを設定 |
kikuchi8810 | 0:a33375289d79 | 21 | #elif CON_TYPE == CON_ADACHI |
kikuchi8810 | 0:a33375289d79 | 22 | ControllerForMbed conmbed(P7_4,P7_5, 115200); |
kikuchi8810 | 0:a33375289d79 | 23 | #endif |
kikuchi8810 | 0:a33375289d79 | 24 | |
kikuchi8810 | 0:a33375289d79 | 25 | CommunicationMonitoring commMonitor; |
kikuchi8810 | 1:6633661058ec | 26 | int contoller_num; //通信監視クラス内でのコントローラのデバイス番号 |
kikuchi8810 | 2:fd0c21600586 | 27 | |
kikuchi8810 | 2:fd0c21600586 | 28 | /*ハードウェアのPWMは非対応*/ |
kikuchi8810 | 2:fd0c21600586 | 29 | SoftPWM led_red(LED_RED); |
kikuchi8810 | 2:fd0c21600586 | 30 | SoftPWM led_green(LED_GREEN); |
kikuchi8810 | 2:fd0c21600586 | 31 | SoftPWM led_blue(LED_BLUE); |
kikuchi8810 | 2:fd0c21600586 | 32 | |
kikuchi8810 | 0:a33375289d79 | 33 | DigitalOut led_user(LED_USER); |
kikuchi8810 | 0:a33375289d79 | 34 | DigitalOut pin_emergency(D0); //非常停止信号 |
kikuchi8810 | 0:a33375289d79 | 35 | |
kikuchi8810 | 2:fd0c21600586 | 36 | Button user_sw(USER_BUTTON0); |
kikuchi8810 | 2:fd0c21600586 | 37 | |
kikuchi8810 | 0:a33375289d79 | 38 | Ticker interrupt; |
kikuchi8810 | 0:a33375289d79 | 39 | bool flag_10ms = false; |
kikuchi8810 | 0:a33375289d79 | 40 | bool flag_1s = false; |
kikuchi8810 | 2:fd0c21600586 | 41 | |
kikuchi8810 | 2:fd0c21600586 | 42 | void RGB_LED() //RGBのLEDをいい感じに光らせる |
kikuchi8810 | 2:fd0c21600586 | 43 | { |
kikuchi8810 | 2:fd0c21600586 | 44 | static float counts = 0.0; |
kikuchi8810 | 2:fd0c21600586 | 45 | counts += CONTROL_PERIOD; // ここで光る周期を変えられる(はず) |
kikuchi8810 | 2:fd0c21600586 | 46 | |
kikuchi8810 | 2:fd0c21600586 | 47 | if(counts < CONTROL_PERIOD*100.0){ |
kikuchi8810 | 2:fd0c21600586 | 48 | led_red.write(counts); |
kikuchi8810 | 2:fd0c21600586 | 49 | led_blue.write(1.0 - counts); |
kikuchi8810 | 2:fd0c21600586 | 50 | |
kikuchi8810 | 2:fd0c21600586 | 51 | }else if(counts < CONTROL_PERIOD*100.0*2.0){ |
kikuchi8810 | 2:fd0c21600586 | 52 | led_green.write(counts - 1.0); |
kikuchi8810 | 2:fd0c21600586 | 53 | led_red.write(1.0*2.0 - counts); |
kikuchi8810 | 2:fd0c21600586 | 54 | }else if(counts < CONTROL_PERIOD*100.0*3.0){ |
kikuchi8810 | 2:fd0c21600586 | 55 | led_blue.write(counts - 1.0*2.0); |
kikuchi8810 | 2:fd0c21600586 | 56 | led_green.write(1.0*3.0 - counts); |
kikuchi8810 | 2:fd0c21600586 | 57 | }else{ |
kikuchi8810 | 2:fd0c21600586 | 58 | counts = 0.0; |
kikuchi8810 | 2:fd0c21600586 | 59 | } |
kikuchi8810 | 2:fd0c21600586 | 60 | } |
kikuchi8810 | 2:fd0c21600586 | 61 | |
kikuchi8810 | 0:a33375289d79 | 62 | void interrupt_func() |
kikuchi8810 | 0:a33375289d79 | 63 | { |
kikuchi8810 | 0:a33375289d79 | 64 | static int count_10ms = 0; |
kikuchi8810 | 0:a33375289d79 | 65 | if(count_10ms++ > (INT_TIME_MS*10 - 1)) |
kikuchi8810 | 0:a33375289d79 | 66 | { |
kikuchi8810 | 0:a33375289d79 | 67 | flag_10ms = true; |
kikuchi8810 | 0:a33375289d79 | 68 | count_10ms = 0; |
kikuchi8810 | 0:a33375289d79 | 69 | |
kikuchi8810 | 0:a33375289d79 | 70 | static int count_1s = 0; |
kikuchi8810 | 0:a33375289d79 | 71 | if(count_1s++ > (INT_TIME_MS*100 -1)) |
kikuchi8810 | 0:a33375289d79 | 72 | { |
kikuchi8810 | 0:a33375289d79 | 73 | flag_1s = true; |
kikuchi8810 | 0:a33375289d79 | 74 | count_1s = 0; |
kikuchi8810 | 0:a33375289d79 | 75 | } |
kikuchi8810 | 0:a33375289d79 | 76 | } |
kikuchi8810 | 0:a33375289d79 | 77 | } |
kikuchi8810 | 0:a33375289d79 | 78 | |
kikuchi8810 | 0:a33375289d79 | 79 | int main() |
kikuchi8810 | 0:a33375289d79 | 80 | { |
kikuchi8810 | 0:a33375289d79 | 81 | pc.baud(115200); |
kikuchi8810 | 0:a33375289d79 | 82 | |
kikuchi8810 | 0:a33375289d79 | 83 | /* 第一引数はタイムアウト時間[ms],第二引数はupdate関数の呼び出し周期[ms] */ |
kikuchi8810 | 0:a33375289d79 | 84 | #if CON_TYPE == CON_DS4 |
kikuchi8810 | 0:a33375289d79 | 85 | con.init(1000, INT_TIME_MS*10); //init関数を呼び出さなければタイムアウトの処理は行われない(available関数は常にtrueを返す) |
kikuchi8810 | 0:a33375289d79 | 86 | #elif CON_TYPE == CON_ADACHI |
kikuchi8810 | 0:a33375289d79 | 87 | conmbed.inti(1000, INT_TIME_MS*10); |
kikuchi8810 | 0:a33375289d79 | 88 | #endif |
kikuchi8810 | 1:6633661058ec | 89 | contoller_num = commMonitor.init(1000, INT_TIME_MS*10); //コントローラのタイムアウトと受信周期を設定し,デバイス番号を取得 |
kikuchi8810 | 0:a33375289d79 | 90 | |
kikuchi8810 | 2:fd0c21600586 | 91 | led_red.write(0.0); |
kikuchi8810 | 2:fd0c21600586 | 92 | led_green.write(0.0); |
kikuchi8810 | 2:fd0c21600586 | 93 | led_blue.write(0.0); |
kikuchi8810 | 2:fd0c21600586 | 94 | led_user.write(1); |
kikuchi8810 | 2:fd0c21600586 | 95 | |
kikuchi8810 | 2:fd0c21600586 | 96 | while(!user_sw.button_fall())pc.printf("waiting\r\n"); //user_swが押されるまで待機 |
kikuchi8810 | 2:fd0c21600586 | 97 | led_user.write(0); |
kikuchi8810 | 2:fd0c21600586 | 98 | |
kikuchi8810 | 0:a33375289d79 | 99 | interrupt.attach(&interrupt_func, INT_TIME); |
kikuchi8810 | 0:a33375289d79 | 100 | |
kikuchi8810 | 0:a33375289d79 | 101 | while(1) |
kikuchi8810 | 0:a33375289d79 | 102 | { |
kikuchi8810 | 0:a33375289d79 | 103 | if(flag_10ms) |
kikuchi8810 | 0:a33375289d79 | 104 | { |
kikuchi8810 | 2:fd0c21600586 | 105 | RGB_LED(); |
kikuchi8810 | 2:fd0c21600586 | 106 | |
kikuchi8810 | 0:a33375289d79 | 107 | #if CON_TYPE == CON_DS4 |
kikuchi8810 | 0:a33375289d79 | 108 | //con.update(); //main関数のflag内で呼び出す. |
kikuchi8810 | 1:6633661058ec | 109 | commMonitor.Monitoring(con.update(), contoller_num); //コントローラの通信状況を監視 |
kikuchi8810 | 0:a33375289d79 | 110 | //if(con.available()) |
kikuchi8810 | 1:6633661058ec | 111 | if(commMonitor.isAvailable(contoller_num)) //コントローラと通信しているかを確認 |
kikuchi8810 | 0:a33375289d79 | 112 | { |
kikuchi8810 | 0:a33375289d79 | 113 | int buttonState = con.getButtonState(); |
kikuchi8810 | 0:a33375289d79 | 114 | uint8_t joyRx = con.readJoyRXbyte(); |
kikuchi8810 | 0:a33375289d79 | 115 | uint8_t joyRy = con.readJoyRYbyte(); |
kikuchi8810 | 0:a33375289d79 | 116 | uint8_t joyLx = con.readJoyLXbyte(); |
kikuchi8810 | 0:a33375289d79 | 117 | uint8_t joyLy = con.readJoyLYbyte(); |
kikuchi8810 | 0:a33375289d79 | 118 | |
kikuchi8810 | 0:a33375289d79 | 119 | pc.printf("%d\t", buttonState); |
kikuchi8810 | 0:a33375289d79 | 120 | pc.printf("%d\t", joyRx); |
kikuchi8810 | 0:a33375289d79 | 121 | pc.printf("%d\t", joyRy); |
kikuchi8810 | 0:a33375289d79 | 122 | pc.printf("%d\t", joyLx); |
kikuchi8810 | 0:a33375289d79 | 123 | pc.printf("%d\r\n", joyLy); |
kikuchi8810 | 0:a33375289d79 | 124 | |
kikuchi8810 | 0:a33375289d79 | 125 | led_user.write(0); |
kikuchi8810 | 0:a33375289d79 | 126 | pin_emergency.write(1); //非常停止を解除する |
kikuchi8810 | 0:a33375289d79 | 127 | } |
kikuchi8810 | 0:a33375289d79 | 128 | else |
kikuchi8810 | 0:a33375289d79 | 129 | { |
kikuchi8810 | 0:a33375289d79 | 130 | pc.printf("disconnected\r\n"); |
kikuchi8810 | 0:a33375289d79 | 131 | led_user.write(1); |
kikuchi8810 | 0:a33375289d79 | 132 | pin_emergency.write(0); //非常停止を作動させる |
kikuchi8810 | 0:a33375289d79 | 133 | } |
kikuchi8810 | 0:a33375289d79 | 134 | |
kikuchi8810 | 0:a33375289d79 | 135 | #elif CON_TYPE == CON_ADACHI |
kikuchi8810 | 0:a33375289d79 | 136 | //conmbed.update(); //main関数のflag内で呼び出す. |
kikuchi8810 | 2:fd0c21600586 | 137 | commMonitor.Monitoring(conmbed.update(), contoller_num); //コントローラの通信状況を監視 |
kikuchi8810 | 2:fd0c21600586 | 138 | |
kikuchi8810 | 0:a33375289d79 | 139 | //if(conmbed.available()) |
kikuchi8810 | 2:fd0c21600586 | 140 | if(commMonitor.getAvailable(contoller_num))//コントローラと通信しているかを確認 |
kikuchi8810 | 0:a33375289d79 | 141 | { |
kikuchi8810 | 0:a33375289d79 | 142 | int buttonState = conmbed.getButtonState(); |
kikuchi8810 | 0:a33375289d79 | 143 | uint8_t joyRx = conmbed.readJoyRXbyte(); |
kikuchi8810 | 0:a33375289d79 | 144 | uint8_t joyRy = conmbed.readJoyRYbyte(); |
kikuchi8810 | 0:a33375289d79 | 145 | uint8_t joyLx = conmbed.readJoyLXbyte(); |
kikuchi8810 | 0:a33375289d79 | 146 | uint8_t joyLy = conmbed.readJoyLYbyte(); |
kikuchi8810 | 0:a33375289d79 | 147 | |
kikuchi8810 | 0:a33375289d79 | 148 | pc.printf("%d\t", buttonState); |
kikuchi8810 | 0:a33375289d79 | 149 | pc.printf("%d\t", joyRx); |
kikuchi8810 | 0:a33375289d79 | 150 | pc.printf("%d\t", joyRy); |
kikuchi8810 | 0:a33375289d79 | 151 | pc.printf("%d\t", joyLx); |
kikuchi8810 | 0:a33375289d79 | 152 | pc.printf("%d\r\n", joyLy); |
kikuchi8810 | 0:a33375289d79 | 153 | |
kikuchi8810 | 0:a33375289d79 | 154 | led_user.write(0); |
kikuchi8810 | 0:a33375289d79 | 155 | pin_emergency.write(1); //非常停止を解除する |
kikuchi8810 | 0:a33375289d79 | 156 | } |
kikuchi8810 | 0:a33375289d79 | 157 | else |
kikuchi8810 | 0:a33375289d79 | 158 | { |
kikuchi8810 | 0:a33375289d79 | 159 | pc.printf("disconnected\r\n"); |
kikuchi8810 | 0:a33375289d79 | 160 | led_user.write(1); |
kikuchi8810 | 0:a33375289d79 | 161 | pin_emergency.write(0); //非常停止を作動させる |
kikuchi8810 | 0:a33375289d79 | 162 | } |
kikuchi8810 | 0:a33375289d79 | 163 | #endif |
kikuchi8810 | 0:a33375289d79 | 164 | |
kikuchi8810 | 0:a33375289d79 | 165 | flag_10ms = false; |
kikuchi8810 | 0:a33375289d79 | 166 | } |
kikuchi8810 | 0:a33375289d79 | 167 | |
kikuchi8810 | 0:a33375289d79 | 168 | if(flag_1s) |
kikuchi8810 | 0:a33375289d79 | 169 | { |
kikuchi8810 | 2:fd0c21600586 | 170 | // code here! |
kikuchi8810 | 0:a33375289d79 | 171 | flag_1s = false; |
kikuchi8810 | 0:a33375289d79 | 172 | } |
kikuchi8810 | 0:a33375289d79 | 173 | |
kikuchi8810 | 2:fd0c21600586 | 174 | thread_sleep_for(1); //眠らせないとなんか変 |
kikuchi8810 | 0:a33375289d79 | 175 | } |
kikuchi8810 | 0:a33375289d79 | 176 | } |