-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathSensorWind.cpp
247 lines (196 loc) · 7.3 KB
/
SensorWind.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include "SensorWind.h"
//Initialize
SensorWind::SensorWind(int interrupt_pin, double calibration, const uint8_t direction_pin)
{
w_calibration=calibration;
w_dir_pin=direction_pin;
w_int_pin=interrupt_pin;
wind=0;
lastWindIRQ = millis();
t_min=36000000;
t_previous=0;
t_interval=0;
control=0;
}
//---------------------------------------- Get Wind Speed --------------------------------------
float SensorWind::getWindSpeed(long minutes)
{
float windSpeed=0;
float period=0;
float deltaTime = (float)minutes*60; // Convert minutes to seconds
if (wind == 0)
return 0; // no interrupts return 0 kmh
if (deltaTime > 0)
{
period = deltaTime/(float)wind;
windSpeed = 2.4 / period;
}
return (windSpeed); // 1 interrupt = 2.4 kPh
}
//----------------------------------------------------------------------------------------------
//---------------------------------- Increment Wind Direction Indice ---------------------------
int SensorWind::incrementWindIndice()
{
unsigned int indice=getWindIndice();
if (indice!=-1)
windPos[indice]++; // Wind Direction (º)
return indice;
}
//----------------------------------------------------------------------------------------------
//-------------------------------- Returns the Wind Direction Indice ---------------------------
int SensorWind::getWindIndice()
{
// The following table is ADC readings for the wind direction sensor output, sorted from low to high.
// Each threshold is the midpoint between adjacent headings. The output is degrees for that ADC reading.
// Note that these are not in compass degree order! See Weather Meters datasheet for more information.
unsigned int adc = w_calibration*analogRead(w_dir_pin);
if (adc < 380) return (5);
if (adc < 393) return (3);
if (adc < 414) return (4);
if (adc < 456) return (7);
if (adc < 508) return (6);
if (adc < 551) return (9);
if (adc < 615) return (8);
if (adc < 680) return (1);
if (adc < 746) return (2);
if (adc < 801) return (11);
if (adc < 833) return (10);
if (adc < 878) return (15);
if (adc < 913) return (0);
if (adc < 940) return (13);
if (adc < 967) return (14);
if (adc < 990) return (12);
return (-1);
}
//----------------------------------------------------------------------------------------------
//-------------------------------- Returns the Wind Direction Indice ---------------------------
float SensorWind::getWindDirection()
{
#ifdef DEBUG_UI
// For testing
Serial.println(""); Serial.println(" |||||| WIND POSICION |||||| ");
#endif
int count=0;
int indice=0;
// Get each minute wind direction
for (int i=0; i<16; i++)
{
if (windPos[i] > count)
{
count = windPos[i];
indice = i;
}
#ifdef DEBUG_UI
// For testing
Serial.print(" i: "); Serial.print(i); Serial.print(" - ");
Serial.print(windPos[i]); Serial.println(" x");
#endif
}
#ifdef DEBUG_UI
// For testing
Serial.print("-- Angle occurencies in the last hour--: ");
Serial.print(" i: "); Serial.print(indice); Serial.print(" - ");
Serial.print(count); Serial.println("x");
#endif
if (indice==5) return (112.5);
if (indice==3) return (67.5);
if (indice==4) return (90);
if (indice==7) return (157.5);
if (indice==6) return (135);
if (indice==9) return (202.5);
if (indice==8) return (180);
if (indice==1) return (22.5);
if (indice==2) return (45);
if (indice==11) return (247.5);
if (indice==10) return (225);
if (indice==15) return (337.5);
if (indice==0) return (0);
if (indice==13) return (292.5);
if (indice==14) return (315);
if (indice==12) return (270);
return (-1); // error
}
//----------------------------------------------------------------------------------------------
//------------------------------------ Returns the Wind Gust -----------------------------------
float SensorWind::getWindGust()
{
float windGust = 0;
if (t_min > 0)
{
float period = ((float)t_min/1000); // Convert milliseconds to seconds
windGust=2.4/period; // (1 interrupt/1 second) = 2.4kPh --> in 0.2 seconds --> (2.4/0.2)
}
return (windGust);
}
//----------------------------------------------------------------------------------------------
//------------------------------------- Get Wind Clicks ----------------------------------------
long SensorWind::getWindClicks()
{
return wind;
}
//----------------------------------------------------------------------------------------------
//------------------------------- Get t_min between interrupts -------------------------------
long SensorWind::getGustInterval()
{
return t_min;
}
//----------------------------------------------------------------------------------------------
//-------------------------------- Wind Interrupt Routine --------------------------------------
void SensorWind::ISR_WIND()
{
// Ignore switch-bounce glitches less than 10ms (142MPH max reading) after the reed switch closes
if (millis() - lastWindIRQ > 10)
{
wind++; // There is 1.492MPH for each click per second.
lastWindIRQ = millis(); // Grab the current time
// For each wind interrupt: get minimum time (in ms) between interrupts
t_interval=lastWindIRQ-t_previous;
// if is a new minimum time between interrups
if(t_interval<t_min)
t_min=t_interval;
t_previous=lastWindIRQ;
}
control=1;
}
//----------------------------------------------------------------------------------------------
//--------------------------------- Attach Rain Interrupt --------------------------------------
void SensorWind::attachWindInterrupt()
{
pinMode(w_int_pin, INPUT_PULLUP); // input from wind meters windspeed sensor
attachInterrupt(digitalPinToInterrupt(w_int_pin), isr0, FALLING); //FALLING
SensorWind_instance = this;
}
//----------------------------------------------------------------------------------------------
//----------------------------- Auxiliar Wind Interrupt ISR ------------------------------------
void SensorWind::isr0()
{
SensorWind_instance->ISR_WIND();
}
//----------------------------------------------------------------------------------------------
SensorWind * SensorWind::SensorWind_instance; // Auxiliar instance to Rain Interrupt ISR
//--------------------------- Clear the Wind Direction Indice vector ---------------------------
void SensorWind::clearWind()
{
// Clear the Wind Direction Indice vector
for (int i=0; i<16; i++)
{
windPos[i]=0;
}
t_min=36000000;
wind=0;
control=0; // (FINAL VERSION: TO BE DELETED)
}
//----------------------------------------------------------------------------------------------
// (FINAL VERSION: TO BE DELETED)
//-------------------------------------- Set Wind Control --------------------------------------
void SensorWind::clearWindControl()
{
control=0;
}
//----------------------------------------------------------------------------------------------
//------------------------------------- Get Wind Control ---------------------------------------
int SensorWind::getWindControl()
{
return control;
}
//----------------------------------------------------------------------------------------------