ArduSub.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. static int j = 0;
  104. j++;
  105. if(j>500)
  106. {
  107. float depth_now =barometer.get_altitude();
  108. gcs().send_text(MAV_SEVERITY_INFO, " %f %f\n",(float)depth_now,inertial_nav.get_altitude());
  109. j=0;
  110. }
  111. // Inertial Nav
  112. // --------------------
  113. read_inertia();
  114. // check if ekf has reset target heading
  115. check_ekf_yaw_reset();
  116. // run the attitude controllers
  117. update_flight_mode();
  118. //motor_toCan();
  119. // update home from EKF if necessary
  120. update_home_from_EKF();
  121. // check if we've reached the surface or bottom
  122. update_surface_and_bottom_detector();
  123. #if MOUNT == ENABLED
  124. // camera mount's fast update
  125. camera_mount.update_fast();
  126. #endif
  127. // log sensor health
  128. if (should_log(MASK_LOG_ANY)) {
  129. Log_Sensor_Health();
  130. }
  131. }
  132. // 50 Hz tasks
  133. void Sub::fifty_hz_loop()
  134. {
  135. // check pilot input failsafe
  136. failsafe_pilot_input_check();
  137. failsafe_crash_check();
  138. failsafe_ekf_check();
  139. failsafe_sensors_check();
  140. // Update rc input/output
  141. rc().read_input();
  142. SRV_Channels::output_ch_all();
  143. }
  144. // update_batt_compass - read battery and compass
  145. // should be called at 10hz
  146. void Sub::update_batt_compass()
  147. {
  148. // read battery before compass because it may be used for motor interference compensation
  149. battery.read();
  150. if (AP::compass().enabled()) {
  151. // update compass with throttle value - used for compassmot
  152. compass.set_throttle(motors.get_throttle());
  153. compass.read();
  154. }
  155. }
  156. // ten_hz_logging_loop
  157. // should be run at 10hz
  158. void Sub::ten_hz_logging_loop()
  159. {
  160. // log attitude data if we're not already logging at the higher rate
  161. if (should_log(MASK_LOG_ATTITUDE_MED) && !should_log(MASK_LOG_ATTITUDE_FAST)) {
  162. Log_Write_Attitude();
  163. logger.Write_Rate(&ahrs_view, motors, attitude_control, pos_control);
  164. if (should_log(MASK_LOG_PID)) {
  165. logger.Write_PID(LOG_PIDR_MSG, attitude_control.get_rate_roll_pid().get_pid_info());
  166. logger.Write_PID(LOG_PIDP_MSG, attitude_control.get_rate_pitch_pid().get_pid_info());
  167. logger.Write_PID(LOG_PIDY_MSG, attitude_control.get_rate_yaw_pid().get_pid_info());
  168. logger.Write_PID(LOG_PIDA_MSG, pos_control.get_accel_z_pid().get_pid_info());
  169. }
  170. }
  171. if (should_log(MASK_LOG_MOTBATT)) {
  172. Log_Write_MotBatt();
  173. }
  174. if (should_log(MASK_LOG_RCIN)) {
  175. logger.Write_RCIN();
  176. }
  177. if (should_log(MASK_LOG_RCOUT)) {
  178. logger.Write_RCOUT();
  179. }
  180. if (should_log(MASK_LOG_NTUN) && mode_requires_GPS(control_mode)) {
  181. pos_control.write_log();
  182. }
  183. if (should_log(MASK_LOG_IMU) || should_log(MASK_LOG_IMU_FAST) || should_log(MASK_LOG_IMU_RAW)) {
  184. logger.Write_Vibration();
  185. }
  186. if (should_log(MASK_LOG_CTUN)) {
  187. attitude_control.control_monitor_log();
  188. }
  189. }
  190. // twentyfive_hz_logging_loop
  191. // should be run at 25hz
  192. void Sub::twentyfive_hz_logging()
  193. {
  194. if (should_log(MASK_LOG_ATTITUDE_FAST)) {
  195. Log_Write_Attitude();
  196. logger.Write_Rate(&ahrs_view, motors, attitude_control, pos_control);
  197. if (should_log(MASK_LOG_PID)) {
  198. logger.Write_PID(LOG_PIDR_MSG, attitude_control.get_rate_roll_pid().get_pid_info());
  199. logger.Write_PID(LOG_PIDP_MSG, attitude_control.get_rate_pitch_pid().get_pid_info());
  200. logger.Write_PID(LOG_PIDY_MSG, attitude_control.get_rate_yaw_pid().get_pid_info());
  201. logger.Write_PID(LOG_PIDA_MSG, pos_control.get_accel_z_pid().get_pid_info());
  202. }
  203. }
  204. // log IMU data if we're not already logging at the higher rate
  205. if (should_log(MASK_LOG_IMU) && !should_log(MASK_LOG_IMU_RAW)) {
  206. logger.Write_IMU();
  207. }
  208. }
  209. // three_hz_loop - 3.3hz loop
  210. void Sub::three_hz_loop()
  211. {
  212. leak_detector.update();
  213. failsafe_leak_check();
  214. failsafe_internal_pressure_check();
  215. failsafe_internal_temperature_check();
  216. // check if we've lost contact with the ground station
  217. failsafe_gcs_check();
  218. // check if we've lost terrain data
  219. failsafe_terrain_check();
  220. #if AC_FENCE == ENABLED
  221. // check if we have breached a fence
  222. fence_check();
  223. #endif // AC_FENCE_ENABLED
  224. ServoRelayEvents.update_events();
  225. }
  226. // one_hz_loop - runs at 1Hz
  227. void Sub::one_hz_loop()
  228. {
  229. bool arm_check = arming.pre_arm_checks(false);
  230. ap.pre_arm_check = arm_check;
  231. AP_Notify::flags.pre_arm_check = arm_check;
  232. AP_Notify::flags.pre_arm_gps_check = position_ok();
  233. AP_Notify::flags.flying = motors.armed();
  234. if (should_log(MASK_LOG_ANY)) {
  235. Log_Write_Data(DATA_AP_STATE, ap.value);
  236. }
  237. if (!motors.armed()) {
  238. // make it possible to change ahrs orientation at runtime during initial config
  239. ahrs.update_orientation();
  240. // set all throttle channel settings
  241. motors.set_throttle_range(channel_throttle->get_radio_min(), channel_throttle->get_radio_max());
  242. }
  243. // update assigned functions and enable auxiliary servos
  244. SRV_Channels::enable_aux_servos();
  245. // update position controller alt limits
  246. update_poscon_alt_max();
  247. // log terrain data
  248. terrain_logging();
  249. // need to set "likely flying" when armed to allow for compass
  250. // learning to run
  251. ahrs.set_likely_flying(hal.util->get_soft_armed());
  252. }
  253. // called at 50hz
  254. void Sub::update_GPS()
  255. {
  256. static uint32_t last_gps_reading[GPS_MAX_INSTANCES]; // time of last gps message
  257. bool gps_updated = false;
  258. gps.update();
  259. // log after every gps message
  260. for (uint8_t i=0; i<gps.num_sensors(); i++) {
  261. if (gps.last_message_time_ms(i) != last_gps_reading[i]) {
  262. last_gps_reading[i] = gps.last_message_time_ms(i);
  263. gps_updated = true;
  264. break;
  265. }
  266. }
  267. if (gps_updated) {
  268. #if CAMERA == ENABLED
  269. camera.update();
  270. #endif
  271. }
  272. }
  273. void Sub::read_AHRS()
  274. {
  275. // Perform IMU calculations and get attitude info
  276. //-----------------------------------------------
  277. // <true> tells AHRS to skip INS update as we have already done it in fast_loop()
  278. ahrs.update(true);
  279. ahrs_view.update(true);
  280. }
  281. // read baro and rangefinder altitude at 10hz
  282. void Sub::update_altitude()
  283. {
  284. // read in baro altitude
  285. read_barometer();
  286. if (should_log(MASK_LOG_CTUN)) {
  287. Log_Write_Control_Tuning();
  288. }
  289. }
  290. bool Sub::control_check_barometer()
  291. {
  292. #if CONFIG_HAL_BOARD != HAL_BOARD_SITL
  293. if (!ap.depth_sensor_present) { // can't hold depth without a depth sensor
  294. gcs().send_text(MAV_SEVERITY_WARNING, "Depth sensor is not connected.");
  295. return false;
  296. } else if (failsafe.sensor_health) {
  297. gcs().send_text(MAV_SEVERITY_WARNING, "Depth sensor error.");
  298. return false;
  299. }
  300. #endif
  301. return true;
  302. }
  303. AP_HAL_MAIN_CALLBACKS(&sub);