MPU6050センサ Wallbot_BLE用 サンプル
Dependencies: BLE_API mbed nRF51822
Diff: main.cpp
- Revision:
- 9:aca7ff8a1945
- Parent:
- 8:35390100e16f
- Child:
- 10:f226b7907cf2
diff -r 35390100e16f -r aca7ff8a1945 main.cpp --- a/main.cpp Sat Feb 07 05:58:51 2015 +0000 +++ b/main.cpp Mon Mar 02 13:28:18 2015 +0000 @@ -15,12 +15,19 @@ #endif // #if NEED_CONSOLE_OUTPUT // */ +//#define MIN_CONN_INTERVAL 250 /**< Minimum connection interval (250 ms) */ +//#define MAX_CONN_INTERVAL 350 /**< Maximum connection interval (350 ms). */ +#define CONN_INTERVAL 250 /**< Minimum connection interval (250 ms) */ +#define CONN_SUP_TIMEOUT 6000 /**< Connection supervisory timeout (6 seconds). */ +#define SLAVE_LATENCY 4 + + BLEDevice ble; MPU6050 mpu(I2C_SDA0, I2C_SCL0); static const char DEVICENAME[] = "BLE-Nano"; -static volatile bool triggerSensorPolling = false; +//static volatile bool triggerSensorPolling = false; //9FDF3283-9049-CF8D-5C4D-98E7E2002731 //const uint8_t MPU6050_adv_service_uuid[] = { @@ -32,7 +39,10 @@ //}; const uint8_t MPU6050_service_uuid[] = { - 0x31,0x27,0x00,0xE2,0xE7,0x98,0x4D,0x5C,0x8D,0xCF,0x49,0x90,0x83,0x32,0xDF,0x9F + 0x31,0x27,0x00,0xE2,0xE7,0x98, + 0x4D,0x5C, + 0x8D,0xCF, + 0x49,0x90,0x83,0x32,0xDF,0x9F }; const uint8_t MPU6050_Accel_Characteristic_uuid[] = { @@ -50,12 +60,14 @@ 0xC6, 0xDD, 0xB5, 0xAE, 0x7F, 0xA5 }; -uint8_t accelPayload[sizeof(float)*8] = {0,}; -uint8_t writePayload[3] = {0,}; +uint8_t accelPayload[sizeof(float)*9] = {0,}; + +uint8_t defaultWriteValue = 2; +uint8_t writePayload[3] = {0, defaultWriteValue, defaultWriteValue,}; GattCharacteristic accelChar (MPU6050_Accel_Characteristic_uuid, - accelPayload, (sizeof(float) * 8), (sizeof(float) * 8), + accelPayload, (sizeof(float) * 9), (sizeof(float) * 9), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); GattCharacteristic writeChar (MPU6050_Write_Characteristic_uuid, @@ -65,7 +77,7 @@ GattCharacteristic *ControllerChars[] = { &accelChar, &writeChar, }; GattService MPU6050Service(MPU6050_service_uuid, ControllerChars, sizeof(ControllerChars) / sizeof(GattCharacteristic *)); -Timer timer; + void updateValue(void){ @@ -73,7 +85,9 @@ float gyData[3]; float tempData = 0.0f; float t = 0.0f; - + float interval = 0.0f; + + Timer timer; timer.start(); //加速度を取得 @@ -112,7 +126,12 @@ t = timer.read_ms(); memcpy(accelPayload+sizeof(float)*7, &t, sizeof(t)); timer.reset(); + + interval = (float)CONN_INTERVAL; + memcpy(accelPayload+sizeof(float)*8, &interval, sizeof(interval)); + ble.updateCharacteristicValue(accelChar.getValueAttribute().getHandle(), accelPayload, sizeof(accelPayload)); //Mod + ble.updateCharacteristicValue(writeChar.getValueAttribute().getHandle(), writePayload, sizeof(writePayload)); //Mod } void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) // Mod @@ -135,15 +154,10 @@ /* CFG_GAP_CONNECTION_MIN_INTERVAL_MS / CFG_GAP_CONNECTION_MAX_INTERVAL_MSを */ /* 直接編集すること */ /******************************************************************************/ - - #define MIN_CONN_INTERVAL 250 /**< Minimum connection interval (250 ms) */ - #define MAX_CONN_INTERVAL 350 /**< Maximum connection interval (350 ms). */ - #define CONN_SUP_TIMEOUT 6000 /**< Connection supervisory timeout (6 seconds). */ - #define SLAVE_LATENCY 4 Gap::ConnectionParams_t gap_conn_params; - gap_conn_params.minConnectionInterval = Gap::MSEC_TO_GAP_DURATION_UNITS(MIN_CONN_INTERVAL); - gap_conn_params.maxConnectionInterval = Gap::MSEC_TO_GAP_DURATION_UNITS(MAX_CONN_INTERVAL); + gap_conn_params.minConnectionInterval = Gap::MSEC_TO_GAP_DURATION_UNITS(CONN_INTERVAL); + gap_conn_params.maxConnectionInterval = Gap::MSEC_TO_GAP_DURATION_UNITS(CONN_INTERVAL); gap_conn_params.connectionSupervisionTimeout = Gap::MSEC_TO_GAP_DURATION_UNITS(CONN_SUP_TIMEOUT); gap_conn_params.slaveLatency = SLAVE_LATENCY; ble.updateConnectionParams(handle, &gap_conn_params); @@ -217,14 +231,15 @@ ble.startAdvertising(); } -//void periodicCallback(void) -//{ +void periodicCallback(void) +{ //oneSecondLed = !oneSecondLed; /* Do blinky on LED1 while we're waiting for BLE events */ /* Note that the periodicCallback() executes in interrupt context, so it is safer to do * heavy-weight sensor polling from the main thread. */ //triggerSensorPolling = true; -//} + updateValue(); +} /**************************************************************************/ /*! @@ -248,9 +263,10 @@ #define MPU6050_GYRO_RANGE_1000 2 #define MPU6050_GYRO_RANGE_2000 3 */ + mpu.initialize(); - mpu.setAcceleroRange(2); // - mpu.setGyroRange(2); + mpu.setAcceleroRange(defaultWriteValue); // + mpu.setGyroRange(defaultWriteValue); if( mpu.testConnection() ){ //pc.printf("mpu test:OK\n\r"); @@ -258,8 +274,8 @@ //pc.printf("mpu test:NG\n\r"); } - //Ticker ticker; - //ticker.attach(periodicCallback, 0.25f); //1sec + Ticker ticker; + ticker.attach(periodicCallback, 0.25f); //1sec ble.init(); ble.onDisconnection(disconnectionCallback); @@ -286,9 +302,9 @@ while(true) { //if (triggerSensorPolling && ble.getGapState().connected) { //triggerSensorPolling = false; - updateValue(); + //updateValue(); //} else { - // ble.waitForEvent(); + ble.waitForEvent(); //} } }