サイコン用プログラム BLE通信送信確認
Dependencies: mbed BLE_API nRF51822
Revision 22:500a89982568, committed 2019-01-09
- Comitter:
- taurin
- Date:
- Wed Jan 09 12:59:49 2019 +0000
- Parent:
- 21:600cf473d9e7
- Commit message:
- a
Changed in this revision
Cadence.h | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Cadence.h Wed Jan 09 12:59:49 2019 +0000 @@ -0,0 +1,42 @@ +#ifndef CADENCE_H +#define CADENCE_H + +#include "mbed.h" + +#define CADENCE_SUM_NUM 2 + +Timer t; + +char cadence_ave = 0; +char cadence_max = 0; +int cadence_counter = 0; +char cadence_sum[CADENCE_SUM_NUM] = {0}; + +class Cadence{ + protected: + + public: + char make_ave(char s[], int n){ + char p = 0; + int i; + for(i = 0; i < n; i++){ + p += s[i]; + } + if(n != 0) + return p / n; + else + return 0; + } + + void calcCadence(char x){ + cadence_sum[cadence_counter % CADENCE_SUM_NUM] = x; + if(cadence_counter % CADENCE_SUM_NUM == 0) + cadence_counter = 0; + cadence_ave = make_ave(cadence_sum, CADENCE_SUM_NUM); + //cadence=cadence_ave; + if(cadence_ave > cadence_max) + cadence_max = cadence_ave; + cadence_counter++; + } +}; +#endif \ No newline at end of file
--- a/main.cpp Sat Jun 27 13:26:34 2015 +0000 +++ b/main.cpp Wed Jan 09 12:59:49 2019 +0000 @@ -1,63 +1,56 @@ #include "mbed.h" #include "BLE.h" -#include "MPU6050.h" - -/* -#define DBG 1 -#define NEED_CONSOLE_OUTPUT 1 // Set this if you need debug messages on the console; - // it will have an impact on code-size and power consumption. +#include "Cadence.h" -#if NEED_CONSOLE_OUTPUT -Serial pc(USBTX, USBRX); -#define DEBUG(...) { pc.printf(__VA_ARGS__); } -#else -#define DEBUG(...) // nothing // -#endif // #if NEED_CONSOLE_OUTPUT // -*/ - -//#define MIN_CONN_INTERVAL 250 /**< Minimum connection interval */ -//#define MAX_CONN_INTERVAL 350 /**< Maximum connection interval */ #define CONN_INTERVAL 25 /**< connection interval 250ms; in multiples of 0.125ms. (durationInMillis * 1000) / UNIT_0_625_MS; */ #define CONN_SUP_TIMEOUT 8000 /**< Connection supervisory timeout (6 seconds); in multiples of 0.125ms. */ #define SLAVE_LATENCY 0 #define TICKER_INTERVAL 2.0f +#define CADENCE_SUM_NUM 2 + +#define CADENCE_LOOP_TIME 5 BLE ble; +Serial pc(USBTX,USBRX); -MPU6050 mpu(I2C_SDA0, I2C_SCL0); +InterruptIn CadencePin(p30); + +Ticker CadenceTicker; +Timer cadence_t; +char cadence_ave = 0; +char cadence_max = 0; +char cadence_sum[CADENCE_SUM_NUM] = {0}; +bool cadence_flag = true; +volatile int cadence_num = 0; + +void cadence_countUp(); +void call_calcCadence(); +void CadenceInit(); static const char DEVICENAME[] = "BLE-Nano"; static volatile bool triggerSensorPolling = false; -//const uint8_t MPU6050_adv_service_uuid[] = { -// 0x9F,0xDF,0x32,0x83, -// 0x90,0x49, -// 0xCF,0x8D, -// 0x5C,0x4D, -// 0x98,0xE7,0xE2,0x00,0x27,0x31 -//}; - const uint8_t MPU6050_service_uuid[] = { - 0x45,0x35,0x56,0x80,0x0F,0xD8,0x5F,0xB5,0x51,0x48,0x30,0x27,0x06,0x9B,0x3F,0xD9 + 0x90,0x0f,0xc9,0xd8,0xe5,0x98,0x11,0xe8,0x9f,0x32,0xf2,0x80,0x1f,0x1b,0x9f,0xd1 }; const uint8_t MPU6050_Accel_Characteristic_uuid[] = { - 0x45,0x35,0x56,0x81,0x0F,0xD8,0x5F,0xB5,0x51,0x48,0x30,0x27,0x06,0x9B,0x3F,0xD9 + 0x90,0x0f,0xd3,0x7e,0xe5,0x98,0x11,0xe8,0x9f,0x32,0xf2,0x80,0x1f,0x1b,0x9f,0xd1 }; const uint8_t MPU6050_Write_Characteristic_uuid[] = { - 0x45,0x35,0x56,0x83,0x0F,0xD8,0x5F,0xB5,0x51,0x48,0x30,0x27,0x06,0x9B,0x3F,0xD9 + 0x90,0x0f,0xd5,0x04,0xe5,0x98,0x11,0xe8,0x9f,0x32,0xf2,0x80,0x1f,0x1b,0x9f,0xd1 }; -uint8_t accelPayload[sizeof(float)*10] = {0,}; +uint8_t accelPayload[sizeof(char)*10] = {0,}; -uint8_t defaultWriteValue = 3; +uint8_t defaultWriteValue = 10; uint8_t writePayload[2] = {defaultWriteValue, defaultWriteValue,}; GattCharacteristic accelChar (MPU6050_Accel_Characteristic_uuid, - accelPayload, (sizeof(float) * 10), (sizeof(float) * 10), + accelPayload, (sizeof(char) * 10), (sizeof(char) * 10), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); GattCharacteristic writeChar (MPU6050_Write_Characteristic_uuid, @@ -70,59 +63,38 @@ void updateValue(void){ - float acData[3]; - float gyData[3]; - float tempData = 0.0f; - float at = 0.0f; - float gt = 0.0f; - float tickerInterval = 0.0f; + char acData[3]; + char gyData[3]; + char tempData = 0; + char at = 7; + char gt = 8; + char tickerInterval = 9; - //加速度を取得 - Timer acTimer; - acTimer.start(); - mpu.getAccelero(acData); - acTimer.stop(); - at = acTimer.read_ms(); - acTimer.reset(); + acData[0] = 0; + acData[1] = 100; + acData[2] = 2000; + + memcpy(accelPayload+sizeof(char)*0, &acData[0], sizeof(acData[0])); + memcpy(accelPayload+sizeof(char)*1, &acData[1], sizeof(acData[1])); + memcpy(accelPayload+sizeof(char)*2, &acData[2], sizeof(acData[2])); - memcpy(accelPayload+sizeof(float)*0, &acData[0], sizeof(acData[0])); - memcpy(accelPayload+sizeof(float)*1, &acData[1], sizeof(acData[1])); - memcpy(accelPayload+sizeof(float)*2, &acData[2], sizeof(acData[2])); + gyData[0] = 3; + gyData[1] = 4; + gyData[2] = 5; - //ジャイロを取得 - Timer gyTimer; - gyTimer.start(); - mpu.getGyro(gyData); - gyTimer.stop(); - gt = gyTimer.read_ms(); - gyTimer.reset(); - - memcpy(accelPayload+sizeof(float)*3, &gyData[0], sizeof(gyData[0])); - memcpy(accelPayload+sizeof(float)*4, &gyData[1], sizeof(gyData[1])); - memcpy(accelPayload+sizeof(float)*5, &gyData[2], sizeof(gyData[2])); + memcpy(accelPayload+sizeof(char)*3, &gyData[0], sizeof(gyData[0])); + memcpy(accelPayload+sizeof(char)*4, &gyData[1], sizeof(gyData[1])); + memcpy(accelPayload+sizeof(char)*5, &gyData[2], sizeof(gyData[2])); //温度を取得 - tempData = mpu.getTemp(); - - memcpy(accelPayload+sizeof(float)*6, &tempData, sizeof(tempData)); - /* - pc.printf("Accel: %.3lf,%.3lf,%.3lf\r\n", - *(float*)&accelPayload[sizeof(float)*0], - *(float*)&accelPayload[sizeof(float)*1], - *(float*)&accelPayload[sizeof(float)*2]); + //tempData = mpu.getTemp(); + tempData = cadence_ave; + memcpy(accelPayload+sizeof(char)*6, &tempData, sizeof(tempData)); + + memcpy(accelPayload+sizeof(char)*7, &at, sizeof(at)); + memcpy(accelPayload+sizeof(char)*8, >, sizeof(gt)); - pc.printf("Gyro: %.3lf,%.3lf,%.3lf\r\n", - *(float*)&accelPayload[sizeof(float)*3], - *(float*)&accelPayload[sizeof(float)*4], - *(float*)&accelPayload[sizeof(float)*5]); - - pc.printf("Temp: %.3lf\r\n", *(float*)&accelPayload[sizeof(float)*6]); - */ - memcpy(accelPayload+sizeof(float)*7, &at, sizeof(at)); - memcpy(accelPayload+sizeof(float)*8, >, sizeof(gt)); - - tickerInterval = TICKER_INTERVAL; - memcpy(accelPayload+sizeof(float)*9, &tickerInterval, sizeof(tickerInterval)); + memcpy(accelPayload+sizeof(char)*9, &tickerInterval, sizeof(tickerInterval)); ble.updateCharacteristicValue(accelChar.getValueAttribute().getHandle(), accelPayload, sizeof(accelPayload)); //Mod ble.updateCharacteristicValue(writeChar.getValueAttribute().getHandle(), writePayload, sizeof(writePayload)); //Mod @@ -160,63 +132,6 @@ } } -//void writtenCallback(const GattCharacteristicWriteCBParams *params){ -void writtenCallback(const GattWriteCallbackParams *params){ - char acceleroRange = 0xFF; - char gyroRange = 0xFF; - - if (params->handle == writeChar.getValueAttribute().getHandle()) { - uint16_t len = params->len; - - if (len == 2) { - uint8_t controller[2] = {0}; - - ble.readCharacteristicValue(writeChar.getValueAttribute().getHandle(), writePayload, &len); - memcpy(controller, writePayload, sizeof(controller)); - - //pc.printf("write: %u, %u, %u\r\n", controller[0],controller[1],controller[2]); - - switch(controller[0]){ - case 1: - acceleroRange = MPU6050_ACCELERO_RANGE_2G; - break; - case 2: - acceleroRange = MPU6050_ACCELERO_RANGE_4G; - break; - case 3: - acceleroRange = MPU6050_ACCELERO_RANGE_8G; - break; - case 4: - acceleroRange = MPU6050_ACCELERO_RANGE_16G; - - break; - default: - break; - } - - switch(controller[1]){ - case 1: - gyroRange = MPU6050_GYRO_RANGE_250; - break; - case 2: - gyroRange = MPU6050_GYRO_RANGE_500; - break; - case 3: - gyroRange = MPU6050_GYRO_RANGE_1000; - break; - case 4: - gyroRange = MPU6050_GYRO_RANGE_2000; - break; - default: - break; - } - mpu.mpu_set_accel_fsr(acceleroRange); - mpu.mpu_set_gyro_fsr(gyroRange); - - } - } -} - void timeoutCallback(const Gap::TimeoutSource_t source) { //DEBUG("TimeOut\n\r"); @@ -234,6 +149,28 @@ triggerSensorPolling = true; } + +void cadence_countUp(){ + if(cadence_flag){ + cadence_flag = false; + pc.printf("count_up!\n\r"); + cadence_num++; + wait(0.15); + cadence_flag = true; + } + +} + +void call_calcCadence(){ + cadence.calcCadence(cadence_num); + cadence_num = 0; +} + +void CadenceInit(){ + CadencePin.rise(cadence_countUp); + CadenceTicker.attach(&call_calcCadence, CADENCE_LOOP_TIME); +} + /**************************************************************************/ /*! @brief Program entry point @@ -241,32 +178,8 @@ /**************************************************************************/ int main(void) { + CadenceInit(); - //#if DBG - // pc.printf("Start\n\r"); - //#endif - /* - #define MPU6050_ACCELERO_RANGE_2G 1 - #define MPU6050_ACCELERO_RANGE_4G 2 - #define MPU6050_ACCELERO_RANGE_8G 3 - #define MPU6050_ACCELERO_RANGE_16G 4 - - #define MPU6050_GYRO_RANGE_250 1 - #define MPU6050_GYRO_RANGE_500 2 - #define MPU6050_GYRO_RANGE_1000 3 - #define MPU6050_GYRO_RANGE_2000 4 - */ - - mpu.initialize(); - mpu.setAcceleroRange(MPU6050_ACCELERO_RANGE_8G); - mpu.setGyroRange(MPU6050_GYRO_RANGE_1000); - - if( mpu.testConnection() ){ - //pc.printf("mpu test:OK\n\r"); - }else{ - //pc.printf("mpu test:NG\n\r"); - } - float ticker_ms = (TICKER_INTERVAL / 100.0f); Ticker ticker; ticker.attach(periodicCallback, ticker_ms);//0.02f //.2f-sec @@ -274,7 +187,7 @@ ble.init(); ble.onDisconnection(disconnectionCallback); ble.onConnection(connectionCallback); - ble.onDataWritten(writtenCallback); + // ble.onDataWritten(writtenCallback); ble.onTimeout(timeoutCallback); /* setup device name */