Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / soft-sensor-manager / SampleApp / arduino / Trackee_Thing / src / proximity.cpp
1 /******************************************************************
2 *
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
4 *
5 *
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 *      http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 ******************************************************************/
20
21 #include "Arduino.h"
22 #include "proximity.h"
23 #include <math.h>
24
25 // Proximity code start
26 float CalculateExponentialAverage(int numberOfSamples, int *array, int startindex, int flag)
27 {
28     float numerator = 0;
29     float denominator = 0;
30
31     float average = 0.0;
32
33     if (flag < arraysize / RSSI_EA)   // first loop buffer full
34     {
35         for (int i = 0; i < startindex; i++)
36         {
37             average += array[i];
38         }
39         if (startindex == 0) {}
40         else
41         {
42             average = average / startindex;
43         }
44         //       Serial.print("exp: ");
45         Serial.print("average1 : ");
46         Serial.println(average);
47     }
48     else
49     {
50         for (int i = 0; i < arraysize; i++)
51         {
52             average += array[i];
53         }
54
55         for (int i = startindex; i < startindex + RSSI_EA; i++)
56         {
57             average -= array[i];
58         }
59         average = average / (arraysize - numberOfSamples);
60         //            Serial.print("exp: ");
61         Serial.print("average2 : ");
62         Serial.println(average);
63     }
64     //exponential moving average
65     int i = 0;
66     //CHANGE THIS FOR DIFFERENT SMOOTHING EFFECT
67     float beta = 0.8f;
68     for (i = startindex + numberOfSamples - 1; i >= startindex; i--)
69     {
70         numerator += array[i] * pow(beta, startindex + numberOfSamples - i - 1);
71         denominator += pow(beta, startindex + numberOfSamples - i - 1);
72     }
73
74     int offset = 3;
75     if (average != 0.0)
76     {
77         numerator += average * pow(beta, offset + numberOfSamples);
78         denominator += pow(beta, offset + numberOfSamples);
79     }
80     return numerator / denominator;
81 }
82
83
84 float calculateDistance(float avgRSSI, float txPower)
85 {
86     if (avgRSSI == 0)
87     {
88         return -1.0;
89     }
90
91     float ratio = avgRSSI * 1.0 / txPower;
92     if (ratio < 1.0)
93     {
94         return pow(ratio, 10);
95     }
96     else
97     {
98         float distance =  (0.7) * pow(ratio, 10) + 0.024;
99         return distance;
100     }
101 }
102
103 // proximity code end
104