Advertisement
Guest User

mazecode

a guest
Nov 15th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.53 KB | None | 0 0
  1. #include <iostream>
  2. #include "Copter.h"
  3.  
  4. using namespace std;
  5.  
  6. /*
  7. * Init and run calls for autonomous flight mode (largely based off of the AltHold flight mode)
  8. */
  9.  
  10. // autonomous_init - initialise autonomous controller
  11. bool Copter::autonomous_init(bool ignore_checks)
  12. {
  13. // initialize vertical speeds and leash lengths
  14. pos_control->set_speed_z(-g.pilot_velocity_z_max, g.pilot_velocity_z_max);
  15. pos_control->set_accel_z(g.pilot_accel_z);
  16.  
  17. // initialise position and desired velocity
  18. if (!pos_control->is_active_z()) {
  19. pos_control->set_alt_target_to_current_alt();
  20. pos_control->set_desired_velocity_z(inertial_nav.get_velocity_z());
  21. }
  22.  
  23. // stop takeoff if running
  24. takeoff_stop();
  25.  
  26. // reset integrators for roll and pitch controllers
  27. g.pid_roll.reset_I();
  28. g.pid_pitch.reset_I();
  29.  
  30. return true;
  31. }
  32.  
  33. // autonomous_run - runs the autonomous controller
  34. // should be called at 100hz or more
  35. void Copter::autonomous_run()
  36. {
  37. AltHoldModeState althold_state;
  38. float takeoff_climb_rate = 0.0f;
  39.  
  40. // initialize vertical speeds and acceleration
  41. pos_control->set_speed_z(-g.pilot_velocity_z_max, g.pilot_velocity_z_max);
  42. pos_control->set_accel_z(g.pilot_accel_z);
  43.  
  44. // apply SIMPLE mode transform to pilot inputs
  45. update_simple_mode();
  46.  
  47. // desired roll, pitch, and yaw_rate
  48. float target_roll = 0.0f, target_pitch = 0.0f, target_yaw_rate = 0.0f;
  49.  
  50. // get pilot desired climb rate
  51. float target_climb_rate = get_pilot_desired_climb_rate(channel_throttle->get_control_in());
  52. target_climb_rate = constrain_float(target_climb_rate, -g.pilot_velocity_z_max, g.pilot_velocity_z_max);
  53.  
  54. #if FRAME_CONFIG == HELI_FRAME
  55. // helicopters are held on the ground until rotor speed runup has finished
  56. bool takeoff_triggered = (ap.land_complete && (target_climb_rate > 0.0f) && motors->rotor_runup_complete());
  57. #else
  58. bool takeoff_triggered = ap.land_complete && (target_climb_rate > 0.0f);
  59. #endif
  60. target_climb_rate = 0.0f;
  61.  
  62. // Alt Hold State Machine Determination
  63. if (!motors->armed() || !motors->get_interlock()) {
  64. althold_state = AltHold_MotorStopped;
  65. } else if (takeoff_state.running || takeoff_triggered) {
  66. althold_state = AltHold_Takeoff;
  67. } else if (!ap.auto_armed || ap.land_complete) {
  68. althold_state = AltHold_Landed;
  69. } else {
  70. althold_state = AltHold_Flying;
  71. }
  72.  
  73. // Alt Hold State Machine
  74. switch (althold_state) {
  75.  
  76. case AltHold_MotorStopped:
  77.  
  78. motors->set_desired_spool_state(AP_Motors::DESIRED_SHUT_DOWN);
  79. attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(target_roll, target_pitch, target_yaw_rate, get_smoothing_gain());
  80. attitude_control->reset_rate_controller_I_terms();
  81. attitude_control->set_yaw_target_to_current_heading();
  82. #if FRAME_CONFIG == HELI_FRAME
  83. // force descent rate and call position controller
  84. pos_control->set_alt_target_from_climb_rate(-abs(g.land_speed), G_Dt, false);
  85. heli_flags.init_targets_on_arming=true;
  86. #else
  87. pos_control->relax_alt_hold_controllers(0.0f); // forces throttle output to go to zero
  88. #endif
  89. pos_control->update_z_controller();
  90. break;
  91.  
  92. case AltHold_Takeoff:
  93. #if FRAME_CONFIG == HELI_FRAME
  94. if (heli_flags.init_targets_on_arming) {
  95. heli_flags.init_targets_on_arming=false;
  96. }
  97. #endif
  98. // set motors to full range
  99. motors->set_desired_spool_state(AP_Motors::DESIRED_THROTTLE_UNLIMITED);
  100.  
  101. // initiate take-off
  102. if (!takeoff_state.running) {
  103. takeoff_timer_start(constrain_float(g.pilot_takeoff_alt,0.0f,1000.0f));
  104. // indicate we are taking off
  105. set_land_complete(false);
  106. // clear i terms
  107. set_throttle_takeoff();
  108. }
  109.  
  110. // get take-off adjusted pilot and takeoff climb rates
  111. takeoff_get_climb_rates(target_climb_rate, takeoff_climb_rate);
  112.  
  113. // get avoidance adjusted climb rate
  114. target_climb_rate = get_avoidance_adjusted_climbrate(target_climb_rate);
  115.  
  116. // call attitude controller
  117. attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(target_roll, target_pitch, target_yaw_rate, get_smoothing_gain());
  118.  
  119. // call position controller
  120. pos_control->set_alt_target_from_climb_rate_ff(target_climb_rate, G_Dt, false);
  121. pos_control->add_takeoff_climb_rate(takeoff_climb_rate, G_Dt);
  122. pos_control->update_z_controller();
  123. break;
  124.  
  125. case AltHold_Landed:
  126. // set motors to spin-when-armed if throttle below deadzone, otherwise full range (but motors will only spin at min throttle)
  127. if (target_climb_rate < 0.0f) {
  128. motors->set_desired_spool_state(AP_Motors::DESIRED_SPIN_WHEN_ARMED);
  129. } else {
  130. motors->set_desired_spool_state(AP_Motors::DESIRED_THROTTLE_UNLIMITED);
  131. }
  132.  
  133. #if FRAME_CONFIG == HELI_FRAME
  134. if (heli_flags.init_targets_on_arming) {
  135. attitude_control->reset_rate_controller_I_terms();
  136. attitude_control->set_yaw_target_to_current_heading();
  137. if (motors->get_interlock()) {
  138. heli_flags.init_targets_on_arming=false;
  139. }
  140. }
  141. #else
  142. attitude_control->reset_rate_controller_I_terms();
  143. attitude_control->set_yaw_target_to_current_heading();
  144. #endif
  145. attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(target_roll, target_pitch, target_yaw_rate, get_smoothing_gain());
  146. pos_control->relax_alt_hold_controllers(0.0f); // forces throttle output to go to zero
  147. pos_control->update_z_controller();
  148. break;
  149.  
  150. case AltHold_Flying:
  151. // compute the target climb rate, roll, pitch and yaw rate
  152. // land if autonomous_controller returns false
  153. if (!autonomous_controller(target_climb_rate, target_roll, target_pitch, target_yaw_rate)) {
  154. // switch to land mode
  155. set_mode(LAND, MODE_REASON_MISSION_END);
  156. break;
  157. }
  158.  
  159. motors->set_desired_spool_state(AP_Motors::DESIRED_THROTTLE_UNLIMITED);
  160.  
  161. #if AC_AVOID_ENABLED == ENABLED
  162. // apply avoidance
  163. avoid.adjust_roll_pitch(target_roll, target_pitch, aparm.angle_max);
  164. #endif
  165.  
  166. // call attitude controller
  167. attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(target_roll, target_pitch, target_yaw_rate, get_smoothing_gain());
  168.  
  169. // adjust climb rate using rangefinder
  170. if (rangefinder_alt_ok()) {
  171. // if rangefinder is ok, use surface tracking
  172. target_climb_rate = get_surface_tracking_climb_rate(target_climb_rate, pos_control->get_alt_target(), G_Dt);
  173. }
  174.  
  175. // get avoidance adjusted climb rate
  176. target_climb_rate = get_avoidance_adjusted_climbrate(target_climb_rate);
  177.  
  178. // call position controller
  179. pos_control->set_alt_target_from_climb_rate_ff(target_climb_rate, G_Dt, false);
  180. pos_control->update_z_controller();
  181. break;
  182. }
  183. }
  184.  
  185. // stores last move (0 -> back, 1 -> right, 2 -> front, 3-> left)
  186. int lastMove = 0;
  187.  
  188. // autonomous_controller - computes target climb rate, roll, pitch, and yaw rate for autonomous flight mode
  189. // returns true to continue flying, and returns false to land
  190. bool Copter::autonomous_controller(float &target_climb_rate, float &target_roll, float &target_pitch, float &target_yaw_rate)
  191. {
  192. // get downward facing sensor reading in meters
  193. float rangefinder_alt = (float)rangefinder_state.alt_cm / 100.0f;
  194.  
  195. // get horizontal sensor readings in meters
  196. float dist_forward, dist_right, dist_backward, dist_left;
  197. g2.proximity.get_horizontal_distance(0, dist_forward);
  198. g2.proximity.get_horizontal_distance(90, dist_right);
  199. g2.proximity.get_horizontal_distance(180, dist_backward);
  200. g2.proximity.get_horizontal_distance(270, dist_left);
  201.  
  202. //stores all distances in array
  203. float dists[4] = {dist_backward, dist_right, dist_forward, dist_left};
  204.  
  205. // set desired climb rate in centimeters per second
  206. target_climb_rate = 0.0f;
  207.  
  208. int biggest (float dist_b, float dist_r, float dist_f, float dist_l)
  209. {
  210. float array[4] = {dist_b, dist_r, dist_f, dist_l};
  211. int temp = 0;
  212.  
  213. for(int i=0; i<3; i++)
  214. {
  215. if(array[i]>temp)
  216. {
  217. temp = i;
  218. }
  219. }
  220.  
  221. return temp;
  222. }
  223.  
  224. dists[lastMove] = 0.0f;
  225.  
  226. targetDir = biggest();
  227.  
  228. switch(targetDir)
  229. {
  230. case 0: //GO BACKWARDS
  231. g.pid_pitch.set_input_filter_all(5.0f - dist_backward);
  232. target_pitch = 100.0f * g.pid_pitch.get_pid();
  233. lastMove = 0;
  234. break;
  235.  
  236. case 1: //GO RIGHT
  237. g.pid_roll.set_input_filter_all(5.0f - dist_right);
  238. target_roll = -100.0f * g.pid_roll.get_pid();
  239. lastMove = 1;
  240. break;
  241.  
  242. case 2: //GO FORWARD
  243. g.pid_pitch.set_input_filter_all(5.0f - dist_forward);
  244. target_pitch = -100.0f * g.pid_pitch.get_pid();
  245. lastMove = 2;
  246. break;
  247.  
  248. case 3: //GO LEFT
  249. g.pid_roll.set_input_filter_all(5.0f - dist_left);
  250. target_roll = 100.0f * g.pid_roll.get_pid();
  251. lastMove = 3;
  252. break;
  253. }
  254.  
  255. // set desired yaw rate in centi-degrees per second (set to zero to hold constant heading)
  256. target_yaw_rate = 0.0f;
  257.  
  258. static int counter = 0;
  259. if (counter++>400) {
  260. gcs_send_text(MAV_SEVERITY_INFO, "Autonomous flight mode for Fukuoka");
  261. gcs_send_text_fmt(MAV_SEVERITY_INFO, "Target Pitch: %f", target_pitch);
  262. gcs_send_text_fmt(MAV_SEVERITY_INFO, "Target Roll: %f", target_roll);
  263. counter = 0;
  264. }
  265.  
  266. return true;
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement