MPU6050センサ Wallbot_BLE用 サンプル

Dependencies:   BLE_API mbed nRF51822

Committer:
Dyotty
Date:
Sun Jun 03 07:00:30 2018 +0000
Revision:
22:05d9fca84f59
Parent:
21:600cf473d9e7
First commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_tw_hoehoe 0:2fa085b36ad8 1 #include "mbed.h"
mbed_tw_hoehoe 21:600cf473d9e7 2 #include "BLE.h"
mbed_tw_hoehoe 0:2fa085b36ad8 3 #include "MPU6050.h"
mbed_tw_hoehoe 0:2fa085b36ad8 4
mbed_tw_hoehoe 0:2fa085b36ad8 5 /*
mbed_tw_hoehoe 0:2fa085b36ad8 6 #define DBG 1
mbed_tw_hoehoe 0:2fa085b36ad8 7 #define NEED_CONSOLE_OUTPUT 1 // Set this if you need debug messages on the console;
mbed_tw_hoehoe 0:2fa085b36ad8 8 // it will have an impact on code-size and power consumption.
mbed_tw_hoehoe 0:2fa085b36ad8 9
mbed_tw_hoehoe 0:2fa085b36ad8 10 #if NEED_CONSOLE_OUTPUT
mbed_tw_hoehoe 0:2fa085b36ad8 11 Serial pc(USBTX, USBRX);
mbed_tw_hoehoe 0:2fa085b36ad8 12 #define DEBUG(...) { pc.printf(__VA_ARGS__); }
mbed_tw_hoehoe 0:2fa085b36ad8 13 #else
mbed_tw_hoehoe 0:2fa085b36ad8 14 #define DEBUG(...) // nothing //
mbed_tw_hoehoe 0:2fa085b36ad8 15 #endif // #if NEED_CONSOLE_OUTPUT //
mbed_tw_hoehoe 0:2fa085b36ad8 16 */
mbed_tw_hoehoe 0:2fa085b36ad8 17
mbed_tw_hoehoe 12:6ef51c5442dd 18 //#define MIN_CONN_INTERVAL 250 /**< Minimum connection interval */
mbed_tw_hoehoe 12:6ef51c5442dd 19 //#define MAX_CONN_INTERVAL 350 /**< Maximum connection interval */
mbed_tw_hoehoe 19:234f3afad29d 20 #define CONN_INTERVAL 25 /**< connection interval 250ms; in multiples of 0.125ms. (durationInMillis * 1000) / UNIT_0_625_MS; */
mbed_tw_hoehoe 12:6ef51c5442dd 21 #define CONN_SUP_TIMEOUT 8000 /**< Connection supervisory timeout (6 seconds); in multiples of 0.125ms. */
mbed_tw_hoehoe 15:f0306f9dc7ad 22 #define SLAVE_LATENCY 0
mbed_tw_hoehoe 20:9ff36d021c34 23 #define TICKER_INTERVAL 2.0f
mbed_tw_hoehoe 9:aca7ff8a1945 24
mbed_tw_hoehoe 21:600cf473d9e7 25 BLE ble;
mbed_tw_hoehoe 0:2fa085b36ad8 26
Dyotty 22:05d9fca84f59 27 ///// 180601
Dyotty 22:05d9fca84f59 28 /********* 変数宣言 ***********/
Dyotty 22:05d9fca84f59 29
Dyotty 22:05d9fca84f59 30 // MPU6050 G & Gyroセンサ 変数宣言
Dyotty 22:05d9fca84f59 31 MPU6050 mpu(I2C_SDA, I2C_SCL);
mbed_tw_hoehoe 10:f226b7907cf2 32
Dyotty 22:05d9fca84f59 33 // Serial通信 変数宣言
Dyotty 22:05d9fca84f59 34 Serial pc(USBTX, USBRX);
Dyotty 22:05d9fca84f59 35 ///// 180601
Dyotty 22:05d9fca84f59 36
Dyotty 22:05d9fca84f59 37
Dyotty 22:05d9fca84f59 38 static const char DEVICENAME[] = "WallbotBLE";
mbed_tw_hoehoe 11:5a3dcafaffbb 39 static volatile bool triggerSensorPolling = false;
mbed_tw_hoehoe 0:2fa085b36ad8 40
mbed_tw_hoehoe 4:3a25f1bdffc7 41 //const uint8_t MPU6050_adv_service_uuid[] = {
mbed_tw_hoehoe 4:3a25f1bdffc7 42 // 0x9F,0xDF,0x32,0x83,
mbed_tw_hoehoe 4:3a25f1bdffc7 43 // 0x90,0x49,
mbed_tw_hoehoe 4:3a25f1bdffc7 44 // 0xCF,0x8D,
mbed_tw_hoehoe 4:3a25f1bdffc7 45 // 0x5C,0x4D,
mbed_tw_hoehoe 4:3a25f1bdffc7 46 // 0x98,0xE7,0xE2,0x00,0x27,0x31
mbed_tw_hoehoe 4:3a25f1bdffc7 47 //};
mbed_tw_hoehoe 0:2fa085b36ad8 48
mbed_tw_hoehoe 0:2fa085b36ad8 49 const uint8_t MPU6050_service_uuid[] = {
mbed_tw_hoehoe 19:234f3afad29d 50 0x45,0x35,0x56,0x80,0x0F,0xD8,0x5F,0xB5,0x51,0x48,0x30,0x27,0x06,0x9B,0x3F,0xD9
mbed_tw_hoehoe 0:2fa085b36ad8 51 };
mbed_tw_hoehoe 0:2fa085b36ad8 52
mbed_tw_hoehoe 0:2fa085b36ad8 53 const uint8_t MPU6050_Accel_Characteristic_uuid[] = {
mbed_tw_hoehoe 19:234f3afad29d 54 0x45,0x35,0x56,0x81,0x0F,0xD8,0x5F,0xB5,0x51,0x48,0x30,0x27,0x06,0x9B,0x3F,0xD9
mbed_tw_hoehoe 0:2fa085b36ad8 55 };
mbed_tw_hoehoe 0:2fa085b36ad8 56
mbed_tw_hoehoe 15:f0306f9dc7ad 57 const uint8_t MPU6050_Write_Characteristic_uuid[] =
mbed_tw_hoehoe 5:807096fdd895 58 {
mbed_tw_hoehoe 19:234f3afad29d 59 0x45,0x35,0x56,0x83,0x0F,0xD8,0x5F,0xB5,0x51,0x48,0x30,0x27,0x06,0x9B,0x3F,0xD9
mbed_tw_hoehoe 5:807096fdd895 60 };
mbed_tw_hoehoe 5:807096fdd895 61
mbed_tw_hoehoe 14:700c99bb766c 62 uint8_t accelPayload[sizeof(float)*10] = {0,};
mbed_tw_hoehoe 9:aca7ff8a1945 63
mbed_tw_hoehoe 19:234f3afad29d 64 uint8_t defaultWriteValue = 3;
mbed_tw_hoehoe 17:e7c43d1b362f 65 uint8_t writePayload[2] = {defaultWriteValue, defaultWriteValue,};
mbed_tw_hoehoe 5:807096fdd895 66
mbed_tw_hoehoe 0:2fa085b36ad8 67
mbed_tw_hoehoe 0:2fa085b36ad8 68 GattCharacteristic accelChar (MPU6050_Accel_Characteristic_uuid,
mbed_tw_hoehoe 14:700c99bb766c 69 accelPayload, (sizeof(float) * 10), (sizeof(float) * 10),
mbed_tw_hoehoe 0:2fa085b36ad8 70 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
mbed_tw_hoehoe 0:2fa085b36ad8 71
mbed_tw_hoehoe 5:807096fdd895 72 GattCharacteristic writeChar (MPU6050_Write_Characteristic_uuid,
mbed_tw_hoehoe 14:700c99bb766c 73 writePayload, 2, 2,
mbed_tw_hoehoe 7:e18346358299 74 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
mbed_tw_hoehoe 5:807096fdd895 75
mbed_tw_hoehoe 5:807096fdd895 76 GattCharacteristic *ControllerChars[] = { &accelChar, &writeChar, };
mbed_tw_hoehoe 4:3a25f1bdffc7 77 GattService MPU6050Service(MPU6050_service_uuid, ControllerChars, sizeof(ControllerChars) / sizeof(GattCharacteristic *));
mbed_tw_hoehoe 0:2fa085b36ad8 78
mbed_tw_hoehoe 9:aca7ff8a1945 79
Dyotty 22:05d9fca84f59 80 ///// 180601
Dyotty 22:05d9fca84f59 81 // ・MPU6050から値を取得
Dyotty 22:05d9fca84f59 82 // ・ループ実行されている関数
mbed_tw_hoehoe 0:2fa085b36ad8 83 void updateValue(void){
Dyotty 22:05d9fca84f59 84 // 値を格納する用の配列、変数
mbed_tw_hoehoe 0:2fa085b36ad8 85 float acData[3];
mbed_tw_hoehoe 0:2fa085b36ad8 86 float gyData[3];
mbed_tw_hoehoe 6:e640afab8288 87 float tempData = 0.0f;
mbed_tw_hoehoe 14:700c99bb766c 88 float at = 0.0f;
mbed_tw_hoehoe 14:700c99bb766c 89 float gt = 0.0f;
mbed_tw_hoehoe 14:700c99bb766c 90 float tickerInterval = 0.0f;
Dyotty 22:05d9fca84f59 91
mbed_tw_hoehoe 3:ca2cdabf5977 92 //加速度を取得
mbed_tw_hoehoe 15:f0306f9dc7ad 93 Timer acTimer;
mbed_tw_hoehoe 15:f0306f9dc7ad 94 acTimer.start();
Dyotty 22:05d9fca84f59 95 mpu.getAccelero(acData); //加速度を取得 acDataに格納
mbed_tw_hoehoe 15:f0306f9dc7ad 96 acTimer.stop();
mbed_tw_hoehoe 15:f0306f9dc7ad 97 at = acTimer.read_ms();
mbed_tw_hoehoe 15:f0306f9dc7ad 98 acTimer.reset();
mbed_tw_hoehoe 14:700c99bb766c 99
Dyotty 22:05d9fca84f59 100 //BLEで送信されるデータメモリに値をコピー(ここは今回使わない)
mbed_tw_hoehoe 0:2fa085b36ad8 101 memcpy(accelPayload+sizeof(float)*0, &acData[0], sizeof(acData[0]));
mbed_tw_hoehoe 0:2fa085b36ad8 102 memcpy(accelPayload+sizeof(float)*1, &acData[1], sizeof(acData[1]));
mbed_tw_hoehoe 0:2fa085b36ad8 103 memcpy(accelPayload+sizeof(float)*2, &acData[2], sizeof(acData[2]));
mbed_tw_hoehoe 0:2fa085b36ad8 104
mbed_tw_hoehoe 3:ca2cdabf5977 105 //ジャイロを取得
mbed_tw_hoehoe 15:f0306f9dc7ad 106 Timer gyTimer;
mbed_tw_hoehoe 15:f0306f9dc7ad 107 gyTimer.start();
Dyotty 22:05d9fca84f59 108 mpu.getGyro(gyData); //ジャイロの値(角加速度)を取得 gyDataに格納
mbed_tw_hoehoe 15:f0306f9dc7ad 109 gyTimer.stop();
mbed_tw_hoehoe 15:f0306f9dc7ad 110 gt = gyTimer.read_ms();
mbed_tw_hoehoe 15:f0306f9dc7ad 111 gyTimer.reset();
Dyotty 22:05d9fca84f59 112
Dyotty 22:05d9fca84f59 113 //BLEで送信されるデータメモリに値をコピー(ここは今回使わない)
mbed_tw_hoehoe 4:3a25f1bdffc7 114 memcpy(accelPayload+sizeof(float)*3, &gyData[0], sizeof(gyData[0]));
mbed_tw_hoehoe 4:3a25f1bdffc7 115 memcpy(accelPayload+sizeof(float)*4, &gyData[1], sizeof(gyData[1]));
mbed_tw_hoehoe 4:3a25f1bdffc7 116 memcpy(accelPayload+sizeof(float)*5, &gyData[2], sizeof(gyData[2]));
mbed_tw_hoehoe 0:2fa085b36ad8 117
Dyotty 22:05d9fca84f59 118 //floatの値を合計5桁、小数点以下3桁の形でPCへ送信
Dyotty 22:05d9fca84f59 119 pc.printf("%5.3f:%5.3f:%5.3f:%5.3f:%5.3f:%5.3f \n", acData[0], acData[1], acData[2],gyData[0],gyData[1],gyData[2]);
Dyotty 22:05d9fca84f59 120
mbed_tw_hoehoe 0:2fa085b36ad8 121 //温度を取得
mbed_tw_hoehoe 0:2fa085b36ad8 122 tempData = mpu.getTemp();
mbed_tw_hoehoe 0:2fa085b36ad8 123
mbed_tw_hoehoe 4:3a25f1bdffc7 124 memcpy(accelPayload+sizeof(float)*6, &tempData, sizeof(tempData));
mbed_tw_hoehoe 0:2fa085b36ad8 125 /*
mbed_tw_hoehoe 0:2fa085b36ad8 126 pc.printf("Accel: %.3lf,%.3lf,%.3lf\r\n",
mbed_tw_hoehoe 0:2fa085b36ad8 127 *(float*)&accelPayload[sizeof(float)*0],
mbed_tw_hoehoe 0:2fa085b36ad8 128 *(float*)&accelPayload[sizeof(float)*1],
mbed_tw_hoehoe 0:2fa085b36ad8 129 *(float*)&accelPayload[sizeof(float)*2]);
mbed_tw_hoehoe 11:5a3dcafaffbb 130
mbed_tw_hoehoe 0:2fa085b36ad8 131 pc.printf("Gyro: %.3lf,%.3lf,%.3lf\r\n",
mbed_tw_hoehoe 11:5a3dcafaffbb 132 *(float*)&accelPayload[sizeof(float)*3],
mbed_tw_hoehoe 11:5a3dcafaffbb 133 *(float*)&accelPayload[sizeof(float)*4],
mbed_tw_hoehoe 11:5a3dcafaffbb 134 *(float*)&accelPayload[sizeof(float)*5]);
mbed_tw_hoehoe 11:5a3dcafaffbb 135
mbed_tw_hoehoe 11:5a3dcafaffbb 136 pc.printf("Temp: %.3lf\r\n", *(float*)&accelPayload[sizeof(float)*6]);
mbed_tw_hoehoe 0:2fa085b36ad8 137 */
mbed_tw_hoehoe 14:700c99bb766c 138 memcpy(accelPayload+sizeof(float)*7, &at, sizeof(at));
mbed_tw_hoehoe 14:700c99bb766c 139 memcpy(accelPayload+sizeof(float)*8, &gt, sizeof(gt));
mbed_tw_hoehoe 10:f226b7907cf2 140
mbed_tw_hoehoe 20:9ff36d021c34 141 tickerInterval = TICKER_INTERVAL;
mbed_tw_hoehoe 14:700c99bb766c 142 memcpy(accelPayload+sizeof(float)*9, &tickerInterval, sizeof(tickerInterval));
mbed_tw_hoehoe 9:aca7ff8a1945 143
mbed_tw_hoehoe 0:2fa085b36ad8 144 ble.updateCharacteristicValue(accelChar.getValueAttribute().getHandle(), accelPayload, sizeof(accelPayload)); //Mod
mbed_tw_hoehoe 9:aca7ff8a1945 145 ble.updateCharacteristicValue(writeChar.getValueAttribute().getHandle(), writePayload, sizeof(writePayload)); //Mod
mbed_tw_hoehoe 0:2fa085b36ad8 146 }
mbed_tw_hoehoe 7:e18346358299 147
mbed_tw_hoehoe 7:e18346358299 148 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) // Mod
mbed_tw_hoehoe 7:e18346358299 149 {
mbed_tw_hoehoe 7:e18346358299 150
mbed_tw_hoehoe 7:e18346358299 151 //DEBUG("Disconnected handle %u, reason %u\n", handle, reason);
mbed_tw_hoehoe 7:e18346358299 152 //DEBUG("Restarting the advertising process\n\r");
mbed_tw_hoehoe 7:e18346358299 153
mbed_tw_hoehoe 7:e18346358299 154 ble.startAdvertising();
mbed_tw_hoehoe 7:e18346358299 155 }
mbed_tw_hoehoe 7:e18346358299 156
mbed_tw_hoehoe 21:600cf473d9e7 157 void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
mbed_tw_hoehoe 7:e18346358299 158 {
mbed_tw_hoehoe 7:e18346358299 159
mbed_tw_hoehoe 7:e18346358299 160 //DEBUG("connected. Got handle %u\r\n", handle);
mbed_tw_hoehoe 7:e18346358299 161
mbed_tw_hoehoe 7:e18346358299 162 /*******************************************************************************/
mbed_tw_hoehoe 7:e18346358299 163 /* CentralがMacOS X の時 connection intervalを設定する場合は */
mbed_tw_hoehoe 7:e18346358299 164 /* nRF51822 -> projectconfig.h -> GAP -> */
mbed_tw_hoehoe 7:e18346358299 165 /* CFG_GAP_CONNECTION_MIN_INTERVAL_MS / CFG_GAP_CONNECTION_MAX_INTERVAL_MSを */
mbed_tw_hoehoe 7:e18346358299 166 /* 直接編集すること */
mbed_tw_hoehoe 7:e18346358299 167 /******************************************************************************/
mbed_tw_hoehoe 18:7ef0e3b768c2 168 //Gap::Handle_t handle;
mbed_tw_hoehoe 7:e18346358299 169 Gap::ConnectionParams_t gap_conn_params;
mbed_tw_hoehoe 9:aca7ff8a1945 170 gap_conn_params.minConnectionInterval = Gap::MSEC_TO_GAP_DURATION_UNITS(CONN_INTERVAL);
mbed_tw_hoehoe 9:aca7ff8a1945 171 gap_conn_params.maxConnectionInterval = Gap::MSEC_TO_GAP_DURATION_UNITS(CONN_INTERVAL);
mbed_tw_hoehoe 7:e18346358299 172 gap_conn_params.connectionSupervisionTimeout = Gap::MSEC_TO_GAP_DURATION_UNITS(CONN_SUP_TIMEOUT);
mbed_tw_hoehoe 7:e18346358299 173 gap_conn_params.slaveLatency = SLAVE_LATENCY;
mbed_tw_hoehoe 18:7ef0e3b768c2 174
mbed_tw_hoehoe 21:600cf473d9e7 175 if (ble.updateConnectionParams(params->handle, &gap_conn_params) != BLE_ERROR_NONE) {
mbed_tw_hoehoe 18:7ef0e3b768c2 176 //DEBUG("failed to update connection paramter\r\n");
mbed_tw_hoehoe 18:7ef0e3b768c2 177 }
mbed_tw_hoehoe 7:e18346358299 178 }
mbed_tw_hoehoe 7:e18346358299 179
mbed_tw_hoehoe 21:600cf473d9e7 180 //void writtenCallback(const GattCharacteristicWriteCBParams *params){
mbed_tw_hoehoe 21:600cf473d9e7 181 void writtenCallback(const GattWriteCallbackParams *params){
mbed_tw_hoehoe 10:f226b7907cf2 182 char acceleroRange = 0xFF;
mbed_tw_hoehoe 10:f226b7907cf2 183 char gyroRange = 0xFF;
mbed_tw_hoehoe 10:f226b7907cf2 184
mbed_tw_hoehoe 21:600cf473d9e7 185 if (params->handle == writeChar.getValueAttribute().getHandle()) {
mbed_tw_hoehoe 7:e18346358299 186 uint16_t len = params->len;
mbed_tw_hoehoe 7:e18346358299 187
mbed_tw_hoehoe 16:e2386ded5c74 188 if (len == 2) {
mbed_tw_hoehoe 16:e2386ded5c74 189 uint8_t controller[2] = {0};
mbed_tw_hoehoe 7:e18346358299 190
mbed_tw_hoehoe 7:e18346358299 191 ble.readCharacteristicValue(writeChar.getValueAttribute().getHandle(), writePayload, &len);
mbed_tw_hoehoe 10:f226b7907cf2 192 memcpy(controller, writePayload, sizeof(controller));
mbed_tw_hoehoe 7:e18346358299 193
mbed_tw_hoehoe 10:f226b7907cf2 194 //pc.printf("write: %u, %u, %u\r\n", controller[0],controller[1],controller[2]);
mbed_tw_hoehoe 7:e18346358299 195
mbed_tw_hoehoe 16:e2386ded5c74 196 switch(controller[0]){
mbed_tw_hoehoe 19:234f3afad29d 197 case 1:
mbed_tw_hoehoe 10:f226b7907cf2 198 acceleroRange = MPU6050_ACCELERO_RANGE_2G;
mbed_tw_hoehoe 7:e18346358299 199 break;
mbed_tw_hoehoe 19:234f3afad29d 200 case 2:
mbed_tw_hoehoe 10:f226b7907cf2 201 acceleroRange = MPU6050_ACCELERO_RANGE_4G;
mbed_tw_hoehoe 7:e18346358299 202 break;
mbed_tw_hoehoe 19:234f3afad29d 203 case 3:
mbed_tw_hoehoe 10:f226b7907cf2 204 acceleroRange = MPU6050_ACCELERO_RANGE_8G;
mbed_tw_hoehoe 10:f226b7907cf2 205 break;
mbed_tw_hoehoe 19:234f3afad29d 206 case 4:
mbed_tw_hoehoe 10:f226b7907cf2 207 acceleroRange = MPU6050_ACCELERO_RANGE_16G;
mbed_tw_hoehoe 10:f226b7907cf2 208
mbed_tw_hoehoe 7:e18346358299 209 break;
mbed_tw_hoehoe 7:e18346358299 210 default:
mbed_tw_hoehoe 7:e18346358299 211 break;
mbed_tw_hoehoe 7:e18346358299 212 }
mbed_tw_hoehoe 7:e18346358299 213
mbed_tw_hoehoe 16:e2386ded5c74 214 switch(controller[1]){
mbed_tw_hoehoe 19:234f3afad29d 215 case 1:
mbed_tw_hoehoe 10:f226b7907cf2 216 gyroRange = MPU6050_GYRO_RANGE_250;
mbed_tw_hoehoe 7:e18346358299 217 break;
mbed_tw_hoehoe 19:234f3afad29d 218 case 2:
mbed_tw_hoehoe 11:5a3dcafaffbb 219 gyroRange = MPU6050_GYRO_RANGE_500;
mbed_tw_hoehoe 7:e18346358299 220 break;
mbed_tw_hoehoe 19:234f3afad29d 221 case 3:
mbed_tw_hoehoe 11:5a3dcafaffbb 222 gyroRange = MPU6050_GYRO_RANGE_1000;
mbed_tw_hoehoe 7:e18346358299 223 break;
mbed_tw_hoehoe 19:234f3afad29d 224 case 4:
mbed_tw_hoehoe 11:5a3dcafaffbb 225 gyroRange = MPU6050_GYRO_RANGE_2000;
mbed_tw_hoehoe 7:e18346358299 226 break;
mbed_tw_hoehoe 7:e18346358299 227 default:
mbed_tw_hoehoe 7:e18346358299 228 break;
mbed_tw_hoehoe 7:e18346358299 229 }
mbed_tw_hoehoe 11:5a3dcafaffbb 230 mpu.mpu_set_accel_fsr(acceleroRange);
mbed_tw_hoehoe 11:5a3dcafaffbb 231 mpu.mpu_set_gyro_fsr(gyroRange);
mbed_tw_hoehoe 11:5a3dcafaffbb 232
mbed_tw_hoehoe 7:e18346358299 233 }
mbed_tw_hoehoe 7:e18346358299 234 }
mbed_tw_hoehoe 7:e18346358299 235 }
mbed_tw_hoehoe 7:e18346358299 236
mbed_tw_hoehoe 21:600cf473d9e7 237 void timeoutCallback(const Gap::TimeoutSource_t source)
mbed_tw_hoehoe 7:e18346358299 238 {
mbed_tw_hoehoe 7:e18346358299 239 //DEBUG("TimeOut\n\r");
mbed_tw_hoehoe 7:e18346358299 240 //DEBUG("Restarting the advertising process\n\r");
mbed_tw_hoehoe 7:e18346358299 241
mbed_tw_hoehoe 7:e18346358299 242 ble.startAdvertising();
mbed_tw_hoehoe 7:e18346358299 243 }
mbed_tw_hoehoe 7:e18346358299 244
mbed_tw_hoehoe 9:aca7ff8a1945 245 void periodicCallback(void)
mbed_tw_hoehoe 9:aca7ff8a1945 246 {
mbed_tw_hoehoe 7:e18346358299 247 //oneSecondLed = !oneSecondLed; /* Do blinky on LED1 while we're waiting for BLE events */
mbed_tw_hoehoe 7:e18346358299 248
mbed_tw_hoehoe 7:e18346358299 249 /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
mbed_tw_hoehoe 7:e18346358299 250 * heavy-weight sensor polling from the main thread. */
mbed_tw_hoehoe 11:5a3dcafaffbb 251 triggerSensorPolling = true;
mbed_tw_hoehoe 9:aca7ff8a1945 252 }
mbed_tw_hoehoe 7:e18346358299 253
mbed_tw_hoehoe 7:e18346358299 254 /**************************************************************************/
mbed_tw_hoehoe 7:e18346358299 255 /*!
mbed_tw_hoehoe 7:e18346358299 256 @brief Program entry point
mbed_tw_hoehoe 7:e18346358299 257 */
mbed_tw_hoehoe 7:e18346358299 258 /**************************************************************************/
mbed_tw_hoehoe 7:e18346358299 259 int main(void)
mbed_tw_hoehoe 7:e18346358299 260 {
mbed_tw_hoehoe 7:e18346358299 261
mbed_tw_hoehoe 7:e18346358299 262 //#if DBG
mbed_tw_hoehoe 7:e18346358299 263 // pc.printf("Start\n\r");
mbed_tw_hoehoe 7:e18346358299 264 //#endif
mbed_tw_hoehoe 8:35390100e16f 265 /*
mbed_tw_hoehoe 19:234f3afad29d 266 #define MPU6050_ACCELERO_RANGE_2G 1
mbed_tw_hoehoe 19:234f3afad29d 267 #define MPU6050_ACCELERO_RANGE_4G 2
mbed_tw_hoehoe 19:234f3afad29d 268 #define MPU6050_ACCELERO_RANGE_8G 3
mbed_tw_hoehoe 19:234f3afad29d 269 #define MPU6050_ACCELERO_RANGE_16G 4
mbed_tw_hoehoe 8:35390100e16f 270
mbed_tw_hoehoe 19:234f3afad29d 271 #define MPU6050_GYRO_RANGE_250 1
mbed_tw_hoehoe 19:234f3afad29d 272 #define MPU6050_GYRO_RANGE_500 2
mbed_tw_hoehoe 19:234f3afad29d 273 #define MPU6050_GYRO_RANGE_1000 3
mbed_tw_hoehoe 19:234f3afad29d 274 #define MPU6050_GYRO_RANGE_2000 4
mbed_tw_hoehoe 8:35390100e16f 275 */
mbed_tw_hoehoe 9:aca7ff8a1945 276
Dyotty 22:05d9fca84f59 277
Dyotty 22:05d9fca84f59 278 ///180601 MPU6050センサの初期化処理
mbed_tw_hoehoe 7:e18346358299 279 mpu.initialize();
Dyotty 22:05d9fca84f59 280 mpu.setAcceleroRange(MPU6050_ACCELERO_RANGE_8G); //加速度センサ 計測レンジ設定(今回は2Gか4Gがよさそう)
Dyotty 22:05d9fca84f59 281 mpu.setGyroRange(MPU6050_GYRO_RANGE_1000); //ジャイロセンサ 計測レンジ設定(ここも250か500がよさそう(そんなに早く回転しないので))
Dyotty 22:05d9fca84f59 282 ///180601
mbed_tw_hoehoe 7:e18346358299 283
mbed_tw_hoehoe 7:e18346358299 284 if( mpu.testConnection() ){
mbed_tw_hoehoe 7:e18346358299 285 //pc.printf("mpu test:OK\n\r");
mbed_tw_hoehoe 7:e18346358299 286 }else{
mbed_tw_hoehoe 7:e18346358299 287 //pc.printf("mpu test:NG\n\r");
mbed_tw_hoehoe 7:e18346358299 288 }
mbed_tw_hoehoe 10:f226b7907cf2 289
mbed_tw_hoehoe 20:9ff36d021c34 290 float ticker_ms = (TICKER_INTERVAL / 100.0f);
mbed_tw_hoehoe 9:aca7ff8a1945 291 Ticker ticker;
mbed_tw_hoehoe 20:9ff36d021c34 292 ticker.attach(periodicCallback, ticker_ms);//0.02f //.2f-sec
mbed_tw_hoehoe 7:e18346358299 293
mbed_tw_hoehoe 7:e18346358299 294 ble.init();
mbed_tw_hoehoe 7:e18346358299 295 ble.onDisconnection(disconnectionCallback);
mbed_tw_hoehoe 18:7ef0e3b768c2 296 ble.onConnection(connectionCallback);
mbed_tw_hoehoe 18:7ef0e3b768c2 297 ble.onDataWritten(writtenCallback);
mbed_tw_hoehoe 7:e18346358299 298 ble.onTimeout(timeoutCallback);
mbed_tw_hoehoe 7:e18346358299 299
mbed_tw_hoehoe 7:e18346358299 300 /* setup device name */
mbed_tw_hoehoe 7:e18346358299 301 ble.setDeviceName((const uint8_t *)DEVICENAME);
mbed_tw_hoehoe 7:e18346358299 302
mbed_tw_hoehoe 7:e18346358299 303 /* setup advertising */
mbed_tw_hoehoe 7:e18346358299 304 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
mbed_tw_hoehoe 7:e18346358299 305 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
mbed_tw_hoehoe 7:e18346358299 306 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (const uint8_t *)DEVICENAME, sizeof(DEVICENAME));
mbed_tw_hoehoe 7:e18346358299 307 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
mbed_tw_hoehoe 7:e18346358299 308 (const uint8_t *)MPU6050_service_uuid, sizeof(MPU6050_service_uuid));
mbed_tw_hoehoe 7:e18346358299 309 //(const uint8_t *)MPU6050_adv_service_uuid, sizeof(MPU6050_adv_service_uuid));
mbed_tw_hoehoe 7:e18346358299 310
mbed_tw_hoehoe 7:e18346358299 311 ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
mbed_tw_hoehoe 7:e18346358299 312 ble.startAdvertising();
mbed_tw_hoehoe 7:e18346358299 313
mbed_tw_hoehoe 7:e18346358299 314 ble.addService(MPU6050Service);
mbed_tw_hoehoe 10:f226b7907cf2 315
Dyotty 22:05d9fca84f59 316
Dyotty 22:05d9fca84f59 317 pc.printf("Start!! \n");
Dyotty 22:05d9fca84f59 318
mbed_tw_hoehoe 7:e18346358299 319 while(true) {
Dyotty 22:05d9fca84f59 320 // if (triggerSensorPolling && ble.getGapState().connected) {
Dyotty 22:05d9fca84f59 321 // triggerSensorPolling = false;
mbed_tw_hoehoe 11:5a3dcafaffbb 322 updateValue();
Dyotty 22:05d9fca84f59 323 wait(0.5);
Dyotty 22:05d9fca84f59 324 // } else {
Dyotty 22:05d9fca84f59 325 // ble.waitForEvent();
Dyotty 22:05d9fca84f59 326 // }
mbed_tw_hoehoe 7:e18346358299 327 }
mbed_tw_hoehoe 7:e18346358299 328 }
mbed_tw_hoehoe 7:e18346358299 329