this is program how build nRF51822 to get ADXL345 data

Dependencies:   BLE_API mbed nRF51822

Fork of ADXL345_I2C by Peter Swanson

Files at this revision

API Documentation at this revision

Comitter:
asyrofi
Date:
Wed Apr 18 05:02:24 2018 +0000
Parent:
18:3e3143249113
Commit message:
masih salah tolong diperbaiki..

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Feb 19 09:11:16 2018 +0000
+++ b/main.cpp	Wed Apr 18 05:02:24 2018 +0000
@@ -3,6 +3,10 @@
 #include "ble/BLE.h"
 #include "ble/services/UARTService.h"
 #include "Serial.h"
+#include <math.h>
+#include <stdlib.h>
+#define MAX 20
+#define k 4
 
 #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. */
@@ -53,62 +57,28 @@
 int xval[10]={0};
 int yval[10]={0};
 int zval[10]={0};
-int xavg;
-int yavg;
-int zavg;
 
-
-/*int calibratex()
-{
-   int readings[3] = {0, 0, 0};
-   led1=1;
-   int sum1  = 0;
-   for (int i=0; i<10; i++)
-   {
-       accelerometer.getOutput(readings);
-       xval[i]=(readings[0] - 345);  //nilai x adxl345
-       sum1 = xval[i]+sum1;
-   }
-   
-   xavg=sum1/10.0;
-
-   led1=0;     
-   return xavg;
-}
-int calibratey()
+using namespace std;
+enum category{STANDING,CLIMBING,WALKING};
+class data{
+    int x,y;
+    category cat;
+    public:
+    void setd(int a,int b,category c){
+        x=a;
+        y=b;
+        cat=c;
+    }
+    int getx(){return x;}
+    int gety(){return y;}
+    category getcat(){
+        return cat;
+    }
+};//end of class
+int dis(data d1,data d2)
 {
-   int readings[3] = {0, 0, 0};
-   led1=1;
-   long int sum2 = 0;
-   for (int i=0; i<10; i++)
-   {
-       accelerometer.getOutput(readings);
-        yval[i]=(readings[1] - 346); //nilai y adxl345
-       sum2 = yval[i]+sum2;
-   }
-   yavg=sum2/10.0;
-   led1=0;     
-   return yavg;
+    return sqrt(pow(((double)d2.getx()-(double)d1.getx()),2.0)+pow(((double)d2.gety()-(double)d1.gety()),2.0));
 }
-int calibratez()
-{
-   int readings[3] = {0, 0, 0};
-    
-   led1=1;
-   long int sum3 = 0;
-   for (int i=0; i<10; i++)
-   {
-       accelerometer.getOutput(readings);
-        zval[i]=(readings[2] - 416); //nilai z adxl345
-       sum3 = zval[i]+sum3;
-   }
-   
-   zavg=sum3/10.0;
-   led1=0;     
-   return zavg;
-}*/
-
-
 
  int main() 
  {
@@ -124,7 +94,7 @@
         int totave    [20] = {0};
         int totvel    [20] = {0};
         int totdist   [20] = {0};
-        int totheight [20] = {0};
+        
             
         
         //float sum1, sum2, sum3 = 0
@@ -140,14 +110,12 @@
        int16_t avg_1= 0;
        int16_t avg_2= 0;
        int16_t avg_3= 0;
+       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);
        
-/*       int16_t lang     = 0;
-       int16_t perc     = 0;
-       int16_t kec      = 0;
-       int16_t jar      = 0;
-       int16_t ting     = 0;
-       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);*/
-       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);
+       int p,q; //input
+       int a[MAX]; //store distances
+       int b[k]; //to get min distances, used in calc
+       int c[k]; //to store freq
 
        
     led1 = 1;
@@ -191,47 +159,42 @@
     accelerometer.setDataRate(ADXL345_3200HZ);
 
     //Measurement mode.
