/
AP_WindVane.cpp
366 lines (321 loc) · 12.5 KB
/
AP_WindVane.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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "AP_WindVane.h"
#include "AP_WindVane_Home.h"
#include "AP_WindVane_Analog.h"
#include "AP_WindVane_ModernDevice.h"
#include "AP_WindVane_Airspeed.h"
#include "AP_WindVane_RPM.h"
#include "AP_WindVane_SITL.h"
#include "AP_WindVane_NMEA.h"
const AP_Param::GroupInfo AP_WindVane::var_info[] = {
// @Param: TYPE
// @DisplayName: Wind Vane Type
// @Description: Wind Vane type
// @Values: 0:None,1:Heading when armed,2:RC input offset heading when armed,3:Analog,4:NMEA,10:SITL
// @User: Standard
// @RebootRequired: True
AP_GROUPINFO_FLAGS("TYPE", 1, AP_WindVane, _direction_type, 0, AP_PARAM_FLAG_ENABLE),
// @Param: RC_IN_NO
// @DisplayName: Wind vane sensor RC Input Channel
// @Description: RC Input Channel to use as wind angle value
// @Range: 0 16
// @Increment: 1
// @User: Standard
AP_GROUPINFO("RC_IN_NO", 2, AP_WindVane, _rc_in_no, 0),
// @Param: DIR_PIN
// @DisplayName: Wind vane analog voltage pin for direction
// @Description: Analog input pin to read as wind vane direction
// @Values: 11:Pixracer,13:Pixhawk ADC4,14:Pixhawk ADC3,15:Pixhawk ADC6/Pixhawk2 ADC,50:PixhawkAUX1,51:PixhawkAUX2,52:PixhawkAUX3,53:PixhawkAUX4,54:PixhawkAUX5,55:PixhawkAUX6,103:Pixhawk SBUS
// @User: Standard
AP_GROUPINFO("DIR_PIN", 3, AP_WindVane, _dir_analog_pin, WINDVANE_DEFAULT_PIN),
// @Param: DIR_V_MIN
// @DisplayName: Wind vane voltage minimum
// @Description: Minimum voltage supplied by analog wind vane
// @Units: V
// @Increment: 0.01
// @Range: 0 5.0
// @User: Standard
AP_GROUPINFO("DIR_V_MIN", 4, AP_WindVane, _dir_analog_volt_min, 0.0f),
// @Param: DIR_V_MAX
// @DisplayName: Wind vane voltage maximum
// @Description: Maximum voltage supplied by analog wind vane
// @Units: V
// @Increment: 0.01
// @Range: 0 5.0
// @User: Standard
AP_GROUPINFO("DIR_V_MAX", 5, AP_WindVane, _dir_analog_volt_max, 3.3f),
// @Param: DIR_OFS
// @DisplayName: Wind vane headwind offset
// @Description: Angle offset when analog windvane is indicating a headwind, ie 0 degress relative to vehicle
// @Units: deg
// @Increment: 1
// @Range: 0 360
// @User: Standard
AP_GROUPINFO("DIR_OFS", 6, AP_WindVane, _dir_analog_bearing_offset, 0.0f),
// @Param: DIR_FILT
// @DisplayName: Wind vane direction low pass filter frequency
// @Description: Wind vane direction low pass filter frequency, a value of -1 disables filter
// @Units: Hz
// @User: Standard
AP_GROUPINFO("DIR_FILT", 7, AP_WindVane, _dir_filt_hz, 0.5f),
// @Param: CAL
// @DisplayName: Wind vane calibration start
// @Description: Start wind vane calibration by setting this to 1 or 2
// @Values: 0:None, 1:Calibrate direction, 2:Calibrate speed
// @User: Standard
AP_GROUPINFO("CAL", 8, AP_WindVane, _calibration, 0),
// @Param: DIR_DZ
// @DisplayName: Wind vane deadzone when using analog sensor
// @Description: Wind vane deadzone when using analog sensor
// @Units: deg
// @Increment: 1
// @Range: 0 360
// @User: Standard
AP_GROUPINFO("DIR_DZ", 9, AP_WindVane, _dir_analog_deadzone, 0),
// @Param: SPEED_MIN
// @DisplayName: Wind vane cut off wind speed
// @Description: Wind vane direction will be ignored when apparent wind speeds are below this value (if wind speed sensor is present). If the apparent wind is consistently below this value the vane will not work
// @Units: m/s
// @Increment: 0.1
// @Range: 0 5
// @User: Standard
AP_GROUPINFO("SPEED_MIN", 10, AP_WindVane, _dir_speed_cutoff, 0),
// @Param: SPEED_TYPE
// @DisplayName: Wind speed sensor Type
// @Description: Wind speed sensor type
// @Values: 0:None,1:Airspeed library,2:Modern Devices Wind Sensor,3:RPM library,4:NMEA,10:SITL
// @User: Standard
// @RebootRequired: True
AP_GROUPINFO("SPEED_TYPE", 11, AP_WindVane, _speed_sensor_type, 0),
// @Param: SPEED_PIN
// @DisplayName: Wind vane speed sensor analog pin
// @Description: Wind speed analog speed input pin for Modern Devices Wind Sensor rev. p
// @Values: 11:Pixracer,13:Pixhawk ADC4,14:Pixhawk ADC3,15:Pixhawk ADC6/Pixhawk2 ADC,50:PixhawkAUX1,51:PixhawkAUX2,52:PixhawkAUX3,53:PixhawkAUX4,54:PixhawkAUX5,55:PixhawkAUX6,103:Pixhawk SBUS
// @User: Standard
AP_GROUPINFO("SPEED_PIN", 12, AP_WindVane, _speed_sensor_speed_pin, WINDSPEED_DEFAULT_SPEED_PIN),
// @Param: TEMP_PIN
// @DisplayName: Wind vane speed sensor analog temp pin
// @Description: Wind speed sensor analog temp input pin for Modern Devices Wind Sensor rev. p, set to -1 to diasble temp readings
// @Values: 11:Pixracer,13:Pixhawk ADC4,14:Pixhawk ADC3,15:Pixhawk ADC6/Pixhawk2 ADC,50:PixhawkAUX1,51:PixhawkAUX2,52:PixhawkAUX3,53:PixhawkAUX4,54:PixhawkAUX5,55:PixhawkAUX6,103:Pixhawk SBUS
// @User: Standard
AP_GROUPINFO("TEMP_PIN", 13, AP_WindVane, _speed_sensor_temp_pin, WINDSPEED_DEFAULT_TEMP_PIN),
// @Param: SPEED_OFS
// @DisplayName: Wind speed sensor analog voltage offset
// @Description: Wind sensor analog voltage offset at zero wind speed
// @Units: V
// @Increment: 0.01
// @Range: 0 3.3
// @User: Standard
AP_GROUPINFO("SPEED_OFS", 14, AP_WindVane, _speed_sensor_voltage_offset, WINDSPEED_DEFAULT_VOLT_OFFSET),
// @Param: SPEED_FILT
// @DisplayName: Wind speed low pass filter frequency
// @Description: Wind speed low pass filter frequency, a value of -1 disables filter
// @Units: Hz
// @User: Standard
AP_GROUPINFO("SPEED_FILT", 15, AP_WindVane, _speed_filt_hz, 0.5f),
AP_GROUPEND
};
// constructor
AP_WindVane::AP_WindVane()
{
AP_Param::setup_object_defaults(this, var_info);
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
if (_singleton) {
AP_HAL::panic("Too many Wind Vane sensors");
}
#endif
_singleton = this;
}
/*
* Get the AP_WindVane singleton
*/
AP_WindVane *AP_WindVane::get_singleton()
{
return _singleton;
}
// return true if wind vane is enabled
bool AP_WindVane::enabled() const
{
return _direction_type != WINDVANE_NONE;
}
// return true if wind speed is enabled
bool AP_WindVane::wind_speed_enabled() const
{
return (_speed_sensor_type != WINDSPEED_NONE);
}
// Initialize the Wind Vane object and prepare it for use
void AP_WindVane::init(const AP_SerialManager& serial_manager)
{
// don't construct twice
if (_direction_driver != nullptr || _speed_driver != nullptr ) {
return;
}
// wind direction
switch (_direction_type) {
case WindVaneType::WINDVANE_NONE:
// WindVane disabled
return;
case WindVaneType::WINDVANE_HOME_HEADING:
case WindVaneType::WINDVANE_PWM_PIN:
_direction_driver = new AP_WindVane_Home(*this);
break;
case WindVaneType::WINDVANE_ANALOG_PIN:
_direction_driver = new AP_WindVane_Analog(*this);
break;
case WindVaneType::WINDVANE_SITL:
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
_direction_driver = new AP_WindVane_SITL(*this);
#endif
break;
case WindVaneType::WINDVANE_NMEA:
_direction_driver = new AP_WindVane_NMEA(*this);
_direction_driver->init(serial_manager);
break;
}
// wind speed
switch (_speed_sensor_type) {
case Speed_type::WINDSPEED_NONE:
break;
case Speed_type::WINDSPEED_AIRSPEED:
_speed_driver = new AP_WindVane_Airspeed(*this);
break;
case Speed_type::WINDVANE_WIND_SENSOR_REV_P:
_speed_driver = new AP_WindVane_ModernDevice(*this);
break;
case Speed_type::WINDSPEED_SITL:
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
// single driver does both speed and direction
if (_direction_type != WindVaneType::WINDVANE_SITL) {
_speed_driver = new AP_WindVane_SITL(*this);
} else {
_speed_driver = _direction_driver;
}
#endif
break;
case Speed_type::WINDSPEED_NMEA:
// single driver does both speed and direction
if (_direction_type != WindVaneType::WINDVANE_NMEA) {
_speed_driver = new AP_WindVane_NMEA(*this);
_speed_driver->init(serial_manager);
} else {
_speed_driver = _direction_driver;
}
break;
case Speed_type::WINDSPEED_RPM:
_speed_driver = new AP_WindVane_RPM(*this);
break;
}
}
// update wind vane, expected to be called at 20hz
void AP_WindVane::update()
{
bool have_speed = _speed_driver != nullptr;
bool have_direciton = _direction_driver != nullptr;
// exit immediately if not enabled
if (!enabled() || (!have_speed && !have_direciton)) {
return;
}
// calibrate if booted and disarmed
if (!hal.util->get_soft_armed()) {
if (_calibration == 1 && have_direciton) {
_direction_driver->calibrate();
} else if (_calibration == 2 && have_speed) {
_speed_driver->calibrate();
} else if (_calibration != 0) {
gcs().send_text(MAV_SEVERITY_INFO, "WindVane: driver not found");
_calibration.set_and_save(0);
}
} else if (_calibration != 0) {
gcs().send_text(MAV_SEVERITY_INFO, "WindVane: disarm for cal");
_calibration.set_and_save(0);
}
// read apparent wind speed
if (have_speed) {
_speed_driver->update_speed();
}
// read apparent wind direction
if (_speed_apparent >= _dir_speed_cutoff && have_direciton) {
// only update if enough wind to move vane
_direction_driver->update_direction();
}
// calculate true wind speed and direction from apparent wind
if (have_speed && have_direciton) {
update_true_wind_speed_and_direction();
} else {
// no wind speed sensor, so can't do true wind calcs
_direction_true = _direction_apparent_ef;
_speed_true = 0.0f;
return;
}
}
// to start direction calibration from mavlink or other
bool AP_WindVane::start_direction_calibration()
{
if (enabled() && (_calibration == 0)) {
_calibration = 1;
return true;
}
return false;
}
// to start speed calibration from mavlink or other
bool AP_WindVane::start_speed_calibration()
{
if (enabled() && (_calibration == 0)) {
_calibration = 2;
return true;
}
return false;
}
// send mavlink wind message
void AP_WindVane::send_wind(mavlink_channel_t chan)
{
// exit immediately if not enabled
if (!enabled()) {
return;
}
// send wind
mavlink_msg_wind_send(
chan,
wrap_360(degrees(get_true_wind_direction_rad())),
get_true_wind_speed(),
0);
}
// calculate true wind speed and direction from apparent wind
// https://en.wikipedia.org/wiki/Apparent_wind
void AP_WindVane::update_true_wind_speed_and_direction()
{
// if no vehicle speed, can't do calcs
Vector3f veh_velocity;
if (!AP::ahrs().get_velocity_NED(veh_velocity)) {
// if no vehicle speed use apparent speed and direction directly
_direction_true = _direction_apparent_ef;
_speed_true = _speed_apparent;
return;
}
// convert apparent wind speed and direction to 2D vector in same frame as vehicle velocity
const float wind_dir_180 = wrap_PI(_direction_apparent_ef + radians(180));
const Vector2f wind_apparent_vec(cosf(wind_dir_180) * _speed_apparent, sinf(wind_dir_180) * _speed_apparent);
// add vehicle velocity
Vector2f wind_true_vec = Vector2f(wind_apparent_vec.x + veh_velocity.x, wind_apparent_vec.y + veh_velocity.y);
// calculate true speed and direction
_direction_true = wrap_PI(atan2f(wind_true_vec.y, wind_true_vec.x) - radians(180));
_speed_true = wind_true_vec.length();
}
AP_WindVane *AP_WindVane::_singleton = nullptr;
namespace AP {
AP_WindVane *windvane()
{
return AP_WindVane::get_singleton();
}
};