NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
mapper_nodelet.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  * http://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 MAPPER_MAPPER_NODELET_H_
20 #define MAPPER_MAPPER_NODELET_H_
21 
22 // PCL specific includes
23 #include <sensor_msgs/PointCloud2.h>
24 #include <pcl/point_cloud.h>
25 #include <pcl/point_types.h>
26 #include <pcl/common/transforms.h>
27 #include <pcl/kdtree/kdtree_flann.h>
28 
29 // ROS libraries
30 #include <ros/ros.h>
31 #include <ros/publisher.h>
32 #include <ros/subscriber.h>
33 #include <tf/transform_listener.h>
34 #include <visualization_msgs/MarkerArray.h>
35 #include <tf/transform_broadcaster.h>
36 #include <std_srvs/Trigger.h>
37 
38 // Keepout zones for the planner
39 #include <boost/filesystem.hpp>
40 #include <boost/range/iterator_range_core.hpp>
41 #include <pluginlib/class_list_macros.h>
42 
43 // FSW utils
44 #include <ff_util/ff_nodelet.h>
45 #include <ff_util/ff_action.h>
46 #include <ff_util/ff_flight.h>
48 #include <ff_util/config_server.h>
49 
50 // Service definition for zone registration
51 #include <ff_msgs/Hazard.h>
52 #include <ff_msgs/GetMap.h>
53 
54 // Service messages
55 #include <ff_msgs/SetFloat.h>
56 #include <ff_msgs/GetFloat.h>
57 
58 // General messages
59 #include <std_msgs/Empty.h>
60 
61 // C++ libraries
62 #include <fstream>
63 #include <vector>
64 #include <string>
65 #include <exception>
66 #include <thread> // std::thread
67 #include <mutex>
68 #include <atomic>
69 
70 // Astrobee message types
71 #include "ff_msgs/Segment.h"
72 #include "ff_msgs/ControlState.h"
73 
74 // Classes
75 #include "mapper/octoclass.h"
76 #include "mapper/polynomials.h"
78 
79 // Data structures
80 #include "mapper/structs.h"
81 
82 // My defined libraries
84 
85 namespace mapper {
86 
87 namespace fs = boost::filesystem;
88 
89 // Convenience declarations
90 
92  public:
93  MapperNodelet();
95 
96  protected:
97  void Initialize(ros::NodeHandle *nh);
98 
99  // Callbacks (see callbacks.cpp for implementation) ----------------
100  // Callback for handling incoming point cloud messages
101  void PclCallback(ros::TimerEvent const& event);
102 
103  // Callback for handling incoming new trajectory messages
104  void SegmentCallback(const ff_msgs::Segment::ConstPtr &msg);
105 
106  // Send diagnostics
107  void DiagnosticsCallback(const ros::TimerEvent &event);
108 
109  // Configure callback for updating config file
110  bool ReconfigureCallback(dynamic_reconfigure::Config &config);
111 
112  // Called when the EKF resets
113  void ResetCallback(std_msgs::EmptyConstPtr const& msg);
114 
115  // Assert a fault - katie's fault code handling will eventually go in here
116  void Complete(int32_t response);
117 
118  void PreemptCallback(void);
119 
120  void CancelCallback(void);
121 
122  // Services (see services.cpp for implementation) -----------------
123  // Update resolution of the map
124  bool SetResolution(ff_msgs::SetFloat::Request &req,
125  ff_msgs::SetFloat::Response &res);
126  // Update resolution of the map
127  bool GetResolution(ff_msgs::GetFloat::Request &req,
128  ff_msgs::GetFloat::Response &res);
129 
130  // Update map memory time
131  bool SetMemoryTime(ff_msgs::SetFloat::Request &req,
132  ff_msgs::SetFloat::Response &res);
133  // Update map memory time
134  bool GetMemoryTime(ff_msgs::GetFloat::Request &req,
135  ff_msgs::GetFloat::Response &res);
136 
137  // Update collision distance
138  bool SetCollisionDistance(ff_msgs::SetFloat::Request &req,
139  ff_msgs::SetFloat::Response &res);
140  // Update collision distance
141  bool GetMapInflation(ff_msgs::GetFloat::Request &req,
142  ff_msgs::GetFloat::Response &res);
143 
144  // Reset the map
145  bool ResetMap(std_srvs::Trigger::Request &req,
146  std_srvs::Trigger::Response &res);
147 
148  // Callback to get the free space in the map
149  bool GetFreeMapCallback(ff_msgs::GetMap::Request& req,
150  ff_msgs::GetMap::Response& res);
151 
152  // Callback to get the obstacles in the map
153  bool GetObstacleMapCallback(ff_msgs::GetMap::Request& req,
154  ff_msgs::GetMap::Response& res);
155 
156  // Threads (see threads.cpp for implementation) -----------------
157  // Thread for fading memory of the octomap
158  void FadeTask(ros::TimerEvent const& event);
159 
160  // Collision checking
161  void CollisionCheckTask();
162 
163  // Timer for getting pcl data and populating the octomap
164  void OctomappingTask();
165 
166  // Initialize fault management
167  void InitFault(std::string const& msg);
168 
169  private:
170  ros::NodeHandle *nh_;
171  bool disable_mapper_ = false;
172  // Declare global variables (structures defined in structs.h)
173  GlobalVariables globals_;
174 
175  // Timer variables
176  ros::Timer timer_o_; // Octomapping Task
177  ros::Timer timer_d_; // Diagnostics
178  ros::Timer timer_f_; // Fade Task
179 
180  // Subscriber variables
181  bool use_haz_cam_, use_perch_cam_;
182  ros::Subscriber segment_sub_, reset_sub_;
183 
184  // Octomap services
185  ros::ServiceServer set_resolution_srv_, set_memory_time_srv_, set_collision_distance_srv_;
186  ros::ServiceServer get_resolution_srv_, get_memory_time_srv_, get_collision_distance_srv_;
187  ros::ServiceServer reset_map_srv_;
188 
189  // Timer rates (hz)
190  double octomap_update_rate_, tf_update_rate_, fading_memory_update_rate_;
191 
192  // Trajectory validation variables -----------------------------
193  State state_; // State of the mapper (structure defined in struct.h)
194  ff_util::Segment segment_; // Segment
195 
196  // Services
197  ros::ServiceServer get_free_map_srv_; // Get free map service
198  ros::ServiceServer get_obstacle_map_srv_; // Set obstacle map service
199 
200  ff_util::ConfigServer cfg_; // Config server
201 
202  // TF2
203  std::shared_ptr<tf2_ros::TransformListener> listener_;
204  tf2_ros::Buffer buffer_;
205 
206  // Marker publishers
207  ros::Publisher hazard_pub_; // Collision warnign
208  ros::Publisher obstacle_marker_pub_; // Obstacle map
209  ros::Publisher free_space_marker_pub_; // Obstacle map
210  ros::Publisher obstacle_cloud_pub_; // Obstacle map
211  ros::Publisher free_space_cloud_pub_; // Obstacle map
212  ros::Publisher inflated_obstacle_marker_pub_; // Inflated obstacle map
213  ros::Publisher inflated_free_space_marker_pub_; // Inflated free map
214  ros::Publisher inflated_obstacle_cloud_pub_; // Inflated obstacle map
215  ros::Publisher inflated_free_space_cloud_pub_; // Inflated free map
216  ros::Publisher path_marker_pub_;
217  ros::Publisher cam_frustum_pub_;
218  ros::Publisher map_keep_in_out_pub_;
219 };
220 
221 } // namespace mapper
222 
223 #endif // MAPPER_MAPPER_NODELET_H_
visualization_functions.h
mapper::State
State
Definition: structs.h:48
mapper::MapperNodelet::PreemptCallback
void PreemptCallback(void)
polynomials.h
mapper::MapperNodelet::ReconfigureCallback
bool ReconfigureCallback(dynamic_reconfigure::Config &config)
Definition: callbacks.cc:134
mapper::MapperNodelet::GetMemoryTime
bool GetMemoryTime(ff_msgs::GetFloat::Request &req, ff_msgs::GetFloat::Response &res)
Definition: services.cc:48
mapper::MapperNodelet::ResetMap
bool ResetMap(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res)
Definition: services.cc:68
ff_flight.h
sampled_trajectory.h
mapper::MapperNodelet::ResetCallback
void ResetCallback(std_msgs::EmptyConstPtr const &msg)
Definition: callbacks.cc:162
ff_nodelet.h
ff_util::ConfigServer
Definition: config_server.h:45
ff_serialization.h
mapper::MapperNodelet::~MapperNodelet
~MapperNodelet()
Definition: mapper_nodelet.cc:31
mapper::MapperNodelet::DiagnosticsCallback
void DiagnosticsCallback(const ros::TimerEvent &event)
Definition: callbacks.cc:129
ff_util::Segment
std::vector< Setpoint > Segment
Definition: ff_flight.h:47
mapper::MapperNodelet::CancelCallback
void CancelCallback(void)
config_server.h
mapper::GlobalVariables
Definition: structs.h:53
mapper::MapperNodelet::GetObstacleMapCallback
bool GetObstacleMapCallback(ff_msgs::GetMap::Request &req, ff_msgs::GetMap::Response &res)
Definition: services.cc:89
structs.h
mapper::MapperNodelet::SegmentCallback
void SegmentCallback(const ff_msgs::Segment::ConstPtr &msg)
Definition: callbacks.cc:81
mapper::MapperNodelet::CollisionCheckTask
void CollisionCheckTask()
Definition: threads.cc:33
mapper::MapperNodelet::PclCallback
void PclCallback(ros::TimerEvent const &event)
Definition: callbacks.cc:26
ff_util::FreeFlyerNodelet
Definition: ff_nodelet.h:57
mapper::MapperNodelet::MapperNodelet
MapperNodelet()
Definition: mapper_nodelet.cc:27
mapper::MapperNodelet::GetResolution
bool GetResolution(ff_msgs::GetFloat::Request &req, ff_msgs::GetFloat::Response &res)
Definition: services.cc:33
mapper::MapperNodelet::SetResolution
bool SetResolution(ff_msgs::SetFloat::Request &req, ff_msgs::SetFloat::Response &res)
Definition: services.cc:26
mapper::MapperNodelet::FadeTask
void FadeTask(ros::TimerEvent const &event)
Definition: threads.cc:27
mapper::MapperNodelet
Definition: mapper_nodelet.h:91
ff_action.h
mapper::MapperNodelet::Complete
void Complete(int32_t response)
mapper::MapperNodelet::SetMemoryTime
bool SetMemoryTime(ff_msgs::SetFloat::Request &req, ff_msgs::SetFloat::Response &res)
Definition: services.cc:41
mapper::MapperNodelet::InitFault
void InitFault(std::string const &msg)
mapper::MapperNodelet::OctomappingTask
void OctomappingTask()
Definition: threads.cc:114
octoclass.h
mapper::MapperNodelet::SetCollisionDistance
bool SetCollisionDistance(ff_msgs::SetFloat::Request &req, ff_msgs::SetFloat::Response &res)
Definition: services.cc:55
mapper::MapperNodelet::Initialize
void Initialize(ros::NodeHandle *nh)
Definition: mapper_nodelet.cc:33
mapper::MapperNodelet::GetFreeMapCallback
bool GetFreeMapCallback(ff_msgs::GetMap::Request &req, ff_msgs::GetMap::Response &res)
Definition: services.cc:76
mapper::MapperNodelet::GetMapInflation
bool GetMapInflation(ff_msgs::GetFloat::Request &req, ff_msgs::GetFloat::Response &res)
Definition: services.cc:61
mapper
Definition: mapper_nodelet.h:85