2011-09-08 22:29:39 -03:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
2012-11-18 17:09:44 -04:00
|
|
|
// filter altitude from the barometer with a low pass filter
|
|
|
|
static LowPassFilterInt32 altitude_filter;
|
|
|
|
|
|
|
|
|
2011-09-08 22:29:39 -03:00
|
|
|
static void init_barometer(void)
|
|
|
|
{
|
2012-12-03 20:13:33 -04:00
|
|
|
gcs_send_text_P(SEVERITY_LOW, PSTR("Calibrating barometer"));
|
2012-12-04 18:22:21 -04:00
|
|
|
barometer.calibrate();
|
2012-11-18 17:09:44 -04:00
|
|
|
|
|
|
|
// filter at 100ms sampling, with 0.7Hz cutoff frequency
|
|
|
|
altitude_filter.set_cutoff_frequency(0.1, 0.7);
|
|
|
|
|
2012-06-19 23:26:58 -03:00
|
|
|
gcs_send_text_P(SEVERITY_LOW, PSTR("barometer calibration complete"));
|
2011-09-08 22:29:39 -03:00
|
|
|
}
|
|
|
|
|
2012-06-19 23:26:58 -03:00
|
|
|
// read the barometer and return the updated altitude in centimeters
|
|
|
|
// above the calibration altitude
|
|
|
|
static int32_t read_barometer(void)
|
|
|
|
{
|
2012-08-16 21:50:15 -03:00
|
|
|
barometer.read();
|
2012-08-07 22:14:01 -03:00
|
|
|
return altitude_filter.apply(barometer.get_altitude() * 100.0);
|
2011-09-08 22:29:39 -03:00
|
|
|
}
|
|
|
|
|
2013-08-28 09:37:07 -03:00
|
|
|
/*
|
|
|
|
ask airspeed sensor for a new value
|
|
|
|
*/
|
2011-09-08 22:29:39 -03:00
|
|
|
static void read_airspeed(void)
|
|
|
|
{
|
2013-06-04 00:34:58 -03:00
|
|
|
if (airspeed.enabled()) {
|
|
|
|
airspeed.read();
|
|
|
|
calc_airspeed_errors();
|
|
|
|
}
|
2011-09-08 22:29:39 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void zero_airspeed(void)
|
|
|
|
{
|
2012-12-04 18:22:21 -04:00
|
|
|
airspeed.calibrate();
|
2012-08-15 06:49:09 -03:00
|
|
|
gcs_send_text_P(SEVERITY_LOW,PSTR("zero airspeed calibrated"));
|
2011-09-08 22:29:39 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void read_battery(void)
|
|
|
|
{
|
2012-08-16 21:50:15 -03:00
|
|
|
if(g.battery_monitoring == 0) {
|
2013-05-22 07:33:57 -03:00
|
|
|
battery.voltage = 0;
|
2012-08-16 21:50:15 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(g.battery_monitoring == 3 || g.battery_monitoring == 4) {
|
2012-10-16 03:20:49 -03:00
|
|
|
// this copes with changing the pin at runtime
|
2012-12-04 18:22:21 -04:00
|
|
|
batt_volt_pin->set_pin(g.battery_volt_pin);
|
2013-05-22 07:33:57 -03:00
|
|
|
battery.voltage = BATTERY_VOLTAGE(batt_volt_pin);
|
2012-06-29 08:51:05 -03:00
|
|
|
}
|
2013-06-04 00:34:58 -03:00
|
|
|
|
|
|
|
if (g.battery_monitoring == 4) {
|
|
|
|
uint32_t tnow = hal.scheduler->millis();
|
2013-07-14 08:59:15 -03:00
|
|
|
float dt = tnow - battery.last_time_ms;
|
|
|
|
// this copes with changing the pin at runtime
|
|
|
|
batt_curr_pin->set_pin(g.battery_curr_pin);
|
|
|
|
battery.current_amps = CURRENT_AMPS(batt_curr_pin);
|
|
|
|
if (battery.last_time_ms != 0 && dt < 2000) {
|
2013-06-04 00:34:58 -03:00
|
|
|
// .0002778 is 1/3600 (conversion to hours)
|
|
|
|
battery.current_total_mah += battery.current_amps * dt * 0.0002778f;
|
|
|
|
}
|
2013-07-14 08:59:15 -03:00
|
|
|
battery.last_time_ms = tnow;
|
2012-08-16 21:50:15 -03:00
|
|
|
}
|
|
|
|
|
2013-05-22 07:33:57 -03:00
|
|
|
if (battery.voltage != 0 &&
|
|
|
|
g.fs_batt_voltage > 0 &&
|
|
|
|
battery.voltage < g.fs_batt_voltage) {
|
|
|
|
low_battery_event();
|
|
|
|
}
|
|
|
|
if (g.battery_monitoring == 4 &&
|
|
|
|
g.fs_batt_mah > 0 &&
|
|
|
|
g.pack_capacity - battery.current_total_mah < g.fs_batt_mah) {
|
|
|
|
low_battery_event();
|
|
|
|
}
|
2011-09-08 22:29:39 -03:00
|
|
|
}
|
|
|
|
|
2012-08-22 00:33:12 -03:00
|
|
|
|
2012-08-22 00:58:25 -03:00
|
|
|
// read the receiver RSSI as an 8 bit number for MAVLink
|
2012-08-22 00:33:12 -03:00
|
|
|
// RC_CHANNELS_SCALED message
|
2012-08-22 00:58:25 -03:00
|
|
|
void read_receiver_rssi(void)
|
2012-08-22 00:33:12 -03:00
|
|
|
{
|
2012-12-04 18:22:21 -04:00
|
|
|
rssi_analog_source->set_pin(g.rssi_pin);
|
2013-05-13 02:14:56 -03:00
|
|
|
float ret = rssi_analog_source->voltage_average() * 50;
|
2012-12-18 22:36:00 -04:00
|
|
|
receiver_rssi = constrain_int16(ret, 0, 255);
|
2012-08-22 00:33:12 -03:00
|
|
|
}
|
2012-09-19 03:22:53 -03:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
return current_loc.alt adjusted for ALT_OFFSET
|
|
|
|
This is useful during long flights to account for barometer changes
|
|
|
|
from the GCS, or to adjust the flying height of a long mission
|
|
|
|
*/
|
|
|
|
static int32_t adjusted_altitude_cm(void)
|
|
|
|
{
|
|
|
|
return current_loc.alt - (g.alt_offset*100);
|
|
|
|
}
|