this is program how build nRF51822 to get ADXL345 data
Dependencies: BLE_API mbed nRF51822
Fork of ADXL345_I2C by
main.cpp@19:5b0da580c77e, 2018-04-18 (annotated)
- Committer:
- asyrofi
- Date:
- Wed Apr 18 05:02:24 2018 +0000
- Revision:
- 19:5b0da580c77e
- Parent:
- 17:75e827cd0923
masih salah tolong diperbaiki..
Who changed what in which revision?
User | Revision | Line number | New 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 | 19:5b0da580c77e | 6 | #include <math.h> |
asyrofi | 19:5b0da580c77e | 7 | #include <stdlib.h> |
asyrofi | 19:5b0da580c77e | 8 | #define MAX 20 |
asyrofi | 19:5b0da580c77e | 9 | #define k 4 |
asyrofi | 2:99e00e9c5035 | 10 | |
asyrofi | 3:1749778af065 | 11 | #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console; |
asyrofi | 2:99e00e9c5035 | 12 | * it will have an impact on code-size and power consumption. */ |
asyrofi | 2:99e00e9c5035 | 13 | |
asyrofi | 2:99e00e9c5035 | 14 | #if NEED_CONSOLE_OUTPUT |
asyrofi | 2:99e00e9c5035 | 15 | #define DEBUG(...) { printf(__VA_ARGS__); } |
asyrofi | 2:99e00e9c5035 | 16 | #else |
asyrofi | 2:99e00e9c5035 | 17 | #define DEBUG(...) /* nothing */ |
asyrofi | 2:99e00e9c5035 | 18 | #endif /* #if NEED_CONSOLE_OUTPUT */ |
peterswanson87 | 0:d0adb548714f | 19 | |
asyrofi | 4:a57b495be9fa | 20 | ADXL345_I2C accelerometer(p30, p7); |
asyrofi | 2:99e00e9c5035 | 21 | BLEDevice ble; |
asyrofi | 2:99e00e9c5035 | 22 | DigitalOut led1(LED1); |
asyrofi | 3:1749778af065 | 23 | Serial uart1(USBTX,USBRX); |
asyrofi | 2:99e00e9c5035 | 24 | UARTService *uartServicePtr; |
asyrofi | 6:bc835d0f686f | 25 | |
asyrofi | 6:bc835d0f686f | 26 | |
asyrofi | 6:bc835d0f686f | 27 | |
asyrofi | 2:99e00e9c5035 | 28 | |
asyrofi | 2:99e00e9c5035 | 29 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
asyrofi | 2:99e00e9c5035 | 30 | { |
asyrofi | 2:99e00e9c5035 | 31 | DEBUG("Disconnected!\n\r"); |
asyrofi | 2:99e00e9c5035 | 32 | DEBUG("Restarting the advertising process\n\r"); |
asyrofi | 2:99e00e9c5035 | 33 | ble.startAdvertising(); |
asyrofi | 2:99e00e9c5035 | 34 | } |
asyrofi | 2:99e00e9c5035 | 35 | |
asyrofi | 3:1749778af065 | 36 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) { |
asyrofi | 3:1749778af065 | 37 | |
asyrofi | 3:1749778af065 | 38 | DEBUG("Connected!\n\r"); |
asyrofi | 3:1749778af065 | 39 | |
asyrofi | 3:1749778af065 | 40 | } |
asyrofi | 3:1749778af065 | 41 | |
asyrofi | 3:1749778af065 | 42 | uint8_t b[40]={'a','d','q','w'}; |
asyrofi | 2:99e00e9c5035 | 43 | void onDataWritten(const GattWriteCallbackParams *params) |
asyrofi | 2:99e00e9c5035 | 44 | { |
asyrofi | 2:99e00e9c5035 | 45 | if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) { |
asyrofi | 2:99e00e9c5035 | 46 | uint16_t bytesRead = params->len; |
asyrofi | 3:1749778af065 | 47 | DEBUG("received %u bytes %s\n\r", bytesRead,params->data); |
asyrofi | 2:99e00e9c5035 | 48 | } |
asyrofi | 2:99e00e9c5035 | 49 | } |
asyrofi | 2:99e00e9c5035 | 50 | |
asyrofi | 2:99e00e9c5035 | 51 | void periodicCallback(void) |
asyrofi | 2:99e00e9c5035 | 52 | { |
asyrofi | 2:99e00e9c5035 | 53 | led1 = !led1; |
asyrofi | 2:99e00e9c5035 | 54 | } |
asyrofi | 10:e5ee9515f4a2 | 55 | |
asyrofi | 16:e97016b6a7e0 | 56 | int threshold =6.8; |
asyrofi | 11:a60e398f9032 | 57 | int xval[10]={0}; |
asyrofi | 11:a60e398f9032 | 58 | int yval[10]={0}; |
asyrofi | 11:a60e398f9032 | 59 | int zval[10]={0}; |
asyrofi | 10:e5ee9515f4a2 | 60 | |
asyrofi | 19:5b0da580c77e | 61 | using namespace std; |
asyrofi | 19:5b0da580c77e | 62 | enum category{STANDING,CLIMBING,WALKING}; |
asyrofi | 19:5b0da580c77e | 63 | class data{ |
asyrofi | 19:5b0da580c77e | 64 | int x,y; |
asyrofi | 19:5b0da580c77e | 65 | category cat; |
asyrofi | 19:5b0da580c77e | 66 | public: |
asyrofi | 19:5b0da580c77e | 67 | void setd(int a,int b,category c){ |
asyrofi | 19:5b0da580c77e | 68 | x=a; |
asyrofi | 19:5b0da580c77e | 69 | y=b; |
asyrofi | 19:5b0da580c77e | 70 | cat=c; |
asyrofi | 19:5b0da580c77e | 71 | } |
asyrofi | 19:5b0da580c77e | 72 | int getx(){return x;} |
asyrofi | 19:5b0da580c77e | 73 | int gety(){return y;} |
asyrofi | 19:5b0da580c77e | 74 | category getcat(){ |
asyrofi | 19:5b0da580c77e | 75 | return cat; |
asyrofi | 19:5b0da580c77e | 76 | } |
asyrofi | 19:5b0da580c77e | 77 | };//end of class |
asyrofi | 19:5b0da580c77e | 78 | int dis(data d1,data d2) |
asyrofi | 10:e5ee9515f4a2 | 79 | { |
asyrofi | 19:5b0da580c77e | 80 | return sqrt(pow(((double)d2.getx()-(double)d1.getx()),2.0)+pow(((double)d2.gety()-(double)d1.gety()),2.0)); |
asyrofi | 10:e5ee9515f4a2 | 81 | } |
asyrofi | 3:1749778af065 | 82 | |
asyrofi | 6:bc835d0f686f | 83 | int main() |
asyrofi | 6:bc835d0f686f | 84 | { |
asyrofi | 4:a57b495be9fa | 85 | uart1.baud(9600); |
peterswanson87 | 0:d0adb548714f | 86 | int readings[3] = {0, 0, 0}; |
asyrofi | 10:e5ee9515f4a2 | 87 | char buffer [20]; |
asyrofi | 6:bc835d0f686f | 88 | |
asyrofi | 10:e5ee9515f4a2 | 89 | //inisiliasi |
asyrofi | 10:e5ee9515f4a2 | 90 | int steps=0; |
asyrofi | 10:e5ee9515f4a2 | 91 | int flag=0; |
asyrofi | 6:bc835d0f686f | 92 | int acc=0; |
asyrofi | 10:e5ee9515f4a2 | 93 | int totvect [20] = {0}; |
asyrofi | 10:e5ee9515f4a2 | 94 | int totave [20] = {0}; |
asyrofi | 10:e5ee9515f4a2 | 95 | int totvel [20] = {0}; |
asyrofi | 10:e5ee9515f4a2 | 96 | int totdist [20] = {0}; |
asyrofi | 19:5b0da580c77e | 97 | |
asyrofi | 10:e5ee9515f4a2 | 98 | |
asyrofi | 6:bc835d0f686f | 99 | |
asyrofi | 6:bc835d0f686f | 100 | //float sum1, sum2, sum3 = 0 |
asyrofi | 10:e5ee9515f4a2 | 101 | int xaccl[20]; |
asyrofi | 10:e5ee9515f4a2 | 102 | int yaccl[20]; |
asyrofi | 10:e5ee9515f4a2 | 103 | int zaccl[20]; |
asyrofi | 9:1e04b80d7199 | 104 | |
asyrofi | 9:1e04b80d7199 | 105 | // Test Daata |
asyrofi | 9:1e04b80d7199 | 106 | memset(&buffer, 0, sizeof(buffer)); |
asyrofi | 16:e97016b6a7e0 | 107 | int16_t reading_1= 0; |
asyrofi | 11:a60e398f9032 | 108 | int16_t reading_2= 0; |
asyrofi | 11:a60e398f9032 | 109 | int16_t reading_3= 0; |
asyrofi | 11:a60e398f9032 | 110 | int16_t avg_1= 0; |
asyrofi | 11:a60e398f9032 | 111 | int16_t avg_2= 0; |
asyrofi | 16:e97016b6a7e0 | 112 | int16_t avg_3= 0; |
asyrofi | 19:5b0da580c77e | 113 | 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 | 114 | |
asyrofi | 19:5b0da580c77e | 115 | int p,q; //input |
asyrofi | 19:5b0da580c77e | 116 | int a[MAX]; //store distances |
asyrofi | 19:5b0da580c77e | 117 | int b[k]; //to get min distances, used in calc |
asyrofi | 19:5b0da580c77e | 118 | int c[k]; //to store freq |
asyrofi | 11:a60e398f9032 | 119 | |
asyrofi | 9:1e04b80d7199 | 120 | |
asyrofi | 2:99e00e9c5035 | 121 | led1 = 1; |
asyrofi | 3:1749778af065 | 122 | uart1.baud(9600); |
asyrofi | 2:99e00e9c5035 | 123 | Ticker ticker; |
asyrofi | 2:99e00e9c5035 | 124 | ticker.attach(periodicCallback, 1); |
asyrofi | 6:bc835d0f686f | 125 | |
asyrofi | 2:99e00e9c5035 | 126 | DEBUG("Initialising the nRF51822\n\r"); |
asyrofi | 2:99e00e9c5035 | 127 | ble.init(); |
asyrofi | 2:99e00e9c5035 | 128 | ble.onDisconnection(disconnectionCallback); |
asyrofi | 3:1749778af065 | 129 | ble.onConnection(connectionCallback); |
asyrofi | 2:99e00e9c5035 | 130 | ble.onDataWritten(onDataWritten); |
asyrofi | 2:99e00e9c5035 | 131 | |
asyrofi | 2:99e00e9c5035 | 132 | /* setup advertising */ |
asyrofi | 2:99e00e9c5035 | 133 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
asyrofi | 2:99e00e9c5035 | 134 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
asyrofi | 2:99e00e9c5035 | 135 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
asyrofi | 2:99e00e9c5035 | 136 | (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1); |
asyrofi | 2:99e00e9c5035 | 137 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
asyrofi | 2:99e00e9c5035 | 138 | (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
asyrofi | 2:99e00e9c5035 | 139 | |
asyrofi | 2:99e00e9c5035 | 140 | ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */ |
asyrofi | 2:99e00e9c5035 | 141 | ble.startAdvertising(); |
asyrofi | 2:99e00e9c5035 | 142 | |
asyrofi | 2:99e00e9c5035 | 143 | UARTService uartService(ble); |
asyrofi | 2:99e00e9c5035 | 144 | uartServicePtr = &uartService; |
asyrofi | 10:e5ee9515f4a2 | 145 | |
asyrofi | 10:e5ee9515f4a2 | 146 | uart1.printf("Starting ADXL345 test...\n"); |
asyrofi | 10:e5ee9515f4a2 | 147 | wait(0.1); |
asyrofi | 10:e5ee9515f4a2 | 148 | uart1.printf("Device ID is: 0x%02x\n", accelerometer.getDeviceID()); |
asyrofi | 10:e5ee9515f4a2 | 149 | wait(0.1); |
asyrofi | 10:e5ee9515f4a2 | 150 | |
asyrofi | 10:e5ee9515f4a2 | 151 | |
asyrofi | 10:e5ee9515f4a2 | 152 | //Go into standby mode to configure the device. |
asyrofi | 10:e5ee9515f4a2 | 153 | accelerometer.setPowerControl(0x00); |
asyrofi | 10:e5ee9515f4a2 | 154 | |
asyrofi | 10:e5ee9515f4a2 | 155 | //Full resolution, +/-16g, 4mg/LSB. |
asyrofi | 10:e5ee9515f4a2 | 156 | accelerometer.setDataFormatControl(0x0B); |
asyrofi | 10:e5ee9515f4a2 | 157 | |
asyrofi | 10:e5ee9515f4a2 | 158 | //3.2kHz data rate. |
asyrofi | 10:e5ee9515f4a2 | 159 | accelerometer.setDataRate(ADXL345_3200HZ); |
asyrofi | 10:e5ee9515f4a2 | 160 | |
asyrofi | 10:e5ee9515f4a2 | 161 | //Measurement mode. |
asyrofi | 19:5b0da580c77e | 162 | accelerometer.setPowerControl(0x08); |
asyrofi | 19:5b0da580c77e | 163 | |
asyrofi | 19:5b0da580c77e | 164 | |
asyrofi | 10:e5ee9515f4a2 | 165 | |
asyrofi | 4:a57b495be9fa | 166 | while (1) |
asyrofi | 4:a57b495be9fa | 167 | { |
asyrofi | 2:99e00e9c5035 | 168 | ble.waitForEvent(); |
peterswanson87 | 0:d0adb548714f | 169 | wait(0.1); |
peterswanson87 | 0:d0adb548714f | 170 | accelerometer.getOutput(readings); |
asyrofi | 19:5b0da580c77e | 171 | /*uart1.printf("\n%i, %i, %i\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]); |
asyrofi | 16:e97016b6a7e0 | 172 | memset(&buffer, 0, sizeof(buffer)); |
asyrofi | 11:a60e398f9032 | 173 | snprintf(buffer, 20, "data: %d,%d,%d\n\n", (int16_t)readings[0],(int16_t)readings[1],(int16_t)readings[0]); |
asyrofi | 11:a60e398f9032 | 174 | ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)buffer, sizeof(buffer), false);*/ |
asyrofi | 11:a60e398f9032 | 175 | |
asyrofi | 6:bc835d0f686f | 176 | |
asyrofi | 10:e5ee9515f4a2 | 177 | //float x,y,z |
asyrofi | 10:e5ee9515f4a2 | 178 | for (int i=0; i<10; i++) |
asyrofi | 10:e5ee9515f4a2 | 179 | { |
asyrofi | 19:5b0da580c77e | 180 | xaccl[i]=(readings[0]); |
asyrofi | 10:e5ee9515f4a2 | 181 | wait(0.1); |
asyrofi | 19:5b0da580c77e | 182 | yaccl[i]=(readings[1]); |
asyrofi | 10:e5ee9515f4a2 | 183 | wait(0.1); |
asyrofi | 19:5b0da580c77e | 184 | zaccl[i]=(readings[2]); |
asyrofi | 10:e5ee9515f4a2 | 185 | wait(0.1); |
asyrofi | 10:e5ee9515f4a2 | 186 | |
asyrofi | 19:5b0da580c77e | 187 | |
asyrofi | 10:e5ee9515f4a2 | 188 | //formula |
asyrofi | 19:5b0da580c77e | 189 | totvect[i] = sqrt(pow((double)xaccl[i],2.0)+pow((double)yaccl[i],2.0)+pow((double)zaccl[i],2.0)); |
asyrofi | 19:5b0da580c77e | 190 | wait(0.3); |
asyrofi | 19:5b0da580c77e | 191 | totave[i] = ((double)totvect[i]+(double)totvect[i-1])/2.0 ; |
asyrofi | 19:5b0da580c77e | 192 | wait(0.3); |
asyrofi | 10:e5ee9515f4a2 | 193 | acc=acc+totave[i]; |
asyrofi | 19:5b0da580c77e | 194 | wait(0.3); |
asyrofi | 19:5b0da580c77e | 195 | totvel[i]=(1*((double)totave[i]-(double)totave[i-1]/2.0)+totvel[i-1]); |
asyrofi | 19:5b0da580c77e | 196 | wait(0.3); |
asyrofi | 19:5b0da580c77e | 197 | totdist[i]=(1*((double)totvel[i]-(double)totvel[i-1]/2.0)+totdist[i-1]); |
asyrofi | 10:e5ee9515f4a2 | 198 | |
asyrofi | 10:e5ee9515f4a2 | 199 | |
asyrofi | 10:e5ee9515f4a2 | 200 | //cal steps |
asyrofi | 10:e5ee9515f4a2 | 201 | if (totave[i] > threshold && flag==0) |
asyrofi | 10:e5ee9515f4a2 | 202 | { |
asyrofi | 10:e5ee9515f4a2 | 203 | steps = steps+1; |
asyrofi | 10:e5ee9515f4a2 | 204 | flag=1; |
asyrofi | 10:e5ee9515f4a2 | 205 | } |
asyrofi | 10:e5ee9515f4a2 | 206 | else if (totave[i] > threshold && flag == 1) |
asyrofi | 10:e5ee9515f4a2 | 207 | { |
asyrofi | 10:e5ee9515f4a2 | 208 | // do nothing |
asyrofi | 10:e5ee9515f4a2 | 209 | } |
asyrofi | 10:e5ee9515f4a2 | 210 | if (totave[i] < threshold && flag == 1) |
asyrofi | 10:e5ee9515f4a2 | 211 | {flag=0;} |
asyrofi | 13:e5327d2de406 | 212 | uart1.printf("\nsteps:%i", (int16_t)steps); |
asyrofi | 11:a60e398f9032 | 213 | memset(&buffer, 0, sizeof(buffer)); |
asyrofi | 19:5b0da580c77e | 214 | snprintf(buffer, 20, "\n%d\t", (int16_t)steps); |
asyrofi | 11:a60e398f9032 | 215 | ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)buffer, sizeof(buffer), false); |
asyrofi | 10:e5ee9515f4a2 | 216 | |
asyrofi | 16:e97016b6a7e0 | 217 | uart1.printf("\tacc:%i", (int16_t)totave[i]); |
asyrofi | 16:e97016b6a7e0 | 218 | memset(&buffer, 0, sizeof(buffer)); |
asyrofi | 19:5b0da580c77e | 219 | snprintf(buffer, 20, "\t%d", (int16_t)totave[i]); |
asyrofi | 16:e97016b6a7e0 | 220 | ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)buffer, sizeof(buffer), false); |
asyrofi | 16:e97016b6a7e0 | 221 | |
asyrofi | 16:e97016b6a7e0 | 222 | uart1.printf("\tvel:%i", (int16_t)totvel[i]); |
asyrofi | 16:e97016b6a7e0 | 223 | memset(&buffer, 0, sizeof(buffer)); |
asyrofi | 19:5b0da580c77e | 224 | snprintf(buffer, 20, "\t%d", (int16_t)totvel[i]); |
asyrofi | 16:e97016b6a7e0 | 225 | ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)buffer, sizeof(buffer), false); |
asyrofi | 16:e97016b6a7e0 | 226 | |
asyrofi | 16:e97016b6a7e0 | 227 | uart1.printf("\tdist:%i", (int16_t)totdist[i]); |
asyrofi | 16:e97016b6a7e0 | 228 | memset(&buffer, 0, sizeof(buffer)); |
asyrofi | 19:5b0da580c77e | 229 | snprintf(buffer, 20, "\t%d\n", (int16_t)totdist[i]); |
asyrofi | 16:e97016b6a7e0 | 230 | ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)buffer, sizeof(buffer), false); |
asyrofi | 16:e97016b6a7e0 | 231 | |
asyrofi | 16:e97016b6a7e0 | 232 | |
asyrofi | 10:e5ee9515f4a2 | 233 | } |
asyrofi | 19:5b0da580c77e | 234 | |
asyrofi | 19:5b0da580c77e | 235 | do{ |
asyrofi | 19:5b0da580c77e | 236 | wait(0.1); |
asyrofi | 16:e97016b6a7e0 | 237 | |
asyrofi | 19:5b0da580c77e | 238 | for(int i=0;i<k;i++) |
asyrofi | 19:5b0da580c77e | 239 | { //initiLIZATION |
asyrofi | 19:5b0da580c77e | 240 | b[i]=-1; |
asyrofi | 19:5b0da580c77e | 241 | c[i]=0; |
asyrofi | 19:5b0da580c77e | 242 | } |
asyrofi | 19:5b0da580c77e | 243 | int min=1000; |
asyrofi | 19:5b0da580c77e | 244 | p = (readings[0]); |
asyrofi | 19:5b0da580c77e | 245 | q = (readings[1]); |
asyrofi | 19:5b0da580c77e | 246 | //uart1.scanf("%ld,%ld",&p,&q); |
asyrofi | 19:5b0da580c77e | 247 | if((p < 0) | (q < 0)) |
asyrofi | 19:5b0da580c77e | 248 | exit(0); |
asyrofi | 19:5b0da580c77e | 249 | data n; //data point to classify |
asyrofi | 19:5b0da580c77e | 250 | n.setd(p,q,STANDING); |
asyrofi | 19:5b0da580c77e | 251 | data d[MAX]; //training set |
asyrofi | 19:5b0da580c77e | 252 | |
asyrofi | 19:5b0da580c77e | 253 | d[0].setd(1,1,STANDING); |
asyrofi | 19:5b0da580c77e | 254 | d[1].setd(1,2,STANDING); |
asyrofi | 19:5b0da580c77e | 255 | d[2].setd(1,3,STANDING); |
asyrofi | 19:5b0da580c77e | 256 | d[3].setd(1,4,STANDING); |
asyrofi | 19:5b0da580c77e | 257 | d[4].setd(1,5,STANDING); |
asyrofi | 19:5b0da580c77e | 258 | d[5].setd(1,6,STANDING); |
asyrofi | 19:5b0da580c77e | 259 | d[6].setd(1,7,STANDING); |
asyrofi | 19:5b0da580c77e | 260 | d[7].setd(2,1,STANDING); |
asyrofi | 19:5b0da580c77e | 261 | d[8].setd(2,2,STANDING); |
asyrofi | 19:5b0da580c77e | 262 | d[9].setd(2,3,WALKING); |
asyrofi | 19:5b0da580c77e | 263 | d[10].setd(2,4,WALKING); |
asyrofi | 19:5b0da580c77e | 264 | d[11].setd(2,5,WALKING); |
asyrofi | 19:5b0da580c77e | 265 | d[12].setd(2,6,WALKING); |
asyrofi | 19:5b0da580c77e | 266 | d[13].setd(2,7,WALKING); |
asyrofi | 19:5b0da580c77e | 267 | d[14].setd(5,1,CLIMBING); |
asyrofi | 19:5b0da580c77e | 268 | d[15].setd(5,2,CLIMBING); |
asyrofi | 19:5b0da580c77e | 269 | d[16].setd(5,3,CLIMBING); |
asyrofi | 19:5b0da580c77e | 270 | d[17].setd(5,4,CLIMBING); |
asyrofi | 19:5b0da580c77e | 271 | d[18].setd(5,5,CLIMBING); |
asyrofi | 19:5b0da580c77e | 272 | d[19].setd(5,6,CLIMBING); |
asyrofi | 19:5b0da580c77e | 273 | for(int i=0;i<20;i++){ |
asyrofi | 19:5b0da580c77e | 274 | a[i]=dis(n,d[i]); |
asyrofi | 19:5b0da580c77e | 275 | //uart1.printf("\t\t %d\n", a[i]); |
asyrofi | 19:5b0da580c77e | 276 | } |
asyrofi | 19:5b0da580c77e | 277 | //k-nearest neighbours calculation i.e smallest k distances |
asyrofi | 19:5b0da580c77e | 278 | for(int j=0;j<k;j++) |
asyrofi | 19:5b0da580c77e | 279 | { |
asyrofi | 19:5b0da580c77e | 280 | min=1000; |
asyrofi | 19:5b0da580c77e | 281 | for(int i=0;i<20;i++) |
asyrofi | 19:5b0da580c77e | 282 | { |
asyrofi | 19:5b0da580c77e | 283 | if(i!=b[0]&&i!=b[1]&&i!=b[2]) |
asyrofi | 19:5b0da580c77e | 284 | { |
asyrofi | 19:5b0da580c77e | 285 | if((a[i]<=min)) |
asyrofi | 19:5b0da580c77e | 286 | { |
asyrofi | 19:5b0da580c77e | 287 | min=a[i]; |
asyrofi | 19:5b0da580c77e | 288 | b[j]=i; |
asyrofi | 19:5b0da580c77e | 289 | } |
asyrofi | 19:5b0da580c77e | 290 | } |
asyrofi | 19:5b0da580c77e | 291 | } |
asyrofi | 19:5b0da580c77e | 292 | |
asyrofi | 19:5b0da580c77e | 293 | //uart1.printf("%d\n",min); |
asyrofi | 19:5b0da580c77e | 294 | } |
asyrofi | 19:5b0da580c77e | 295 | //counting frequency of a class in each neighbour |
asyrofi | 19:5b0da580c77e | 296 | for(int i=0;i<k;i++) |
asyrofi | 19:5b0da580c77e | 297 | { |
asyrofi | 19:5b0da580c77e | 298 | switch (d[b[i]].getcat()) |
asyrofi | 19:5b0da580c77e | 299 | { |
asyrofi | 19:5b0da580c77e | 300 | case STANDING: |
asyrofi | 19:5b0da580c77e | 301 | c[0]++; |
asyrofi | 19:5b0da580c77e | 302 | break; |
asyrofi | 19:5b0da580c77e | 303 | case WALKING: |
asyrofi | 19:5b0da580c77e | 304 | c[2]++; |
asyrofi | 19:5b0da580c77e | 305 | break; |
asyrofi | 19:5b0da580c77e | 306 | case CLIMBING: |
asyrofi | 19:5b0da580c77e | 307 | c[1]++; |
asyrofi | 19:5b0da580c77e | 308 | break; |
asyrofi | 19:5b0da580c77e | 309 | } |
asyrofi | 19:5b0da580c77e | 310 | } //counting max frequency |
asyrofi | 19:5b0da580c77e | 311 | int max=-1,j; |
asyrofi | 19:5b0da580c77e | 312 | for(int i=0;i<k;i++) |
asyrofi | 19:5b0da580c77e | 313 | { |
asyrofi | 19:5b0da580c77e | 314 | if(c[i]>max){ |
asyrofi | 19:5b0da580c77e | 315 | max=c[i]; |
asyrofi | 19:5b0da580c77e | 316 | j=i; |
asyrofi | 19:5b0da580c77e | 317 | } |
asyrofi | 19:5b0da580c77e | 318 | } |
asyrofi | 19:5b0da580c77e | 319 | |
asyrofi | 19:5b0da580c77e | 320 | wait(0.1); |
asyrofi | 19:5b0da580c77e | 321 | printf("Prediction is:"); |
asyrofi | 19:5b0da580c77e | 322 | switch (j) |
asyrofi | 19:5b0da580c77e | 323 | { |
asyrofi | 19:5b0da580c77e | 324 | case 0: |
asyrofi | 19:5b0da580c77e | 325 | uart1.printf("STANDING\n"); |
asyrofi | 19:5b0da580c77e | 326 | break; |
asyrofi | 19:5b0da580c77e | 327 | case 1: |
asyrofi | 19:5b0da580c77e | 328 | uart1.printf("CLIMBING\n"); |
asyrofi | 19:5b0da580c77e | 329 | break; |
asyrofi | 19:5b0da580c77e | 330 | case 2: |
asyrofi | 19:5b0da580c77e | 331 | uart1.printf("WALKING\n"); |
asyrofi | 19:5b0da580c77e | 332 | break; |
asyrofi | 19:5b0da580c77e | 333 | } |
asyrofi | 19:5b0da580c77e | 334 | |
asyrofi | 19:5b0da580c77e | 335 | |
asyrofi | 19:5b0da580c77e | 336 | }while(true); |
asyrofi | 16:e97016b6a7e0 | 337 | |
asyrofi | 6:bc835d0f686f | 338 | } |
peterswanson87 | 0:d0adb548714f | 339 | |
peterswanson87 | 0:d0adb548714f | 340 | } |
asyrofi | 6:bc835d0f686f | 341 |