forked from CodeForCuritiba/WeAreAllSmart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ultrasonic.cpp
195 lines (167 loc) · 4.25 KB
/
Ultrasonic.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Ultrasonic.cpp - Library for HC-SR04 Ultrasonic Sensing Module.
*
* With ideas from:
* Created by ITead studio. Apr 20, 2010.
* iteadstudio.com
*
* SVN Keywords
* ----------------------------------
* $Author: cnobile $
* $Date: 2011-10-08 21:07:42 -0400 (Sat, 08 Oct 2011) $
* $Revision: 35 $
* ----------------------------------
*
* Centimeters Divisor
* =========== =======
* 15.875 26.9029
* 46.355 27.6233
* 92.075 28.1949
* 137.795 28.4717
* 183.515 28.5584
* 229.235 28.5936
* 274.955 28.7194
*
* Inches Divisor
* ====== =======
* 6.25 68.3333
* 18.25 70.1633
* 36.25 71.6152
* 54.25 72.3182
* 72.25 72.5384
* 90.25 72.6277
* 108.25 72.9473
*/
#include <stdlib.h>
#include <string.h>
#include <Ultrasonic.h>
Ultrasonic::Ultrasonic(int tp, int ep)
{
pinMode(tp, OUTPUT);
pinMode(ep, INPUT);
_trigPin = tp;
_echoPin = ep;
_cmDivisor = 27.6233;
_inDivisor = 70.1633;
}
long Ultrasonic::timing()
{
digitalWrite(_trigPin, LOW);
delayMicroseconds(2);
digitalWrite(_trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(_trigPin, LOW);
return pulseIn(_echoPin, HIGH);
}
float Ultrasonic::convert(long microsec, int metric)
{
// microsec / 29 / 2;
if(metric) return microsec / _cmDivisor / 2.0; // CM
// microsec / 74 / 2;
else return microsec / _inDivisor / 2.0; // IN
}
void Ultrasonic::setDivisor(float value, int metric)
{
if(metric) _cmDivisor = value;
else _inDivisor = value;
}
#ifdef COMPILE_STD_DEV
bool Ultrasonic::sampleCreate(size_t numBufs, ...)
{
bool result = false;
va_list ap;
_numBufs = numBufs;
if((_pBuffers = (BufCtl *) calloc(numBufs, sizeof(BufCtl))) != NULL)
{
va_start(ap, numBufs);
BufCtl *buf;
size_t smpSize;
for(size_t i = 0; i < _numBufs; i++)
{
buf = &_pBuffers[i];
smpSize = va_arg(ap, size_t);
if((buf->pBegin = (float *) calloc(smpSize, sizeof(float))) != NULL)
{
buf->pIndex = buf->pBegin;
buf->length = smpSize;
buf->filled = false;
result = true;
}
else
{
result = false;
break;
}
}
va_end(ap);
}
if(!result) _freeBuffers();
return result;
}
void Ultrasonic::sampleClear()
{
if(_pBuffers)
{
BufCtl *buf;
for(size_t i = 0; i < _numBufs; i++)
{
buf = &_pBuffers[i];
memset(buf, '\0', sizeof(float) * buf->length);
buf->pIndex = buf->pBegin;
buf->filled = false;
}
}
}
float Ultrasonic::unbiasedStdDev(float value, size_t bufNum)
{
float result = 0.0;
if(_pBuffers)
{
BufCtl *buf = &_pBuffers[bufNum];
if(buf->length > 1)
{
_sampleUpdate(buf, float(value));
if(buf->filled)
{
float sum = 0.0, mean, tmp;
for(size_t i = 0; i < buf->length; i++)
sum += buf->pBegin[i];
mean = sum / buf->length;
sum = 0.0;
for(size_t i = 0; i < buf->length; i++)
{
tmp = buf->pBegin[i] - mean;
sum += (tmp * tmp);
}
result = sqrt(sum / (buf->length - 1));
//Serial.print(bufNum);
//Serial.print(" : ");
//Serial.println(result);
}
}
}
return result;
}
void Ultrasonic::_sampleUpdate(BufCtl *buf, float msec)
{
if(buf->pIndex >= (buf->pBegin + buf->length))
{
buf->pIndex = buf->pBegin;
buf->filled = true;
}
*(buf->pIndex++) = msec;
}
void Ultrasonic::_freeBuffers()
{
if(_pBuffers)
{
BufCtl *buf;
for(size_t i = 0; i < _numBufs; i++)
{
buf = &_pBuffers[i];
free(buf->pBegin);
}
free(_pBuffers);
}
}
#endif // COMPILE_STD_DEV