Path: blob/master/libraries/AC_AttitudeControl/AC_AttitudeControl_Multi_6DoF.cpp
9390 views
#include <AP_Scripting/AP_Scripting_config.h>12#if AP_SCRIPTING_ENABLED34#include "AC_AttitudeControl_Multi_6DoF.h"5#include <AP_HAL/AP_HAL.h>6#include <AP_Math/AP_Math.h>78// 6DoF control is extracted from the existing copter code by treating desired angles as thrust angles rather than vehicle attitude.9// Vehicle attitude is then set separately, typically the vehicle would maintain 0 roll and pitch.10// rate commands result in the vehicle behaving as a ordinary copter.1112// run lowest level body-frame rate controller and send outputs to the motors13void AC_AttitudeControl_Multi_6DoF::rate_controller_run() {1415// pass current offsets to motors and run baseclass controller16// motors require the offsets to know which way is up17float roll_deg = roll_offset_deg;18float pitch_deg = pitch_offset_deg;19// if 6DoF control, always point directly up20// this stops horizontal drift due to error between target and true attitude21if (lateral_enable) {22roll_deg = AP::ahrs().get_roll_deg();23}24if (forward_enable) {25pitch_deg = AP::ahrs().get_pitch_deg();26}27_motors.set_roll_pitch(roll_deg,pitch_deg);2829AC_AttitudeControl_Multi::rate_controller_run();30}3132/*33override all input to the attitude controller and convert desired angles into thrust angles and substitute34*/3536// Sets desired roll and pitch angles (in radians) and yaw rate (in radians/s).37// Used when roll/pitch stabilization is needed with manual or autonomous yaw rate control.38// Applies acceleration-limited input shaping for smooth transitions and computes body-frame angular velocity targets.39void AC_AttitudeControl_Multi_6DoF::input_euler_angle_roll_pitch_euler_rate_yaw_rad(float euler_roll_angle_rad, float euler_pitch_angle_rad, float euler_yaw_rate_rads) {4041set_forward_lateral_rad(euler_pitch_angle_rad, euler_roll_angle_rad);4243AC_AttitudeControl_Multi::input_euler_angle_roll_pitch_euler_rate_yaw_rad(euler_roll_angle_rad, euler_pitch_angle_rad, euler_yaw_rate_rads);44}454647// Sets desired roll, pitch, and yaw angles (in radians).48// Used to follow an absolute attitude setpoint. Input shaping and yaw slew limits are applied.49// Outputs are passed to the rate controller via shaped angular velocity targets.50void AC_AttitudeControl_Multi_6DoF::input_euler_angle_roll_pitch_yaw_rad(float euler_roll_angle_rad, float euler_pitch_angle_rad, float euler_yaw_angle_rad, bool slew_yaw) {5152set_forward_lateral_rad(euler_pitch_angle_rad, euler_roll_angle_rad);5354AC_AttitudeControl_Multi::input_euler_angle_roll_pitch_yaw_rad(euler_roll_angle_rad, euler_pitch_angle_rad, euler_yaw_angle_rad, slew_yaw);55}5657// Sets desired thrust vector and heading rate (in radians/s).58// Used for tilt-based navigation with independent yaw control.59// The thrust vector defines the desired orientation (e.g., pointing direction for vertical thrust),60// while the heading rate adjusts yaw. The input is shaped by acceleration and slew limits.61void AC_AttitudeControl_Multi_6DoF::input_thrust_vector_rate_heading_rads(const Vector3f& thrust_vector, float heading_rate_rads, bool slew_yaw)62{63// convert thrust vector to a roll and pitch angles64// this negates the advantage of using thrust vector control, but works just fine65Vector3f angle_target = attitude_from_thrust_vector(thrust_vector, _ahrs.yaw).to_vector312();6667input_euler_angle_roll_pitch_euler_rate_yaw_rad(angle_target.x, angle_target.y, heading_rate_rads);68}6970// Sets desired thrust vector and heading (in radians) with heading rate (in radians/s).71// Used for advanced attitude control where thrust direction is separated from yaw orientation.72// Heading slew is constrained based on configured limits.73void AC_AttitudeControl_Multi_6DoF::input_thrust_vector_heading_rad(const Vector3f& thrust_vector, float heading_angle_rad, float heading_rate_rads)74{75// convert thrust vector to a roll and pitch angles76Vector3f angle_target = attitude_from_thrust_vector(thrust_vector, _ahrs.yaw).to_vector312();7778// note that we are throwing away heading rate here79input_euler_angle_roll_pitch_yaw_rad(angle_target.x, angle_target.y, heading_angle_rad, true);80}8182void AC_AttitudeControl_Multi_6DoF::set_forward_lateral_rad(float &euler_pitch_angle_rad, float &euler_roll_angle_rad)83{84// pitch/forward85if (forward_enable) {86_motors.set_forward(-sinf(euler_pitch_angle_rad));87euler_pitch_angle_rad = radians(pitch_offset_deg);88} else {89_motors.set_forward(0.0f);90euler_pitch_angle_rad += radians(pitch_offset_deg);91}92euler_pitch_angle_rad = wrap_PI(euler_pitch_angle_rad);9394// roll/lateral95if (lateral_enable) {96_motors.set_lateral(sinf(euler_roll_angle_rad));97euler_roll_angle_rad = radians(roll_offset_deg);98} else {99_motors.set_lateral(0.0f);100euler_roll_angle_rad += radians(roll_offset_deg);101}102euler_roll_angle_rad = wrap_PI(euler_roll_angle_rad);103}104105/*106all other input functions should zero thrust vectoring107*/108109// Command euler yaw rate and pitch angle with roll angle specified in body frame110// (used only by tailsitter quadplanes)111void AC_AttitudeControl_Multi_6DoF::input_euler_rate_yaw_euler_angle_pitch_bf_roll_rad(bool plane_controls, float euler_roll_angle_rad, float euler_pitch_angle_rad, float euler_yaw_rate_rads) {112_motors.set_lateral(0.0f);113_motors.set_forward(0.0f);114115AC_AttitudeControl_Multi::input_euler_rate_yaw_euler_angle_pitch_bf_roll_rad(plane_controls, euler_roll_angle_rad, euler_pitch_angle_rad, euler_yaw_rate_rads);116}117118// Sets desired roll, pitch, and yaw angular rates (in radians/s).119// This command is used to apply angular rate targets in the earth frame.120// The inputs are shaped using acceleration limits and time constants.121// Resulting targets are converted into body-frame angular velocities122// and passed to the rate controller.123void AC_AttitudeControl_Multi_6DoF::input_euler_rate_roll_pitch_yaw_rads(float euler_roll_rate_rads, float euler_pitch_rate_rads, float euler_yaw_rate_rads) {124_motors.set_lateral(0.0f);125_motors.set_forward(0.0f);126127AC_AttitudeControl_Multi::input_euler_rate_roll_pitch_yaw_rads(euler_roll_rate_rads, euler_pitch_rate_rads, euler_yaw_rate_rads);128}129130131// Sets desired roll, pitch, and yaw angular rates in body-frame (in radians/s).132// This command is used by fully stabilized acro modes.133// It applies angular velocity targets in the body frame,134// shaped using acceleration limits and passed to the rate controller.135void AC_AttitudeControl_Multi_6DoF::input_rate_bf_roll_pitch_yaw_rads(float roll_rate_bf_rads, float pitch_rate_bf_rads, float yaw_rate_bf_rads) {136_motors.set_lateral(0.0f);137_motors.set_forward(0.0f);138139AC_AttitudeControl_Multi::input_rate_bf_roll_pitch_yaw_rads(roll_rate_bf_rads, pitch_rate_bf_rads, yaw_rate_bf_rads);140}141142// Sets desired roll, pitch, and yaw angular rates in body-frame (in radians/s).143// Used by Copter's rate-only acro mode.144// Applies raw angular velocity targets directly to the rate controller with smoothing145// and no attitude feedback or stabilization.146void AC_AttitudeControl_Multi_6DoF::input_rate_bf_roll_pitch_yaw_2_rads(float roll_rate_bf_rads, float pitch_rate_bf_rads, float yaw_rate_bf_rads) {147_motors.set_lateral(0.0f);148_motors.set_forward(0.0f);149150AC_AttitudeControl_Multi::input_rate_bf_roll_pitch_yaw_2_rads(roll_rate_bf_rads, pitch_rate_bf_rads, yaw_rate_bf_rads);151}152153// Sets desired roll, pitch, and yaw angular rates in body-frame (in radians/s).154// Used by Plane's acro mode with rate error integration.155// Integrates attitude error over time to generate target angular rates.156void AC_AttitudeControl_Multi_6DoF::input_rate_bf_roll_pitch_yaw_3_rads(float roll_rate_bf_rads, float pitch_rate_bf_rads, float yaw_rate_bf_rads) {157_motors.set_lateral(0.0f);158_motors.set_forward(0.0f);159160AC_AttitudeControl_Multi::input_rate_bf_roll_pitch_yaw_3_rads(roll_rate_bf_rads, pitch_rate_bf_rads, yaw_rate_bf_rads);161}162163// Applies a one-time angular offset in body-frame roll/pitch/yaw angles (in radians).164// Used for initiating step responses during autotuning or manual test inputs.165void AC_AttitudeControl_Multi_6DoF::input_angle_step_bf_roll_pitch_yaw_rad(float roll_angle_step_bf_rad, float pitch_angle_step_bf_rad, float yaw_angle_step_bf_rad) {166_motors.set_lateral(0.0f);167_motors.set_forward(0.0f);168169AC_AttitudeControl_Multi::input_angle_step_bf_roll_pitch_yaw_rad(roll_angle_step_bf_rad, pitch_angle_step_bf_rad, yaw_angle_step_bf_rad);170}171172// Sets a desired attitude using a quaternion and body-frame angular velocity (rad/s).173// The desired quaternion is incrementally updated each timestep. Angular velocity is shaped by acceleration limits and feedforward.174void AC_AttitudeControl_Multi_6DoF::input_quaternion(Quaternion& attitude_desired_quat, Vector3f ang_vel_body) {175#if CONFIG_HAL_BOARD == HAL_BOARD_SITL176AP_HAL::panic("input_quaternion not implemented AC_AttitudeControl_Multi_6DoF");177#endif178179_motors.set_lateral(0.0f);180_motors.set_forward(0.0f);181182AC_AttitudeControl_Multi::input_quaternion(attitude_desired_quat, ang_vel_body);183}184185186AC_AttitudeControl_Multi_6DoF *AC_AttitudeControl_Multi_6DoF::_singleton = nullptr;187188#endif // AP_SCRIPTING_ENABLED189190191