Eigen
Dependencies: Eigen
Dependents: optWingforHAPS_Eigen
Autopilot.cpp
- Committer:
- NaotoMorita
- Date:
- 2021-11-29
- Revision:
- 11:51a0fedd7745
- Parent:
- 10:c8b1ab9841c9
- Child:
- 12:59e547742cd8
File content as of revision 11:51a0fedd7745:
#include "Autopilot.hpp" Autopilot::Autopilot() { } void Autopilot::set_dest(float x, float y) { this->destination.x = x; this->destination.y = y; } void Autopilot::set_turn(float x, float y, float r) { this->turn_center.x = x; this->turn_center.y = y; this->turn_r = r; } void Autopilot::set_alt(float alt, float vel) { this->alt_obj = alt; this->vel_obj = vel; } void Autopilot::update_val(const Vector3 rpy, const float altitude, const Vector3 pos, const Vector3 vel) { this->roll = rpy.x; this->pitch = rpy.y; this->yaw = rpy.z; this->alt = altitude; this->pos_ned = pos; this->vel_ned = vel; } void Autopilot::level() { roll_obj = 0.0f; } void Autopilot::guide() { float x_dist = destination.x - pos_ned.x; float y_dist = destination.y - pos_ned.y; yaw_obj = atan2f(y_dist, x_dist); //rad float yaw_diff = atan_angdiff(yaw_obj,yaw); roll_obj = p_control(yaw_diff, 0.3f); } void Autopilot::turn() { float x_dist = turn_center.x - pos_ned.x; float y_dist = turn_center.y - pos_ned.y; float yaw_center = atan2f(y_dist, x_dist); float yaw_diff = atan_angdiff(yaw_center,yaw); //旋回中心までの方位角, rad float dist = sqrt(x_dist*x_dist + y_dist*y_dist); //旋回中心までの距離 //旋回円から遠い場合は旋回中心まで誘導 if (dist > 2*turn_r) { set_dest(turn_center.x, turn_center.y); guide(); } else { roll_obj = 0.35f; //基準 float yaw_obj = 0.5f*M_PI+0.05f*(turn_r - dist); float coasediff = (atan_angdiff(yaw_obj,yaw_diff)); roll_obj -= 0.4f * coasediff; //旋回中心方向に対する角度を90[deg]にする } } void Autopilot::keep_alt() { //TECS使用(https://docs.px4.io/master/en/flight_stack/controller_diagrams.html), p制御のみ float v2 = vel_ned.x*vel_ned.x + vel_ned.y*vel_ned.y + vel_ned.z*vel_ned.z; float E = alt + v2 / (2*G); float Esp = alt_obj + vel_obj*vel_obj / (2*G); float dT_cruise = 0.0f; dT_obj = dT_cruise; dT_obj += p_control(Esp - E, 0.1f); float B = alt - v2 / (2*G); float Bsp = alt_obj - vel_obj*vel_obj / (2*G); pitch_obj = p_control(Bsp - B, 0.05f); } void Autopilot::return_val(float &r_obj, float &p_obj, float &t_obj) { limit_obj(); r_obj = this->roll_obj; p_obj = this->pitch_obj; t_obj = this->dT_obj; } void Autopilot::limit_obj() { //roll目標値を±30[deg]に制限 if (roll_obj > deg2rad(30.0f)) roll_obj = deg2rad(30.0f); else if (roll_obj < deg2rad(-30.0f)) roll_obj = deg2rad(-30.0f); //pitch目標値を±10[deg]に制限 if (pitch_obj > deg2rad(10.0f)) pitch_obj = deg2rad(10.0f); else if (pitch_obj < deg2rad(-10.0f)) pitch_obj = deg2rad(-10.0f); //dT目標値を0~1に制限 if (dT_obj > 1.0f) dT_obj = 1.0f; else if (dT_obj < -1.0f) dT_obj = -1.0f; } float Autopilot::p_control(float diff, float kp) { return kp * diff; } float Autopilot::angdiff_pi(float rad) { if (rad > M_PI) return rad - 2*M_PI; else if (rad < -M_PI) return rad + 2*M_PI; else return rad; } float Autopilot::atan_angdiff(float a1,float a2) { return atan2f(sinf(a1-a2),cosf(a1-a2)); } float Autopilot::deg2rad(float deg) { return deg * M_PI / 180.0f; }