2017-02-11 04:14:23 -04: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.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* AHRS View class - for creating a 2nd view of the vehicle attitude
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "AP_AHRS_View.h"
|
2017-02-11 04:15:34 -04:00
|
|
|
#include <stdio.h>
|
2017-02-11 04:14:23 -04:00
|
|
|
|
2018-11-12 00:58:54 -04:00
|
|
|
AP_AHRS_View::AP_AHRS_View(AP_AHRS &_ahrs, enum Rotation _rotation, float pitch_trim_deg) :
|
2017-02-11 04:14:23 -04:00
|
|
|
rotation(_rotation),
|
|
|
|
ahrs(_ahrs)
|
|
|
|
{
|
2017-02-11 04:15:34 -04:00
|
|
|
switch (rotation) {
|
|
|
|
case ROTATION_NONE:
|
2018-11-12 00:58:54 -04:00
|
|
|
y_angle = 0;
|
2017-02-11 04:15:34 -04:00
|
|
|
break;
|
|
|
|
case ROTATION_PITCH_90:
|
2018-09-25 12:14:36 -03:00
|
|
|
y_angle = 90;
|
2017-02-11 04:15:34 -04:00
|
|
|
break;
|
|
|
|
case ROTATION_PITCH_270:
|
2018-09-25 12:14:36 -03:00
|
|
|
y_angle = 270;
|
2017-02-11 04:15:34 -04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
AP_HAL::panic("Unsupported AHRS view %u\n", (unsigned)rotation);
|
|
|
|
}
|
2017-02-11 04:14:23 -04:00
|
|
|
|
2019-02-04 13:12:29 -04:00
|
|
|
_pitch_trim_deg = pitch_trim_deg;
|
2018-09-25 12:14:36 -03:00
|
|
|
// Add pitch trim
|
2019-02-04 13:12:29 -04:00
|
|
|
rot_view.from_euler(0, radians(wrap_360(y_angle + pitch_trim_deg)), 0);
|
2019-03-23 13:45:57 -03:00
|
|
|
rot_view_T = rot_view;
|
|
|
|
rot_view_T.transpose();
|
2018-09-25 12:14:36 -03:00
|
|
|
|
2017-02-11 04:15:34 -04:00
|
|
|
// setup initial state
|
|
|
|
update();
|
2017-02-11 04:14:23 -04:00
|
|
|
}
|
|
|
|
|
2019-02-04 13:12:29 -04:00
|
|
|
// apply pitch trim
|
|
|
|
void AP_AHRS_View::set_pitch_trim(float trim_deg) {
|
|
|
|
_pitch_trim_deg = trim_deg;
|
|
|
|
rot_view.from_euler(0, radians(wrap_360(y_angle + _pitch_trim_deg)), 0);
|
2019-03-23 13:45:57 -03:00
|
|
|
rot_view_T = rot_view;
|
|
|
|
rot_view_T.transpose();
|
2019-02-04 13:12:29 -04:00
|
|
|
};
|
|
|
|
|
2017-02-11 04:14:23 -04:00
|
|
|
// update state
|
2021-08-26 01:34:08 -03:00
|
|
|
void AP_AHRS_View::update()
|
2017-02-11 04:14:23 -04:00
|
|
|
{
|
|
|
|
rot_body_to_ned = ahrs.get_rotation_body_to_ned();
|
|
|
|
gyro = ahrs.get_gyro();
|
|
|
|
|
2019-03-14 14:40:57 -03:00
|
|
|
if (!is_zero(y_angle + _pitch_trim_deg)) {
|
2019-03-23 13:45:57 -03:00
|
|
|
rot_body_to_ned = rot_body_to_ned * rot_view_T;
|
|
|
|
gyro = rot_view * gyro;
|
2017-02-11 04:15:34 -04:00
|
|
|
}
|
|
|
|
|
2017-02-11 04:14:23 -04:00
|
|
|
rot_body_to_ned.to_euler(&roll, &pitch, &yaw);
|
|
|
|
|
|
|
|
roll_sensor = degrees(roll) * 100;
|
|
|
|
pitch_sensor = degrees(pitch) * 100;
|
|
|
|
yaw_sensor = degrees(yaw) * 100;
|
2017-04-16 23:14:57 -03:00
|
|
|
if (yaw_sensor < 0) {
|
|
|
|
yaw_sensor += 36000;
|
|
|
|
}
|
2017-02-11 04:14:23 -04:00
|
|
|
|
|
|
|
ahrs.calc_trig(rot_body_to_ned,
|
|
|
|
trig.cos_roll, trig.cos_pitch, trig.cos_yaw,
|
|
|
|
trig.sin_roll, trig.sin_pitch, trig.sin_yaw);
|
|
|
|
}
|
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_View::get_gyro_latest(void) const {
|
2022-02-11 18:26:28 -04:00
|
|
|
return rot_view * ahrs.get_gyro_latest();
|
2017-03-02 07:33:13 -04:00
|
|
|
}
|
2017-09-04 03:53:56 -03:00
|
|
|
|
|
|
|
// rotate a 2D vector from earth frame to body frame
|
2020-06-01 21:16:33 -03:00
|
|
|
Vector2f AP_AHRS_View::earth_to_body2D(const Vector2f &ef) const
|
2017-09-04 03:53:56 -03:00
|
|
|
{
|
|
|
|
return Vector2f(ef.x * trig.cos_yaw + ef.y * trig.sin_yaw,
|
|
|
|
-ef.x * trig.sin_yaw + ef.y * trig.cos_yaw);
|
|
|
|
}
|
|
|
|
|
|
|
|
// rotate a 2D vector from earth frame to body frame
|
2020-06-01 21:16:33 -03:00
|
|
|
Vector2f AP_AHRS_View::body_to_earth2D(const Vector2f &bf) const
|
2017-09-04 03:53:56 -03:00
|
|
|
{
|
|
|
|
return Vector2f(bf.x * trig.cos_yaw - bf.y * trig.sin_yaw,
|
|
|
|
bf.x * trig.sin_yaw + bf.y * trig.cos_yaw);
|
|
|
|
}
|
2022-06-16 19:42:39 -03:00
|
|
|
|
|
|
|
// Rotate vector from AHRS reference frame to AHRS view reference frame
|
|
|
|
void AP_AHRS_View::rotate(Vector3f &vec) const
|
|
|
|
{
|
|
|
|
vec = rot_view * vec;
|
|
|
|
}
|