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 00:22:57 2015 +0000
Child:
1:84af7a219830
Commit message:
initial.

Changed in this revision

SNICInterface_mod.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
WebSocketServer.h 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
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SNICInterface_mod.lib	Fri Mar 13 00:22:57 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/teams/KDDI-Fx0-hackathon/code/SNICInterface_mod/#5a5cae02bdf0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WebSocketServer.cpp	Fri Mar 13 00:22:57 2015 +0000
@@ -0,0 +1,8 @@
+#include "WebSocketServer.h"
+
+WebSocketServer::WebSocketServer() {
+}
+
+WebSocketServer::~WebSocketServer() {
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WebSocketServer.h	Fri Mar 13 00:22:57 2015 +0000
@@ -0,0 +1,29 @@
+#ifndef _WEB_SOCKET_SERVER_H_
+#define _WEB_SOCKET_SERVER_H_
+
+#include "TCPSocketServer.h"
+
+class WebSocketHandler
+{
+public:
+    virtual void onOpen() = 0;
+    virtual void onClose() = 0;
+    virtual void onMessage() = 0;
+    virtual void onError() = 0;
+};
+
+class WebSocketServer
+{
+public:
+    WebSocketServer();
+    virtual ~WebSocketServer();
+
+    bool init(int port);
+    void run();
+    bool setHandler(const char* path, WebSocketHandler* handler);
+
+private:
+    TCPSocketServer mTcpSocketServer;
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Mar 13 00:22:57 2015 +0000
@@ -0,0 +1,94 @@
+#include "mbed.h"
+#include "SNIC_WifiInterface.h"
+#include "SNIC_Core.h"
+#include "SNIC_UartMsgUtil.h"
+
+#define SSID        ""
+#define SEC_TYPE    e_SEC_WPA2_AES
+#define SEC_KEY     ""
+
+//static char* getIPAddress();
+
+// tx, rx, cts, rts, reset, alarm=NC, baud=115200
+C_SNIC_WifiInterface wifi(p9, p10, NC, NC, p30);
+
+int main() {
+    int ret = 0;
+
+    ret = wifi.init();
+    if (ret != 0) {
+        printf("ERROR: Failed to init wifi %d\r\n", ret);
+        return 1;
+    }
+    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);
+
+    printf("WiFi connected: %s\r\n", wifi.getIPAddress());
+}
+
+// from http://developer.mbed.org/users/MACRUM/code/SNICInterface_mod/
+//char* ::getIPAddress() {
+//    C_SNIC_Core               *snic_core_p  = C_SNIC_Core::getInstance();
+//    C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
+//    
+//    snic_core_p->lockAPI();
+//    // Get local ip address.
+//    // Get buffer for response payload from MemoryPool
+//    tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf();
+//    if( payload_buf_p == NULL )
+//    {
+//        DEBUG_PRINT("getIPAddress payload_buf_p NULL\r\n");
+//        snic_core_p->unlockAPI();
+//        return 0;
+//    }
+//
+//    C_SNIC_Core::tagSNIC_GET_DHCP_INFO_REQ_T req;
+//    // Make request
+//    req.cmd_sid      = UART_CMD_SID_SNIC_GET_DHCP_INFO_REQ;
+//    req.seq          = mUartRequestSeq++;
+//    req.interface    = 0;
+//    
+//    unsigned char *command_array_p = snic_core_p->getCommandBuf();
+//    unsigned int  command_len;
+//    // Preparation of command
+//    command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req
+//                            , sizeof(C_SNIC_Core::tagSNIC_GET_DHCP_INFO_REQ_T), payload_buf_p->buf, command_array_p );
+//    // Send uart command request
+//    snic_core_p->sendUart( command_len, command_array_p );
+//    // Wait UART response
+//    int ret = uartCmdMgr_p->wait();
+//    if( ret != 0 )
+//    {
+//        DEBUG_PRINT( "getIPAddress failed\r\n" );
+//        snic_core_p->freeCmdBuf( payload_buf_p );
+//        snic_core_p->unlockAPI();
+//        return 0;
+//    }
+//    
+//    if( uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_SNIC_SUCCESS )
+//    {
+//        snic_core_p->freeCmdBuf( payload_buf_p );
+//        snic_core_p->unlockAPI();
+//        return 0;
+//    }
+//    
+//    snic_core_p->freeCmdBuf( payload_buf_p );
+//    snic_core_p->unlockAPI();
+//
+//    sprintf(ip_addr, "%d.%d.%d.%d\0", payload_buf_p->buf[9], payload_buf_p->buf[10], payload_buf_p->buf[11], payload_buf_p->buf[12]);
+//
+//    return ip_addr;
+//}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Fri Mar 13 00:22:57 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#63988a2238f7
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Fri Mar 13 00:22:57 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/7e07b6fb45cf
\ No newline at end of file