2012-08-21 02:58:09 -03:00
/*
2013-08-26 03:55:30 -03:00
APM_AHRS . cpp
2012-08-21 02:58:09 -03:00
2013-08-26 03:55:30 -03:00
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 .
2015-09-23 04:29:43 -03:00
2013-08-26 03:55:30 -03:00
You should have received a copy of the GNU General Public License
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2012-08-21 02:58:09 -03:00
*/
2015-08-11 03:28:42 -03:00
# include "AP_AHRS.h"
2017-02-11 04:15:34 -04:00
# include "AP_AHRS_View.h"
2015-08-11 03:28:42 -03:00
# include <AP_HAL/AP_HAL.h>
2019-01-18 00:23:42 -04:00
# include <AP_Logger/AP_Logger.h>
2017-02-11 04:15:34 -04:00
2012-11-14 12:10:15 -04:00
extern const AP_HAL : : HAL & hal ;
2012-08-21 02:58:09 -03:00
// table of user settable parameters
2015-10-25 14:03:46 -03:00
const AP_Param : : GroupInfo AP_AHRS : : var_info [ ] = {
2015-09-23 04:29:43 -03:00
// index 0 and 1 are for old parameters that are no longer not used
2012-08-21 02:58:09 -03:00
// @Param: GPS_GAIN
// @DisplayName: AHRS GPS gain
2017-01-17 14:35:08 -04:00
// @Description: This controls how much to use the GPS to correct the attitude. This should never be set to zero for a plane as it would result in the plane losing control in turns. For a plane please use the default value of 1.0.
2012-08-21 02:58:09 -03:00
// @Range: 0.0 1.0
// @Increment: .01
2016-07-26 02:35:15 -03:00
// @User: Advanced
2013-01-10 14:42:24 -04:00
AP_GROUPINFO ( " GPS_GAIN " , 2 , AP_AHRS , gps_gain , 1.0f ) ,
2012-08-21 02:58:09 -03:00
// @Param: GPS_USE
2012-11-27 00:41:52 -04:00
// @DisplayName: AHRS use GPS for navigation
2017-11-16 11:15:28 -04:00
// @Description: This controls whether to use dead-reckoning or GPS based navigation. If set to 0 then the GPS won't be used for navigation, and only dead reckoning will be used. A value of zero should never be used for normal flight. Currently this affects only the DCM-based AHRS: the EKF uses GPS whenever it is available.
2013-05-21 03:39:03 -03:00
// @Values: 0:Disabled,1:Enabled
2012-08-21 02:58:09 -03:00
// @User: Advanced
AP_GROUPINFO ( " GPS_USE " , 3 , AP_AHRS , _gps_use , 1 ) ,
// @Param: YAW_P
// @DisplayName: Yaw P
2012-11-19 07:49:36 -04:00
// @Description: This controls the weight the compass or GPS has on the heading. A higher value means the heading will track the yaw source (GPS or compass) more rapidly.
// @Range: 0.1 0.4
2012-08-21 02:58:09 -03:00
// @Increment: .01
2016-07-26 02:35:15 -03:00
// @User: Advanced
2013-11-04 23:52:39 -04:00
AP_GROUPINFO ( " YAW_P " , 4 , AP_AHRS , _kp_yaw , 0.2f ) ,
2012-08-21 02:58:09 -03:00
// @Param: RP_P
// @DisplayName: AHRS RP_P
// @Description: This controls how fast the accelerometers correct the attitude
2012-11-19 07:49:36 -04:00
// @Range: 0.1 0.4
2012-08-21 02:58:09 -03:00
// @Increment: .01
2016-07-26 02:35:15 -03:00
// @User: Advanced
2013-11-04 23:52:39 -04:00
AP_GROUPINFO ( " RP_P " , 5 , AP_AHRS , _kp , 0.2f ) ,
2012-08-21 02:58:09 -03:00
2012-09-07 22:27:12 -03:00
// @Param: WIND_MAX
// @DisplayName: Maximum wind
// @Description: This sets the maximum allowable difference between ground speed and airspeed. This allows the plane to cope with a failing airspeed sensor. A value of zero means to use the airspeed as is.
// @Range: 0 127
2013-01-02 03:06:53 -04:00
// @Units: m/s
2012-09-07 22:27:12 -03:00
// @Increment: 1
2016-07-26 02:35:15 -03:00
// @User: Advanced
2013-01-10 14:42:24 -04:00
AP_GROUPINFO ( " WIND_MAX " , 6 , AP_AHRS , _wind_max , 0.0f ) ,
2012-09-07 22:27:12 -03:00
2013-04-14 02:40:26 -03:00
// NOTE: 7 was BARO_USE
2012-09-12 01:42:04 -03:00
2013-01-02 02:47:59 -04:00
// @Param: TRIM_X
2013-01-02 02:43:50 -04:00
// @DisplayName: AHRS Trim Roll
2014-01-17 04:19:42 -04:00
// @Description: Compensates for the roll angle difference between the control board and the frame. Positive values make the vehicle roll right.
2017-05-02 10:39:54 -03:00
// @Units: rad
2014-01-17 04:19:42 -04:00
// @Range: -0.1745 +0.1745
// @Increment: 0.01
2016-07-26 02:35:15 -03:00
// @User: Standard
2013-01-02 02:43:50 -04:00
2013-01-02 02:47:59 -04:00
// @Param: TRIM_Y
2013-01-02 02:43:50 -04:00
// @DisplayName: AHRS Trim Pitch
2014-01-17 04:19:42 -04:00
// @Description: Compensates for the pitch angle difference between the control board and the frame. Positive values make the vehicle pitch up/back.
2017-05-02 10:39:54 -03:00
// @Units: rad
2014-01-17 04:19:42 -04:00
// @Range: -0.1745 +0.1745
// @Increment: 0.01
2016-07-26 02:35:15 -03:00
// @User: Standard
2013-01-02 02:43:50 -04:00
2013-01-02 02:47:59 -04:00
// @Param: TRIM_Z
2013-01-02 02:43:50 -04:00
// @DisplayName: AHRS Trim Yaw
// @Description: Not Used
2017-05-02 10:39:54 -03:00
// @Units: rad
2014-01-17 04:19:42 -04:00
// @Range: -0.1745 +0.1745
// @Increment: 0.01
2012-11-05 00:29:00 -04:00
// @User: Advanced
AP_GROUPINFO ( " TRIM " , 8 , AP_AHRS , _trim , 0 ) ,
2013-01-13 01:03:35 -04:00
// @Param: ORIENTATION
// @DisplayName: Board Orientation
2013-01-13 02:29:41 -04:00
// @Description: Overall board orientation relative to the standard orientation for the board type. This rotates the IMU and compass readings to allow the board to be oriented in your vehicle at any 90 or 45 degree angle. This option takes affect on next boot. After changing you will need to re-level your vehicle.
2018-03-08 22:26:56 -04:00
// @Values: 0:None,1:Yaw45,2:Yaw90,3:Yaw135,4:Yaw180,5:Yaw225,6:Yaw270,7:Yaw315,8:Roll180,9:Roll180Yaw45,10:Roll180Yaw90,11:Roll180Yaw135,12:Pitch180,13:Roll180Yaw225,14:Roll180Yaw270,15:Roll180Yaw315,16:Roll90,17:Roll90Yaw45,18:Roll90Yaw90,19:Roll90Yaw135,20:Roll270,21:Roll270Yaw45,22:Roll270Yaw90,23:Roll270Yaw135,24:Pitch90,25:Pitch270,26:Pitch180Yaw90,27:Pitch180Yaw270,28:Roll90Pitch90,29:Roll180Pitch90,30:Roll270Pitch90,31:Roll90Pitch180,32:Roll270Pitch180,33:Roll90Pitch270,34:Roll180Pitch270,35:Roll270Pitch270,36:Roll90Pitch180Yaw90,37:Roll90Yaw270,38:Yaw293Pitch68Roll180,39:Pitch315,40:Roll90Pitch315,100:Custom
2013-01-13 01:03:35 -04:00
// @User: Advanced
AP_GROUPINFO ( " ORIENTATION " , 9 , AP_AHRS , _board_orientation , 0 ) ,
2013-03-29 06:42:26 -03:00
// @Param: COMP_BETA
2015-09-15 21:11:52 -03:00
// @DisplayName: AHRS Velocity Complementary Filter Beta Coefficient
2013-03-29 06:42:26 -03:00
// @Description: This controls the time constant for the cross-over frequency used to fuse AHRS (airspeed and heading) and GPS data to estimate ground velocity. Time constant is 0.1/beta. A larger time constant will use GPS data less and a small time constant will use air data less.
// @Range: 0.001 0.5
// @Increment: .01
2013-05-21 03:39:03 -03:00
// @User: Advanced
2013-03-29 06:42:26 -03:00
AP_GROUPINFO ( " COMP_BETA " , 10 , AP_AHRS , beta , 0.1f ) ,
2013-05-04 23:47:49 -03:00
// @Param: GPS_MINSATS
// @DisplayName: AHRS GPS Minimum satellites
// @Description: Minimum number of satellites visible to use GPS for velocity based corrections attitude correction. This defaults to 6, which is about the point at which the velocity numbers from a GPS become too unreliable for accurate correction of the accelerometers.
// @Range: 0 10
2013-05-21 03:39:03 -03:00
// @Increment: 1
2013-05-04 23:47:49 -03:00
// @User: Advanced
AP_GROUPINFO ( " GPS_MINSATS " , 11 , AP_AHRS , _gps_minsats , 6 ) ,
2014-02-26 17:57:44 -04:00
// NOTE: index 12 was for GPS_DELAY, but now removed, fixed delay
// of 1 was found to be the best choice
2013-11-04 00:50:33 -04:00
2015-09-22 22:40:25 -03:00
// 13 was the old EKF_USE
2015-09-23 04:29:43 -03:00
2015-09-22 22:40:25 -03:00
# if AP_AHRS_NAVEKF_AVAILABLE
// @Param: EKF_TYPE
2014-01-01 22:05:28 -04:00
// @DisplayName: Use NavEKF Kalman filter for attitude and position estimation
2016-06-28 04:36:12 -03:00
// @Description: This controls which NavEKF Kalman filter version is used for attitude and position estimation
// @Values: 0:Disabled,2:Enable EKF2,3:Enable EKF3
2014-01-01 22:05:28 -04:00
// @User: Advanced
2016-01-05 01:42:01 -04:00
AP_GROUPINFO ( " EKF_TYPE " , 14 , AP_AHRS , _ekf_type , 2 ) ,
2014-01-01 22:05:28 -04:00
# endif
2018-03-08 22:26:56 -04:00
// @Param: CUSTOM_ROLL
// @DisplayName: Board orientation roll offset
// @Description: Autopilot mounting position roll offset. Positive values = roll right, negative values = roll left. This parameter is only used when AHRS_ORIENTATION is set to CUSTOM.
// @Range: -180 180
// @Units: deg
// @Increment: 1
// @User: Advanced
AP_GROUPINFO ( " CUSTOM_ROLL " , 15 , AP_AHRS , _custom_roll , 0 ) ,
// @Param: CUSTOM_PIT
// @DisplayName: Board orientation pitch offset
// @Description: Autopilot mounting position pitch offset. Positive values = pitch up, negative values = pitch down. This parameter is only used when AHRS_ORIENTATION is set to CUSTOM.
// @Range: -180 180
// @Units: deg
// @Increment: 1
// @User: Advanced
AP_GROUPINFO ( " CUSTOM_PIT " , 16 , AP_AHRS , _custom_pitch , 0 ) ,
// @Param: CUSTOM_YAW
// @DisplayName: Board orientation yaw offset
// @Description: Autopilot mounting position yaw offset. Positive values = yaw right, negative values = yaw left. This parameter is only used when AHRS_ORIENTATION is set to CUSTOM.
// @Range: -180 180
// @Units: deg
// @Increment: 1
// @User: Advanced
AP_GROUPINFO ( " CUSTOM_YAW " , 17 , AP_AHRS , _custom_yaw , 0 ) ,
2012-08-21 02:58:09 -03:00
AP_GROUPEND
} ;
2012-08-24 08:22:58 -03:00
2017-03-02 07:33:13 -04:00
// return a smoothed and corrected gyro vector using the latest ins data (which may not have been consumed by the EKF yet)
Vector3f AP_AHRS : : get_gyro_latest ( void ) const
{
2017-12-03 06:10:19 -04:00
const uint8_t primary_gyro = get_primary_gyro_index ( ) ;
2018-03-10 05:35:03 -04:00
return AP : : ins ( ) . get_gyro ( primary_gyro ) + get_gyro_drift ( ) ;
2017-03-02 07:33:13 -04:00
}
2012-08-24 08:22:58 -03:00
// return airspeed estimate if available
2013-12-29 03:36:40 -04:00
bool AP_AHRS : : airspeed_estimate ( float * airspeed_ret ) const
2012-08-24 08:22:58 -03:00
{
2015-09-23 04:29:43 -03:00
if ( airspeed_sensor_enabled ( ) ) {
* airspeed_ret = _airspeed - > get_airspeed ( ) ;
2017-12-01 19:50:46 -04:00
if ( _wind_max > 0 & & AP : : gps ( ) . status ( ) > = AP_GPS : : GPS_OK_FIX_2D ) {
2015-09-23 04:29:43 -03:00
// constrain the airspeed by the ground speed
// and AHRS_WIND_MAX
2017-12-01 19:50:46 -04:00
const float gnd_speed = AP : : gps ( ) . ground_speed ( ) ;
2015-09-23 04:29:43 -03:00
float true_airspeed = * airspeed_ret * get_EAS2TAS ( ) ;
true_airspeed = constrain_float ( true_airspeed ,
gnd_speed - _wind_max ,
gnd_speed + _wind_max ) ;
* airspeed_ret = true_airspeed / get_EAS2TAS ( ) ;
}
return true ;
}
return false ;
2012-08-24 08:22:58 -03:00
}
2012-11-05 00:29:00 -04:00
2013-02-19 05:50:57 -04:00
// set_trim
2018-12-20 05:11:16 -04:00
void AP_AHRS : : set_trim ( const Vector3f & new_trim )
2013-02-19 05:50:57 -04:00
{
Vector3f trim ;
2013-05-01 21:25:40 -03:00
trim . x = constrain_float ( new_trim . x , ToRad ( - AP_AHRS_TRIM_LIMIT ) , ToRad ( AP_AHRS_TRIM_LIMIT ) ) ;
trim . y = constrain_float ( new_trim . y , ToRad ( - AP_AHRS_TRIM_LIMIT ) , ToRad ( AP_AHRS_TRIM_LIMIT ) ) ;
2013-02-19 05:50:57 -04:00
_trim . set_and_save ( trim ) ;
}
2012-11-05 00:29:00 -04:00
// add_trim - adjust the roll and pitch trim up to a total of 10 degrees
2012-12-19 11:06:20 -04:00
void AP_AHRS : : add_trim ( float roll_in_radians , float pitch_in_radians , bool save_to_eeprom )
2012-11-05 00:29:00 -04:00
{
Vector3f trim = _trim . get ( ) ;
// add new trim
2013-05-01 21:25:40 -03:00
trim . x = constrain_float ( trim . x + roll_in_radians , ToRad ( - AP_AHRS_TRIM_LIMIT ) , ToRad ( AP_AHRS_TRIM_LIMIT ) ) ;
trim . y = constrain_float ( trim . y + pitch_in_radians , ToRad ( - AP_AHRS_TRIM_LIMIT ) , ToRad ( AP_AHRS_TRIM_LIMIT ) ) ;
2012-11-05 00:29:00 -04:00
2012-12-19 11:06:20 -04:00
// set new trim values
_trim . set ( trim ) ;
// save to eeprom
if ( save_to_eeprom ) {
_trim . save ( ) ;
}
2012-11-19 07:49:36 -04:00
}
2013-03-29 03:23:22 -03:00
2018-03-08 22:26:56 -04:00
// Set the board mounting orientation, may be called while disarmed
2019-02-17 00:53:15 -04:00
void AP_AHRS : : update_orientation ( )
2018-03-08 22:26:56 -04:00
{
2018-12-20 05:11:16 -04:00
const enum Rotation orientation = ( enum Rotation ) _board_orientation . get ( ) ;
2018-03-08 22:26:56 -04:00
if ( orientation ! = ROTATION_CUSTOM ) {
AP : : ins ( ) . set_board_orientation ( orientation ) ;
if ( _compass ! = nullptr ) {
_compass - > set_board_orientation ( orientation ) ;
}
} else {
_custom_rotation . from_euler ( _custom_roll , _custom_pitch , _custom_yaw ) ;
AP : : ins ( ) . set_board_orientation ( orientation , & _custom_rotation ) ;
if ( _compass ! = nullptr ) {
_compass - > set_board_orientation ( orientation , & _custom_rotation ) ;
}
}
}
2013-03-29 04:22:03 -03:00
// return a ground speed estimate in m/s
2013-03-29 03:23:22 -03:00
Vector2f AP_AHRS : : groundspeed_vector ( void )
{
2013-03-29 06:42:26 -03:00
// Generate estimate of ground speed vector using air data system
Vector2f gndVelADS ;
Vector2f gndVelGPS ;
2018-09-04 19:01:59 -03:00
float airspeed = 0 ;
2017-12-03 06:10:19 -04:00
const bool gotAirspeed = airspeed_estimate_true ( & airspeed ) ;
2017-12-01 19:50:46 -04:00
const bool gotGPS = ( AP : : gps ( ) . status ( ) > = AP_GPS : : GPS_OK_FIX_2D ) ;
2013-03-29 06:42:26 -03:00
if ( gotAirspeed ) {
2017-12-02 09:02:37 -04:00
const Vector3f wind = wind_estimate ( ) ;
const Vector2f wind2d ( wind . x , wind . y ) ;
2018-09-04 19:01:59 -03:00
const Vector2f airspeed_vector ( _cos_yaw * airspeed , _sin_yaw * airspeed ) ;
2015-09-23 04:29:43 -03:00
gndVelADS = airspeed_vector - wind2d ;
2013-03-29 06:42:26 -03:00
}
2015-09-23 04:29:43 -03:00
2013-03-29 06:42:26 -03:00
// Generate estimate of ground speed vector using GPS
if ( gotGPS ) {
2017-12-01 19:50:46 -04:00
const float cog = radians ( AP : : gps ( ) . ground_course_cd ( ) * 0.01f ) ;
gndVelGPS = Vector2f ( cosf ( cog ) , sinf ( cog ) ) * AP : : gps ( ) . ground_speed ( ) ;
2013-03-29 06:42:26 -03:00
}
// If both ADS and GPS data is available, apply a complementary filter
if ( gotAirspeed & & gotGPS ) {
2015-09-23 04:29:43 -03:00
// The LPF is applied to the GPS and the HPF is applied to the air data estimate
// before the two are summed
//Define filter coefficients
// alpha and beta must sum to one
// beta = dt/Tau, where
// dt = filter time step (0.1 sec if called by nav loop)
// Tau = cross-over time constant (nominal 2 seconds)
// More lag on GPS requires Tau to be bigger, less lag allows it to be smaller
// To-Do - set Tau as a function of GPS lag.
const float alpha = 1.0f - beta ;
// Run LP filters
_lp = gndVelGPS * beta + _lp * alpha ;
// Run HP filters
_hp = ( gndVelADS - _lastGndVelADS ) + _hp * alpha ;
// Save the current ADS ground vector for the next time step
_lastGndVelADS = gndVelADS ;
// Sum the HP and LP filter outputs
return _hp + _lp ;
2013-03-29 06:42:26 -03:00
}
// Only ADS data is available return ADS estimate
if ( gotAirspeed & & ! gotGPS ) {
2015-09-23 04:29:43 -03:00
return gndVelADS ;
2013-03-29 06:42:26 -03:00
}
// Only GPS data is available so return GPS estimate
if ( ! gotAirspeed & & gotGPS ) {
2015-09-23 04:29:43 -03:00
return gndVelGPS ;
2013-03-29 06:42:26 -03:00
}
2018-09-04 19:01:59 -03:00
if ( airspeed > 0 ) {
// we have a rough airspeed, and we have a yaw. For
// dead-reckoning purposes we can create a estimated
// groundspeed vector
Vector2f ret ( cosf ( yaw ) , sinf ( yaw ) ) ;
ret * = airspeed ;
// adjust for estimated wind
2018-12-20 05:11:16 -04:00
const Vector3f wind = wind_estimate ( ) ;
2018-09-04 19:01:59 -03:00
ret . x + = wind . x ;
ret . y + = wind . y ;
return ret ;
}
2013-03-29 06:42:26 -03:00
return Vector2f ( 0.0f , 0.0f ) ;
2013-03-29 03:23:22 -03:00
}
2013-05-16 19:32:21 -03:00
2017-02-11 04:15:34 -04:00
/*
calculate sin and cos of roll / pitch / yaw from a body_to_ned rotation matrix
*/
void AP_AHRS : : calc_trig ( const Matrix3f & rot ,
float & cr , float & cp , float & cy ,
float & sr , float & sp , float & sy ) const
2014-02-08 02:52:03 -04:00
{
2017-12-02 09:02:37 -04:00
Vector2f yaw_vector ( rot . a . x , rot . b . x ) ;
2014-02-08 02:52:03 -04:00
2017-02-11 04:15:34 -04:00
if ( fabsf ( yaw_vector . x ) > 0 | |
fabsf ( yaw_vector . y ) > 0 ) {
yaw_vector . normalize ( ) ;
}
2017-12-02 09:02:37 -04:00
sy = constrain_float ( yaw_vector . y , - 1.0f , 1.0f ) ;
cy = constrain_float ( yaw_vector . x , - 1.0f , 1.0f ) ;
2014-02-08 02:52:03 -04:00
2017-02-11 04:15:34 -04:00
// sanity checks
if ( yaw_vector . is_inf ( ) | | yaw_vector . is_nan ( ) ) {
sy = 0.0f ;
cy = 1.0f ;
}
2017-12-03 06:10:19 -04:00
const float cx2 = rot . c . x * rot . c . x ;
2014-10-24 21:21:04 -03:00
if ( cx2 > = 1.0f ) {
2017-02-11 04:15:34 -04:00
cp = 0 ;
cr = 1.0f ;
2014-10-24 21:21:04 -03:00
} else {
2017-02-11 04:15:34 -04:00
cp = safe_sqrt ( 1 - cx2 ) ;
cr = rot . c . z / cp ;
2014-10-24 21:21:04 -03:00
}
2017-12-02 09:02:37 -04:00
cp = constrain_float ( cp , 0.0f , 1.0f ) ;
cr = constrain_float ( cr , - 1.0f , 1.0f ) ; // this relies on constrain_float() of infinity doing the right thing
2014-02-08 02:52:03 -04:00
2017-02-11 04:15:34 -04:00
sp = - rot . c . x ;
2015-03-22 21:03:54 -03:00
2017-02-11 04:15:34 -04:00
if ( ! is_zero ( cp ) ) {
sr = rot . c . y / cp ;
2015-03-22 21:03:54 -03:00
}
2017-02-11 04:15:34 -04:00
if ( is_zero ( cp ) | | isinf ( cr ) | | isnan ( cr ) | | isinf ( sr ) | | isnan ( sr ) ) {
float r , p , y ;
rot . to_euler ( & r , & p , & y ) ;
cr = cosf ( r ) ;
sr = sinf ( r ) ;
2015-03-22 21:03:54 -03:00
}
2017-02-11 04:15:34 -04:00
}
2015-03-22 21:03:54 -03:00
2017-02-11 04:15:34 -04:00
// update_trig - recalculates _cos_roll, _cos_pitch, etc based on latest attitude
// should be called after _dcm_matrix is updated
void AP_AHRS : : update_trig ( void )
{
if ( _last_trim ! = _trim . get ( ) ) {
_last_trim = _trim . get ( ) ;
_rotation_autopilot_body_to_vehicle_body . from_euler ( _last_trim . x , _last_trim . y , 0.0f ) ;
_rotation_vehicle_body_to_autopilot_body = _rotation_autopilot_body_to_vehicle_body . transposed ( ) ;
2015-03-22 21:03:54 -03:00
}
2017-02-11 04:15:34 -04:00
calc_trig ( get_rotation_body_to_ned ( ) ,
_cos_roll , _cos_pitch , _cos_yaw ,
_sin_roll , _sin_pitch , _sin_yaw ) ;
2014-02-08 02:52:03 -04:00
}
2014-10-14 23:18:08 -03:00
/*
update the centi - degree values
*/
void AP_AHRS : : update_cd_values ( void )
{
roll_sensor = degrees ( roll ) * 100 ;
pitch_sensor = degrees ( pitch ) * 100 ;
yaw_sensor = degrees ( yaw ) * 100 ;
if ( yaw_sensor < 0 )
yaw_sensor + = 36000 ;
}
2017-02-11 04:15:34 -04:00
/*
2018-11-12 00:58:54 -04:00
create a rotated view of AP_AHRS with optional pitch trim
2017-02-11 04:15:34 -04:00
*/
2018-11-12 00:58:54 -04:00
AP_AHRS_View * AP_AHRS : : create_view ( enum Rotation rotation , float pitch_trim_deg )
2017-02-11 04:15:34 -04:00
{
if ( _view ! = nullptr ) {
// can only have one
return nullptr ;
}
2018-11-12 00:58:54 -04:00
_view = new AP_AHRS_View ( * this , rotation , pitch_trim_deg ) ;
2017-02-11 04:15:34 -04:00
return _view ;
}
2017-04-09 08:17:05 -03:00
/*
* Update AOA and SSA estimation based on airspeed , velocity vector and wind vector
*
* Based on :
* " On estimation of wind velocity, angle-of-attack and sideslip angle of small UAVs using standard sensors " by
* Tor A . Johansen , Andrea Cristofaro , Kim Sorensen , Jakob M . Hansen , Thor I . Fossen
*
* " Multi-Stage Fusion Algorithm for Estimation of Aerodynamic Angles in Mini Aerial Vehicle " by
* C . Ramprasadh and Hemendra Arya
*
* " ANGLE OF ATTACK AND SIDESLIP ESTIMATION USING AN INERTIAL REFERENCE PLATFORM " by
* JOSEPH E . ZEIS , JR . , CAPTAIN , USAF
*/
void AP_AHRS : : update_AOA_SSA ( void )
{
# if APM_BUILD_TYPE(APM_BUILD_ArduPlane)
2017-12-03 06:10:19 -04:00
const uint32_t now = AP_HAL : : millis ( ) ;
2017-04-09 08:17:05 -03:00
if ( now - _last_AOA_update_ms < 50 ) {
// don't update at more than 20Hz
return ;
}
_last_AOA_update_ms = now ;
Vector3f aoa_velocity , aoa_wind ;
// get velocity and wind
if ( get_velocity_NED ( aoa_velocity ) = = false ) {
return ;
}
aoa_wind = wind_estimate ( ) ;
// Rotate vectors to the body frame and calculate velocity and wind
const Matrix3f & rot = get_rotation_body_to_ned ( ) ;
aoa_velocity = rot . mul_transpose ( aoa_velocity ) ;
aoa_wind = rot . mul_transpose ( aoa_wind ) ;
// calculate relative velocity in body coordinates
aoa_velocity = aoa_velocity - aoa_wind ;
2017-12-03 06:10:19 -04:00
const float vel_len = aoa_velocity . length ( ) ;
2017-04-09 08:17:05 -03:00
// do not calculate if speed is too low
if ( vel_len < 2.0 ) {
_AOA = 0 ;
_SSA = 0 ;
return ;
}
// Calculate AOA and SSA
if ( aoa_velocity . x > 0 ) {
_AOA = degrees ( atanf ( aoa_velocity . z / aoa_velocity . x ) ) ;
} else {
_AOA = 0 ;
}
_SSA = degrees ( safe_asin ( aoa_velocity . y / vel_len ) ) ;
# endif
}
// return current AOA
float AP_AHRS : : getAOA ( void )
{
update_AOA_SSA ( ) ;
return _AOA ;
}
// return calculated SSA
float AP_AHRS : : getSSA ( void )
{
update_AOA_SSA ( ) ;
return _SSA ;
}
2017-09-04 03:53:56 -03:00
// rotate a 2D vector from earth frame to body frame
Vector2f AP_AHRS : : rotate_earth_to_body2D ( const Vector2f & ef ) const
{
return Vector2f ( ef . x * _cos_yaw + ef . y * _sin_yaw ,
- ef . x * _sin_yaw + ef . y * _cos_yaw ) ;
}
// rotate a 2D vector from earth frame to body frame
Vector2f AP_AHRS : : rotate_body_to_earth2D ( const Vector2f & bf ) const
{
return Vector2f ( bf . x * _cos_yaw - bf . y * _sin_yaw ,
bf . x * _sin_yaw + bf . y * _cos_yaw ) ;
}
2018-01-01 08:03:28 -04:00
2019-02-11 04:15:24 -04:00
// log ahrs home and EKF origin
2018-05-16 00:28:31 -03:00
void AP_AHRS : : Log_Write_Home_And_Origin ( )
{
2019-02-11 04:15:24 -04:00
AP_Logger * logger = AP_Logger : : get_singleton ( ) ;
if ( logger = = nullptr ) {
2018-05-16 00:28:31 -03:00
return ;
}
# if AP_AHRS_NAVEKF_AVAILABLE
Location ekf_orig ;
if ( get_origin ( ekf_orig ) ) {
2019-02-11 04:15:24 -04:00
logger - > Write_Origin ( LogOriginType : : ekf_origin , ekf_orig ) ;
2018-05-16 00:28:31 -03:00
}
# endif
if ( home_is_set ( ) ) {
2019-02-11 04:15:24 -04:00
logger - > Write_Origin ( LogOriginType : : ahrs_home , _home ) ;
2018-05-16 00:28:31 -03:00
}
}
2018-01-01 08:03:28 -04:00
// singleton instance
AP_AHRS * AP_AHRS : : _singleton ;
namespace AP {
AP_AHRS & ahrs ( )
{
return * AP_AHRS : : get_singleton ( ) ;
}
}