【これは旧バージョンです】 AndroidのBLEラジコンプロポアプリ「BLEPropo」と接続し、RCサーボとDCモータを制御するプログラムです。 mbed HRM1017で動作を確認しています。 BLEPropo → https://github.com/lipoyang/BLEPropo
Fork of BLE_RCBController2 by
BLEを使ったAndroid用ラジコンプロポアプリ「BLEPropo」に対応するmbed HRM1017用ファームウェアです。
BLEPropoは、GitHubにて公開中。
https://github.com/lipoyang/BLEPropo
ラジコンは、mbed HRM1017とRCサーボやDCモータを組み合わせて作ります。
main.cpp@13:2075c4878557, 2014-09-18 (annotated)
- Committer:
- lipoyang
- Date:
- Thu Sep 18 04:47:00 2014 +0000
- Revision:
- 13:2075c4878557
- Parent:
- 12:35292b16b401
Servo????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lipoyang | 9:ed80cfb10df8 | 1 | /* |
lipoyang | 9:ed80cfb10df8 | 2 | * This repo was forked from jksoft/BLE_RCBController2. |
lipoyang | 9:ed80cfb10df8 | 3 | * The copyrights of the original repo belongs to Junichi Katsu. |
lipoyang | 9:ed80cfb10df8 | 4 | */ |
lipoyang | 9:ed80cfb10df8 | 5 | |
lipoyang | 9:ed80cfb10df8 | 6 | /* |
lipoyang | 9:ed80cfb10df8 | 7 | * Copyright (C) 2014 Bizan Nishimura (@lipoyang) |
lipoyang | 9:ed80cfb10df8 | 8 | * |
lipoyang | 9:ed80cfb10df8 | 9 | * Licensed under the Apache License, Version 2.0 (the "License"); |
lipoyang | 9:ed80cfb10df8 | 10 | * you may not use this file except in compliance with the License. |
lipoyang | 9:ed80cfb10df8 | 11 | * You may obtain a copy of the License at |
lipoyang | 9:ed80cfb10df8 | 12 | * |
lipoyang | 9:ed80cfb10df8 | 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
lipoyang | 9:ed80cfb10df8 | 14 | * |
lipoyang | 9:ed80cfb10df8 | 15 | * Unless required by applicable law or agreed to in writing, software |
lipoyang | 9:ed80cfb10df8 | 16 | * distributed under the License is distributed on an "AS IS" BASIS, |
lipoyang | 9:ed80cfb10df8 | 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
lipoyang | 9:ed80cfb10df8 | 18 | * See the License for the specific language governing permissions and |
lipoyang | 9:ed80cfb10df8 | 19 | * limitations under the License. |
lipoyang | 9:ed80cfb10df8 | 20 | */ |
lipoyang | 9:ed80cfb10df8 | 21 | |
lipoyang | 9:ed80cfb10df8 | 22 | /* |
lipoyang | 9:ed80cfb10df8 | 23 | * Library Dependencies: |
lipoyang | 12:35292b16b401 | 24 | * - BLE_API (revision 111) |
lipoyang | 9:ed80cfb10df8 | 25 | * - HRM1017 |
lipoyang | 9:ed80cfb10df8 | 26 | * - HRM1017/projectconfig.h |
lipoyang | 9:ed80cfb10df8 | 27 | * connection interval constants are modified. (No need?) |
lipoyang | 9:ed80cfb10df8 | 28 | * CFG_GAP_CONNECTION_MIN_INTERVAL_MS 30 |
lipoyang | 9:ed80cfb10df8 | 29 | * CFG_GAP_CONNECTION_MAX_INTERVAL_MS 50 |
lipoyang | 9:ed80cfb10df8 | 30 | */ |
lipoyang | 9:ed80cfb10df8 | 31 | |
jksoft | 0:8c643bfe55b7 | 32 | #include "mbed.h" |
jksoft | 1:48f6e08a3ac2 | 33 | #include "BLEDevice.h" |
jksoft | 0:8c643bfe55b7 | 34 | |
jksoft | 1:48f6e08a3ac2 | 35 | #define DBG 1 |
jksoft | 0:8c643bfe55b7 | 36 | |
lipoyang | 5:572c91221792 | 37 | // BLE device object |
jksoft | 1:48f6e08a3ac2 | 38 | BLEDevice ble; |
jksoft | 0:8c643bfe55b7 | 39 | |
lipoyang | 5:572c91221792 | 40 | // BluePropo service UUID |
lipoyang | 5:572c91221792 | 41 | //static const uint16_t UUID_BLUEPROPO = 0xFFF0; |
lipoyang | 5:572c91221792 | 42 | static const uint8_t UUID_BLUEPROPO[] = |
lipoyang | 5:572c91221792 | 43 | { 0xc4, 0x9d, 0xfd, 0x1b, 0x86, 0x04, 0x41, 0xd2, 0x89, 0x43, 0x13, 0x6f, 0x21, 0x4d, 0xd0, 0xbf }; |
lipoyang | 5:572c91221792 | 44 | |
lipoyang | 5:572c91221792 | 45 | // BluePropo::Stick characteristic UUID |
lipoyang | 5:572c91221792 | 46 | //static const uint16_t UUID_BLUEPROPO_STICK = 0xFFF1; |
lipoyang | 5:572c91221792 | 47 | static const uint8_t UUID_BLUEPROPO_STICK[] = |
lipoyang | 5:572c91221792 | 48 | { 0x74, 0x25, 0xfb, 0xa0, 0x72, 0x15, 0x41, 0x36, 0xaa, 0x3f, 0x07, 0x2a, 0xa0, 0x7d, 0x93, 0x54 }; |
jksoft | 0:8c643bfe55b7 | 49 | |
lipoyang | 9:ed80cfb10df8 | 50 | // Device Name (for display) |
lipoyang | 9:ed80cfb10df8 | 51 | #define DEVICE_NAME "MiniSteer HRM1017" |
lipoyang | 9:ed80cfb10df8 | 52 | |
lipoyang | 5:572c91221792 | 53 | // BluePropo::Stick data structure |
lipoyang | 5:572c91221792 | 54 | union StickData |
lipoyang | 5:572c91221792 | 55 | { |
lipoyang | 5:572c91221792 | 56 | struct { |
lipoyang | 5:572c91221792 | 57 | // F(-128)<- 0 ->B(+127) |
lipoyang | 5:572c91221792 | 58 | signed char fb; |
lipoyang | 5:572c91221792 | 59 | // L(-128)<- 0 ->R(+127) |
lipoyang | 5:572c91221792 | 60 | signed char lr; |
lipoyang | 5:572c91221792 | 61 | }value; |
lipoyang | 5:572c91221792 | 62 | unsigned char bytes[2]; |
lipoyang | 5:572c91221792 | 63 | }; |
lipoyang | 5:572c91221792 | 64 | StickData stickData; |
jksoft | 1:48f6e08a3ac2 | 65 | |
lipoyang | 5:572c91221792 | 66 | // buffer for BluePropo payload |
lipoyang | 5:572c91221792 | 67 | uint8_t payload[10] = {0,}; |
lipoyang | 5:572c91221792 | 68 | |
lipoyang | 5:572c91221792 | 69 | // BluePropo::Stick characteristic |
lipoyang | 5:572c91221792 | 70 | GattCharacteristic charStick (UUID_BLUEPROPO_STICK, payload, 2, 2, |
jksoft | 0:8c643bfe55b7 | 71 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | |
jksoft | 0:8c643bfe55b7 | 72 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
lipoyang | 5:572c91221792 | 73 | // BluePropo characteristics set |
lipoyang | 5:572c91221792 | 74 | GattCharacteristic *chars[] = {&charStick}; |
lipoyang | 5:572c91221792 | 75 | // BluePropo service |
lipoyang | 5:572c91221792 | 76 | GattService serviceBluePropo(UUID_BLUEPROPO, chars, sizeof(chars) / sizeof(GattCharacteristic *)); |
lipoyang | 5:572c91221792 | 77 | |
lipoyang | 5:572c91221792 | 78 | // USB COM port for Debug |
lipoyang | 5:572c91221792 | 79 | Serial pc(USBTX, USBRX); |
lipoyang | 5:572c91221792 | 80 | |
lipoyang | 5:572c91221792 | 81 | // pin asign |
lipoyang | 5:572c91221792 | 82 | DigitalOut tb6612_ain1(P0_28); |
lipoyang | 5:572c91221792 | 83 | DigitalOut tb6612_ain2(P0_29); |
lipoyang | 7:6115339996eb | 84 | PwmOut tb6612_pwma(P0_30); |
lipoyang | 7:6115339996eb | 85 | PwmOut servo_pwm (P0_12); |
jksoft | 0:8c643bfe55b7 | 86 | |
lipoyang | 5:572c91221792 | 87 | // DC motor driver (TB6612) |
lipoyang | 5:572c91221792 | 88 | void motor (float speed) |
lipoyang | 5:572c91221792 | 89 | { |
lipoyang | 5:572c91221792 | 90 | if (speed > 0) { |
lipoyang | 5:572c91221792 | 91 | // CW |
lipoyang | 7:6115339996eb | 92 | tb6612_pwma = speed; |
lipoyang | 5:572c91221792 | 93 | tb6612_ain1 = 1; |
lipoyang | 5:572c91221792 | 94 | tb6612_ain2 = 0; |
lipoyang | 5:572c91221792 | 95 | } else |
lipoyang | 5:572c91221792 | 96 | if (speed < 0) { |
lipoyang | 5:572c91221792 | 97 | // CCW |
lipoyang | 7:6115339996eb | 98 | tb6612_pwma = - speed; |
lipoyang | 5:572c91221792 | 99 | tb6612_ain1 = 0; |
lipoyang | 5:572c91221792 | 100 | tb6612_ain2 = 1; |
lipoyang | 5:572c91221792 | 101 | } else { |
lipoyang | 5:572c91221792 | 102 | // stop |
lipoyang | 7:6115339996eb | 103 | tb6612_pwma = 1; |
lipoyang | 5:572c91221792 | 104 | tb6612_ain1 = 0; |
lipoyang | 5:572c91221792 | 105 | tb6612_ain2 = 0; |
lipoyang | 5:572c91221792 | 106 | // // break |
lipoyang | 5:572c91221792 | 107 | // tb6612_pwma = 1; |
lipoyang | 5:572c91221792 | 108 | // tb6612_ain1 = 1; |
lipoyang | 5:572c91221792 | 109 | // tb6612_ain2 = 1; |
lipoyang | 5:572c91221792 | 110 | } |
lipoyang | 5:572c91221792 | 111 | } |
jksoft | 0:8c643bfe55b7 | 112 | |
lipoyang | 5:572c91221792 | 113 | // RC servo |
lipoyang | 5:572c91221792 | 114 | void servo (float deg) |
lipoyang | 5:572c91221792 | 115 | { |
lipoyang | 5:572c91221792 | 116 | servo_pwm.pulsewidth_us(1500 + (int)(500.0 * deg)); |
lipoyang | 5:572c91221792 | 117 | } |
lipoyang | 5:572c91221792 | 118 | |
lipoyang | 5:572c91221792 | 119 | // BLE onConnection handler |
jksoft | 1:48f6e08a3ac2 | 120 | void onConnected(uint16_t h) |
jksoft | 0:8c643bfe55b7 | 121 | { |
jksoft | 0:8c643bfe55b7 | 122 | #if DBG |
jksoft | 1:48f6e08a3ac2 | 123 | pc.printf("Connected\n\r"); |
jksoft | 0:8c643bfe55b7 | 124 | #endif |
jksoft | 1:48f6e08a3ac2 | 125 | } |
jksoft | 0:8c643bfe55b7 | 126 | |
lipoyang | 5:572c91221792 | 127 | // BLE onDisconnection handler |
jksoft | 1:48f6e08a3ac2 | 128 | void onDisconnected(uint16_t h) |
jksoft | 1:48f6e08a3ac2 | 129 | { |
jksoft | 1:48f6e08a3ac2 | 130 | ble.startAdvertising(); |
jksoft | 0:8c643bfe55b7 | 131 | #if DBG |
jksoft | 1:48f6e08a3ac2 | 132 | pc.printf("Disconnected\n\r"); |
jksoft | 0:8c643bfe55b7 | 133 | #endif |
jksoft | 1:48f6e08a3ac2 | 134 | } |
jksoft | 1:48f6e08a3ac2 | 135 | |
lipoyang | 5:572c91221792 | 136 | // BLE onDataWritten handler (Gatt event) |
jksoft | 1:48f6e08a3ac2 | 137 | void onDataWritten(uint16_t charHandle) |
jksoft | 0:8c643bfe55b7 | 138 | { |
lipoyang | 5:572c91221792 | 139 | if (charHandle == charStick.getHandle()) { |
jksoft | 1:48f6e08a3ac2 | 140 | uint16_t bytesRead; |
lipoyang | 5:572c91221792 | 141 | ble.readCharacteristicValue(charStick.getHandle(),payload, &bytesRead); |
lipoyang | 5:572c91221792 | 142 | memcpy( &stickData.bytes[0], payload, sizeof(stickData)); |
jksoft | 0:8c643bfe55b7 | 143 | #if DBG |
jksoft | 1:48f6e08a3ac2 | 144 | |
lipoyang | 5:572c91221792 | 145 | pc.printf("DATA:%02X %02X\n\r",stickData.bytes[0],stickData.bytes[1]); |
jksoft | 0:8c643bfe55b7 | 146 | #endif |
lipoyang | 5:572c91221792 | 147 | float m = (float)stickData.value.fb / 128.0; |
lipoyang | 5:572c91221792 | 148 | motor(m); |
lipoyang | 7:6115339996eb | 149 | float s = (float)stickData.value.lr / 128.0; |
lipoyang | 7:6115339996eb | 150 | servo(s); |
jksoft | 1:48f6e08a3ac2 | 151 | } |
jksoft | 1:48f6e08a3ac2 | 152 | } |
jksoft | 0:8c643bfe55b7 | 153 | |
lipoyang | 5:572c91221792 | 154 | // Program entry point |
jksoft | 0:8c643bfe55b7 | 155 | int main(void) |
jksoft | 0:8c643bfe55b7 | 156 | { |
jksoft | 0:8c643bfe55b7 | 157 | #if DBG |
jksoft | 0:8c643bfe55b7 | 158 | pc.printf("Start\n\r"); |
jksoft | 0:8c643bfe55b7 | 159 | #endif |
lipoyang | 5:572c91221792 | 160 | // initialize servo & motor |
lipoyang | 5:572c91221792 | 161 | servo_pwm.period_ms(20); |
lipoyang | 5:572c91221792 | 162 | servo(0.5); |
lipoyang | 5:572c91221792 | 163 | motor(0); |
jksoft | 1:48f6e08a3ac2 | 164 | |
lipoyang | 5:572c91221792 | 165 | // initialize BLE |
jksoft | 2:dd85fdc18224 | 166 | ble.init(); |
jksoft | 1:48f6e08a3ac2 | 167 | ble.onConnection(onConnected); |
jksoft | 1:48f6e08a3ac2 | 168 | ble.onDisconnection(onDisconnected); |
jksoft | 1:48f6e08a3ac2 | 169 | ble.onDataWritten(onDataWritten); |
lipoyang | 5:572c91221792 | 170 | // setup advertising |
jksoft | 1:48f6e08a3ac2 | 171 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
jksoft | 1:48f6e08a3ac2 | 172 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
jksoft | 1:48f6e08a3ac2 | 173 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
lipoyang | 9:ed80cfb10df8 | 174 | (const uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME) - 1); |
jksoft | 1:48f6e08a3ac2 | 175 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, |
lipoyang | 5:572c91221792 | 176 | (const uint8_t *)UUID_BLUEPROPO, sizeof(UUID_BLUEPROPO)); |
jksoft | 1:48f6e08a3ac2 | 177 | ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ |
jksoft | 1:48f6e08a3ac2 | 178 | ble.startAdvertising(); |
lipoyang | 7:6115339996eb | 179 | ble.addService(serviceBluePropo); |
jksoft | 0:8c643bfe55b7 | 180 | |
lipoyang | 5:572c91221792 | 181 | // main loop (wait for BLE event) |
jksoft | 1:48f6e08a3ac2 | 182 | while (true) { |
lipoyang | 7:6115339996eb | 183 | ble.waitForEvent(); |
jksoft | 0:8c643bfe55b7 | 184 | } |
jksoft | 0:8c643bfe55b7 | 185 | } |