-    accelerometer.setPowerControl(0x08);   
-     
-     /*int avg1 = calibratex();
-     int avg2 = calibratey();
-     int avg3 = calibratez();*/
-     
+    accelerometer.setPowerControl(0x08);
+    
+       
       
      while (1) 
      {
          ble.waitForEvent();
          wait(0.1);
          accelerometer.getOutput(readings);
-/*         uart1.printf("\n%i, %i, %i\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]);
+         /*uart1.printf("\n%i, %i, %i\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]);
          memset(&buffer, 0, sizeof(buffer));
          snprintf(buffer, 20, "data: %d,%d,%d\n\n", (int16_t)readings[0],(int16_t)readings[1],(int16_t)readings[0]);
          ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)buffer, sizeof(buffer), false);*/
          
-         uart1.printf("\n%i, %i, %i\n", (int16_t)avg1, (int16_t)avg2, (int16_t)avg3);
-         memset(&buffer, 0, sizeof(buffer));
-         snprintf(buffer, 20, "data: %d,%d,%d\n\n", (int16_t)avg1,(int16_t)avg2,(int16_t)avg3);
-         ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)buffer, sizeof(buffer), false);
-         wait(0.1);
-         
-         
          
          //float x,y,z
         for (int i=0; i<10; i++)
         {
-            xaccl[i]=(readings[0] - 345);
+            xaccl[i]=(readings[0]);
             wait(0.1);
-            yaccl[i]=(readings[1] - 346);
+            yaccl[i]=(readings[1]);
             wait(0.1);
-            zaccl[i]=(readings[2] - 416);
+            zaccl[i]=(readings[2]);
             wait(0.1);
             
+            
             //formula
-            //totvect[i] = sqrt((double)((xaccl[i]-xavg)* (xaccl[i]-xavg))+(double) ((yaccl[i] - yavg)*(yaccl[i] - yavg)) +(double) ((zaccl[i] - zavg)*(zaccl[i] - zavg)));
-            totvect[i] = sqrt((double)(xaccl[i]*xaccl[i])+(double)(yaccl[i]*yaccl[i])+(double)(zaccl[i]*zaccl[i]));
-            totave[i] = (totvect[i] + totvect[i-1]) / 2 ;
+            totvect[i] = sqrt(pow((double)xaccl[i],2.0)+pow((double)yaccl[i],2.0)+pow((double)zaccl[i],2.0));
+            wait(0.3);
+            totave[i] = ((double)totvect[i]+(double)totvect[i-1])/2.0 ;
+            wait(0.3);
             acc=acc+totave[i];
-         
+            wait(0.3);
+            totvel[i]=(1*((double)totave[i]-(double)totave[i-1]/2.0)+totvel[i-1]);
+            wait(0.3);
+            totdist[i]=(1*((double)totvel[i]-(double)totvel[i-1]/2.0)+totdist[i-1]);         
          
          
          //cal steps
@@ -248,35 +211,129 @@
              {flag=0;}
              uart1.printf("\nsteps:%i", (int16_t)steps);
              memset(&buffer, 0, sizeof(buffer));
-             snprintf(buffer, 20, "\nsteps:%d", (int16_t)steps);
+             snprintf(buffer, 20, "\n%d\t", (int16_t)steps);
              ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)buffer, sizeof(buffer), false);
              
             uart1.printf("\tacc:%i", (int16_t)totave[i]);
             memset(&buffer, 0, sizeof(buffer));
-            snprintf(buffer, 20, "\tacc:%d", (int16_t)totave[i]);
+            snprintf(buffer, 20, "\t%d", (int16_t)totave[i]);
             ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)buffer, sizeof(buffer), false);
         
