Bizan Nishimura / Mbed 2 deprecated MiniSteer2

Dependencies:   BLE_API mbed

Fork of BLE_RCBController2 by Junichi Katsu

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * This repo was forked from jksoft/BLE_RCBController2.
00003  * The copyrights of the original repo belongs to Junichi Katsu.
00004  */
00005 
00006 /*
00007  * Copyright (C) 2014 Bizan Nishimura (@lipoyang)
00008  *
00009  * Licensed under the Apache License, Version 2.0 (the "License");
00010  * you may not use this file except in compliance with the License.
00011  * You may obtain a copy of the License at
00012  *
00013  *     http://www.apache.org/licenses/LICENSE-2.0
00014  *
00015  * Unless required by applicable law or agreed to in writing, software
00016  * distributed under the License is distributed on an "AS IS" BASIS,
00017  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00018  * See the License for the specific language governing permissions and
00019  * limitations under the License.
00020  */
00021 
00022 #include "mbed.h"
00023 #include "BLEDevice.h"
00024 
00025 #define DBG 1
00026 
00027 // BLE device object
00028 BLEDevice  ble;
00029 
00030 // BluePropo service UUID
00031 //static const uint16_t UUID_BLUEPROPO = 0xFFF0;
00032 static const uint8_t UUID_BLUEPROPO[] = 
00033 { 0xc4, 0x9d, 0xfd, 0x1b, 0x86, 0x04, 0x41, 0xd2, 0x89, 0x43, 0x13, 0x6f, 0x21, 0x4d, 0xd0, 0xbf };
00034 
00035 // BluePropo::Stick characteristic UUID
00036 //static const uint16_t UUID_BLUEPROPO_STICK = 0xFFF1;
00037 static const uint8_t UUID_BLUEPROPO_STICK[] =
00038 { 0x74, 0x25, 0xfb, 0xa0, 0x72, 0x15, 0x41, 0x36, 0xaa, 0x3f, 0x07, 0x2a, 0xa0, 0x7d, 0x93, 0x54 };
00039 
00040 // Device Name (for display)
00041 #define DEVICE_NAME "MiniSteer HRM1017"
00042 
00043 // BluePropo::Stick data structure
00044 union StickData
00045 {
00046     struct  {
00047         // F(-128)<- 0 ->B(+127)
00048         signed char fb;
00049         // L(-128)<- 0 ->R(+127)
00050         signed char lr;
00051     }value;
00052     unsigned char bytes[2];
00053 };
00054 StickData stickData;
00055 
00056 // buffer for BluePropo payload
00057 uint8_t payload[10] = {0,};
00058 
00059 // BluePropo::Stick characteristic
00060 GattCharacteristic  charStick (UUID_BLUEPROPO_STICK, payload, 2, 2,
00061                                 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | 
00062                                 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
00063 // BluePropo characteristics set
00064 GattCharacteristic *chars[] = {&charStick};
00065 // BluePropo service
00066 GattService         serviceBluePropo(UUID_BLUEPROPO, chars, sizeof(chars) / sizeof(GattCharacteristic *));
00067 
00068 // USB COM port for Debug
00069 Serial  pc(USBTX, USBRX);
00070 
00071 // pin asign
00072 DigitalOut tb6612_ain1(P0_28);
00073 DigitalOut tb6612_ain2(P0_29);
00074 PwmOut     tb6612_pwma(P0_30);
00075 PwmOut     servo_pwm  (P0_12);
00076 
00077 // DC motor driver (TB6612)
00078 void motor (float speed)
00079 {
00080     if (speed > 0) {
00081         // CW
00082         tb6612_pwma = speed;
00083         tb6612_ain1 = 1;
00084         tb6612_ain2 = 0;
00085     } else
00086     if (speed < 0) {
00087         // CCW
00088         tb6612_pwma = - speed;
00089         tb6612_ain1 = 0;
00090         tb6612_ain2 = 1;
00091     } else {
00092         // stop
00093         tb6612_pwma = 1;
00094         tb6612_ain1 = 0;
00095         tb6612_ain2 = 0;
00096     }
00097 }
00098 
00099 void motor_break()
00100 {
00101     // break
00102     tb6612_pwma = 1;
00103     tb6612_ain1 = 1;
00104     tb6612_ain2 = 1;
00105 }
00106 
00107 // you must adjust center position
00108 #define RL_ADJ      (0)
00109 #define RL_CENTER   (1500)
00110 #define RL_RANGE    (500.0)
00111 
00112 // RC servo
00113 // rl : -1.0 ? 1.0
00114 void servo (float rl)
00115 {
00116     servo_pwm.pulsewidth_us(RL_CENTER + RL_ADJ + (int)(RL_RANGE * rl));
00117 }
00118 
00119 // BLE onConnection handler
00120 void onConnected(uint16_t h)
00121 {
00122 #if DBG
00123     pc.printf("Connected\n\r");
00124 #endif
00125 }
00126 
00127 // BLE onDisconnection handler
00128 void onDisconnected(uint16_t h)
00129 {
00130     ble.startAdvertising();
00131 #if DBG
00132     pc.printf("Disconnected\n\r");
00133 #endif
00134 }
00135 
00136 // BLE onDataWritten handler (Gatt event)
00137 void onDataWritten(uint16_t charHandle)
00138 {
00139     if (charHandle == charStick.getHandle()) {
00140         uint16_t bytesRead;
00141         ble.readCharacteristicValue(charStick.getHandle(),payload, &bytesRead);
00142         memcpy( &stickData.bytes[0], payload, sizeof(stickData));
00143 #if DBG
00144 
00145         pc.printf("DATA:%02X %02X\n\r",stickData.bytes[0],stickData.bytes[1]);
00146 #endif
00147         float m = (float)stickData.value.fb / 128.0;
00148         motor(m);
00149         float s = (float)stickData.value.lr / 128.0;
00150         servo(s);
00151     }
00152 }
00153 
00154 // Program entry point
00155 int main(void)
00156 {
00157 #if DBG
00158         pc.printf("Start\n\r");
00159 #endif
00160     // initialize servo & motor
00161     servo_pwm.period_ms(20);
00162     servo(0);
00163     motor(0);
00164     
00165     // initialize BLE
00166     ble.init(); 
00167     ble.onConnection(onConnected);
00168     ble.onDisconnection(onDisconnected);
00169     ble.onDataWritten(onDataWritten);
00170     // setup advertising
00171     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
00172     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00173     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
00174                                     (const uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME) - 1);
00175     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
00176                                     (const uint8_t *)UUID_BLUEPROPO, sizeof(UUID_BLUEPROPO));
00177     ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
00178     ble.startAdvertising();
00179     ble.addService(serviceBluePropo);
00180 
00181     // main loop (wait for BLE event)
00182     while (true) {
00183         ble.waitForEvent();
00184     }
00185 }