【これは旧バージョンです】 AndroidのBLEラジコンプロポアプリ「BLEPropo」と接続し、RCサーボとDCモータを制御するプログラムです。 mbed HRM1017で動作を確認しています。 BLEPropo → https://github.com/lipoyang/BLEPropo

Dependencies:   BLE_API mbed

Fork of BLE_RCBController2 by Junichi Katsu

BLEを使ったAndroid用ラジコンプロポアプリ「BLEPropo」に対応するmbed HRM1017用ファームウェアです。
BLEPropoは、GitHubにて公開中。
https://github.com/lipoyang/BLEPropo
/media/uploads/lipoyang/blepropo_ui.png
ラジコンは、mbed HRM1017とRCサーボやDCモータを組み合わせて作ります。
/media/uploads/lipoyang/ble_wiring.png

Files at this revision

API Documentation at this revision

Comitter:
lipoyang
Date:
Wed Sep 10 04:11:28 2014 +0000
Parent:
4:ebda47d22091
Child:
6:1b978b32118c
Commit message:
1st test version.

Changed in this revision

RCBController.h Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/RCBController.h	Wed Aug 20 13:41:01 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,74 +0,0 @@
-
-
-typedef union
-{
-
-	struct  {
-/*
-1-2バイト目:ボタン
- UP: 0x0001
- DOWN: 0x0002
- RIGHT: 0x0004
- LEFT: 0x0008
- Y button: 0x0010
- A button: 0x0020
- B button: 0x0040
- X button: 0x0100
- L1: 0x0200
- L2: 0x0400
- R1: 0x0800
- R2: 0x1000
- START: 0x0003
- SELECT: 0x000C
-*/
-		unsigned X		: 1;
-		unsigned L1		: 1;
-		unsigned L2		: 1;
-		unsigned R1		: 1;
-		unsigned R2		: 1;
-		unsigned space1	: 3;
-		unsigned UP		: 1;
-		unsigned DOWN	: 1;
-		unsigned RIGHT	: 1;
-		unsigned LEFT	: 1;
-		unsigned Y		: 1;
-		unsigned A		: 1;
-		unsigned B		: 1;
-		unsigned space2	: 1;
-/*
-3-4バイト目:左アナログ
- 左右: 1-255 (Neutral=128) 
- 上下: 1-255 (Neutral=128)
-*/
-		unsigned LeftAnalogLR:8;
-		unsigned LeftAnalogUD:8;
-/*
-5-6バイト目:右アナログ
- 左右: 1-255 (Neutral=128) 
- 上下: 1-255 (Neutral=128)
-*/
-		unsigned RightAnalogLR:8;
-		unsigned RightAnalogUD:8;
-/*
-7-9バイト目:アクセラレータ
- X軸: 1-255 (Neutral=128) 
- Y軸: 1-255 (Neutral=128)
- Z軸: 1-255 (Neutral=128)
-*/
-		unsigned AcceleX:8;
-		unsigned AcceleY:8;
-		unsigned AcceleZ:8;
-/*
-10バイト目:設定(向き、設定)
- 7-6bit目: アクセラレータ設定(0-3)
- 5bit目: 左アナログ(0-1)
- 4bit目: 右アナログ(0-1)
- 3-1bit目: iOSデバイス向き(1-4)
-*/
-		unsigned DEV_DIR:4;
-		unsigned RIGHT_ANALOG:1;
-		unsigned LEFT_ANALOG:1;
-		unsigned ACCELE_SETTING:2;
-	}status;
-	unsigned char data[10];
-}RCBController;
\ No newline at end of file
--- a/main.cpp	Wed Aug 20 13:41:01 2014 +0000
+++ b/main.cpp	Wed Sep 10 04:11:28 2014 +0000
@@ -1,95 +1,153 @@
 #include "mbed.h"
 #include "BLEDevice.h"
-#include "RCBController.h"
 
 #define DBG 1
 
+// BLE device object
 BLEDevice  ble;
-Serial  pc(USBTX, USBRX);
-/* LEDs for indication: */
-DigitalOut  ConnectStateLed(LED1);
-PwmOut  ControllerStateLed(LED2);
 
+// BluePropo service UUID
+//static const uint16_t UUID_BLUEPROPO = 0xFFF0;
+static const uint8_t UUID_BLUEPROPO[] = 
+{ 0xc4, 0x9d, 0xfd, 0x1b, 0x86, 0x04, 0x41, 0xd2, 0x89, 0x43, 0x13, 0x6f, 0x21, 0x4d, 0xd0, 0xbf };
+
+// BluePropo::Stick characteristic UUID
+//static const uint16_t UUID_BLUEPROPO_STICK = 0xFFF1;
+static const uint8_t UUID_BLUEPROPO_STICK[] =
+{ 0x74, 0x25, 0xfb, 0xa0, 0x72, 0x15, 0x41, 0x36, 0xaa, 0x3f, 0x07, 0x2a, 0xa0, 0x7d, 0x93, 0x54 };
 
-/* RCBController Service */
-static const uint16_t RCBController_service_uuid = 0xFFF0;
-static const uint16_t RCBController_Characteristic_uuid = 0xFFF1;
-uint8_t RCBControllerPayload[10] = {0,};
+// BluePropo::Stick data structure
+union StickData
+{
+	struct  {
+		// F(-128)<- 0 ->B(+127)
+		signed char fb;
+		// L(-128)<- 0 ->R(+127)
+		signed char lr;
+	}value;
+	unsigned char bytes[2];
+};
+StickData stickData;
 
