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:
Fri Mar 13 11:55:55 2015 +0000
Parent:
5:ce3f1dd90068
Child:
7:6cfe0638b957
Commit message:
fixed errors.

Changed in this revision

GSwifi.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/GSwifi.lib	Fri Mar 13 11:55:55 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/gsfan/code/GSwifi/#0b5e2727e020
--- a/WebSocketServer.cpp	Fri Mar 13 20:27:06 2015 +0900
+++ b/WebSocketServer.cpp	Fri Mar 13 11:55:55 2015 +0000
@@ -1,5 +1,5 @@
 #include "WebSocketServer.h"
-// #include "sha1.h"
+#include "sha1.h"
 
 WebSocketServer::WebSocketServer() {
 }
@@ -40,7 +40,7 @@
 		while (connection.is_connected()) {
 			ret = connection.receive(buf, sizeof(buf) - 1);
 			if (ret == 0) {
-				printf("No data to receive\r\n");
+//				printf("No data to receive\r\n");
 				continue;
 			}
 			if (ret < 0) {
@@ -87,9 +87,9 @@
 			}
 			printf("%s\r\n", line);
 			if (line == &buf[0]) {
-				char* method = strtok(buf, ' ');
-				char* path = strtok(NULL, ' ');
-				char* version = strtok(NULL, ' ');
+				char* method = strtok(buf, " ");
+				char* path = strtok(NULL, " ");
+				char* version = strtok(NULL, " ");
 				printf("[%s] [%s] [%s]\r\n", method, path, version);
 
 			} else if (strncmp(line, UPGRADE_WEBSOCKET, strlen(UPGRADE_WEBSOCKET)) == 0) {
@@ -100,7 +100,7 @@
 				isSecWebSocketKeyFound = true;
 				char* ptr = line + strlen(SEC_WEBSOCKET_KEY);
 				while (*ptr == ' ') ++ptr;
-				strcpy(ptr, key);
+				strcpy(key, ptr);
 				printf("key=%s\r\n", key);
 			}
 			i += 2;
@@ -180,7 +180,7 @@
 
     unsigned char hash[20];
 	char encoded[30];
-    sha1((unsigned char*)buf, strlen(buf), hash);
+    sha1(buf, strlen(buf), (char*)hash);
     base64_encode(hash, 20, encoded, sizeof(encoded));
 
     char resp[] = "HTTP/1.1 101 Switching Protocols\r\n" \
--- a/main.cpp	Fri Mar 13 20:27:06 2015 +0900
+++ b/main.cpp	Fri Mar 13 11:55:55 2015 +0000
@@ -10,6 +10,8 @@
 #define SEC_TYPE    e_SEC_WPA2_AES
 #define SEC_KEY     "wotpasswd"
 
+static bool connectWiFi();
+
 // tx, rx, cts, rts, reset, alarm=NC, baud=115200
 C_SNIC_WifiInterface wifi(p9, p10, NC, NC, p30);
 
@@ -17,33 +19,9 @@
 #if defined(TARGET_LPC1768)
     PHY_PowerDown();
 #endif
-    int ret = 0;
-
-    printf("connecting wifi\r\n");
-
-    ret = wifi.init();
-    if (ret != 0) {
-        printf("ERROR: Failed to init wifi %d\r\n", ret);
-        return 1;
+    while (!connectWiFi()) {
+        wait(3);
     }
-    wait(0.5);
-    
-    ret = wifi.disconnect();
-    if (ret != 0) {
-        printf("ERROR: Failed to disconnect wifi %d\r\n", ret);
-        return 1;
-    }
-    wait(0.3);
-
-    wifi.connect(SSID, strlen(SSID), SEC_TYPE, SEC_KEY, strlen(SEC_KEY));
-    if (ret != 0) {
-        printf("ERROR: Failed to connect wifi %d\r\n", ret);
-        return 1;
-    }
-    wait(0.5);
-
-    wifi.setIPConfig(true);
-    wait(0.5);
 
     printf("WiFi connected: %s\r\n", wifi.getIPAddress());
 
@@ -57,3 +35,33 @@
     server.run();
 }
 
+bool connectWiFi() {
+    printf("connecting wifi\r\n");
+
+    int ret = wifi.init();
+    if (ret != 0) {
+        printf("ERROR: Failed to init wifi %d\r\n", ret);
+        return false;
+    }
+    wait(0.5);
+    
+    ret = wifi.disconnect();
+    if (ret != 0) {
+        printf("ERROR: Failed to disconnect wifi %d\r\n", ret);
+        return false;
+    }
+    wait(0.3);
+
+    wifi.connect(SSID, strlen(SSID), SEC_TYPE, SEC_KEY, strlen(SEC_KEY));
+    if (ret != 0) {
+        printf("ERROR: Failed to connect wifi %d\r\n", ret);
+        return false;
+    }
+    wait(0.5);
+
+    wifi.setIPConfig(true);
+    wait(0.5);
+    
+    return true;
+}
+