Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of BLE_RCBController2 by
main.cpp@5:85c6be3d9008, 2015-01-30 (annotated)
- Committer:
- lipoyang
- Date:
- Fri Jan 30 02:41:31 2015 +0000
- Revision:
- 5:85c6be3d9008
- Parent:
- 2:dd85fdc18224
- Child:
- 7:505a9a98b776
fork
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lipoyang | 5:85c6be3d9008 | 1 | /* |
lipoyang | 5:85c6be3d9008 | 2 | * This repo was forked from jksoft/BLE_RCBController2. |
lipoyang | 5:85c6be3d9008 | 3 | * The copyrights of the original repo belongs to Junichi Katsu. |
lipoyang | 5:85c6be3d9008 | 4 | */ |
lipoyang | 5:85c6be3d9008 | 5 | |
lipoyang | 5:85c6be3d9008 | 6 | /* |
lipoyang | 5:85c6be3d9008 | 7 | * Copyright (C) 2014 Bizan Nishimura (@lipoyang) |
lipoyang | 5:85c6be3d9008 | 8 | * |
lipoyang | 5:85c6be3d9008 | 9 | * Licensed under the Apache License, Version 2.0 (the "License"); |
lipoyang | 5:85c6be3d9008 | 10 | * you may not use this file except in compliance with the License. |
lipoyang | 5:85c6be3d9008 | 11 | * You may obtain a copy of the License at |
lipoyang | 5:85c6be3d9008 | 12 | * |
lipoyang | 5:85c6be3d9008 | 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
lipoyang | 5:85c6be3d9008 | 14 | * |
lipoyang | 5:85c6be3d9008 | 15 | * Unless required by applicable law or agreed to in writing, software |
lipoyang | 5:85c6be3d9008 | 16 | * distributed under the License is distributed on an "AS IS" BASIS, |
lipoyang | 5:85c6be3d9008 | 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
lipoyang | 5:85c6be3d9008 | 18 | * See the License for the specific language governing permissions and |
lipoyang | 5:85c6be3d9008 | 19 | * limitations under the License. |
lipoyang | 5:85c6be3d9008 | 20 | */ |
lipoyang | 5:85c6be3d9008 | 21 | |
lipoyang | 5:85c6be3d9008 | 22 | /* |
lipoyang | 5:85c6be3d9008 | 23 | * Library Dependencies: |
lipoyang | 5:85c6be3d9008 | 24 | * - BLE_API (revision 111) |
lipoyang | 5:85c6be3d9008 | 25 | * - HRM1017 |
lipoyang | 5:85c6be3d9008 | 26 | * - HRM1017/projectconfig.h |
lipoyang | 5:85c6be3d9008 | 27 | * connection interval constants are modified. (No need?) |
lipoyang | 5:85c6be3d9008 | 28 | * CFG_GAP_CONNECTION_MIN_INTERVAL_MS 30 |
lipoyang | 5:85c6be3d9008 | 29 | * CFG_GAP_CONNECTION_MAX_INTERVAL_MS 50 |
lipoyang | 5:85c6be3d9008 | 30 | */ |
lipoyang | 5:85c6be3d9008 | 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:85c6be3d9008 | 37 | // BLE device object |
jksoft | 1:48f6e08a3ac2 | 38 | BLEDevice ble; |
lipoyang | 5:85c6be3d9008 | 39 | |
lipoyang | 5:85c6be3d9008 | 40 | // BluePropo service UUID |
lipoyang | 5:85c6be3d9008 | 41 | //static const uint16_t UUID_BLUEPROPO = 0xFFF0; |
lipoyang | 5:85c6be3d9008 | 42 | static const uint8_t UUID_BLUEPROPO[] = |
lipoyang | 5:85c6be3d9008 | 43 | { 0xc4, 0x9d, 0xfd, 0x1b, 0x86, 0x04, 0x41, 0xd2, 0x89, 0x43, 0x13, 0x6f, 0x21, 0x4d, 0xd0, 0xbf }; |
jksoft | 0:8c643bfe55b7 | 44 | |
lipoyang | 5:85c6be3d9008 | 45 | // BluePropo::Stick characteristic UUID |
lipoyang | 5:85c6be3d9008 | 46 | //static const uint16_t UUID_BLUEPROPO_STICK = 0xFFF1; |
lipoyang | 5:85c6be3d9008 | 47 | static const uint8_t UUID_BLUEPROPO_STICK[] = |
lipoyang | 5:85c6be3d9008 | 48 | { 0x74, 0x25, 0xfb, 0xa0, 0x72, 0x15, 0x41, 0x36, 0xaa, 0x3f, 0x07, 0x2a, 0xa0, 0x7d, 0x93, 0x54 }; |
lipoyang | 5:85c6be3d9008 | 49 | |
lipoyang | 5:85c6be3d9008 | 50 | // Device Name (for display) |
lipoyang | 5:85c6be3d9008 | 51 | #define DEVICE_NAME "MiniSteer HRM1017" |
jksoft | 0:8c643bfe55b7 | 52 | |
lipoyang | 5:85c6be3d9008 | 53 | // BluePropo::Stick data structure |
lipoyang | 5:85c6be3d9008 | 54 | union StickData |
lipoyang | 5:85c6be3d9008 | 55 | { |
lipoyang | 5:85c6be3d9008 | 56 | struct { |
lipoyang | 5:85c6be3d9008 | 57 | // F(-128)<- 0 ->B(+127) |
lipoyang | 5:85c6be3d9008 | 58 | signed char fb; |
lipoyang | 5:85c6be3d9008 | 59 | // L(-128)<- 0 ->R(+127) |
lipoyang | 5:85c6be3d9008 | 60 | signed char lr; |
lipoyang | 5:85c6be3d9008 | 61 | }value; |
lipoyang | 5:85c6be3d9008 | 62 | unsigned char bytes[2]; |
lipoyang | 5:85c6be3d9008 | 63 | }; |
lipoyang | 5:85c6be3d9008 | 64 | StickData stickData; |
jksoft | 1:48f6e08a3ac2 | 65 | |
lipoyang | 5:85c6be3d9008 | 66 | // buffer for BluePropo payload |
lipoyang | 5:85c6be3d9008 | 67 | uint8_t payload[10] = {0,}; |
lipoyang | 5:85c6be3d9008 | 68 | |
lipoyang | 5:85c6be3d9008 | 69 | // BluePropo::Stick characteristic |
lipoyang | 5:85c6be3d9008 | 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:85c6be3d9008 | 73 | // BluePropo characteristics set |
lipoyang | 5:85c6be3d9008 | 74 | GattCharacteristic *chars[] = {&charStick}; |
lipoyang | 5:85c6be3d9008 | 75 | // BluePropo service |
lipoyang | 5:85c6be3d9008 | 76 | GattService serviceBluePropo(UUID_BLUEPROPO, chars, sizeof(chars) / sizeof(GattCharacteristic *)); |
lipoyang | 5:85c6be3d9008 | 77 | |
lipoyang | 5:85c6be3d9008 | 78 | // USB COM port for Debug |
lipoyang | 5:85c6be3d9008 | 79 | Serial pc(USBTX, USBRX); |
lipoyang | 5:85c6be3d9008 | 80 | |
lipoyang | 5:85c6be3d9008 | 81 | // pin asign |
lipoyang | 5:85c6be3d9008 | 82 | DigitalOut tb6612_ain1(P0_28); |
lipoyang | 5:85c6be3d9008 | 83 | DigitalOut tb6612_ain2(P0_29); |
lipoyang | 5:85c6be3d9008 | 84 | PwmOut tb6612_pwma(P0_30); |
lipoyang | 5:85c6be3d9008 | 85 | PwmOut servo_pwm (P0_12); |
jksoft | 0:8c643bfe55b7 | 86 | |
lipoyang | 5:85c6be3d9008 | 87 | // DC motor driver (TB6612) |
lipoyang | 5:85c6be3d9008 | 88 | void motor (float speed) |
lipoyang | 5:85c6be3d9008 | 89 | { |
lipoyang | 5:85c6be3d9008 | 90 | if (speed > 0) { |
lipoyang | 5:85c6be3d9008 | 91 | // CW |
lipoyang | 5:85c6be3d9008 | 92 | tb6612_pwma = speed; |
lipoyang | 5:85c6be3d9008 | 93 | tb6612_ain1 = 1; |
lipoyang | 5:85c6be3d9008 | 94 | tb6612_ain2 = 0; |
lipoyang | 5:85c6be3d9008 | 95 | } else |
lipoyang | 5:85c6be3d9008 | 96 | if (speed < 0) { |
lipoyang | 5:85c6be3d9008 | 97 | // CCW |
lipoyang | 5:85c6be3d9008 | 98 | tb6612_pwma = - speed; |
lipoyang | 5:85c6be3d9008 | 99 | tb6612_ain1 = 0; |
lipoyang | 5:85c6be3d9008 | 100 | tb6612_ain2 = 1; |
lipoyang | 5:85c6be3d9008 | 101 | } else { |
lipoyang | 5:85c6be3d9008 | 102 | // stop |
lipoyang | 5:85c6be3d9008 | 103 | tb6612_pwma = 1; |
lipoyang | 5:85c6be3d9008 | 104 | tb6612_ain1 = 0; |
lipoyang | 5:85c6be3d9008 | 105 | tb6612_ain2 = 0; |
lipoyang | 5:85c6be3d9008 | 106 | // // break |
lipoyang | 5:85c6be3d9008 | 107 | // tb6612_pwma = 1; |
lipoyang | 5:85c6be3d9008 | 108 | // tb6612_ain1 = 1; |
lipoyang | 5:85c6be3d9008 | 109 | // tb6612_ain2 = 1; |
lipoyang | 5:85c6be3d9008 | 110 | } |
lipoyang | 5:85c6be3d9008 | 111 | } |
jksoft | 0:8c643bfe55b7 | 112 | |
lipoyang | 5:85c6be3d9008 | 113 | // RC servo |
lipoyang | 5:85c6be3d9008 | 114 | void servo (float deg) |
lipoyang | 5:85c6be3d9008 | 115 | { |
lipoyang | 5:85c6be3d9008 | 116 | servo_pwm.pulsewidth_us(1500 + (int)(500.0 * deg)); |
lipoyang | 5:85c6be3d9008 | 117 | } |
lipoyang | 5:85c6be3d9008 | 118 | |
lipoyang | 5:85c6be3d9008 | 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:85c6be3d9008 | 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:85c6be3d9008 | 136 | // BLE onDataWritten handler (Gatt event) |
jksoft | 1:48f6e08a3ac2 | 137 | void onDataWritten(uint16_t charHandle) |
jksoft | 0:8c643bfe55b7 | 138 | { |
lipoyang | 5:85c6be3d9008 | 139 | if (charHandle == charStick.getHandle()) { |
jksoft | 1:48f6e08a3ac2 | 140 | uint16_t bytesRead; |
lipoyang | 5:85c6be3d9008 | 141 | ble.readCharacteristicValue(charStick.getHandle(),payload, &bytesRead); |
lipoyang | 5:85c6be3d9008 | 142 | memcpy( &stickData.bytes[0], payload, sizeof(stickData)); |
jksoft | 0:8c643bfe55b7 | 143 | #if DBG |
jksoft | 1:48f6e08a3ac2 | 144 | |
lipoyang | 5:85c6be3d9008 | 145 | pc.printf("DATA:%02X %02X\n\r",stickData.bytes[0],stickData.bytes[1]); |
jksoft | 0:8c643bfe55b7 | 146 | #endif |
lipoyang | 5:85c6be3d9008 | 147 | float m = (float)stickData.value.fb / 128.0; |
lipoyang | 5:85c6be3d9008 | 148 | motor(m); |
lipoyang | 5:85c6be3d9008 | 149 | float s = (float)stickData.value.lr / 128.0; |
lipoyang | 5:85c6be3d9008 | 150 | servo(s); |
jksoft | 1:48f6e08a3ac2 | 151 | } |
jksoft | 1:48f6e08a3ac2 | 152 | } |
jksoft | 0:8c643bfe55b7 | 153 | |
lipoyang | 5:85c6be3d9008 | 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:85c6be3d9008 | 160 | // initialize servo & motor |
lipoyang | 5:85c6be3d9008 | 161 | servo_pwm.period_ms(20); |
lipoyang | 5:85c6be3d9008 | 162 | servo(0.5); |
lipoyang | 5:85c6be3d9008 | 163 | motor(0); |
jksoft | 1:48f6e08a3ac2 | 164 | |
lipoyang | 5:85c6be3d9008 | 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:85c6be3d9008 | 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 | 5:85c6be3d9008 | 174 | (const uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME) - 1); |
jksoft | 1:48f6e08a3ac2 | 175 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, |
lipoyang | 5:85c6be3d9008 | 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 | 5:85c6be3d9008 | 179 | ble.addService(serviceBluePropo); |
jksoft | 0:8c643bfe55b7 | 180 | |
lipoyang | 5:85c6be3d9008 | 181 | // main loop (wait for BLE event) |
jksoft | 1:48f6e08a3ac2 | 182 | while (true) { |
jksoft | 1:48f6e08a3ac2 | 183 | ble.waitForEvent(); |
jksoft | 0:8c643bfe55b7 | 184 | } |
jksoft | 0:8c643bfe55b7 | 185 | } |