2011-09-08 22:29:39 -03:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
2015-05-13 03:09:36 -03:00
|
|
|
#include "Plane.h"
|
2015-08-28 03:01:32 -03:00
|
|
|
#include <AP_RSSI/AP_RSSI.h>
|
2015-05-13 03:09:36 -03:00
|
|
|
|
2016-05-23 22:31:53 -03:00
|
|
|
void Plane::init_barometer(bool full_calibration)
|
2011-09-08 22:29:39 -03:00
|
|
|
{
|
2015-11-03 23:52:54 -04:00
|
|
|
gcs_send_text(MAV_SEVERITY_INFO, "Calibrating barometer");
|
2016-05-23 22:31:53 -03:00
|
|
|
if (full_calibration) {
|
|
|
|
barometer.calibrate();
|
|
|
|
} else {
|
|
|
|
barometer.update_calibration();
|
|
|
|
}
|
2015-11-18 15:17:50 -04:00
|
|
|
gcs_send_text(MAV_SEVERITY_INFO, "Barometer calibration complete");
|
2011-09-08 22:29:39 -03:00
|
|
|
}
|
|
|
|
|
2015-05-13 03:09:36 -03:00
|
|
|
void Plane::init_rangefinder(void)
|
2013-10-30 19:23:21 -03:00
|
|
|
{
|
2015-05-13 20:00:11 -03:00
|
|
|
#if RANGEFINDER_ENABLED == ENABLED
|
2014-08-26 08:17:47 -03:00
|
|
|
rangefinder.init();
|
2015-05-13 20:00:11 -03:00
|
|
|
#endif
|
2013-10-30 19:23:21 -03:00
|
|
|
}
|
|
|
|
|
2014-08-26 08:17:47 -03:00
|
|
|
/*
|
|
|
|
read the rangefinder and update height estimate
|
|
|
|
*/
|
2015-05-13 03:09:36 -03:00
|
|
|
void Plane::read_rangefinder(void)
|
2013-10-30 19:23:21 -03:00
|
|
|
{
|
2015-05-13 20:00:11 -03:00
|
|
|
#if RANGEFINDER_ENABLED == ENABLED
|
2015-10-15 18:50:06 -03:00
|
|
|
|
|
|
|
// notify the rangefinder of our approximate altitude above ground to allow it to power on
|
|
|
|
// during low-altitude flight when configured to power down during higher-altitude flight
|
|
|
|
float height;
|
|
|
|
#if AP_TERRAIN_AVAILABLE
|
|
|
|
if (terrain.status() == AP_Terrain::TerrainStatusOK && terrain.height_above_terrain(height, true)) {
|
|
|
|
rangefinder.set_estimated_terrain_height(height);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
// use the best available alt estimate via baro above home
|
|
|
|
if (flight_stage == AP_SpdHgtControl::FLIGHT_LAND_APPROACH ||
|
2016-01-30 02:32:42 -04:00
|
|
|
flight_stage == AP_SpdHgtControl::FLIGHT_LAND_PREFLARE ||
|
2015-10-15 18:50:06 -03:00
|
|
|
flight_stage == AP_SpdHgtControl::FLIGHT_LAND_FINAL) {
|
|
|
|
// ensure the rangefinder is powered-on when land alt is higher than home altitude.
|
|
|
|
// This is done using the target alt which we know is below us and we are sinking to it
|
|
|
|
height = height_above_target();
|
|
|
|
} else {
|
|
|
|
// otherwise just use the best available baro estimate above home.
|
|
|
|
height = relative_altitude();
|
|
|
|
}
|
|
|
|
rangefinder.set_estimated_terrain_height(height);
|
|
|
|
}
|
|
|
|
|
2014-08-26 08:17:47 -03:00
|
|
|
rangefinder.update();
|
|
|
|
|
2014-01-13 22:07:43 -04:00
|
|
|
if (should_log(MASK_LOG_SONAR))
|
2013-10-30 19:23:21 -03:00
|
|
|
Log_Write_Sonar();
|
2014-08-26 22:50:07 -03:00
|
|
|
|
|
|
|
rangefinder_height_update();
|
2015-05-13 20:00:11 -03:00
|
|
|
#endif
|
2013-10-30 19:23:21 -03:00
|
|
|
}
|
|
|
|
|
2015-07-31 00:21:50 -03:00
|
|
|
/*
|
|
|
|
calibrate compass
|
|
|
|
*/
|
|
|
|
void Plane::compass_cal_update() {
|
|
|
|
if (!hal.util->get_soft_armed()) {
|
|
|
|
compass.compass_cal_update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-21 18:11:52 -03:00
|
|
|
/*
|
|
|
|
Accel calibration
|
|
|
|
*/
|
|
|
|
void Plane::accel_cal_update() {
|
|
|
|
if (hal.util->get_soft_armed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ins.acal_update();
|
|
|
|
float trim_roll, trim_pitch;
|
|
|
|
if(ins.get_new_trim(trim_roll, trim_pitch)) {
|
|
|
|
ahrs.set_trim(Vector3f(trim_roll, trim_pitch, 0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-28 09:37:07 -03:00
|
|
|
/*
|
|
|
|
ask airspeed sensor for a new value
|
|
|
|
*/
|
2015-05-13 03:09:36 -03:00
|
|
|
void Plane::read_airspeed(void)
|
2011-09-08 22:29:39 -03:00
|
|
|
{
|
2013-06-04 00:34:58 -03:00
|
|
|
if (airspeed.enabled()) {
|
|
|
|
airspeed.read();
|
2014-01-27 19:35:59 -04:00
|
|
|
if (should_log(MASK_LOG_IMU)) {
|
|
|
|
Log_Write_Airspeed();
|
|
|
|
}
|
2013-06-04 00:34:58 -03:00
|
|
|
calc_airspeed_errors();
|
2014-11-11 20:26:49 -04:00
|
|
|
|
|
|
|
// supply a new temperature to the barometer from the digital
|
|
|
|
// airspeed sensor if we can
|
|
|
|
float temperature;
|
|
|
|
if (airspeed.get_temperature(temperature)) {
|
|
|
|
barometer.set_external_temperature(temperature);
|
|
|
|
}
|
2013-06-04 00:34:58 -03:00
|
|
|
}
|
2014-11-11 22:35:34 -04:00
|
|
|
|
|
|
|
// update smoothed airspeed estimate
|
|
|
|
float aspeed;
|
|
|
|
if (ahrs.airspeed_estimate(&aspeed)) {
|
|
|
|
smoothed_airspeed = smoothed_airspeed * 0.8f + aspeed * 0.2f;
|
|
|
|
}
|
2011-09-08 22:29:39 -03:00
|
|
|
}
|
|
|
|
|
2015-05-13 03:09:36 -03:00
|
|
|
void Plane::zero_airspeed(bool in_startup)
|
2011-09-08 22:29:39 -03:00
|
|
|
{
|
2014-11-13 06:12:59 -04:00
|
|
|
airspeed.calibrate(in_startup);
|
2014-11-11 20:26:49 -04:00
|
|
|
read_airspeed();
|
|
|
|
// update barometric calibration with new airspeed supplied temperature
|
|
|
|
barometer.update_calibration();
|
2016-05-24 01:23:17 -03:00
|
|
|
gcs_send_text(MAV_SEVERITY_INFO,"Airspeed calibration started");
|
2011-09-08 22:29:39 -03:00
|
|
|
}
|
|
|
|
|
2013-09-29 09:54:39 -03:00
|
|
|
// read_battery - reads battery voltage and current and invokes failsafe
|
|
|
|
// should be called at 10hz
|
2015-05-13 03:09:36 -03:00
|
|
|
void Plane::read_battery(void)
|
2011-09-08 22:29:39 -03:00
|
|
|
{
|
2013-09-29 09:54:39 -03:00
|
|
|
battery.read();
|
2014-04-14 19:28:32 -03:00
|
|
|
compass.set_current(battery.current_amps());
|
2012-08-16 21:50:15 -03:00
|
|
|
|
2015-08-13 23:41:19 -03:00
|
|
|
if (!usb_connected &&
|
|
|
|
hal.util->get_soft_armed() &&
|
|
|
|
battery.exhausted(g.fs_batt_voltage, g.fs_batt_mah)) {
|
2013-05-22 07:33:57 -03:00
|
|
|
low_battery_event();
|
|
|
|
}
|
2016-02-29 06:24:33 -04:00
|
|
|
|
|
|
|
if (should_log(MASK_LOG_CURRENT)) {
|
|
|
|
Log_Write_Current();
|
|
|
|
}
|
2011-09-08 22:29:39 -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
|
2015-05-13 03:09:36 -03:00
|
|
|
void Plane::read_receiver_rssi(void)
|
2012-08-22 00:33:12 -03:00
|
|
|
{
|
2015-08-28 03:01:32 -03:00
|
|
|
receiver_rssi = rssi.read_receiver_rssi_uint8();
|
2012-08-22 00:33:12 -03:00
|
|
|
}
|
2012-09-19 03:22:53 -03:00
|
|
|
|
2015-09-24 07:58:18 -03:00
|
|
|
/*
|
|
|
|
update RPM sensors
|
|
|
|
*/
|
|
|
|
void Plane::rpm_update(void)
|
|
|
|
{
|
|
|
|
rpm_sensor.update();
|
|
|
|
if (rpm_sensor.healthy(0) || rpm_sensor.healthy(1)) {
|
|
|
|
if (should_log(MASK_LOG_RC)) {
|
|
|
|
DataFlash.Log_Write_RPM(rpm_sensor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-21 21:28:17 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
update AP_Button
|
|
|
|
*/
|
|
|
|
void Plane::button_update(void)
|
|
|
|
{
|
|
|
|
g2.button.update();
|
|
|
|
}
|
2016-07-23 04:37:42 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
update AP_ICEngine
|
|
|
|
*/
|
|
|
|
void Plane::ice_update(void)
|
|
|
|
{
|
|
|
|
g2.ice_control.update();
|
|
|
|
}
|