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
26#include <sdf/sdf.hh>
27
32#include "gazebo/util/system.hh"
33
35GZ_SINGLETON_DECLARE(GZ_SENSORS_VISIBLE, gazebo, sensors, SensorManager)
36
37namespace gazebo
38{
41 namespace sensors
42 {
45 class GZ_SENSORS_VISIBLE SimTimeEvent
46 {
48 public: common::Time time;
49
51 public: boost::condition_variable *condition;
52 };
53
56 class GZ_SENSORS_VISIBLE SimTimeEventHandler
57 {
59 public: SimTimeEventHandler();
60
62 public: virtual ~SimTimeEventHandler();
63
69 public: void AddRelativeEvent(const common::Time &_time,
70 boost::condition_variable *_var);
71
74 private: void OnUpdate(const common::UpdateInfo &_info);
75
77 private: boost::mutex mutex;
78
80 private: std::list<SimTimeEvent*> events;
81
83 private: event::ConnectionPtr updateConnection;
84 };
86
91 class GZ_SENSORS_VISIBLE SensorManager : public SingletonT<SensorManager>
92 {
95 private: SensorManager();
96
98 private: virtual ~SensorManager();
99
105 public: void Update(bool _force = false);
106
108 public: void Init();
109
112 public: void RunThreads();
113
115 public: void Stop();
116
118 public: void Fini();
119
122 public: bool Running() const;
123
126 public: void GetSensorTypes(std::vector<std::string> &_types) const;
127
135 public: std::string CreateSensor(sdf::ElementPtr _elem,
136 const std::string &_worldName,
137 const std::string &_parentName,
138 uint32_t _parentId);
139
147 public: void OnCreateSensor(sdf::ElementPtr _elem,
148 const std::string &_worldName,
149 const std::string &_parentName,
150 const uint32_t _parentId);
151
155 public: SensorPtr GetSensor(const std::string &_name) const;
156
159 public: Sensor_V GetSensors() const;
160
163 public: void RemoveSensor(const std::string &_name);
164
166 public: void RemoveSensors();
167
170 public: bool SensorsInitialized();
171
173 public: void ResetLastUpdateTimes();
174
177 private: void AddSensor(SensorPtr _sensor);
178
185 private: class SensorContainer
186 {
188 public: SensorContainer();
189
191 public: virtual ~SensorContainer();
192
194 public: void Init();
195
197 public: void Fini();
198
200 public: void Run();
201
203 public: void Stop();
204
207 public: bool Running() const;
208
212 public: virtual void Update(bool _force = false);
213
216 public: void AddSensor(SensorPtr _sensor);
217
223 public: SensorPtr GetSensor(const std::string &_name,
224 bool _useLeafName = false) const;
225
229 public: bool RemoveSensor(const std::string &_name);
230
232 public: void RemoveSensors();
233
235 public: void ResetLastUpdateTimes();
236
239 private: void RunLoop();
240
242 public: Sensor_V sensors;
243
245 private: bool stop;
246
249 private: bool initialized;
250
252 private: boost::thread *runThread;
253
255 private: mutable boost::recursive_mutex mutex;
256
259 private: boost::condition_variable runCondition;
260 };
262
266 private: class ImageSensorContainer : public SensorContainer
267 {
271 public: virtual void Update(bool _force = false);
272 };
274
277 private: bool initialized;
278
280 private: bool removeAllSensors;
281
283 private: mutable boost::recursive_mutex mutex;
284
286 private: Sensor_V initSensors;
287
289 private: std::vector<std::string> removeSensors;
290
292 private: typedef std::vector<SensorContainer*> SensorContainer_V;
293
295 private: SensorContainer_V sensorContainers;
296
298 private: friend class SingletonT<SensorManager>;
299
301 private: friend class SensorContainer;
302
304 private: SimTimeEventHandler *simTimeEventHandler;
305
308 private: std::map<std::string, physics::WorldPtr> worlds;
309
311 private: event::ConnectionPtr timeResetConnection;
312
314 private: event::ConnectionPtr createSensorConnection;
315
317 private: event::ConnectionPtr removeSensorConnection;
318 };
320 }
321}
322#endif
default namespace for gazebo
gazebo
Definition SensorManager.hh:35
sensors
Definition SensorManager.hh:35
Forward declarations and typedefs for sensors.
Singleton template class.
Definition SingletonT.hh:34
Class to manage and update all sensors.
Definition SensorManager.hh:92
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.
#define GZ_SINGLETON_DECLARE(visibility, n1, n2, singletonType)
Helper to declare typed SingletonT.
Definition SingletonT.hh:58
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