-        
-/*            totvel[i]=(totave[i]*0.2);
             uart1.printf("\tvel:%i", (int16_t)totvel[i]);
             memset(&buffer, 0, sizeof(buffer));
-            snprintf(buffer, 20, "\tvel:%d", (int16_t)totvel[i]);
+            snprintf(buffer, 20, "\t%d", (int16_t)totvel[i]);
             ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)buffer, sizeof(buffer), false);
 
-            totdist[i]=(totvect[i]*(0.2*0.2)/2);
             uart1.printf("\tdist:%i", (int16_t)totdist[i]);
             memset(&buffer, 0, sizeof(buffer));
-            snprintf(buffer, 20, "\tdist:%d", (int16_t)totdist[i]);
+            snprintf(buffer, 20, "\t%d\n", (int16_t)totdist[i]);
             ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)buffer, sizeof(buffer), false);
   
-            totheight[i]=sqrt((double)(xaccl[i]*xaccl[i])+(double)(yaccl[i]*yaccl[i])) * ((0.2*0.2)/2);
-            uart1.printf("\thght:%i", (int16_t)totheight[i]);
-            memset(&buffer, 0, sizeof(buffer));
-            snprintf(buffer, 20, "\thght:%d", (int16_t)totheight[i]);
-            ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)buffer, sizeof(buffer), false);*/
       
         }
+       
+       do{
+        wait(0.1);
         
+        for(int i=0;i<k;i++)
+        { //initiLIZATION
+            b[i]=-1;
+            c[i]=0;
+        } 
+        int min=1000;
+        p = (readings[0]);
+        q = (readings[1]);
+        //uart1.scanf("%ld,%ld",&p,&q);
+        if((p < 0) | (q < 0))
+            exit(0);
+        data n; //data point to classify
+        n.setd(p,q,STANDING);
+        data d[MAX]; //training set
+        
+        d[0].setd(1,1,STANDING);
+        d[1].setd(1,2,STANDING);
+        d[2].setd(1,3,STANDING);
+        d[3].setd(1,4,STANDING);
+        d[4].setd(1,5,STANDING);
+        d[5].setd(1,6,STANDING);
+        d[6].setd(1,7,STANDING);
+        d[7].setd(2,1,STANDING);
+        d[8].setd(2,2,STANDING);
+        d[9].setd(2,3,WALKING);
+        d[10].setd(2,4,WALKING);
+        d[11].setd(2,5,WALKING);
+        d[12].setd(2,6,WALKING);
+        d[13].setd(2,7,WALKING);
+        d[14].setd(5,1,CLIMBING);
+        d[15].setd(5,2,CLIMBING);
+        d[16].setd(5,3,CLIMBING);
+        d[17].setd(5,4,CLIMBING);
+        d[18].setd(5,5,CLIMBING);
+        d[19].setd(5,6,CLIMBING);
+        for(int i=0;i<20;i++){
+            a[i]=dis(n,d[i]);
+            //uart1.printf("\t\t %d\n", a[i]);
+        }
+        //k-nearest neighbours calculation i.e smallest k distances
+        for(int j=0;j<k;j++)
+        {
+            min=1000;
+            for(int i=0;i<20;i++)
+            {
+                if(i!=b[0]&&i!=b[1]&&i!=b[2])
+                { 
+                    if((a[i]<=min))
+                    {
+                        min=a[i];
+                        b[j]=i;
+                    }
+                }
+            } 
+            
+            //uart1.printf("%d\n",min);
+        }
+        //counting frequency of a class in each neighbour   
+        for(int i=0;i<k;i++)
+        {
+            switch (d[b[i]].getcat())
+            { 
+            case STANDING:
+                c[0]++;
+                break;
+            case WALKING:
+                c[2]++;
+                break;
+            case CLIMBING:
+                c[1]++;
+                break;
+            }
+        } //counting max frequency
+        int max=-1,j;
+        for(int i=0;i<k;i++)
+        {    
+            if(c[i]>max){
+                max=c[i];
+                j=i;
+            }
+        }
+       
+            wait(0.1);
+            printf("Prediction is:");
+            switch (j)
+            {    
+            case 0:
+                uart1.printf("STANDING\n");
+                break;
+            case 1:
+                uart1.printf("CLIMBING\n");
+                break;
+            case 2:
+                uart1.printf("WALKING\n");
+                break;
+            }
+         
+        
+    }while(true); 
         
     }
  
--- a/mbed.bld	Mon Feb 19 09:11:16 2018 +0000
+++ b/mbed.bld	Wed Apr 18 05:02:24 2018 +0000
@@ -1,1 +1,1 @@
-https://os.mbed.com/users/mbed_official/code/mbed/builds/7130f322cb7e
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/7130f322cb7e
\ No newline at end of file