Firmware
mavlink_receiver.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2012-2015 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
42 #pragma once
43 
44 #include <perf/perf_counter.h>
45 #include <uORB/uORB.h>
46 
47 #include <uORB/topics/airspeed.h>
48 #include <uORB/topics/actuator_armed.h>
49 #include <uORB/topics/actuator_controls.h>
50 #include <uORB/topics/actuator_outputs.h>
51 #include <uORB/topics/battery_status.h>
52 #include <uORB/topics/collision_report.h>
53 #include <uORB/topics/debug_array.h>
54 #include <uORB/topics/debug_key_value.h>
55 #include <uORB/topics/debug_value.h>
56 #include <uORB/topics/debug_vect.h>
57 #include <uORB/topics/distance_sensor.h>
58 #include <uORB/topics/follow_target.h>
59 #include <uORB/topics/gps_inject_data.h>
60 #include <uORB/topics/home_position.h>
61 #include <uORB/topics/landing_target_pose.h>
62 #include <uORB/topics/manual_control_setpoint.h>
63 #include <uORB/topics/obstacle_distance.h>
64 #include <uORB/topics/offboard_control_mode.h>
65 #include <uORB/topics/optical_flow.h>
66 #include <uORB/topics/ping.h>
67 #include <uORB/topics/position_setpoint_triplet.h>
68 #include <uORB/topics/rc_channels.h>
69 #include <uORB/topics/sensor_combined.h>
70 #include <uORB/topics/telemetry_status.h>
71 #include <uORB/topics/transponder_report.h>
72 #include <uORB/topics/vehicle_attitude.h>
73 #include <uORB/topics/vehicle_attitude_setpoint.h>
74 #include <uORB/topics/vehicle_command.h>
75 #include <uORB/topics/vehicle_control_mode.h>
76 #include <uORB/topics/vehicle_gps_position.h>
77 #include <uORB/topics/vehicle_global_position.h>
78 #include <uORB/topics/vehicle_land_detected.h>
79 #include <uORB/topics/vehicle_local_position.h>
80 #include <uORB/topics/vehicle_local_position_setpoint.h>
81 #include <uORB/topics/vehicle_odometry.h>
82 #include <uORB/topics/vehicle_rates_setpoint.h>
83 #include <uORB/topics/vehicle_status.h>
84 #include <uORB/topics/vehicle_trajectory_waypoint.h>
85 
86 #include "mavlink_ftp.h"
87 #include "mavlink_log_handler.h"
88 #include "mavlink_mission.h"
89 #include "mavlink_parameters.h"
90 #include "mavlink_timesync.h"
91 
92 class Mavlink;
93 
95 {
96 public:
100  MavlinkReceiver(Mavlink *parent);
101 
106 
110  void print_status();
111 
115  static void receive_start(pthread_t *thread, Mavlink *parent);
116 
117  static void *start_helper(void *context);
118 
119 private:
120 
121  void acknowledge(uint8_t sysid, uint8_t compid, uint16_t command, uint8_t result);
122 
126  template<class T>
127  void handle_message_command_both(mavlink_message_t *msg, const T &cmd_mavlink,
128  const vehicle_command_s &vehicle_command);
129 
130  void handle_message(mavlink_message_t *msg);
131  void handle_message_adsb_vehicle(mavlink_message_t *msg);
132  void handle_message_att_pos_mocap(mavlink_message_t *msg);
133  void handle_message_battery_status(mavlink_message_t *msg);
134  void handle_message_collision(mavlink_message_t *msg);
135  void handle_message_command_ack(mavlink_message_t *msg);
136  void handle_message_command_int(mavlink_message_t *msg);
137  void handle_message_command_long(mavlink_message_t *msg);
138  void handle_message_debug(mavlink_message_t *msg);
139  void handle_message_debug_float_array(mavlink_message_t *msg);
140  void handle_message_debug_vect(mavlink_message_t *msg);
141  void handle_message_distance_sensor(mavlink_message_t *msg);
142  void handle_message_follow_target(mavlink_message_t *msg);
143  void handle_message_gps_global_origin(mavlink_message_t *msg);
144  void handle_message_gps_rtcm_data(mavlink_message_t *msg);
145  void handle_message_heartbeat(mavlink_message_t *msg);
146  void handle_message_hil_gps(mavlink_message_t *msg);
147  void handle_message_hil_optical_flow(mavlink_message_t *msg);
148  void handle_message_hil_sensor(mavlink_message_t *msg);
149  void handle_message_hil_state_quaternion(mavlink_message_t *msg);
150  void handle_message_landing_target(mavlink_message_t *msg);
151  void handle_message_logging_ack(mavlink_message_t *msg);
152  void handle_message_manual_control(mavlink_message_t *msg);
153  void handle_message_named_value_float(mavlink_message_t *msg);
154  void handle_message_obstacle_distance(mavlink_message_t *msg);
155  void handle_message_odometry(mavlink_message_t *msg);
156  void handle_message_optical_flow_rad(mavlink_message_t *msg);
157  void handle_message_ping(mavlink_message_t *msg);
158  void handle_message_play_tune(mavlink_message_t *msg);
159  void handle_message_radio_status(mavlink_message_t *msg);
160  void handle_message_rc_channels_override(mavlink_message_t *msg);
161  void handle_message_serial_control(mavlink_message_t *msg);
162  void handle_message_set_actuator_control_target(mavlink_message_t *msg);
163  void handle_message_set_attitude_target(mavlink_message_t *msg);
164  void handle_message_set_mode(mavlink_message_t *msg);
165  void handle_message_set_position_target_local_ned(mavlink_message_t *msg);
166  void handle_message_trajectory_representation_waypoints(mavlink_message_t *msg);
167  void handle_message_vision_position_estimate(mavlink_message_t *msg);
168 
169  void *receive_thread(void *arg);
170 
181  int set_message_interval(int msgId, float interval, int data_rate = -1);
182  void get_message_interval(int msgId);
183 
187  switch_pos_t decode_switch_pos(uint16_t buttons, unsigned sw);
188 
192  int decode_switch_pos_n(uint16_t buttons, unsigned sw);
193 
194  bool evaluate_target_ok(int command, int target_system, int target_component);
195 
196  void send_flight_information();
197 
198  void send_storage_information(int storage_id);
199 
200  Mavlink *_mavlink;
201 
202  MavlinkFTP _mavlink_ftp;
203  MavlinkLogHandler _mavlink_log_handler;
204  MavlinkTimesync _mavlink_timesync;
205  MavlinkMissionManager _mission_manager;
206  MavlinkParametersManager _parameters_manager;
207 
208  mavlink_status_t _status{};
209 
210  map_projection_reference_s _hil_local_proj_ref {};
211  offboard_control_mode_s _offboard_control_mode{};
212 
213  vehicle_attitude_s _att {};
214  vehicle_local_position_s _hil_local_pos {};
215  vehicle_land_detected_s _hil_land_detector {};
216  vehicle_control_mode_s _control_mode {};
217 
218  orb_advert_t _accel_pub{nullptr};
219  orb_advert_t _actuator_controls_pubs[4] {nullptr, nullptr, nullptr, nullptr};
220  orb_advert_t _airspeed_pub{nullptr};
221  orb_advert_t _att_sp_pub{nullptr};
222  orb_advert_t _attitude_pub{nullptr};
223  orb_advert_t _baro_pub{nullptr};
224  orb_advert_t _battery_pub{nullptr};
225  orb_advert_t _cmd_pub{nullptr};
226  orb_advert_t _collision_report_pub{nullptr};
227  orb_advert_t _command_ack_pub{nullptr};
228  orb_advert_t _debug_array_pub{nullptr};
229  orb_advert_t _debug_key_value_pub{nullptr};
230  orb_advert_t _debug_value_pub{nullptr};
231  orb_advert_t _debug_vect_pub{nullptr};
232  orb_advert_t _distance_sensor_pub{nullptr};
233  orb_advert_t _flow_distance_sensor_pub{nullptr};
234  orb_advert_t _flow_pub{nullptr};
235  orb_advert_t _follow_target_pub{nullptr};
236  orb_advert_t _global_pos_pub{nullptr};
237  orb_advert_t _gps_inject_data_pub{nullptr};
238  orb_advert_t _gps_pub{nullptr};
239  orb_advert_t _gyro_pub{nullptr};
240  orb_advert_t _hil_distance_sensor_pub{nullptr};
241  orb_advert_t _land_detector_pub{nullptr};
242  orb_advert_t _landing_target_pose_pub{nullptr};
243  orb_advert_t _local_pos_pub{nullptr};
244  orb_advert_t _mag_pub{nullptr};
245  orb_advert_t _manual_pub{nullptr};
246  orb_advert_t _mocap_odometry_pub{nullptr};
247  orb_advert_t _obstacle_distance_pub{nullptr};
248  orb_advert_t _offboard_control_mode_pub{nullptr};
249  orb_advert_t _ping_pub{nullptr};
250  orb_advert_t _pos_sp_triplet_pub{nullptr};
251  orb_advert_t _radio_status_pub{nullptr};
252  orb_advert_t _rates_sp_pub{nullptr};
253  orb_advert_t _rc_pub{nullptr};
254  orb_advert_t _trajectory_waypoint_pub{nullptr};
255  orb_advert_t _transponder_report_pub{nullptr};
256  orb_advert_t _visual_odometry_pub{nullptr};
257 
258  static constexpr int _gps_inject_data_queue_size{6};
259 
260  int _actuator_armed_sub{orb_subscribe(ORB_ID(actuator_armed))};
261  int _control_mode_sub{orb_subscribe(ORB_ID(vehicle_control_mode))};
262  int _vehicle_attitude_sub{orb_subscribe(ORB_ID(vehicle_attitude))};
263 
264  int _orb_class_instance{-1};
265 
266  uint64_t _global_ref_timestamp{0};
267 
268  bool _hil_local_proj_inited{false};
269 
270  float _hil_local_alt0{0.0f};
271 
272  static constexpr unsigned MOM_SWITCH_COUNT{8};
273 
274  uint8_t _mom_switch_pos[MOM_SWITCH_COUNT] {};
275  uint16_t _mom_switch_state{0};
276 
277  param_t _p_bat_emergen_thr{PARAM_INVALID};
278  param_t _p_bat_crit_thr{PARAM_INVALID};
279  param_t _p_bat_low_thr{PARAM_INVALID};
280  param_t _p_flow_rot{PARAM_INVALID};
281  param_t _p_flow_maxr{PARAM_INVALID};
282  param_t _p_flow_minhgt{PARAM_INVALID};
283  param_t _p_flow_maxhgt{PARAM_INVALID};
284 
285  // Disallow copy construction and move assignment.
286  MavlinkReceiver(const MavlinkReceiver &) = delete;
287  MavlinkReceiver operator=(const MavlinkReceiver &) = delete;
288 };
#define PARAM_INVALID
Handle returned when a parameter cannot be found.
Definition: param.h:103
MavlinkReceiver(Mavlink *parent)
Constructor.
Definition: mavlink_receiver.cpp:85
#define ORB_ID(_name)
Generates a pointer to the uORB metadata structure for a given topic.
Definition: uORB.h:87
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
Definition: uORB.h:134
int orb_subscribe(const struct orb_metadata *meta)
Definition: uORB.cpp:75
static void receive_start(pthread_t *thread, Mavlink *parent)
Start the receiver thread.
Definition: mavlink_receiver.cpp:2734
Definition: mavlink_receiver.h:94
Definition: mavlink_timesync.h:97
API for the uORB lightweight object broker.
Definition: mavlink_mission.h:75
~MavlinkReceiver()
Destructor, also kills the mavlinks task.
Definition: mavlink_receiver.cpp:104
void print_status()
Display the mavlink status.
Definition: mavlink_receiver.cpp:2710
MAVLink remote file server. Support FTP like commands using MAVLINK_MSG_ID_FILE_TRANSFER_PROTOCOL mes...
Definition: mavlink_ftp.h:52
Definition: mavlink_log_handler.h:91
Definition: mavlink_parameters.h:56
uint32_t param_t
Parameter handle.
Definition: param.h:98
Performance measuring tools.