ArduSub.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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, 10, 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. logger.Write_PID(LOG_PIDR_MSG, attitude_control.get_rate_roll_pid().get_pid_info());
  158. logger.Write_PID(LOG_PIDP_MSG, attitude_control.get_rate_pitch_pid().get_pid_info());
  159. logger.Write_PID(LOG_PIDY_MSG, attitude_control.get_rate_yaw_pid().get_pid_info());
  160. logger.Write_PID(LOG_PIDA_MSG, pos_control.get_accel_z_pid().get_pid_info());
  161. }
  162. }
  163. if (should_log(MASK_LOG_MOTBATT)) {
  164. Log_Write_MotBatt();
  165. }
  166. if (should_log(MASK_LOG_RCIN)) {
  167. logger.Write_RCIN();
  168. }
  169. if (should_log(MASK_LOG_RCOUT)) {
  170. logger.Write_RCOUT();
  171. }
  172. if (should_log(MASK_LOG_NTUN) && mode_requires_GPS(control_mode)) {
  173. pos_control.write_log();
  174. }
  175. if (should_log(MASK_LOG_IMU) || should_log(MASK_LOG_IMU_FAST) || should_log(MASK_LOG_IMU_RAW)) {
  176. logger.Write_Vibration();
  177. }
  178. if (should_log(MASK_LOG_CTUN)) {
  179. attitude_control.control_monitor_log();
  180. }
  181. }
  182. // twentyfive_hz_logging_loop
  183. // should be run at 25hz
  184. void Sub::twentyfive_hz_logging()
  185. {
  186. if (should_log(MASK_LOG_ATTITUDE_FAST)) {
  187. Log_Write_Attitude();
  188. logger.Write_Rate(&ahrs_view, motors, attitude_control, pos_control);
  189. if (should_log(MASK_LOG_PID)) {
  190. logger.Write_PID(LOG_PIDR_MSG, attitude_control.get_rate_roll_pid().get_pid_info());
  191. logger.Write_PID(LOG_PIDP_MSG, attitude_control.get_rate_pitch_pid().get_pid_info());
  192. logger.Write_PID(LOG_PIDY_MSG, attitude_control.get_rate_yaw_pid().get_pid_info());
  193. logger.Write_PID(LOG_PIDA_MSG, pos_control.get_accel_z_pid().get_pid_info());
  194. }
  195. }
  196. // log IMU data if we're not already logging at the higher rate
  197. if (should_log(MASK_LOG_IMU) && !should_log(MASK_LOG_IMU_RAW)) {
  198. logger.Write_IMU();
  199. }
  200. }
  201. // three_hz_loop - 3.3hz loop
  202. void Sub::three_hz_loop()
  203. {
  204. leak_detector.update();
  205. failsafe_leak_check();
  206. failsafe_internal_pressure_check();
  207. failsafe_internal_temperature_check();
  208. // check if we've lost contact with the ground station
  209. failsafe_gcs_check();
  210. // check if we've lost terrain data
  211. failsafe_terrain_check();
  212. #if AC_FENCE == ENABLED
  213. // check if we have breached a fence
  214. fence_check();
  215. #endif // AC_FENCE_ENABLED
  216. ServoRelayEvents.update_events();
  217. }
  218. // one_hz_loop - runs at 1Hz
  219. void Sub::one_hz_loop()
  220. {
  221. bool arm_check = arming.pre_arm_checks(false);
  222. ap.pre_arm_check = arm_check;
  223. AP_Notify::flags.pre_arm_check = arm_check;
  224. AP_Notify::flags.pre_arm_gps_check = position_ok();
  225. AP_Notify::flags.flying = motors.armed();
  226. if (should_log(MASK_LOG_ANY)) {
  227. Log_Write_Data(DATA_AP_STATE, ap.value);
  228. }
  229. if (!motors.armed()) {
  230. // make it possible to change ahrs orientation at runtime during initial config
  231. ahrs.update_orientation();
  232. // set all throttle channel settings
  233. motors.set_throttle_range(channel_throttle->get_radio_min(), channel_throttle->get_radio_max());
  234. }
  235. // update assigned functions and enable auxiliary servos
  236. SRV_Channels::enable_aux_servos();
  237. // update position controller alt limits
  238. update_poscon_alt_max();
  239. // log terrain data
  240. terrain_logging();
  241. // need to set "likely flying" when armed to allow for compass
  242. // learning to run
  243. ahrs.set_likely_flying(hal.util->get_soft_armed());
  244. }
  245. // called at 50hz
  246. void Sub::update_GPS()
  247. {
  248. static uint32_t last_gps_reading[GPS_MAX_INSTANCES]; // time of last gps message
  249. bool gps_updated = false;
  250. gps.update();
  251. // log after every gps message
  252. for (uint8_t i=0; i<gps.num_sensors(); i++) {
  253. if (gps.last_message_time_ms(i) != last_gps_reading[i]) {
  254. last_gps_reading[i] = gps.last_message_time_ms(i);
  255. gps_updated = true;
  256. break;
  257. }
  258. }
  259. if (gps_updated) {
  260. #if CAMERA == ENABLED
  261. camera.update();
  262. #endif
  263. }
  264. }
  265. void Sub::read_AHRS()
  266. {
  267. // Perform IMU calculations and get attitude info
  268. //-----------------------------------------------
  269. // <true> tells AHRS to skip INS update as we have already done it in fast_loop()
  270. ahrs.update(true);
  271. ahrs_view.update(true);
  272. }
  273. // read baro and rangefinder altitude at 10hz
  274. void Sub::update_altitude()
  275. {
  276. // read in baro altitude
  277. read_barometer();
  278. if (should_log(MASK_LOG_CTUN)) {
  279. Log_Write_Control_Tuning();
  280. }
  281. }
  282. bool Sub::control_check_barometer()
  283. {
  284. #if CONFIG_HAL_BOARD != HAL_BOARD_SITL
  285. if (!ap.depth_sensor_present) { // can't hold depth without a depth sensor
  286. gcs().send_text(MAV_SEVERITY_WARNING, "Depth sensor is not connected.");
  287. return false;
  288. } else if (failsafe.sensor_health) {
  289. gcs().send_text(MAV_SEVERITY_WARNING, "Depth sensor error.");
  290. return false;
  291. }
  292. #endif
  293. return true;
  294. }
  295. AP_HAL_MAIN_CALLBACKS(&sub);