-
Notifications
You must be signed in to change notification settings - Fork 8
/
wind.ino
339 lines (290 loc) · 10.4 KB
/
wind.ino
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
Wind - NMEA Wind Instrument
Copyright (c) 2018 Tom K
Originally written for Arduino Pro Mini 328
v3c
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#define VERSION "Wind v3 18-Sep-2015"
#include "PString.h"
#define windSpeedPin 2
#define windDirPin 3
#define windSpeedINT 0 // INT0
#define windDirINT 1 // INT1
// Pin 13 has an LED connected on most Arduino boards.
int LED = 13;
const unsigned long DEBOUNCE = 10000ul; // Minimum switch time in microseconds
const unsigned long DIRECTION_OFFSET = 0ul; // Manual direction offset in degrees, if required
const unsigned long TIMEOUT = 1500000ul; // Maximum time allowed between speed pulses in microseconds
const unsigned long UPDATE_RATE = 500ul; // How often to send out NMEA data in milliseconds
const float filterGain = 0.25; // Filter gain on direction output filter. Range: 0.0 to 1.0
// 1.0 means no filtering. A smaller number increases the filtering
// Knots is actually stored as (Knots * 100). Deviations below should match these units.
const int BAND_0 = 10 * 100;
const int BAND_1 = 80 * 100;
const int SPEED_DEV_LIMIT_0 = 5 * 100; // Deviation from last measurement to be valid. Band_0: 0 to 10 knots
const int SPEED_DEV_LIMIT_1 = 10 * 100; // Deviation from last measurement to be valid. Band_1: 10 to 80 knots
const int SPEED_DEV_LIMIT_2 = 30 * 100; // Deviation from last measurement to be valid. Band_2: 80+ knots
// Should be larger limits as lower speed, as the direction can change more per speed update
const int DIR_DEV_LIMIT_0 = 25; // Deviation from last measurement to be valid. Band_0: 0 to 10 knots
const int DIR_DEV_LIMIT_1 = 18; // Deviation from last measurement to be valid. Band_1: 10 to 80 knots
const int DIR_DEV_LIMIT_2 = 10; // Deviation from last measurement to be valid. Band_2: 80+ knots
volatile unsigned long speedPulse = 0ul; // Time capture of speed pulse
volatile unsigned long dirPulse = 0ul; // Time capture of direction pulse
volatile unsigned long speedTime = 0ul; // Time between speed pulses (microseconds)
volatile unsigned long directionTime = 0ul; // Time between direction pulses (microseconds)
volatile boolean newData = false; // New speed pulse received
volatile unsigned long lastUpdate = 0ul; // Time of last serial output
volatile int knotsOut = 0; // Wind speed output in knots * 100
volatile int dirOut = 0; // Direction output in degrees
volatile boolean ignoreNextReading = false;
boolean debug = false;
void setup()
{
pinMode(LED, OUTPUT);
Serial.begin(38400, SERIAL_8N1);
Serial.println(VERSION);
Serial.print("Direction Filter: ");
Serial.println(filterGain);
pinMode(windSpeedPin, INPUT);
attachInterrupt(windSpeedINT, readWindSpeed, FALLING);
pinMode(windDirPin, INPUT);
attachInterrupt(windDirINT, readWindDir, FALLING);
interrupts();
}
void readWindSpeed()
{
// Despite the interrupt being set to FALLING edge, double check the pin is now LOW
if (((micros() - speedPulse) > DEBOUNCE) && (digitalRead(windSpeedPin) == LOW))
{
// Work out time difference between last pulse and now
speedTime = micros() - speedPulse;
// Direction pulse should have occured after the last speed pulse
if (dirPulse - speedPulse >= 0) directionTime = dirPulse - speedPulse;
newData = true;
speedPulse = micros(); // Capture time of the new speed pulse
}
}
void readWindDir()
{
if (((micros() - dirPulse) > DEBOUNCE) && (digitalRead(windDirPin) == LOW))
{
dirPulse = micros(); // Capture time of direction pulse
}
}
boolean checkDirDev(long knots, int dev)
{
if (knots < BAND_0)
{
if ((abs(dev) < DIR_DEV_LIMIT_0) || (abs(dev) > 360 - DIR_DEV_LIMIT_0)) return true;
}
else if (knots < BAND_1)
{
if ((abs(dev) < DIR_DEV_LIMIT_1) || (abs(dev) > 360 - DIR_DEV_LIMIT_1)) return true;
}
else
{
if ((abs(dev) < DIR_DEV_LIMIT_2) || (abs(dev) > 360 - DIR_DEV_LIMIT_2)) return true;
}
return false;
}
boolean checkSpeedDev(long knots, int dev)
{
if (knots < BAND_0)
{
if (abs(dev) < SPEED_DEV_LIMIT_0) return true;
}
else if (knots < BAND_1)
{
if (abs(dev) < SPEED_DEV_LIMIT_1) return true;
}
else
{
if (abs(dev) < SPEED_DEV_LIMIT_2) return true;
}
return false;
}
void calcWindSpeedAndDir()
{
unsigned long dirPulse_, speedPulse_;
unsigned long speedTime_;
unsigned long directionTime_;
long windDirection = 0l, rps = 0l, knots = 0l;
static int prevKnots = 0;
static int prevDir = 0;
int dev = 0;
// Get snapshot of data into local variables. Note: an interrupt could trigger here
noInterrupts();
dirPulse_ = dirPulse;
speedPulse_ = speedPulse;
speedTime_ = speedTime;
directionTime_ = directionTime;
interrupts();
// Make speed zero, if the pulse delay is too long
if (micros() - speedPulse_ > TIMEOUT) speedTime_ = 0ul;
// The following converts revolutions per 100 seconds (rps) to knots x 100
// This calculation follows the Peet Bros. piecemeal calibration data
if (speedTime_ > 0)
{
rps = 100000000/speedTime_; //revolutions per 100s
if (rps < 323)
{
knots = (rps * rps * -11)/11507 + (293 * rps)/115 - 12;
}
else if (rps < 5436)
{
knots = (rps * rps / 2)/11507 + (220 * rps)/115 + 96;
}
else
{
knots = (rps * rps * 11)/11507 - (957 * rps)/115 + 28664;
}
//knots = mph * 0.86897
if (knots < 0l) knots = 0l; // Remove the possibility of negative speed
// Find deviation from previous value
dev = (int)knots - prevKnots;
// Only update output if in deviation limit
if (checkSpeedDev(knots, dev))
{
knotsOut = knots;
// If speed data is ok, then continue with direction data
if (directionTime_ > speedTime_)
{
windDirection = 999; // For debugging only (not output to knots)
}
else
{
// Calculate direction from captured pulse times
windDirection = (((directionTime_ * 360) / speedTime_) + DIRECTION_OFFSET) % 360;
// Find deviation from previous value
dev = (int)windDirection - prevDir;
// Check deviation is in range
if (checkDirDev(knots, dev))
{
int delta = ((int)windDirection - dirOut);
if (delta < -180)
{
delta = delta + 360; // Take the shortest path when filtering
}
else if (delta > +180)
{
delta = delta - 360;
}
// Perform filtering to smooth the direction output
dirOut = (dirOut + (int)(round(filterGain * delta))) % 360;
if (dirOut < 0) dirOut = dirOut + 360;
}
prevDir = windDirection;
}
}
else
{
ignoreNextReading = true;
}
prevKnots = knots; // Update, even if outside deviation limit, cause it might be valid!?
}
else
{
knotsOut = 0;
prevKnots = 0;
}
if (debug)
{
Serial.print(millis());
Serial.print(",");
Serial.print(dirOut);
Serial.print(",");
Serial.print(windDirection);
Serial.print(",");
Serial.println(knotsOut/100);
//Serial.print(",");
//Serial.print(knots/100);
//Serial.print(",");
//Serial.println(rps);
}
else
{
if (millis() - lastUpdate > UPDATE_RATE)
{
printWindNmea();
lastUpdate = millis();
}
}
}
byte getChecksum(char* str)
{
byte cs = 0;
for (unsigned int n = 1; n < strlen(str) - 1; n++)
{
cs ^= str[n];
}
return cs;
}
/*=== MWV - Wind Speed and Angle ===
*
* ------------------------------------------------------------------------------
* 1 2 3 4 5
* | | | | |
* $--MWV,x.x,a,x.x,a*hh<CR><LF>
* ------------------------------------------------------------------------------
*
* Field Number:
*
* 1. Wind Angle, 0 to 360 degrees
* 2. Reference, R = Relative, T = True
* 3. Wind Speed
* 4. Wind Speed Units, K/M/N
* 5. Status, A = Data Valid
* 6. Checksum
*
*/
void printWindNmea()
{
char windSentence [30];
float spd = knotsOut / 100.0;
byte cs;
//Assemble a sentence of the various parts so that we can calculate the proper checksum
PString str(windSentence, sizeof(windSentence));
str.print("$WIMWV,");
str.print(dirOut);
str.print(".0,R,");
str.print(spd);
str.print(",N,A*");
//calculate the checksum
cs = getChecksum(windSentence);
//bug - arduino prints 0x007 as 7, 0x02B as 2B, so we add it now
if (cs < 0x10) str.print('0');
str.print(cs, HEX); // Assemble the final message and send it out the serial port
Serial.println(windSentence);
}
void loop()
{
int i;
const unsigned int LOOP_DELAY = 50;
const unsigned int LOOP_TIME = TIMEOUT / LOOP_DELAY;
digitalWrite(LED, !digitalRead(LED)); // Toggle LED
i = 0;
// If there is new data, process it, otherwise wait for LOOP_TIME to pass
while ((newData != true) && (i < LOOP_TIME))
{
i++;
delayMicroseconds(LOOP_DELAY);
}
calcWindSpeedAndDir(); // Process new data
newData = false;
}