NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
astrobee_gazebo.h
Go to the documentation of this file.
1 /* Copyright (c) 2017, United States Government, as represented by the
2  * Administrator of the National Aeronautics and Space Administration.
3  *
4  * All rights reserved.
5  *
6  * The Astrobee platform is licensed under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  *
10  * https://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations
16  * under the License.
17  */
18 
19 #ifndef ASTROBEE_GAZEBO_ASTROBEE_GAZEBO_H_
20 #define ASTROBEE_GAZEBO_ASTROBEE_GAZEBO_H_
21 
22 // ROS includes
23 #include <ros/ros.h>
24 #include <ros/callback_queue.h>
25 
26 // General messages
27 #include <geometry_msgs/TransformStamped.h>
28 #include <sensor_msgs/PointCloud2.h>
29 #include <sensor_msgs/point_cloud2_iterator.h>
30 #include <sensor_msgs/Image.h>
31 #include <sensor_msgs/image_encodings.h>
32 #include <sensor_msgs/CameraInfo.h>
33 
34 // Transformation helper code
35 #include <tf2_ros/transform_listener.h>
36 
37 // FSW includes
38 #include <ff_util/ff_nodelet.h>
39 
40 // Gazebo includes
41 #include <gazebo/common/common.hh>
42 #include <gazebo/physics/physics.hh>
43 #include <gazebo/sensors/sensors.hh>
44 #if GAZEBO_MAJOR_VERSION <= 7
45 #include <gazebo/math/gzmath.hh>
46 #endif
47 #include <gazebo/rendering/rendering.hh>
48 
49 // Eigen includes
50 #include <Eigen/Geometry>
51 
52 // STL includes
53 #include <string>
54 #include <thread>
55 #include <memory>
56 
57 namespace gazebo {
58 
59 // Convenience wrapper around a model plugin
61  public:
62  // Constructor
63  explicit FreeFlyerPlugin(std::string const& plugin_name,
64  std::string const& plugin_frame, bool send_heartbeats = false);
65 
66  // Destructor
67  virtual ~FreeFlyerPlugin();
68 
69  protected:
70  // Initialize the plugin
71  void InitializePlugin(std::string const& robot_name, std::string const& plugin_name);
72 
73  // Some plugins might want the world as the parent frame
74  void SetParentFrame(std::string const& parent);
75 
76  // Get the robot-prefixed frame name for the given traget frame
77  std::string GetFrame(std::string target = "", std::string delim = "/");
78 
79  // Called when extrinsics become available
80  virtual bool ExtrinsicsCallback(
81  geometry_msgs::TransformStamped const* tf) = 0;
82 
83  // Optional callback for nodes to know when extrinsics were received
84  virtual void OnExtrinsicsReceived(ros::NodeHandle *nh) {}
85 
86  // Manage the extrinsics based on the sensor type
87  void SetupExtrinsics(const ros::TimerEvent& event);
88 
89  // Custom callback queue to avoid contention between the global callback
90  // queue and gazebo update work.
91  void CallbackThread();
92 
93  // Child classes need access
95  ros::NodeHandle nh_, nh_ff_, nh_ff_mt_;
96  std::shared_ptr<tf2_ros::TransformListener> listener_;
97  ros::CallbackQueue callback_queue_;
98  std::thread thread_;
99  ros::Timer timer_;
100  tf2_ros::Buffer buffer_;
101 };
102 
103 // Convenience wrapper around a model plugin
104 class FreeFlyerModelPlugin : public FreeFlyerPlugin, public ModelPlugin {
105  public:
106  // Constructor
107  explicit FreeFlyerModelPlugin(std::string const& plugin_name,
108  std::string const& plugin_frame, bool send_heartbeats = false);
109 
110  // Destructor
111  virtual ~FreeFlyerModelPlugin();
112 
113  protected:
114  // Called when the model is loaded
115  virtual void Load(physics::ModelPtr model, sdf::ElementPtr sdf);
116 
117  // Get the model link
118  physics::LinkPtr GetLink();
119 
120  // Get the model world
121  physics::WorldPtr GetWorld();
122 
123  // Get the model
124  physics::ModelPtr GetModel();
125 
126  // Callback when the model has loaded
127  virtual void LoadCallback(ros::NodeHandle *nh,
128  physics::ModelPtr model, sdf::ElementPtr sdf) = 0;
129 
130  // Manage the extrinsics based on the sensor type
131  virtual bool ExtrinsicsCallback(geometry_msgs::TransformStamped const* tf);
132 
133  private:
134  sdf::ElementPtr sdf_;
135  physics::LinkPtr link_;
136  physics::WorldPtr world_;
137  physics::ModelPtr model_;
138 };
139 
140 
141 // Convenience wrapper around a sensor plugin
142 class FreeFlyerSensorPlugin : public FreeFlyerPlugin, public SensorPlugin {
143  public:
144  // Constructor
145  explicit FreeFlyerSensorPlugin(std::string const& plugin_name,
146  std::string const& plugin_frame, bool send_heartbeats = false);
147 
148  // Destructor
149  virtual ~FreeFlyerSensorPlugin();
150 
151  protected:
152  // Called when the sensor is loaded
153  void Load(sensors::SensorPtr sensor, sdf::ElementPtr sdf);
154 
155  // Get the sensor world
156  physics::WorldPtr GetWorld();
157 
158  // Get the sensor model
159  physics::ModelPtr GetModel();
160 
161  // Get the type of the sensor
162  std::string GetRotationType();
163 
164  // Callback when the sensor has loaded
165  virtual void LoadCallback(ros::NodeHandle *nh,
166  sensors::SensorPtr sensor, sdf::ElementPtr sdf) = 0;
167 
168  // Manage the extrinsics based on the sensor type
169  virtual bool ExtrinsicsCallback(geometry_msgs::TransformStamped const* tf);
170 
171  private:
172  sensors::SensorPtr sensor_;
173  physics::WorldPtr world_;
174  physics::ModelPtr model_;
175  sdf::ElementPtr sdf_;
176 };
177 
178 // Utility functions
179 
180 // Find the transform from the sensor to the world
181 #if GAZEBO_MAJOR_VERSION > 7
182 Eigen::Affine3d SensorToWorld(ignition::math::Pose3d const& world_pose,
183  ignition::math::Pose3d const& sensor_pose);
184 #else
185 Eigen::Affine3d SensorToWorld(gazebo::math::Pose const& world_pose,
186  ignition::math::Pose3d const& sensor_pose);
187 #endif
188 
189 // Read the camera info
190 void FillCameraInfo(rendering::CameraPtr camera, sensor_msgs::CameraInfo & info_msg);
191 
192 } // namespace gazebo
193 
194 #endif // ASTROBEE_GAZEBO_ASTROBEE_GAZEBO_H_
gazebo::FreeFlyerPlugin::callback_queue_
ros::CallbackQueue callback_queue_
Definition: astrobee_gazebo.h:97
gazebo::FreeFlyerPlugin::listener_
std::shared_ptr< tf2_ros::TransformListener > listener_
Definition: astrobee_gazebo.h:96
gazebo::FreeFlyerPlugin::nh_ff_mt_
ros::NodeHandle nh_ff_mt_
Definition: astrobee_gazebo.h:95
gazebo
Definition: astrobee_gazebo.h:57
gazebo::FreeFlyerSensorPlugin::GetWorld
physics::WorldPtr GetWorld()
Definition: astrobee_gazebo.cc:223
camera
Definition: camera_model.h:26
gazebo::FreeFlyerModelPlugin::GetWorld
physics::WorldPtr GetWorld()
Definition: astrobee_gazebo.cc:148
gazebo::FreeFlyerModelPlugin
Definition: astrobee_gazebo.h:104
gazebo::FreeFlyerPlugin::plugin_name_
std::string plugin_name_
Definition: astrobee_gazebo.h:94
gazebo::FreeFlyerModelPlugin::Load
virtual void Load(physics::ModelPtr model, sdf::ElementPtr sdf)
Definition: astrobee_gazebo.cc:116
ff_nodelet.h
gazebo::FreeFlyerModelPlugin::~FreeFlyerModelPlugin
virtual ~FreeFlyerModelPlugin()
Definition: astrobee_gazebo.cc:113
gazebo::FreeFlyerSensorPlugin::Load
void Load(sensors::SensorPtr sensor, sdf::ElementPtr sdf)
Definition: astrobee_gazebo.cc:190
gazebo::FreeFlyerPlugin::nh_
ros::NodeHandle nh_
Definition: astrobee_gazebo.h:95
gazebo::FreeFlyerModelPlugin::LoadCallback
virtual void LoadCallback(ros::NodeHandle *nh, physics::ModelPtr model, sdf::ElementPtr sdf)=0
gazebo::FreeFlyerModelPlugin::ExtrinsicsCallback
virtual bool ExtrinsicsCallback(geometry_msgs::TransformStamped const *tf)
Definition: astrobee_gazebo.cc:158
gazebo::FreeFlyerPlugin::nh_ff_
ros::NodeHandle nh_ff_
Definition: astrobee_gazebo.h:95
gazebo::FreeFlyerPlugin::parent_frame_
std::string parent_frame_
Definition: astrobee_gazebo.h:94
gazebo::FreeFlyerSensorPlugin::LoadCallback
virtual void LoadCallback(ros::NodeHandle *nh, sensors::SensorPtr sensor, sdf::ElementPtr sdf)=0
gazebo::FreeFlyerPlugin
Definition: astrobee_gazebo.h:60
gazebo::FreeFlyerPlugin::OnExtrinsicsReceived
virtual void OnExtrinsicsReceived(ros::NodeHandle *nh)
Definition: astrobee_gazebo.h:84
gazebo::FreeFlyerPlugin::InitializePlugin
void InitializePlugin(std::string const &robot_name, std::string const &plugin_name)
Definition: astrobee_gazebo.cc:47
gazebo::FreeFlyerPlugin::~FreeFlyerPlugin
virtual ~FreeFlyerPlugin()
Definition: astrobee_gazebo.cc:36
gazebo::FreeFlyerSensorPlugin::GetRotationType
std::string GetRotationType()
gazebo::FreeFlyerModelPlugin::GetModel
physics::ModelPtr GetModel()
Definition: astrobee_gazebo.cc:153
ff_util::FreeFlyerNodelet
Definition: ff_nodelet.h:57
gazebo::FreeFlyerPlugin::thread_
std::thread thread_
Definition: astrobee_gazebo.h:98
gazebo::FreeFlyerModelPlugin::FreeFlyerModelPlugin
FreeFlyerModelPlugin(std::string const &plugin_name, std::string const &plugin_frame, bool send_heartbeats=false)
Definition: astrobee_gazebo.cc:107
gazebo::FreeFlyerSensorPlugin
Definition: astrobee_gazebo.h:142
gazebo::FreeFlyerModelPlugin::GetLink
physics::LinkPtr GetLink()
Definition: astrobee_gazebo.cc:143
gazebo::FreeFlyerPlugin::GetFrame
std::string GetFrame(std::string target="", std::string delim="/")
Definition: astrobee_gazebo.cc:99
gazebo::FreeFlyerSensorPlugin::~FreeFlyerSensorPlugin
virtual ~FreeFlyerSensorPlugin()
Definition: astrobee_gazebo.cc:187
gazebo::FreeFlyerPlugin::FreeFlyerPlugin
FreeFlyerPlugin(std::string const &plugin_name, std::string const &plugin_frame, bool send_heartbeats=false)
Definition: astrobee_gazebo.cc:29
gazebo::SensorToWorld
Eigen::Affine3d SensorToWorld(gazebo::math::Pose const &world_pose, ignition::math::Pose3d const &sensor_pose)
Definition: astrobee_gazebo.cc:328
gazebo::FreeFlyerPlugin::SetupExtrinsics
void SetupExtrinsics(const ros::TimerEvent &event)
Definition: astrobee_gazebo.cc:77
gazebo::FreeFlyerPlugin::SetParentFrame
void SetParentFrame(std::string const &parent)
Definition: astrobee_gazebo.cc:42
optimization_common::Affine3d
Eigen::Affine3d Affine3d(const Eigen::Matrix< double, 7, 1 > &affine_vector)
Definition: utilities.cc:43
gazebo::FreeFlyerSensorPlugin::FreeFlyerSensorPlugin
FreeFlyerSensorPlugin(std::string const &plugin_name, std::string const &plugin_frame, bool send_heartbeats=false)
Definition: astrobee_gazebo.cc:181
gazebo::FreeFlyerPlugin::buffer_
tf2_ros::Buffer buffer_
Definition: astrobee_gazebo.h:100
gazebo::FreeFlyerSensorPlugin::ExtrinsicsCallback
virtual bool ExtrinsicsCallback(geometry_msgs::TransformStamped const *tf)
Definition: astrobee_gazebo.cc:233
gazebo::FreeFlyerPlugin::ExtrinsicsCallback
virtual bool ExtrinsicsCallback(geometry_msgs::TransformStamped const *tf)=0
gazebo::FillCameraInfo
void FillCameraInfo(rendering::CameraPtr camera, sensor_msgs::CameraInfo &info_msg)
Definition: astrobee_gazebo.cc:350
gazebo::FreeFlyerPlugin::CallbackThread
void CallbackThread()
Definition: astrobee_gazebo.cc:71
gazebo::FreeFlyerPlugin::robot_name_
std::string robot_name_
Definition: astrobee_gazebo.h:94
gazebo::FreeFlyerSensorPlugin::GetModel
physics::ModelPtr GetModel()
Definition: astrobee_gazebo.cc:228
gazebo::FreeFlyerPlugin::timer_
ros::Timer timer_
Definition: astrobee_gazebo.h:99
gazebo::FreeFlyerPlugin::plugin_frame_
std::string plugin_frame_
Definition: astrobee_gazebo.h:94