MPU6050センサ Wallbot_BLE用 サンプル
Dependencies: BLE_API mbed nRF51822
Diff: main.cpp
- Revision:
- 7:e18346358299
- Parent:
- 6:e640afab8288
- Child:
- 8:35390100e16f
diff -r e640afab8288 -r e18346358299 main.cpp --- a/main.cpp Sat Jan 31 21:54:53 2015 +0000 +++ b/main.cpp Tue Feb 03 17:16:08 2015 +0000 @@ -50,20 +50,9 @@ 0xC6, 0xDD, 0xB5, 0xAE, 0x7F, 0xA5 }; -//const uint8_t MPU6050_Gyro_Characteristic_uuid[] = { -// 0x65, 0x60, 0xBE, 0x95,0xD9, 0x31, -// 0x4F, 0x64, -// 0x95, 0x75, -// 0xC6, 0xDD, 0xB5, 0xAE, 0x7F, 0xA5 -//}; - uint8_t accelPayload[sizeof(float)*8] = {0,}; uint8_t writePayload[3] = {0,}; -//uint8_t gyroPayload[sizeof(float)*3] = {0,0,0}; -//uint8_t tempPayload[sizeof(float)*1] = {0}; -//uint8_t batt = 100; /* Battery level */ -//uint8_t read_batt = 0; /* Variable to hold battery level reads */ GattCharacteristic accelChar (MPU6050_Accel_Characteristic_uuid, accelPayload, (sizeof(float) * 8), (sizeof(float) * 8), @@ -71,189 +60,13 @@ GattCharacteristic writeChar (MPU6050_Write_Characteristic_uuid, writePayload, 3, 3, - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); -// | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); -//GattCharacteristic gyroChar (MPU6050_Gyro_Characteristic_uuid, -// gyroPayload, (sizeof(float) * 3), (sizeof(float) * 3), -// GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); - -//GattCharacteristic *ControllerChars[] = { &accelChar, &gyroChar, }; GattCharacteristic *ControllerChars[] = { &accelChar, &writeChar, }; GattService MPU6050Service(MPU6050_service_uuid, ControllerChars, sizeof(ControllerChars) / sizeof(GattCharacteristic *)); Timer timer; -void updateValue(); - -void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) // Mod -{ - - //DEBUG("Disconnected handle %u, reason %u\n", handle, reason); - //DEBUG("Restarting the advertising process\n\r"); - - ble.startAdvertising(); -} - -void onConnectionCallback(Gap::Handle_t handle, Gap::addr_type_t peerAddrType, const Gap::address_t peerAddr, const Gap::ConnectionParams_t *params) //Mod -{ - - //DEBUG("connected. Got handle %u\r\n", handle); - - /*******************************************************************************/ - /* CentralがMacOS X の時 connection intervalを設定する場合は */ - /* nRF51822 -> projectconfig.h -> GAP -> */ - /* 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.connectionSupervisionTimeout = Gap::MSEC_TO_GAP_DURATION_UNITS(CONN_SUP_TIMEOUT); - gap_conn_params.slaveLatency = SLAVE_LATENCY; - ble.updateConnectionParams(handle, &gap_conn_params); - //if (ble.updateConnectionParams(handle, &gap_conn_params) != BLE_ERROR_NONE) { - // DEBUG("failed to update connection paramter\r\n"); - //} -} - -void onDataWritten(const GattCharacteristicWriteCBParams *params){ - - if (params->charHandle == writeChar.getValueAttribute().getHandle()) { - uint16_t len = params->len; - - if (len == 3) { - uint8_t controller[3] = {0}; - - ble.readCharacteristicValue(writeChar.getValueAttribute().getHandle(), writePayload, &len); - memcpy( &controller, writePayload, sizeof(controller)); - - /* mpu reset */ - if(controller[0] == 1){ - mpu.initialize(); - } - - /* - MPU6050_ACCELERO_RANGE_2G 0 - MPU6050_ACCELERO_RANGE_4G 1 - MPU6050_ACCELERO_RANGE_8G 2 - */ - switch(controller[1]){ - case 0: - mpu.setAcceleroRange(0); - break; - case 1: - mpu.setAcceleroRange(1); - break; - case 2: - mpu.setAcceleroRange(2); - break; - default: - break; - } - - /* - MPU6050_GYRO_RANGE_250 0 - MPU6050_GYRO_RANGE_500 1 - MPU6050_GYRO_RANGE_1000 2 - MPU6050_GYRO_RANGE_2000 3 - */ - switch(controller[2]){ - case 0: - mpu.setGyroRange(0); - break; - case 1: - mpu.setGyroRange(1); - break; - case 2: - mpu.setGyroRange(2); - break; - case 3: - mpu.setGyroRange(3); - break; - default: - break; - } - } - } -} - -void timeoutCallback(void) -{ - //DEBUG("TimeOut\n\r"); - //DEBUG("Restarting the advertising process\n\r"); - - ble.startAdvertising(); -} - -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; -} - -/**************************************************************************/ -/*! - @brief Program entry point -*/ -/**************************************************************************/ -int main(void) -{ - - //#if DBG - // pc.printf("Start\n\r"); - //#endif - - if( mpu.testConnection() ){ - //pc.printf("mpu test:OK\n\r"); - }else{ - //pc.printf("mpu test:NG\n\r"); - } - - //Ticker ticker; - //ticker.attach(periodicCallback, 0.25f); //1sec - - ble.init(); - ble.onDisconnection(disconnectionCallback); - ble.onConnection(onConnectionCallback); - ble.onDataWritten(onDataWritten); - ble.onTimeout(timeoutCallback); - - /* setup device name */ - ble.setDeviceName((const uint8_t *)DEVICENAME); - - /* setup advertising */ - ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); - ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); - ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (const uint8_t *)DEVICENAME, sizeof(DEVICENAME)); - ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, - (const uint8_t *)MPU6050_service_uuid, sizeof(MPU6050_service_uuid)); - //(const uint8_t *)MPU6050_adv_service_uuid, sizeof(MPU6050_adv_service_uuid)); - - ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ - ble.startAdvertising(); - - ble.addService(MPU6050Service); - - while(true) { - //if (triggerSensorPolling && ble.getGapState().connected) { - //triggerSensorPolling = false; - updateValue(); - //} else { - // ble.waitForEvent(); - //} - } -} - void updateValue(void){ float acData[3]; @@ -301,3 +114,169 @@ timer.reset(); ble.updateCharacteristicValue(accelChar.getValueAttribute().getHandle(), accelPayload, sizeof(accelPayload)); //Mod } + +void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) // Mod +{ + + //DEBUG("Disconnected handle %u, reason %u\n", handle, reason); + //DEBUG("Restarting the advertising process\n\r"); + + ble.startAdvertising(); +} + +void onConnectionCallback(Gap::Handle_t handle, Gap::addr_type_t peerAddrType, const Gap::address_t peerAddr, const Gap::ConnectionParams_t *params) //Mod +{ + + //DEBUG("connected. Got handle %u\r\n", handle); + + /*******************************************************************************/ + /* CentralがMacOS X の時 connection intervalを設定する場合は */ + /* nRF51822 -> projectconfig.h -> GAP -> */ + /* 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.connectionSupervisionTimeout = Gap::MSEC_TO_GAP_DURATION_UNITS(CONN_SUP_TIMEOUT); + gap_conn_params.slaveLatency = SLAVE_LATENCY; + ble.updateConnectionParams(handle, &gap_conn_params); + //if (ble.updateConnectionParams(handle, &gap_conn_params) != BLE_ERROR_NONE) { + // DEBUG("failed to update connection paramter\r\n"); + //} +} + +void onDataWritten(const GattCharacteristicWriteCBParams *params){ + + if (params->charHandle == writeChar.getValueAttribute().getHandle()) { + uint16_t len = params->len; + + if (len == 3) { + uint8_t controller[3] = {0}; + + ble.readCharacteristicValue(writeChar.getValueAttribute().getHandle(), writePayload, &len); + + memcpy(controller+sizeof(uint8_t)*0, &writePayload[0], sizeof(writePayload[0])); + memcpy(controller+sizeof(uint8_t)*1, &writePayload[1], sizeof(writePayload[1])); + memcpy(controller+sizeof(uint8_t)*2, &writePayload[2], sizeof(writePayload[2])); + + if(controller[0] == 1){ + mpu.initialize(); + } + + switch(controller[1]){ + case 0: + mpu.setAcceleroRange(0); + break; + case 1: + mpu.setAcceleroRange(1); + break; + case 2: + mpu.setAcceleroRange(2); + break; + default: + break; + } + + switch(controller[2]){ + case 0: + mpu.setGyroRange(0); + break; + case 1: + mpu.setGyroRange(1); + break; + case 2: + mpu.setGyroRange(2); + break; + case 3: + mpu.setGyroRange(3); + break; + default: + break; + } + if( mpu.testConnection() ){ + //pc.printf("mpu test:OK\n\r"); + }else{ + //pc.printf("mpu test:NG\n\r"); + } + } + } +} + +void timeoutCallback(void) +{ + //DEBUG("TimeOut\n\r"); + //DEBUG("Restarting the advertising process\n\r"); + + ble.startAdvertising(); +} + +//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; +//} + +/**************************************************************************/ +/*! + @brief Program entry point +*/ +/**************************************************************************/ +int main(void) +{ + + //#if DBG + // pc.printf("Start\n\r"); + //#endif + mpu.initialize(); + + if( mpu.testConnection() ){ + //pc.printf("mpu test:OK\n\r"); + }else{ + //pc.printf("mpu test:NG\n\r"); + } + + //Ticker ticker; + //ticker.attach(periodicCallback, 0.25f); //1sec + + ble.init(); + ble.onDisconnection(disconnectionCallback); + ble.onConnection(onConnectionCallback); + ble.onDataWritten(onDataWritten); + ble.onTimeout(timeoutCallback); + + /* setup device name */ + ble.setDeviceName((const uint8_t *)DEVICENAME); + + /* setup advertising */ + ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); + ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); + ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (const uint8_t *)DEVICENAME, sizeof(DEVICENAME)); + ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, + (const uint8_t *)MPU6050_service_uuid, sizeof(MPU6050_service_uuid)); + //(const uint8_t *)MPU6050_adv_service_uuid, sizeof(MPU6050_adv_service_uuid)); + + ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ + ble.startAdvertising(); + + ble.addService(MPU6050Service); + + while(true) { + //if (triggerSensorPolling && ble.getGapState().connected) { + //triggerSensorPolling = false; + updateValue(); + //} else { + // ble.waitForEvent(); + //} + } +} +