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

Files at this revision

API Documentation at this revision

Comitter:
flatbird
Date:
Sat Mar 14 11:06:11 2015 +0000
Parent:
8:6635ca3b5a5c
Child:
10:578778037efb
Commit message:
fixed

Changed in this revision

C12832.lib Show annotated file Show diff for this revision Revisions of this file
WebSocketServer.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/C12832.lib	Sat Mar 14 11:06:11 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/chris/code/C12832/#7de323fa46fe
--- a/WebSocketServer.cpp	Sat Mar 14 07:52:50 2015 +0000
+++ b/WebSocketServer.cpp	Sat Mar 14 11:06:11 2015 +0000
@@ -113,6 +113,9 @@
 
 	if (isUpgradeWebSocket && isSecWebSocketKeyFound) {
 		this->sendUpgradeResponse(key);
+		if (mHandler) {
+			mHandler->onOpen();
+		}
 		mPrevFin = true;
 		return true;
 	}
@@ -143,6 +146,9 @@
 		return true;
 	}
 	if (opcode == OP_CLOSE) {
+		if (mHandler) {
+			mHandler->onClose();
+		}
 		return false;
 	}
 	ptr++;
--- a/main.cpp	Sat Mar 14 07:52:50 2015 +0000
+++ b/main.cpp	Sat Mar 14 11:06:11 2015 +0000
@@ -1,3 +1,5 @@
+#include <stdarg.h>
+#include <stdio.h>
 #include "mbed.h"
 #include "SNIC_WifiInterface.h"
 #include "SNIC_Core.h"
@@ -5,29 +7,38 @@
 #if defined(TARGET_LPC1768)
 #include "PowerControl/EthernetPowerControl.h"
 #endif
+#include "C12832.h"
 
 #define SSID        "KDDI_hackathon05"
 #define SEC_TYPE    e_SEC_WPA2_AES
 #define SEC_KEY     "123456789"
 
-DigitalOut l1(p5);
-DigitalOut l2(p6);
+#if defined(TARGET_LPC1768)
+C12832 lcd(p5, p7, p6, p8, p11);
+#endif
 
-DigitalOut r1(p7);
-DigitalOut r2(p8);
+DigitalOut l1(p12);
+DigitalOut l2(p13);
+
+DigitalOut r1(p14);
+DigitalOut r2(p15);
 
 class WSHandler: public WebSocketHandler
 {
 public:
     virtual void onMessage(char* text);
     virtual void onMessage(char* data, size_t size);
+    virtual void onOpen();
+    virtual void onClose();
 };
 
 static bool connectWiFi();
+static void lcd_printf( const char* fmt, ...  );
 
 // tx, rx, cts, rts, reset, alarm=NC, baud=115200
 C_SNIC_WifiInterface wifi(p9, p10, NC, NC, p30);
 
+
 int main() {
 #if defined(TARGET_LPC1768)
     PHY_PowerDown();
@@ -45,8 +56,7 @@
             wait(3);
             continue;
         }
-        printf("WiFi connected: %s\r\n", wifi.getIPAddress());
-
+    
         server.setHandler("/ws", &handler);
         server.run();
     }
@@ -54,6 +64,7 @@
 
 bool connectWiFi() {
     printf("connecting wifi\r\n");
+    lcd_printf("connecting wifi\r\n");
 
     int ret = wifi.init();
     if (ret != 0) {
@@ -78,14 +89,40 @@
 
     wifi.setIPConfig(true);
     wait(0.5);
+
+    printf("WiFi connected: %s\r\n", wifi.getIPAddress());
+    lcd_printf("connected: %s\r\n", wifi.getIPAddress());
+
+    return true;
+}
+
+void lcd_printf( const char* fmt, ...  )
+{
+    char buffer[ 64 ];
     
-    return true;
+    va_list ap;
+    va_start( ap, fmt );
+    vsnprintf( buffer, 64, fmt, ap );
+    va_end( ap );
+    
+    lcd.cls();
+    lcd.locate( 0, 3 );
+    lcd.printf( buffer );
+    //wait( 1.0 );
 }
 
 void WSHandler::onMessage(char* text) {
     printf("TEXT: [%s]\r\n", text);
 }
 
+void WSHandler::onOpen() {
+    lcd_printf("websocket opened\r\n");
+}
+
+void WSHandler::onClose() {
+    lcd_printf("websocket closed\r\n");
+}
+    
 #define THRESHOLD 30
 
 void WSHandler::onMessage(char* data, size_t size) {