Advertisement
Guest User

Untitled

a guest
Jul 30th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.49 KB | None | 0 0
  1. /// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
  2. #include "Sub.h"
  3.  
  4. /*
  5.  * control_althold.pde - init and run calls for althold, flight mode
  6.  */
  7.  
  8. // althold_init - initialise althold controller
  9. bool Sub::althold_init(bool ignore_checks)
  10. {
  11.     // initialize vertical speeds and leash lengths
  12.     // sets the maximum speed up and down returned by position controller
  13.     pos_control.set_speed_z(-g.pilot_velocity_z_max, g.pilot_velocity_z_max);
  14.     pos_control.set_accel_z(g.pilot_accel_z);
  15.  
  16.     // initialise position and desired velocity
  17.     pos_control.set_alt_target(inertial_nav.get_altitude());
  18.     pos_control.set_desired_velocity_z(inertial_nav.get_velocity_z());
  19.  
  20.     // stop takeoff if running
  21.     takeoff_stop();
  22.  
  23.     return true;
  24. }
  25.  
  26. // althold_run - runs the althold controller
  27. // should be called at 100hz or more
  28. void Sub::althold_run()
  29. {
  30.     AltHoldModeState althold_state;
  31.     float takeoff_climb_rate = 0.0f;
  32.  
  33.     // initialize vertical speeds and acceleration
  34.     pos_control.set_speed_z(-g.pilot_velocity_z_max, g.pilot_velocity_z_max);
  35.     pos_control.set_accel_z(g.pilot_accel_z);
  36.  
  37.     // apply SIMPLE mode transform to pilot inputs
  38.     update_simple_mode();
  39.  
  40.     // get pilot desired lean angles
  41.     float target_roll, target_pitch;
  42.     get_pilot_desired_lean_angles(channel_roll->get_control_in(), channel_pitch->get_control_in(), target_roll, target_pitch, attitude_control.get_althold_lean_angle_max());
  43.  
  44.     // get pilot's desired yaw rate
  45.     float target_yaw_rate = get_pilot_desired_yaw_rate(channel_yaw->get_control_in());
  46.  
  47.     // get pilot desired climb rate
  48.     float target_climb_rate = get_pilot_desired_climb_rate(channel_throttle->get_control_in());
  49.     target_climb_rate = constrain_float(target_climb_rate, -g.pilot_velocity_z_max, g.pilot_velocity_z_max);
  50.  
  51.     //bool takeoff_triggered = (ap.land_complete && (channel_throttle->get_control_in() > get_takeoff_trigger_throttle()) && motors.spool_up_complete());
  52.  
  53. //    // Alt Hold State Machine Determination
  54.     if(!ap.auto_armed) {
  55.         althold_state = AltHold_NotAutoArmed;
  56.     } else {
  57.         althold_state = AltHold_Flying;
  58.     }
  59.  
  60.     // Alt Hold State Machine
  61.     switch (althold_state) {
  62.  
  63.     case AltHold_MotorStopped:
  64.         // Multicopter do not stabilize roll/pitch/yaw when motor are stopped
  65.         motors.set_desired_spool_state(AP_Motors::DESIRED_SHUT_DOWN);
  66.         attitude_control.set_throttle_out_unstabilized(0,true,g.throttle_filt);
  67.         pos_control.relax_alt_hold_controllers(get_throttle_pre_takeoff(channel_throttle->get_control_in())-motors.get_throttle_hover());
  68.         break;
  69.  
  70.     case AltHold_NotAutoArmed:
  71.         motors.set_desired_spool_state(AP_Motors::DESIRED_SPIN_WHEN_ARMED);
  72.         // Multicopters do not stabilize roll/pitch/yaw when not auto-armed (i.e. on the ground, pilot has never raised throttle)
  73.         attitude_control.set_throttle_out_unstabilized(0,true,g.throttle_filt);
  74.         pos_control.relax_alt_hold_controllers(get_throttle_pre_takeoff(channel_throttle->get_control_in())-motors.get_throttle_hover());
  75.         break;
  76.  
  77.     case AltHold_Takeoff:
  78.  
  79.         // initiate take-off
  80.         if (!takeoff_state.running) {
  81.             takeoff_timer_start(constrain_float(g.pilot_takeoff_alt,0.0f,1000.0f));
  82.             // indicate we are taking off
  83.             set_land_complete(false);
  84.             // clear i terms
  85.             set_throttle_takeoff();
  86.         }
  87.  
  88.         // get take-off adjusted pilot and takeoff climb rates
  89.         takeoff_get_climb_rates(target_climb_rate, takeoff_climb_rate);
  90.  
  91.         // set motors to full range
  92.         motors.set_desired_spool_state(AP_Motors::DESIRED_THROTTLE_UNLIMITED);
  93.  
  94.         // call attitude controller
  95.         attitude_control.input_euler_angle_roll_pitch_euler_rate_yaw(atthold_desired_roll, atthold_desired_pitch, target_yaw_rate, get_smoothing_gain());
  96.  
  97.         // call position controller
  98.         pos_control.set_alt_target_from_climb_rate_ff(target_climb_rate, G_Dt, false);
  99.         pos_control.add_takeoff_climb_rate(takeoff_climb_rate, G_Dt);
  100.         pos_control.update_z_controller();
  101.         break;
  102.  
  103.     case AltHold_Landed:
  104.         // Multicopter do not stabilize roll/pitch/yaw when disarmed
  105.         attitude_control.set_throttle_out(get_throttle_pre_takeoff(channel_throttle->get_control_in()),false,g.throttle_filt);
  106.         // if throttle zero reset attitude and exit immediately
  107.         if (ap.throttle_zero) {
  108.             motors.set_desired_spool_state(AP_Motors::DESIRED_SPIN_WHEN_ARMED);
  109.         } else {
  110.             motors.set_desired_spool_state(AP_Motors::DESIRED_THROTTLE_UNLIMITED);
  111.         }
  112.  
  113.         pos_control.relax_alt_hold_controllers(get_throttle_pre_takeoff(channel_throttle->get_control_in())-motors.get_throttle_hover());
  114.         break;
  115.  
  116.     case AltHold_Flying:
  117.         motors.set_desired_spool_state(AP_Motors::DESIRED_THROTTLE_UNLIMITED);
  118.         // call attitude controller
  119.         attitude_control.input_euler_angle_roll_pitch_euler_rate_yaw(atthold_desired_roll, atthold_desired_pitch, target_yaw_rate, get_smoothing_gain());
  120.  
  121.         // adjust climb rate using rangefinder
  122.         if (rangefinder_alt_ok()) {
  123.             // if rangefinder is ok, use surface tracking
  124.             target_climb_rate = get_surface_tracking_climb_rate(target_climb_rate, pos_control.get_alt_target(), G_Dt);
  125.         }
  126.  
  127.         // call position controller
  128.         if(ap.at_bottom) {
  129.             pos_control.relax_alt_hold_controllers(0.0); // clear velocity and position targets, and integrator
  130.             pos_control.set_alt_target(inertial_nav.get_altitude() + 10.0f); // set target to 10 cm above bottom
  131.         } else if(ap.at_surface) {
  132.             if(target_climb_rate < 0.0) { // Dive if the pilot wants to
  133.                 pos_control.set_alt_target_from_climb_rate_ff(target_climb_rate, G_Dt, false);
  134.             } else if(pos_control.get_vel_target_z() > 0.0) {
  135.                 pos_control.relax_alt_hold_controllers(0.0); // clear velocity and position targets, and integrator
  136.                 pos_control.set_alt_target(g.surface_depth); // set alt target to the same depth that triggers the surface detector.
  137.             }
  138.         } else {
  139.             pos_control.set_alt_target_from_climb_rate_ff(target_climb_rate, G_Dt, false);
  140.         }
  141.  
  142.         pos_control.update_z_controller();
  143.         break;
  144.     }
  145.  
  146.     //control_in is range 0-1000
  147.     //radio_in is raw pwm value
  148.     motors.set_forward(channel_forward->norm_input());
  149.     motors.set_lateral(channel_lateral->norm_input());
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement