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

Committer:
lipoyang
Date:
Tue Sep 16 13:09:22 2014 +0000
Revision:
7:6115339996eb
Parent:
6:1b978b32118c
Child:
9:ed80cfb10df8
connection interval changed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft 0:8c643bfe55b7 1 #include "mbed.h"
jksoft 1:48f6e08a3ac2 2 #include "BLEDevice.h"
jksoft 0:8c643bfe55b7 3
jksoft 1:48f6e08a3ac2 4 #define DBG 1
jksoft 0:8c643bfe55b7 5
lipoyang 5:572c91221792 6 // BLE device object
jksoft 1:48f6e08a3ac2 7 BLEDevice ble;
jksoft 0:8c643bfe55b7 8
lipoyang 5:572c91221792 9 // BluePropo service UUID
lipoyang 5:572c91221792 10 //static const uint16_t UUID_BLUEPROPO = 0xFFF0;
lipoyang 5:572c91221792 11 static const uint8_t UUID_BLUEPROPO[] =
lipoyang 5:572c91221792 12 { 0xc4, 0x9d, 0xfd, 0x1b, 0x86, 0x04, 0x41, 0xd2, 0x89, 0x43, 0x13, 0x6f, 0x21, 0x4d, 0xd0, 0xbf };
lipoyang 5:572c91221792 13
lipoyang 5:572c91221792 14 // BluePropo::Stick characteristic UUID
lipoyang 5:572c91221792 15 //static const uint16_t UUID_BLUEPROPO_STICK = 0xFFF1;
lipoyang 5:572c91221792 16 static const uint8_t UUID_BLUEPROPO_STICK[] =
lipoyang 5:572c91221792 17 { 0x74, 0x25, 0xfb, 0xa0, 0x72, 0x15, 0x41, 0x36, 0xaa, 0x3f, 0x07, 0x2a, 0xa0, 0x7d, 0x93, 0x54 };
jksoft 0:8c643bfe55b7 18
lipoyang 5:572c91221792 19 // BluePropo::Stick data structure
lipoyang 5:572c91221792 20 union StickData
lipoyang 5:572c91221792 21 {
lipoyang 5:572c91221792 22 struct {
lipoyang 5:572c91221792 23 // F(-128)<- 0 ->B(+127)
lipoyang 5:572c91221792 24 signed char fb;
lipoyang 5:572c91221792 25 // L(-128)<- 0 ->R(+127)
lipoyang 5:572c91221792 26 signed char lr;
lipoyang 5:572c91221792 27 }value;
lipoyang 5:572c91221792 28 unsigned char bytes[2];
lipoyang 5:572c91221792 29 };
lipoyang 5:572c91221792 30 StickData stickData;
jksoft 1:48f6e08a3ac2 31
lipoyang 5:572c91221792 32 // buffer for BluePropo payload
lipoyang 5:572c91221792 33 uint8_t payload[10] = {0,};
lipoyang 5:572c91221792 34
lipoyang 5:572c91221792 35 // BluePropo::Stick characteristic
lipoyang 5:572c91221792 36 GattCharacteristic charStick (UUID_BLUEPROPO_STICK, payload, 2, 2,
jksoft 0:8c643bfe55b7 37 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE |
jksoft 0:8c643bfe55b7 38 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
lipoyang 5:572c91221792 39 // BluePropo characteristics set
lipoyang 5:572c91221792 40 GattCharacteristic *chars[] = {&charStick};
lipoyang 5:572c91221792 41 // BluePropo service
lipoyang 5:572c91221792 42 GattService serviceBluePropo(UUID_BLUEPROPO, chars, sizeof(chars) / sizeof(GattCharacteristic *));
lipoyang 5:572c91221792 43
lipoyang 5:572c91221792 44 // USB COM port for Debug
lipoyang 5:572c91221792 45 Serial pc(USBTX, USBRX);
lipoyang 5:572c91221792 46
lipoyang 5:572c91221792 47 // pin asign
lipoyang 5:572c91221792 48 DigitalOut tb6612_ain1(P0_28);
lipoyang 5:572c91221792 49 DigitalOut tb6612_ain2(P0_29);
lipoyang 7:6115339996eb 50 PwmOut tb6612_pwma(P0_30);
lipoyang 7:6115339996eb 51 PwmOut servo_pwm (P0_12);
jksoft 0:8c643bfe55b7 52
lipoyang 5:572c91221792 53 // DC motor driver (TB6612)
lipoyang 5:572c91221792 54 void motor (float speed)
lipoyang 5:572c91221792 55 {
lipoyang 5:572c91221792 56 if (speed > 0) {
lipoyang 5:572c91221792 57 // CW
lipoyang 7:6115339996eb 58 tb6612_pwma = speed;
lipoyang 5:572c91221792 59 tb6612_ain1 = 1;
lipoyang 5:572c91221792 60 tb6612_ain2 = 0;
lipoyang 5:572c91221792 61 } else
lipoyang 5:572c91221792 62 if (speed < 0) {
lipoyang 5:572c91221792 63 // CCW
lipoyang 7:6115339996eb 64 tb6612_pwma = - speed;
lipoyang 5:572c91221792 65 tb6612_ain1 = 0;
lipoyang 5:572c91221792 66 tb6612_ain2 = 1;
lipoyang 5:572c91221792 67 } else {
lipoyang 5:572c91221792 68 // stop
lipoyang 7:6115339996eb 69 tb6612_pwma = 1;
lipoyang 5:572c91221792 70 tb6612_ain1 = 0;
lipoyang 5:572c91221792 71 tb6612_ain2 = 0;
lipoyang 5:572c91221792 72 // // break
lipoyang 5:572c91221792 73 // tb6612_pwma = 1;
lipoyang 5:572c91221792 74 // tb6612_ain1 = 1;
lipoyang 5:572c91221792 75 // tb6612_ain2 = 1;
lipoyang 5:572c91221792 76 }
lipoyang 5:572c91221792 77 }
jksoft 0:8c643bfe55b7 78
lipoyang 5:572c91221792 79 // RC servo
lipoyang 5:572c91221792 80 void servo (float deg)
lipoyang 5:572c91221792 81 {
lipoyang 5:572c91221792 82 servo_pwm.pulsewidth_us(1500 + (int)(500.0 * deg));
lipoyang 5:572c91221792 83 }
lipoyang 5:572c91221792 84
lipoyang 5:572c91221792 85 // BLE onConnection handler
jksoft 1:48f6e08a3ac2 86 void onConnected(uint16_t h)
jksoft 0:8c643bfe55b7 87 {
jksoft 0:8c643bfe55b7 88 #if DBG
jksoft 1:48f6e08a3ac2 89 pc.printf("Connected\n\r");
jksoft 0:8c643bfe55b7 90 #endif
jksoft 1:48f6e08a3ac2 91 }
jksoft 0:8c643bfe55b7 92
lipoyang 5:572c91221792 93 // BLE onDisconnection handler
jksoft 1:48f6e08a3ac2 94 void onDisconnected(uint16_t h)
jksoft 1:48f6e08a3ac2 95 {
jksoft 1:48f6e08a3ac2 96 ble.startAdvertising();
jksoft 0:8c643bfe55b7 97 #if DBG
jksoft 1:48f6e08a3ac2 98 pc.printf("Disconnected\n\r");
jksoft 0:8c643bfe55b7 99 #endif
jksoft 1:48f6e08a3ac2 100 }
jksoft 1:48f6e08a3ac2 101
lipoyang 5:572c91221792 102 // BLE onDataWritten handler (Gatt event)
jksoft 1:48f6e08a3ac2 103 void onDataWritten(uint16_t charHandle)
jksoft 0:8c643bfe55b7 104 {
lipoyang 5:572c91221792 105 if (charHandle == charStick.getHandle()) {
jksoft 1:48f6e08a3ac2 106 uint16_t bytesRead;
lipoyang 5:572c91221792 107 ble.readCharacteristicValue(charStick.getHandle(),payload, &bytesRead);
lipoyang 5:572c91221792 108 memcpy( &stickData.bytes[0], payload, sizeof(stickData));
jksoft 0:8c643bfe55b7 109 #if DBG
jksoft 1:48f6e08a3ac2 110
lipoyang 5:572c91221792 111 pc.printf("DATA:%02X %02X\n\r",stickData.bytes[0],stickData.bytes[1]);
jksoft 0:8c643bfe55b7 112 #endif
lipoyang 5:572c91221792 113 float m = (float)stickData.value.fb / 128.0;
lipoyang 5:572c91221792 114 motor(m);
lipoyang 7:6115339996eb 115 float s = (float)stickData.value.lr / 128.0;
lipoyang 7:6115339996eb 116 servo(s);
jksoft 1:48f6e08a3ac2 117 }
jksoft 1:48f6e08a3ac2 118 }
jksoft 0:8c643bfe55b7 119
lipoyang 5:572c91221792 120 // Program entry point
jksoft 0:8c643bfe55b7 121 int main(void)
jksoft 0:8c643bfe55b7 122 {
jksoft 0:8c643bfe55b7 123 #if DBG
jksoft 0:8c643bfe55b7 124 pc.printf("Start\n\r");
jksoft 0:8c643bfe55b7 125 #endif
lipoyang 5:572c91221792 126 // initialize servo & motor
lipoyang 5:572c91221792 127 servo_pwm.period_ms(20);
lipoyang 5:572c91221792 128 servo(0.5);
lipoyang 5:572c91221792 129 motor(0);
jksoft 1:48f6e08a3ac2 130
lipoyang 5:572c91221792 131 // initialize BLE
jksoft 2:dd85fdc18224 132 ble.init();
jksoft 1:48f6e08a3ac2 133 ble.onConnection(onConnected);
jksoft 1:48f6e08a3ac2 134 ble.onDisconnection(onDisconnected);
jksoft 1:48f6e08a3ac2 135 ble.onDataWritten(onDataWritten);
lipoyang 5:572c91221792 136 // setup advertising
jksoft 1:48f6e08a3ac2 137 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
jksoft 1:48f6e08a3ac2 138 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
jksoft 1:48f6e08a3ac2 139 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
lipoyang 5:572c91221792 140 (const uint8_t *)"MiniSteer HRM1017", sizeof("MiniSteer HRM1017") - 1);
jksoft 1:48f6e08a3ac2 141 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
lipoyang 5:572c91221792 142 (const uint8_t *)UUID_BLUEPROPO, sizeof(UUID_BLUEPROPO));
jksoft 1:48f6e08a3ac2 143 ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
jksoft 1:48f6e08a3ac2 144 ble.startAdvertising();
lipoyang 7:6115339996eb 145 ble.addService(serviceBluePropo);
jksoft 0:8c643bfe55b7 146
lipoyang 5:572c91221792 147 // main loop (wait for BLE event)
jksoft 1:48f6e08a3ac2 148 while (true) {
lipoyang 7:6115339996eb 149 ble.waitForEvent();
jksoft 0:8c643bfe55b7 150 }
jksoft 0:8c643bfe55b7 151 }