this is program how build nRF51822 to get ADXL345 data

Dependencies:   BLE_API mbed nRF51822

Fork of ADXL345_I2C by Peter Swanson

main.cpp

Committer:
asyrofi
Date:
2017-12-06
Revision:
6:bc835d0f686f
Parent:
5:8242f251bcac
Child:
7:31412349a322

File content as of revision 6:bc835d0f686f:

#include "ADXL345_I2C.h"
#include "mbed.h"
#include "ble/BLE.h"
#include "ble/services/UARTService.h"
#include "Serial.h"

#define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
                               * it will have an impact on code-size and power consumption. */

#if NEED_CONSOLE_OUTPUT
#define DEBUG(...) { printf(__VA_ARGS__); }
#else
#define DEBUG(...) /* nothing */
#endif /* #if NEED_CONSOLE_OUTPUT */

 ADXL345_I2C accelerometer(p30, p7);
 BLEDevice  ble;
 DigitalOut led1(LED1);
 Serial uart1(USBTX,USBRX);
 UARTService *uartServicePtr;
 
 
 
 
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
{
    DEBUG("Disconnected!\n\r");
    DEBUG("Restarting the advertising process\n\r");
    ble.startAdvertising();
}

void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
   
    DEBUG("Connected!\n\r");
    
}

uint8_t b[40]={'a','d','q','w'};
void onDataWritten(const GattWriteCallbackParams *params)
{
    if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
        uint16_t bytesRead = params->len;
        DEBUG("received %u bytes %s\n\r", bytesRead,params->data);
        /*uint16_t bytesRead = params->len;
        DEBUG("received %u bytes\n\r", bytesRead);
        ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);*/
    }
}

void periodicCallback(void)
{
    led1 = !led1;
}

int readings[3] = {0, 0, 0};
int threshold =80;
int xval[100]={0};
int yval[100]={0};
int zval[100]={0};
int xavg;
int yavg;
int zavg;
int steps, flag=0;
void calibrate(void)
{
   led1=1;
   int sum1  = 0;
   int sum2 = 0;
   int sum3 = 0;
   for (int i=0; i<100; i++)
   {
       xval[i]=(1);  //nilai x adxl345
       sum1 = xval[i]+sum1;
   }
   wait(0.1);
   xavg=sum1/100.0;
   uart1.printf("%i\n", (int16_t)xavg); //nilai hasil kalibrasi x
   
   
   for (int i=0; i<100; i++)
   {
       yval[i]=(1); //nilai y adxl345
       sum2 = yval[i]+sum2;
   }
   wait(0.1);
   yavg=sum2/100.0;
   uart1.printf("%i\n", (int16_t)yavg); //nilai hasil kalibrasi y
   
   for (int i=0; i<100; i++)
   {
       zval[i]=(1); //nilai z adxl345
       sum3 = zval[i]+sum3;
   }
   wait(0.1);
   zavg=sum3/100.0;
   uart1.printf("%i\n", (int16_t)zavg); //nilai hasil kalibrasi z
   led1=0;     
}


 int main() 
 {
     uart1.baud(9600);
     int readings[3] = {0, 0, 0};
     calibrate();
     uart1.printf("Starting ADXL345 test...\n");
     wait(.001);
     uart1.printf("Device ID is: 0x%02x\n", accelerometer.getDeviceID());
     wait(.001);
     
    
    //Go into standby mode to configure the device.
    accelerometer.setPowerControl(0x00);

    //Full resolution, +/-16g, 4mg/LSB.
    accelerometer.setDataFormatControl(0x0B);
    
    //3.2kHz data rate.
    accelerometer.setDataRate(ADXL345_3200HZ);

    //Measurement mode.
    accelerometer.setPowerControl(0x08);
    
        int acc=0;
        int totvect   [100] = {0};
        int totave    [100] = {0};
        int totvel    [100] = {0};
        int totdist   [100] = {0};
        int totheight [100] = {0};
        
        
        //float sum1, sum2, sum3 = 0
        double xaccl[100] = {0};
        double yaccl[100] = {0};
        double zaccl[100] = {0};
        
        //float x,y,z
        for (int i=0; i<100; i++)
        {
            xaccl[i]=(1);
            wait(0.1);
            yaccl[i]=(1);
            wait(0.1);
            zaccl[i]=(1);
            wait(0.1);
            
            //formula
        
         totvect[i] = sqrt(((xaccl[i]-xavg)* (xaccl[i]-xavg))+ ((yaccl[i] - yavg)*(yaccl[i] - yavg)) + ((zaccl[i] - zavg)*(zaccl[i] - zavg)));
         totave[i] = (totvect[i] + totvect[i-1]) / 2 ;
         totvel[i]=(totave[i]*0.2);
 //totave1[i]=(totvel[i]+totvel[i-1])/2;
         totdist[i]=(totvect[i]*(0.2*0.2)/2);
         totheight[i]=sqrt((((xaccl[i]-xavg)* (xaccl[i]-xavg))+ ((yaccl[i] - yavg)*(yaccl[i] - yavg)))*(0.2*0.2)/2);
 //totave3[i]=((totheight[i]+totheight[i-1])/2);
         acc=acc+totave[i];
         
         uart1.printf("%i\n", (int16_t)totave[i]);
         uart1.printf("%i\n", (int16_t)totvel[i]);
         uart1.printf("%i\n", (int16_t)totdist[i]);
         uart1.printf("%i\n", (int16_t)totheight[i]);
         
         wait(0.1);
         
         if (steps == 10)
         {
             uart1.printf("capek");
         }
         
         else
         {
           //nothing  
         }
         
         //cal steps
         if (totave[i] > threshold && flag==0)
         {
             steps = steps+1;
             flag=1;
         }
         else if (totave[i] > threshold && flag == 1)
         {
          // do nothing   
         }
         if (totave[i] < threshold && flag == 1)
             {flag=0;}
             uart1.printf("%i\n", (int16_t)steps);
             
    
        }
         
         
    led1 = 1;
    uart1.baud(9600);
    Ticker ticker;
    ticker.attach(periodicCallback, 1);
    
    
    
    DEBUG("Initialising the nRF51822\n\r");
    ble.init();
    ble.onDisconnection(disconnectionCallback);
    ble.onConnection(connectionCallback);
    ble.onDataWritten(onDataWritten);

    /* setup advertising */
    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
    ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
                                     (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
                                     (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));

    ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */
    ble.startAdvertising();

    UARTService uartService(ble);
    uartServicePtr = &uartService;
 
     while (1) 
     {
         ble.waitForEvent();
         wait(0.1);
         accelerometer.getOutput(readings);
         uart1.printf("%i, %i, %i\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]);
         ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)readings[0]/*params->data*/, sizeof(readings),false/*bytesRead*/);
         ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)readings[1]/*params->data*/, sizeof(readings),false/*bytesRead*/);
         ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)readings[2]/*params->data*/, sizeof(readings),false/*bytesRead*/);         
         
    }
 
 }