SensorManager.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #ifndef _GAZEBO_SENSORMANAGER_HH_
18 #define _GAZEBO_SENSORMANAGER_HH_
19 
20 #include <boost/thread.hpp>
21 #include <string>
22 #include <vector>
23 #include <list>
24 #include <map>
25 #include <condition_variable>
26 
27 #include <sdf/sdf.hh>
28 
33 #include "gazebo/sensors/Sensor.hh"
34 #include "gazebo/util/system.hh"
35 
37 GZ_SINGLETON_DECLARE(GZ_SENSORS_VISIBLE, gazebo, sensors, SensorManager)
38 
39 namespace gazebo
40 {
43  namespace sensors
44  {
47  class GZ_SENSORS_VISIBLE SimTimeEvent
48  {
50  public: common::Time time;
51 
53  public: boost::condition_variable *condition;
54  };
55 
58  class GZ_SENSORS_VISIBLE SimTimeEventHandler
59  {
61  public: SimTimeEventHandler();
62 
64  public: virtual ~SimTimeEventHandler();
65 
71  public: void AddRelativeEvent(const common::Time &_time,
72  boost::condition_variable *_var);
73 
76  private: void OnUpdate(const common::UpdateInfo &_info);
77 
79  private: boost::mutex mutex;
80 
82  private: std::list<SimTimeEvent*> events;
83 
85  private: event::ConnectionPtr updateConnection;
86  };
88 
93  class GZ_SENSORS_VISIBLE SensorManager : public SingletonT<SensorManager>
94  {
97  private: SensorManager();
98 
100  private: virtual ~SensorManager();
101 
107  public: void Update(bool _force = false);
108 
112  public: double NextRequiredTimestamp();
113 
115  public: void Init();
116 
119  public: void RunThreads();
120 
122  public: void Stop();
123 
125  public: void Fini();
126 
129  public: bool Running() const;
130 
133  public: void GetSensorTypes(std::vector<std::string> &_types) const;
134 
142  public: std::string CreateSensor(sdf::ElementPtr _elem,
143  const std::string &_worldName,
144  const std::string &_parentName,
145  uint32_t _parentId);
146 
154  public: void OnCreateSensor(sdf::ElementPtr _elem,
155  const std::string &_worldName,
156  const std::string &_parentName,
157  const uint32_t _parentId);
158 
162  public: SensorPtr GetSensor(const std::string &_name) const;
163 
166  public: Sensor_V GetSensors() const;
167 
170  public: void RemoveSensor(const std::string &_name);
171 
173  public: void RemoveSensors();
174 
177  public: bool SensorsInitialized();
178 
180  public: void ResetLastUpdateTimes();
181 
185  private: void WaitForSensors(double _clk, double _dt);
186 
190  private: bool WaitForPrerendered(double _timeoutsec);
191 
194  private: void AddSensor(SensorPtr _sensor);
195 
202  private: class SensorContainer
203  {
205  public: SensorContainer();
206 
208  public: virtual ~SensorContainer();
209 
211  public: void Init();
212 
214  public: void Fini();
215 
217  public: void Run();
218 
220  public: void Stop();
221 
224  public: bool Running() const;
225 
229  public: virtual void Update(bool _force = false);
230 
233  public: void AddSensor(SensorPtr _sensor);
234 
240  public: SensorPtr GetSensor(const std::string &_name,
241  bool _useLeafName = false) const;
242 
246  public: bool RemoveSensor(const std::string &_name);
247 
249  public: void RemoveSensors();
250 
252  public: void ResetLastUpdateTimes();
253 
256  private: void RunLoop();
257 
259  public: Sensor_V sensors;
260 
262  private: bool stop;
263 
266  private: bool initialized;
267 
269  private: boost::thread *runThread;
270 
272  private: mutable boost::recursive_mutex mutex;
273 
276  private: boost::condition_variable runCondition;
277  };
279 
283  private: class ImageSensorContainer : public SensorContainer
284  {
288  public: bool WaitForPrerendered(double _timeoutsec);
289 
293  public: virtual void Update(bool _force = false);
294 
296  private: std::condition_variable conditionPrerendered;
297  };
299 
302  private: bool initialized;
303 
305  private: bool removeAllSensors;
306 
308  private: mutable boost::recursive_mutex mutex;
309 
311  private: Sensor_V initSensors;
312 
314  private: std::vector<std::string> removeSensors;
315 
317  private: typedef std::vector<SensorContainer*> SensorContainer_V;
318 
320  private: SensorContainer_V sensorContainers;
321 
323  private: friend class SingletonT<SensorManager>;
324 
326  private: friend class SensorContainer;
327 
329  private: SimTimeEventHandler *simTimeEventHandler;
330 
333  private: std::map<std::string, physics::WorldPtr> worlds;
334 
336  private: event::ConnectionPtr timeResetConnection;
337 
339  private: event::ConnectionPtr createSensorConnection;
340 
342  private: event::ConnectionPtr removeSensorConnection;
343  };
345  }
346 }
347 #endif
default namespace for gazebo
sensors
Definition: SensorManager.hh:37
Forward declarations and typedefs for sensors.
Singleton template class.
Definition: SingletonT.hh:34
Class to manage and update all sensors.
Definition: SensorManager.hh:94
void Fini()
Finalize all the sensors.
void ResetLastUpdateTimes()
Reset last update times in all sensors.
void Stop()
Stop the run thread.
std::string CreateSensor(sdf::ElementPtr _elem, const std::string &_worldName, const std::string &_parentName, uint32_t _parentId)
Add a sensor from an SDF element.
void Init()
Init all the sensors.
void Update(bool _force=false)
Update all the sensors.
bool SensorsInitialized()
True if SensorManager::initSensors queue is empty i.e.
void RemoveSensor(const std::string &_name)
Remove a sensor.
void RemoveSensors()
Remove all sensors.
SensorPtr GetSensor(const std::string &_name) const
Get a sensor.
bool Running() const
Get whether there's at least one sensor container running.
Sensor_V GetSensors() const
Get all the sensors.
void RunThreads()
Run sensor updates in separate threads.
void OnCreateSensor(sdf::ElementPtr _elem, const std::string &_worldName, const std::string &_parentName, const uint32_t _parentId)
Add a sensor from an SDF element.
void GetSensorTypes(std::vector< std::string > &_types) const
Get all the sensor types.
double NextRequiredTimestamp()
Amongst all IMAGE sensors, returns the forthcoming timestamp used by one (or several) sensor.
#define GZ_SINGLETON_DECLARE(visibility, n1, n2, singletonType)
Helper to declare typed SingletonT.
Definition: SingletonT.hh:58
GAZEBO_VISIBLE void Init(google::protobuf::Message &_message, const std::string &_id="")
Initialize a message.
void stop()
Stop the sensor generation loop.
boost::shared_ptr< Connection > ConnectionPtr
Definition: CommonTypes.hh:134
std::vector< SensorPtr > Sensor_V
Definition: SensorTypes.hh:152
std::shared_ptr< Sensor > SensorPtr
Definition: SensorTypes.hh:64
Forward declarations for the common classes.
Definition: Animation.hh:27