PX4 Firmware
PX4 Autopilot Software http://px4.io
mavlink_receiver.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2012-2019 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 
34 /**
35  * @file mavlink_receiver.h
36  * MAVLink receiver thread
37  *
38  * @author Lorenz Meier <lorenz@px4.io>
39  * @author Anton Babushkin <anton@px4.io>
40  */
41 
42 #pragma once
43 
44 #include "mavlink_ftp.h"
45 #include "mavlink_log_handler.h"
46 #include "mavlink_mission.h"
47 #include "mavlink_parameters.h"
48 #include "mavlink_timesync.h"
49 
50 #include <px4_platform_common/module_params.h>
51 #include <uORB/Publication.hpp>
57 #include <uORB/topics/airspeed.h>
58 #include <uORB/topics/airspeed.h>
65 #include <uORB/topics/debug_vect.h>
70 #include <uORB/topics/input_rc.h>
78 #include <uORB/topics/ping.h>
86 #include <uORB/topics/sensor_mag.h>
103 
104 class Mavlink;
105 
106 class MavlinkReceiver : public ModuleParams
107 {
108 public:
109  MavlinkReceiver(Mavlink *parent);
110  ~MavlinkReceiver() = default;
111 
112  /**
113  * Start the receiver thread
114  */
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 
123  /**
124  * Common method to handle both mavlink command types. T is one of mavlink_command_int_t or mavlink_command_long_t.
125  */
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 
132  void handle_message_adsb_vehicle(mavlink_message_t *msg);
133  void handle_message_att_pos_mocap(mavlink_message_t *msg);
134  void handle_message_battery_status(mavlink_message_t *msg);
135  void handle_message_cellular_status(mavlink_message_t *msg);
136  void handle_message_collision(mavlink_message_t *msg);
137  void handle_message_command_ack(mavlink_message_t *msg);
138  void handle_message_command_int(mavlink_message_t *msg);
139  void handle_message_command_long(mavlink_message_t *msg);
140  void handle_message_debug(mavlink_message_t *msg);
141  void handle_message_debug_float_array(mavlink_message_t *msg);
142  void handle_message_debug_vect(mavlink_message_t *msg);
143  void handle_message_distance_sensor(mavlink_message_t *msg);
144  void handle_message_follow_target(mavlink_message_t *msg);
145  void handle_message_gps_global_origin(mavlink_message_t *msg);
146  void handle_message_gps_rtcm_data(mavlink_message_t *msg);
147  void handle_message_heartbeat(mavlink_message_t *msg);
148  void handle_message_hil_gps(mavlink_message_t *msg);
149  void handle_message_hil_optical_flow(mavlink_message_t *msg);
150  void handle_message_hil_sensor(mavlink_message_t *msg);
151  void handle_message_hil_state_quaternion(mavlink_message_t *msg);
152  void handle_message_landing_target(mavlink_message_t *msg);
153  void handle_message_logging_ack(mavlink_message_t *msg);
154  void handle_message_manual_control(mavlink_message_t *msg);
155  void handle_message_named_value_float(mavlink_message_t *msg);
156  void handle_message_obstacle_distance(mavlink_message_t *msg);
157  void handle_message_odometry(mavlink_message_t *msg);
158  void handle_message_optical_flow_rad(mavlink_message_t *msg);
159  void handle_message_ping(mavlink_message_t *msg);
160  void handle_message_play_tune(mavlink_message_t *msg);
161  void handle_message_radio_status(mavlink_message_t *msg);
162  void handle_message_rc_channels_override(mavlink_message_t *msg);
163  void handle_message_serial_control(mavlink_message_t *msg);
164  void handle_message_set_actuator_control_target(mavlink_message_t *msg);
165  void handle_message_set_attitude_target(mavlink_message_t *msg);
166  void handle_message_set_mode(mavlink_message_t *msg);
167  void handle_message_set_position_target_local_ned(mavlink_message_t *msg);
168  void handle_message_set_position_target_global_int(mavlink_message_t *msg);
169  void handle_message_statustext(mavlink_message_t *msg);
170  void handle_message_trajectory_representation_waypoints(mavlink_message_t *msg);
171  void handle_message_utm_global_position(mavlink_message_t *msg);
172  void handle_message_vision_position_estimate(mavlink_message_t *msg);
173  void handle_message_onboard_computer_status(mavlink_message_t *msg);
174 
175 
176  void Run();
177 
178  /**
179  * Set the interval at which the given message stream is published.
180  * The rate is the number of messages per second.
181  *
182  * @param msgId The ID of the message interval to be set.
183  * @param interval The interval in usec to send the message.
184  * @param data_rate The total link data rate in bytes per second.
185  *
186  * @return PX4_OK on success, PX4_ERROR on fail.
187  */
188  int set_message_interval(int msgId, float interval, int data_rate = -1);
189  void get_message_interval(int msgId);
190 
191  /**
192  * Decode a switch position from a bitfield.
193  */
194  switch_pos_t decode_switch_pos(uint16_t buttons, unsigned sw);
195 
196  /**
197  * Decode a switch position from a bitfield and state.
198  */
199  int decode_switch_pos_n(uint16_t buttons, unsigned sw);
200 
201  bool evaluate_target_ok(int command, int target_system, int target_component);
202 
204  void send_storage_information(int storage_id);
205 
206  void fill_thrust(float *thrust_body_array, uint8_t vehicle_type, float thrust);
207 
208  /**
209  * @brief Updates the battery, optical flow, and flight ID subscribed parameters.
210  */
211  void update_params();
212 
214 
220 
221  mavlink_status_t _status{}; ///< receiver status, used for mavlink_parse_char()
222 
223  // ORB publications
224  uORB::Publication<actuator_controls_s> _actuator_controls_pubs[4] {ORB_ID(actuator_controls_0), ORB_ID(actuator_controls_1), ORB_ID(actuator_controls_2), ORB_ID(actuator_controls_3)};
253 
254  // ORB publications (multi)
265 
266  // ORB publications (queue length > 1)
271 
272  // ORB subscriptions
279 
280  static constexpr unsigned int MOM_SWITCH_COUNT{8};
282  uint16_t _mom_switch_state{0};
283 
285 
287  float _hil_local_alt0{0.0f};
289 
291 
293  (ParamFloat<px4::params::BAT_CRIT_THR>) _param_bat_crit_thr,
294  (ParamFloat<px4::params::BAT_EMERGEN_THR>) _param_bat_emergen_thr,
295  (ParamFloat<px4::params::BAT_LOW_THR>) _param_bat_low_thr,
296  (ParamInt<px4::params::COM_FLIGHT_UUID>) _param_com_flight_uuid,
297  (ParamFloat<px4::params::SENS_FLOW_MAXHGT>) _param_sens_flow_maxhgt,
298  (ParamFloat<px4::params::SENS_FLOW_MAXR>) _param_sens_flow_maxr,
299  (ParamFloat<px4::params::SENS_FLOW_MINHGT>) _param_sens_flow_minhgt,
300  (ParamInt<px4::params::SENS_FLOW_ROT>) _param_sens_flow_rot
301  );
302 
303  // Disallow copy construction and move assignment.
304  MavlinkReceiver(const MavlinkReceiver &) = delete;
305  MavlinkReceiver operator=(const MavlinkReceiver &) = delete;
306 };
static char msg[NUM_MSG][CONFIG_USART1_TXBUFSIZE]
Definition: px4io.c:89
#define ORB_ID(_name)
Generates a pointer to the uORB metadata structure for a given topic.
Definition: uORB.h:87
uint8_t switch_pos_t
Definition: uORB.h:261
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58