Sub.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  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. #pragma once
  14. /*
  15. This is the main Sub class
  16. */
  17. ////////////////////////////////////////////////////////////////////////////////
  18. // Header includes
  19. ////////////////////////////////////////////////////////////////////////////////
  20. #include <cmath>
  21. #include <stdio.h>
  22. #include <stdarg.h>
  23. #include <AP_HAL/AP_HAL.h>
  24. // Common dependencies
  25. #include <AP_Common/AP_Common.h>
  26. #include <AP_Common/Location.h>
  27. #include <AP_Param/AP_Param.h>
  28. #include <StorageManager/StorageManager.h>
  29. #include <GCS_MAVLink/GCS.h>
  30. #include <AP_AccelCal/AP_AccelCal.h> // interface and maths for accelerometer calibration
  31. #include <AP_Math/AP_Math.h> // ArduPilot Mega Vector/Matrix math Library
  32. #include <AP_Declination/AP_Declination.h> // ArduPilot Mega Declination Helper Library
  33. // Application dependencies
  34. #include <AP_SerialManager/AP_SerialManager.h> // Serial manager library
  35. #include <AP_GPS/AP_GPS.h> // ArduPilot GPS library
  36. #include <AP_Logger/AP_Logger.h> // ArduPilot Mega Flash Memory Library
  37. #include <AP_Baro/AP_Baro.h>
  38. #include <AP_Compass/AP_Compass.h> // ArduPilot Mega Magnetometer Library
  39. #include <AP_InertialSensor/AP_InertialSensor.h> // ArduPilot Mega Inertial Sensor (accel & gyro) Library
  40. #include <AP_AHRS/AP_AHRS.h>
  41. #include <AP_NavEKF2/AP_NavEKF2.h>
  42. #include <AP_NavEKF3/AP_NavEKF3.h>
  43. #include <AP_Mission/AP_Mission.h> // Mission command library
  44. #include <AC_PID/AC_P.h> // P library
  45. #include <AC_PID/AC_PID.h> // PID library
  46. #include <AC_PID/AC_PI_2D.h> // PI library (2-axis)
  47. #include <AC_PID/AC_PID_2D.h> // PID library (2-axis)
  48. #include <AC_AttitudeControl/AC_AttitudeControl_Sub.h> // Attitude control library
  49. #include <AC_AttitudeControl/AC_PosControl_Sub.h> // Position control library
  50. #include <AP_Motors/AP_Motors.h> // AP Motors library
  51. #include <AP_RangeFinder/AP_RangeFinder.h> // Range finder library
  52. #include <Filter/Filter.h> // Filter library
  53. #include <AP_Relay/AP_Relay.h> // APM relay
  54. #include <AP_ServoRelayEvents/AP_ServoRelayEvents.h>
  55. #include <AP_Mount/AP_Mount.h> // Camera/Antenna mount
  56. #include <AP_Vehicle/AP_Vehicle.h> // needed for AHRS build
  57. #include <AP_InertialNav/AP_InertialNav.h> // ArduPilot Mega inertial navigation library
  58. #include <AC_WPNav/AC_WPNav.h> // Waypoint navigation library
  59. #include <AC_WPNav/AC_Loiter.h>
  60. #include <AC_WPNav/AC_Circle.h> // circle navigation library
  61. #include <AC_Fence/AC_Fence.h> // Fence library
  62. #include <AP_Scheduler/AP_Scheduler.h> // main loop scheduler
  63. #include <AP_Scheduler/PerfInfo.h> // loop perf monitoring
  64. #include <AP_Notify/AP_Notify.h> // Notify library
  65. #include <AP_BattMonitor/AP_BattMonitor.h> // Battery monitor library
  66. #include <AP_BoardConfig/AP_BoardConfig.h> // board configuration library
  67. #include <AP_BoardConfig/AP_BoardConfig_CAN.h>
  68. #include <AP_Terrain/AP_Terrain.h>
  69. #include <AP_JSButton/AP_JSButton.h> // Joystick/gamepad button function assignment
  70. #include <AP_LeakDetector/AP_LeakDetector.h> // Leak detector
  71. #include <AP_TemperatureSensor/TSYS01.h>
  72. #include <AP_Common/AP_FWVersion.h>
  73. // Local modules
  74. #include "defines.h"
  75. #include "config.h"
  76. #include "GCS_Mavlink.h"
  77. #include "RC_Channel.h" // RC Channel Library
  78. #include "Parameters.h"
  79. #include "AP_Arming_Sub.h"
  80. #include "GCS_Sub.h"
  81. //------self define start-------------------
  82. #include "Gearmotorpid.h"
  83. #define OUSHENCAN_MAX_NUM_ESCS 6
  84. #ifndef USERHOOK_50HZLOOP
  85. #define USERHOOK_50HZLOOP 1
  86. #endif
  87. #define startval 0
  88. #define Speedmax_hand 62
  89. #define Speedmax_hand_f 62.0
  90. #define speedmin 31
  91. #define maxerror 41//31
  92. #define maxerror_f 41.0//31.0
  93. #define Horizontal 0
  94. #define Vertical 1
  95. #define section0 0
  96. #define section90 1
  97. #define section_90 2
  98. #define action_Hor 0
  99. #define action_Ver_postive 1
  100. #define action_Ver_negtive 2
  101. #define Orign 0
  102. #define foward 1
  103. #define backward 2
  104. #define Speedmax 60
  105. #define STARTCONT 2000
  106. //压力等级 包含下压和上浮总共10级 第五级为1500 即中值
  107. enum PressNetLevel {
  108. first = 0,
  109. second = 1,
  110. third = 2,
  111. forth = 3,
  112. fifth = 4,
  113. no =5,
  114. sixth = 6,
  115. seventh = 7,
  116. eighth = 8,
  117. ninth = 9,
  118. tenth = 10
  119. };
  120. #define SaberDatapacketID 0x8106
  121. #define RespAllStatusID 0x8602
  122. #define SystemResetAckID 0x8401
  123. #define Saber_selftest_Failed 0x08 //自检失败
  124. #define Saber_WakeUpHost 0x0101 //WakeUp模式
  125. #define Saber_MesureMode 0x02 //测量模式
  126. //usigned char to float将char类型转换成float类型
  127. typedef union{
  128. unsigned char u[4];
  129. float f;
  130. }uTof;
  131. //------self define end-------------------
  132. // libraries which are dependent on #defines in defines.h and/or config.h
  133. #if OPTFLOW == ENABLED
  134. #include <AP_OpticalFlow/AP_OpticalFlow.h> // Optical Flow library
  135. #endif
  136. #if RCMAP_ENABLED == ENABLED
  137. #include <AP_RCMapper/AP_RCMapper.h> // RC input mapping library
  138. #endif
  139. #if RPM_ENABLED == ENABLED
  140. #include <AP_RPM/AP_RPM.h>
  141. #endif
  142. #if GRIPPER_ENABLED == ENABLED
  143. #include <AP_Gripper/AP_Gripper.h> // gripper stuff
  144. #endif
  145. #if PROXIMITY_ENABLED == ENABLED
  146. #include <AP_Proximity/AP_Proximity.h>
  147. #endif
  148. #if AVOIDANCE_ENABLED == ENABLED
  149. #include <AC_Avoidance/AC_Avoid.h> // Stop at fence library
  150. #endif
  151. #if AC_RALLY == ENABLED
  152. #include <AP_Rally/AP_Rally.h> // Rally point library
  153. #endif
  154. #if CAMERA == ENABLED
  155. #include <AP_Camera/AP_Camera.h> // Photo or video camera
  156. #endif
  157. #ifdef ENABLE_SCRIPTING
  158. #include <AP_Scripting/AP_Scripting.h>
  159. #endif
  160. #if CONFIG_HAL_BOARD == HAL_BOARD_SITL
  161. #include <SITL/SITL.h>
  162. #endif
  163. class Sub : public AP_HAL::HAL::Callbacks {
  164. public:
  165. friend class GCS_MAVLINK_Sub;
  166. friend class GCS_Sub;
  167. friend class Parameters;
  168. friend class ParametersG2;
  169. friend class AP_Arming_Sub;
  170. friend class RC_Channels_Sub;
  171. Sub(void);
  172. // HAL::Callbacks implementation.
  173. void setup() override;
  174. void loop() override;
  175. private:
  176. static const AP_FWVersion fwver;
  177. // key aircraft parameters passed to multiple libraries
  178. AP_Vehicle::MultiCopter aparm;
  179. // Global parameters are all contained within the 'g' class.
  180. Parameters g;
  181. ParametersG2 g2;
  182. // main loop scheduler
  183. AP_Scheduler scheduler{FUNCTOR_BIND_MEMBER(&Sub::fast_loop, void)};
  184. // AP_Notify instance
  185. AP_Notify notify;
  186. // primary input control channels
  187. RC_Channel *channel_roll;
  188. RC_Channel *channel_pitch;
  189. RC_Channel *channel_throttle;
  190. RC_Channel *channel_yaw;
  191. RC_Channel *channel_forward;
  192. RC_Channel *channel_lateral;
  193. AP_Logger logger;
  194. AP_GPS gps;
  195. AP_LeakDetector leak_detector;
  196. TSYS01 celsius;
  197. Compass compass;
  198. AP_InertialSensor ins;
  199. RangeFinder rangefinder;
  200. struct {
  201. bool enabled:1;
  202. bool alt_healthy:1; // true if we can trust the altitude from the rangefinder
  203. int16_t alt_cm; // tilt compensated altitude (in cm) from rangefinder
  204. uint32_t last_healthy_ms;
  205. LowPassFilterFloat alt_cm_filt; // altitude filter
  206. } rangefinder_state = { false, false, 0, 0 };
  207. #if RPM_ENABLED == ENABLED
  208. AP_RPM rpm_sensor;
  209. #endif
  210. // Inertial Navigation EKF
  211. NavEKF2 EKF2{&ahrs, rangefinder};
  212. NavEKF3 EKF3{&ahrs, rangefinder};
  213. AP_AHRS_NavEKF ahrs{EKF2, EKF3, AP_AHRS_NavEKF::FLAG_ALWAYS_USE_EKF};
  214. #if CONFIG_HAL_BOARD == HAL_BOARD_SITL
  215. SITL::SITL sitl;
  216. #endif
  217. // Mission library
  218. AP_Mission mission{
  219. FUNCTOR_BIND_MEMBER(&Sub::start_command, bool, const AP_Mission::Mission_Command &),
  220. FUNCTOR_BIND_MEMBER(&Sub::verify_command_callback, bool, const AP_Mission::Mission_Command &),
  221. FUNCTOR_BIND_MEMBER(&Sub::exit_mission, void)};
  222. // Optical flow sensor
  223. #if OPTFLOW == ENABLED
  224. OpticalFlow optflow;
  225. #endif
  226. // system time in milliseconds of last recorded yaw reset from ekf
  227. uint32_t ekfYawReset_ms = 0;
  228. AP_SerialManager serial_manager;
  229. // GCS selection
  230. GCS_Sub _gcs; // avoid using this; use gcs()
  231. GCS_Sub &gcs() { return _gcs; }
  232. // User variables
  233. #ifdef USERHOOK_VARIABLES
  234. # include USERHOOK_VARIABLES
  235. #endif
  236. // Documentation of Globals:
  237. union {
  238. struct {
  239. uint8_t pre_arm_check : 1; // true if all pre-arm checks (rc, accel calibration, gps lock) have been performed
  240. uint8_t logging_started : 1; // true if logging has started
  241. uint8_t compass_mot : 1; // true if we are currently performing compassmot calibration
  242. uint8_t motor_test : 1; // true if we are currently performing the motors test
  243. uint8_t initialised : 1; // true once the init_ardupilot function has completed. Extended status to GCS is not sent until this completes
  244. uint8_t gps_base_pos_set : 1; // true when the gps base position has been set (used for RTK gps only)
  245. uint8_t at_bottom : 1; // true if we are at the bottom
  246. uint8_t at_surface : 1; // true if we are at the surface
  247. uint8_t depth_sensor_present: 1; // true if there is a depth sensor detected at boot
  248. uint8_t compass_init_location:1; // true when the compass's initial location has been set
  249. };
  250. uint32_t value;
  251. } ap;
  252. // This is the state of the flight control system
  253. // There are multiple states defined such as STABILIZE, ACRO,
  254. #if RCMAP_ENABLED == ENABLED
  255. RCMapper rcmap;
  256. #endif
  257. // board specific config
  258. AP_BoardConfig BoardConfig;
  259. #if HAL_WITH_UAVCAN
  260. // board specific config for CAN bus
  261. AP_BoardConfig_CAN BoardConfig_CAN;
  262. #endif
  263. // Failsafe
  264. struct {
  265. uint32_t last_leak_warn_ms; // last time a leak warning was sent to gcs
  266. uint32_t last_gcs_warn_ms;
  267. uint32_t last_heartbeat_ms; // the time when the last HEARTBEAT message arrived from a GCS - used for triggering gcs failsafe
  268. uint32_t last_pilot_input_ms; // last time we received pilot input in the form of MANUAL_CONTROL or RC_CHANNELS_OVERRIDE messages
  269. uint32_t terrain_first_failure_ms; // the first time terrain data access failed - used to calculate the duration of the failure
  270. uint32_t terrain_last_failure_ms; // the most recent time terrain data access failed
  271. uint32_t last_crash_warn_ms; // last time a crash warning was sent to gcs
  272. uint32_t last_ekf_warn_ms; // last time an ekf warning was sent to gcs
  273. uint8_t pilot_input : 1; // true if pilot input failsafe is active, handles things like joystick being disconnected during operation
  274. uint8_t gcs : 1; // A status flag for the ground station failsafe
  275. uint8_t ekf : 1; // true if ekf failsafe has occurred
  276. uint8_t terrain : 1; // true if the missing terrain data failsafe has occurred
  277. uint8_t leak : 1; // true if leak recently detected
  278. uint8_t internal_pressure : 1; // true if internal pressure is over threshold
  279. uint8_t internal_temperature : 1; // true if temperature is over threshold
  280. uint8_t crash : 1; // true if we are crashed
  281. uint8_t sensor_health : 1; // true if at least one sensor has triggered a failsafe (currently only used for depth in depth enabled modes)
  282. } failsafe;
  283. bool any_failsafe_triggered() const {
  284. return (failsafe.pilot_input || battery.has_failsafed() || failsafe.gcs || failsafe.ekf || failsafe.terrain);
  285. }
  286. // sensor health for logging
  287. struct {
  288. uint8_t baro : 1; // true if any single baro is healthy
  289. uint8_t depth : 1; // true if depth sensor is healthy
  290. uint8_t compass : 1; // true if compass is healthy
  291. } sensor_health;
  292. // Baro sensor instance index of the external water pressure sensor
  293. uint8_t depth_sensor_idx;
  294. // GPS variables
  295. // Sometimes we need to remove the scaling for distance calcs
  296. float scaleLongDown;
  297. // Auto
  298. AutoMode auto_mode; // controls which auto controller is run
  299. // Guided
  300. GuidedMode guided_mode; // controls which controller is run (pos or vel)
  301. // Circle
  302. bool circle_pilot_yaw_override; // true if pilot is overriding yaw
  303. // Stores initial bearing when armed
  304. int32_t initial_armed_bearing;
  305. // Throttle variables
  306. int16_t desired_climb_rate; // pilot desired climb rate - for logging purposes only
  307. // Loiter control
  308. uint16_t loiter_time_max; // How long we should stay in Loiter Mode for mission scripting (time in seconds)
  309. uint32_t loiter_time; // How long have we been loitering - The start time in millis
  310. // Delay the next navigation command
  311. uint32_t nav_delay_time_max_ms; // used for delaying the navigation commands
  312. uint32_t nav_delay_time_start_ms;
  313. // Battery Sensors
  314. AP_BattMonitor battery{MASK_LOG_CURRENT,
  315. FUNCTOR_BIND_MEMBER(&Sub::handle_battery_failsafe, void, const char*, const int8_t),
  316. _failsafe_priorities};
  317. // Altitude
  318. // The cm/s we are moving up or down based on filtered data - Positive = UP
  319. int16_t climb_rate;
  320. float target_rangefinder_alt; // desired altitude in cm above the ground
  321. bool holding_depth;
  322. // Turn counter
  323. int32_t quarter_turn_count;
  324. uint8_t last_turn_state;
  325. // Input gain
  326. float gain;
  327. // Flag indicating if we are currently using input hold
  328. bool input_hold_engaged;
  329. // 3D Location vectors
  330. // Current location of the Sub (altitude is relative to home)
  331. Location current_loc;
  332. // Navigation Yaw control
  333. // auto flight mode's yaw mode
  334. uint8_t auto_yaw_mode;
  335. // Yaw will point at this location if auto_yaw_mode is set to AUTO_YAW_ROI
  336. Vector3f roi_WP;
  337. // bearing from current location to the yaw_look_at_WP
  338. float yaw_look_at_WP_bearing;
  339. float yaw_xtrack_correct_heading;
  340. // yaw used for YAW_LOOK_AT_HEADING yaw_mode
  341. int32_t yaw_look_at_heading;
  342. // Deg/s we should turn
  343. int16_t yaw_look_at_heading_slew;
  344. // heading when in yaw_look_ahead_bearing
  345. float yaw_look_ahead_bearing;
  346. // Delay Mission Scripting Command
  347. int32_t condition_value; // used in condition commands (eg delay, change alt, etc.)
  348. uint32_t condition_start;
  349. // IMU variables
  350. // Integration time (in seconds) for the gyros (DCM algorithm)
  351. // Updated with the fast loop
  352. float G_Dt;
  353. // Inertial Navigation
  354. AP_InertialNav_NavEKF inertial_nav;
  355. AP_AHRS_View ahrs_view;
  356. // Attitude, Position and Waypoint navigation objects
  357. // To-Do: move inertial nav up or other navigation variables down here
  358. AC_AttitudeControl_Sub attitude_control;
  359. AC_PosControl_Sub pos_control;
  360. AC_WPNav wp_nav;
  361. AC_Loiter loiter_nav;
  362. AC_Circle circle_nav;
  363. // Reference to the relay object
  364. AP_Relay relay;
  365. // handle repeated servo and relay events
  366. AP_ServoRelayEvents ServoRelayEvents;
  367. // Camera
  368. #if CAMERA == ENABLED
  369. AP_Camera camera{MASK_LOG_CAMERA, current_loc};
  370. #endif
  371. // Camera/Antenna mount tracking and stabilisation stuff
  372. #if MOUNT == ENABLED
  373. // current_loc uses the baro/gps soloution for altitude rather than gps only.
  374. AP_Mount camera_mount{current_loc};
  375. #endif
  376. // AC_Fence library to reduce fly-aways
  377. #if AC_FENCE == ENABLED
  378. AC_Fence fence;
  379. #endif
  380. #if AVOIDANCE_ENABLED == ENABLED
  381. AC_Avoid avoid;
  382. #endif
  383. // Rally library
  384. #if AC_RALLY == ENABLED
  385. AP_Rally rally;
  386. #endif
  387. // terrain handling
  388. #if AP_TERRAIN_AVAILABLE && AC_TERRAIN
  389. AP_Terrain terrain{mission};
  390. #endif
  391. // used to allow attitude and depth control without a position system
  392. struct attitude_no_gps_struct {
  393. uint32_t last_message_ms;
  394. mavlink_set_attitude_target_t packet;
  395. };
  396. attitude_no_gps_struct set_attitude_target_no_gps {0};
  397. // Top-level logic
  398. // setup the var_info table
  399. AP_Param param_loader;
  400. uint32_t last_pilot_heading;
  401. uint32_t last_input_ms;
  402. uint32_t last_input_ms_stable;
  403. int32_t last_roll_s;
  404. int32_t last_pitch_s;
  405. int32_t last_yaw_s;
  406. int32_t last_roll;
  407. int32_t last_pitch;
  408. int32_t last_yaw;
  409. uint32_t last_pilot_yaw_input_ms;
  410. uint32_t fs_terrain_recover_start_ms;
  411. static const AP_Scheduler::Task scheduler_tasks[];
  412. static const AP_Param::Info var_info[];
  413. static const struct LogStructure log_structure[];
  414. void fast_loop();
  415. void fifty_hz_loop();
  416. void update_batt_compass(void);
  417. void ten_hz_logging_loop();
  418. void twentyfive_hz_logging();
  419. void three_hz_loop();
  420. void one_hz_loop();
  421. void update_GPS(void);
  422. void update_turn_counter();
  423. void read_AHRS(void);
  424. void update_altitude();
  425. float get_smoothing_gain();
  426. void get_pilot_desired_lean_angles(float roll_in, float pitch_in, float &roll_out, float &pitch_out, float angle_max);
  427. float get_pilot_desired_yaw_rate(int16_t stick_angle);
  428. void check_ekf_yaw_reset();
  429. float get_roi_yaw();
  430. float get_look_ahead_yaw();
  431. float get_pilot_desired_climb_rate(float throttle_control);
  432. float get_surface_tracking_climb_rate(int16_t target_rate, float current_alt_target, float dt);
  433. void update_poscon_alt_max();
  434. void rotate_body_frame_to_NE(float &x, float &y);
  435. void send_heartbeat(mavlink_channel_t chan);
  436. #if RPM_ENABLED == ENABLED
  437. void rpm_update();
  438. #endif
  439. void Log_Write_Control_Tuning();
  440. void Log_Write_Performance();
  441. void Log_Write_Attitude();
  442. void Log_Write_MotBatt();
  443. void Log_Write_Event(Log_Event id);
  444. void Log_Write_Data(uint8_t id, int32_t value);
  445. void Log_Write_Data(uint8_t id, uint32_t value);
  446. void Log_Write_Data(uint8_t id, int16_t value);
  447. void Log_Write_Data(uint8_t id, uint16_t value);
  448. void Log_Write_Data(uint8_t id, float value);
  449. void Log_Sensor_Health();
  450. void Log_Write_GuidedTarget(uint8_t target_type, const Vector3f& pos_target, const Vector3f& vel_target);
  451. void Log_Write_Vehicle_Startup_Messages();
  452. void load_parameters(void);
  453. void userhook_init();
  454. void userhook_FastLoop();
  455. void userhook_50Hz();
  456. void userhook_MediumLoop();
  457. void userhook_SlowLoop();
  458. void userhook_SuperSlowLoop();
  459. void update_home_from_EKF();
  460. void set_home_to_current_location_inflight();
  461. bool set_home_to_current_location(bool lock) WARN_IF_UNUSED;
  462. bool set_home(const Location& loc, bool lock) WARN_IF_UNUSED;
  463. bool far_from_EKF_origin(const Location& loc);
  464. void exit_mission();
  465. bool verify_loiter_unlimited();
  466. bool verify_loiter_time();
  467. bool verify_wait_delay();
  468. bool verify_within_distance();
  469. bool verify_yaw();
  470. bool acro_init(void);
  471. void acro_run();
  472. void get_pilot_desired_angle_rates(int16_t roll_in, int16_t pitch_in, int16_t yaw_in, float &roll_out, float &pitch_out, float &yaw_out);
  473. bool althold_init(void);
  474. void althold_run();
  475. // Handles attitude control for stabilize and althold modes
  476. void handle_attitude();
  477. void handle_attitude_stablize();
  478. void handle_attitude_sport();
  479. bool auto_init(void);
  480. void auto_run();
  481. void auto_wp_start(const Vector3f& destination);
  482. void auto_wp_start(const Location& dest_loc);
  483. void auto_wp_run();
  484. void auto_spline_run();
  485. void auto_circle_movetoedge_start(const Location &circle_center, float radius_m);
  486. void auto_circle_start();
  487. void auto_circle_run();
  488. void auto_nav_guided_start();
  489. void auto_nav_guided_run();
  490. bool auto_loiter_start();
  491. void auto_loiter_run();
  492. uint8_t get_default_auto_yaw_mode(bool rtl);
  493. void set_auto_yaw_mode(uint8_t yaw_mode);
  494. void set_auto_yaw_look_at_heading(float angle_deg, float turn_rate_dps, int8_t direction, uint8_t relative_angle);
  495. void set_auto_yaw_roi(const Location &roi_location);
  496. float get_auto_heading(void);
  497. bool circle_init(void);
  498. void circle_run();
  499. bool guided_init(bool ignore_checks = false);
  500. void guided_pos_control_start();
  501. void guided_vel_control_start();
  502. void guided_posvel_control_start();
  503. void guided_angle_control_start();
  504. bool guided_set_destination(const Vector3f& destination);
  505. bool guided_set_destination(const Location& dest_loc);
  506. void guided_set_velocity(const Vector3f& velocity);
  507. bool guided_set_destination_posvel(const Vector3f& destination, const Vector3f& velocity);
  508. void guided_set_angle(const Quaternion &q, float climb_rate_cms);
  509. void guided_run();
  510. void guided_pos_control_run();
  511. void guided_vel_control_run();
  512. void guided_posvel_control_run();
  513. void guided_angle_control_run();
  514. void guided_limit_clear();
  515. void guided_limit_set(uint32_t timeout_ms, float alt_min_cm, float alt_max_cm, float horiz_max_cm);
  516. void guided_limit_init_time_and_pos();
  517. bool guided_limit_check();
  518. bool poshold_init(void);
  519. void poshold_run();
  520. bool motordetect_init();
  521. void motordetect_run();
  522. bool stabilize_init(void);
  523. void stabilize_run();
  524. bool manual_init(void);
  525. void manual_run();
  526. void failsafe_sensors_check(void);
  527. void failsafe_crash_check();
  528. void failsafe_ekf_check(void);
  529. void handle_battery_failsafe(const char* type_str, const int8_t action);
  530. void failsafe_gcs_check();
  531. void failsafe_pilot_input_check(void);
  532. void set_neutral_controls(void);
  533. void failsafe_terrain_check();
  534. void failsafe_terrain_set_status(bool data_ok);
  535. void failsafe_terrain_on_event();
  536. void mainloop_failsafe_enable();
  537. void mainloop_failsafe_disable();
  538. void fence_check();
  539. bool set_mode(control_mode_t mode, mode_reason_t reason);
  540. void update_flight_mode();
  541. void exit_mode(control_mode_t old_control_mode, control_mode_t new_control_mode);
  542. bool mode_requires_GPS(control_mode_t mode);
  543. bool mode_has_manual_throttle(control_mode_t mode);
  544. bool mode_allows_arming(control_mode_t mode, bool arming_from_gcs);
  545. void notify_flight_mode(control_mode_t mode);
  546. void read_inertia();
  547. void update_surface_and_bottom_detector();
  548. void set_surfaced(bool at_surface);
  549. void set_bottomed(bool at_bottom);
  550. void motors_output();
  551. Vector3f pv_location_to_vector(const Location& loc);
  552. float pv_alt_above_origin(float alt_above_home_cm);
  553. void init_rc_in();
  554. void init_rc_out();
  555. void enable_motor_output();
  556. void init_joystick();
  557. void transform_manual_control_to_rc_override(int16_t x, int16_t y, int16_t z, int16_t r, uint16_t buttons);
  558. void handle_jsbutton_press(uint8_t button,bool shift=false,bool held=false);
  559. void handle_jsbutton_release(uint8_t button, bool shift);
  560. JSButton* get_button(uint8_t index);
  561. void default_js_buttons(void);
  562. void clear_input_hold();
  563. void read_barometer(void);
  564. void init_rangefinder(void);
  565. void read_rangefinder(void);
  566. bool rangefinder_alt_ok(void);
  567. #if OPTFLOW == ENABLED
  568. void init_optflow();
  569. #endif
  570. void terrain_update();
  571. void terrain_logging();
  572. bool terrain_use();
  573. void init_ardupilot();
  574. void startup_INS_ground();
  575. bool position_ok();
  576. bool ekf_position_ok();
  577. bool optflow_position_ok();
  578. bool should_log(uint32_t mask);
  579. bool start_command(const AP_Mission::Mission_Command& cmd);
  580. bool verify_command(const AP_Mission::Mission_Command& cmd);
  581. bool verify_command_callback(const AP_Mission::Mission_Command& cmd);
  582. bool do_guided(const AP_Mission::Mission_Command& cmd);
  583. void do_nav_wp(const AP_Mission::Mission_Command& cmd);
  584. void do_surface(const AP_Mission::Mission_Command& cmd);
  585. void do_RTL(void);
  586. void do_loiter_unlimited(const AP_Mission::Mission_Command& cmd);
  587. void do_circle(const AP_Mission::Mission_Command& cmd);
  588. void do_loiter_time(const AP_Mission::Mission_Command& cmd);
  589. void do_spline_wp(const AP_Mission::Mission_Command& cmd);
  590. #if NAV_GUIDED == ENABLED
  591. void do_nav_guided_enable(const AP_Mission::Mission_Command& cmd);
  592. void do_guided_limits(const AP_Mission::Mission_Command& cmd);
  593. #endif
  594. void do_nav_delay(const AP_Mission::Mission_Command& cmd);
  595. void do_wait_delay(const AP_Mission::Mission_Command& cmd);
  596. void do_within_distance(const AP_Mission::Mission_Command& cmd);
  597. void do_yaw(const AP_Mission::Mission_Command& cmd);
  598. void do_change_speed(const AP_Mission::Mission_Command& cmd);
  599. void do_set_home(const AP_Mission::Mission_Command& cmd);
  600. void do_roi(const AP_Mission::Mission_Command& cmd);
  601. void do_mount_control(const AP_Mission::Mission_Command& cmd);
  602. bool verify_nav_wp(const AP_Mission::Mission_Command& cmd);
  603. bool verify_surface(const AP_Mission::Mission_Command& cmd);
  604. bool verify_RTL(void);
  605. bool verify_circle(const AP_Mission::Mission_Command& cmd);
  606. bool verify_spline_wp(const AP_Mission::Mission_Command& cmd);
  607. #if NAV_GUIDED == ENABLED
  608. bool verify_nav_guided_enable(const AP_Mission::Mission_Command& cmd);
  609. #endif
  610. bool verify_nav_delay(const AP_Mission::Mission_Command& cmd);
  611. void auto_spline_start(const Location& destination, bool stopped_at_start, AC_WPNav::spline_segment_end_type seg_end_type, const Location& next_destination);
  612. void log_init(void);
  613. void accel_cal_update(void);
  614. void failsafe_leak_check();
  615. void failsafe_internal_pressure_check();
  616. void failsafe_internal_temperature_check();
  617. void failsafe_terrain_act(void);
  618. bool auto_terrain_recover_start(void);
  619. void auto_terrain_recover_run(void);
  620. void translate_wpnav_rp(float &lateral_out, float &forward_out);
  621. void translate_circle_nav_rp(float &lateral_out, float &forward_out);
  622. void translate_pos_control_rp(float &lateral_out, float &forward_out);
  623. bool surface_init(void);
  624. void surface_run();
  625. uint16_t get_pilot_speed_dn();
  626. void convert_old_parameters(void);
  627. bool handle_do_motor_test(mavlink_command_long_t command);
  628. bool init_motor_test();
  629. bool verify_motor_test();
  630. uint32_t last_do_motor_test_fail_ms = 0;
  631. uint32_t last_do_motor_test_ms = 0;
  632. bool control_check_barometer();
  633. enum Failsafe_Action {
  634. Failsafe_Action_None = 0,
  635. Failsafe_Action_Warn = 1,
  636. Failsafe_Action_Disarm = 2,
  637. Failsafe_Action_Surface = 3
  638. };
  639. static constexpr int8_t _failsafe_priorities[] = {
  640. Failsafe_Action_Disarm,
  641. Failsafe_Action_Surface,
  642. Failsafe_Action_Warn,
  643. Failsafe_Action_None,
  644. -1 // the priority list must end with a sentinel of -1
  645. };
  646. static_assert(_failsafe_priorities[ARRAY_SIZE(_failsafe_priorities) - 1] == -1,
  647. "_failsafe_priorities is missing the sentinel");
  648. public:
  649. void mavlink_delay_cb();
  650. void mainloop_failsafe_check();
  651. //------------selfdefine START-----------------------------
  652. control_mode_t control_mode;
  653. mode_reason_t control_mode_reason = MODE_REASON_UNKNOWN;
  654. control_mode_t prev_control_mode;
  655. mode_reason_t prev_control_mode_reason = MODE_REASON_UNKNOWN;
  656. TrackPidClass trackpid;//履带pid控制对象
  657. //------ATOM ------------
  658. float GyroX;//roll角速度
  659. float GyroY;//pitch角速度
  660. float GyroZ;//yaw角速度
  661. float Roll;//roll角度
  662. float Pitch;//pitch角度
  663. float Yaw;//yaw角度
  664. //
  665. uint8_t agl_sec;//机器人俯仰所处于的角度区间
  666. uint8_t agl_act;//机器人当前的姿态动作 水平 or 竖直
  667. //----ATOM---------
  668. unsigned char ucRxBufferATOM[256];//原子九轴接收缓冲区
  669. unsigned char SaberCommandRes[24];//原子配置数据
  670. unsigned char ucRxCnt_atom ;//原子数据接收个数
  671. bool usart_state_atom;//原子九轴通信状态
  672. void updat_Atom(void);//原子九轴数据更新
  673. void GetAngle(void);//计算角度
  674. void uart2_read_Atom(AP_HAL::UARTDriver *uart);//USART2即是temlem2 读取 atom接在temlem2上
  675. void Atom_config(AP_HAL::UARTDriver *uart);//配置
  676. unsigned char Atom_BBC(unsigned char *addr,uint16_t len);//异或校验
  677. float char_to_float(unsigned char u1,unsigned char u2,unsigned char u3,unsigned char u4);
  678. void direction0_90(void);
  679. //------九轴读到的数----------------------
  680. float Roll_Raian;//roll角度弧度
  681. float Pitch_Raian;//pitch角度弧度
  682. float Yaw_Raian;//yaw角度弧度
  683. float Yaw_Angle;//yaw角度,纠正了安装角度
  684. uTof data_floatfromchar;
  685. AP_Motors6DOF motors;
  686. AP_Arming_Sub arming;
  687. AP_Baro barometer;
  688. //灯光
  689. int16_t lights;
  690. //---------USBL ---------------
  691. uint8_t usblpoweroff;
  692. void USBL_PowerSwitch(void);
  693. //-------------------
  694. int16_t yaw_press;//yaw角度给定
  695. void getyaw(void);
  696. void getgain(void);
  697. //---------track----------------
  698. int16_t brushleft;//左毛刷
  699. int16_t brushright;//右毛刷
  700. int16_t motor1_speed_target;//履带电机1 目标PWM
  701. int16_t motor2_speed_target;//履带电机2 目标PWM
  702. float turn_angle;
  703. float track_head_gd;//履带的方位角度给定
  704. uint8_t prepare_state;//水平或者竖直命令
  705. int16_t min_depth;// 最小深度
  706. int16_t max_depth;//最大深度
  707. int16_t autoclean_orgin;//自动洗网开始时,机器人的深度
  708. uint8_t autoclean_step;//自动洗网的阶段:开始 、向上、向下
  709. bool autoclean_flag;//自动洗网状态
  710. bool autoclean_command;//自动洗网状态
  711. bool handclean;
  712. uint8_t clean_mode;
  713. bool clean_bottom_flag;//清洗底网
  714. bool clean_bottom_command;
  715. uint8_t track_motor_arm;//测试履带时的前进2 后退0 停1
  716. PressNetLevel PressLevel;//压力分级枚举类型
  717. float PressLevel_f;//压力分级float类型
  718. float updowmgain;
  719. float delaygain;
  720. int16_t pitch_input_inc;//pitch给定
  721. int16_t lights1;
  722. bool stable_alt_control;
  723. uint16_t continuous_count;
  724. uint16_t continuous_countdec;
  725. uint8_t last_continuous;
  726. float velocity_z_filer;
  727. bool bottom_set;
  728. Matrix3f rotpitch;
  729. Matrix3f rotyaw;
  730. struct telemetry_info_t {
  731. int16_t dutycycle;
  732. int16_t totalcurrent;
  733. int32_t rpm;// 速度
  734. int32_t amphours;
  735. int32_t watthours;
  736. int16_t toalcurrentIn;
  737. int16_t mottremperature;
  738. int16_t mostemperature;
  739. int16_t voltage;
  740. } _telemetry[OUSHENCAN_MAX_NUM_ESCS];
  741. bool sport_init(void);
  742. void sport_run();
  743. void sport_run_alt(void);
  744. bool clean_init(void);
  745. void clean_run();
  746. void track_reset(void);
  747. void autoclean_flag_chose(void);
  748. void clean_net_joystick(void);
  749. void clean_sidenet_auto(void);
  750. void slowly_speed1(int16_t &p1, int16_t p2,int16_t step,int16_t per) ;
  751. void slowly_speed2(int16_t &p1, int16_t p2,int16_t step,int16_t per) ;
  752. float Constrate1(float d1);
  753. void motor_toCan(void);
  754. void clean_sidenet_state(void);
  755. void clean_sidenet_run(void);
  756. void track_pidcontrol(float _targethead,int16_t &_motor1,int16_t &_motor2);
  757. float get_yaw_error(float yaw_heading);
  758. float getgainf(float data);
  759. bool throttle_continuous(float gain1);
  760. float surface_depth_use(void);
  761. float gaindelay(float gain1);
  762. void altcontrol_ff();
  763. void altcontrol();
  764. void ratecontrol();
  765. float bottom_detector(float z);
  766. void bottom_detectorgain(void);
  767. void get_heading_to_ned(Matrix3f &mat_half,Matrix3f mat_body);
  768. float get_pitch_to_ned(Matrix3f mat_body);
  769. float sign_in(float f);
  770. };
  771. extern const AP_HAL::HAL& hal;
  772. extern Sub sub;
  773. extern AP_HAL::AnalogSource* chan_adc;