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/ArduSub/Attitude.cpp
Views: 1798
#include "Sub.h"12// get_pilot_desired_angle - transform pilot's roll or pitch input into a desired lean angle3// returns desired angle in centi-degrees4void Sub::get_pilot_desired_lean_angles(float roll_in, float pitch_in, float &roll_out, float &pitch_out, float angle_max)5{6// sanity check angle max parameter7aparm.angle_max.set(constrain_int16(aparm.angle_max,1000,8000));89// limit max lean angle10angle_max = constrain_float(angle_max, 1000, aparm.angle_max);1112// scale roll_in, pitch_in to ANGLE_MAX parameter range13float scaler = aparm.angle_max/(float)ROLL_PITCH_INPUT_MAX;14roll_in *= scaler;15pitch_in *= scaler;1617// do circular limit18float total_in = norm(pitch_in, roll_in);19if (total_in > angle_max) {20float ratio = angle_max / total_in;21roll_in *= ratio;22pitch_in *= ratio;23}2425// do lateral tilt to euler roll conversion26roll_in = (18000/M_PI) * atanf(cosf(pitch_in*(M_PI/18000))*tanf(roll_in*(M_PI/18000)));2728// return29roll_out = roll_in;30pitch_out = pitch_in;31}3233// get_pilot_desired_heading - transform pilot's yaw input into a34// desired yaw rate35// returns desired yaw rate in centi-degrees per second36float Sub::get_pilot_desired_yaw_rate(int16_t stick_angle) const37{38// convert pilot input to the desired yaw rate39return stick_angle * g.acro_yaw_p;40}4142// check for ekf yaw reset and adjust target heading43void Sub::check_ekf_yaw_reset()44{45float yaw_angle_change_rad;46uint32_t new_ekfYawReset_ms = ahrs.getLastYawResetAngle(yaw_angle_change_rad);47if (new_ekfYawReset_ms != ekfYawReset_ms) {48attitude_control.inertial_frame_reset();49ekfYawReset_ms = new_ekfYawReset_ms;50}51}5253/*************************************************************54* yaw controllers55*************************************************************/5657// get_roi_yaw - returns heading towards location held in roi_WP58// should be called at 100hz59float Sub::get_roi_yaw()60{61static uint8_t roi_yaw_counter = 0; // used to reduce update rate to 100hz6263roi_yaw_counter++;64if (roi_yaw_counter >= 4) {65roi_yaw_counter = 0;66yaw_look_at_WP_bearing = get_bearing_cd(inertial_nav.get_position_xy_cm(), roi_WP.xy());67}6869return yaw_look_at_WP_bearing;70}7172float Sub::get_look_ahead_yaw()73{74const Vector3f& vel = inertial_nav.get_velocity_neu_cms();75const float speed_sq = vel.xy().length_squared();76// Commanded Yaw to automatically look ahead.77if (position_ok() && (speed_sq > (YAW_LOOK_AHEAD_MIN_SPEED * YAW_LOOK_AHEAD_MIN_SPEED))) {78yaw_look_ahead_bearing = degrees(atan2f(vel.y,vel.x))*100.0f;79}80return yaw_look_ahead_bearing;81}8283/*************************************************************84* throttle control85****************************************************************/8687// get_pilot_desired_climb_rate - transform pilot's throttle input to climb rate in cm/s88// without any deadzone at the bottom89float Sub::get_pilot_desired_climb_rate(float throttle_control)90{91// throttle failsafe check92if (failsafe.pilot_input) {93return 0.0f;94}9596float desired_rate = 0.0f;97float mid_stick = channel_throttle->get_control_mid();98float deadband_top = mid_stick + g.throttle_deadzone * gain;99float deadband_bottom = mid_stick - g.throttle_deadzone * gain;100101// ensure a reasonable throttle value102throttle_control = constrain_float(throttle_control,0.0f,1000.0f);103104// ensure a reasonable deadzone105g.throttle_deadzone.set(constrain_int16(g.throttle_deadzone, 0, 400));106107// check throttle is above, below or in the deadband108if (throttle_control < deadband_bottom) {109// below the deadband110desired_rate = get_pilot_speed_dn() * (throttle_control-deadband_bottom) / deadband_bottom;111} else if (throttle_control > deadband_top) {112// above the deadband113desired_rate = g.pilot_speed_up * (throttle_control-deadband_top) / (1000.0f-deadband_top);114} else {115// must be in the deadband116desired_rate = 0.0f;117}118119// desired climb rate for logging120desired_climb_rate = desired_rate;121122return desired_rate;123}124125// rotate vector from vehicle's perspective to North-East frame126void Sub::rotate_body_frame_to_NE(float &x, float &y)127{128float ne_x = x*ahrs.cos_yaw() - y*ahrs.sin_yaw();129float ne_y = x*ahrs.sin_yaw() + y*ahrs.cos_yaw();130x = ne_x;131y = ne_y;132}133134// It will return the PILOT_SPEED_DN value if non zero, otherwise if zero it returns the PILOT_SPEED_UP value.135uint16_t Sub::get_pilot_speed_dn() const136{137if (g.pilot_speed_dn == 0) {138return abs(g.pilot_speed_up);139}140return abs(g.pilot_speed_dn);141}142143144