Sub.h 30 KB

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