comm_manager.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, James Jackson and Daniel Koch, BYU MAGICC Lab
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * * Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef ROSFLIGHT_FIRMWARE_COMM_MANAGER_H
33 #define ROSFLIGHT_FIRMWARE_COMM_MANAGER_H
34 
35 #include "interface/comm_link.h"
37 
38 #include "nanoprintf.h"
39 
40 #include <cstdint>
41 #include <functional>
42 
43 namespace rosflight_firmware
44 {
45 class ROSflight;
46 
48 {
49 private:
50  enum StreamId
51  {
54 
56 
63 
70  };
71 
73  {
78  };
79 
80  uint8_t sysid_;
85  bool initialized_ = false;
86  bool connected_ = false;
87 
88  static constexpr int LOG_MSG_SIZE = 50;
90  {
91  public:
92  static constexpr int LOG_BUF_SIZE = 25;
94 
95  struct LogMessage
96  {
99  };
100  void add_message(CommLinkInterface::LogSeverity severity, char msg[LOG_MSG_SIZE]);
101  size_t size() const { return length_; }
102  bool empty() const { return length_ == 0; }
103  bool full() const { return length_ == LOG_BUF_SIZE; }
104  const LogMessage& oldest() const { return buffer_[oldest_]; }
105  void pop();
106 
107  private:
109  size_t oldest_ = 0;
110  size_t newest_ = 0;
111  size_t length_ = 0;
112  };
114 
115  StateManager::BackupData backup_data_buffer_;
116  bool have_backup_data_ = false;
117 
118  class Stream
119  {
120  public:
121  Stream(uint32_t period_us, std::function<void(void)> send_function);
122 
123  void stream(uint64_t now_us);
124  void set_rate(uint32_t rate_hz);
125 
126  uint32_t period_us_;
127  uint64_t next_time_us_;
128  std::function<void(void)> send_function_;
129  };
130 
131  void update_system_id(uint16_t param_id);
132 
133  void param_request_list_callback(uint8_t target_system) override;
134  void param_request_read_callback(uint8_t target_system, const char* const param_name, int16_t param_index) override;
135  void param_set_int_callback(uint8_t target_system, const char* const param_name, int32_t param_value) override;
136  void param_set_float_callback(uint8_t target_system, const char* const param_name, float param_value) override;
137  void command_callback(CommLinkInterface::Command command) override;
138  void timesync_callback(int64_t tc1, int64_t ts1) override;
140  void aux_command_callback(const CommLinkInterface::AuxCommand& command) override;
141  void external_attitude_callback(const turbomath::Quaternion& q) override;
142  void heartbeat_callback() override;
143 
144  void send_heartbeat(void);
145  void send_status(void);
146  void send_attitude(void);
147  void send_imu(void);
148  void send_output_raw(void);
149  void send_rc_raw(void);
150  void send_diff_pressure(void);
151  void send_baro(void);
152  void send_sonar(void);
153  void send_mag(void);
154  void send_battery_status(void);
155  void send_gnss(void);
156  void send_gnss_full(void);
157  void send_low_priority(void);
158 
159  // Debugging Utils
160  void send_named_value_int(const char* const name, int32_t value);
161  // void send_named_command_struct(const char *const name, control_t command_struct);
162 
163  void send_next_param(void);
164 
166  Stream(0, [this] { this->send_heartbeat(); }), Stream(0, [this] { this->send_status(); }),
167  Stream(0, [this] { this->send_attitude(); }), Stream(0, [this] { this->send_imu(); }),
168  Stream(0, [this] { this->send_diff_pressure(); }), Stream(0, [this] { this->send_baro(); }),
169  Stream(0, [this] { this->send_sonar(); }), Stream(0, [this] { this->send_mag(); }),
170  Stream(0, [this] { this->send_battery_status(); }), Stream(0, [this] { this->send_output_raw(); }),
171  Stream(0, [this] { this->send_gnss(); }), Stream(0, [this] { this->send_gnss_full(); }),
172  Stream(0, [this] { this->send_rc_raw(); }), Stream(20000, [this] { this->send_low_priority(); })};
173 
174  // the time of week stamp for the last sent GNSS message, to prevent re-sending
175  uint32_t last_sent_gnss_tow_ = 0;
177 
178 public:
179  CommManager(ROSflight& rf, CommLinkInterface& comm_link);
180 
181  void init();
182  void param_change_callback(uint16_t param_id) override;
183  void receive(void);
184  void stream();
185  void send_param_value(uint16_t param_id);
186  void set_streaming_rate(uint8_t stream_id, int16_t param_id);
187  void update_status();
188  void log(CommLinkInterface::LogSeverity severity, const char* fmt, ...);
189 
190  void send_parameter_list();
191  void send_named_value_float(const char* const name, float value);
192 
193  void send_backup_data(const StateManager::BackupData& backup_data);
194 };
195 
196 } // namespace rosflight_firmware
197 
198 #endif // ROSFLIGHT_FIRMWARE_COMM_MANAGER_H
void set_streaming_rate(uint8_t stream_id, int16_t param_id)
void param_set_float_callback(uint8_t target_system, const char *const param_name, float param_value) override
void log(CommLinkInterface::LogSeverity severity, const char *fmt,...)
void update_system_id(uint16_t param_id)
void timesync_callback(int64_t tc1, int64_t ts1) override
CommManager(ROSflight &rf, CommLinkInterface &comm_link)
void send_named_value_int(const char *const name, int32_t value)
StateManager::BackupData backup_data_buffer_
Definition: comm_manager.h:115
void send_param_value(uint16_t param_id)
static constexpr int LOG_MSG_SIZE
Definition: comm_manager.h:88
CommLinkInterface & comm_link_
Definition: comm_manager.h:83
void param_change_callback(uint16_t param_id) override
void aux_command_callback(const CommLinkInterface::AuxCommand &command) override
Stream streams_[STREAM_COUNT]
Definition: comm_manager.h:165
void send_backup_data(const StateManager::BackupData &backup_data)
void param_set_int_callback(uint8_t target_system, const char *const param_name, int32_t param_value) override
void param_request_list_callback(uint8_t target_system) override
void offboard_control_callback(const CommLinkInterface::OffboardControl &control) override
void external_attitude_callback(const turbomath::Quaternion &q) override
void command_callback(CommLinkInterface::Command command) override
std::function< void(void)> send_function_
Definition: comm_manager.h:128
void param_request_read_callback(uint8_t target_system, const char *const param_name, int16_t param_index) override
void send_named_value_float(const char *const name, float value)
void add_message(CommLinkInterface::LogSeverity severity, char msg[LOG_MSG_SIZE])


rosflight_firmware
Author(s): Daniel Koch , James Jackson
autogenerated on Fri Oct 9 2020 03:17:15