GCS_Mavlink.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  1. #include "Sub.h"
  2. #include "GCS_Mavlink.h"
  3. /*
  4. * !!NOTE!!
  5. *
  6. * the use of NOINLINE separate functions for each message type avoids
  7. * a compiler bug in gcc that would cause it to use far more stack
  8. * space than is needed. Without the NOINLINE we use the sum of the
  9. * stack needed for each message type. Please be careful to follow the
  10. * pattern below when adding any new messages
  11. */
  12. MAV_TYPE GCS_Sub::frame_type() const
  13. {
  14. return MAV_TYPE_SUBMARINE;
  15. }
  16. MAV_MODE GCS_MAVLINK_Sub::base_mode() const
  17. {
  18. uint8_t _base_mode = MAV_MODE_FLAG_STABILIZE_ENABLED;
  19. // work out the base_mode. This value is not very useful
  20. // for APM, but we calculate it as best we can so a generic
  21. // MAVLink enabled ground station can work out something about
  22. // what the MAV is up to. The actual bit values are highly
  23. // ambiguous for most of the APM flight modes. In practice, you
  24. // only get useful information from the custom_mode, which maps to
  25. // the APM flight mode and has a well defined meaning in the
  26. // ArduPlane documentation
  27. switch (sub.control_mode) {
  28. case AUTO:
  29. case GUIDED:
  30. case CIRCLE:
  31. case POSHOLD:
  32. _base_mode |= MAV_MODE_FLAG_GUIDED_ENABLED;
  33. // note that MAV_MODE_FLAG_AUTO_ENABLED does not match what
  34. // APM does in any mode, as that is defined as "system finds its own goal
  35. // positions", which APM does not currently do
  36. break;
  37. default:
  38. break;
  39. }
  40. // all modes except INITIALISING have some form of manual
  41. // override if stick mixing is enabled
  42. _base_mode |= MAV_MODE_FLAG_MANUAL_INPUT_ENABLED;
  43. if (sub.motors.armed()) {
  44. _base_mode |= MAV_MODE_FLAG_SAFETY_ARMED;
  45. }
  46. // indicate we have set a custom mode
  47. _base_mode |= MAV_MODE_FLAG_CUSTOM_MODE_ENABLED;
  48. return (MAV_MODE)_base_mode;
  49. }
  50. uint32_t GCS_Sub::custom_mode() const
  51. {
  52. return sub.control_mode;
  53. }
  54. MAV_STATE GCS_MAVLINK_Sub::system_status() const
  55. {
  56. // set system as critical if any failsafe have triggered
  57. if (sub.any_failsafe_triggered()) {
  58. return MAV_STATE_CRITICAL;
  59. }
  60. if (sub.motors.armed()) {
  61. return MAV_STATE_ACTIVE;
  62. }
  63. return MAV_STATE_STANDBY;
  64. }
  65. void GCS_MAVLINK_Sub::send_nav_controller_output() const
  66. {
  67. const Vector3f &targets = sub.attitude_control.get_att_target_euler_cd();
  68. mavlink_msg_nav_controller_output_send(
  69. chan,
  70. targets.x * 1.0e-2f,
  71. targets.y * 1.0e-2f,
  72. targets.z * 1.0e-2f,
  73. sub.wp_nav.get_wp_bearing_to_destination() * 1.0e-2f,
  74. MIN(sub.wp_nav.get_wp_distance_to_destination() * 1.0e-2f, UINT16_MAX),
  75. sub.pos_control.get_alt_error() * 1.0e-2f,
  76. 0,
  77. 0);
  78. }
  79. int16_t GCS_MAVLINK_Sub::vfr_hud_throttle() const
  80. {
  81. return (int16_t)(sub.motors.get_throttle() * 100);
  82. }
  83. // Work around to get temperature sensor data out
  84. void GCS_MAVLINK_Sub::send_scaled_pressure3()
  85. {
  86. if (!sub.celsius.healthy()) {
  87. return;
  88. }
  89. mavlink_msg_scaled_pressure3_send(
  90. chan,
  91. AP_HAL::millis(),
  92. 0,
  93. 0,
  94. sub.celsius.temperature() * 100);
  95. }
  96. bool GCS_MAVLINK_Sub::send_info()
  97. {
  98. // Just do this all at once, hopefully the hard-wire telemetry requirement means this is ok
  99. // Name is char[10]
  100. CHECK_PAYLOAD_SIZE(NAMED_VALUE_FLOAT);
  101. send_named_float("CamTilt",
  102. 1 - (SRV_Channels::get_output_norm(SRV_Channel::k_mount_tilt) / 2.0f + 0.5f));
  103. CHECK_PAYLOAD_SIZE(NAMED_VALUE_FLOAT);
  104. send_named_float("CamPan",
  105. 1 - (SRV_Channels::get_output_norm(SRV_Channel::k_mount_pan) / 2.0f + 0.5f));
  106. CHECK_PAYLOAD_SIZE(NAMED_VALUE_FLOAT);
  107. send_named_float("TetherTrn",
  108. sub.quarter_turn_count/4);
  109. CHECK_PAYLOAD_SIZE(NAMED_VALUE_FLOAT);
  110. send_named_float("Lights1",
  111. SRV_Channels::get_output_norm(SRV_Channel::k_rcin9) / 2.0f + 0.5f);
  112. CHECK_PAYLOAD_SIZE(NAMED_VALUE_FLOAT);
  113. send_named_float("Lights2",
  114. SRV_Channels::get_output_norm(SRV_Channel::k_rcin10) / 2.0f + 0.5f);
  115. CHECK_PAYLOAD_SIZE(NAMED_VALUE_FLOAT);
  116. send_named_float("PilotGain", sub.gain);
  117. CHECK_PAYLOAD_SIZE(NAMED_VALUE_FLOAT);
  118. send_named_float("InputHold", sub.input_hold_engaged);
  119. return true;
  120. }
  121. /*
  122. send PID tuning message
  123. */
  124. void GCS_MAVLINK_Sub::send_pid_tuning()
  125. {
  126. const Parameters &g = sub.g;
  127. AP_AHRS &ahrs = AP::ahrs();
  128. AC_AttitudeControl_Sub &attitude_control = sub.attitude_control;
  129. const Vector3f &gyro = ahrs.get_gyro();
  130. if (g.gcs_pid_mask & 1) {
  131. const AP_Logger::PID_Info &pid_info = attitude_control.get_rate_roll_pid().get_pid_info();
  132. mavlink_msg_pid_tuning_send(chan, PID_TUNING_ROLL,
  133. pid_info.target*0.01f,
  134. degrees(gyro.x),
  135. pid_info.FF*0.01f,
  136. pid_info.P*0.01f,
  137. pid_info.I*0.01f,
  138. pid_info.D*0.01f);
  139. if (!HAVE_PAYLOAD_SPACE(chan, PID_TUNING)) {
  140. return;
  141. }
  142. }
  143. if (g.gcs_pid_mask & 2) {
  144. const AP_Logger::PID_Info &pid_info = attitude_control.get_rate_pitch_pid().get_pid_info();
  145. mavlink_msg_pid_tuning_send(chan, PID_TUNING_PITCH,
  146. pid_info.target*0.01f,
  147. degrees(gyro.y),
  148. pid_info.FF*0.01f,
  149. pid_info.P*0.01f,
  150. pid_info.I*0.01f,
  151. pid_info.D*0.01f);
  152. if (!HAVE_PAYLOAD_SPACE(chan, PID_TUNING)) {
  153. return;
  154. }
  155. }
  156. if (g.gcs_pid_mask & 4) {
  157. const AP_Logger::PID_Info &pid_info = attitude_control.get_rate_yaw_pid().get_pid_info();
  158. mavlink_msg_pid_tuning_send(chan, PID_TUNING_YAW,
  159. pid_info.target*0.01f,
  160. degrees(gyro.z),
  161. pid_info.FF*0.01f,
  162. pid_info.P*0.01f,
  163. pid_info.I*0.01f,
  164. pid_info.D*0.01f);
  165. if (!HAVE_PAYLOAD_SPACE(chan, PID_TUNING)) {
  166. return;
  167. }
  168. }
  169. if (g.gcs_pid_mask & 8) {
  170. const AP_Logger::PID_Info &pid_info = sub.pos_control.get_accel_z_pid().get_pid_info();
  171. mavlink_msg_pid_tuning_send(chan, PID_TUNING_ACCZ,
  172. pid_info.target*0.01f,
  173. -(ahrs.get_accel_ef_blended().z + GRAVITY_MSS),
  174. pid_info.FF*0.01f,
  175. pid_info.P*0.01f,
  176. pid_info.I*0.01f,
  177. pid_info.D*0.01f);
  178. if (!HAVE_PAYLOAD_SPACE(chan, PID_TUNING)) {
  179. return;
  180. }
  181. }
  182. }
  183. uint8_t GCS_MAVLINK_Sub::sysid_my_gcs() const
  184. {
  185. return sub.g.sysid_my_gcs;
  186. }
  187. bool GCS_Sub::vehicle_initialised() const {
  188. return sub.ap.initialised;
  189. }
  190. // try to send a message, return false if it won't fit in the serial tx buffer
  191. bool GCS_MAVLINK_Sub::try_send_message(enum ap_message id)
  192. {
  193. switch (id) {
  194. case MSG_NAMED_FLOAT:
  195. send_info();
  196. break;
  197. case MSG_TERRAIN:
  198. #if AP_TERRAIN_AVAILABLE && AC_TERRAIN
  199. CHECK_PAYLOAD_SIZE(TERRAIN_REQUEST);
  200. sub.terrain.send_request(chan);
  201. #endif
  202. break;
  203. default:
  204. return GCS_MAVLINK::try_send_message(id);
  205. }
  206. return true;
  207. }
  208. const AP_Param::GroupInfo GCS_MAVLINK_Parameters::var_info[] = {
  209. // @Param: RAW_SENS
  210. // @DisplayName: Raw sensor stream rate
  211. // @Description: Stream rate of RAW_IMU, SCALED_IMU2, SCALED_PRESSURE, and SENSOR_OFFSETS to ground station
  212. // @Units: Hz
  213. // @Range: 0 10
  214. // @Increment: 1
  215. // @User: Advanced
  216. AP_GROUPINFO("RAW_SENS", 0, GCS_MAVLINK_Parameters, streamRates[GCS_MAVLINK::STREAM_RAW_SENSORS], 0),
  217. // @Param: EXT_STAT
  218. // @DisplayName: Extended status stream rate to ground station
  219. // @Description: Stream rate of SYS_STATUS, MEMINFO, MISSION_CURRENT, GPS_RAW_INT, NAV_CONTROLLER_OUTPUT, and LIMITS_STATUS to ground station
  220. // @Units: Hz
  221. // @Range: 0 10
  222. // @Increment: 1
  223. // @User: Advanced
  224. AP_GROUPINFO("EXT_STAT", 1, GCS_MAVLINK_Parameters, streamRates[GCS_MAVLINK::STREAM_EXTENDED_STATUS], 0),
  225. // @Param: RC_CHAN
  226. // @DisplayName: RC Channel stream rate to ground station
  227. // @Description: Stream rate of SERVO_OUTPUT_RAW and RC_CHANNELS_RAW to ground station
  228. // @Units: Hz
  229. // @Range: 0 10
  230. // @Increment: 1
  231. // @User: Advanced
  232. AP_GROUPINFO("RC_CHAN", 2, GCS_MAVLINK_Parameters, streamRates[GCS_MAVLINK::STREAM_RC_CHANNELS], 0),
  233. // @Param: POSITION
  234. // @DisplayName: Position stream rate to ground station
  235. // @Description: Stream rate of GLOBAL_POSITION_INT to ground station
  236. // @Units: Hz
  237. // @Range: 0 10
  238. // @Increment: 1
  239. // @User: Advanced
  240. AP_GROUPINFO("POSITION", 4, GCS_MAVLINK_Parameters, streamRates[GCS_MAVLINK::STREAM_POSITION], 0),
  241. // @Param: EXTRA1
  242. // @DisplayName: Extra data type 1 stream rate to ground station
  243. // @Description: Stream rate of ATTITUDE and SIMSTATE (SITL only) to ground station
  244. // @Units: Hz
  245. // @Range: 0 10
  246. // @Increment: 1
  247. // @User: Advanced
  248. AP_GROUPINFO("EXTRA1", 5, GCS_MAVLINK_Parameters, streamRates[GCS_MAVLINK::STREAM_EXTRA1], 0),
  249. // @Param: EXTRA2
  250. // @DisplayName: Extra data type 2 stream rate to ground station
  251. // @Description: Stream rate of VFR_HUD to ground station
  252. // @Units: Hz
  253. // @Range: 0 10
  254. // @Increment: 1
  255. // @User: Advanced
  256. AP_GROUPINFO("EXTRA2", 6, GCS_MAVLINK_Parameters, streamRates[GCS_MAVLINK::STREAM_EXTRA2], 0),
  257. // @Param: EXTRA3
  258. // @DisplayName: Extra data type 3 stream rate to ground station
  259. // @Description: Stream rate of AHRS, HWSTATUS, and SYSTEM_TIME to ground station
  260. // @Units: Hz
  261. // @Range: 0 10
  262. // @Increment: 1
  263. // @User: Advanced
  264. AP_GROUPINFO("EXTRA3", 7, GCS_MAVLINK_Parameters, streamRates[GCS_MAVLINK::STREAM_EXTRA3], 0),
  265. // @Param: PARAMS
  266. // @DisplayName: Parameter stream rate to ground station
  267. // @Description: Stream rate of PARAM_VALUE to ground station
  268. // @Units: Hz
  269. // @Range: 0 10
  270. // @Increment: 1
  271. // @User: Advanced
  272. AP_GROUPINFO("PARAMS", 8, GCS_MAVLINK_Parameters, streamRates[GCS_MAVLINK::STREAM_PARAMS], 0),
  273. AP_GROUPEND
  274. };
  275. /*
  276. static const ap_message STREAM_RAW_SENSORS_msgs[] = {
  277. MSG_RAW_IMU,
  278. MSG_SCALED_IMU2,
  279. MSG_SCALED_IMU3,
  280. MSG_SCALED_PRESSURE,
  281. MSG_SCALED_PRESSURE2,
  282. MSG_SCALED_PRESSURE3,
  283. MSG_SENSOR_OFFSETS
  284. };
  285. static const ap_message STREAM_EXTENDED_STATUS_msgs[] = {
  286. MSG_SYS_STATUS,
  287. MSG_POWER_STATUS,
  288. MSG_MEMINFO,
  289. MSG_CURRENT_WAYPOINT,
  290. MSG_GPS_RAW,
  291. MSG_GPS_RTK,
  292. MSG_GPS2_RAW,
  293. MSG_GPS2_RTK,
  294. MSG_NAV_CONTROLLER_OUTPUT,
  295. MSG_FENCE_STATUS,
  296. MSG_NAMED_FLOAT
  297. };
  298. static const ap_message STREAM_POSITION_msgs[] = {
  299. MSG_LOCATION,
  300. MSG_LOCAL_POSITION
  301. };
  302. static const ap_message STREAM_RC_CHANNELS_msgs[] = {
  303. MSG_SERVO_OUTPUT_RAW,
  304. MSG_RC_CHANNELS,
  305. MSG_RC_CHANNELS_RAW, // only sent on a mavlink1 connection
  306. };
  307. static const ap_message STREAM_EXTRA1_msgs[] = {
  308. MSG_ATTITUDE,
  309. MSG_SIMSTATE,
  310. MSG_AHRS2,
  311. MSG_AHRS3,
  312. MSG_PID_TUNING
  313. };
  314. static const ap_message STREAM_EXTRA2_msgs[] = {
  315. MSG_VFR_HUD
  316. };
  317. static const ap_message STREAM_EXTRA3_msgs[] = {
  318. MSG_AHRS,
  319. MSG_HWSTATUS,
  320. MSG_SYSTEM_TIME,
  321. MSG_RANGEFINDER,
  322. MSG_DISTANCE_SENSOR,
  323. #if AP_TERRAIN_AVAILABLE && AC_TERRAIN
  324. MSG_TERRAIN,
  325. #endif
  326. MSG_BATTERY2,
  327. MSG_BATTERY_STATUS,
  328. MSG_MOUNT_STATUS,
  329. MSG_OPTICAL_FLOW,
  330. MSG_GIMBAL_REPORT,
  331. MSG_MAG_CAL_REPORT,
  332. MSG_MAG_CAL_PROGRESS,
  333. MSG_EKF_STATUS_REPORT,
  334. MSG_VIBRATION,
  335. #if RPM_ENABLED == ENABLED
  336. MSG_RPM,
  337. #endif
  338. MSG_ESC_TELEMETRY,
  339. };
  340. static const ap_message STREAM_PARAMS_msgs[] = {
  341. MSG_NEXT_PARAM
  342. };*/
  343. static const ap_message STREAM_RAW_SENSORS_msgs[] = {
  344. MSG_RAW_IMU,
  345. MSG_SCALED_IMU2,
  346. MSG_SCALED_IMU3,
  347. MSG_SCALED_PRESSURE,
  348. MSG_SCALED_PRESSURE2,
  349. MSG_SCALED_PRESSURE3,
  350. MSG_SENSOR_OFFSETS
  351. };
  352. static const ap_message STREAM_EXTENDED_STATUS_msgs[] = {
  353. MSG_SYS_STATUS,
  354. // MSG_POWER_STATUS,
  355. MSG_MEMINFO,
  356. // MSG_CURRENT_WAYPOINT,
  357. // MSG_GPS_RAW,
  358. // MSG_GPS_RTK,
  359. // MSG_GPS2_RAW,
  360. // MSG_GPS2_RTK,
  361. // MSG_NAV_CONTROLLER_OUTPUT,
  362. // MSG_FENCE_STATUS,
  363. //MSG_NAMED_FLOAT
  364. };
  365. static const ap_message STREAM_POSITION_msgs[] = {
  366. // MSG_LOCATION,
  367. // MSG_LOCAL_POSITION
  368. };
  369. static const ap_message STREAM_RC_CHANNELS_msgs[] = {
  370. MSG_SERVO_OUTPUT_RAW,
  371. MSG_RC_CHANNELS,
  372. MSG_RC_CHANNELS_RAW, // only sent on a mavlink1 connection
  373. };
  374. static const ap_message STREAM_EXTRA1_msgs[] = {
  375. MSG_ATTITUDE,
  376. //MSG_SIMSTATE,
  377. //MSG_AHRS2,
  378. MSG_AHRS3,
  379. MSG_PID_TUNING
  380. };
  381. static const ap_message STREAM_EXTRA2_msgs[] = {
  382. //MSG_VFR_HUD
  383. //MSG_ROTATION_MATRIX_ARRAY,
  384. MSG_ROV_STATE_MONITORING,
  385. //MSG_SET_SLAVE_PARAMETER,
  386. MSG_MOTOR_SPEED,
  387. //MSG_HV_REG_GET
  388. };
  389. static const ap_message STREAM_EXTRA3_msgs[] = {
  390. //MSG_AHRS,
  391. MSG_HWSTATUS,
  392. MSG_SYSTEM_TIME,
  393. // MSG_RANGEFINDER,
  394. // MSG_DISTANCE_SENSOR,
  395. #if AP_TERRAIN_AVAILABLE && AC_TERRAIN
  396. // MSG_TERRAIN,
  397. #endif
  398. // MSG_BATTERY2,
  399. // MSG_BATTERY_STATUS,
  400. // MSG_MOUNT_STATUS,
  401. // MSG_OPTICAL_FLOW,
  402. // MSG_GIMBAL_REPORT,
  403. MSG_MAG_CAL_REPORT,
  404. MSG_MAG_CAL_PROGRESS,
  405. MSG_EKF_STATUS_REPORT,
  406. //MSG_VIBRATION,
  407. #if RPM_ENABLED == ENABLED
  408. //MSG_RPM,
  409. #endif
  410. MSG_ESC_TELEMETRY,
  411. };
  412. static const ap_message STREAM_PARAMS_msgs[] = {
  413. MSG_NEXT_PARAM
  414. };
  415. const struct GCS_MAVLINK::stream_entries GCS_MAVLINK::all_stream_entries[] = {
  416. MAV_STREAM_ENTRY(STREAM_RAW_SENSORS),
  417. MAV_STREAM_ENTRY(STREAM_EXTENDED_STATUS),
  418. MAV_STREAM_ENTRY(STREAM_POSITION),
  419. MAV_STREAM_ENTRY(STREAM_RC_CHANNELS),
  420. MAV_STREAM_ENTRY(STREAM_EXTRA1),
  421. MAV_STREAM_ENTRY(STREAM_EXTRA2),
  422. MAV_STREAM_ENTRY(STREAM_EXTRA3),
  423. MAV_STREAM_ENTRY(STREAM_PARAMS),
  424. MAV_STREAM_TERMINATOR // must have this at end of stream_entries
  425. };
  426. bool GCS_MAVLINK_Sub::handle_guided_request(AP_Mission::Mission_Command &cmd)
  427. {
  428. return sub.do_guided(cmd);
  429. }
  430. void GCS_MAVLINK_Sub::handle_change_alt_request(AP_Mission::Mission_Command &cmd)
  431. {
  432. // add home alt if needed
  433. if (cmd.content.location.relative_alt) {
  434. cmd.content.location.alt += sub.ahrs.get_home().alt;
  435. }
  436. // To-Do: update target altitude for loiter or waypoint controller depending upon nav mode
  437. }
  438. MAV_RESULT GCS_MAVLINK_Sub::_handle_command_preflight_calibration_baro()
  439. {
  440. if (sub.motors.armed()) {
  441. gcs().send_text(MAV_SEVERITY_INFO, "Disarm before calibration.");
  442. return MAV_RESULT_FAILED;
  443. }
  444. if (!sub.control_check_barometer()) {
  445. return MAV_RESULT_FAILED;
  446. }
  447. AP::baro().calibrate(true);
  448. return MAV_RESULT_ACCEPTED;
  449. }
  450. MAV_RESULT GCS_MAVLINK_Sub::_handle_command_preflight_calibration(const mavlink_command_long_t &packet)
  451. {
  452. if (is_equal(packet.param6,1.0f)) {
  453. // compassmot calibration
  454. //result = sub.mavlink_compassmot(chan);
  455. gcs().send_text(MAV_SEVERITY_INFO, "#CompassMot calibration not supported");
  456. return MAV_RESULT_UNSUPPORTED;
  457. }
  458. return GCS_MAVLINK::_handle_command_preflight_calibration(packet);
  459. }
  460. MAV_RESULT GCS_MAVLINK_Sub::handle_command_do_set_roi(const Location &roi_loc)
  461. {
  462. if (!roi_loc.check_latlng()) {
  463. return MAV_RESULT_FAILED;
  464. }
  465. sub.set_auto_yaw_roi(roi_loc);
  466. return MAV_RESULT_ACCEPTED;
  467. }
  468. bool GCS_MAVLINK_Sub::set_home_to_current_location(bool lock) {
  469. return sub.set_home_to_current_location(lock);
  470. }
  471. bool GCS_MAVLINK_Sub::set_home(const Location& loc, bool lock) {
  472. return sub.set_home(loc, lock);
  473. }
  474. MAV_RESULT GCS_MAVLINK_Sub::handle_command_long_packet(const mavlink_command_long_t &packet)
  475. {
  476. switch (packet.command) {
  477. case MAV_CMD_NAV_LOITER_UNLIM:
  478. if (!sub.set_mode(POSHOLD, MODE_REASON_GCS_COMMAND)) {
  479. return MAV_RESULT_FAILED;
  480. }
  481. return MAV_RESULT_ACCEPTED;
  482. case MAV_CMD_NAV_LAND:
  483. if (!sub.set_mode(SURFACE, MODE_REASON_GCS_COMMAND)) {
  484. return MAV_RESULT_FAILED;
  485. }
  486. return MAV_RESULT_ACCEPTED;
  487. case MAV_CMD_CONDITION_YAW:
  488. // param1 : target angle [0-360]
  489. // param2 : speed during change [deg per second]
  490. // param3 : direction (-1:ccw, +1:cw)
  491. // param4 : relative offset (1) or absolute angle (0)
  492. if ((packet.param1 >= 0.0f) &&
  493. (packet.param1 <= 360.0f) &&
  494. (is_zero(packet.param4) || is_equal(packet.param4,1.0f))) {
  495. sub.set_auto_yaw_look_at_heading(packet.param1, packet.param2, (int8_t)packet.param3, (uint8_t)packet.param4);
  496. return MAV_RESULT_ACCEPTED;
  497. }
  498. return MAV_RESULT_FAILED;
  499. case MAV_CMD_DO_CHANGE_SPEED:
  500. // param1 : unused
  501. // param2 : new speed in m/s
  502. // param3 : unused
  503. // param4 : unused
  504. if (packet.param2 > 0.0f) {
  505. sub.wp_nav.set_speed_xy(packet.param2 * 100.0f);
  506. return MAV_RESULT_ACCEPTED;
  507. }
  508. return MAV_RESULT_FAILED;
  509. case MAV_CMD_MISSION_START:
  510. if (sub.motors.armed() && sub.set_mode(AUTO, MODE_REASON_GCS_COMMAND)) {
  511. return MAV_RESULT_ACCEPTED;
  512. }
  513. return MAV_RESULT_FAILED;
  514. case MAV_CMD_DO_MOTOR_TEST:
  515. // param1 : motor sequence number (a number from 1 to max number of motors on the vehicle)
  516. // param2 : throttle type (0=throttle percentage, 1=PWM, 2=pilot throttle channel pass-through. See MOTOR_TEST_THROTTLE_TYPE enum)
  517. // param3 : throttle (range depends upon param2)
  518. // param4 : timeout (in seconds)
  519. if (!sub.handle_do_motor_test(packet)) {
  520. return MAV_RESULT_FAILED;
  521. }
  522. return MAV_RESULT_ACCEPTED;
  523. default:
  524. return GCS_MAVLINK::handle_command_long_packet(packet);
  525. }
  526. }
  527. mavlink_data64_t rov_message2;
  528. mavlink_motor_speed_t mav_motor_speed;
  529. mavlink_motor_speed_t mav_motor_speed_back;
  530. mavlink_rov_control_t rov_control{0,60,0};
  531. mavlink_rov_state_monitoring_t rov_message;
  532. //rov_message.hvMotorMod = 0xFFFF;
  533. mavlink_set_slave_parameter_t set_stm32_param{0xFF,0xFF,0,0,0,0,0};
  534. mavlink_set_slave_parameter_t get_stm32_param{0xFF,0xF1,0,0,0,0,0};
  535. mavlink_hv_reg_set_t hv_reg_set = {0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  536. mavlink_hv_reg_get_t hv_reg_get;
  537. void GCS_MAVLINK_Sub::handleMessage(const mavlink_message_t &msg)
  538. {
  539. switch (msg.msgid) {
  540. case MAVLINK_MSG_ID_SET_SLAVE_PARAMETER:{//ok
  541. mavlink_set_slave_parameter_t packet;
  542. mavlink_msg_set_slave_parameter_decode(&msg,&packet);
  543. set_stm32_param = packet;
  544. //gcs().send_text(MAV_SEVERITY_WARNING, "MAVLINK_MSG_ID_SET_SLAVE_PARAMETER .");
  545. break;
  546. }
  547. case MAVLINK_MSG_ID_ROV_STATE_MONITORING:{
  548. mavlink_rov_state_monitoring_t packet;
  549. mavlink_msg_rov_state_monitoring_decode(&msg, &packet);
  550. rov_message = packet;
  551. //gcs().send_text(MAV_SEVERITY_INFO, "MAVLINK_MSG_ID_ROV_STATE_MONITORING .");
  552. break;
  553. }
  554. case MAVLINK_MSG_ID_MOTOR_SPEED:{//ok
  555. mavlink_motor_speed_t packet;
  556. mavlink_msg_motor_speed_decode(&msg, &packet);
  557. mav_motor_speed = packet;
  558. break;
  559. }
  560. case MAVLINK_MSG_ID_ROV_CONTROL:{//ok
  561. mavlink_rov_control_t packet;
  562. mavlink_msg_rov_control_decode(&msg, &packet);
  563. rov_control = packet;
  564. break;
  565. }
  566. case MAVLINK_MSG_ID_HV_REG_SET:{
  567. mavlink_hv_reg_set_t packet;
  568. mavlink_msg_hv_reg_set_decode(&msg, &packet);
  569. hv_reg_set = packet;
  570. //gcs().send_text(MAV_SEVERITY_WARNING, "MAVLINK_MSG_ID_HV_REG_SET .");
  571. break;
  572. }
  573. case MAVLINK_MSG_ID_HEARTBEAT: { // MAV ID: 0
  574. // We keep track of the last time we received a heartbeat from our GCS for failsafe purposes
  575. if (msg.sysid != sub.g.sysid_my_gcs) {
  576. break;
  577. }
  578. sub.failsafe.last_heartbeat_ms = AP_HAL::millis();
  579. break;
  580. }
  581. case MAVLINK_MSG_ID_MANUAL_CONTROL: { // MAV ID: 69
  582. if (msg.sysid != sub.g.sysid_my_gcs) {
  583. break; // Only accept control from our gcs
  584. }
  585. mavlink_manual_control_t packet;
  586. mavlink_msg_manual_control_decode(&msg, &packet);
  587. if (packet.target != sub.g.sysid_this_mav) {
  588. break; // only accept control aimed at us
  589. }
  590. sub.transform_manual_control_to_rc_override(packet.x,packet.y,packet.z,packet.r,packet.buttons);
  591. sub.failsafe.last_pilot_input_ms = AP_HAL::millis();
  592. // a RC override message is considered to be a 'heartbeat' from the ground station for failsafe purposes
  593. sub.failsafe.last_heartbeat_ms = AP_HAL::millis();
  594. break;
  595. }
  596. case MAVLINK_MSG_ID_SET_ATTITUDE_TARGET: { // MAV ID: 82
  597. // decode packet
  598. mavlink_set_attitude_target_t packet;
  599. mavlink_msg_set_attitude_target_decode(&msg, &packet);
  600. // ensure type_mask specifies to use attitude
  601. // the thrust can be used from the altitude hold
  602. if (packet.type_mask & (1<<6)) {
  603. sub.set_attitude_target_no_gps = {AP_HAL::millis(), packet};
  604. }
  605. // ensure type_mask specifies to use attitude and thrust
  606. if ((packet.type_mask & ((1<<7)|(1<<6))) != 0) {
  607. break;
  608. }
  609. // convert thrust to climb rate
  610. packet.thrust = constrain_float(packet.thrust, 0.0f, 1.0f);
  611. float climb_rate_cms = 0.0f;
  612. if (is_equal(packet.thrust, 0.5f)) {
  613. climb_rate_cms = 0.0f;
  614. } else if (packet.thrust > 0.5f) {
  615. // climb at up to WPNAV_SPEED_UP
  616. climb_rate_cms = (packet.thrust - 0.5f) * 2.0f * sub.wp_nav.get_default_speed_up();
  617. } else {
  618. // descend at up to WPNAV_SPEED_DN
  619. climb_rate_cms = (packet.thrust - 0.5f) * 2.0f * fabsf(sub.wp_nav.get_default_speed_down());
  620. }
  621. sub.guided_set_angle(Quaternion(packet.q[0],packet.q[1],packet.q[2],packet.q[3]), climb_rate_cms);
  622. break;
  623. }
  624. case MAVLINK_MSG_ID_SET_POSITION_TARGET_LOCAL_NED: { // MAV ID: 84
  625. // decode packet
  626. mavlink_set_position_target_local_ned_t packet;
  627. mavlink_msg_set_position_target_local_ned_decode(&msg, &packet);
  628. // exit if vehicle is not in Guided mode or Auto-Guided mode
  629. if ((sub.control_mode != GUIDED) && !(sub.control_mode == AUTO && sub.auto_mode == Auto_NavGuided)) {
  630. break;
  631. }
  632. // check for supported coordinate frames
  633. if (packet.coordinate_frame != MAV_FRAME_LOCAL_NED &&
  634. packet.coordinate_frame != MAV_FRAME_LOCAL_OFFSET_NED &&
  635. packet.coordinate_frame != MAV_FRAME_BODY_NED &&
  636. packet.coordinate_frame != MAV_FRAME_BODY_OFFSET_NED) {
  637. break;
  638. }
  639. bool pos_ignore = packet.type_mask & MAVLINK_SET_POS_TYPE_MASK_POS_IGNORE;
  640. bool vel_ignore = packet.type_mask & MAVLINK_SET_POS_TYPE_MASK_VEL_IGNORE;
  641. bool acc_ignore = packet.type_mask & MAVLINK_SET_POS_TYPE_MASK_ACC_IGNORE;
  642. /*
  643. * for future use:
  644. * bool force = packet.type_mask & MAVLINK_SET_POS_TYPE_MASK_FORCE;
  645. * bool yaw_ignore = packet.type_mask & MAVLINK_SET_POS_TYPE_MASK_YAW_IGNORE;
  646. * bool yaw_rate_ignore = packet.type_mask & MAVLINK_SET_POS_TYPE_MASK_YAW_RATE_IGNORE;
  647. */
  648. // prepare position
  649. Vector3f pos_vector;
  650. if (!pos_ignore) {
  651. // convert to cm
  652. pos_vector = Vector3f(packet.x * 100.0f, packet.y * 100.0f, -packet.z * 100.0f);
  653. // rotate to body-frame if necessary
  654. if (packet.coordinate_frame == MAV_FRAME_BODY_NED ||
  655. packet.coordinate_frame == MAV_FRAME_BODY_OFFSET_NED) {
  656. sub.rotate_body_frame_to_NE(pos_vector.x, pos_vector.y);
  657. }
  658. // add body offset if necessary
  659. if (packet.coordinate_frame == MAV_FRAME_LOCAL_OFFSET_NED ||
  660. packet.coordinate_frame == MAV_FRAME_BODY_NED ||
  661. packet.coordinate_frame == MAV_FRAME_BODY_OFFSET_NED) {
  662. pos_vector += sub.inertial_nav.get_position();
  663. } else {
  664. // convert from alt-above-home to alt-above-ekf-origin
  665. pos_vector.z = sub.pv_alt_above_origin(pos_vector.z);
  666. }
  667. }
  668. // prepare velocity
  669. Vector3f vel_vector;
  670. if (!vel_ignore) {
  671. // convert to cm
  672. vel_vector = Vector3f(packet.vx * 100.0f, packet.vy * 100.0f, -packet.vz * 100.0f);
  673. // rotate to body-frame if necessary
  674. if (packet.coordinate_frame == MAV_FRAME_BODY_NED || packet.coordinate_frame == MAV_FRAME_BODY_OFFSET_NED) {
  675. sub.rotate_body_frame_to_NE(vel_vector.x, vel_vector.y);
  676. }
  677. }
  678. // send request
  679. if (!pos_ignore && !vel_ignore && acc_ignore) {
  680. sub.guided_set_destination_posvel(pos_vector, vel_vector);
  681. } else if (pos_ignore && !vel_ignore && acc_ignore) {
  682. sub.guided_set_velocity(vel_vector);
  683. } else if (!pos_ignore && vel_ignore && acc_ignore) {
  684. sub.guided_set_destination(pos_vector);
  685. }
  686. break;
  687. }
  688. case MAVLINK_MSG_ID_SET_POSITION_TARGET_GLOBAL_INT: { // MAV ID: 86
  689. // decode packet
  690. mavlink_set_position_target_global_int_t packet;
  691. mavlink_msg_set_position_target_global_int_decode(&msg, &packet);
  692. // exit if vehicle is not in Guided, Auto-Guided, or Depth Hold modes
  693. if ((sub.control_mode != GUIDED)
  694. && !(sub.control_mode == AUTO && sub.auto_mode == Auto_NavGuided)
  695. && !(sub.control_mode == ALT_HOLD)) {
  696. break;
  697. }
  698. bool pos_ignore = packet.type_mask & MAVLINK_SET_POS_TYPE_MASK_POS_IGNORE;
  699. bool vel_ignore = packet.type_mask & MAVLINK_SET_POS_TYPE_MASK_VEL_IGNORE;
  700. bool acc_ignore = packet.type_mask & MAVLINK_SET_POS_TYPE_MASK_ACC_IGNORE;
  701. /*
  702. * for future use:
  703. * bool force = packet.type_mask & MAVLINK_SET_POS_TYPE_MASK_FORCE;
  704. * bool yaw_ignore = packet.type_mask & MAVLINK_SET_POS_TYPE_MASK_YAW_IGNORE;
  705. * bool yaw_rate_ignore = packet.type_mask & MAVLINK_SET_POS_TYPE_MASK_YAW_RATE_IGNORE;
  706. */
  707. if (!pos_ignore && sub.control_mode == ALT_HOLD) { // Control only target depth when in ALT_HOLD
  708. sub.pos_control.set_alt_target(packet.alt*100);
  709. break;
  710. }
  711. Vector3f pos_neu_cm; // position (North, East, Up coordinates) in centimeters
  712. if (!pos_ignore) {
  713. // sanity check location
  714. if (!check_latlng(packet.lat_int, packet.lon_int)) {
  715. break;
  716. }
  717. Location::AltFrame frame;
  718. if (!mavlink_coordinate_frame_to_location_alt_frame((MAV_FRAME)packet.coordinate_frame, frame)) {
  719. // unknown coordinate frame
  720. break;
  721. }
  722. const Location loc{
  723. packet.lat_int,
  724. packet.lon_int,
  725. int32_t(packet.alt*100),
  726. frame,
  727. };
  728. if (!loc.get_vector_from_origin_NEU(pos_neu_cm)) {
  729. break;
  730. }
  731. }
  732. if (!pos_ignore && !vel_ignore && acc_ignore) {
  733. sub.guided_set_destination_posvel(pos_neu_cm, Vector3f(packet.vx * 100.0f, packet.vy * 100.0f, -packet.vz * 100.0f));
  734. } else if (pos_ignore && !vel_ignore && acc_ignore) {
  735. sub.guided_set_velocity(Vector3f(packet.vx * 100.0f, packet.vy * 100.0f, -packet.vz * 100.0f));
  736. } else if (!pos_ignore && vel_ignore && acc_ignore) {
  737. sub.guided_set_destination(pos_neu_cm);
  738. }
  739. break;
  740. }
  741. case MAVLINK_MSG_ID_DISTANCE_SENSOR: {
  742. sub.rangefinder.handle_msg(msg);
  743. break;
  744. }
  745. case MAVLINK_MSG_ID_TERRAIN_DATA:
  746. case MAVLINK_MSG_ID_TERRAIN_CHECK:
  747. #if AP_TERRAIN_AVAILABLE && AC_TERRAIN
  748. sub.terrain.handle_data(chan, msg);
  749. #endif
  750. break;
  751. case MAVLINK_MSG_ID_SET_HOME_POSITION: {
  752. mavlink_set_home_position_t packet;
  753. mavlink_msg_set_home_position_decode(&msg, &packet);
  754. if ((packet.latitude == 0) && (packet.longitude == 0) && (packet.altitude == 0)) {
  755. if (!sub.set_home_to_current_location(true)) {
  756. // ignore this failure
  757. }
  758. } else {
  759. Location new_home_loc;
  760. new_home_loc.lat = packet.latitude;
  761. new_home_loc.lng = packet.longitude;
  762. new_home_loc.alt = packet.altitude / 10;
  763. if (sub.far_from_EKF_origin(new_home_loc)) {
  764. break;
  765. }
  766. if (!sub.set_home(new_home_loc, true)) {
  767. // silently ignored
  768. }
  769. }
  770. break;
  771. }
  772. // This adds support for leak detectors in a separate enclosure
  773. // connected to a mavlink enabled subsystem
  774. case MAVLINK_MSG_ID_SYS_STATUS: {
  775. uint32_t MAV_SENSOR_WATER = 0x20000000;
  776. mavlink_sys_status_t packet;
  777. mavlink_msg_sys_status_decode(&msg, &packet);
  778. if ((packet.onboard_control_sensors_enabled & MAV_SENSOR_WATER) && !(packet.onboard_control_sensors_health & MAV_SENSOR_WATER)) {
  779. sub.leak_detector.set_detect();
  780. }
  781. }
  782. break;
  783. default:
  784. handle_common_message(msg);
  785. break;
  786. } // end switch
  787. } // end handle mavlink
  788. uint64_t GCS_MAVLINK_Sub::capabilities() const
  789. {
  790. return (MAV_PROTOCOL_CAPABILITY_MISSION_FLOAT |
  791. MAV_PROTOCOL_CAPABILITY_PARAM_FLOAT |
  792. MAV_PROTOCOL_CAPABILITY_MISSION_INT |
  793. MAV_PROTOCOL_CAPABILITY_SET_POSITION_TARGET_LOCAL_NED |
  794. MAV_PROTOCOL_CAPABILITY_SET_POSITION_TARGET_GLOBAL_INT |
  795. MAV_PROTOCOL_CAPABILITY_FLIGHT_TERMINATION |
  796. #if AP_TERRAIN_AVAILABLE && AC_TERRAIN
  797. (sub.terrain.enabled() ? MAV_PROTOCOL_CAPABILITY_TERRAIN : 0) |
  798. #endif
  799. MAV_PROTOCOL_CAPABILITY_SET_ATTITUDE_TARGET |
  800. GCS_MAVLINK::capabilities()
  801. );
  802. }
  803. // a RC override message is considered to be a 'heartbeat' from the ground station for failsafe purposes
  804. void GCS_MAVLINK_Sub::handle_rc_channels_override(const mavlink_message_t &msg)
  805. {
  806. sub.failsafe.last_heartbeat_ms = AP_HAL::millis();
  807. GCS_MAVLINK::handle_rc_channels_override(msg);
  808. }
  809. /*
  810. * a delay() callback that processes MAVLink packets. We set this as the
  811. * callback in long running library initialisation routines to allow
  812. * MAVLink to process packets while waiting for the initialisation to
  813. * complete
  814. */
  815. void Sub::mavlink_delay_cb()
  816. {
  817. static uint32_t last_1hz, last_50hz, last_5s;
  818. logger.EnableWrites(false);
  819. uint32_t tnow = AP_HAL::millis();
  820. if (tnow - last_1hz > 1000) {
  821. last_1hz = tnow;
  822. gcs().send_message(MSG_HEARTBEAT);
  823. gcs().send_message(MSG_SYS_STATUS);
  824. }
  825. if (tnow - last_50hz > 20) {
  826. last_50hz = tnow;
  827. gcs().update_receive();
  828. gcs().update_send();
  829. notify.update();
  830. }
  831. if (tnow - last_5s > 5000) {
  832. last_5s = tnow;
  833. gcs().send_text(MAV_SEVERITY_INFO, "Initialising APM");
  834. }
  835. logger.EnableWrites(true);
  836. }
  837. MAV_RESULT GCS_MAVLINK_Sub::handle_flight_termination(const mavlink_command_long_t &packet) {
  838. if (packet.param1 > 0.5f) {
  839. sub.arming.disarm();
  840. return MAV_RESULT_ACCEPTED;
  841. }
  842. return MAV_RESULT_FAILED;
  843. }
  844. bool GCS_MAVLINK_Sub::set_mode(uint8_t mode)
  845. {
  846. return sub.set_mode((control_mode_t)mode, MODE_REASON_GCS_COMMAND);
  847. }
  848. int32_t GCS_MAVLINK_Sub::global_position_int_alt() const {
  849. if (!sub.ap.depth_sensor_present) {
  850. return 0;
  851. }
  852. return GCS_MAVLINK::global_position_int_alt();
  853. }
  854. int32_t GCS_MAVLINK_Sub::global_position_int_relative_alt() const {
  855. if (!sub.ap.depth_sensor_present) {
  856. return 0;
  857. }
  858. return GCS_MAVLINK::global_position_int_relative_alt();
  859. }
  860. // dummy method to avoid linking AFS
  861. bool AP_AdvancedFailsafe::gcs_terminate(bool should_terminate, const char *reason) { return false; }