Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AP_AHRS/AP_AHRS_View.cpp
Views: 1798
/*1This program is free software: you can redistribute it and/or modify2it under the terms of the GNU General Public License as published by3the Free Software Foundation, either version 3 of the License, or4(at your option) any later version.56This program is distributed in the hope that it will be useful,7but WITHOUT ANY WARRANTY; without even the implied warranty of8MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9GNU General Public License for more details.1011You should have received a copy of the GNU General Public License12along with this program. If not, see <http://www.gnu.org/licenses/>.13*/1415/*16* AHRS View class - for creating a 2nd view of the vehicle attitude17*18*/1920#include "AP_AHRS_View.h"21#include <stdio.h>2223AP_AHRS_View::AP_AHRS_View(AP_AHRS &_ahrs, enum Rotation _rotation, float pitch_trim_deg) :24rotation(_rotation),25ahrs(_ahrs)26{27switch (rotation) {28case ROTATION_NONE:29y_angle = 0;30break;31case ROTATION_PITCH_90:32y_angle = 90;33break;34case ROTATION_PITCH_270:35y_angle = 270;36break;37default:38AP_HAL::panic("Unsupported AHRS view %u", (unsigned)rotation);39}4041_pitch_trim_deg = pitch_trim_deg;42// Add pitch trim43rot_view.from_euler(0, radians(wrap_360(y_angle + pitch_trim_deg)), 0);44rot_view_T = rot_view;45rot_view_T.transpose();4647// setup initial state48update();49}5051// apply pitch trim52void AP_AHRS_View::set_pitch_trim(float trim_deg) {53_pitch_trim_deg = trim_deg;54rot_view.from_euler(0, radians(wrap_360(y_angle + _pitch_trim_deg)), 0);55rot_view_T = rot_view;56rot_view_T.transpose();57};5859// update state60void AP_AHRS_View::update()61{62rot_body_to_ned = ahrs.get_rotation_body_to_ned();63gyro = ahrs.get_gyro();6465if (!is_zero(y_angle + _pitch_trim_deg)) {66rot_body_to_ned = rot_body_to_ned * rot_view_T;67gyro = rot_view * gyro;68}6970rot_body_to_ned.to_euler(&roll, &pitch, &yaw);7172roll_sensor = degrees(roll) * 100;73pitch_sensor = degrees(pitch) * 100;74yaw_sensor = degrees(yaw) * 100;75if (yaw_sensor < 0) {76yaw_sensor += 36000;77}7879ahrs.calc_trig(rot_body_to_ned,80trig.cos_roll, trig.cos_pitch, trig.cos_yaw,81trig.sin_roll, trig.sin_pitch, trig.sin_yaw);82}8384// return a smoothed and corrected gyro vector using the latest ins data (which may not have been consumed by the EKF yet)85Vector3f AP_AHRS_View::get_gyro_latest(void) const {86return rot_view * ahrs.get_gyro_latest();87}8889// rotate a 2D vector from earth frame to body frame90Vector2f AP_AHRS_View::earth_to_body2D(const Vector2f &ef) const91{92return Vector2f(ef.x * trig.cos_yaw + ef.y * trig.sin_yaw,93-ef.x * trig.sin_yaw + ef.y * trig.cos_yaw);94}9596// rotate a 2D vector from earth frame to body frame97Vector2f AP_AHRS_View::body_to_earth2D(const Vector2f &bf) const98{99return Vector2f(bf.x * trig.cos_yaw - bf.y * trig.sin_yaw,100bf.x * trig.sin_yaw + bf.y * trig.cos_yaw);101}102103// Rotate vector from AHRS reference frame to AHRS view reference frame104void AP_AHRS_View::rotate(Vector3f &vec) const105{106vec = rot_view * vec;107}108109110