2012-11-05 00:31:02 -04:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
2012-12-12 15:21:58 -04:00
|
|
|
#include <AP_HAL.h>
|
2012-11-07 09:24:00 -04:00
|
|
|
#include <AP_InertialNav.h>
|
2012-11-05 00:31:02 -04:00
|
|
|
|
2012-12-12 15:21:58 -04:00
|
|
|
extern const AP_HAL::HAL& hal;
|
2012-11-05 00:31:02 -04:00
|
|
|
|
|
|
|
// table of user settable parameters
|
2012-11-07 09:24:00 -04:00
|
|
|
const AP_Param::GroupInfo AP_InertialNav::var_info[] PROGMEM = {
|
2013-01-22 05:16:18 -04:00
|
|
|
// start numbering at 1 because 0 was previous used for body frame accel offsets
|
2012-11-05 00:31:02 -04:00
|
|
|
// @Param: TC_XY
|
|
|
|
// @DisplayName: Horizontal Time Constant
|
2012-12-09 11:43:11 -04:00
|
|
|
// @Description: Time constant for GPS and accel mixing. Higher TC decreases GPS impact on position estimate
|
2012-11-05 00:31:02 -04:00
|
|
|
// @Range: 0 10
|
|
|
|
// @Increment: 0.1
|
2012-11-07 09:24:00 -04:00
|
|
|
AP_GROUPINFO("TC_XY", 1, AP_InertialNav, _time_constant_xy, AP_INTERTIALNAV_TC_XY),
|
2012-11-05 00:31:02 -04:00
|
|
|
|
|
|
|
// @Param: TC_Z
|
|
|
|
// @DisplayName: Vertical Time Constant
|
2012-12-09 11:43:11 -04:00
|
|
|
// @Description: Time constant for baro and accel mixing. Higher TC decreases barometers impact on altitude estimate
|
2012-11-05 00:31:02 -04:00
|
|
|
// @Range: 0 10
|
|
|
|
// @Increment: 0.1
|
2012-11-07 09:24:00 -04:00
|
|
|
AP_GROUPINFO("TC_Z", 2, AP_InertialNav, _time_constant_z, AP_INTERTIALNAV_TC_Z),
|
2012-11-05 00:31:02 -04:00
|
|
|
|
|
|
|
AP_GROUPEND
|
|
|
|
};
|
|
|
|
|
2012-12-09 11:43:11 -04:00
|
|
|
// init - initialise library
|
|
|
|
void AP_InertialNav::init()
|
2012-11-05 00:31:02 -04:00
|
|
|
{
|
2012-12-09 11:43:11 -04:00
|
|
|
// recalculate the gains
|
|
|
|
update_gains();
|
2012-11-05 00:31:02 -04:00
|
|
|
}
|
|
|
|
|
2013-10-25 10:55:31 -03:00
|
|
|
// update - updates velocities and positions using latest info from ahrs and barometer if new data is available;
|
2012-12-09 11:43:11 -04:00
|
|
|
void AP_InertialNav::update(float dt)
|
2012-11-05 00:31:02 -04:00
|
|
|
{
|
2012-12-09 11:43:11 -04:00
|
|
|
// discard samples where dt is too large
|
2013-01-10 14:42:24 -04:00
|
|
|
if( dt > 0.1f ) {
|
2012-12-09 11:43:11 -04:00
|
|
|
return;
|
2012-11-05 00:31:02 -04:00
|
|
|
}
|
|
|
|
|
2013-11-21 02:32:16 -04:00
|
|
|
// decrement ignore error count if required
|
|
|
|
if (_flags.ignore_error > 0) {
|
|
|
|
_flags.ignore_error--;
|
|
|
|
}
|
|
|
|
|
2013-10-25 10:55:31 -03:00
|
|
|
// check if new baro readings have arrived and use them to correct vertical accelerometer offsets.
|
2012-12-09 11:43:11 -04:00
|
|
|
check_baro();
|
2012-11-05 00:31:02 -04:00
|
|
|
|
2014-10-17 15:39:52 -03:00
|
|
|
// check if home has moved and update
|
|
|
|
check_home();
|
|
|
|
|
2013-10-25 10:55:31 -03:00
|
|
|
// check if new gps readings have arrived and use them to correct position estimates
|
2012-12-09 11:43:11 -04:00
|
|
|
check_gps();
|
2012-11-05 00:31:02 -04:00
|
|
|
|
2014-01-02 01:04:52 -04:00
|
|
|
Vector3f accel_ef = _ahrs.get_accel_ef();
|
2012-12-09 11:43:11 -04:00
|
|
|
|
|
|
|
// remove influence of gravity
|
2013-04-04 23:48:41 -03:00
|
|
|
accel_ef.z += GRAVITY_MSS;
|
2014-07-14 22:33:46 -03:00
|
|
|
accel_ef *= 100.0f;
|
2012-12-09 11:43:11 -04:00
|
|
|
|
|
|
|
// remove xy if not enabled
|
|
|
|
if( !_xy_enabled ) {
|
2014-07-14 22:33:46 -03:00
|
|
|
accel_ef.x = 0.0f;
|
|
|
|
accel_ef.y = 0.0f;
|
2012-11-05 00:31:02 -04:00
|
|
|
}
|
2013-01-22 05:16:18 -04:00
|
|
|
|
2013-01-05 22:59:09 -04:00
|
|
|
//Convert North-East-Down to North-East-Up
|
|
|
|
accel_ef.z = -accel_ef.z;
|
2013-01-22 05:16:18 -04:00
|
|
|
|
2014-06-19 20:53:12 -03:00
|
|
|
// convert ef position error to horizontal body frame
|
|
|
|
Vector2f position_error_hbf;
|
|
|
|
position_error_hbf.x = _position_error.x * _ahrs.cos_yaw() + _position_error.y * _ahrs.sin_yaw();
|
|
|
|
position_error_hbf.y = -_position_error.x * _ahrs.sin_yaw() + _position_error.y * _ahrs.cos_yaw();
|
|
|
|
|
2013-04-21 09:24:09 -03:00
|
|
|
float tmp = _k3_xy * dt;
|
2014-06-19 20:53:12 -03:00
|
|
|
accel_correction_hbf.x += position_error_hbf.x * tmp;
|
|
|
|
accel_correction_hbf.y += position_error_hbf.y * tmp;
|
|
|
|
accel_correction_hbf.z += _position_error.z * _k3_z * dt;
|
2013-01-22 05:16:18 -04:00
|
|
|
|
2013-04-21 09:24:09 -03:00
|
|
|
tmp = _k2_xy * dt;
|
|
|
|
_velocity.x += _position_error.x * tmp;
|
|
|
|
_velocity.y += _position_error.y * tmp;
|
2013-01-05 22:59:09 -04:00
|
|
|
_velocity.z += _position_error.z * _k2_z * dt;
|
2013-01-22 05:16:18 -04:00
|
|
|
|
2013-04-21 09:24:09 -03:00
|
|
|
tmp = _k1_xy * dt;
|
|
|
|
_position_correction.x += _position_error.x * tmp;
|
|
|
|
_position_correction.y += _position_error.y * tmp;
|
2013-01-05 22:59:09 -04:00
|
|
|
_position_correction.z += _position_error.z * _k1_z * dt;
|
2013-01-22 05:16:18 -04:00
|
|
|
|
2014-06-19 20:53:12 -03:00
|
|
|
// convert horizontal body frame accel correction to earth frame
|
|
|
|
Vector2f accel_correction_ef;
|
|
|
|
accel_correction_ef.x = accel_correction_hbf.x * _ahrs.cos_yaw() - accel_correction_hbf.y * _ahrs.sin_yaw();
|
|
|
|
accel_correction_ef.y = accel_correction_hbf.x * _ahrs.sin_yaw() + accel_correction_hbf.y * _ahrs.cos_yaw();
|
|
|
|
|
2012-12-18 03:38:56 -04:00
|
|
|
// calculate velocity increase adding new acceleration from accelerometers
|
2014-06-19 20:53:12 -03:00
|
|
|
Vector3f velocity_increase;
|
|
|
|
velocity_increase.x = (accel_ef.x + accel_correction_ef.x) * dt;
|
|
|
|
velocity_increase.y = (accel_ef.y + accel_correction_ef.y) * dt;
|
|
|
|
velocity_increase.z = (accel_ef.z + accel_correction_hbf.z) * dt;
|
2012-11-05 00:31:02 -04:00
|
|
|
|
2012-12-09 11:43:11 -04:00
|
|
|
// calculate new estimate of position
|
2012-12-18 03:38:56 -04:00
|
|
|
_position_base += (_velocity + velocity_increase*0.5) * dt;
|
|
|
|
|
2013-10-25 10:55:31 -03:00
|
|
|
// update the corrected position estimate
|
|
|
|
_position = _position_base + _position_correction;
|
|
|
|
|
2012-12-18 03:38:56 -04:00
|
|
|
// calculate new velocity
|
|
|
|
_velocity += velocity_increase;
|
2012-11-05 00:31:02 -04:00
|
|
|
|
2012-12-09 11:43:11 -04:00
|
|
|
// store 3rd order estimate (i.e. estimated vertical position) for future use
|
2013-07-25 12:40:16 -03:00
|
|
|
_hist_position_estimate_z.push_back(_position_base.z);
|
2012-12-09 11:43:11 -04:00
|
|
|
|
|
|
|
// store 3rd order estimate (i.e. horizontal position) for future use at 10hz
|
|
|
|
_historic_xy_counter++;
|
|
|
|
if( _historic_xy_counter >= AP_INTERTIALNAV_SAVE_POS_AFTER_ITERATIONS ) {
|
|
|
|
_historic_xy_counter = 0;
|
2013-07-25 12:40:16 -03:00
|
|
|
_hist_position_estimate_x.push_back(_position_base.x);
|
|
|
|
_hist_position_estimate_y.push_back(_position_base.y);
|
2012-11-05 00:31:02 -04:00
|
|
|
}
|
2012-12-09 11:43:11 -04:00
|
|
|
}
|
2012-11-05 00:31:02 -04:00
|
|
|
|
2012-12-09 11:43:11 -04:00
|
|
|
//
|
|
|
|
// XY Axis specific methods
|
|
|
|
//
|
2012-11-05 00:31:02 -04:00
|
|
|
|
2012-12-09 11:43:11 -04:00
|
|
|
// position_ok - return true if position has been initialised and have received gps data within 3 seconds
|
2013-04-21 08:04:47 -03:00
|
|
|
bool AP_InertialNav::position_ok() const
|
2012-11-05 00:31:02 -04:00
|
|
|
{
|
2013-03-16 06:12:51 -03:00
|
|
|
return _xy_enabled;
|
2012-11-05 00:31:02 -04:00
|
|
|
}
|
|
|
|
|
2014-10-19 23:08:38 -03:00
|
|
|
// check_home - checks if the home position has moved and offsets everything so it still lines up
|
2014-10-17 15:39:52 -03:00
|
|
|
void AP_InertialNav::check_home() {
|
|
|
|
if (!_xy_enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-19 23:08:38 -03:00
|
|
|
// get position move in lat, lon coordinates
|
2014-10-17 15:39:52 -03:00
|
|
|
int32_t lat_offset = _ahrs.get_home().lat - _last_home_lat;
|
|
|
|
int32_t lng_offset = _ahrs.get_home().lng - _last_home_lng;
|
|
|
|
|
|
|
|
if (lat_offset != 0) {
|
2014-10-19 23:08:38 -03:00
|
|
|
// calculate the position move in cm
|
2014-10-17 15:39:52 -03:00
|
|
|
float x_offset_cm = lat_offset * LATLON_TO_CM;
|
|
|
|
|
2014-10-19 23:08:38 -03:00
|
|
|
// move position
|
2014-10-17 15:39:52 -03:00
|
|
|
_position_base.x -= x_offset_cm;
|
|
|
|
_position.x -= x_offset_cm;
|
|
|
|
|
2014-10-19 23:08:38 -03:00
|
|
|
// update historic positions
|
2014-10-17 15:39:52 -03:00
|
|
|
for (uint8_t i = 0; i < _hist_position_estimate_x.size(); i++) {
|
|
|
|
float &x = _hist_position_estimate_x.peek_mutable(i);
|
|
|
|
x -= x_offset_cm;
|
|
|
|
}
|
|
|
|
|
2014-10-19 23:08:38 -03:00
|
|
|
// update lon scaling
|
2014-10-17 15:39:52 -03:00
|
|
|
_lon_to_cm_scaling = longitude_scale(_ahrs.get_home()) * LATLON_TO_CM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lng_offset != 0) {
|
2014-10-19 23:08:38 -03:00
|
|
|
// calculate the position move in cm
|
2014-10-17 15:39:52 -03:00
|
|
|
float y_offset_cm = lng_offset * _lon_to_cm_scaling;
|
|
|
|
|
2014-10-19 23:08:38 -03:00
|
|
|
// move position
|
2014-10-17 15:39:52 -03:00
|
|
|
_position_base.y -= y_offset_cm;
|
|
|
|
_position.y -= y_offset_cm;
|
|
|
|
|
2014-10-19 23:08:38 -03:00
|
|
|
// update historic positions
|
2014-10-17 15:39:52 -03:00
|
|
|
for (uint8_t i = 0; i < _hist_position_estimate_y.size(); i++) {
|
|
|
|
float &y = _hist_position_estimate_y.peek_mutable(i);
|
|
|
|
y -= y_offset_cm;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-19 23:08:38 -03:00
|
|
|
// store updated lat, lon position
|
2014-10-17 15:39:52 -03:00
|
|
|
_last_home_lat = _ahrs.get_home().lat;
|
|
|
|
_last_home_lng = _ahrs.get_home().lng;
|
|
|
|
}
|
|
|
|
|
2012-11-05 00:31:02 -04:00
|
|
|
// check_gps - check if new gps readings have arrived and use them to correct position estimates
|
2012-11-07 09:24:00 -04:00
|
|
|
void AP_InertialNav::check_gps()
|
2012-11-05 00:31:02 -04:00
|
|
|
{
|
2013-10-25 10:55:31 -03:00
|
|
|
const uint32_t now = hal.scheduler->millis();
|
2012-11-05 00:31:02 -04:00
|
|
|
|
|
|
|
// compare gps time to previous reading
|
2014-03-31 04:08:02 -03:00
|
|
|
const AP_GPS &gps = _ahrs.get_gps();
|
|
|
|
if(gps.last_fix_time_ms() != _gps_last_time ) {
|
2012-11-05 00:31:02 -04:00
|
|
|
|
|
|
|
// call position correction method
|
2014-03-31 04:08:02 -03:00
|
|
|
correct_with_gps(now, gps.location().lng, gps.location().lat);
|
2012-11-05 00:31:02 -04:00
|
|
|
|
|
|
|
// record gps time and system time of this update
|
2014-03-31 04:08:02 -03:00
|
|
|
_gps_last_time = gps.last_fix_time_ms();
|
2013-09-19 03:56:23 -03:00
|
|
|
}else{
|
2013-11-18 10:19:57 -04:00
|
|
|
// if GPS updates stop arriving degrade position error to 10% over 2 seconds (assumes 100hz update rate)
|
2013-09-19 03:56:23 -03:00
|
|
|
if (now - _gps_last_update > AP_INTERTIALNAV_GPS_TIMEOUT_MS) {
|
2014-07-14 22:33:46 -03:00
|
|
|
_position_error.x *= 0.9886f;
|
|
|
|
_position_error.y *= 0.9886f;
|
2013-11-21 02:32:16 -04:00
|
|
|
// increment error count
|
|
|
|
if (_flags.ignore_error == 0 && _error_count < 255 && _xy_enabled) {
|
|
|
|
_error_count++;
|
|
|
|
}
|
2013-09-19 03:56:23 -03:00
|
|
|
}
|
2013-01-22 05:16:18 -04:00
|
|
|
}
|
2012-11-05 00:31:02 -04:00
|
|
|
}
|
|
|
|
|
2013-09-19 03:56:23 -03:00
|
|
|
// correct_with_gps - modifies accelerometer offsets using gps
|
|
|
|
void AP_InertialNav::correct_with_gps(uint32_t now, int32_t lon, int32_t lat)
|
2012-11-05 00:31:02 -04:00
|
|
|
{
|
2013-09-19 03:56:23 -03:00
|
|
|
float dt,x,y;
|
2012-12-09 11:43:11 -04:00
|
|
|
float hist_position_base_x, hist_position_base_y;
|
2012-11-05 00:31:02 -04:00
|
|
|
|
2013-09-19 03:56:23 -03:00
|
|
|
// calculate time since last gps reading
|
|
|
|
dt = (float)(now - _gps_last_update) * 0.001f;
|
|
|
|
|
|
|
|
// update last gps update time
|
|
|
|
_gps_last_update = now;
|
|
|
|
|
2012-11-05 00:31:02 -04:00
|
|
|
// discard samples where dt is too large
|
2014-07-14 22:33:46 -03:00
|
|
|
if( dt > 1.0f || dt == 0.0f || !_xy_enabled) {
|
2012-11-05 00:31:02 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-09 11:43:11 -04:00
|
|
|
// calculate distance from base location
|
2014-01-03 07:41:03 -04:00
|
|
|
x = (float)(lat - _ahrs.get_home().lat) * LATLON_TO_CM;
|
|
|
|
y = (float)(lon - _ahrs.get_home().lng) * _lon_to_cm_scaling;
|
2012-11-05 00:31:02 -04:00
|
|
|
|
2013-09-19 03:56:23 -03:00
|
|
|
// sanity check the gps position. Relies on the main code calling GPS_Glitch::check_position() immediatley after a GPS update
|
|
|
|
if (_glitch_detector.glitching()) {
|
2013-11-18 10:19:57 -04:00
|
|
|
// failed sanity check so degrate position_error to 10% over 2 seconds (assumes 5hz update rate)
|
2014-07-14 22:33:46 -03:00
|
|
|
_position_error.x *= 0.7943f;
|
|
|
|
_position_error.y *= 0.7943f;
|
2012-12-09 11:43:11 -04:00
|
|
|
}else{
|
2013-09-19 03:56:23 -03:00
|
|
|
// if our internal glitching flag (from previous iteration) is true we have just recovered from a glitch
|
|
|
|
// reset the inertial nav position and velocity to gps values
|
|
|
|
if (_flags.gps_glitching) {
|
|
|
|
set_position_xy(x,y);
|
2014-07-14 22:33:46 -03:00
|
|
|
_position_error.x = 0.0f;
|
|
|
|
_position_error.y = 0.0f;
|
2013-09-19 03:56:23 -03:00
|
|
|
}else{
|
|
|
|
// ublox gps positions are delayed by 400ms
|
|
|
|
// we store historical position at 10hz so 4 iterations ago
|
2013-10-25 05:49:43 -03:00
|
|
|
if( _hist_position_estimate_x.is_full()) {
|
|
|
|
hist_position_base_x = _hist_position_estimate_x.front();
|
|
|
|
hist_position_base_y = _hist_position_estimate_y.front();
|
2013-09-19 03:56:23 -03:00
|
|
|
}else{
|
|
|
|
hist_position_base_x = _position_base.x;
|
|
|
|
hist_position_base_y = _position_base.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculate error in position from gps with our historical estimate
|
|
|
|
_position_error.x = x - (hist_position_base_x + _position_correction.x);
|
|
|
|
_position_error.y = y - (hist_position_base_y + _position_correction.y);
|
|
|
|
}
|
2012-11-05 00:31:02 -04:00
|
|
|
}
|
|
|
|
|
2013-09-19 03:56:23 -03:00
|
|
|
// update our internal record of glitching flag so that we can notice a change
|
|
|
|
_flags.gps_glitching = _glitch_detector.glitching();
|
2012-11-05 00:31:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// get accel based latitude
|
2013-04-21 08:04:47 -03:00
|
|
|
int32_t AP_InertialNav::get_latitude() const
|
2012-11-05 00:31:02 -04:00
|
|
|
{
|
|
|
|
// make sure we've been initialised
|
|
|
|
if( !_xy_enabled ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-03 07:41:03 -04:00
|
|
|
return _ahrs.get_home().lat + (int32_t)(_position.x/LATLON_TO_CM);
|
2012-11-05 00:31:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// get accel based longitude
|
2013-04-21 08:04:47 -03:00
|
|
|
int32_t AP_InertialNav::get_longitude() const
|
2012-11-05 00:31:02 -04:00
|
|
|
{
|
|
|
|
// make sure we've been initialised
|
|
|
|
if( !_xy_enabled ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-03 07:41:03 -04:00
|
|
|
return _ahrs.get_home().lng + (int32_t)(_position.y / _lon_to_cm_scaling);
|
2012-12-09 11:43:11 -04:00
|
|
|
}
|
|
|
|
|
2014-01-03 07:41:03 -04:00
|
|
|
// setup_home_position - reset state for home position change
|
|
|
|
void AP_InertialNav::setup_home_position(void)
|
2012-12-09 11:43:11 -04:00
|
|
|
{
|
2013-05-05 02:32:25 -03:00
|
|
|
// set longitude to meters scaling to offset the shrinking longitude as we go towards the poles
|
2014-01-03 07:41:03 -04:00
|
|
|
_lon_to_cm_scaling = longitude_scale(_ahrs.get_home()) * LATLON_TO_CM;
|
2012-12-09 11:43:11 -04:00
|
|
|
|
|
|
|
// reset corrections to base position to zero
|
2014-07-14 22:33:46 -03:00
|
|
|
_position_base.x = 0.0f;
|
|
|
|
_position_base.y = 0.0f;
|
|
|
|
_position_correction.x = 0.0f;
|
|
|
|
_position_correction.y = 0.0f;
|
|
|
|
_position.x = 0.0f;
|
|
|
|
_position.y = 0.0f;
|
2014-10-17 15:39:52 -03:00
|
|
|
_last_home_lat = _ahrs.get_home().lat;
|
|
|
|
_last_home_lng = _ahrs.get_home().lng;
|
2012-12-09 11:43:11 -04:00
|
|
|
|
|
|
|
// clear historic estimates
|
|
|
|
_hist_position_estimate_x.clear();
|
|
|
|
_hist_position_estimate_y.clear();
|
|
|
|
|
|
|
|
// set xy as enabled
|
|
|
|
_xy_enabled = true;
|
2012-11-05 00:31:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// get accel based latitude
|
2013-04-21 08:04:47 -03:00
|
|
|
float AP_InertialNav::get_latitude_diff() const
|
2012-11-05 00:31:02 -04:00
|
|
|
{
|
|
|
|
// make sure we've been initialised
|
|
|
|
if( !_xy_enabled ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-25 10:55:31 -03:00
|
|
|
return (_position.x/LATLON_TO_CM);
|
2012-11-05 00:31:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// get accel based longitude
|
2013-04-21 08:04:47 -03:00
|
|
|
float AP_InertialNav::get_longitude_diff() const
|
2012-11-05 00:31:02 -04:00
|
|
|
{
|
|
|
|
// make sure we've been initialised
|
|
|
|
if( !_xy_enabled ) {
|
2014-07-14 22:33:46 -03:00
|
|
|
return 0.0f;
|
2012-11-05 00:31:02 -04:00
|
|
|
}
|
|
|
|
|
2013-10-25 10:55:31 -03:00
|
|
|
return (_position.y / _lon_to_cm_scaling);
|
2012-11-05 00:31:02 -04:00
|
|
|
}
|
|
|
|
|
2012-12-09 11:43:11 -04:00
|
|
|
// set_velocity_xy - set velocity in latitude & longitude directions (in cm/s)
|
|
|
|
void AP_InertialNav::set_velocity_xy(float x, float y)
|
|
|
|
{
|
|
|
|
_velocity.x = x;
|
|
|
|
_velocity.y = y;
|
|
|
|
}
|
|
|
|
|
2013-10-29 01:25:14 -03:00
|
|
|
// set_velocity_xy - set velocity in latitude & longitude directions (in cm/s)
|
2014-04-21 09:58:42 -03:00
|
|
|
float AP_InertialNav::get_velocity_xy() const
|
2013-10-29 01:25:14 -03:00
|
|
|
{
|
2014-04-01 08:41:26 -03:00
|
|
|
return pythagorous2(_velocity.x, _velocity.y);
|
2013-10-29 01:25:14 -03:00
|
|
|
}
|
|
|
|
|
2012-12-09 11:43:11 -04:00
|
|
|
//
|
|
|
|
// Z Axis methods
|
|
|
|
//
|
|
|
|
|
|
|
|
// check_baro - check if new baro readings have arrived and use them to correct vertical accelerometer offsets
|
|
|
|
void AP_InertialNav::check_baro()
|
|
|
|
{
|
|
|
|
uint32_t baro_update_time;
|
|
|
|
|
2013-10-25 10:55:31 -03:00
|
|
|
// calculate time since last baro reading (in ms)
|
2014-01-02 01:04:52 -04:00
|
|
|
baro_update_time = _baro.get_last_update();
|
2012-12-09 11:43:11 -04:00
|
|
|
if( baro_update_time != _baro_last_update ) {
|
2013-10-25 10:55:31 -03:00
|
|
|
const float dt = (float)(baro_update_time - _baro_last_update) * 0.001f; // in seconds
|
2012-12-09 11:43:11 -04:00
|
|
|
// call correction method
|
2014-07-14 22:33:46 -03:00
|
|
|
correct_with_baro(_baro.get_altitude()*100.0f, dt);
|
2012-12-09 11:43:11 -04:00
|
|
|
_baro_last_update = baro_update_time;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// correct_with_baro - modifies accelerometer offsets using barometer. dt is time since last baro reading
|
|
|
|
void AP_InertialNav::correct_with_baro(float baro_alt, float dt)
|
|
|
|
{
|
|
|
|
static uint8_t first_reads = 0;
|
|
|
|
|
|
|
|
// discard samples where dt is too large
|
2013-01-10 14:42:24 -04:00
|
|
|
if( dt > 0.5f ) {
|
2012-12-09 11:43:11 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// discard first 10 reads but perform some initialisation
|
|
|
|
if( first_reads <= 10 ) {
|
|
|
|
set_altitude(baro_alt);
|
|
|
|
first_reads++;
|
|
|
|
}
|
|
|
|
|
2014-07-28 05:36:46 -03:00
|
|
|
// sanity check the baro position. Relies on the main code calling Baro_Glitch::check_alt() immediatley after a baro update
|
|
|
|
if (_baro_glitch.glitching()) {
|
|
|
|
// failed sanity check so degrate position_error to 10% over 2 seconds (assumes 10hz update rate)
|
|
|
|
_position_error.z *= 0.89715f;
|
2012-12-09 11:43:11 -04:00
|
|
|
}else{
|
2014-07-28 05:36:46 -03:00
|
|
|
// if our internal baro glitching flag (from previous iteration) is true we have just recovered from a glitch
|
|
|
|
// reset the inertial nav alt to baro alt
|
|
|
|
if (_flags.baro_glitching) {
|
|
|
|
set_altitude(baro_alt);
|
|
|
|
_position_error.z = 0.0f;
|
|
|
|
}else{
|
|
|
|
// 3rd order samples (i.e. position from baro) are delayed by 150ms (15 iterations at 100hz)
|
|
|
|
// so we should calculate error using historical estimates
|
|
|
|
float hist_position_base_z;
|
|
|
|
if (_hist_position_estimate_z.is_full()) {
|
|
|
|
hist_position_base_z = _hist_position_estimate_z.front();
|
|
|
|
} else {
|
|
|
|
hist_position_base_z = _position_base.z;
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculate error in position from baro with our estimate
|
|
|
|
_position_error.z = baro_alt - (hist_position_base_z + _position_correction.z);
|
|
|
|
}
|
2012-12-09 11:43:11 -04:00
|
|
|
}
|
|
|
|
|
2014-07-28 05:36:46 -03:00
|
|
|
// update our internal record of glitching flag so that we can notice a change
|
|
|
|
_flags.baro_glitching = _baro_glitch.glitching();
|
2012-12-09 11:43:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// set_altitude - set base altitude estimate in cm
|
|
|
|
void AP_InertialNav::set_altitude( float new_altitude)
|
|
|
|
{
|
|
|
|
_position_base.z = new_altitude;
|
|
|
|
_position_correction.z = 0;
|
2013-10-25 10:55:31 -03:00
|
|
|
_position.z = new_altitude; // _position = _position_base + _position_correction
|
2014-06-06 06:10:55 -03:00
|
|
|
_hist_position_estimate_z.clear(); // reset z history to avoid fake z velocity at next baro calibration (next rearm)
|
2012-12-09 11:43:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Private methods
|
|
|
|
//
|
|
|
|
|
|
|
|
// update_gains - update gains from time constant (given in seconds)
|
|
|
|
void AP_InertialNav::update_gains()
|
|
|
|
{
|
|
|
|
// X & Y axis time constant
|
2014-07-14 22:33:46 -03:00
|
|
|
if (_time_constant_xy == 0.0f) {
|
|
|
|
_k1_xy = _k2_xy = _k3_xy = 0.0f;
|
2012-12-09 11:43:11 -04:00
|
|
|
}else{
|
2014-07-14 22:33:46 -03:00
|
|
|
_k1_xy = 3.0f / _time_constant_xy;
|
|
|
|
_k2_xy = 3.0f / (_time_constant_xy*_time_constant_xy);
|
|
|
|
_k3_xy = 1.0f / (_time_constant_xy*_time_constant_xy*_time_constant_xy);
|
2012-12-09 11:43:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Z axis time constant
|
2014-07-14 22:33:46 -03:00
|
|
|
if (_time_constant_z == 0.0f) {
|
|
|
|
_k1_z = _k2_z = _k3_z = 0.0f;
|
2012-12-09 11:43:11 -04:00
|
|
|
}else{
|
2014-07-14 22:33:46 -03:00
|
|
|
_k1_z = 3.0f / _time_constant_z;
|
|
|
|
_k2_z = 3.0f / (_time_constant_z*_time_constant_z);
|
|
|
|
_k3_z = 1.0f / (_time_constant_z*_time_constant_z*_time_constant_z);
|
2012-12-09 11:43:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set_velocity_z - get latest climb rate (in cm/s)
|
|
|
|
void AP_InertialNav::set_velocity_z(float z )
|
|
|
|
{
|
|
|
|
_velocity.z = z;
|
|
|
|
}
|
2013-09-19 03:56:23 -03:00
|
|
|
|
|
|
|
// set_position_xy - sets inertial navigation position to given xy coordinates from home
|
2013-10-25 10:55:31 -03:00
|
|
|
void AP_InertialNav::set_position_xy(float x, float y)
|
2013-09-19 03:56:23 -03:00
|
|
|
{
|
|
|
|
// reset position from home
|
2013-10-25 10:55:31 -03:00
|
|
|
_position_base.x = x;
|
|
|
|
_position_base.y = y;
|
2014-07-14 22:33:46 -03:00
|
|
|
_position_correction.x = 0.0f;
|
|
|
|
_position_correction.y = 0.0f;
|
2013-09-19 03:56:23 -03:00
|
|
|
|
|
|
|
// clear historic estimates
|
|
|
|
_hist_position_estimate_x.clear();
|
|
|
|
_hist_position_estimate_y.clear();
|
|
|
|
|
|
|
|
// add new position for future use
|
|
|
|
_historic_xy_counter = 0;
|
2013-07-25 12:40:16 -03:00
|
|
|
_hist_position_estimate_x.push_back(_position_base.x);
|
|
|
|
_hist_position_estimate_y.push_back(_position_base.y);
|
2013-10-23 08:14:33 -03:00
|
|
|
}
|