-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWind.cpp
More file actions
203 lines (151 loc) · 5.52 KB
/
Wind.cpp
File metadata and controls
203 lines (151 loc) · 5.52 KB
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
#include "Wind.h"
#include <avr/wdt.h>
#include <LiquidCrystal.h>
#include <EEPROM.h>
#ifdef WITH_WIND
// The pin definitions are per obfuscated Arduino pin defines -- see aka for ATMEL pin names as found on the MEGA328P spec sheet
#define WIND_SPEED_PIN 16 // aka PC2 (ADC2)
#define WIND_DIR_ADC 6 // ADC6
#define WIND_GUST_PER ( 10 * 60000/ WIND_SAMPLE_PER ) // that is 10 minutes
#define ANEMO_CONST (2.5) // For Vortex/Inspeed wind cups
#define ANEMO_COUNT_Rev 16 // For high fidelity opto interrupter pickup with 8 fingers
struct tagCalData
{
int WDir_min;
int WDir_max;
int WDir_offs;
} WindCal = {70, 660, 0};
// array to keep 10 minutes of wind data for gust and average calculations
static unsigned char Wind_Gust[WIND_GUST_PER];
static volatile unsigned short WindCnt = 0;
// Globals for reporing the wind data
unsigned char WindGustMPH;
unsigned char WindSpdMPH;
unsigned char WindAvgMPH;
long WindDir; // needs to be long for overflow protection in math
// Pin change interrupt to capture the edges of the wind speed interrupter
ISR(PCINT1_vect)
{
// check PCINT1 interrupt flags for the wind_count pin if any other pin change interrupts are used in this code
WindCnt++; // count every edge from the wind sensor
}
void WindSetup()
{
pinMode(WIND_SPEED_PIN, INPUT); // the Windspeed count
// enable the pin change interrupt for PC2, aka, A0, aka pin14
// Note: this enabling of the pinchange interrupt pin has to go hand in hand with the chosen wind speed pin above
PCMSK1 |= 1 << PCINT10 ; // enable PCinterupt10 , aka PC2
PCICR |= 1 << PCIE1; // enable PCinterupt 1 vector
// Read from eeprom
// TODO: this needs to go further out in scope if the EEPROM is used to store other setup related items such as metric/imperial display etc
EEPROM.get(2, WindCal);
}
extern unsigned char ShortPressCnt;
extern unsigned char EncoderCnt; // decalared unsigned for this modules use
extern LiquidCrystal lcd;
void
WindDirCal( void )
{
unsigned short adc_val;
unsigned char p;
unsigned off = 0;
WindCal.WDir_min = 0xffff;
WindCal.WDir_max = 0;
ShortPressCnt = 0;
while ( !ShortPressCnt )
{
adc_val = analogRead(WIND_DIR_ADC);// read the vane position
if ( adc_val > WindCal.WDir_max)
WindCal.WDir_max = adc_val;
if (adc_val < WindCal.WDir_min)
WindCal.WDir_min = adc_val;
lcd.setCursor ( 0, 0 );
lcd.print("Min ");
lcd.print( WindCal.WDir_min);
lcd.print(" ");
lcd.setCursor ( 0, 1 );
lcd.print("Max ");
lcd.print(WindCal.WDir_max);
lcd.print(" ");
wdt_reset();
}
ShortPressCnt = 0;
if ( WindCal.WDir_offs > 255 )
off = 255;
else
off = 0;
EncoderCnt = p = WindCal.WDir_offs - off;
lcd.clear();
lcd.print("Offset N");
while ( !ShortPressCnt )
{
wdt_reset();
if ( EncoderCnt == 0 && p == 255) // to continue past 255
off = 255;
if ( EncoderCnt == 255 && p == 0) // as it comes below 255
{ if (off != 0)
off = 0;
else
EncoderCnt++; // lock at 0
}
if (off + EncoderCnt >= 360 )
EncoderCnt--; // lock at 359
p = EncoderCnt;
WindCal.WDir_offs = off + EncoderCnt;
lcd.setCursor ( 0, 1 );
lcd.print(WindCal.WDir_offs);
lcd.print(" ");
lcd.print(char(223)); // degree symbol
lcd.print(" "); // fill to end of line
}
// Store min & max in EEprom
// TODO: this needs to go further out in scope if the EEPROM is used to store other setup related items such as metric/imperial display etc
EEPROM.put(2, WindCal);
}
void WindRead()
{
unsigned short adc_val;
unsigned short wind_count;
unsigned short n;
float wind_speed;
unsigned long t_now = 0;
unsigned long t_sample = 0;
static unsigned long t_next = 0;
static unsigned short GustNdx = 0;
if ( (t_now = millis()) < t_next) // wait until next update period
{
// not time yet
return;
}
// disable the interrupt so we can get a two byte variable without getting interrupted
PCICR = 0; // disable PCinterupt 1 vector
wind_count = WindCnt; // get the count from the pin change interrupt
WindCnt = 0;
t_sample = WIND_SAMPLE_PER + (t_now - t_next);
t_next = t_now + WIND_SAMPLE_PER; // every second we update the display with new data
PCICR |= 1 << PCIE1; // enable PCinterupt 1 vector
// calc and store the current wind speed
wind_speed = (wind_count * ANEMO_CONST) / ANEMO_COUNT_Rev; // 2.5 miles/rev/sec; div by counts per revolution.
// correct for actual sampling periode
wind_speed = wind_speed * WIND_SAMPLE_PER / t_sample;
WindSpdMPH = wind_speed + 0.5; \
Wind_Gust[GustNdx] = WindSpdMPH; // store current measure wind speed in uchar, use rounding.
GustNdx = ++GustNdx % WIND_GUST_PER; // Advance to next position, wrap around
// go through the 1 second array and take the peak and average for the last 10 minutes.
// note average will not be ready for first 10 minuntes after turning the device on
WindGustMPH = wind_speed = 0;
for ( unsigned short i = 0; i < WIND_GUST_PER; i++)
{
wind_speed += Wind_Gust[i];
if (Wind_Gust[i] > WindGustMPH)
WindGustMPH = Wind_Gust[i] ; // this is the highest in the last 10 minutes
}
wind_speed /= WIND_GUST_PER;
WindAvgMPH = wind_speed + 0.5;
// calc and store the current wind dir
adc_val = analogRead(WIND_DIR_ADC); // read the input pin
WindDir = ((adc_val - WindCal.WDir_min) * 360L) / (WindCal.WDir_max - WindCal.WDir_min);
WindDir += WindCal.WDir_offs;
WindDir = WindDir %360; // to make the offset wrap around
}
#endif