【これは旧バージョンです】 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

Revision:
5:572c91221792
Parent:
2:dd85fdc18224
Child:
6:1b978b32118c
diff -r ebda47d22091 -r 572c91221792 main.cpp
--- 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();
     }
 }
-