All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SensorManager.hh
Go to the documentation of this file.
1 /*
2  * Copyright 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 /*
18  * Desc: Class to manager all sensors
19  * Author: Nate Koenig
20  * Date: 18 Dec 2009
21  */
22 
23 #ifndef _SENSORMANAGER_HH_
24 #define _SENSORMANAGER_HH_
25 
26 #include <boost/thread.hpp>
27 #include <string>
28 #include <vector>
29 #include <list>
30 
35 #include "gazebo/sdf/sdf.hh"
36 
37 namespace gazebo
38 {
41  namespace sensors
42  {
46  {
48  public: common::Time time;
49 
51  public: boost::condition_variable *condition;
52  };
53 
56  class SimTimeEventHandler : public SingletonT<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: physics::WorldPtr world;
84 
86  private: friend class SingletonT<SimTimeEventHandler>;
87 
89  private: event::ConnectionPtr updateConnection;
90  };
92 
97  class SensorManager : public SingletonT<SensorManager>
98  {
101  private: SensorManager();
102 
104  private: virtual ~SensorManager();
105 
111  public: void Update(bool _force = false);
112 
114  public: void Init();
115 
118  public: void Run() GAZEBO_DEPRECATED;
119 
122  public: void RunThreads();
123 
125  public: void Stop();
126 
128  public: void Fini();
129 
132  public: void GetSensorTypes(std::vector<std::string> &_types) const;
133 
141  public: std::string CreateSensor(sdf::ElementPtr _elem,
142  const std::string &_worldName,
143  const std::string &_parentName);
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: mutable boost::recursive_mutex mutex;
270 
272  private: Sensor_V initSensors;
273 
275  private: typedef std::vector<SensorContainer*> SensorContainer_V;
276 
278  private: SensorContainer_V sensorContainers;
279 
282  private: static boost::mutex sensorTimingMutex;
283 
285  private: friend class SingletonT<SensorManager>;
286 
288  private: friend class SensorContainer;
289 
291  private: friend class SimTimeEventHandler;
292  };
294  }
295 }
296 #endif