Firmware
mavlink_command_sender.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2017 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 
41 #pragma once
42 
43 #include <px4_tasks.h>
44 #include <px4_sem.h>
45 #include <drivers/drv_hrt.h>
46 
47 #include <uORB/topics/vehicle_command.h>
48 #include <uORB/topics/vehicle_command_ack.h>
49 
50 #include "timestamped_list.h"
51 #include "mavlink_bridge_header.h"
52 #include <v2.0/mavlink_types.h>
53 
58 {
59 public:
63  static void initialize();
64 
65  static MavlinkCommandSender &instance();
66 
72  int handle_vehicle_command(const struct vehicle_command_s &command, mavlink_channel_t channel);
73 
78  void check_timeout(mavlink_channel_t channel);
79 
84  void handle_mavlink_command_ack(const mavlink_command_ack_t &ack, uint8_t from_sysid, uint8_t from_compid);
85 
86 private:
87  MavlinkCommandSender() = default;
88 
90 
91  static void lock()
92  {
93  do {} while (px4_sem_wait(&_lock) != 0);
94  }
95 
96  static void unlock()
97  {
98  px4_sem_post(&_lock);
99  }
100 
101  static MavlinkCommandSender *_instance;
102  static px4_sem_t _lock;
103 
104  // There are MAVLINK_COMM_0 to MAVLINK_COMM_3, so it should be 4.
105  static const unsigned MAX_MAVLINK_CHANNEL = 4;
106 
107  typedef struct {
108  mavlink_command_long_t command = {};
109  hrt_abstime timestamp_us = 0;
110  hrt_abstime last_time_sent_us = 0;
111  int8_t num_sent_per_channel[MAX_MAVLINK_CHANNEL] = {-1, -1, -1, -1};
112  } command_item_t;
113 
114  TimestampedList<command_item_t> _commands{3};
115 
116  bool _debug_enabled = false;
117  static constexpr uint8_t RETRIES = 3;
118  static constexpr uint64_t TIMEOUT_US = 500000;
119 
120  /* do not allow copying or assigning this class */
121  MavlinkCommandSender(const MavlinkCommandSender &) = delete;
122  MavlinkCommandSender operator=(const MavlinkCommandSender &) = delete;
123 };
static void initialize()
initialize: call this once on startup (this function is not thread-safe!)
Definition: mavlink_command_sender.cpp:49
void check_timeout(mavlink_channel_t channel)
Check timeouts to verify if an commands need retransmission.
Definition: mavlink_command_sender.cpp:138
High-resolution timer with callouts and timekeeping.
Synchronization primitive: Semaphore.
void handle_mavlink_command_ack(const mavlink_command_ack_t &ack, uint8_t from_sysid, uint8_t from_compid)
Handle mavlink command_ack.
Definition: mavlink_command_sender.cpp:114
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58
int handle_vehicle_command(const struct vehicle_command_s &command, mavlink_channel_t channel)
Send a command on a channel and keep it in a queue for retransmission.
Definition: mavlink_command_sender.cpp:68
Definition: mavlink_command_sender.h:57