ArduSub.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. // ArduSub scheduling, originally copied from ArduCopter
  14. #include "Sub.h"
  15. #define SCHED_TASK(func, rate_hz, max_time_micros) SCHED_TASK_CLASS(Sub, &sub, func, rate_hz, max_time_micros)
  16. /*
  17. scheduler table for fast CPUs - all regular tasks apart from the fast_loop()
  18. should be listed here, along with how often they should be called (in hz)
  19. and the maximum time they are expected to take (in microseconds)
  20. */
  21. const AP_Scheduler::Task Sub::scheduler_tasks[] = {
  22. SCHED_TASK(fifty_hz_loop, 50, 75),
  23. SCHED_TASK(update_GPS, 50, 200),
  24. #if OPTFLOW == ENABLED
  25. SCHED_TASK_CLASS(OpticalFlow, &sub.optflow, update, 200, 160),
  26. #endif
  27. SCHED_TASK(update_batt_compass, 10, 120),
  28. SCHED_TASK(read_rangefinder, 20, 100),
  29. SCHED_TASK(update_altitude, 10, 100),
  30. SCHED_TASK(three_hz_loop, 3, 75),
  31. SCHED_TASK(update_turn_counter, 10, 50),
  32. SCHED_TASK_CLASS(AP_Baro, &sub.barometer, accumulate, 50, 90),
  33. SCHED_TASK_CLASS(AP_Notify, &sub.notify, update, 50, 90),
  34. SCHED_TASK(one_hz_loop, 1, 100),
  35. SCHED_TASK_CLASS(GCS, (GCS*)&sub._gcs, update_receive, 400, 180),
  36. SCHED_TASK_CLASS(GCS, (GCS*)&sub._gcs, update_send, 200, 800),
  37. #if MOUNT == ENABLED
  38. SCHED_TASK_CLASS(AP_Mount, &sub.camera_mount, update, 50, 75),
  39. #endif
  40. #if CAMERA == ENABLED
  41. SCHED_TASK_CLASS(AP_Camera, &sub.camera, update_trigger, 50, 75),
  42. #endif
  43. SCHED_TASK(ten_hz_logging_loop, 10, 350),
  44. SCHED_TASK(twentyfive_hz_logging, 25, 110),
  45. SCHED_TASK_CLASS(AP_Logger, &sub.logger, periodic_tasks, 400, 300),
  46. SCHED_TASK_CLASS(AP_InertialSensor, &sub.ins, periodic, 400, 50),
  47. SCHED_TASK_CLASS(AP_Scheduler, &sub.scheduler, update_logging, 0.1, 75),
  48. #if RPM_ENABLED == ENABLED
  49. SCHED_TASK(rpm_update, 10, 200),
  50. #endif
  51. SCHED_TASK_CLASS(Compass, &sub.compass, cal_update, 100, 100),
  52. SCHED_TASK(accel_cal_update, 10, 100),
  53. SCHED_TASK(terrain_update, 10, 100),
  54. #if GRIPPER_ENABLED == ENABLED
  55. SCHED_TASK_CLASS(AP_Gripper, &sub.g2.gripper, update, 10, 75),
  56. #endif
  57. #ifdef USERHOOK_FASTLOOP
  58. SCHED_TASK(userhook_FastLoop, 100, 75),
  59. #endif
  60. #ifdef USERHOOK_50HZLOOP
  61. SCHED_TASK(userhook_50Hz, 50, 75),
  62. #endif
  63. #ifdef USERHOOK_MEDIUMLOOP
  64. SCHED_TASK(userhook_MediumLoop, 15, 75),
  65. #endif
  66. #ifdef USERHOOK_SLOWLOOP
  67. SCHED_TASK(userhook_SlowLoop, 3.3, 75),
  68. #endif
  69. #ifdef USERHOOK_SUPERSLOWLOOP
  70. SCHED_TASK(userhook_SuperSlowLoop, 1, 75),
  71. #endif
  72. };
  73. constexpr int8_t Sub::_failsafe_priorities[5];
  74. void Sub::setup()
  75. {
  76. // Load the default values of variables listed in var_info[]s
  77. AP_Param::setup_sketch_defaults();
  78. init_ardupilot();
  79. chan_adc= hal.analogin->channel(0);
  80. // initialise the main loop scheduler
  81. scheduler.init(&scheduler_tasks[0], ARRAY_SIZE(scheduler_tasks), MASK_LOG_PM);
  82. }
  83. void Sub::loop()
  84. {
  85. scheduler.loop();
  86. G_Dt = scheduler.get_loop_period_s();
  87. }
  88. // Main loop - 400hz
  89. void Sub::fast_loop()
  90. {
  91. // update INS immediately to get current gyro data populated
  92. ins.update();
  93. //don't run rate controller in manual or motordetection modes
  94. if (control_mode != MANUAL && control_mode != MOTOR_DETECT&& control_mode != CLEAN) {
  95. // run low level rate controllers that only require IMU data
  96. attitude_control.rate_controller_run();
  97. }
  98. // send outputs to the motors library
  99. motors_output();
  100. // run EKF state estimator (expensive)
  101. // --------------------
  102. read_AHRS();
  103. // Inertial Nav
  104. // --------------------
  105. read_inertia();
  106. // check if ekf has reset target heading
  107. check_ekf_yaw_reset();
  108. // run the attitude controllers
  109. update_flight_mode();
  110. //motor_toCan();
  111. // update home from EKF if necessary
  112. update_home_from_EKF();
  113. // check if we've reached the surface or bottom
  114. update_surface_and_bottom_detector();
  115. #if MOUNT == ENABLED
  116. // camera mount's fast update
  117. camera_mount.update_fast();
  118. #endif
  119. // log sensor health
  120. if (should_log(MASK_LOG_ANY)) {
  121. Log_Sensor_Health();
  122. }
  123. }
  124. // 50 Hz tasks
  125. void Sub::fifty_hz_loop()
  126. {
  127. // check pilot input failsafe
  128. failsafe_pilot_input_check();
  129. failsafe_crash_check();
  130. failsafe_ekf_check();
  131. failsafe_sensors_check();
  132. // Update rc input/output
  133. rc().read_input();
  134. SRV_Channels::output_ch_all();
  135. }
  136. // update_batt_compass - read battery and compass
  137. // should be called at 10hz
  138. void Sub::update_batt_compass()
  139. {
  140. // read battery before compass because it may be used for motor interference compensation
  141. battery.read();
  142. if (AP::compass().enabled()) {
  143. // update compass with throttle value - used for compassmot
  144. compass.set_throttle(motors.get_throttle());
  145. compass.read();
  146. }
  147. }
  148. // ten_hz_logging_loop
  149. // should be run at 10hz
  150. void Sub::ten_hz_logging_loop()
  151. {
  152. // log attitude data if we're not already logging at the higher rate
  153. if (should_log(MASK_LOG_ATTITUDE_MED) && !should_log(MASK_LOG_ATTITUDE_FAST)) {
  154. Log_Write_Attitude();
  155. logger.Write_Rate(&ahrs_view, motors, attitude_control, pos_control);
  156. if (should_log(MASK_LOG_PID)){
  157. /*attitude_control.get_rate_roll_pid().get_pid_info().P = channel_forward->norm_input();
  158. attitude_control.get_rate_roll_pid().get_pid_info().I = channel_lateral->norm_input();
  159. attitude_control.get_rate_roll_pid().get_pid_info().D = channel_yaw->norm_input();
  160. attitude_control.get_rate_roll_pid().get_pid_info().error = channel_throttle->norm_input();
  161. attitude_control.get_rate_roll_pid().get_pid_info().target = (float)PressLevel_f*0.1;*/
  162. logger.Write_PID(LOG_PIDR_MSG, attitude_control.get_rate_roll_pid().get_pid_info());
  163. logger.Write_PID(LOG_PIDP_MSG, attitude_control.get_rate_pitch_pid().get_pid_info());
  164. logger.Write_PID(LOG_PIDY_MSG, attitude_control.get_rate_yaw_pid().get_pid_info());
  165. logger.Write_PID(LOG_PIDA_MSG, pos_control.get_accel_z_pid().get_pid_info());
  166. }
  167. }
  168. if (should_log(MASK_LOG_MOTBATT)) {
  169. Log_Write_MotBatt();
  170. }
  171. if (should_log(MASK_LOG_RCIN)) {
  172. logger.Write_RCIN();
  173. }
  174. if (should_log(MASK_LOG_RCOUT)) {
  175. logger.Write_RCOUT();
  176. }
  177. if (should_log(MASK_LOG_NTUN) && mode_requires_GPS(control_mode)) {
  178. pos_control.write_log();
  179. }
  180. if (should_log(MASK_LOG_IMU) || should_log(MASK_LOG_IMU_FAST) || should_log(MASK_LOG_IMU_RAW)) {
  181. logger.Write_Vibration();
  182. }
  183. if (should_log(MASK_LOG_CTUN)) {
  184. attitude_control.control_monitor_log();
  185. }
  186. }
  187. // twentyfive_hz_logging_loop
  188. // should be run at 25hz
  189. void Sub::twentyfive_hz_logging()
  190. {
  191. if (should_log(MASK_LOG_ATTITUDE_FAST)) {
  192. Log_Write_Attitude();
  193. logger.Write_Rate(&ahrs_view, motors, attitude_control, pos_control);
  194. if (should_log(MASK_LOG_PID)) {
  195. logger.Write_PID(LOG_PIDR_MSG, attitude_control.get_rate_roll_pid().get_pid_info());
  196. logger.Write_PID(LOG_PIDP_MSG, attitude_control.get_rate_pitch_pid().get_pid_info());
  197. logger.Write_PID(LOG_PIDY_MSG, attitude_control.get_rate_yaw_pid().get_pid_info());
  198. logger.Write_PID(LOG_PIDA_MSG, pos_control.get_accel_z_pid().get_pid_info());
  199. }
  200. }
  201. // log IMU data if we're not already logging at the higher rate
  202. if (should_log(MASK_LOG_IMU) && !should_log(MASK_LOG_IMU_RAW)) {
  203. logger.Write_IMU();
  204. }
  205. }
  206. // three_hz_loop - 3.3hz loop
  207. void Sub::three_hz_loop()
  208. {
  209. leak_detector.update();
  210. failsafe_leak_check();
  211. failsafe_internal_pressure_check();
  212. failsafe_internal_temperature_check();
  213. // check if we've lost contact with the ground station
  214. failsafe_gcs_check();
  215. // check if we've lost terrain data
  216. failsafe_terrain_check();
  217. #if AC_FENCE == ENABLED
  218. // check if we have breached a fence
  219. fence_check();
  220. #endif // AC_FENCE_ENABLED
  221. ServoRelayEvents.update_events();
  222. }
  223. // one_hz_loop - runs at 1Hz
  224. void Sub::one_hz_loop()
  225. {
  226. bool arm_check = arming.pre_arm_checks(false);
  227. ap.pre_arm_check = arm_check;
  228. AP_Notify::flags.pre_arm_check = arm_check;
  229. AP_Notify::flags.pre_arm_gps_check = position_ok();
  230. AP_Notify::flags.flying = motors.armed();
  231. if (should_log(MASK_LOG_ANY)) {
  232. Log_Write_Data(DATA_AP_STATE, ap.value);
  233. }
  234. if (!motors.armed()) {
  235. // make it possible to change ahrs orientation at runtime during initial config
  236. ahrs.update_orientation();
  237. // set all throttle channel settings
  238. motors.set_throttle_range(channel_throttle->get_radio_min(), channel_throttle->get_radio_max());
  239. }
  240. // update assigned functions and enable auxiliary servos
  241. SRV_Channels::enable_aux_servos();
  242. // update position controller alt limits
  243. update_poscon_alt_max();
  244. // log terrain data
  245. terrain_logging();
  246. // need to set "likely flying" when armed to allow for compass
  247. // learning to run
  248. ahrs.set_likely_flying(hal.util->get_soft_armed());
  249. }
  250. // called at 50hz
  251. void Sub::update_GPS()
  252. {
  253. static uint32_t last_gps_reading[GPS_MAX_INSTANCES]; // time of last gps message
  254. bool gps_updated = false;
  255. gps.update();
  256. // log after every gps message
  257. for (uint8_t i=0; i<gps.num_sensors(); i++) {
  258. if (gps.last_message_time_ms(i) != last_gps_reading[i]) {
  259. last_gps_reading[i] = gps.last_message_time_ms(i);
  260. gps_updated = true;
  261. break;
  262. }
  263. }
  264. if (gps_updated) {
  265. #if CAMERA == ENABLED
  266. camera.update();
  267. #endif
  268. }
  269. }
  270. void Sub::read_AHRS()
  271. {
  272. // Perform IMU calculations and get attitude info
  273. //-----------------------------------------------
  274. // <true> tells AHRS to skip INS update as we have already done it in fast_loop()
  275. ahrs.update(true);
  276. ahrs_view.update(true);
  277. }
  278. // read baro and rangefinder altitude at 10hz
  279. void Sub::update_altitude()
  280. {
  281. // read in baro altitude
  282. read_barometer();
  283. pos_control.calculate_rate(0.1);
  284. if (should_log(MASK_LOG_CTUN)) {
  285. Log_Write_Control_Tuning();
  286. }
  287. }
  288. bool Sub::control_check_barometer()
  289. {
  290. #if CONFIG_HAL_BOARD != HAL_BOARD_SITL
  291. if (!ap.depth_sensor_present) { // can't hold depth without a depth sensor
  292. gcs().send_text(MAV_SEVERITY_WARNING, "Depth sensor is not connected.");
  293. return false;
  294. } else if (failsafe.sensor_health) {
  295. gcs().send_text(MAV_SEVERITY_WARNING, "Depth sensor error.");
  296. return false;
  297. }
  298. #endif
  299. return true;
  300. }
  301. AP_HAL_MAIN_CALLBACKS(&sub);