this is implement of uart adxl345 ble nrf51

Dependencies:   BLE_API adxl345 mbed nRF51822

Fork of ADXL345_HelloWorld by Aaron Berk

Committer:
asyrofi
Date:
Sat Dec 09 05:48:49 2017 +0000
Revision:
3:3ee58f31a637
Parent:
2:89f008ca5911
mantaplah

Who changed what in which revision?

UserRevisionLine numberNew contents of line
asyrofi 1:cfb4bb8f33e9 1 #include "mbed.h"
asyrofi 1:cfb4bb8f33e9 2 #include "ble/BLE.h"
asyrofi 1:cfb4bb8f33e9 3 #include "ble/services/UARTService.h"
asyrofi 1:cfb4bb8f33e9 4 #include "Serial.h"
aberk 0:9e92575dece6 5 #include "ADXL345.h"
asyrofi 3:3ee58f31a637 6
asyrofi 1:cfb4bb8f33e9 7
asyrofi 1:cfb4bb8f33e9 8 #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
asyrofi 1:cfb4bb8f33e9 9 * it will have an impact on code-size and power consumption. */
asyrofi 1:cfb4bb8f33e9 10
asyrofi 1:cfb4bb8f33e9 11 #if NEED_CONSOLE_OUTPUT
asyrofi 1:cfb4bb8f33e9 12 #define DEBUG(...) { printf(__VA_ARGS__); }
asyrofi 1:cfb4bb8f33e9 13 #else
asyrofi 1:cfb4bb8f33e9 14 #define DEBUG(...) /* nothing */
asyrofi 1:cfb4bb8f33e9 15 #endif /* #if NEED_CONSOLE_OUTPUT */
asyrofi 1:cfb4bb8f33e9 16
asyrofi 2:89f008ca5911 17 ADXL345 accelerometer(p5, p6, p7, p8); // (SDA, SDO, SCL, CS);
asyrofi 1:cfb4bb8f33e9 18 BLEDevice ble;
asyrofi 1:cfb4bb8f33e9 19 DigitalOut led1(LED1);
asyrofi 1:cfb4bb8f33e9 20 Serial uart1(USBTX,USBRX);
asyrofi 1:cfb4bb8f33e9 21 UARTService *uartServicePtr;
asyrofi 1:cfb4bb8f33e9 22
aberk 0:9e92575dece6 23
asyrofi 1:cfb4bb8f33e9 24 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
asyrofi 1:cfb4bb8f33e9 25 {
asyrofi 1:cfb4bb8f33e9 26 DEBUG("Disconnected!\n\r");
asyrofi 1:cfb4bb8f33e9 27 DEBUG("Restarting the advertising process\n\r");
asyrofi 1:cfb4bb8f33e9 28 ble.startAdvertising();
asyrofi 1:cfb4bb8f33e9 29 }
asyrofi 1:cfb4bb8f33e9 30
asyrofi 1:cfb4bb8f33e9 31 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
asyrofi 1:cfb4bb8f33e9 32
asyrofi 1:cfb4bb8f33e9 33 DEBUG("Connected!\n\r");
asyrofi 1:cfb4bb8f33e9 34
asyrofi 1:cfb4bb8f33e9 35 }
asyrofi 1:cfb4bb8f33e9 36
asyrofi 1:cfb4bb8f33e9 37
asyrofi 1:cfb4bb8f33e9 38 uint8_t b[40]={'a','d','q','w'};
asyrofi 1:cfb4bb8f33e9 39 void onDataWritten1(const GattWriteCallbackParams *params)
asyrofi 1:cfb4bb8f33e9 40 {
asyrofi 1:cfb4bb8f33e9 41
asyrofi 1:cfb4bb8f33e9 42
asyrofi 1:cfb4bb8f33e9 43 if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
asyrofi 1:cfb4bb8f33e9 44 uint16_t bytesRead = params->len;
asyrofi 1:cfb4bb8f33e9 45 DEBUG("received %u bytes %s\n\r", bytesRead,params->data);
asyrofi 1:cfb4bb8f33e9 46 // if(sFlag == 1)
asyrofi 1:cfb4bb8f33e9 47 // ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (const uint8_t*)b/*params->data*/, 4/*bytesRead*/);
asyrofi 1:cfb4bb8f33e9 48 }
asyrofi 1:cfb4bb8f33e9 49 }
asyrofi 1:cfb4bb8f33e9 50
asyrofi 1:cfb4bb8f33e9 51
asyrofi 1:cfb4bb8f33e9 52 void periodicCallback(void)
asyrofi 1:cfb4bb8f33e9 53 {
asyrofi 1:cfb4bb8f33e9 54 led1 = !led1;
asyrofi 1:cfb4bb8f33e9 55
asyrofi 1:cfb4bb8f33e9 56 }
asyrofi 1:cfb4bb8f33e9 57
asyrofi 3:3ee58f31a637 58 int threshold =80;
asyrofi 3:3ee58f31a637 59 int xval[10]={0};
asyrofi 3:3ee58f31a637 60 int yval[10]={0};
asyrofi 3:3ee58f31a637 61 int zval[10]={0};
asyrofi 3:3ee58f31a637 62 int xavg;
asyrofi 3:3ee58f31a637 63 int yavg;
asyrofi 3:3ee58f31a637 64 int zavg;
asyrofi 3:3ee58f31a637 65
asyrofi 3:3ee58f31a637 66
asyrofi 3:3ee58f31a637 67 int calibratex()
asyrofi 3:3ee58f31a637 68 {
asyrofi 3:3ee58f31a637 69 int readings[3] = {0, 0, 0};
asyrofi 3:3ee58f31a637 70 led1=1;
asyrofi 3:3ee58f31a637 71 long int sum1 = 0;
asyrofi 3:3ee58f31a637 72 for (int i=0; i<10; i++)
asyrofi 3:3ee58f31a637 73 {
asyrofi 3:3ee58f31a637 74 accelerometer.getOutput(readings);
asyrofi 3:3ee58f31a637 75 xval[i]=(readings[0]); //nilai x adxl345
asyrofi 3:3ee58f31a637 76 sum1 = yval[i]+sum1;
asyrofi 3:3ee58f31a637 77 }
asyrofi 3:3ee58f31a637 78
asyrofi 3:3ee58f31a637 79 xavg=sum1/10.0;
asyrofi 3:3ee58f31a637 80
asyrofi 3:3ee58f31a637 81 led1=0;
asyrofi 3:3ee58f31a637 82 return xavg;
asyrofi 3:3ee58f31a637 83 }
asyrofi 3:3ee58f31a637 84 int calibratey()
asyrofi 3:3ee58f31a637 85 {
asyrofi 3:3ee58f31a637 86 int readings[3] = {0, 0, 0};
asyrofi 3:3ee58f31a637 87 led1=1;
asyrofi 3:3ee58f31a637 88 long int sum2 = 0;
asyrofi 3:3ee58f31a637 89 for (int i=0; i<10; i++)
asyrofi 3:3ee58f31a637 90 {
asyrofi 3:3ee58f31a637 91 accelerometer.getOutput(readings);
asyrofi 3:3ee58f31a637 92 yval[i]=(readings[1]); //nilai y adxl345
asyrofi 3:3ee58f31a637 93 sum2 = yval[i]+sum2;
asyrofi 3:3ee58f31a637 94 }
asyrofi 3:3ee58f31a637 95 yavg=sum2/10.0;
asyrofi 3:3ee58f31a637 96 led1=0;
asyrofi 3:3ee58f31a637 97 return yavg;
asyrofi 3:3ee58f31a637 98 }
asyrofi 3:3ee58f31a637 99 int calibratez()
asyrofi 3:3ee58f31a637 100 {
asyrofi 3:3ee58f31a637 101 int readings[3] = {0, 0, 0};
asyrofi 3:3ee58f31a637 102
asyrofi 3:3ee58f31a637 103 led1=1;
asyrofi 3:3ee58f31a637 104 long int sum3 = 0;
asyrofi 3:3ee58f31a637 105 for (int i=0; i<10; i++)
asyrofi 3:3ee58f31a637 106 {
asyrofi 3:3ee58f31a637 107 accelerometer.getOutput(readings);
asyrofi 3:3ee58f31a637 108 zval[i]=(readings[2]); //nilai z adxl345
asyrofi 3:3ee58f31a637 109 sum3 = zval[i]+sum3;
asyrofi 3:3ee58f31a637 110 }
asyrofi 3:3ee58f31a637 111
asyrofi 3:3ee58f31a637 112 zavg=sum3/10.0;
asyrofi 3:3ee58f31a637 113 led1=0;
asyrofi 3:3ee58f31a637 114 return zavg;
asyrofi 3:3ee58f31a637 115 }
asyrofi 3:3ee58f31a637 116
aberk 0:9e92575dece6 117
asyrofi 2:89f008ca5911 118 int main()
asyrofi 2:89f008ca5911 119 {
asyrofi 2:89f008ca5911 120 uart1.baud(9600);
aberk 0:9e92575dece6 121 int readings[3] = {0, 0, 0};
asyrofi 2:89f008ca5911 122 char buffer [20];
asyrofi 2:89f008ca5911 123
asyrofi 1:cfb4bb8f33e9 124 led1 = 1;
asyrofi 1:cfb4bb8f33e9 125 uart1.baud(9600);
asyrofi 1:cfb4bb8f33e9 126 Ticker ticker;
asyrofi 1:cfb4bb8f33e9 127 ticker.attach(periodicCallback, 1);
asyrofi 1:cfb4bb8f33e9 128
asyrofi 1:cfb4bb8f33e9 129 DEBUG("Initialising the nRF51822\n\r");
asyrofi 1:cfb4bb8f33e9 130 ble.init();
asyrofi 1:cfb4bb8f33e9 131 ble.onDisconnection(disconnectionCallback);
asyrofi 1:cfb4bb8f33e9 132 ble.onConnection(connectionCallback);
asyrofi 1:cfb4bb8f33e9 133 ble.onDataWritten(onDataWritten1);
asyrofi 1:cfb4bb8f33e9 134
asyrofi 1:cfb4bb8f33e9 135 /* setup advertising */
asyrofi 1:cfb4bb8f33e9 136 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
asyrofi 1:cfb4bb8f33e9 137 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
asyrofi 1:cfb4bb8f33e9 138 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
asyrofi 1:cfb4bb8f33e9 139 (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
asyrofi 1:cfb4bb8f33e9 140 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
asyrofi 1:cfb4bb8f33e9 141 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
asyrofi 1:cfb4bb8f33e9 142
asyrofi 1:cfb4bb8f33e9 143 ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */
asyrofi 1:cfb4bb8f33e9 144 ble.startAdvertising();
asyrofi 1:cfb4bb8f33e9 145
asyrofi 1:cfb4bb8f33e9 146 UARTService uartService(ble);
asyrofi 1:cfb4bb8f33e9 147 uartServicePtr = &uartService;
asyrofi 3:3ee58f31a637 148
asyrofi 3:3ee58f31a637 149 uart1.printf("Starting ADXL345 test...\n");
asyrofi 3:3ee58f31a637 150 uart1.printf("Device ID is: 0x%02x\n", accelerometer.getDevId());
aberk 0:9e92575dece6 151
asyrofi 3:3ee58f31a637 152 //Go into standby mode to configure the device.
asyrofi 3:3ee58f31a637 153 accelerometer.setPowerControl(0x00);
asyrofi 3:3ee58f31a637 154
asyrofi 3:3ee58f31a637 155 //Full resolution, +/-16g, 4mg/LSB.
asyrofi 3:3ee58f31a637 156 accelerometer.setDataFormatControl(0x0B);
asyrofi 3:3ee58f31a637 157
asyrofi 3:3ee58f31a637 158 //3.2kHz data rate.
asyrofi 3:3ee58f31a637 159 accelerometer.setDataRate(ADXL345_3200HZ);
asyrofi 3:3ee58f31a637 160
asyrofi 3:3ee58f31a637 161 //Measurement mode.
asyrofi 3:3ee58f31a637 162 accelerometer.setPowerControl(0x08);
asyrofi 3:3ee58f31a637 163
asyrofi 3:3ee58f31a637 164 int avg1 = calibratex();
asyrofi 3:3ee58f31a637 165 int avg2 = calibratey();
asyrofi 3:3ee58f31a637 166 int avg3 = calibratez();
asyrofi 3:3ee58f31a637 167
asyrofi 3:3ee58f31a637 168 //inisiliasi
asyrofi 3:3ee58f31a637 169 int steps=0;
asyrofi 3:3ee58f31a637 170 int flag=0;
asyrofi 3:3ee58f31a637 171 int acc=0;
asyrofi 3:3ee58f31a637 172 int totvect [20] = {0};
asyrofi 3:3ee58f31a637 173 int totave [20] = {0};
asyrofi 3:3ee58f31a637 174 int totvel [20] = {0};
asyrofi 3:3ee58f31a637 175 int totdist [20] = {0};
asyrofi 3:3ee58f31a637 176 int totheight [20] = {0};
asyrofi 3:3ee58f31a637 177
aberk 0:9e92575dece6 178
asyrofi 3:3ee58f31a637 179 //float sum1, sum2, sum3 = 0
asyrofi 3:3ee58f31a637 180 int xaccl[20];
asyrofi 3:3ee58f31a637 181 int yaccl[20];
asyrofi 3:3ee58f31a637 182 int zaccl[20];
asyrofi 3:3ee58f31a637 183
asyrofi 3:3ee58f31a637 184 // Test Daata
asyrofi 3:3ee58f31a637 185 memset(&buffer, 0, sizeof(buffer));
asyrofi 3:3ee58f31a637 186 /* int16_t reading_1= 0;
asyrofi 3:3ee58f31a637 187 int16_t reading_2= 0;
asyrofi 3:3ee58f31a637 188 int16_t reading_3= 0;
asyrofi 3:3ee58f31a637 189 int16_t avg_1= 0;
asyrofi 3:3ee58f31a637 190 int16_t avg_2= 0;
asyrofi 3:3ee58f31a637 191 int16_t avg_3= 0;*/
asyrofi 3:3ee58f31a637 192
asyrofi 3:3ee58f31a637 193 int16_t lang = 0;
asyrofi 3:3ee58f31a637 194 int16_t perc = 0;
asyrofi 3:3ee58f31a637 195 int16_t kec = 0;
asyrofi 3:3ee58f31a637 196 int16_t jar = 0;
asyrofi 3:3ee58f31a637 197 int16_t ting = 0;
asyrofi 3:3ee58f31a637 198 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 3:3ee58f31a637 199 /* 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);*/
aberk 0:9e92575dece6 200
asyrofi 3:3ee58f31a637 201 while (1)
asyrofi 3:3ee58f31a637 202 {
asyrofi 3:3ee58f31a637 203 ble.waitForEvent();
asyrofi 3:3ee58f31a637 204 wait(0.1);
asyrofi 3:3ee58f31a637 205 accelerometer.getOutput(readings);
asyrofi 3:3ee58f31a637 206 uart1.printf("\n%i, %i, %i\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]);
asyrofi 3:3ee58f31a637 207 /* memset(&buffer, 0, sizeof(buffer));
asyrofi 3:3ee58f31a637 208 snprintf(buffer, 20, "data: %d,%d,%d\n\n", (int16_t)readings[0],(int16_t)readings[1],(int16_t)readings[0]);
asyrofi 3:3ee58f31a637 209 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)buffer, sizeof(buffer), false);*/
asyrofi 3:3ee58f31a637 210
asyrofi 3:3ee58f31a637 211 uart1.printf("\n%i, %i, %i\n", (int16_t)avg1, (int16_t)avg2, (int16_t)avg3);
asyrofi 3:3ee58f31a637 212 /* memset(&buffer, 0, sizeof(buffer));
asyrofi 3:3ee58f31a637 213 snprintf(buffer, 20, "data: %d,%d,%d\n\n", (int16_t)avg1,(int16_t)avg2,(int16_t)avg3);
asyrofi 3:3ee58f31a637 214 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)buffer, sizeof(buffer), false);*/
asyrofi 3:3ee58f31a637 215 wait(0.1);
asyrofi 3:3ee58f31a637 216
asyrofi 3:3ee58f31a637 217
asyrofi 3:3ee58f31a637 218
asyrofi 3:3ee58f31a637 219 //float x,y,z
asyrofi 3:3ee58f31a637 220 for (int i=0; i<10; i++)
asyrofi 3:3ee58f31a637 221 {
asyrofi 3:3ee58f31a637 222 xaccl[i]=(readings[0]);
asyrofi 3:3ee58f31a637 223 wait(0.1);
asyrofi 3:3ee58f31a637 224 yaccl[i]=(readings[1]);
asyrofi 3:3ee58f31a637 225 wait(0.1);
asyrofi 3:3ee58f31a637 226 zaccl[i]=(readings[2]);
asyrofi 3:3ee58f31a637 227 wait(0.1);
asyrofi 3:3ee58f31a637 228
asyrofi 3:3ee58f31a637 229 //formula
asyrofi 3:3ee58f31a637 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 3:3ee58f31a637 231 totave[i] = (totvect[i] + totvect[i-1]) / 2 ;
asyrofi 3:3ee58f31a637 232 uart1.printf("\t acc: %i\n", (int16_t)totave[i]);
asyrofi 3:3ee58f31a637 233 memset(&buffer, 0, sizeof(buffer));
asyrofi 3:3ee58f31a637 234 snprintf(buffer, 20, "\t acc: %d\n", (int16_t)totave[i]);
asyrofi 3:3ee58f31a637 235 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)buffer, sizeof(buffer), false);
asyrofi 3:3ee58f31a637 236
asyrofi 3:3ee58f31a637 237 totvel[i]=(totave[i]*0.2);
asyrofi 3:3ee58f31a637 238 uart1.printf("\t vel: %i\n", (int16_t)totvel[i]);
asyrofi 3:3ee58f31a637 239 memset(&buffer, 0, sizeof(buffer));
asyrofi 3:3ee58f31a637 240 snprintf(buffer, 20, "\t vel: %d\n", (int16_t)totvel[i]);
asyrofi 3:3ee58f31a637 241 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)buffer, sizeof(buffer), false);
asyrofi 3:3ee58f31a637 242
asyrofi 3:3ee58f31a637 243 totdist[i]=(totvect[i]*(0.2*0.2)/2);
asyrofi 3:3ee58f31a637 244 uart1.printf("\t dist: %i\n", (int16_t)totdist[i]);
asyrofi 3:3ee58f31a637 245 memset(&buffer, 0, sizeof(buffer));
asyrofi 3:3ee58f31a637 246 snprintf(buffer, 20, "\tdist: %d\n", (int16_t)totdist[i]);
asyrofi 3:3ee58f31a637 247 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)buffer, sizeof(buffer), false);
asyrofi 3:3ee58f31a637 248
asyrofi 3:3ee58f31a637 249 totheight[i]=sqrt((((xaccl[i]-xavg)* (xaccl[i]-xavg))+ ((yaccl[i] - yavg)*(yaccl[i] - yavg)))*(0.2*0.2)/2);
asyrofi 3:3ee58f31a637 250 uart1.printf("\thght: %i\n", (int16_t)totheight[i]);
asyrofi 3:3ee58f31a637 251 memset(&buffer, 0, sizeof(buffer));
asyrofi 3:3ee58f31a637 252 snprintf(buffer, 20, "\thght: %d\n", (int16_t)totheight[i]);
asyrofi 3:3ee58f31a637 253 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)buffer, sizeof(buffer), false);
asyrofi 3:3ee58f31a637 254
asyrofi 3:3ee58f31a637 255 acc=acc+totave[i];
asyrofi 3:3ee58f31a637 256
asyrofi 3:3ee58f31a637 257
asyrofi 3:3ee58f31a637 258
asyrofi 3:3ee58f31a637 259 //cal steps
asyrofi 3:3ee58f31a637 260 if (totave[i] > threshold && flag==0)
asyrofi 3:3ee58f31a637 261 {
asyrofi 3:3ee58f31a637 262 steps = steps+1;
asyrofi 3:3ee58f31a637 263 flag=1;
asyrofi 3:3ee58f31a637 264 }
asyrofi 3:3ee58f31a637 265 else if (totave[i] > threshold && flag == 1)
asyrofi 3:3ee58f31a637 266 {
asyrofi 3:3ee58f31a637 267 // do nothing
asyrofi 3:3ee58f31a637 268 }
asyrofi 3:3ee58f31a637 269 if (totave[i] < threshold && flag == 1)
asyrofi 3:3ee58f31a637 270 {flag=0;}
asyrofi 3:3ee58f31a637 271 uart1.printf("steps: %i\n", (int16_t)steps);
asyrofi 3:3ee58f31a637 272 memset(&buffer, 0, sizeof(buffer));
asyrofi 3:3ee58f31a637 273 snprintf(buffer, 20, "\nsteps: %d\t", (int16_t)steps);
asyrofi 3:3ee58f31a637 274 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)buffer, sizeof(buffer), false);
asyrofi 3:3ee58f31a637 275
asyrofi 3:3ee58f31a637 276 }
aberk 0:9e92575dece6 277 }
aberk 0:9e92575dece6 278
aberk 0:9e92575dece6 279 }