123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- #include "Sub.h"
- void Sub::get_pilot_desired_lean_angles(float roll_in, float pitch_in, float &roll_out, float &pitch_out, float angle_max)
- {
-
- aparm.angle_max = constrain_int16(aparm.angle_max,1000,8000);
-
- angle_max = constrain_float(angle_max, 1000, aparm.angle_max);
-
- float scaler = aparm.angle_max/(float)ROLL_PITCH_INPUT_MAX;
- roll_in *= scaler;
- pitch_in *= scaler;
-
- float total_in = norm(pitch_in, roll_in);
- if (total_in > angle_max) {
- float ratio = angle_max / total_in;
- roll_in *= ratio;
- pitch_in *= ratio;
- }
-
- roll_in = (18000/M_PI) * atanf(cosf(pitch_in*(M_PI/18000))*tanf(roll_in*(M_PI/18000)));
-
- roll_out = roll_in;
- pitch_out = pitch_in;
- }
- float Sub::get_pilot_desired_yaw_rate(int16_t stick_angle)
- {
-
- return stick_angle * g.acro_yaw_p;
- }
- void Sub::check_ekf_yaw_reset()
- {
- float yaw_angle_change_rad;
- uint32_t new_ekfYawReset_ms = ahrs.getLastYawResetAngle(yaw_angle_change_rad);
- if (new_ekfYawReset_ms != ekfYawReset_ms) {
- attitude_control.inertial_frame_reset();
- ekfYawReset_ms = new_ekfYawReset_ms;
- }
- }
- float Sub::get_roi_yaw()
- {
- static uint8_t roi_yaw_counter = 0;
- roi_yaw_counter++;
- if (roi_yaw_counter >= 4) {
- roi_yaw_counter = 0;
- yaw_look_at_WP_bearing = get_bearing_cd(inertial_nav.get_position(), roi_WP);
- }
- return yaw_look_at_WP_bearing;
- }
- float Sub::get_look_ahead_yaw()
- {
- const Vector3f& vel = inertial_nav.get_velocity();
- float speed = norm(vel.x,vel.y);
-
- if (position_ok() && (speed > YAW_LOOK_AHEAD_MIN_SPEED)) {
- yaw_look_ahead_bearing = degrees(atan2f(vel.y,vel.x))*100.0f;
- }
- return yaw_look_ahead_bearing;
- }
- float Sub::get_pilot_desired_climb_rate(float throttle_control)
- {
-
- if (failsafe.pilot_input) {
- return 0.0f;
- }
- float desired_rate = 0.0f;
- float mid_stick = channel_throttle->get_control_mid();
- float deadband_top = mid_stick + g.throttle_deadzone;
- float deadband_bottom = mid_stick - g.throttle_deadzone;
-
- throttle_control = constrain_float(throttle_control,0.0f,1000.0f);
-
- g.throttle_deadzone = constrain_int16(g.throttle_deadzone, 0, 400);
-
- if (throttle_control < deadband_bottom) {
-
- desired_rate = get_pilot_speed_dn() * (throttle_control-deadband_bottom) / deadband_bottom;
- } else if (throttle_control > deadband_top) {
-
- desired_rate = g.pilot_speed_up * (throttle_control-deadband_top) / (1000.0f-deadband_top);
- } else {
-
- desired_rate = 0.0f;
- }
-
- desired_climb_rate = desired_rate;
- return desired_rate;
- }
- float Sub::get_surface_tracking_climb_rate(int16_t target_rate, float current_alt_target, float dt)
- {
- #if RANGEFINDER_ENABLED == ENABLED
- static uint32_t last_call_ms = 0;
- float distance_error;
- float velocity_correction;
- float current_alt = inertial_nav.get_altitude();
- uint32_t now = AP_HAL::millis();
-
- if (now - last_call_ms > RANGEFINDER_TIMEOUT_MS) {
- target_rangefinder_alt = rangefinder_state.alt_cm + current_alt_target - current_alt;
- }
- last_call_ms = now;
-
- if ((target_rate<0 && !motors.limit.throttle_lower) || (target_rate>0 && !motors.limit.throttle_upper)) {
- target_rangefinder_alt += target_rate * dt;
- }
-
-
- target_rangefinder_alt = constrain_float(target_rangefinder_alt,rangefinder_state.alt_cm-pos_control.get_leash_down_z(),rangefinder_state.alt_cm+pos_control.get_leash_up_z());
-
- distance_error = (target_rangefinder_alt - rangefinder_state.alt_cm) - (current_alt_target - current_alt);
- velocity_correction = distance_error * g.rangefinder_gain;
- velocity_correction = constrain_float(velocity_correction, -THR_SURFACE_TRACKING_VELZ_MAX, THR_SURFACE_TRACKING_VELZ_MAX);
-
- return (target_rate + velocity_correction);
- #else
- return (float)target_rate;
- #endif
- }
- void Sub::update_poscon_alt_max()
- {
-
-
- float min_alt_cm = 0.0;
-
-
- float max_alt_cm = g.surface_depth;
- #if AC_FENCE == ENABLED
-
- if ((fence.get_enabled_fences() & AC_FENCE_TYPE_ALT_MAX) != 0) {
- min_alt_cm = fence.get_safe_alt_min()*100.0f;
- max_alt_cm = fence.get_safe_alt_max()*100.0f;
- }
- #endif
-
- pos_control.set_alt_min(min_alt_cm);
- pos_control.set_alt_max(max_alt_cm);
- }
- void Sub::rotate_body_frame_to_NE(float &x, float &y)
- {
- float ne_x = x*ahrs.cos_yaw() - y*ahrs.sin_yaw();
- float ne_y = x*ahrs.sin_yaw() + y*ahrs.cos_yaw();
- x = ne_x;
- y = ne_y;
- }
- uint16_t Sub::get_pilot_speed_dn()
- {
- if (g.pilot_speed_dn == 0) {
- return abs(g.pilot_speed_up);
- }
- return abs(g.pilot_speed_dn);
- }
|