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 <cstdint>
36 #include <functional>
37 
38 #include "interface/comm_link.h"
40 
41 #include "nanoprintf.h"
42 
43 namespace rosflight_firmware
44 {
45 
46 class ROSflight;
47 
49 {
50 private:
51  enum StreamId
52  {
55 
57 
63 
70  };
71 
73  {
78  };
79 
80  uint8_t sysid_;
85  bool initialized_ = false;
86  bool connected_ = false;
87 
88 
89  static constexpr int LOG_MSG_SIZE = 50;
91  {
92  public:
93  static constexpr int LOG_BUF_SIZE = 25;
95 
96  struct LogMessage
97  {
100  };
101  void add_message(CommLinkInterface::LogSeverity severity, char msg[LOG_MSG_SIZE]);
102  size_t size() const { return length_; }
103  bool empty() const { return length_ == 0; }
104  bool full() const { return length_ == LOG_BUF_SIZE; }
105  const LogMessage& oldest() const { return buffer_[oldest_]; }
106  void pop();
107 
108  private:
110  size_t oldest_ = 0;
111  size_t newest_ = 0;
112  size_t length_ = 0;
113  };
115 
116  class Stream
117  {
118  public:
119  Stream(uint32_t period_us, std::function<void(void)> send_function);
120 
121  void stream(uint64_t now_us);
122  void set_rate(uint32_t rate_hz);
123 
124  uint32_t period_us_;
125  uint64_t next_time_us_;
126  std::function<void(void)> send_function_;
127  };
128 
129  void update_system_id(uint16_t param_id);
130 
131  void param_request_list_callback(uint8_t target_system) override;
132  void param_request_read_callback(uint8_t target_system, const char* const param_name, int16_t param_index) override;
133  void param_set_int_callback(uint8_t target_system, const char* const param_name, int32_t param_value) override;
134  void param_set_float_callback(uint8_t target_system, const char* const param_name, float param_value) override;
135  void command_callback(CommLinkInterface::Command command) override;
136  void timesync_callback(int64_t tc1, int64_t ts1) override;
138  void aux_command_callback(const CommLinkInterface::AuxCommand &command) override;
139  void attitude_correction_callback(const turbomath::Quaternion &q) override;
140  void heartbeat_callback() override;
141 
142  void send_heartbeat(void);
143  void send_status(void);
144  void send_attitude(void);
145  void send_imu(void);
146  void send_output_raw(void);
147  void send_rc_raw(void);
148  void send_diff_pressure(void);
149  void send_baro(void);
150  void send_sonar(void);
151  void send_mag(void);
152  void send_gnss(void);
153  void send_gnss_raw(void);
154  void send_low_priority(void);
155  void send_error_data(void);
156 
157  // Debugging Utils
158  void send_named_value_int(const char *const name, int32_t value);
159 // void send_named_command_struct(const char *const name, control_t command_struct);
160 
161  void send_next_param(void);
162 
164  Stream(0, [this]{this->send_heartbeat();}),
165  Stream(0, [this]{this->send_status();}),
166  Stream(0, [this]{this->send_attitude();}),
167  Stream(0, [this]{this->send_imu();}),
168  Stream(0, [this]{this->send_diff_pressure();}),
169  Stream(0, [this]{this->send_baro();}),
170  Stream(0, [this]{this->send_sonar();}),
171  Stream(0, [this]{this->send_mag();}),
172  Stream(0, [this]{this->send_output_raw();}),
173  Stream(0, [this]{this->send_gnss();}),
174  Stream(0, [this]{this->send_gnss_raw();}),
175  Stream(0, [this]{this->send_rc_raw();}),
176  Stream(20000, [this]{this->send_low_priority();})
177  };
178 
179  // the time of week stamp for the last sent GNSS message, to prevent re-sending
180  uint32_t last_sent_gnss_tow_ = 0;
182 
183 public:
184 
185  CommManager(ROSflight& rf, CommLinkInterface& comm_link);
186 
187  void init();
188  void param_change_callback(uint16_t param_id) override;
189  void receive(void);
190  void stream();
191  void send_param_value(uint16_t param_id);
192  void set_streaming_rate(uint8_t stream_id, int16_t param_id);
193  void update_status();
194  void log(CommLinkInterface::LogSeverity severity, const char *fmt, ...);
195 
196  void send_parameter_list();
197  void send_named_value_float(const char *const name, float value);
198 };
199 
200 } // namespace rosflight_firmware
201 
202 #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)
void send_param_value(uint16_t param_id)
static constexpr int LOG_MSG_SIZE
Definition: comm_manager.h:89
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:163
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 attitude_correction_callback(const turbomath::Quaternion &q) override
void command_callback(CommLinkInterface::Command command) override
std::function< void(void)> send_function_
Definition: comm_manager.h:126
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 Thu Oct 24 2019 03:17:18