environment.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018, The Regents of the University of California (Regents).
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
7  * met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following
14  * disclaimer in the documentation and/or other materials provided
15  * with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Please contact the author(s) of this library if you have any questions.
34  * Authors: David Fridovich-Keil ( dfk@eecs.berkeley.edu )
35  */
36 
38 //
39 // Base class for all environment models, providing separate collision check
40 // functions for each type of tracking error bound. All environments are
41 // boxes in 3D space. Environment is templated on the specific type of sensor
42 // message (M) which may be generated from or incorporated into a derived class,
43 // and sensor parameters (P) which may be used to generate sensor readings.
44 //
46 
47 #ifndef FASTRACK_ENVIRONMENT_ENVIRONMENT_H
48 #define FASTRACK_ENVIRONMENT_ENVIRONMENT_H
49 
50 #include <fastrack/bound/box.h>
52 #include <fastrack/bound/sphere.h>
54 #include <fastrack/utils/types.h>
55 
56 #include <ros/ros.h>
57 #include <std_msgs/Empty.h>
58 #include <visualization_msgs/Marker.h>
59 
60 namespace fastrack {
61 namespace environment {
62 
63 using bound::Box;
64 using bound::Sphere;
65 using bound::Cylinder;
66 using bound::TrackingBound;
67 
68 template <typename M, typename P>
69 class Environment {
70  public:
71  virtual ~Environment() {}
72 
73  // Initialize from a ROS NodeHandle.
74  bool Initialize(const ros::NodeHandle& n);
75 
76  // Derived classes must provide a collision checker which returns true if
77  // and only if the provided position is a valid collision-free configuration.
78  virtual bool IsValid(
79  const Vector3d& position, const TrackingBound& bound,
80  double time = std::numeric_limits<double>::quiet_NaN()) const = 0;
81 
82  // Utility for checking multiple positions.
83  bool AreValid(const std::vector<Vector3d>& positions,
84  const TrackingBound& bound,
85  double time = std::numeric_limits<double>::quiet_NaN()) const {
86  // Return Boolean AND of all IsValid calls.
87  for (const auto& p : positions) {
88  if (!IsValid(p, bound, time)) return false;
89  }
90 
91  return true;
92  }
93 
94  // Generate a sensor measurement.
95  virtual M SimulateSensor(const P& params) const = 0;
96 
97  // Derived classes must have some sort of visualization through RViz.
98  virtual void Visualize() const = 0;
99 
100  protected:
101  explicit Environment() : initialized_(false) {}
102 
103  // Load parameters. This may be overridden by derived classes if needed
104  // (they should still call this one via Environment::LoadParameters).
105  virtual bool LoadParameters(const ros::NodeHandle& n);
106 
107  // Register callbacks.
108  virtual bool RegisterCallbacks(const ros::NodeHandle& n);
109 
110  // Update this environment with the information contained in the given
111  // sensor measurement.
112  // NOTE! This function needs to publish on `updated_topic_`.
113  virtual void SensorCallback(const typename M::ConstPtr& msg) = 0;
114 
115  // Upper and lower bounds.
116  Vector3d lower_;
117  Vector3d upper_;
118 
119  // Publishers and subscribers.
120  ros::Publisher vis_pub_;
121  ros::Publisher updated_pub_;
122  ros::Subscriber sensor_sub_;
123 
124  std::string vis_topic_;
125  std::string updated_topic_;
126  std::string sensor_topic_;
127 
128  // Frame in which to publish visualization.
129  std::string fixed_frame_;
130 
131  // Naming and initialization.
132  std::string name_;
134 }; //\class Environment
135 
136 // ----------------------------- IMPLEMENTATION ----------------------------- //
137 
138 // Initialize from a ROS NodeHandle.
139 template <typename M, typename P>
140 bool Environment<M, P>::Initialize(const ros::NodeHandle& n) {
141  name_ = ros::names::append(n.getNamespace(), "Environment");
142 
143  if (!LoadParameters(n)) {
144  ROS_ERROR("%s: Failed to load parameters.", name_.c_str());
145  return false;
146  }
147 
148  if (!RegisterCallbacks(n)) {
149  ROS_ERROR("%s: Failed to register callbacks.", name_.c_str());
150  return false;
151  }
152 
153  // Visualize.
154  Visualize();
155 
156  initialized_ = true;
157  return true;
158 }
159 
160 // Load parameters. This may be overridden by derived classes if needed
161 // (they should still call this one via Environment::LoadParameters).
162 template <typename M, typename P>
163 bool Environment<M, P>::LoadParameters(const ros::NodeHandle& n) {
164  ros::NodeHandle nl(n);
165 
166  // Sensor topic/service.
167  if (!nl.getParam("topic/sensor_sub", sensor_topic_)) return false;
168  if (!nl.getParam("topic/updated_env", updated_topic_)) return false;
169  if (!nl.getParam("vis/env", vis_topic_)) return false;
170 
171  // Frame of reference to publish visualization in.
172  if (!nl.getParam("frame/fixed", fixed_frame_)) return false;
173 
174  // Upper and lower bounds of the environment.
175  if (!nl.getParam("env/upper/x", upper_(0))) return false;
176  if (!nl.getParam("env/upper/y", upper_(1))) return false;
177  if (!nl.getParam("env/upper/z", upper_(2))) return false;
178 
179  if (!nl.getParam("env/lower/x", lower_(0))) return false;
180  if (!nl.getParam("env/lower/y", lower_(1))) return false;
181  if (!nl.getParam("env/lower/z", lower_(2))) return false;
182 
183  return true;
184 }
185 
186 // Register callbacks.
187 template <typename M, typename P>
188 bool Environment<M, P>::RegisterCallbacks(const ros::NodeHandle& n) {
189  ros::NodeHandle nl(n);
190 
191  // Subscribers.
192  sensor_sub_ = nl.subscribe(sensor_topic_.c_str(), 1,
194 
195  // Publishers.
196  vis_pub_ =
197  nl.advertise<visualization_msgs::Marker>(vis_topic_.c_str(), 1, false);
198 
199  updated_pub_ =
200  nl.advertise<std_msgs::Empty>(updated_topic_.c_str(), 1, false);
201 
202  return true;
203 }
204 
205 } //\namespace environment
206 } //\namespace fastrack
207 
208 #endif
bool Initialize(const ros::NodeHandle &n)
Definition: environment.h:140
virtual bool LoadParameters(const ros::NodeHandle &n)
Definition: environment.h:163
virtual void SensorCallback(const typename M::ConstPtr &msg)=0
virtual M SimulateSensor(const P &params) const =0
Definition: box.h:53
virtual void Visualize() const =0
virtual bool IsValid(const Vector3d &position, const TrackingBound &bound, double time=std::numeric_limits< double >::quiet_NaN()) const =0
virtual bool RegisterCallbacks(const ros::NodeHandle &n)
Definition: environment.h:188
bool AreValid(const std::vector< Vector3d > &positions, const TrackingBound &bound, double time=std::numeric_limits< double >::quiet_NaN()) const
Definition: environment.h:83


fastrack
Author(s): David Fridovich-Keil
autogenerated on Mon Aug 3 2020 21:28:37