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 GZ_SENSORS_VISIBLE SimTimeEvent
43  {
45  public: common::Time time;
46 
48  public: boost::condition_variable *condition;
49  };
50 
53  class GZ_SENSORS_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 GZ_SENSORS_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: bool Running() const;
120 
123  public: void GetSensorTypes(std::vector<std::string> &_types) const;
124 
132  public: std::string CreateSensor(sdf::ElementPtr _elem,
133  const std::string &_worldName,
134  const std::string &_parentName,
135  uint32_t _parentId);
136 
144  public: void OnCreateSensor(sdf::ElementPtr _elem,
145  const std::string &_worldName,
146  const std::string &_parentName,
147  const uint32_t _parentId);
148 
152  public: SensorPtr GetSensor(const std::string &_name) const;
153 
156  public: Sensor_V GetSensors() const;
157 
160  public: void RemoveSensor(const std::string &_name);
161 
163  public: void RemoveSensors();
164 
167  public: bool SensorsInitialized();
168 
170  public: void ResetLastUpdateTimes();
171 
174  private: void AddSensor(SensorPtr _sensor);
175 
182  private: class SensorContainer
183  {
185  public: SensorContainer();
186 
188  public: virtual ~SensorContainer();
189 
191  public: void Init();
192 
194  public: void Fini();
195 
197  public: void Run();
198 
200  public: void Stop();
201 
204  public: bool Running() const;
205 
209  public: virtual void Update(bool _force = false);
210 
213  public: void AddSensor(SensorPtr _sensor);
214 
220  public: SensorPtr GetSensor(const std::string &_name,
221  bool _useLeafName = false) const;
222 
226  public: bool RemoveSensor(const std::string &_name);
227 
229  public: void RemoveSensors();
230 
232  public: void ResetLastUpdateTimes();
233 
236  private: void RunLoop();
237 
239  public: Sensor_V sensors;
240 
242  private: bool stop;
243 
246  private: bool initialized;
247 
249  private: boost::thread *runThread;
250 
252  private: mutable boost::recursive_mutex mutex;
253 
256  private: boost::condition_variable runCondition;
257  };
259 
263  private: class ImageSensorContainer : public SensorContainer
264  {
268  public: virtual void Update(bool _force = false);
269  };
271 
274  private: bool initialized;
275 
277  private: bool removeAllSensors;
278 
280  private: mutable boost::recursive_mutex mutex;
281 
283  private: Sensor_V initSensors;
284 
286  private: std::vector<std::string> removeSensors;
287 
289  private: typedef std::vector<SensorContainer*> SensorContainer_V;
290 
292  private: SensorContainer_V sensorContainers;
293 
295  private: friend class SingletonT<SensorManager>;
296 
298  private: friend class SensorContainer;
299 
301  private: SimTimeEventHandler *simTimeEventHandler;
302 
305  private: std::map<std::string, physics::WorldPtr> worlds;
306 
308  private: event::ConnectionPtr timeResetConnection;
309 
311  private: event::ConnectionPtr createSensorConnection;
312 
314  private: event::ConnectionPtr removeSensorConnection;
315  };
317  }
318 }
319 #endif
std::shared_ptr< Sensor > SensorPtr
Definition: SensorTypes.hh:64
void stop()
Stop the sensor generation loop.
Forward declarations for the common classes.
Definition: Animation.hh:26
Singleton template class.
Definition: SingletonT.hh:33
default namespace for gazebo
Class to manage and update all sensors.
Definition: SensorManager.hh:88
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.