PX4 Firmware
PX4 Autopilot Software http://px4.io
uavcan_servers.hpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2014 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 #pragma once
35 
36 #include <px4_platform_common/px4_config.h>
37 #include "uavcan_driver.hpp"
39 #include <lib/perf/perf_counter.h>
40 #include <uORB/Publication.hpp>
44 
45 #include <uavcan/node/sub_node.hpp>
46 #include <uavcan/protocol/node_status_monitor.hpp>
47 #include <uavcan/protocol/param/GetSet.hpp>
48 #include <uavcan/protocol/param/ExecuteOpcode.hpp>
49 
50 #include <uavcan/protocol/dynamic_node_id_server/centralized.hpp>
51 #include <uavcan/protocol/node_info_retriever.hpp>
52 #include <uavcan_posix/basic_file_server_backend.hpp>
53 #include <uavcan/protocol/firmware_update_trigger.hpp>
54 #include <uavcan/protocol/file_server.hpp>
55 #include <uavcan_posix/dynamic_node_id_server/file_event_tracer.hpp>
56 #include <uavcan_posix/dynamic_node_id_server/file_storage_backend.hpp>
57 #include <uavcan_posix/firmware_version_checker.hpp>
58 #include <uavcan/equipment/esc/RawCommand.hpp>
59 #include <uavcan/equipment/indication/BeepCommand.hpp>
60 #include <uavcan/protocol/enumeration/Begin.hpp>
61 #include <uavcan/protocol/enumeration/Indication.hpp>
62 
63 #include "uavcan_module.hpp"
65 
66 /**
67  * @file uavcan_servers.hpp
68  *
69  * Defines basic functionality of UAVCAN node.
70  *
71  * @author Pavel Kirienko <pavel.kirienko@gmail.com>
72  * @author David Sidrane <david_s5@nscdg.com>
73  */
74 
75 /**
76  * A UAVCAN Server Sub node.
77  */
79 {
80  static constexpr unsigned NumIfaces = 1; // UAVCAN_STM32_NUM_IFACES
81 
82  static constexpr unsigned StackSize = 6000;
83  static constexpr unsigned Priority = 120;
84 
85  static constexpr unsigned VirtualIfaceBlockAllocationQuota = 80;
86 
87  static constexpr float BeepFrequencyGenericIndication = 1000.0F;
88  static constexpr float BeepFrequencySuccess = 2000.0F;
89  static constexpr float BeepFrequencyError = 100.0F;
90 
91 public:
92  UavcanServers(uavcan::INode &main_node);
93 
94  virtual ~UavcanServers();
95 
96  static int start(uavcan::INode &main_node);
97  static int stop();
98 
99  uavcan::SubNode<> &get_node() { return _subnode; }
100 
101  static UavcanServers *instance() { return _instance; }
102 
103  /*
104  * Set the main node's pointer to to the injector
105  * This is a work around as main_node.getDispatcher().remeveRxFrameListener();
106  * would require a dynamic cast and rtti is not enabled.
107  */
108  void attachITxQueueInjector(ITxQueueInjector **injector) {*injector = &_vdriver;}
109 
110  void requestCheckAllNodesFirmwareAndUpdate() { _check_fw = true; }
111 
112  bool guessIfAllDynamicNodesAreAllocated() { return _server_instance.guessIfAllDynamicNodesAreAllocated(); }
113 
114 private:
115  pthread_t _subnode_thread{-1};
116  pthread_mutex_t _subnode_mutex{};
117  volatile bool _subnode_thread_should_exit{false};
118 
119  int init();
120 
121  pthread_addr_t run(pthread_addr_t);
122 
123  static UavcanServers *_instance; ///< singleton pointer
124 
126 
127  uavcan::SubNode<> _subnode;
128  uavcan::INode &_main_node;
129 
130  uavcan_posix::dynamic_node_id_server::FileEventTracer _tracer;
131  uavcan_posix::dynamic_node_id_server::FileStorageBackend _storage_backend;
132  uavcan_posix::FirmwareVersionChecker _fw_version_checker;
133  uavcan::dynamic_node_id_server::CentralizedServer _server_instance; ///< server singleton pointer
134  uavcan_posix::BasicFileServerBackend _fileserver_backend;
135  uavcan::NodeInfoRetriever _node_info_retriever;
136  uavcan::FirmwareUpdateTrigger _fw_upgrade_trigger;
137  uavcan::BasicFileServer _fw_server;
138 
139  /*
140  * The MAVLink parameter bridge needs to know the maximum parameter index
141  * of each node so that clients can determine when parameter listings have
142  * finished. We do that by querying a node's entire parameter set whenever
143  * a parameter message is received for a node with a zero _param_count,
144  * and storing the count here. If a node doesn't respond, or doesn't have
145  * any parameters, its count will stay at zero and we'll try to query the
146  * set again next time.
147  *
148  * The node's UAVCAN ID is used as the index into the _param_counts array.
149  */
150  uint8_t _param_counts[128] = {};
151  bool _count_in_progress = false;
152  uint8_t _count_index = 0;
153 
154  bool _param_in_progress = false;
155  uint8_t _param_index = 0;
156  bool _param_list_in_progress = false;
157  bool _param_list_all_nodes = false;
158  uint8_t _param_list_node_id = 1;
159 
160  uint32_t _param_dirty_bitmap[4] = {};
161  uint8_t _param_save_opcode = 0;
162 
163  bool _cmd_in_progress = false;
164 
165  // uORB topic handle for MAVLink parameter responses
166  uORB::Publication<uavcan_parameter_value_s> _param_response_pub{ORB_ID(uavcan_parameter_value)};
167  uORB::PublicationQueued<vehicle_command_ack_s> _command_ack_pub{ORB_ID(vehicle_command_ack)};
168 
169  typedef uavcan::MethodBinder<UavcanServers *,
170  void (UavcanServers::*)(const uavcan::ServiceCallResult<uavcan::protocol::param::GetSet> &)> GetSetCallback;
171  typedef uavcan::MethodBinder<UavcanServers *,
172  void (UavcanServers::*)(const uavcan::ServiceCallResult<uavcan::protocol::param::ExecuteOpcode> &)>
174  typedef uavcan::MethodBinder<UavcanServers *,
175  void (UavcanServers::*)(const uavcan::ServiceCallResult<uavcan::protocol::RestartNode> &)> RestartNodeCallback;
176 
177  void cb_getset(const uavcan::ServiceCallResult<uavcan::protocol::param::GetSet> &result);
178  void cb_count(const uavcan::ServiceCallResult<uavcan::protocol::param::GetSet> &result);
179  void cb_opcode(const uavcan::ServiceCallResult<uavcan::protocol::param::ExecuteOpcode> &result);
180  void cb_restart(const uavcan::ServiceCallResult<uavcan::protocol::RestartNode> &result);
181 
182  uavcan::ServiceClient<uavcan::protocol::param::GetSet, GetSetCallback> _param_getset_client;
183  uavcan::ServiceClient<uavcan::protocol::param::ExecuteOpcode, ExecuteOpcodeCallback> _param_opcode_client;
184  uavcan::ServiceClient<uavcan::protocol::RestartNode, RestartNodeCallback> _param_restartnode_client;
185  void param_count(uavcan::NodeID node_id);
186  void param_opcode(uavcan::NodeID node_id);
187 
188  uint8_t get_next_active_node_id(uint8_t base);
189  uint8_t get_next_dirty_node_id(uint8_t base);
190  void set_node_params_dirty(uint8_t node_id) { _param_dirty_bitmap[node_id >> 5] |= 1 << (node_id & 31); }
191  void clear_node_params_dirty(uint8_t node_id) { _param_dirty_bitmap[node_id >> 5] &= ~(1 << (node_id & 31)); }
192  bool are_node_params_dirty(uint8_t node_id) const { return bool((_param_dirty_bitmap[node_id >> 5] >> (node_id & 31)) & 1); }
193 
194  void beep(float frequency);
195 
196  bool _mutex_inited = false;
197  volatile bool _check_fw = false;
198 
199  // ESC enumeration
200  bool _esc_enumeration_active = false;
201  uint8_t _esc_enumeration_ids[uavcan::equipment::esc::RawCommand::FieldTypes::cmd::MaxSize];
202  uint8_t _esc_enumeration_index = 0;
203  uint8_t _esc_count = 0;
204 
205  typedef uavcan::MethodBinder<UavcanServers *,
206  void (UavcanServers::*)(const uavcan::ServiceCallResult<uavcan::protocol::enumeration::Begin> &)>
208  typedef uavcan::MethodBinder<UavcanServers *,
209  void (UavcanServers::*)(const uavcan::ReceivedDataStructure<uavcan::protocol::enumeration::Indication>&)>
211  void cb_enumeration_begin(const uavcan::ServiceCallResult<uavcan::protocol::enumeration::Begin> &result);
212  void cb_enumeration_indication(const uavcan::ReceivedDataStructure<uavcan::protocol::enumeration::Indication> &msg);
213  void cb_enumeration_getset(const uavcan::ServiceCallResult<uavcan::protocol::param::GetSet> &result);
214  void cb_enumeration_save(const uavcan::ServiceCallResult<uavcan::protocol::param::ExecuteOpcode> &result);
215 
216  uavcan::Publisher<uavcan::equipment::indication::BeepCommand> _beep_pub;
217  uavcan::Subscriber<uavcan::protocol::enumeration::Indication, EnumerationIndicationCallback>
219  uavcan::ServiceClient<uavcan::protocol::enumeration::Begin, EnumerationBeginCallback> _enumeration_client;
220  uavcan::ServiceClient<uavcan::protocol::param::GetSet, GetSetCallback> _enumeration_getset_client;
221  uavcan::ServiceClient<uavcan::protocol::param::ExecuteOpcode, ExecuteOpcodeCallback> _enumeration_save_client;
222 
223  void unpackFwFromROMFS(const char *sd_path, const char *romfs_path);
224  int copyFw(const char *dst, const char *src);
225 };
void clear_node_params_dirty(uint8_t node_id)
uavcan::ServiceClient< uavcan::protocol::param::ExecuteOpcode, ExecuteOpcodeCallback > _param_opcode_client
uavcan::ServiceClient< uavcan::protocol::param::GetSet, GetSetCallback > _enumeration_getset_client
uavcan::ServiceClient< uavcan::protocol::param::GetSet, GetSetCallback > _param_getset_client
VirtualCanDriver _vdriver
void set_node_params_dirty(uint8_t node_id)
Definition: I2C.hpp:51
uavcan::FirmwareUpdateTrigger _fw_upgrade_trigger
bool guessIfAllDynamicNodesAreAllocated()
uavcan::INode & _main_node
uavcan::NodeInfoRetriever _node_info_retriever
static void stop()
Definition: dataman.cpp:1491
uavcan::MethodBinder< UavcanServers *, void(UavcanServers::*)(const uavcan::ServiceCallResult< uavcan::protocol::param::ExecuteOpcode > &)> ExecuteOpcodeCallback
void attachITxQueueInjector(ITxQueueInjector **injector)
__EXPORT unsigned param_count(void)
Return the total number of parameters.
Definition: parameters.cpp:382
uavcan::MethodBinder< UavcanServers *, void(UavcanServers::*)(const uavcan::ReceivedDataStructure< uavcan::protocol::enumeration::Indication > &)> EnumerationIndicationCallback
bool are_node_params_dirty(uint8_t node_id) const
uavcan::MethodBinder< UavcanServers *, void(UavcanServers::*)(const uavcan::ServiceCallResult< uavcan::protocol::RestartNode > &)> RestartNodeCallback
static char msg[NUM_MSG][CONFIG_USART1_TXBUFSIZE]
Definition: px4io.c:89
Definitions for the generic base classes in the device framework.
#define ORB_ID(_name)
Generates a pointer to the uORB metadata structure for a given topic.
Definition: uORB.h:87
uavcan_posix::dynamic_node_id_server::FileStorageBackend _storage_backend
void init()
Activates/configures the hardware registers.
static UavcanServers * _instance
singleton pointer
uavcan_posix::dynamic_node_id_server::FileEventTracer _tracer
A UAVCAN Server Sub node.
uavcan_posix::BasicFileServerBackend _fileserver_backend
uavcan::dynamic_node_id_server::CentralizedServer _server_instance
server singleton pointer
void requestCheckAllNodesFirmwareAndUpdate()
uavcan::SubNode & get_node()
uavcan::ServiceClient< uavcan::protocol::enumeration::Begin, EnumerationBeginCallback > _enumeration_client
uavcan::ServiceClient< uavcan::protocol::param::ExecuteOpcode, ExecuteOpcodeCallback > _enumeration_save_client
static int start()
Definition: dataman.cpp:1452
uavcan::SubNode _subnode
uavcan::ServiceClient< uavcan::protocol::RestartNode, RestartNodeCallback > _param_restartnode_client
uavcan::BasicFileServer _fw_server
uavcan::MethodBinder< UavcanServers *, void(UavcanServers::*)(const uavcan::ServiceCallResult< uavcan::protocol::enumeration::Begin > &)> EnumerationBeginCallback
uavcan_posix::FirmwareVersionChecker _fw_version_checker
Objects of this class are owned by the sub-node thread.
static UavcanServers * instance()
uavcan::MethodBinder< UavcanServers *, void(UavcanServers::*)(const uavcan::ServiceCallResult< uavcan::protocol::param::GetSet > &)> GetSetCallback
Performance measuring tools.
uavcan::Subscriber< uavcan::protocol::enumeration::Indication, EnumerationIndicationCallback > _enumeration_indication_sub
uavcan::Publisher< uavcan::equipment::indication::BeepCommand > _beep_pub
This interface defines one method that will be called by the main node thread periodically in order t...