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 
34 namespace gazebo
35 {
38  namespace sensors
39  {
42  class GAZEBO_VISIBLE SimTimeEvent
43  {
45  public: common::Time time;
46 
48  public: boost::condition_variable *condition;
49  };
50 
53  class GAZEBO_VISIBLE SimTimeEventHandler
54  {
56  public: SimTimeEventHandler();
57 
59  public: virtual ~SimTimeEventHandler();
60 
66  public: void AddRelativeEvent(const common::Time &_time,
67  boost::condition_variable *_var);
68 
71  private: void OnUpdate(const common::UpdateInfo &_info);
72 
74  private: boost::mutex mutex;
75 
77  private: std::list<SimTimeEvent*> events;
78 
80  private: event::ConnectionPtr updateConnection;
81  };
83 
88  class GAZEBO_VISIBLE SensorManager : public SingletonT<SensorManager>
89  {
92  private: SensorManager();
93 
95  private: virtual ~SensorManager();
96 
102  public: void Update(bool _force = false);
103 
105  public: void Init();
106 
109  public: void RunThreads();
110 
112  public: void Stop();
113 
115  public: void Fini();
116 
119  public: void GetSensorTypes(std::vector<std::string> &_types) const;
120 
128  public: std::string CreateSensor(sdf::ElementPtr _elem,
129  const std::string &_worldName,
130  const std::string &_parentName,
131  uint32_t _parentId);
132 
140  public: void OnCreateSensor(sdf::ElementPtr _elem,
141  const std::string &_worldName,
142  const std::string &_parentName,
143  const uint32_t _parentId);
144 
148  public: SensorPtr GetSensor(const std::string &_name) const;
149 
152  public: Sensor_V GetSensors() const;
153 
156  public: void RemoveSensor(const std::string &_name);
157 
159  public: void RemoveSensors();
160 
163  public: bool SensorsInitialized();
164 
166  public: void ResetLastUpdateTimes();
167 
170  private: void AddSensor(SensorPtr _sensor);
171 
178  private: class SensorContainer
179  {
181  public: SensorContainer();
182 
184  public: virtual ~SensorContainer();
185 
187  public: void Init();
188 
190  public: void Fini();
191 
193  public: void Run();
194 
196  public: void Stop();
197 
201  public: virtual void Update(bool _force = false);
202 
205  public: void AddSensor(SensorPtr _sensor);
206 
212  public: SensorPtr GetSensor(const std::string &_name,
213  bool _useLeafName = false) const;
214 
218  public: bool RemoveSensor(const std::string &_name);
219 
221  public: void RemoveSensors();
222 
224  public: void ResetLastUpdateTimes();
225 
228  private: void RunLoop();
229 
231  public: Sensor_V sensors;
232 
234  private: bool stop;
235 
238  private: bool initialized;
239 
241  private: boost::thread *runThread;
242 
244  private: mutable boost::recursive_mutex mutex;
245 
248  private: boost::condition_variable runCondition;
249  };
251 
255  private: class ImageSensorContainer : public SensorContainer
256  {
260  public: virtual void Update(bool _force = false);
261  };
263 
266  private: bool initialized;
267 
269  private: bool removeAllSensors;
270 
272  private: mutable boost::recursive_mutex mutex;
273 
275  private: Sensor_V initSensors;
276 
278  private: std::vector<std::string> removeSensors;
279 
281  private: typedef std::vector<SensorContainer*> SensorContainer_V;
282 
284  private: SensorContainer_V sensorContainers;
285 
287  private: friend class SingletonT<SensorManager>;
288 
290  private: friend class SensorContainer;
291 
293  private: SimTimeEventHandler *simTimeEventHandler;
294 
296  private: std::map<std::string, physics::WorldPtr> worlds;
297 
299  private: event::ConnectionPtr timeResetConnection;
300 
302  private: event::ConnectionPtr createSensorConnection;
303 
305  private: event::ConnectionPtr removeSensorConnection;
306  };
308  }
309 }
310 #endif
std::shared_ptr< Sensor > SensorPtr
Definition: SensorTypes.hh:64
Forward declarations for the common classes.
Definition: Animation.hh:33
Singleton template class.
Definition: SingletonT.hh:33
default namespace for gazebo
Class to manage and update all sensors.
Definition: SensorManager.hh:88
GAZEBO_VISIBLE 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
GAZEBO_VISIBLE void Init(google::protobuf::Message &_message, const std::string &_id="")
Initialize a message.
Forward declarations and typedefs for sensors.
#define GAZEBO_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system.hh:58