this is program how build nRF51822 to get ADXL345 data
Dependencies: BLE_API mbed nRF51822
Fork of ADXL345_I2C by
main.cpp@6:bc835d0f686f, 2017-12-06 (annotated)
- Committer:
- asyrofi
- Date:
- Wed Dec 06 03:10:08 2017 +0000
- Revision:
- 6:bc835d0f686f
- Parent:
- 5:8242f251bcac
- Child:
- 7:31412349a322
mantap;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
peterswanson87 | 0:d0adb548714f | 1 | #include "ADXL345_I2C.h" |
asyrofi | 2:99e00e9c5035 | 2 | #include "mbed.h" |
asyrofi | 2:99e00e9c5035 | 3 | #include "ble/BLE.h" |
asyrofi | 3:1749778af065 | 4 | #include "ble/services/UARTService.h" |
asyrofi | 3:1749778af065 | 5 | #include "Serial.h" |
asyrofi | 2:99e00e9c5035 | 6 | |
asyrofi | 3:1749778af065 | 7 | #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console; |
asyrofi | 2:99e00e9c5035 | 8 | * it will have an impact on code-size and power consumption. */ |
asyrofi | 2:99e00e9c5035 | 9 | |
asyrofi | 2:99e00e9c5035 | 10 | #if NEED_CONSOLE_OUTPUT |
asyrofi | 2:99e00e9c5035 | 11 | #define DEBUG(...) { printf(__VA_ARGS__); } |
asyrofi | 2:99e00e9c5035 | 12 | #else |
asyrofi | 2:99e00e9c5035 | 13 | #define DEBUG(...) /* nothing */ |
asyrofi | 2:99e00e9c5035 | 14 | #endif /* #if NEED_CONSOLE_OUTPUT */ |
peterswanson87 | 0:d0adb548714f | 15 | |
asyrofi | 4:a57b495be9fa | 16 | ADXL345_I2C accelerometer(p30, p7); |
asyrofi | 2:99e00e9c5035 | 17 | BLEDevice ble; |
asyrofi | 2:99e00e9c5035 | 18 | DigitalOut led1(LED1); |
asyrofi | 3:1749778af065 | 19 | Serial uart1(USBTX,USBRX); |
asyrofi | 2:99e00e9c5035 | 20 | UARTService *uartServicePtr; |
asyrofi | 6:bc835d0f686f | 21 | |
asyrofi | 6:bc835d0f686f | 22 | |
asyrofi | 6:bc835d0f686f | 23 | |
asyrofi | 2:99e00e9c5035 | 24 | |
asyrofi | 2:99e00e9c5035 | 25 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
asyrofi | 2:99e00e9c5035 | 26 | { |
asyrofi | 2:99e00e9c5035 | 27 | DEBUG("Disconnected!\n\r"); |
asyrofi | 2:99e00e9c5035 | 28 | DEBUG("Restarting the advertising process\n\r"); |
asyrofi | 2:99e00e9c5035 | 29 | ble.startAdvertising(); |
asyrofi | 2:99e00e9c5035 | 30 | } |
asyrofi | 2:99e00e9c5035 | 31 | |
asyrofi | 3:1749778af065 | 32 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) { |
asyrofi | 3:1749778af065 | 33 | |
asyrofi | 3:1749778af065 | 34 | DEBUG("Connected!\n\r"); |
asyrofi | 3:1749778af065 | 35 | |
asyrofi | 3:1749778af065 | 36 | } |
asyrofi | 3:1749778af065 | 37 | |
asyrofi | 3:1749778af065 | 38 | uint8_t b[40]={'a','d','q','w'}; |
asyrofi | 2:99e00e9c5035 | 39 | void onDataWritten(const GattWriteCallbackParams *params) |
asyrofi | 2:99e00e9c5035 | 40 | { |
asyrofi | 2:99e00e9c5035 | 41 | if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) { |
asyrofi | 2:99e00e9c5035 | 42 | uint16_t bytesRead = params->len; |
asyrofi | 3:1749778af065 | 43 | DEBUG("received %u bytes %s\n\r", bytesRead,params->data); |
asyrofi | 3:1749778af065 | 44 | /*uint16_t bytesRead = params->len; |
asyrofi | 2:99e00e9c5035 | 45 | DEBUG("received %u bytes\n\r", bytesRead); |
asyrofi | 3:1749778af065 | 46 | ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);*/ |
asyrofi | 2:99e00e9c5035 | 47 | } |
asyrofi | 2:99e00e9c5035 | 48 | } |
asyrofi | 2:99e00e9c5035 | 49 | |
asyrofi | 2:99e00e9c5035 | 50 | void periodicCallback(void) |
asyrofi | 2:99e00e9c5035 | 51 | { |
asyrofi | 2:99e00e9c5035 | 52 | led1 = !led1; |
asyrofi | 2:99e00e9c5035 | 53 | } |
asyrofi | 2:99e00e9c5035 | 54 | |
asyrofi | 6:bc835d0f686f | 55 | int readings[3] = {0, 0, 0}; |
asyrofi | 6:bc835d0f686f | 56 | int threshold =80; |
asyrofi | 6:bc835d0f686f | 57 | int xval[100]={0}; |
asyrofi | 6:bc835d0f686f | 58 | int yval[100]={0}; |
asyrofi | 6:bc835d0f686f | 59 | int zval[100]={0}; |
asyrofi | 6:bc835d0f686f | 60 | int xavg; |
asyrofi | 6:bc835d0f686f | 61 | int yavg; |
asyrofi | 6:bc835d0f686f | 62 | int zavg; |
asyrofi | 6:bc835d0f686f | 63 | int steps, flag=0; |
asyrofi | 6:bc835d0f686f | 64 | void calibrate(void) |
asyrofi | 5:8242f251bcac | 65 | { |
asyrofi | 6:bc835d0f686f | 66 | led1=1; |
asyrofi | 6:bc835d0f686f | 67 | int sum1 = 0; |
asyrofi | 6:bc835d0f686f | 68 | int sum2 = 0; |
asyrofi | 6:bc835d0f686f | 69 | int sum3 = 0; |
asyrofi | 6:bc835d0f686f | 70 | for (int i=0; i<100; i++) |
asyrofi | 6:bc835d0f686f | 71 | { |
asyrofi | 6:bc835d0f686f | 72 | xval[i]=(1); //nilai x adxl345 |
asyrofi | 6:bc835d0f686f | 73 | sum1 = xval[i]+sum1; |
asyrofi | 6:bc835d0f686f | 74 | } |
asyrofi | 6:bc835d0f686f | 75 | wait(0.1); |
asyrofi | 6:bc835d0f686f | 76 | xavg=sum1/100.0; |
asyrofi | 6:bc835d0f686f | 77 | uart1.printf("%i\n", (int16_t)xavg); //nilai hasil kalibrasi x |
asyrofi | 6:bc835d0f686f | 78 | |
asyrofi | 6:bc835d0f686f | 79 | |
asyrofi | 6:bc835d0f686f | 80 | for (int i=0; i<100; i++) |
asyrofi | 6:bc835d0f686f | 81 | { |
asyrofi | 6:bc835d0f686f | 82 | yval[i]=(1); //nilai y adxl345 |
asyrofi | 6:bc835d0f686f | 83 | sum2 = yval[i]+sum2; |
asyrofi | 6:bc835d0f686f | 84 | } |
asyrofi | 6:bc835d0f686f | 85 | wait(0.1); |
asyrofi | 6:bc835d0f686f | 86 | yavg=sum2/100.0; |
asyrofi | 6:bc835d0f686f | 87 | uart1.printf("%i\n", (int16_t)yavg); //nilai hasil kalibrasi y |
asyrofi | 6:bc835d0f686f | 88 | |
asyrofi | 6:bc835d0f686f | 89 | for (int i=0; i<100; i++) |
asyrofi | 6:bc835d0f686f | 90 | { |
asyrofi | 6:bc835d0f686f | 91 | zval[i]=(1); //nilai z adxl345 |
asyrofi | 6:bc835d0f686f | 92 | sum3 = zval[i]+sum3; |
asyrofi | 6:bc835d0f686f | 93 | } |
asyrofi | 6:bc835d0f686f | 94 | wait(0.1); |
asyrofi | 6:bc835d0f686f | 95 | zavg=sum3/100.0; |
asyrofi | 6:bc835d0f686f | 96 | uart1.printf("%i\n", (int16_t)zavg); //nilai hasil kalibrasi z |
asyrofi | 6:bc835d0f686f | 97 | led1=0; |
asyrofi | 5:8242f251bcac | 98 | } |
asyrofi | 5:8242f251bcac | 99 | |
asyrofi | 3:1749778af065 | 100 | |
asyrofi | 6:bc835d0f686f | 101 | int main() |
asyrofi | 6:bc835d0f686f | 102 | { |
asyrofi | 4:a57b495be9fa | 103 | uart1.baud(9600); |
peterswanson87 | 0:d0adb548714f | 104 | int readings[3] = {0, 0, 0}; |
asyrofi | 6:bc835d0f686f | 105 | calibrate(); |
asyrofi | 4:a57b495be9fa | 106 | uart1.printf("Starting ADXL345 test...\n"); |
peterswanson87 | 0:d0adb548714f | 107 | wait(.001); |
asyrofi | 4:a57b495be9fa | 108 | uart1.printf("Device ID is: 0x%02x\n", accelerometer.getDeviceID()); |
asyrofi | 3:1749778af065 | 109 | wait(.001); |
asyrofi | 6:bc835d0f686f | 110 | |
peterswanson87 | 0:d0adb548714f | 111 | |
asyrofi | 2:99e00e9c5035 | 112 | //Go into standby mode to configure the device. |
asyrofi | 2:99e00e9c5035 | 113 | accelerometer.setPowerControl(0x00); |
asyrofi | 2:99e00e9c5035 | 114 | |
asyrofi | 2:99e00e9c5035 | 115 | //Full resolution, +/-16g, 4mg/LSB. |
asyrofi | 2:99e00e9c5035 | 116 | accelerometer.setDataFormatControl(0x0B); |
asyrofi | 2:99e00e9c5035 | 117 | |
asyrofi | 2:99e00e9c5035 | 118 | //3.2kHz data rate. |
asyrofi | 2:99e00e9c5035 | 119 | accelerometer.setDataRate(ADXL345_3200HZ); |
asyrofi | 2:99e00e9c5035 | 120 | |
asyrofi | 2:99e00e9c5035 | 121 | //Measurement mode. |
asyrofi | 2:99e00e9c5035 | 122 | accelerometer.setPowerControl(0x08); |
asyrofi | 2:99e00e9c5035 | 123 | |
asyrofi | 6:bc835d0f686f | 124 | int acc=0; |
asyrofi | 6:bc835d0f686f | 125 | int totvect [100] = {0}; |
asyrofi | 6:bc835d0f686f | 126 | int totave [100] = {0}; |
asyrofi | 6:bc835d0f686f | 127 | int totvel [100] = {0}; |
asyrofi | 6:bc835d0f686f | 128 | int totdist [100] = {0}; |
asyrofi | 6:bc835d0f686f | 129 | int totheight [100] = {0}; |
asyrofi | 6:bc835d0f686f | 130 | |
asyrofi | 6:bc835d0f686f | 131 | |
asyrofi | 6:bc835d0f686f | 132 | //float sum1, sum2, sum3 = 0 |
asyrofi | 6:bc835d0f686f | 133 | double xaccl[100] = {0}; |
asyrofi | 6:bc835d0f686f | 134 | double yaccl[100] = {0}; |
asyrofi | 6:bc835d0f686f | 135 | double zaccl[100] = {0}; |
asyrofi | 6:bc835d0f686f | 136 | |
asyrofi | 6:bc835d0f686f | 137 | //float x,y,z |
asyrofi | 6:bc835d0f686f | 138 | for (int i=0; i<100; i++) |
asyrofi | 6:bc835d0f686f | 139 | { |
asyrofi | 6:bc835d0f686f | 140 | xaccl[i]=(1); |
asyrofi | 6:bc835d0f686f | 141 | wait(0.1); |
asyrofi | 6:bc835d0f686f | 142 | yaccl[i]=(1); |
asyrofi | 6:bc835d0f686f | 143 | wait(0.1); |
asyrofi | 6:bc835d0f686f | 144 | zaccl[i]=(1); |
asyrofi | 6:bc835d0f686f | 145 | wait(0.1); |
asyrofi | 6:bc835d0f686f | 146 | |
asyrofi | 6:bc835d0f686f | 147 | //formula |
asyrofi | 6:bc835d0f686f | 148 | |
asyrofi | 6:bc835d0f686f | 149 | totvect[i] = sqrt(((xaccl[i]-xavg)* (xaccl[i]-xavg))+ ((yaccl[i] - yavg)*(yaccl[i] - yavg)) + ((zaccl[i] - zavg)*(zaccl[i] - zavg))); |
asyrofi | 6:bc835d0f686f | 150 | totave[i] = (totvect[i] + totvect[i-1]) / 2 ; |
asyrofi | 6:bc835d0f686f | 151 | totvel[i]=(totave[i]*0.2); |
asyrofi | 6:bc835d0f686f | 152 | //totave1[i]=(totvel[i]+totvel[i-1])/2; |
asyrofi | 6:bc835d0f686f | 153 | totdist[i]=(totvect[i]*(0.2*0.2)/2); |
asyrofi | 6:bc835d0f686f | 154 | totheight[i]=sqrt((((xaccl[i]-xavg)* (xaccl[i]-xavg))+ ((yaccl[i] - yavg)*(yaccl[i] - yavg)))*(0.2*0.2)/2); |
asyrofi | 6:bc835d0f686f | 155 | //totave3[i]=((totheight[i]+totheight[i-1])/2); |
asyrofi | 6:bc835d0f686f | 156 | acc=acc+totave[i]; |
asyrofi | 6:bc835d0f686f | 157 | |
asyrofi | 6:bc835d0f686f | 158 | uart1.printf("%i\n", (int16_t)totave[i]); |
asyrofi | 6:bc835d0f686f | 159 | uart1.printf("%i\n", (int16_t)totvel[i]); |
asyrofi | 6:bc835d0f686f | 160 | uart1.printf("%i\n", (int16_t)totdist[i]); |
asyrofi | 6:bc835d0f686f | 161 | uart1.printf("%i\n", (int16_t)totheight[i]); |
asyrofi | 6:bc835d0f686f | 162 | |
asyrofi | 6:bc835d0f686f | 163 | wait(0.1); |
asyrofi | 6:bc835d0f686f | 164 | |
asyrofi | 6:bc835d0f686f | 165 | if (steps == 10) |
asyrofi | 6:bc835d0f686f | 166 | { |
asyrofi | 6:bc835d0f686f | 167 | uart1.printf("capek"); |
asyrofi | 6:bc835d0f686f | 168 | } |
asyrofi | 6:bc835d0f686f | 169 | |
asyrofi | 6:bc835d0f686f | 170 | else |
asyrofi | 6:bc835d0f686f | 171 | { |
asyrofi | 6:bc835d0f686f | 172 | //nothing |
asyrofi | 6:bc835d0f686f | 173 | } |
asyrofi | 6:bc835d0f686f | 174 | |
asyrofi | 6:bc835d0f686f | 175 | //cal steps |
asyrofi | 6:bc835d0f686f | 176 | if (totave[i] > threshold && flag==0) |
asyrofi | 6:bc835d0f686f | 177 | { |
asyrofi | 6:bc835d0f686f | 178 | steps = steps+1; |
asyrofi | 6:bc835d0f686f | 179 | flag=1; |
asyrofi | 6:bc835d0f686f | 180 | } |
asyrofi | 6:bc835d0f686f | 181 | else if (totave[i] > threshold && flag == 1) |
asyrofi | 6:bc835d0f686f | 182 | { |
asyrofi | 6:bc835d0f686f | 183 | // do nothing |
asyrofi | 6:bc835d0f686f | 184 | } |
asyrofi | 6:bc835d0f686f | 185 | if (totave[i] < threshold && flag == 1) |
asyrofi | 6:bc835d0f686f | 186 | {flag=0;} |
asyrofi | 6:bc835d0f686f | 187 | uart1.printf("%i\n", (int16_t)steps); |
asyrofi | 6:bc835d0f686f | 188 | |
asyrofi | 6:bc835d0f686f | 189 | |
asyrofi | 6:bc835d0f686f | 190 | } |
asyrofi | 6:bc835d0f686f | 191 | |
asyrofi | 6:bc835d0f686f | 192 | |
asyrofi | 2:99e00e9c5035 | 193 | led1 = 1; |
asyrofi | 3:1749778af065 | 194 | uart1.baud(9600); |
asyrofi | 2:99e00e9c5035 | 195 | Ticker ticker; |
asyrofi | 2:99e00e9c5035 | 196 | ticker.attach(periodicCallback, 1); |
asyrofi | 6:bc835d0f686f | 197 | |
asyrofi | 4:a57b495be9fa | 198 | |
asyrofi | 4:a57b495be9fa | 199 | |
asyrofi | 2:99e00e9c5035 | 200 | DEBUG("Initialising the nRF51822\n\r"); |
asyrofi | 2:99e00e9c5035 | 201 | ble.init(); |
asyrofi | 2:99e00e9c5035 | 202 | ble.onDisconnection(disconnectionCallback); |
asyrofi | 3:1749778af065 | 203 | ble.onConnection(connectionCallback); |
asyrofi | 2:99e00e9c5035 | 204 | ble.onDataWritten(onDataWritten); |
asyrofi | 2:99e00e9c5035 | 205 | |
asyrofi | 2:99e00e9c5035 | 206 | /* setup advertising */ |
asyrofi | 2:99e00e9c5035 | 207 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
asyrofi | 2:99e00e9c5035 | 208 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
asyrofi | 2:99e00e9c5035 | 209 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
asyrofi | 2:99e00e9c5035 | 210 | (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1); |
asyrofi | 2:99e00e9c5035 | 211 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
asyrofi | 2:99e00e9c5035 | 212 | (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
asyrofi | 2:99e00e9c5035 | 213 | |
asyrofi | 2:99e00e9c5035 | 214 | ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */ |
asyrofi | 2:99e00e9c5035 | 215 | ble.startAdvertising(); |
asyrofi | 2:99e00e9c5035 | 216 | |
asyrofi | 2:99e00e9c5035 | 217 | UARTService uartService(ble); |
asyrofi | 2:99e00e9c5035 | 218 | uartServicePtr = &uartService; |
peterswanson87 | 0:d0adb548714f | 219 | |
asyrofi | 4:a57b495be9fa | 220 | while (1) |
asyrofi | 4:a57b495be9fa | 221 | { |
asyrofi | 2:99e00e9c5035 | 222 | ble.waitForEvent(); |
peterswanson87 | 0:d0adb548714f | 223 | wait(0.1); |
peterswanson87 | 0:d0adb548714f | 224 | accelerometer.getOutput(readings); |
asyrofi | 5:8242f251bcac | 225 | uart1.printf("%i, %i, %i\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]); |
asyrofi | 6:bc835d0f686f | 226 | ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)readings[0]/*params->data*/, sizeof(readings),false/*bytesRead*/); |
asyrofi | 6:bc835d0f686f | 227 | ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)readings[1]/*params->data*/, sizeof(readings),false/*bytesRead*/); |
asyrofi | 6:bc835d0f686f | 228 | ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)readings[2]/*params->data*/, sizeof(readings),false/*bytesRead*/); |
asyrofi | 6:bc835d0f686f | 229 | |
asyrofi | 6:bc835d0f686f | 230 | } |
peterswanson87 | 0:d0adb548714f | 231 | |
peterswanson87 | 0:d0adb548714f | 232 | } |
asyrofi | 6:bc835d0f686f | 233 |