123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- #include "AP_WindVane_Analog.h"
- #define WINDVANE_CALIBRATION_VOLT_DIFF_MIN 1.0f
- AP_WindVane_Analog::AP_WindVane_Analog(AP_WindVane &frontend) :
- AP_WindVane_Backend(frontend)
- {
- _dir_analog_source = hal.analogin->channel(ANALOG_INPUT_NONE);
- }
- void AP_WindVane_Analog::update_direction()
- {
- _dir_analog_source->set_pin(_frontend._dir_analog_pin);
- _current_analog_voltage = _dir_analog_source->voltage_average_ratiometric();
- const float voltage_ratio = linear_interpolate(0.0f, 1.0f, _current_analog_voltage, _frontend._dir_analog_volt_min, _frontend._dir_analog_volt_max);
- const float direction = (voltage_ratio * radians(360 - _frontend._dir_analog_deadzone)) + radians(_frontend._dir_analog_bearing_offset);
- direction_update_frontend(wrap_PI(direction + AP::ahrs().yaw));
- }
- void AP_WindVane_Analog::calibrate()
- {
-
- if (_cal_start_ms == 0) {
- _cal_start_ms = AP_HAL::millis();
- _cal_volt_max = _current_analog_voltage;
- _cal_volt_min = _current_analog_voltage;
- gcs().send_text(MAV_SEVERITY_INFO, "WindVane: Calibration started, rotate wind vane");
- }
-
- _cal_volt_max = MAX(_cal_volt_max, _current_analog_voltage);
- _cal_volt_min = MIN(_cal_volt_min, _current_analog_voltage);
-
- if ((AP_HAL::millis() - _cal_start_ms) > 30000) {
-
- const float volt_diff = _cal_volt_max - _cal_volt_min;
- if (volt_diff >= WINDVANE_CALIBRATION_VOLT_DIFF_MIN) {
-
- _frontend._dir_analog_volt_max.set_and_save(_cal_volt_max);
- _frontend._dir_analog_volt_min.set_and_save(_cal_volt_min);
- gcs().send_text(MAV_SEVERITY_INFO, "WindVane: Calibration complete (volt min:%.1f max:%1.f)",
- (double)_cal_volt_min,
- (double)_cal_volt_max);
- } else {
- gcs().send_text(MAV_SEVERITY_INFO, "WindVane: Calibration failed (volt diff %.1f below %.1f)",
- (double)volt_diff,
- (double)WINDVANE_CALIBRATION_VOLT_DIFF_MIN);
- }
- _frontend._calibration.set_and_save(0);
- _cal_start_ms = 0;
- }
- }
|