
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@10:578778037efb, 2015-03-16 (annotated)
- Committer:
- flatbird
- Date:
- Mon Mar 16 18:48:50 2015 +0900
- Revision:
- 10:578778037efb
- Parent:
- 9:774f408b9740
added WebSocketConnection.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
flatbird | 0:f7596ed7ab5c | 1 | #include "WebSocketServer.h" |
flatbird |
10:578778037efb | 2 | #include "WebSocketConnection.h" |
flatbird | 0:f7596ed7ab5c | 3 | |
flatbird |
10:578778037efb | 4 | WebSocketServer::WebSocketServer() |
flatbird |
10:578778037efb | 5 | { |
flatbird | 0:f7596ed7ab5c | 6 | } |
flatbird | 0:f7596ed7ab5c | 7 | |
flatbird |
10:578778037efb | 8 | WebSocketServer::~WebSocketServer() |
flatbird |
10:578778037efb | 9 | { |
flatbird | 0:f7596ed7ab5c | 10 | } |
flatbird | 0:f7596ed7ab5c | 11 | |
flatbird |
10:578778037efb | 12 | bool WebSocketServer::init(int port) |
flatbird |
10:578778037efb | 13 | { |
flatbird |
1:84af7a219830 | 14 | mTCPSocketServer.set_blocking(true); |
flatbird |
1:84af7a219830 | 15 | |
flatbird |
1:84af7a219830 | 16 | int ret = mTCPSocketServer.bind(port); |
flatbird |
1:84af7a219830 | 17 | if (ret != 0) { |
flatbird |
1:84af7a219830 | 18 | printf("ERROR: Failed to bind %d\r\n", ret); |
flatbird |
1:84af7a219830 | 19 | return false; |
flatbird |
1:84af7a219830 | 20 | } |
flatbird |
1:84af7a219830 | 21 | ret = mTCPSocketServer.listen(); |
flatbird |
1:84af7a219830 | 22 | if (ret != 0) { |
flatbird |
1:84af7a219830 | 23 | printf("ERROR: Failed to listen %d\r\n", ret); |
flatbird |
1:84af7a219830 | 24 | return false; |
flatbird |
1:84af7a219830 | 25 | } |
flatbird |
1:84af7a219830 | 26 | |
flatbird |
1:84af7a219830 | 27 | return true; |
flatbird |
1:84af7a219830 | 28 | } |
flatbird |
1:84af7a219830 | 29 | |
flatbird |
10:578778037efb | 30 | void WebSocketServer::run() |
flatbird |
10:578778037efb | 31 | { |
flatbird |
10:578778037efb | 32 | WebSocketConnection connection(this); |
flatbird |
1:84af7a219830 | 33 | |
flatbird |
1:84af7a219830 | 34 | while (true) { |
flatbird |
10:578778037efb | 35 | // printf("accepting\r\n"); |
flatbird |
10:578778037efb | 36 | int ret = mTCPSocketServer.accept(connection.getTCPSocketConnection()); |
flatbird |
1:84af7a219830 | 37 | if (ret != 0) { |
flatbird |
1:84af7a219830 | 38 | continue; |
flatbird |
1:84af7a219830 | 39 | } |
flatbird |
10:578778037efb | 40 | connection.run(); |
flatbird |
1:84af7a219830 | 41 | } |
flatbird |
1:84af7a219830 | 42 | } |
flatbird |
1:84af7a219830 | 43 | |
flatbird |
10:578778037efb | 44 | void WebSocketServer::setHandler(const char* path, WebSocketHandler* handler) |
flatbird |
10:578778037efb | 45 | { |
flatbird |
10:578778037efb | 46 | mHandlers[path] = handler; |
flatbird |
5:ce3f1dd90068 | 47 | } |
flatbird |
5:ce3f1dd90068 | 48 | |
flatbird |
10:578778037efb | 49 | WebSocketHandler* WebSocketServer::getHandler(const char* path) |
flatbird |
10:578778037efb | 50 | { |
flatbird |
10:578778037efb | 51 | WebSocketHandlerContainer::iterator it; |
flatbird | 8:6635ca3b5a5c | 52 | |
flatbird |
10:578778037efb | 53 | it = mHandlers.find(path); |
flatbird |
10:578778037efb | 54 | if (it != mHandlers.end()) { |
flatbird |
10:578778037efb | 55 | return it->second; |
flatbird |
7:6cfe0638b957 | 56 | } |
flatbird |
10:578778037efb | 57 | return NULL; |
flatbird |
1:84af7a219830 | 58 | } |
flatbird |
1:84af7a219830 | 59 |