Simple WebSocket server to control a tank.
Dependencies: SNICInterface_mod WebSocketServer mbed-rtos mbed PowerControl C12832
au Firefox OS WoTハッカソン on ホワイトデーで使用した、タンクを動かすプログラムです。 ゲームパッドでタンクを操縦します。
ゲームパッドは PC に接続し、ブラウザ上の Web アプリから Gamepad API で入力を取得します。 取得した入力データは WebSocket で mbed 上の WebSocket サーバに送信します。
WebSocket サーバのコードはライブラリ化したので、他のプログラムでもインポートして使えます。
使用した機材
- LPC1768
- Murata TypeYD
- LPC1768 用アプリケーションボード
- TAMIYA トラック&ホイールセット
- TAMIYA ダブルキヤボックス(左右独立4速タイプ)
- TAMIYA ユニバーサルプレート
- TOSHIBA TA7291P x 2
- その他、モバイルバッテリー、電池ボックス等
左右のモータードライバにそれぞれ LPC1768 の p12, p13 と p14, p15 のピンを割り当てていますが、必要に応じてコードを変更してください。
コントローラー側(Webアプリ)
https://github.com/chikoski/wotxwot-control
Firefox ブラウザで動作確認しています(他のブラウザでは動かないかも)。 ゲームパッドの左右のスティックの前後の操作が左右それぞれのモータの前転・後転に対応しています。
動いているところの動画
https://www.facebook.com/video.php?v=456620974491805
ハッカソンでは ARM 賞をいただきました!
(参考) au Firefox OS WoTハッカソン on ホワイトデー
http://au-fx.kddi.com/event/20150314/wot_hackathon0314.html
main.cpp@9:774f408b9740, 2015-03-14 (annotated)
- Committer:
- flatbird
- Date:
- Sat Mar 14 11:06:11 2015 +0000
- Revision:
- 9:774f408b9740
- Parent:
- 8:6635ca3b5a5c
- Child:
- 10:578778037efb
fixed
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
flatbird | 9:774f408b9740 | 1 | #include <stdarg.h> |
flatbird | 9:774f408b9740 | 2 | #include <stdio.h> |
flatbird | 0:f7596ed7ab5c | 3 | #include "mbed.h" |
flatbird | 0:f7596ed7ab5c | 4 | #include "SNIC_WifiInterface.h" |
flatbird | 0:f7596ed7ab5c | 5 | #include "SNIC_Core.h" |
flatbird |
2:160c20430be3 | 6 | #include "WebSocketServer.h" |
flatbird |
2:160c20430be3 | 7 | #if defined(TARGET_LPC1768) |
flatbird |
2:160c20430be3 | 8 | #include "PowerControl/EthernetPowerControl.h" |
flatbird |
2:160c20430be3 | 9 | #endif |
flatbird | 9:774f408b9740 | 10 | #include "C12832.h" |
flatbird | 0:f7596ed7ab5c | 11 | |
flatbird | 8:6635ca3b5a5c | 12 | #define SSID "KDDI_hackathon05" |
flatbird | 0:f7596ed7ab5c | 13 | #define SEC_TYPE e_SEC_WPA2_AES |
flatbird | 8:6635ca3b5a5c | 14 | #define SEC_KEY "123456789" |
flatbird | 8:6635ca3b5a5c | 15 | |
flatbird | 9:774f408b9740 | 16 | #if defined(TARGET_LPC1768) |
flatbird | 9:774f408b9740 | 17 | C12832 lcd(p5, p7, p6, p8, p11); |
flatbird | 9:774f408b9740 | 18 | #endif |
flatbird | 8:6635ca3b5a5c | 19 | |
flatbird | 9:774f408b9740 | 20 | DigitalOut l1(p12); |
flatbird | 9:774f408b9740 | 21 | DigitalOut l2(p13); |
flatbird | 9:774f408b9740 | 22 | |
flatbird | 9:774f408b9740 | 23 | DigitalOut r1(p14); |
flatbird | 9:774f408b9740 | 24 | DigitalOut r2(p15); |
flatbird | 8:6635ca3b5a5c | 25 | |
flatbird | 8:6635ca3b5a5c | 26 | class WSHandler: public WebSocketHandler |
flatbird | 8:6635ca3b5a5c | 27 | { |
flatbird | 8:6635ca3b5a5c | 28 | public: |
flatbird | 8:6635ca3b5a5c | 29 | virtual void onMessage(char* text); |
flatbird | 8:6635ca3b5a5c | 30 | virtual void onMessage(char* data, size_t size); |
flatbird | 9:774f408b9740 | 31 | virtual void onOpen(); |
flatbird | 9:774f408b9740 | 32 | virtual void onClose(); |
flatbird | 8:6635ca3b5a5c | 33 | }; |
flatbird | 0:f7596ed7ab5c | 34 | |
flatbird | 6:79ecd4e53456 | 35 | static bool connectWiFi(); |
flatbird | 9:774f408b9740 | 36 | static void lcd_printf( const char* fmt, ... ); |
flatbird | 6:79ecd4e53456 | 37 | |
flatbird | 0:f7596ed7ab5c | 38 | // tx, rx, cts, rts, reset, alarm=NC, baud=115200 |
flatbird | 0:f7596ed7ab5c | 39 | C_SNIC_WifiInterface wifi(p9, p10, NC, NC, p30); |
flatbird | 0:f7596ed7ab5c | 40 | |
flatbird | 9:774f408b9740 | 41 | |
flatbird | 0:f7596ed7ab5c | 42 | int main() { |
flatbird |
2:160c20430be3 | 43 | #if defined(TARGET_LPC1768) |
flatbird |
2:160c20430be3 | 44 | PHY_PowerDown(); |
flatbird |
2:160c20430be3 | 45 | #endif |
flatbird | 8:6635ca3b5a5c | 46 | while (true) { |
flatbird | 8:6635ca3b5a5c | 47 | if (!connectWiFi()) { |
flatbird | 8:6635ca3b5a5c | 48 | wait(3); |
flatbird | 8:6635ca3b5a5c | 49 | continue; |
flatbird | 8:6635ca3b5a5c | 50 | } |
flatbird | 8:6635ca3b5a5c | 51 | WebSocketServer server; |
flatbird | 8:6635ca3b5a5c | 52 | WSHandler handler; |
flatbird |
2:160c20430be3 | 53 | |
flatbird | 8:6635ca3b5a5c | 54 | if (!server.init(80)) { |
flatbird | 8:6635ca3b5a5c | 55 | printf("Failed to init server\r\n"); |
flatbird | 8:6635ca3b5a5c | 56 | wait(3); |
flatbird | 8:6635ca3b5a5c | 57 | continue; |
flatbird | 8:6635ca3b5a5c | 58 | } |
flatbird | 9:774f408b9740 | 59 | |
flatbird | 8:6635ca3b5a5c | 60 | server.setHandler("/ws", &handler); |
flatbird | 8:6635ca3b5a5c | 61 | server.run(); |
flatbird |
2:160c20430be3 | 62 | } |
flatbird | 0:f7596ed7ab5c | 63 | } |
flatbird | 0:f7596ed7ab5c | 64 | |
flatbird | 6:79ecd4e53456 | 65 | bool connectWiFi() { |
flatbird | 6:79ecd4e53456 | 66 | printf("connecting wifi\r\n"); |
flatbird | 9:774f408b9740 | 67 | lcd_printf("connecting wifi\r\n"); |
flatbird | 6:79ecd4e53456 | 68 | |
flatbird | 6:79ecd4e53456 | 69 | int ret = wifi.init(); |
flatbird | 6:79ecd4e53456 | 70 | if (ret != 0) { |
flatbird | 6:79ecd4e53456 | 71 | printf("ERROR: Failed to init wifi %d\r\n", ret); |
flatbird | 6:79ecd4e53456 | 72 | return false; |
flatbird | 6:79ecd4e53456 | 73 | } |
flatbird | 6:79ecd4e53456 | 74 | wait(0.5); |
flatbird | 6:79ecd4e53456 | 75 | |
flatbird | 6:79ecd4e53456 | 76 | ret = wifi.disconnect(); |
flatbird | 6:79ecd4e53456 | 77 | if (ret != 0) { |
flatbird | 6:79ecd4e53456 | 78 | printf("ERROR: Failed to disconnect wifi %d\r\n", ret); |
flatbird | 6:79ecd4e53456 | 79 | return false; |
flatbird | 6:79ecd4e53456 | 80 | } |
flatbird | 6:79ecd4e53456 | 81 | wait(0.3); |
flatbird | 6:79ecd4e53456 | 82 | |
flatbird | 6:79ecd4e53456 | 83 | wifi.connect(SSID, strlen(SSID), SEC_TYPE, SEC_KEY, strlen(SEC_KEY)); |
flatbird | 6:79ecd4e53456 | 84 | if (ret != 0) { |
flatbird | 6:79ecd4e53456 | 85 | printf("ERROR: Failed to connect wifi %d\r\n", ret); |
flatbird | 6:79ecd4e53456 | 86 | return false; |
flatbird | 6:79ecd4e53456 | 87 | } |
flatbird | 6:79ecd4e53456 | 88 | wait(0.5); |
flatbird | 6:79ecd4e53456 | 89 | |
flatbird | 6:79ecd4e53456 | 90 | wifi.setIPConfig(true); |
flatbird | 6:79ecd4e53456 | 91 | wait(0.5); |
flatbird | 9:774f408b9740 | 92 | |
flatbird | 9:774f408b9740 | 93 | printf("WiFi connected: %s\r\n", wifi.getIPAddress()); |
flatbird | 9:774f408b9740 | 94 | lcd_printf("connected: %s\r\n", wifi.getIPAddress()); |
flatbird | 9:774f408b9740 | 95 | |
flatbird | 9:774f408b9740 | 96 | return true; |
flatbird | 9:774f408b9740 | 97 | } |
flatbird | 9:774f408b9740 | 98 | |
flatbird | 9:774f408b9740 | 99 | void lcd_printf( const char* fmt, ... ) |
flatbird | 9:774f408b9740 | 100 | { |
flatbird | 9:774f408b9740 | 101 | char buffer[ 64 ]; |
flatbird | 6:79ecd4e53456 | 102 | |
flatbird | 9:774f408b9740 | 103 | va_list ap; |
flatbird | 9:774f408b9740 | 104 | va_start( ap, fmt ); |
flatbird | 9:774f408b9740 | 105 | vsnprintf( buffer, 64, fmt, ap ); |
flatbird | 9:774f408b9740 | 106 | va_end( ap ); |
flatbird | 9:774f408b9740 | 107 | |
flatbird | 9:774f408b9740 | 108 | lcd.cls(); |
flatbird | 9:774f408b9740 | 109 | lcd.locate( 0, 3 ); |
flatbird | 9:774f408b9740 | 110 | lcd.printf( buffer ); |
flatbird | 9:774f408b9740 | 111 | //wait( 1.0 ); |
flatbird | 6:79ecd4e53456 | 112 | } |
flatbird | 6:79ecd4e53456 | 113 | |
flatbird | 8:6635ca3b5a5c | 114 | void WSHandler::onMessage(char* text) { |
flatbird | 8:6635ca3b5a5c | 115 | printf("TEXT: [%s]\r\n", text); |
flatbird | 8:6635ca3b5a5c | 116 | } |
flatbird | 8:6635ca3b5a5c | 117 | |
flatbird | 9:774f408b9740 | 118 | void WSHandler::onOpen() { |
flatbird | 9:774f408b9740 | 119 | lcd_printf("websocket opened\r\n"); |
flatbird | 9:774f408b9740 | 120 | } |
flatbird | 9:774f408b9740 | 121 | |
flatbird | 9:774f408b9740 | 122 | void WSHandler::onClose() { |
flatbird | 9:774f408b9740 | 123 | lcd_printf("websocket closed\r\n"); |
flatbird | 9:774f408b9740 | 124 | } |
flatbird | 9:774f408b9740 | 125 | |
flatbird | 8:6635ca3b5a5c | 126 | #define THRESHOLD 30 |
flatbird | 8:6635ca3b5a5c | 127 | |
flatbird | 8:6635ca3b5a5c | 128 | void WSHandler::onMessage(char* data, size_t size) { |
flatbird | 8:6635ca3b5a5c | 129 | int8_t lv = data[0]; |
flatbird | 8:6635ca3b5a5c | 130 | int8_t rv = data[1]; |
flatbird | 8:6635ca3b5a5c | 131 | |
flatbird | 8:6635ca3b5a5c | 132 | printf("[%d/%d]\r\n", lv, rv); |
flatbird | 8:6635ca3b5a5c | 133 | |
flatbird | 8:6635ca3b5a5c | 134 | // 0 0 free |
flatbird | 8:6635ca3b5a5c | 135 | // 0 1 normal rotation |
flatbird | 8:6635ca3b5a5c | 136 | // 1 0 reverse rotation |
flatbird | 8:6635ca3b5a5c | 137 | // 1 1 brake |
flatbird | 8:6635ca3b5a5c | 138 | if (lv <= -THRESHOLD) { // normal |
flatbird | 8:6635ca3b5a5c | 139 | l1 = 0; |
flatbird | 8:6635ca3b5a5c | 140 | l2 = 1; |
flatbird | 8:6635ca3b5a5c | 141 | } else if (lv >= THRESHOLD) { // reverse |
flatbird | 8:6635ca3b5a5c | 142 | l1 = 1; |
flatbird | 8:6635ca3b5a5c | 143 | l2 = 0; |
flatbird | 8:6635ca3b5a5c | 144 | } else { // free |
flatbird | 8:6635ca3b5a5c | 145 | l1 = 0; |
flatbird | 8:6635ca3b5a5c | 146 | l2 = 0; |
flatbird | 8:6635ca3b5a5c | 147 | } |
flatbird | 8:6635ca3b5a5c | 148 | |
flatbird | 8:6635ca3b5a5c | 149 | if (rv <= -THRESHOLD) { // normal |
flatbird | 8:6635ca3b5a5c | 150 | r1 = 0; |
flatbird | 8:6635ca3b5a5c | 151 | r2 = 1; |
flatbird | 8:6635ca3b5a5c | 152 | } else if (rv >= THRESHOLD) { // reverse |
flatbird | 8:6635ca3b5a5c | 153 | r1 = 1; |
flatbird | 8:6635ca3b5a5c | 154 | r2 = 0; |
flatbird | 8:6635ca3b5a5c | 155 | } else { // free |
flatbird | 8:6635ca3b5a5c | 156 | r1 = 0; |
flatbird | 8:6635ca3b5a5c | 157 | r2 = 0; |
flatbird | 8:6635ca3b5a5c | 158 | } |
flatbird | 8:6635ca3b5a5c | 159 | } |
flatbird | 8:6635ca3b5a5c | 160 |