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:
Sat Dec 09 02:06:19 2017 +0000
Revision:
10:e5ee9515f4a2
Parent:
9:1e04b80d7199
Child:
11:a60e398f9032
mantap cuy

Who changed what in which revision?

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