this is program how build nRF51822 to get ADXL345 data

Dependencies:   BLE_API mbed nRF51822

Fork of ADXL345_I2C by Peter Swanson

Committer:
asyrofi
Date:
Tue Dec 12 16:12:22 2017 +0000
Revision:
15:a24cfc193284
Parent:
14:afe556cc0a6c
Child:
16:e97016b6a7e0
mantaplah

Who changed what in which revision?

UserRevisionLine numberNew contents of line
asyrofi 12:58823c0cdf99 1 #include "mbed.h"
peterswanson87 0:d0adb548714f 2 #include "ADXL345_I2C.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 2:99e00e9c5035 44 }
asyrofi 2:99e00e9c5035 45 }
asyrofi 2:99e00e9c5035 46
asyrofi 2:99e00e9c5035 47 void periodicCallback(void)
asyrofi 2:99e00e9c5035 48 {
asyrofi 2:99e00e9c5035 49 led1 = !led1;
asyrofi 2:99e00e9c5035 50 }
asyrofi 10:e5ee9515f4a2 51
asyrofi 6:bc835d0f686f 52 int threshold =80;
asyrofi 11:a60e398f9032 53 int xval[10]={0};
asyrofi 11:a60e398f9032 54 int yval[10]={0};
asyrofi 11:a60e398f9032 55 int zval[10]={0};
asyrofi 6:bc835d0f686f 56 int xavg;
asyrofi 6:bc835d0f686f 57 int yavg;
asyrofi 6:bc835d0f686f 58 int zavg;
asyrofi 10:e5ee9515f4a2 59
asyrofi 10:e5ee9515f4a2 60
asyrofi 10:e5ee9515f4a2 61 int calibratex()
asyrofi 5:8242f251bcac 62 {
asyrofi 10:e5ee9515f4a2 63 int readings[3] = {0, 0, 0};
asyrofi 6:bc835d0f686f 64 led1=1;
asyrofi 15:a24cfc193284 65 int sum1 = 0;
asyrofi 11:a60e398f9032 66 for (int i=0; i<10; i++)
asyrofi 6:bc835d0f686f 67 {
asyrofi 10:e5ee9515f4a2 68 accelerometer.getOutput(readings);
asyrofi 10:e5ee9515f4a2 69 xval[i]=(readings[0]); //nilai x adxl345
asyrofi 14:afe556cc0a6c 70 sum1 = xval[i]+sum1;
asyrofi 6:bc835d0f686f 71 }
asyrofi 10:e5ee9515f4a2 72
asyrofi 15:a24cfc193284 73 xavg=sum1/10.0;
asyrofi 10:e5ee9515f4a2 74
asyrofi 10:e5ee9515f4a2 75 led1=0;
asyrofi 10:e5ee9515f4a2 76 return xavg;
asyrofi 10:e5ee9515f4a2 77 }
asyrofi 10:e5ee9515f4a2 78 int calibratey()
asyrofi 10:e5ee9515f4a2 79 {
asyrofi 10:e5ee9515f4a2 80 int readings[3] = {0, 0, 0};
asyrofi 10:e5ee9515f4a2 81 led1=1;
asyrofi 10:e5ee9515f4a2 82 long int sum2 = 0;
asyrofi 11:a60e398f9032 83 for (int i=0; i<10; i++)
asyrofi 6:bc835d0f686f 84 {
asyrofi 10:e5ee9515f4a2 85 accelerometer.getOutput(readings);
asyrofi 10:e5ee9515f4a2 86 yval[i]=(readings[1]); //nilai y adxl345
asyrofi 6:bc835d0f686f 87 sum2 = yval[i]+sum2;
asyrofi 6:bc835d0f686f 88 }
asyrofi 11:a60e398f9032 89 yavg=sum2/10.0;
asyrofi 10:e5ee9515f4a2 90 led1=0;
asyrofi 10:e5ee9515f4a2 91 return yavg;
asyrofi 10:e5ee9515f4a2 92 }
asyrofi 10:e5ee9515f4a2 93 int calibratez()
asyrofi 10:e5ee9515f4a2 94 {
asyrofi 10:e5ee9515f4a2 95 int readings[3] = {0, 0, 0};
asyrofi 10:e5ee9515f4a2 96
asyrofi 10:e5ee9515f4a2 97 led1=1;
asyrofi 10:e5ee9515f4a2 98 long int sum3 = 0;
asyrofi 11:a60e398f9032 99 for (int i=0; i<10; i++)
asyrofi 6:bc835d0f686f 100 {
asyrofi 10:e5ee9515f4a2 101 accelerometer.getOutput(readings);
asyrofi 10:e5ee9515f4a2 102 zval[i]=(readings[2]); //nilai z adxl345
asyrofi 6:bc835d0f686f 103 sum3 = zval[i]+sum3;
asyrofi 6:bc835d0f686f 104 }
asyrofi 10:e5ee9515f4a2 105
asyrofi 11:a60e398f9032 106 zavg=sum3/10.0;
asyrofi 6:bc835d0f686f 107 led1=0;
asyrofi 10:e5ee9515f4a2 108 return zavg;
asyrofi 5:8242f251bcac 109 }
asyrofi 10:e5ee9515f4a2 110
asyrofi 10:e5ee9515f4a2 111
asyrofi 3:1749778af065 112
asyrofi 6:bc835d0f686f 113 int main()
asyrofi 6:bc835d0f686f 114 {
asyrofi 4:a57b495be9fa 115 uart1.baud(9600);
peterswanson87 0:d0adb548714f 116 int readings[3] = {0, 0, 0};
asyrofi 10:e5ee9515f4a2 117 char buffer [20];
asyrofi 6:bc835d0f686f 118
asyrofi 10:e5ee9515f4a2 119 //inisiliasi
asyrofi 10:e5ee9515f4a2 120 int steps=0;
asyrofi 10:e5ee9515f4a2 121 int flag=0;
asyrofi 6:bc835d0f686f 122 int acc=0;
asyrofi 10:e5ee9515f4a2 123 int totvect [20] = {0};
asyrofi 10:e5ee9515f4a2 124 int totave [20] = {0};
asyrofi 10:e5ee9515f4a2 125 int totvel [20] = {0};
asyrofi 10:e5ee9515f4a2 126 int totdist [20] = {0};
asyrofi 10:e5ee9515f4a2 127 int totheight [20] = {0};
asyrofi 10:e5ee9515f4a2 128
asyrofi 6:bc835d0f686f 129
asyrofi 6:bc835d0f686f 130 //float sum1, sum2, sum3 = 0
asyrofi 10:e5ee9515f4a2 131 int xaccl[20];
asyrofi 10:e5ee9515f4a2 132 int yaccl[20];
asyrofi 10:e5ee9515f4a2 133 int zaccl[20];
asyrofi 9:1e04b80d7199 134
asyrofi 9:1e04b80d7199 135 // Test Daata
asyrofi 9:1e04b80d7199 136 memset(&buffer, 0, sizeof(buffer));
asyrofi 11:a60e398f9032 137 /* int16_t reading_1= 0;
asyrofi 11:a60e398f9032 138 int16_t reading_2= 0;
asyrofi 11:a60e398f9032 139 int16_t reading_3= 0;
asyrofi 11:a60e398f9032 140 int16_t avg_1= 0;
asyrofi 11:a60e398f9032 141 int16_t avg_2= 0;
asyrofi 11:a60e398f9032 142 int16_t avg_3= 0;*/
asyrofi 11:a60e398f9032 143
asyrofi 11:a60e398f9032 144 int16_t lang = 0;
asyrofi 10:e5ee9515f4a2 145 int16_t perc = 0;
asyrofi 10:e5ee9515f4a2 146 int16_t kec = 0;
asyrofi 10:e5ee9515f4a2 147 int16_t jar = 0;
asyrofi 10:e5ee9515f4a2 148 int16_t ting = 0;
asyrofi 11:a60e398f9032 149 snprintf(buffer, 20, "data: %d,%d,%d,%d,%d\n",(int16_t)lang,(int16_t)perc,(int16_t)kec,(int16_t)jar,(int16_t)ting);
asyrofi 11:a60e398f9032 150 /* snprintf(buffer, 20, "data: %d,%d,%d,%d,%d,%d\n",(int16_t)reading_1,(int16_t)reading_2,(int16_t)reading_3,(int16_t)avg_1,(int16_t)avg_2,(int16_t)avg_3);*/
asyrofi 11:a60e398f9032 151
asyrofi 9:1e04b80d7199 152
asyrofi 2:99e00e9c5035 153 led1 = 1;
asyrofi 3:1749778af065 154 uart1.baud(9600);
asyrofi 2:99e00e9c5035 155 Ticker ticker;
asyrofi 2:99e00e9c5035 156 ticker.attach(periodicCallback, 1);
asyrofi 6:bc835d0f686f 157
asyrofi 2:99e00e9c5035 158 DEBUG("Initialising the nRF51822\n\r");
asyrofi 2:99e00e9c5035 159 ble.init();
asyrofi 2:99e00e9c5035 160 ble.onDisconnection(disconnectionCallback);
asyrofi 3:1749778af065 161 ble.onConnection(connectionCallback);
asyrofi 2:99e00e9c5035 162 ble.onDataWritten(onDataWritten);
asyrofi 2:99e00e9c5035 163
asyrofi 2:99e00e9c5035 164 /* setup advertising */
asyrofi 2:99e00e9c5035 165 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
asyrofi 2:99e00e9c5035 166 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
asyrofi 2:99e00e9c5035 167 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
asyrofi 2:99e00e9c5035 168 (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
asyrofi 2:99e00e9c5035 169 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
asyrofi 2:99e00e9c5035 170 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
asyrofi 2:99e00e9c5035 171
asyrofi 2:99e00e9c5035 172 ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */
asyrofi 2:99e00e9c5035 173 ble.startAdvertising();
asyrofi 2:99e00e9c5035 174
asyrofi 2:99e00e9c5035 175 UARTService uartService(ble);
asyrofi 2:99e00e9c5035 176 uartServicePtr = &uartService;
asyrofi 10:e5ee9515f4a2 177
asyrofi 10:e5ee9515f4a2 178 uart1.printf("Starting ADXL345 test...\n");
asyrofi 10:e5ee9515f4a2 179 wait(0.1);
asyrofi 10:e5ee9515f4a2 180 uart1.printf("Device ID is: 0x%02x\n", accelerometer.getDeviceID());
asyrofi 10:e5ee9515f4a2 181 wait(0.1);
asyrofi 10:e5ee9515f4a2 182
asyrofi 10:e5ee9515f4a2 183
asyrofi 10:e5ee9515f4a2 184 //Go into standby mode to configure the device.
asyrofi 10:e5ee9515f4a2 185 accelerometer.setPowerControl(0x00);
asyrofi 10:e5ee9515f4a2 186
asyrofi 10:e5ee9515f4a2 187 //Full resolution, +/-16g, 4mg/LSB.
asyrofi 10:e5ee9515f4a2 188 accelerometer.setDataFormatControl(0x0B);
asyrofi 10:e5ee9515f4a2 189
asyrofi 10:e5ee9515f4a2 190 //3.2kHz data rate.
asyrofi 10:e5ee9515f4a2 191 accelerometer.setDataRate(ADXL345_3200HZ);
asyrofi 10:e5ee9515f4a2 192
asyrofi 10:e5ee9515f4a2 193 //Measurement mode.
asyrofi 10:e5ee9515f4a2 194 accelerometer.setPowerControl(0x08);
asyrofi 10:e5ee9515f4a2 195
asyrofi 10:e5ee9515f4a2 196 int avg1 = calibratex();
asyrofi 10:e5ee9515f4a2 197 int avg2 = calibratey();
asyrofi 10:e5ee9515f4a2 198 int avg3 = calibratez();
asyrofi 10:e5ee9515f4a2 199
asyrofi 10:e5ee9515f4a2 200
asyrofi 4:a57b495be9fa 201 while (1)
asyrofi 4:a57b495be9fa 202 {
asyrofi 2:99e00e9c5035 203 ble.waitForEvent();
peterswanson87 0:d0adb548714f 204 wait(0.1);
peterswanson87 0:d0adb548714f 205 accelerometer.getOutput(readings);
asyrofi 12:58823c0cdf99 206 uart1.printf("\n%i, %i, %i\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]);
asyrofi 11:a60e398f9032 207 /* memset(&buffer, 0, sizeof(buffer));
asyrofi 11:a60e398f9032 208 snprintf(buffer, 20, "data: %d,%d,%d\n\n", (int16_t)readings[0],(int16_t)readings[1],(int16_t)readings[0]);
asyrofi 11:a60e398f9032 209 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)buffer, sizeof(buffer), false);*/
asyrofi 11:a60e398f9032 210
asyrofi 12:58823c0cdf99 211 uart1.printf("\n%i, %i, %i\n", (int16_t)avg1, (int16_t)avg2, (int16_t)avg3);
asyrofi 11:a60e398f9032 212 /* memset(&buffer, 0, sizeof(buffer));
asyrofi 11:a60e398f9032 213 snprintf(buffer, 20, "data: %d,%d,%d\n\n", (int16_t)avg1,(int16_t)avg2,(int16_t)avg3);
asyrofi 14:afe556cc0a6c 214 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)buffer, sizeof(buffer), false);
asyrofi 14:afe556cc0a6c 215 wait(0.1);*/
asyrofi 10:e5ee9515f4a2 216
asyrofi 10:e5ee9515f4a2 217
asyrofi 6:bc835d0f686f 218
asyrofi 10:e5ee9515f4a2 219 //float x,y,z
asyrofi 10:e5ee9515f4a2 220 for (int i=0; i<10; i++)
asyrofi 10:e5ee9515f4a2 221 {
asyrofi 10:e5ee9515f4a2 222 xaccl[i]=(readings[0]);
asyrofi 10:e5ee9515f4a2 223 wait(0.1);
asyrofi 10:e5ee9515f4a2 224 yaccl[i]=(readings[1]);
asyrofi 10:e5ee9515f4a2 225 wait(0.1);
asyrofi 10:e5ee9515f4a2 226 zaccl[i]=(readings[2]);
asyrofi 10:e5ee9515f4a2 227 wait(0.1);
asyrofi 10:e5ee9515f4a2 228
asyrofi 10:e5ee9515f4a2 229 //formula
asyrofi 10:e5ee9515f4a2 230 totvect[i] = sqrt((double)((xaccl[i]-xavg)* (xaccl[i]-xavg))+(double) ((yaccl[i] - yavg)*(yaccl[i] - yavg)) +(double) ((zaccl[i] - zavg)*(zaccl[i] - zavg)));
asyrofi 10:e5ee9515f4a2 231 totave[i] = (totvect[i] + totvect[i-1]) / 2 ;
asyrofi 13:e5327d2de406 232 uart1.printf("\tacc:%i", (int16_t)totave[i]);
asyrofi 10:e5ee9515f4a2 233 memset(&buffer, 0, sizeof(buffer));
asyrofi 13:e5327d2de406 234 snprintf(buffer, 20, "\tacc:%d", (int16_t)totave[i]);
asyrofi 10:e5ee9515f4a2 235 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)buffer, sizeof(buffer), false);
asyrofi 10:e5ee9515f4a2 236
asyrofi 10:e5ee9515f4a2 237 totvel[i]=(totave[i]*0.2);
asyrofi 13:e5327d2de406 238 uart1.printf("\tvel:%i", (int16_t)totvel[i]);
asyrofi 10:e5ee9515f4a2 239 memset(&buffer, 0, sizeof(buffer));
asyrofi 13:e5327d2de406 240 snprintf(buffer, 20, "\tvel:%d", (int16_t)totvel[i]);
asyrofi 10:e5ee9515f4a2 241 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)buffer, sizeof(buffer), false);
asyrofi 10:e5ee9515f4a2 242
asyrofi 10:e5ee9515f4a2 243 totdist[i]=(totvect[i]*(0.2*0.2)/2);
asyrofi 13:e5327d2de406 244 uart1.printf("\tdist:%i", (int16_t)totdist[i]);
asyrofi 10:e5ee9515f4a2 245 memset(&buffer, 0, sizeof(buffer));
asyrofi 13:e5327d2de406 246 snprintf(buffer, 20, "\tdist:%d", (int16_t)totdist[i]);
asyrofi 10:e5ee9515f4a2 247 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)buffer, sizeof(buffer), false);
asyrofi 10:e5ee9515f4a2 248
asyrofi 10:e5ee9515f4a2 249 totheight[i]=sqrt((((xaccl[i]-xavg)* (xaccl[i]-xavg))+ ((yaccl[i] - yavg)*(yaccl[i] - yavg)))*(0.2*0.2)/2);
asyrofi 13:e5327d2de406 250 uart1.printf("\thght:%i", (int16_t)totheight[i]);
asyrofi 10:e5ee9515f4a2 251 memset(&buffer, 0, sizeof(buffer));
asyrofi 13:e5327d2de406 252 snprintf(buffer, 20, "\thght:%d", (int16_t)totheight[i]);
asyrofi 10:e5ee9515f4a2 253 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)buffer, sizeof(buffer), false);
asyrofi 10:e5ee9515f4a2 254
asyrofi 10:e5ee9515f4a2 255 acc=acc+totave[i];
asyrofi 10:e5ee9515f4a2 256
asyrofi 10:e5ee9515f4a2 257
asyrofi 10:e5ee9515f4a2 258
asyrofi 10:e5ee9515f4a2 259 //cal steps
asyrofi 10:e5ee9515f4a2 260 if (totave[i] > threshold && flag==0)
asyrofi 10:e5ee9515f4a2 261 {
asyrofi 10:e5ee9515f4a2 262 steps = steps+1;
asyrofi 10:e5ee9515f4a2 263 flag=1;
asyrofi 10:e5ee9515f4a2 264 }
asyrofi 10:e5ee9515f4a2 265 else if (totave[i] > threshold && flag == 1)
asyrofi 10:e5ee9515f4a2 266 {
asyrofi 10:e5ee9515f4a2 267 // do nothing
asyrofi 10:e5ee9515f4a2 268 }
asyrofi 10:e5ee9515f4a2 269 if (totave[i] < threshold && flag == 1)
asyrofi 10:e5ee9515f4a2 270 {flag=0;}
asyrofi 13:e5327d2de406 271 uart1.printf("\nsteps:%i", (int16_t)steps);
asyrofi 11:a60e398f9032 272 memset(&buffer, 0, sizeof(buffer));
asyrofi 13:e5327d2de406 273 snprintf(buffer, 20, "\nsteps:%d", (int16_t)steps);
asyrofi 11:a60e398f9032 274 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)buffer, sizeof(buffer), false);
asyrofi 10:e5ee9515f4a2 275
asyrofi 10:e5ee9515f4a2 276 }
asyrofi 6:bc835d0f686f 277 }
peterswanson87 0:d0adb548714f 278
peterswanson87 0:d0adb548714f 279 }
asyrofi 6:bc835d0f686f 280