
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
WebSocketServer.cpp@4:4a46982a4cf2, 2015-03-13 (annotated)
- Committer:
- flatbird
- Date:
- Fri Mar 13 06:57:04 2015 +0000
- Revision:
- 4:4a46982a4cf2
- Parent:
- 1:84af7a219830
- Child:
- 5:ce3f1dd90068
fixed error by including unnecessary header.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
flatbird | 0:f7596ed7ab5c | 1 | #include "WebSocketServer.h" |
flatbird | 0:f7596ed7ab5c | 2 | |
flatbird | 0:f7596ed7ab5c | 3 | WebSocketServer::WebSocketServer() { |
flatbird | 0:f7596ed7ab5c | 4 | } |
flatbird | 0:f7596ed7ab5c | 5 | |
flatbird | 0:f7596ed7ab5c | 6 | WebSocketServer::~WebSocketServer() { |
flatbird | 0:f7596ed7ab5c | 7 | } |
flatbird | 0:f7596ed7ab5c | 8 | |
flatbird |
1:84af7a219830 | 9 | bool WebSocketServer::init(int port) { |
flatbird |
1:84af7a219830 | 10 | mTCPSocketServer.set_blocking(true); |
flatbird |
1:84af7a219830 | 11 | |
flatbird |
1:84af7a219830 | 12 | int ret = mTCPSocketServer.bind(port); |
flatbird |
1:84af7a219830 | 13 | if (ret != 0) { |
flatbird |
1:84af7a219830 | 14 | printf("ERROR: Failed to bind %d\r\n", ret); |
flatbird |
1:84af7a219830 | 15 | return false; |
flatbird |
1:84af7a219830 | 16 | } |
flatbird |
1:84af7a219830 | 17 | ret = mTCPSocketServer.listen(); |
flatbird |
1:84af7a219830 | 18 | if (ret != 0) { |
flatbird |
1:84af7a219830 | 19 | printf("ERROR: Failed to listen %d\r\n", ret); |
flatbird |
1:84af7a219830 | 20 | return false; |
flatbird |
1:84af7a219830 | 21 | } |
flatbird |
1:84af7a219830 | 22 | |
flatbird |
1:84af7a219830 | 23 | return true; |
flatbird |
1:84af7a219830 | 24 | } |
flatbird |
1:84af7a219830 | 25 | |
flatbird |
1:84af7a219830 | 26 | void WebSocketServer::run() { |
flatbird |
1:84af7a219830 | 27 | TCPSocketConnection connection; |
flatbird |
1:84af7a219830 | 28 | char buf[1024]; |
flatbird |
1:84af7a219830 | 29 | |
flatbird |
1:84af7a219830 | 30 | while (true) { |
flatbird |
1:84af7a219830 | 31 | bool isWebSocket = false; |
flatbird |
1:84af7a219830 | 32 | int ret = mTCPSocketServer.accept(connection); |
flatbird |
1:84af7a219830 | 33 | if (ret != 0) { |
flatbird |
1:84af7a219830 | 34 | continue; |
flatbird |
1:84af7a219830 | 35 | } |
flatbird |
1:84af7a219830 | 36 | printf("New connection\r\n"); |
flatbird |
1:84af7a219830 | 37 | while (connection.is_connected()) { |
flatbird |
1:84af7a219830 | 38 | ret = connection.receive(buf, sizeof(buf) - 1); |
flatbird |
1:84af7a219830 | 39 | if (ret <= 0) { |
flatbird |
1:84af7a219830 | 40 | if (ret == 0) { |
flatbird |
1:84af7a219830 | 41 | printf("closed\r\n"); |
flatbird |
1:84af7a219830 | 42 | } else { |
flatbird |
1:84af7a219830 | 43 | printf("ERROR: Failed to receive %d\r\n", ret); |
flatbird |
1:84af7a219830 | 44 | } |
flatbird |
1:84af7a219830 | 45 | break; |
flatbird |
1:84af7a219830 | 46 | } |
flatbird |
1:84af7a219830 | 47 | if (!isWebSocket) { |
flatbird |
1:84af7a219830 | 48 | if (this->handleHTTP(buf, ret)) { |
flatbird |
1:84af7a219830 | 49 | isWebSocket = true; |
flatbird |
1:84af7a219830 | 50 | } else { |
flatbird |
1:84af7a219830 | 51 | printf("ERROR: Non websocket\r\n"); |
flatbird |
1:84af7a219830 | 52 | break; |
flatbird |
1:84af7a219830 | 53 | } |
flatbird |
1:84af7a219830 | 54 | } else { |
flatbird |
1:84af7a219830 | 55 | if (!this->handleWebSocket(buf, ret)) { |
flatbird |
1:84af7a219830 | 56 | break; |
flatbird |
1:84af7a219830 | 57 | } |
flatbird |
1:84af7a219830 | 58 | } |
flatbird |
1:84af7a219830 | 59 | } |
flatbird |
1:84af7a219830 | 60 | connection.close(); |
flatbird |
1:84af7a219830 | 61 | } |
flatbird |
1:84af7a219830 | 62 | } |
flatbird |
1:84af7a219830 | 63 | |
flatbird |
1:84af7a219830 | 64 | bool WebSocketServer::setHandler(const char* path, WebSocketHandler* handler) { |
flatbird |
1:84af7a219830 | 65 | return true; |
flatbird |
1:84af7a219830 | 66 | } |
flatbird |
1:84af7a219830 | 67 | |
flatbird |
1:84af7a219830 | 68 | bool WebSocketServer::handleHTTP(char* buf, int size) { |
flatbird |
1:84af7a219830 | 69 | char* line = &buf[0]; |
flatbird |
1:84af7a219830 | 70 | |
flatbird |
1:84af7a219830 | 71 | for (int i = 0; i < size; i++) { |
flatbird |
1:84af7a219830 | 72 | if (buf[i] == '\r' && i+1 < size && buf[i+1] == '\n') { |
flatbird |
1:84af7a219830 | 73 | buf[i] = '\0'; |
flatbird |
1:84af7a219830 | 74 | if (strlen(buf) <= 0) { |
flatbird |
1:84af7a219830 | 75 | break; |
flatbird |
1:84af7a219830 | 76 | } |
flatbird |
1:84af7a219830 | 77 | printf("%s\r\n", line); |
flatbird |
1:84af7a219830 | 78 | i += 2; |
flatbird |
1:84af7a219830 | 79 | line = &buf[i]; |
flatbird |
1:84af7a219830 | 80 | } |
flatbird |
1:84af7a219830 | 81 | } |
flatbird |
1:84af7a219830 | 82 | |
flatbird |
1:84af7a219830 | 83 | return true; |
flatbird |
1:84af7a219830 | 84 | } |
flatbird |
1:84af7a219830 | 85 | |
flatbird |
1:84af7a219830 | 86 | bool WebSocketServer::handleWebSocket(char* buf, int size) { |
flatbird |
1:84af7a219830 | 87 | return true; |
flatbird |
1:84af7a219830 | 88 | } |