sensor.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 sensor models. Sensors are used in simulation to generate
40 // "fake" sensor measurements from a known environment.
41 //
42 // Sensors are templated on the type of environment (E), on the type of
43 // message (M) that they publish, and on the parameter struct (P) used to
44 // query the environment for sensor messages.
45 //
47 
48 #ifndef FASTRACK_SENSOR_SENSOR_H
49 #define FASTRACK_SENSOR_SENSOR_H
50 
51 #include <fastrack/utils/types.h>
52 
53 #include <ros/ros.h>
54 #include <visualization_msgs/Marker.h>
55 #include <geometry_msgs/TransformStamped.h>
56 #include <tf2_ros/transform_listener.h>
57 
58 namespace fastrack {
59 namespace sensor {
60 
61 template<typename E, typename M, typename P>
62 class Sensor {
63 public:
64  virtual ~Sensor() {}
65 
66  // Initialize from a ROS NodeHandle.
67  bool Initialize(const ros::NodeHandle& n);
68 
69 protected:
70  explicit Sensor()
72  initialized_(false) {}
73 
74  // Load parameters. This may be overridden by derived classes if needed
75  // (they should still call this one via Sensor::LoadParameters).
76  virtual bool LoadParameters(const ros::NodeHandle& n);
77 
78  // Register callbacks.
79  bool RegisterCallbacks(const ros::NodeHandle& n);
80 
81  // Every time the timer fires, generate a new sensor measurement.
82  void TimerCallback(const ros::TimerEvent& e);
83 
84  // Update sensor parameters.
85  virtual void UpdateParameters() = 0;
86 
87  // Derived classes must have some sort of visualization through RViz.
88  virtual void Visualize() const = 0;
89 
90  // Ground truth environment.
91  E env_;
92 
93  // Parameters.
95 
96  // Publishers.
97  ros::Publisher vis_pub_;
98  ros::Publisher sensor_pub_;
99 
100  std::string vis_topic_;
101  std::string sensor_topic_;
102 
103  // Frames of reference.
104  std::string sensor_frame_;
105  std::string fixed_frame_;
106 
107  // Buffer and listener to get current sensor pose.
108  tf2_ros::Buffer tf_buffer_;
109  tf2_ros::TransformListener tf_listener_;
110 
111  // Timer.
112  ros::Timer timer_;
113  double time_step_;
114 
115  // Naming and initialization.
116  std::string name_;
118 }; //\class Sensor
119 
120 // ----------------------------- IMPLEMENTATION ----------------------------- //
121 
122 // Initialize from a ROS NodeHandle.
123 template<typename E, typename M, typename P>
124 bool Sensor<E, M, P>::Initialize(const ros::NodeHandle& n) {
125  name_ = ros::names::append(n.getNamespace(), "Sensor");
126 
127  if (!LoadParameters(n)) {
128  ROS_ERROR("%s: Failed to load parameters.", name_.c_str());
129  return false;
130  }
131 
132  if (!RegisterCallbacks(n)) {
133  ROS_ERROR("%s: Failed to register callbacks.", name_.c_str());
134  return false;
135  }
136 
137  // Initialize the ground truth environment.
138  if (!env_.Initialize(n)) {
139  ROS_ERROR("%s: Failed to initialize environment.", name_.c_str());
140  return false;
141  }
142 
143  initialized_ = true;
144  return true;
145 }
146 
147 // Load parameters. This may be overridden by derived classes if needed
148 // (they should still call this one via Sensor::LoadParameters).
149 template<typename E, typename M, typename P>
150 bool Sensor<E, M, P>::LoadParameters(const ros::NodeHandle& n) {
151  ros::NodeHandle nl(n);
152 
153  // Topics.
154  if (!nl.getParam("topic/sensor_pub", sensor_topic_)) return false;
155  if (!nl.getParam("vis/sensor", vis_topic_)) return false;
156 
157  // Frames of reference.
158  if (!nl.getParam("frame/fixed", fixed_frame_)) return false;
159  if (!nl.getParam("frame/sensor", sensor_frame_)) return false;
160 
161  // Time step.
162  if (!nl.getParam("time_step", time_step_)) return false;
163 
164  return true;
165 }
166 
167 // Register callbacks.
168 template<typename E, typename M, typename P>
169 bool Sensor<E, M, P>::RegisterCallbacks(const ros::NodeHandle& n) {
170  ros::NodeHandle nl(n);
171 
172  // Publishers.
173  sensor_pub_ = nl.advertise<M>(
174  sensor_topic_.c_str(), 1, false);
175 
176  vis_pub_ = nl.advertise<visualization_msgs::Marker>(
177  vis_topic_.c_str(), 1, false);
178 
179  // Timer.
180  timer_ = nl.createTimer(
181  ros::Duration(time_step_), &Sensor<E, M, P>::TimerCallback, this);
182 
183  return true;
184 }
185 
186 // Every time the timer fires, generate a new sensor measurement.
187 template<typename E, typename M, typename P>
188 void Sensor<E, M, P>::TimerCallback(const ros::TimerEvent& e) {
189  // Get most up-to-date parameters (e.g. by reading pose from TF).
191 
192  // Query environment and publish.
193  sensor_pub_.publish(env_.SimulateSensor(params_));
194 
195  // Visualize.
196  env_.Visualize();
197  Visualize();
198 }
199 
200 } //\namespace sensor
201 } //\namespace fastrack
202 
203 #endif
tf2_ros::TransformListener tf_listener_
Definition: sensor.h:109
std::string sensor_frame_
Definition: sensor.h:104
std::string sensor_topic_
Definition: sensor.h:101
void TimerCallback(const ros::TimerEvent &e)
Definition: sensor.h:188
bool RegisterCallbacks(const ros::NodeHandle &n)
Definition: sensor.h:169
Definition: box.h:53
virtual bool LoadParameters(const ros::NodeHandle &n)
Definition: sensor.h:150
ros::Publisher sensor_pub_
Definition: sensor.h:98
virtual void Visualize() const =0
tf2_ros::Buffer tf_buffer_
Definition: sensor.h:108
bool Initialize(const ros::NodeHandle &n)
Definition: sensor.h:124
std::string fixed_frame_
Definition: sensor.h:105
ros::Publisher vis_pub_
Definition: sensor.h:97
std::string vis_topic_
Definition: sensor.h:100
virtual void UpdateParameters()=0
std::string name_
Definition: sensor.h:116


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