-GattCharacteristic  ControllerChar (RCBController_Characteristic_uuid,RCBControllerPayload,10, 10,
+// buffer for BluePropo payload
+uint8_t payload[10] = {0,};
+
+// BluePropo::Stick characteristic
+GattCharacteristic  charStick (UUID_BLUEPROPO_STICK, payload, 2, 2,
 								GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | 
 								GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
-GattCharacteristic *ControllerChars[] = {&ControllerChar};
-GattService         RCBControllerService(RCBController_service_uuid, ControllerChars, sizeof(ControllerChars) / sizeof(GattCharacteristic *));
+// BluePropo characteristics set
+GattCharacteristic *chars[] = {&charStick};
+// BluePropo service
+GattService         serviceBluePropo(UUID_BLUEPROPO, chars, sizeof(chars) / sizeof(GattCharacteristic *));
+
+// USB COM port for Debug
+Serial  pc(USBTX, USBRX);
+
+// pin asign
+DigitalOut tb6612_ain1(P0_28);
+DigitalOut tb6612_ain2(P0_29);
+PwmOut     tb6612_pwma(P0_30);
+PwmOut     servo_pwm  (P0_12);
 
-RCBController controller;
+// DC motor driver (TB6612)
+void motor (float speed)
+{
+    if (speed > 0) {
+        // CW
+        tb6612_pwma = speed;
+        tb6612_ain1 = 1;
+        tb6612_ain2 = 0;
+    } else
+    if (speed < 0) {
+        // CCW
+        tb6612_pwma = - speed;
+        tb6612_ain1 = 0;
+        tb6612_ain2 = 1;
+    } else {
+        // stop
+        tb6612_pwma = 1;
+        tb6612_ain1 = 0;
+        tb6612_ain2 = 0;
+//        // break
+//        tb6612_pwma = 1;
+//        tb6612_ain1 = 1;
+//        tb6612_ain2 = 1;
+    }
+}
 
+// RC servo
+void servo (float deg)
+{
+    servo_pwm.pulsewidth_us(1500 + (int)(500.0 * deg));
+}
+
+// BLE onConnection handler
 void onConnected(uint16_t h)
 {
-    ConnectStateLed = 0;
 #if DBG
 	pc.printf("Connected\n\r");
 #endif
 }
 
+// BLE onDisconnection handler
 void onDisconnected(uint16_t h)
 {
     ble.startAdvertising();
-	ConnectStateLed = 1;
 #if DBG
 	pc.printf("Disconnected\n\r");
 #endif
 }
 
-
-// GattEvent
+// BLE onDataWritten handler (Gatt event)
 void onDataWritten(uint16_t charHandle)
 {
-	if (charHandle == ControllerChar.getHandle()) {
+	if (charHandle == charStick.getHandle()) {
 		uint16_t bytesRead;
-	 	ble.readCharacteristicValue(ControllerChar.getHandle(),RCBControllerPayload, &bytesRead);
-        memcpy( &controller.data[0], RCBControllerPayload, sizeof(controller));
+	 	ble.readCharacteristicValue(charStick.getHandle(),payload, &bytesRead);
+        memcpy( &stickData.bytes[0], payload, sizeof(stickData));
 #if DBG
 
-		pc.printf("DATA:%02X %02X %d %d %d %d %d %d %d %02X\n\r",controller.data[0],controller.data[1],controller.data[2],controller.data[3],controller.data[4],
-															   controller.data[5],controller.data[6],controller.data[7],controller.data[8],controller.data[9]);
+		pc.printf("DATA:%02X %02X\n\r",stickData.bytes[0],stickData.bytes[1]);
 #endif
-		ControllerStateLed = (float)controller.status.LeftAnalogLR / 255.0;		
+ 		float m = (float)stickData.value.fb / 128.0;
+        motor(m);
+        float s = 0.5 + (float)stickData.value.lr /256.0;
+        if(s<0) s=0;
+        servo(s);
 	}
 		 
 }
 
-/**************************************************************************/
-/*!
-    @brief  Program entry point
-*/
-/**************************************************************************/
+// Program entry point
 int main(void)
 {
 #if DBG
 		pc.printf("Start\n\r");
 #endif
+	// initialize servo & motor
+    servo_pwm.period_ms(20);
+    servo(0.5);
+    motor(0);
 	
+	// initialize BLE
     ble.init(); 
     ble.onConnection(onConnected);
     ble.onDisconnection(onDisconnected);
     ble.onDataWritten(onDataWritten);
-	
-    /* setup advertising */
+    // setup advertising
     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
-                                    (const uint8_t *)"mbed HRM1017", sizeof("mbed HRM1017") - 1);
+                                    (const uint8_t *)"MiniSteer HRM1017", sizeof("MiniSteer HRM1017") - 1);
     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
-                                    (const uint8_t *)RCBController_service_uuid, sizeof(RCBController_service_uuid));
-
+                                    (const uint8_t *)UUID_BLUEPROPO, sizeof(UUID_BLUEPROPO));
     ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
     ble.startAdvertising();
+    ble.addService(serviceBluePropo);
 
-    ble.addService(RCBControllerService);
-
+	// main loop (wait for BLE event)
     while (true) {
         ble.waitForEvent();
     }
 }
-