uas_data.cpp
Go to the documentation of this file.
1 
6 /*
7  * Copyright 2014,2015 Vladimir Ermakov.
8  *
9  * This file is part of the mavros package and subject to the license terms
10  * in the top-level LICENSE file of the mavros repository.
11  * https://github.com/mavlink/mavros/tree/master/LICENSE.md
12  */
13 
14 #include <array>
15 #include <unordered_map>
16 #include <stdexcept>
17 #include <mavros/mavros_uas.h>
18 #include <mavros/utils.h>
19 #include <mavros/px4_custom_mode.h>
20 
21 using namespace mavros;
22 using utils::enum_value;
23 
25  tf2_listener(tf2_buffer, true),
26  type(enum_value(MAV_TYPE::GENERIC)),
27  autopilot(enum_value(MAV_AUTOPILOT::GENERIC)),
28  base_mode(0),
29  target_system(1),
30  target_component(1),
31  connected(false),
32  gps_eph(NAN),
33  gps_epv(NAN),
34  gps_fix_type(0),
35  gps_satellites_visible(0),
36  time_offset(0),
37  tsync_mode(UAS::timesync_mode::NONE),
38  fcu_caps_known(false),
39  fcu_capabilities(0)
40 {
41  try {
42  // Using smallest dataset with 5' grid,
43  // From default location,
44  // Use cubic interpolation, Thread safe
45  egm96_5 = std::make_shared<GeographicLib::Geoid>("egm96-5", "", true, true);
46  }
47  catch (const std::exception &e) {
48  // catch exception and shutdown node
49  ROS_FATAL_STREAM("UAS: GeographicLib exception: " << e.what() <<
50  " | Run install_geographiclib_dataset.sh script in order to install Geoid Model dataset!");
51  ros::shutdown();
52  }
53 
54  // send static transform from "local_origin" (ENU) to "local_origin_ned" (NED)
55  publish_static_transform("local_origin", "local_origin_ned", Eigen::Affine3d(ftf::quaternion_from_rpy(M_PI, 0, M_PI_2)));
56  publish_static_transform("fcu", "fcu_frd", Eigen::Affine3d(ftf::quaternion_from_rpy(M_PI, 0, 0)));
57  // for being able to apply the transforms at the data coming from the FCU, the opposite transform is also required
58  publish_static_transform("fcu_frd", "fcu", Eigen::Affine3d(ftf::quaternion_from_rpy(-M_PI, 0, 0)));
59 }
60 
61 /* -*- heartbeat handlers -*- */
62 
63 void UAS::update_heartbeat(uint8_t type_, uint8_t autopilot_, uint8_t base_mode_)
64 {
65  type = type_;
66  autopilot = autopilot_;
67  base_mode = base_mode_;
68 }
69 
71 {
72  if (conn_ != connected) {
73  connected = conn_;
74 
75  // call all change cb's
76  for (auto &cb : connection_cb_vec)
77  cb(conn_);
78  }
79 }
80 
82 {
83  lock_guard lock(mutex);
84  connection_cb_vec.push_back(cb);
85 }
86 
87 /* -*- autopilot version -*- */
88 
89 static uint64_t get_default_caps(UAS::MAV_AUTOPILOT ap_type)
90 {
91  // TODO: return default caps mask for known FCU's
92  return 0;
93 }
94 
96 {
97  if (fcu_caps_known) {
98  uint64_t caps = fcu_capabilities;
99  return caps;
100  }
101  else {
103  }
104 }
105 
106 void UAS::update_capabilities(bool known, uint64_t caps)
107 {
108  fcu_caps_known = known;
109  fcu_capabilities = caps;
110 }
111 
112 
113 /* -*- IMU data -*- */
114 
116 {
117  lock_guard lock(mutex);
118  imu_enu_data = imu;
119 }
120 
122 {
123  lock_guard lock(mutex);
124  imu_ned_data = imu;
125 }
126 
128 {
129  lock_guard lock(mutex);
130  return imu_enu_data;
131 }
132 
134 {
135  lock_guard lock(mutex);
136  return imu_ned_data;
137 }
138 
139 geometry_msgs::Quaternion UAS::get_attitude_orientation_enu()
140 {
141  lock_guard lock(mutex);
142  if (imu_enu_data)
143  return imu_enu_data->orientation;
144  else {
145  // fallback - return identity
146  geometry_msgs::Quaternion q;
147  q.w = 1.0; q.x = q.y = q.z = 0.0;
148  return q;
149  }
150 }
151 
152 geometry_msgs::Quaternion UAS::get_attitude_orientation_ned()
153 {
154  lock_guard lock(mutex);
155  if (imu_ned_data)
156  return imu_ned_data->orientation;
157  else {
158  // fallback - return identity
159  geometry_msgs::Quaternion q;
160  q.w = 1.0; q.x = q.y = q.z = 0.0;
161  return q;
162  }
163 }
164 
166 {
167  lock_guard lock(mutex);
168  if (imu_enu_data)
169  return imu_enu_data->angular_velocity;
170  else {
171  // fallback
172  geometry_msgs::Vector3 v;
173  v.x = v.y = v.z = 0.0;
174  return v;
175  }
176 }
177 
179 {
180  lock_guard lock(mutex);
181  if (imu_ned_data)
182  return imu_ned_data->angular_velocity;
183  else {
184  // fallback
185  geometry_msgs::Vector3 v;
186  v.x = v.y = v.z = 0.0;
187  return v;
188  }
189 }
190 
191 
192 /* -*- GPS data -*- */
193 
195  float eph, float epv,
196  int fix_type, int satellites_visible)
197 {
198  lock_guard lock(mutex);
199 
200  gps_fix = fix;
201  gps_eph = eph;
202  gps_epv = epv;
203  gps_fix_type = fix_type;
204  gps_satellites_visible = satellites_visible;
205 }
206 
208 void UAS::get_gps_epts(float &eph, float &epv, int &fix_type, int &satellites_visible)
209 {
210  lock_guard lock(mutex);
211 
212  eph = gps_eph;
213  epv = gps_epv;
214  fix_type = gps_fix_type;
215  satellites_visible = gps_satellites_visible;
216 }
217 
220 {
221  lock_guard lock(mutex);
222  return gps_fix;
223 }
224 
225 /* -*- transform -*- */
226 
228 void UAS::publish_static_transform(const std::string &frame_id, const std::string &child_id, const Eigen::Affine3d &tr)
229 {
230  geometry_msgs::TransformStamped static_transformStamped;
231 
232  static_transformStamped.header.stamp = ros::Time::now();
233  static_transformStamped.header.frame_id = frame_id;
234  static_transformStamped.child_frame_id = child_id;
235  tf::transformEigenToMsg(tr, static_transformStamped.transform);
236 
237  tf2_static_broadcaster.sendTransform(static_transformStamped);
238 }
std::atomic< uint8_t > autopilot
Definition: mavros_uas.h:401
sensor_msgs::NavSatFix::Ptr gps_fix
Definition: mavros_uas.h:413
void publish_static_transform(const std::string &frame_id, const std::string &child_id, const Eigen::Affine3d &tr)
Publishes static transform.
Definition: uas_data.cpp:228
geometry_msgs::Quaternion get_attitude_orientation_enu()
Get Attitude orientation quaternion.
Definition: uas_data.cpp:139
MAV_AUTOPILOT get_autopilot()
Returns autopilot type.
Definition: mavros_uas.h:127
void update_connection_status(bool conn_)
Definition: uas_data.cpp:70
float gps_epv
Definition: mavros_uas.h:415
void transformEigenToMsg(const Eigen::Affine3d &e, geometry_msgs::Transform &m)
sensor_msgs::Imu::Ptr imu_enu_data
Definition: mavros_uas.h:410
mavlink::common::MAV_TYPE MAV_TYPE
Definition: mavros_uas.h:73
mavlink::common::MAV_AUTOPILOT MAV_AUTOPILOT
Definition: mavros_uas.h:74
tf2_ros::StaticTransformBroadcaster tf2_static_broadcaster
Definition: mavros_uas.h:272
std::function< void(bool)> ConnectionCb
Definition: mavros_uas.h:68
geometry_msgs::Quaternion get_attitude_orientation_ned()
Get Attitude orientation quaternion.
Definition: uas_data.cpp:152
void update_capabilities(bool known, uint64_t caps=0)
Definition: uas_data.cpp:106
void update_attitude_imu_enu(sensor_msgs::Imu::Ptr &imu)
Store IMU data [ENU].
Definition: uas_data.cpp:115
UAS for plugins.
Definition: mavros_uas.h:66
std::atomic< uint8_t > type
Definition: mavros_uas.h:400
#define ROS_FATAL_STREAM(args)
std::shared_ptr< GeographicLib::Geoid > egm96_5
Geoid dataset used to convert between AMSL and WGS-84.
Definition: mavros_uas.h:239
void add_connection_change_handler(ConnectionCb cb)
Add connection change handler callback.
Definition: uas_data.cpp:81
geometry_msgs::Vector3 get_attitude_angular_velocity_ned()
Get angular velocity from IMU data.
Definition: uas_data.cpp:178
std::lock_guard< std::recursive_mutex > lock_guard
Definition: mavros_uas.h:69
std::atomic< bool > connected
Definition: mavros_uas.h:407
static uint64_t get_default_caps(UAS::MAV_AUTOPILOT ap_type)
Definition: uas_data.cpp:89
std::atomic< bool > fcu_caps_known
Definition: mavros_uas.h:422
MAVROS Plugin context.
sensor_msgs::Imu::Ptr get_attitude_imu_ned()
Get IMU data [NED].
Definition: uas_data.cpp:133
std::recursive_mutex mutex
Definition: mavros_uas.h:398
void update_heartbeat(uint8_t type_, uint8_t autopilot_, uint8_t base_mode_)
Definition: uas_data.cpp:63
Eigen::Quaterniond quaternion_from_rpy(const Eigen::Vector3d &rpy)
Convert euler angles to quaternion.
int gps_fix_type
Definition: mavros_uas.h:416
sensor_msgs::NavSatFix::Ptr get_gps_fix()
Retunrs last GPS RAW message.
Definition: uas_data.cpp:219
float gps_eph
Definition: mavros_uas.h:414
uint64_t get_capabilities()
Definition: uas_data.cpp:95
PX4 custom mode constantsPX4 custom flight modes.
std::atomic< uint8_t > base_mode
Definition: mavros_uas.h:402
static Time now()
ROSCPP_DECL void shutdown()
sensor_msgs::Imu::Ptr imu_ned_data
Definition: mavros_uas.h:411
void update_attitude_imu_ned(sensor_msgs::Imu::Ptr &imu)
Store IMU data [NED].
Definition: uas_data.cpp:121
void sendTransform(const geometry_msgs::TransformStamped &transform)
sensor_msgs::Imu::Ptr get_attitude_imu_enu()
Get IMU data [ENU].
Definition: uas_data.cpp:127
void update_gps_fix_epts(sensor_msgs::NavSatFix::Ptr &fix, float eph, float epv, int fix_type, int satellites_visible)
Store GPS RAW data.
Definition: uas_data.cpp:194
std::atomic< uint64_t > fcu_capabilities
Definition: mavros_uas.h:423
geometry_msgs::Vector3 get_attitude_angular_velocity_enu()
Get angular velocity from IMU data.
Definition: uas_data.cpp:165
int gps_satellites_visible
Definition: mavros_uas.h:417
std::vector< ConnectionCb > connection_cb_vec
Definition: mavros_uas.h:408
constexpr std::underlying_type< _T >::type enum_value(_T e)
Definition: utils.h:55
void get_gps_epts(float &eph, float &epv, int &fix_type, int &satellites_visible)
Returns EPH, EPV, Fix type and satellites visible.
Definition: uas_data.cpp:208
std::shared_ptr< MAVConnInterface > Ptr


mavros
Author(s): Vladimir Ermakov
autogenerated on Mon Jul 8 2019 03:20:11