rosflight_io.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Daniel Koch and James Jackson, BYU MAGICC Lab.
3  * 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 are met:
7  *
8  * * Redistributions of source code must retain the above copyright notice, this
9  * list of conditions and the following disclaimer.
10  *
11  * * Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * * Neither the name of the copyright holder nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
37 #ifndef ROSFLIGHT_IO_MAVROSFLIGHT_ROS_H
38 #define ROSFLIGHT_IO_MAVROSFLIGHT_ROS_H
39 
40 #include <map>
41 #include <string>
42 
43 #include <ros/ros.h>
44 
45 #include <geometry_msgs/Quaternion.h>
46 #include <geometry_msgs/TwistStamped.h>
47 #include <std_msgs/Bool.h>
48 #include <std_msgs/Float32.h>
49 #include <std_msgs/Int32.h>
50 #include <std_msgs/String.h>
51 
52 #include <sensor_msgs/FluidPressure.h>
53 #include <sensor_msgs/Imu.h>
54 #include <sensor_msgs/MagneticField.h>
55 #include <sensor_msgs/NavSatFix.h>
56 #include <sensor_msgs/Range.h>
57 #include <sensor_msgs/Temperature.h>
58 #include <sensor_msgs/TimeReference.h>
59 
60 #include <std_srvs/Trigger.h>
61 
62 #include <rosflight_msgs/Airspeed.h>
63 #include <rosflight_msgs/Attitude.h>
64 #include <rosflight_msgs/AuxCommand.h>
65 #include <rosflight_msgs/Barometer.h>
66 #include <rosflight_msgs/BatteryStatus.h>
67 #include <rosflight_msgs/Command.h>
68 #include <rosflight_msgs/Error.h>
69 #include <rosflight_msgs/GNSS.h>
70 #include <rosflight_msgs/GNSSFull.h>
71 #include <rosflight_msgs/OutputRaw.h>
72 #include <rosflight_msgs/RCRaw.h>
73 #include <rosflight_msgs/Status.h>
74 
75 #include <rosflight_msgs/ParamFile.h>
76 #include <rosflight_msgs/ParamGet.h>
77 #include <rosflight_msgs/ParamSet.h>
78 
83 #include <rosflight/ros_logger.h>
84 #include <rosflight/ros_time.h>
85 #include <rosflight/ros_timer.h>
86 
87 #include <geometry_msgs/Quaternion.h>
88 
89 namespace rosflight_io
90 {
92 {
93 public:
94  rosflightIO();
95  ~rosflightIO();
96 
97  virtual void handle_mavlink_message(const mavlink_message_t &msg);
98 
99  virtual void on_new_param_received(std::string name, double value);
100  virtual void on_param_value_updated(std::string name, double value);
101  virtual void on_params_saved_change(bool unsaved_changes);
102 
103  static constexpr float HEARTBEAT_PERIOD = 1; // Time between heartbeat messages
104  static constexpr float VERSION_PERIOD = 10; // Time between version requests
105  static constexpr float PARAMETER_PERIOD = 3; // Time between parameter requests
106 
107 private:
108  // handle mavlink messages
109  void handle_heartbeat_msg(const mavlink_message_t &msg);
110  void handle_status_msg(const mavlink_message_t &msg);
111  void handle_command_ack_msg(const mavlink_message_t &msg);
112  void handle_statustext_msg(const mavlink_message_t &msg);
113  void handle_attitude_quaternion_msg(const mavlink_message_t &msg);
114  void handle_small_imu_msg(const mavlink_message_t &msg);
115  void handle_rosflight_output_raw_msg(const mavlink_message_t &msg);
116  void handle_rc_channels_raw_msg(const mavlink_message_t &msg);
117  void handle_diff_pressure_msg(const mavlink_message_t &msg);
118  void handle_small_baro_msg(const mavlink_message_t &msg);
119  void handle_small_mag_msg(const mavlink_message_t &msg);
120  void handle_rosflight_gnss_msg(const mavlink_message_t &msg);
121  void handle_rosflight_gnss_full_msg(const mavlink_message_t &msg);
122  void handle_named_value_int_msg(const mavlink_message_t &msg);
123  void handle_named_value_float_msg(const mavlink_message_t &msg);
124  void handle_named_command_struct_msg(const mavlink_message_t &msg);
125  void handle_small_range_msg(const mavlink_message_t &msg);
126  std::string get_major_minor_version(const std::string &version);
127  void handle_version_msg(const mavlink_message_t &msg);
128  void handle_hard_error_msg(const mavlink_message_t &msg);
129  void handle_battery_status_msg(const mavlink_message_t &msg);
130 
131  // ROS message callbacks
132  void commandCallback(rosflight_msgs::Command::ConstPtr msg);
133  void auxCommandCallback(rosflight_msgs::AuxCommand::ConstPtr msg);
134  void externalAttitudeCallback(geometry_msgs::Quaternion::ConstPtr msg);
135 
136  // ROS service callbacks
137  bool paramGetSrvCallback(rosflight_msgs::ParamGet::Request &req, rosflight_msgs::ParamGet::Response &res);
138  bool paramSetSrvCallback(rosflight_msgs::ParamSet::Request &req, rosflight_msgs::ParamSet::Response &res);
139  bool paramWriteSrvCallback(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res);
140  bool paramSaveToFileCallback(rosflight_msgs::ParamFile::Request &req, rosflight_msgs::ParamFile::Response &res);
141  bool paramLoadFromFileCallback(rosflight_msgs::ParamFile::Request &req, rosflight_msgs::ParamFile::Response &res);
142  bool calibrateImuBiasSrvCallback(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res);
143  bool calibrateRCTrimSrvCallback(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res);
144  bool calibrateBaroSrvCallback(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res);
145  bool calibrateAirspeedSrvCallback(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res);
146  bool rebootSrvCallback(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res);
147  bool rebootToBootloaderSrvCallback(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res);
148 
149  // timer callbacks
150  void paramTimerCallback(const ros::TimerEvent &e);
151  void versionTimerCallback(const ros::TimerEvent &e);
153 
154  // helpers
155  void request_version();
156  void send_heartbeat();
157  void check_error_code(uint8_t current, uint8_t previous, ROSFLIGHT_ERROR_CODE code, std::string name);
158  ros::Time fcu_time_to_ros_time(std::chrono::nanoseconds fcu_time);
159 
160  template <class T>
161  inline T saturate(T value, T min, T max)
162  {
163  return value < min ? min : (value > max ? max : value);
164  }
165 
167 
171 
194  std::map<std::string, ros::Publisher> named_value_int_pubs_;
195  std::map<std::string, ros::Publisher> named_value_float_pubs_;
196  std::map<std::string, ros::Publisher> named_command_struct_pubs_;
197 
210 
214 
215  geometry_msgs::Quaternion attitude_quat_;
217 
218  std::string frame_id_;
219 
222 
226 };
227 
228 } // namespace rosflight_io
229 
230 #endif // ROSFLIGHT_IO_MAVROSFLIGHT_ROS_H
void versionTimerCallback(const ros::TimerEvent &e)
void handle_small_mag_msg(const mavlink_message_t &msg)
bool calibrateRCTrimSrvCallback(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res)
void handle_battery_status_msg(const mavlink_message_t &msg)
ros::Publisher twist_stamped_pub_
Definition: rosflight_io.h:184
void handle_diff_pressure_msg(const mavlink_message_t &msg)
ros::Publisher attitude_pub_
Definition: rosflight_io.h:187
Describes an interface classes can implement to receive and handle mavlink messages.
ros::Publisher unsaved_params_pub_
Definition: rosflight_io.h:172
static constexpr float HEARTBEAT_PERIOD
Definition: rosflight_io.h:103
void handle_heartbeat_msg(const mavlink_message_t &msg)
ros::Publisher output_raw_pub_
Definition: rosflight_io.h:175
std::string get_major_minor_version(const std::string &version)
ros::ServiceServer calibrate_rc_srv_
Definition: rosflight_io.h:205
ros::Subscriber extatt_sub_
Definition: rosflight_io.h:170
virtual void handle_mavlink_message(const mavlink_message_t &msg)
The handler function for mavlink messages to be implemented by derived classes.
void handle_small_baro_msg(const mavlink_message_t &msg)
rosflight::ROSTimerProvider timer_provider_
Definition: rosflight_io.h:225
ros::Subscriber aux_command_sub_
Definition: rosflight_io.h:169
void handle_command_ack_msg(const mavlink_message_t &msg)
virtual void on_param_value_updated(std::string name, double value)
Called when an updated value is received for a parameter.
void handle_small_range_msg(const mavlink_message_t &msg)
void check_error_code(uint8_t current, uint8_t previous, ROSFLIGHT_ERROR_CODE code, std::string name)
ros::Publisher baro_pub_
Definition: rosflight_io.h:179
virtual void on_params_saved_change(bool unsaved_changes)
Called when the status of whether there are unsaved parameters changes.
ros::ServiceServer reboot_srv_
Definition: rosflight_io.h:208
bool paramLoadFromFileCallback(rosflight_msgs::ParamFile::Request &req, rosflight_msgs::ParamFile::Response &res)
ros::Publisher nav_sat_fix_pub_
Definition: rosflight_io.h:183
geometry_msgs::Quaternion attitude_quat_
Definition: rosflight_io.h:215
void handle_attitude_quaternion_msg(const mavlink_message_t &msg)
void handle_statustext_msg(const mavlink_message_t &msg)
void paramTimerCallback(const ros::TimerEvent &e)
bool calibrateBaroSrvCallback(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res)
void handle_status_msg(const mavlink_message_t &msg)
ros::Publisher status_pub_
Definition: rosflight_io.h:189
void handle_rosflight_output_raw_msg(const mavlink_message_t &msg)
mavrosflight::MavlinkComm * mavlink_comm_
Definition: rosflight_io.h:220
void handle_named_value_int_msg(const mavlink_message_t &msg)
mavrosflight::MavROSflight< rosflight::ROSLogger > * mavrosflight_
Definition: rosflight_io.h:221
ros::Publisher gnss_full_pub_
Definition: rosflight_io.h:182
static constexpr float PARAMETER_PERIOD
Definition: rosflight_io.h:105
ros::Publisher lidar_pub_
Definition: rosflight_io.h:191
ros::Publisher sonar_pub_
Definition: rosflight_io.h:180
ros::Publisher time_reference_pub_
Definition: rosflight_io.h:185
void handle_small_imu_msg(const mavlink_message_t &msg)
void handle_named_command_struct_msg(const mavlink_message_t &msg)
rosflight::ROSLogger logger_
Definition: rosflight_io.h:223
ros::Publisher imu_temp_pub_
Definition: rosflight_io.h:174
bool paramGetSrvCallback(rosflight_msgs::ParamGet::Request &req, rosflight_msgs::ParamGet::Response &res)
void heartbeatTimerCallback(const ros::TimerEvent &e)
bool rebootSrvCallback(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res)
ros::ServiceServer param_set_srv_
Definition: rosflight_io.h:199
ROSFLIGHT_ERROR_CODE
Definition: rosflight.h:114
mavlink_rosflight_status_t prev_status_
Definition: rosflight_io.h:216
ros::ServiceServer calibrate_baro_srv_
Definition: rosflight_io.h:206
void commandCallback(rosflight_msgs::Command::ConstPtr msg)
Describes an interface classes can implement to receive and handle mavlink messages.
ros::Publisher version_pub_
Definition: rosflight_io.h:190
ros::Publisher error_pub_
Definition: rosflight_io.h:192
rosflight::ROSTimeInterface time_interface_
Definition: rosflight_io.h:224
ros::ServiceServer imu_calibrate_temp_srv_
Definition: rosflight_io.h:204
void handle_named_value_float_msg(const mavlink_message_t &msg)
bool paramSetSrvCallback(rosflight_msgs::ParamSet::Request &req, rosflight_msgs::ParamSet::Response &res)
bool paramWriteSrvCallback(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res)
void externalAttitudeCallback(geometry_msgs::Quaternion::ConstPtr msg)
void auxCommandCallback(rosflight_msgs::AuxCommand::ConstPtr msg)
ros::Publisher diff_pressure_pub_
Definition: rosflight_io.h:177
bool calibrateImuBiasSrvCallback(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res)
void handle_hard_error_msg(const mavlink_message_t &msg)
void handle_version_msg(const mavlink_message_t &msg)
void handle_rc_channels_raw_msg(const mavlink_message_t &msg)
void handle_rosflight_gnss_full_msg(const mavlink_message_t &msg)
ros::Publisher battery_status_pub_
Definition: rosflight_io.h:193
ros::ServiceServer imu_calibrate_bias_srv_
Definition: rosflight_io.h:203
ros::Publisher gnss_pub_
Definition: rosflight_io.h:181
T saturate(T value, T min, T max)
Definition: rosflight_io.h:161
ros::ServiceServer param_save_to_file_srv_
Definition: rosflight_io.h:201
ros::Publisher euler_pub_
Definition: rosflight_io.h:188
ros::Subscriber command_sub_
Definition: rosflight_io.h:168
static constexpr float VERSION_PERIOD
Definition: rosflight_io.h:104
ros::ServiceServer calibrate_airspeed_srv_
Definition: rosflight_io.h:207
bool paramSaveToFileCallback(rosflight_msgs::ParamFile::Request &req, rosflight_msgs::ParamFile::Response &res)
std::map< std::string, ros::Publisher > named_value_int_pubs_
Definition: rosflight_io.h:194
ros::Publisher temperature_pub_
Definition: rosflight_io.h:178
void handle_rosflight_gnss_msg(const mavlink_message_t &msg)
ros::Publisher rc_raw_pub_
Definition: rosflight_io.h:176
bool rebootToBootloaderSrvCallback(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res)
ros::ServiceServer param_write_srv_
Definition: rosflight_io.h:200
ros::Time fcu_time_to_ros_time(std::chrono::nanoseconds fcu_time)
virtual void on_new_param_received(std::string name, double value)
Called when a parameter is received from the autopilot for the first time.
ros::ServiceServer reboot_bootloader_srv_
Definition: rosflight_io.h:209
ros::ServiceServer param_get_srv_
Definition: rosflight_io.h:198
bool calibrateAirspeedSrvCallback(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res)
std::map< std::string, ros::Publisher > named_command_struct_pubs_
Definition: rosflight_io.h:196
std::map< std::string, ros::Publisher > named_value_float_pubs_
Definition: rosflight_io.h:195
ros::ServiceServer param_load_from_file_srv_
Definition: rosflight_io.h:202
Logger implementation for ROS environments.
Definition: ros_logger.h:54


rosflight
Author(s): Daniel Koch , James Jackson
autogenerated on Thu Apr 15 2021 05:09:26