CSSE4011_BLE_IMU IMU Seeed Tiny Ble

Dependencies:   BLE_API_Tiny_BLE MPU6050-DMP-Seeed-Tiny-BLE mbed

Committer:
flywind
Date:
Wed Jun 10 12:56:48 2015 +0000
Revision:
2:44bc61abdf33
Parent:
0:f90c3452d779
milestone working one , yaw pitch roll update in BLE sucessfull

Who changed what in which revision?

UserRevisionLine numberNew contents of line
flywind 0:f90c3452d779 1 /* mbed Microcontroller Library
flywind 0:f90c3452d779 2 * Copyright (c) 2006-2013 ARM Limited
flywind 0:f90c3452d779 3 *
flywind 0:f90c3452d779 4 * Licensed under the Apache License, Version 2.0 (the "License");
flywind 0:f90c3452d779 5 * you may not use this file except in compliance with the License.
flywind 0:f90c3452d779 6 * You may obtain a copy of the License at
flywind 0:f90c3452d779 7 *
flywind 0:f90c3452d779 8 * http://www.apache.org/licenses/LICENSE-2.0
flywind 0:f90c3452d779 9 *
flywind 0:f90c3452d779 10 * Unless required by applicable law or agreed to in writing, software
flywind 0:f90c3452d779 11 * distributed under the License is distributed on an "AS IS" BASIS,
flywind 0:f90c3452d779 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
flywind 0:f90c3452d779 13 * See the License for the specific language governing permissions and
flywind 0:f90c3452d779 14 * limitations under the License.
flywind 0:f90c3452d779 15 */
flywind 0:f90c3452d779 16
flywind 0:f90c3452d779 17 #ifndef __BLE_MPU_SERVICE_H__
flywind 0:f90c3452d779 18 #define __BLE_MPU_SERVICE_H__
flywind 0:f90c3452d779 19 #include "mbed.h"
flywind 0:f90c3452d779 20 #include "Stream.h"
flywind 0:f90c3452d779 21 #include "UUID.h"
flywind 0:f90c3452d779 22 #include "BLEDevice.h"
flywind 0:f90c3452d779 23
flywind 0:f90c3452d779 24 extern const uint16_t MPUServiceShortUUID;
flywind 0:f90c3452d779 25 extern const uint16_t MPUServiceYPRCharacteristicShortUUID;
flywind 0:f90c3452d779 26
flywind 0:f90c3452d779 27 extern const uint16_t MPUServiceAccelCharacteristicShortUUID;
flywind 0:f90c3452d779 28 extern const uint16_t MPUServiceQuatCharacteristicShortUUID;
flywind 0:f90c3452d779 29 extern const uint16_t MPUServiceTimeCharacteristicShortUUID;
flywind 0:f90c3452d779 30 extern const uint16_t MPUServiceSettingCharacteristicShortUUID;
flywind 0:f90c3452d779 31
flywind 0:f90c3452d779 32 extern const uint8_t MPUServiceBaseUUID[LENGTH_OF_LONG_UUID];
flywind 0:f90c3452d779 33 extern const uint8_t MPUServiceUUID[LENGTH_OF_LONG_UUID];
flywind 0:f90c3452d779 34 extern const uint8_t MPUServiceUUID_reversed[LENGTH_OF_LONG_UUID];
flywind 0:f90c3452d779 35
flywind 0:f90c3452d779 36 extern const uint8_t MPUServiceYPRCharacteristicUUID[LENGTH_OF_LONG_UUID];
flywind 0:f90c3452d779 37
flywind 0:f90c3452d779 38 extern const uint8_t MPUServiceAccelCharacteristicUUID[LENGTH_OF_LONG_UUID];
flywind 0:f90c3452d779 39 extern const uint8_t MPUServiceQuatCharacteristicUUID[LENGTH_OF_LONG_UUID];
flywind 0:f90c3452d779 40 extern const uint8_t MPUServiceTimeCharacteristicUUID[LENGTH_OF_LONG_UUID];
flywind 0:f90c3452d779 41 extern const uint8_t MPUServiceSettingCharacteristicUUID[LENGTH_OF_LONG_UUID];
flywind 0:f90c3452d779 42 /**
flywind 0:f90c3452d779 43 * @class MPUService
flywind 0:f90c3452d779 44 * @brief BLE Service to enable MPU6050 over BLE
flywind 0:f90c3452d779 45 */
flywind 0:f90c3452d779 46 class MPUService
flywind 0:f90c3452d779 47 {
flywind 0:f90c3452d779 48
flywind 0:f90c3452d779 49 public:
flywind 0:f90c3452d779 50 /**< Maximum length of data (in bytes) that can be transmitted by the UART service module to the peer. */
flywind 0:f90c3452d779 51 static const unsigned GATT_MTU_SIZE_DEFAULT = 23;
flywind 0:f90c3452d779 52 static const unsigned GATT_MTU_SIZE_POSITION = 10;
flywind 0:f90c3452d779 53 static const unsigned BLE_MPU_SERVICE_MAX_DATA_LEN = (GATT_MTU_SIZE_DEFAULT - 3);
flywind 0:f90c3452d779 54 static const unsigned BLE_MPU_SERVICE_MAX_POSITION_DATA_LEN = (GATT_MTU_SIZE_POSITION - 3);
flywind 0:f90c3452d779 55
flywind 0:f90c3452d779 56 public:
flywind 0:f90c3452d779 57
flywind 0:f90c3452d779 58 /**
flywind 0:f90c3452d779 59 * @param[ref] ble
flywind 0:f90c3452d779 60 * BLEDevice object for the underlying controller.
flywind 0:f90c3452d779 61 */
flywind 0:f90c3452d779 62 MPUService(BLEDevice &_ble, float *initialYPRData) :
flywind 0:f90c3452d779 63 ble(_ble),
flywind 0:f90c3452d779 64 receiveBuffer(),
flywind 0:f90c3452d779 65 yprValueBytes(initialYPRData),
flywind 2:44bc61abdf33 66 numBytesReceived(),
flywind 2:44bc61abdf33 67 receiveBufferIndex(),
flywind 0:f90c3452d779 68
flywind 0:f90c3452d779 69 YPRCharacteristic(MPUServiceYPRCharacteristicShortUUID, (YawPitchRollValueBytes *)yprValueBytes.getPointer(),GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
flywind 0:f90c3452d779 70 settingCharacteristic(MPUServiceSettingCharacteristicShortUUID, receiveBuffer, 1, BLE_MPU_SERVICE_MAX_DATA_LEN,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE){
flywind 0:f90c3452d779 71
flywind 0:f90c3452d779 72 static bool serviceAdded = false; /* We should only ever need to add the heart rate service once. */
flywind 0:f90c3452d779 73 if (serviceAdded) {
flywind 0:f90c3452d779 74 return;
flywind 0:f90c3452d779 75 }
flywind 0:f90c3452d779 76
flywind 0:f90c3452d779 77 GattCharacteristic *charTable[] = {&settingCharacteristic, &YPRCharacteristic};
flywind 0:f90c3452d779 78 GattService mpuService(MPUServiceShortUUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
flywind 0:f90c3452d779 79 ble.addService(mpuService);
flywind 0:f90c3452d779 80 serviceAdded = true;
flywind 0:f90c3452d779 81 ble.onDataWritten(this, &MPUService::onDataWritten);
flywind 0:f90c3452d779 82 }
flywind 0:f90c3452d779 83
flywind 0:f90c3452d779 84
flywind 0:f90c3452d779 85
flywind 0:f90c3452d779 86
flywind 0:f90c3452d779 87
flywind 0:f90c3452d779 88 /**
flywind 0:f90c3452d779 89 * Note: TX and RX characteristics are to be interpreted from the viewpoint of the GATT client using this service.
flywind 0:f90c3452d779 90 */
flywind 0:f90c3452d779 91 uint16_t getSettingCharacteristic() {
flywind 0:f90c3452d779 92 return settingCharacteristic.getValueAttribute().getHandle();
flywind 0:f90c3452d779 93 }
flywind 0:f90c3452d779 94
flywind 0:f90c3452d779 95
flywind 0:f90c3452d779 96
flywind 0:f90c3452d779 97
flywind 0:f90c3452d779 98 /**
flywind 0:f90c3452d779 99 * @brief Update the temperature being broadcast
flywind 0:f90c3452d779 100 *
flywind 0:f90c3452d779 101 * @param[in] temperature
flywind 0:f90c3452d779 102 * Floating point value of the temperature
flywind 0:f90c3452d779 103 *
flywind 0:f90c3452d779 104 */
flywind 0:f90c3452d779 105 void updateYawPitchRoll(float *yprData) {
flywind 0:f90c3452d779 106 if (ble.getGapState().connected) {
flywind 0:f90c3452d779 107 yprValueBytes.updateYawPitchRoll(yprData);
flywind 0:f90c3452d779 108 ble.updateCharacteristicValue(YPRCharacteristic.getValueAttribute().getHandle(), yprValueBytes.getPointer(), sizeof(YawPitchRollValueBytes));
flywind 0:f90c3452d779 109 }
flywind 0:f90c3452d779 110 }
flywind 0:f90c3452d779 111
flywind 0:f90c3452d779 112
flywind 0:f90c3452d779 113
flywind 0:f90c3452d779 114
flywind 0:f90c3452d779 115
flywind 0:f90c3452d779 116 private:
flywind 0:f90c3452d779 117 /**
flywind 0:f90c3452d779 118 * This callback allows the UART service to receive updates to the
flywind 0:f90c3452d779 119 * txCharacteristic. The application should forward the call to this
flywind 0:f90c3452d779 120 * function from the global onDataWritten() callback handler; or if that's
flywind 0:f90c3452d779 121 * not used, this method can be used as a callback directly.
flywind 0:f90c3452d779 122 */
flywind 0:f90c3452d779 123 void onDataWritten(const GattCharacteristicWriteCBParams *params) {
flywind 0:f90c3452d779 124 if (params->charHandle == getSettingCharacteristic()) {
flywind 0:f90c3452d779 125 uint16_t bytesRead = params->len;
flywind 0:f90c3452d779 126 if (bytesRead <= BLE_MPU_SERVICE_MAX_DATA_LEN) {
flywind 0:f90c3452d779 127 numBytesReceived = bytesRead;
flywind 0:f90c3452d779 128 receiveBufferIndex = 0;
flywind 0:f90c3452d779 129 memcpy(receiveBuffer, params->data, numBytesReceived);
flywind 0:f90c3452d779 130 }
flywind 0:f90c3452d779 131 }
flywind 0:f90c3452d779 132 }
flywind 0:f90c3452d779 133
flywind 0:f90c3452d779 134
flywind 0:f90c3452d779 135
flywind 0:f90c3452d779 136
flywind 0:f90c3452d779 137 private:
flywind 0:f90c3452d779 138 /* Private internal representation for the bytes used to work with the vaulue of the heart-rate characteristic. */
flywind 0:f90c3452d779 139 struct YawPitchRollValueBytes {
flywind 0:f90c3452d779 140 static const unsigned OFFSET_OF_FLAGS = 0;
flywind 2:44bc61abdf33 141 //static const unsigned OFFSET_OF_VALUE = OFFSET_OF_FLAGS + sizeof(uint8_t);
flywind 2:44bc61abdf33 142 static const unsigned SIZEOF_VALUE_BYTES = 3*sizeof(uint16_t);
flywind 0:f90c3452d779 143
flywind 0:f90c3452d779 144
flywind 0:f90c3452d779 145 YawPitchRollValueBytes(float *yprData) : bytes() {
flywind 0:f90c3452d779 146 /* assumption: temperature values are expressed in Celsius */
flywind 0:f90c3452d779 147 updateYawPitchRoll(yprData);
flywind 0:f90c3452d779 148 }
flywind 0:f90c3452d779 149
flywind 0:f90c3452d779 150
flywind 0:f90c3452d779 151 void updateYawPitchRoll(float *yprData) {
flywind 0:f90c3452d779 152
flywind 2:44bc61abdf33 153 /*
flywind 2:44bc61abdf33 154 int32_t yaw_ieee11073 = quick_ieee11073_from_float(yprData[0]);
flywind 2:44bc61abdf33 155 int32_t pitch_ieee11073 = quick_ieee11073_from_float(yprData[1]);
flywind 2:44bc61abdf33 156 int32_t roll_ieee11073 = quick_ieee11073_from_float(yprData[2]);
flywind 2:44bc61abdf33 157 */
flywind 2:44bc61abdf33 158 uint16_t yaw_ieee11073 = (uint16_t)(100*(yprData[0]+180));
flywind 2:44bc61abdf33 159 uint16_t pitch_ieee11073 = (uint16_t)(100*(yprData[1]+180));
flywind 2:44bc61abdf33 160 uint16_t roll_ieee11073 = (uint16_t)(100*(yprData[2]+180));
flywind 2:44bc61abdf33 161 memcpy(&bytes[0], &yaw_ieee11073, sizeof(uint16_t));
flywind 2:44bc61abdf33 162 memcpy(&bytes[OFFSET_OF_FLAGS+sizeof(uint16_t)], &pitch_ieee11073, sizeof(uint16_t));
flywind 2:44bc61abdf33 163 memcpy(&bytes[OFFSET_OF_FLAGS+2*sizeof(uint16_t)], &roll_ieee11073, sizeof(uint16_t));
flywind 0:f90c3452d779 164 }
flywind 0:f90c3452d779 165
flywind 0:f90c3452d779 166 uint8_t *getPointer(void) {
flywind 0:f90c3452d779 167 return bytes;
flywind 0:f90c3452d779 168 }
flywind 0:f90c3452d779 169
flywind 0:f90c3452d779 170 const uint8_t *getPointer(void) const {
flywind 0:f90c3452d779 171 return bytes;
flywind 0:f90c3452d779 172 }
flywind 0:f90c3452d779 173
flywind 0:f90c3452d779 174 private:
flywind 0:f90c3452d779 175 /**
flywind 0:f90c3452d779 176 * @brief A very quick conversion between a float temperature and 11073-20601 FLOAT-Type.
flywind 0:f90c3452d779 177 * @param temperature The temperature as a float.
flywind 0:f90c3452d779 178 * @return The temperature in 11073-20601 FLOAT-Type format.
flywind 0:f90c3452d779 179 */
flywind 2:44bc61abdf33 180 int32_t quick_ieee11073_from_float(float yprData) {
flywind 2:44bc61abdf33 181 int8_t exponent = 0xFE; //exponent is -2
flywind 2:44bc61abdf33 182 int32_t mantissa = (int32_t)(yprData * 100);
flywind 2:44bc61abdf33 183 return (((int32_t)exponent) << 24) | mantissa;
flywind 0:f90c3452d779 184 }
flywind 0:f90c3452d779 185
flywind 0:f90c3452d779 186 private:
flywind 0:f90c3452d779 187 /* First byte = 8-bit flags, Second field is a float holding the temperature value. */
flywind 0:f90c3452d779 188 /* See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_measurement.xml */
flywind 0:f90c3452d779 189 uint8_t bytes[SIZEOF_VALUE_BYTES];
flywind 0:f90c3452d779 190 };
flywind 0:f90c3452d779 191
flywind 0:f90c3452d779 192
flywind 0:f90c3452d779 193
flywind 0:f90c3452d779 194
flywind 0:f90c3452d779 195
flywind 0:f90c3452d779 196 private:
flywind 0:f90c3452d779 197 BLEDevice &ble;
flywind 0:f90c3452d779 198
flywind 0:f90c3452d779 199 uint8_t receiveBuffer[BLE_MPU_SERVICE_MAX_DATA_LEN]; /**< The local buffer into which we receive
flywind 0:f90c3452d779 200 * inbound data before forwarding it to the
flywind 0:f90c3452d779 201 * application. */
flywind 0:f90c3452d779 202 uint8_t YPRBuffer[BLE_MPU_SERVICE_MAX_DATA_LEN];
flywind 0:f90c3452d779 203
flywind 0:f90c3452d779 204
flywind 2:44bc61abdf33 205 uint8_t numBytesReceived ;
flywind 0:f90c3452d779 206 uint8_t receiveBufferIndex;
flywind 0:f90c3452d779 207
flywind 0:f90c3452d779 208 YawPitchRollValueBytes yprValueBytes;
flywind 0:f90c3452d779 209
flywind 0:f90c3452d779 210
flywind 0:f90c3452d779 211 ReadOnlyGattCharacteristic<YawPitchRollValueBytes> YPRCharacteristic;
flywind 0:f90c3452d779 212
flywind 0:f90c3452d779 213 GattCharacteristic settingCharacteristic;
flywind 0:f90c3452d779 214
flywind 0:f90c3452d779 215 };
flywind 0:f90c3452d779 216
flywind 0:f90c3452d779 217 #endif /* #ifndef __BLE_MPU_SERVICE_H__*/