![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
get the accelaration and judge the moving state then broadcast by BLE
Dependencies: AE_KXSD9 BLE_API mbed nRF51822
main.cpp@0:de01d2d04e9d, 2016-06-09 (annotated)
- Committer:
- MonroeLee
- Date:
- Thu Jun 09 05:18:41 2016 +0000
- Revision:
- 0:de01d2d04e9d
get the accelaration and judge the moving state then broadcast by BLE
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MonroeLee | 0:de01d2d04e9d | 1 | #include "mbed.h" |
MonroeLee | 0:de01d2d04e9d | 2 | #include "BLEDevice.h" |
MonroeLee | 0:de01d2d04e9d | 3 | #include "AE_KXSD9.h" |
MonroeLee | 0:de01d2d04e9d | 4 | #include "nRF51822n.h" |
MonroeLee | 0:de01d2d04e9d | 5 | |
MonroeLee | 0:de01d2d04e9d | 6 | #define ADDR_W 0x30 |
MonroeLee | 0:de01d2d04e9d | 7 | #define ADDR_R 0x31 |
MonroeLee | 0:de01d2d04e9d | 8 | |
MonroeLee | 0:de01d2d04e9d | 9 | #define DBG 1 |
MonroeLee | 0:de01d2d04e9d | 10 | #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console; |
MonroeLee | 0:de01d2d04e9d | 11 | * it will have an impact on code-size and power consumption. */ |
MonroeLee | 0:de01d2d04e9d | 12 | |
MonroeLee | 0:de01d2d04e9d | 13 | #if NEED_CONSOLE_OUTPUT |
MonroeLee | 0:de01d2d04e9d | 14 | Serial pc(USBTX, USBRX); |
MonroeLee | 0:de01d2d04e9d | 15 | #define DEBUG(...) { pc.printf(__VA_ARGS__); } |
MonroeLee | 0:de01d2d04e9d | 16 | #else |
MonroeLee | 0:de01d2d04e9d | 17 | #define DEBUG(...) /* nothing */ |
MonroeLee | 0:de01d2d04e9d | 18 | #endif /* #if NEED_CONSOLE_OUTPUT */ |
MonroeLee | 0:de01d2d04e9d | 19 | |
MonroeLee | 0:de01d2d04e9d | 20 | BLEDevice ble; |
MonroeLee | 0:de01d2d04e9d | 21 | |
MonroeLee | 0:de01d2d04e9d | 22 | //AE_KXSD9 kxsd9(PinName sda, PinName scl, int addr1, int addr2); /* KXSD9 */ |
MonroeLee | 0:de01d2d04e9d | 23 | AE_KXSD9 i2c(p22, p20, ADDR_W, ADDR_R); //sda, scl, addr_w, addr_r |
MonroeLee | 0:de01d2d04e9d | 24 | |
MonroeLee | 0:de01d2d04e9d | 25 | |
MonroeLee | 0:de01d2d04e9d | 26 | static const char DEVICENAME[] = "HRM1017"; |
MonroeLee | 0:de01d2d04e9d | 27 | static volatile bool triggerSensorPolling = false; |
MonroeLee | 0:de01d2d04e9d | 28 | |
MonroeLee | 0:de01d2d04e9d | 29 | const uint8_t KXSD9_service_uuid[] = { |
MonroeLee | 0:de01d2d04e9d | 30 | 0x31,0x27,0x00,0xE2,0xE7,0x98, |
MonroeLee | 0:de01d2d04e9d | 31 | 0x4D,0x5C, |
MonroeLee | 0:de01d2d04e9d | 32 | 0x8D,0xCF, |
MonroeLee | 0:de01d2d04e9d | 33 | 0x49,0x90,0x83,0x32,0xDF,0x9F |
MonroeLee | 0:de01d2d04e9d | 34 | }; |
MonroeLee | 0:de01d2d04e9d | 35 | |
MonroeLee | 0:de01d2d04e9d | 36 | const uint8_t KXSD9_Characteristic_uuid[] = { |
MonroeLee | 0:de01d2d04e9d | 37 | 0xFF,0xA2,0x8C,0xDE,0x65,0x25, |
MonroeLee | 0:de01d2d04e9d | 38 | 0x44,0x89, |
MonroeLee | 0:de01d2d04e9d | 39 | 0x80,0x1C, |
MonroeLee | 0:de01d2d04e9d | 40 | 0x1C,0x06,0x0C,0xAC,0x97,0x67 |
MonroeLee | 0:de01d2d04e9d | 41 | }; |
MonroeLee | 0:de01d2d04e9d | 42 | |
MonroeLee | 0:de01d2d04e9d | 43 | |
MonroeLee | 0:de01d2d04e9d | 44 | uint8_t thermTempPayload[sizeof(double)*4] = {0,0,0,0}; |
MonroeLee | 0:de01d2d04e9d | 45 | |
MonroeLee | 0:de01d2d04e9d | 46 | GattCharacteristic ControllerChar (KXSD9_Characteristic_uuid, |
MonroeLee | 0:de01d2d04e9d | 47 | thermTempPayload, (sizeof(double) * 4), (sizeof(double) * 4), |
MonroeLee | 0:de01d2d04e9d | 48 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
MonroeLee | 0:de01d2d04e9d | 49 | |
MonroeLee | 0:de01d2d04e9d | 50 | GattCharacteristic *ControllerChars[] = { &ControllerChar, }; |
MonroeLee | 0:de01d2d04e9d | 51 | GattService KXSD9Service(KXSD9_service_uuid, ControllerChars, sizeof(ControllerChars) / sizeof(GattCharacteristic *)); |
MonroeLee | 0:de01d2d04e9d | 52 | |
MonroeLee | 0:de01d2d04e9d | 53 | static Gap::ConnectionParams_t connectionParams; |
MonroeLee | 0:de01d2d04e9d | 54 | |
MonroeLee | 0:de01d2d04e9d | 55 | void updateValue(); |
MonroeLee | 0:de01d2d04e9d | 56 | |
MonroeLee | 0:de01d2d04e9d | 57 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) // Mod |
MonroeLee | 0:de01d2d04e9d | 58 | { |
MonroeLee | 0:de01d2d04e9d | 59 | |
MonroeLee | 0:de01d2d04e9d | 60 | DEBUG("Disconnected handle %u, reason %u\n", handle, reason); |
MonroeLee | 0:de01d2d04e9d | 61 | DEBUG("Restarting the advertising process\n\r"); |
MonroeLee | 0:de01d2d04e9d | 62 | ble.startAdvertising(); |
MonroeLee | 0:de01d2d04e9d | 63 | } |
MonroeLee | 0:de01d2d04e9d | 64 | |
MonroeLee | 0:de01d2d04e9d | 65 | void onConnectionCallback(Gap::Handle_t handle, const Gap::ConnectionParams_t *params) //Mod |
MonroeLee | 0:de01d2d04e9d | 66 | { |
MonroeLee | 0:de01d2d04e9d | 67 | |
MonroeLee | 0:de01d2d04e9d | 68 | DEBUG("connected. Got handle %u\r\n", handle); |
MonroeLee | 0:de01d2d04e9d | 69 | |
MonroeLee | 0:de01d2d04e9d | 70 | connectionParams.slaveLatency = 1; |
MonroeLee | 0:de01d2d04e9d | 71 | if (ble.updateConnectionParams(handle, &connectionParams) != BLE_ERROR_NONE) { |
MonroeLee | 0:de01d2d04e9d | 72 | DEBUG("failed to update connection paramter\r\n"); |
MonroeLee | 0:de01d2d04e9d | 73 | } |
MonroeLee | 0:de01d2d04e9d | 74 | } |
MonroeLee | 0:de01d2d04e9d | 75 | |
MonroeLee | 0:de01d2d04e9d | 76 | void periodicCallback(void) |
MonroeLee | 0:de01d2d04e9d | 77 | { |
MonroeLee | 0:de01d2d04e9d | 78 | //oneSecondLed = !oneSecondLed; /* Do blinky on LED1 while we're waiting for BLE events */ |
MonroeLee | 0:de01d2d04e9d | 79 | |
MonroeLee | 0:de01d2d04e9d | 80 | /* Note that the periodicCallback() executes in interrupt context, so it is safer to do |
MonroeLee | 0:de01d2d04e9d | 81 | * heavy-weight sensor polling from the main thread. */ |
MonroeLee | 0:de01d2d04e9d | 82 | triggerSensorPolling = true; |
MonroeLee | 0:de01d2d04e9d | 83 | } |
MonroeLee | 0:de01d2d04e9d | 84 | /**************************************************************************/ |
MonroeLee | 0:de01d2d04e9d | 85 | /*! |
MonroeLee | 0:de01d2d04e9d | 86 | @brief Program entry point |
MonroeLee | 0:de01d2d04e9d | 87 | */ |
MonroeLee | 0:de01d2d04e9d | 88 | /**************************************************************************/ |
MonroeLee | 0:de01d2d04e9d | 89 | int main(void) |
MonroeLee | 0:de01d2d04e9d | 90 | { |
MonroeLee | 0:de01d2d04e9d | 91 | //pc.baud(115200); |
MonroeLee | 0:de01d2d04e9d | 92 | |
MonroeLee | 0:de01d2d04e9d | 93 | #if DBG |
MonroeLee | 0:de01d2d04e9d | 94 | pc.printf("Start\n\r"); |
MonroeLee | 0:de01d2d04e9d | 95 | #endif |
MonroeLee | 0:de01d2d04e9d | 96 | Ticker ticker; |
MonroeLee | 0:de01d2d04e9d | 97 | ticker.attach(periodicCallback, 1); |
MonroeLee | 0:de01d2d04e9d | 98 | |
MonroeLee | 0:de01d2d04e9d | 99 | i2c.init(); |
MonroeLee | 0:de01d2d04e9d | 100 | |
MonroeLee | 0:de01d2d04e9d | 101 | ble.init(); |
MonroeLee | 0:de01d2d04e9d | 102 | ble.onDisconnection(disconnectionCallback); |
MonroeLee | 0:de01d2d04e9d | 103 | ble.onConnection(onConnectionCallback); |
MonroeLee | 0:de01d2d04e9d | 104 | |
MonroeLee | 0:de01d2d04e9d | 105 | ble.getPreferredConnectionParams(&connectionParams); |
MonroeLee | 0:de01d2d04e9d | 106 | |
MonroeLee | 0:de01d2d04e9d | 107 | /* setup advertising */ |
MonroeLee | 0:de01d2d04e9d | 108 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
MonroeLee | 0:de01d2d04e9d | 109 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
MonroeLee | 0:de01d2d04e9d | 110 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)DEVICENAME, sizeof(DEVICENAME)); |
MonroeLee | 0:de01d2d04e9d | 111 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
MonroeLee | 0:de01d2d04e9d | 112 | (const uint8_t *)KXSD9_service_uuid, sizeof(KXSD9_service_uuid)); |
MonroeLee | 0:de01d2d04e9d | 113 | |
MonroeLee | 0:de01d2d04e9d | 114 | ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ |
MonroeLee | 0:de01d2d04e9d | 115 | ble.startAdvertising(); |
MonroeLee | 0:de01d2d04e9d | 116 | |
MonroeLee | 0:de01d2d04e9d | 117 | ble.addService(KXSD9Service); |
MonroeLee | 0:de01d2d04e9d | 118 | |
MonroeLee | 0:de01d2d04e9d | 119 | for(;;) { |
MonroeLee | 0:de01d2d04e9d | 120 | if (triggerSensorPolling) { |
MonroeLee | 0:de01d2d04e9d | 121 | triggerSensorPolling = false; |
MonroeLee | 0:de01d2d04e9d | 122 | updateValue(); |
MonroeLee | 0:de01d2d04e9d | 123 | } else { |
MonroeLee | 0:de01d2d04e9d | 124 | ble.waitForEvent(); |
MonroeLee | 0:de01d2d04e9d | 125 | } |
MonroeLee | 0:de01d2d04e9d | 126 | } |
MonroeLee | 0:de01d2d04e9d | 127 | } |
MonroeLee | 0:de01d2d04e9d | 128 | |
MonroeLee | 0:de01d2d04e9d | 129 | double Judge_state(double a[128]) |
MonroeLee | 0:de01d2d04e9d | 130 | { |
MonroeLee | 0:de01d2d04e9d | 131 | double state=0; |
MonroeLee | 0:de01d2d04e9d | 132 | int i; |
MonroeLee | 0:de01d2d04e9d | 133 | double sum0=0; |
MonroeLee | 0:de01d2d04e9d | 134 | double sum1=0; |
MonroeLee | 0:de01d2d04e9d | 135 | double avg=0; |
MonroeLee | 0:de01d2d04e9d | 136 | double M=0; |
MonroeLee | 0:de01d2d04e9d | 137 | int n=128; |
MonroeLee | 0:de01d2d04e9d | 138 | double LowChecker=1000;//区分静止和行走的界限 |
MonroeLee | 0:de01d2d04e9d | 139 | double HighChecker=10000;//区分行走和跑步的界限 |
MonroeLee | 0:de01d2d04e9d | 140 | for(i=0;i<n;i++) |
MonroeLee | 0:de01d2d04e9d | 141 | { |
MonroeLee | 0:de01d2d04e9d | 142 | sum0+=a[i]; |
MonroeLee | 0:de01d2d04e9d | 143 | } |
MonroeLee | 0:de01d2d04e9d | 144 | avg=sum0/n; |
MonroeLee | 0:de01d2d04e9d | 145 | for(i=0;i<n;i++) |
MonroeLee | 0:de01d2d04e9d | 146 | { |
MonroeLee | 0:de01d2d04e9d | 147 | sum1+=abs(a[i]-avg); |
MonroeLee | 0:de01d2d04e9d | 148 | } |
MonroeLee | 0:de01d2d04e9d | 149 | M=sum1/n; |
MonroeLee | 0:de01d2d04e9d | 150 | if(M>=0&&M<LowChecker) |
MonroeLee | 0:de01d2d04e9d | 151 | { |
MonroeLee | 0:de01d2d04e9d | 152 | state=1.0; |
MonroeLee | 0:de01d2d04e9d | 153 | } |
MonroeLee | 0:de01d2d04e9d | 154 | else if(M>=LowChecker&&M<HighChecker) |
MonroeLee | 0:de01d2d04e9d | 155 | { |
MonroeLee | 0:de01d2d04e9d | 156 | state=2.0; |
MonroeLee | 0:de01d2d04e9d | 157 | } |
MonroeLee | 0:de01d2d04e9d | 158 | else state=3.0; |
MonroeLee | 0:de01d2d04e9d | 159 | return state; |
MonroeLee | 0:de01d2d04e9d | 160 | } |
MonroeLee | 0:de01d2d04e9d | 161 | |
MonroeLee | 0:de01d2d04e9d | 162 | void updateValue(void){ |
MonroeLee | 0:de01d2d04e9d | 163 | |
MonroeLee | 0:de01d2d04e9d | 164 | double x = 0.0; |
MonroeLee | 0:de01d2d04e9d | 165 | double y = 0.0; |
MonroeLee | 0:de01d2d04e9d | 166 | double z = 0.0; |
MonroeLee | 0:de01d2d04e9d | 167 | double s=0; |
MonroeLee | 0:de01d2d04e9d | 168 | int i=0; |
MonroeLee | 0:de01d2d04e9d | 169 | double s_a; //合成的一维加速度 |
MonroeLee | 0:de01d2d04e9d | 170 | double a[128];//用于存放128个一维加速度取样值 |
MonroeLee | 0:de01d2d04e9d | 171 | i2c.read_xyz(&x, &y, &z); |
MonroeLee | 0:de01d2d04e9d | 172 | //pc.printf("%.3lf,%.3lf,%.3lf\r\n", x, y, z); |
MonroeLee | 0:de01d2d04e9d | 173 | |
MonroeLee | 0:de01d2d04e9d | 174 | s_a=sqrt(x*x+y*y+z*z); |
MonroeLee | 0:de01d2d04e9d | 175 | a[i]=s_a-55602; |
MonroeLee | 0:de01d2d04e9d | 176 | i=i+1; |
MonroeLee | 0:de01d2d04e9d | 177 | if(i==127) |
MonroeLee | 0:de01d2d04e9d | 178 | { |
MonroeLee | 0:de01d2d04e9d | 179 | s=Judge_state(a); |
MonroeLee | 0:de01d2d04e9d | 180 | i=0; |
MonroeLee | 0:de01d2d04e9d | 181 | //printf("Dx=:%lf\r\n",x); |
MonroeLee | 0:de01d2d04e9d | 182 | //printf("Dy=:%lf\r\n",y); |
MonroeLee | 0:de01d2d04e9d | 183 | //printf("Dz=:%lf\r\n",z); |
MonroeLee | 0:de01d2d04e9d | 184 | //printf("Dz=:%lf\r\n",s); |
MonroeLee | 0:de01d2d04e9d | 185 | } |
MonroeLee | 0:de01d2d04e9d | 186 | |
MonroeLee | 0:de01d2d04e9d | 187 | |
MonroeLee | 0:de01d2d04e9d | 188 | memcpy(thermTempPayload+sizeof(double)*0, &x, sizeof(x)); |
MonroeLee | 0:de01d2d04e9d | 189 | |
MonroeLee | 0:de01d2d04e9d | 190 | memcpy(thermTempPayload+sizeof(double)*1, &y, sizeof(y)); |
MonroeLee | 0:de01d2d04e9d | 191 | |
MonroeLee | 0:de01d2d04e9d | 192 | memcpy(thermTempPayload+sizeof(double)*2, &z, sizeof(z)); |
MonroeLee | 0:de01d2d04e9d | 193 | |
MonroeLee | 0:de01d2d04e9d | 194 | memcpy(thermTempPayload+sizeof(double)*3, &s, sizeof(s)); |
MonroeLee | 0:de01d2d04e9d | 195 | |
MonroeLee | 0:de01d2d04e9d | 196 | pc.printf("%.3lf,%.3lf,%.3lf,%.3lf\r\n", |
MonroeLee | 0:de01d2d04e9d | 197 | *(double*)&thermTempPayload[sizeof(double)*0], |
MonroeLee | 0:de01d2d04e9d | 198 | *(double*)&thermTempPayload[sizeof(double)*1], |
MonroeLee | 0:de01d2d04e9d | 199 | *(double*)&thermTempPayload[sizeof(double)*2], |
MonroeLee | 0:de01d2d04e9d | 200 | *(double*)&thermTempPayload[sizeof(double)*3]); |
MonroeLee | 0:de01d2d04e9d | 201 | ble.updateCharacteristicValue(ControllerChar.getValueAttribute().getHandle(), thermTempPayload, sizeof(thermTempPayload)); //Mod |
MonroeLee | 0:de01d2d04e9d | 202 | } |