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 (C) 2012-2014 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 
31 #include <sdf/sdf.hh>
32 
37 
38 namespace gazebo
39 {
42  namespace sensors
43  {
46  class SimTimeEvent
47  {
49  public: common::Time time;
50 
52  public: boost::condition_variable *condition;
53  };
54 
57  class SimTimeEventHandler
58  {
60  public: SimTimeEventHandler();
61 
63  public: virtual ~SimTimeEventHandler();
64 
70  public: void AddRelativeEvent(const common::Time &_time,
71  boost::condition_variable *_var);
72 
75  private: void OnUpdate(const common::UpdateInfo &_info);
76 
78  private: boost::mutex mutex;
79 
81  private: std::list<SimTimeEvent*> events;
82 
84  private: event::ConnectionPtr updateConnection;
85  };
87 
92  class SensorManager : public SingletonT<SensorManager>
93  {
96  private: SensorManager();
97 
99  private: virtual ~SensorManager();
100 
106  public: void Update(bool _force = false);
107 
109  public: void Init();
110 
113  public: void RunThreads();
114 
116  public: void Stop();
117 
119  public: void Fini();
120 
123  public: void GetSensorTypes(std::vector<std::string> &_types) const;
124 
126  public: std::string CreateSensor(sdf::ElementPtr _elem,
127  const std::string &_worldName,
128  const std::string &_parentName) GAZEBO_DEPRECATED(2.0);
129 
137  public: std::string CreateSensor(sdf::ElementPtr _elem,
138  const std::string &_worldName,
139  const std::string &_parentName,
140  uint32_t _parentId);
141 
145  public: SensorPtr GetSensor(const std::string &_name) const;
146 
149  public: Sensor_V GetSensors() const;
150 
153  public: void RemoveSensor(const std::string &_name);
154 
156  public: void RemoveSensors();
157 
160  public: bool SensorsInitialized();
161 
163  public: void ResetLastUpdateTimes();
164 
167  private: void AddSensor(SensorPtr _sensor);
168 
175  private: class SensorContainer
176  {
178  public: SensorContainer();
179 
181  public: virtual ~SensorContainer();
182 
184  public: void Init();
185 
187  public: void Fini();
188 
190  public: void Run();
191 
193  public: void Stop();
194 
198  public: virtual void Update(bool _force = false);
199 
202  public: void AddSensor(SensorPtr _sensor);
203 
209  public: SensorPtr GetSensor(const std::string &_name,
210  bool _useLeafName = false) const;
211 
215  public: bool RemoveSensor(const std::string &_name);
216 
218  public: void RemoveSensors();
219 
221  public: void ResetLastUpdateTimes();
222 
225  private: void RunLoop();
226 
228  public: Sensor_V sensors;
229 
231  private: bool stop;
232 
235  private: bool initialized;
236 
238  private: boost::thread *runThread;
239 
241  private: mutable boost::recursive_mutex mutex;
242 
245  private: boost::condition_variable runCondition;
246  };
248 
252  private: class ImageSensorContainer : public SensorContainer
253  {
257  public: virtual void Update(bool _force = false);
258  };
260 
263  private: bool initialized;
264 
266  private: bool removeAllSensors;
267 
269  private: mutable boost::recursive_mutex mutex;
270 
272  private: Sensor_V initSensors;
273 
275  private: std::vector<std::string> removeSensors;
276 
278  private: typedef std::vector<SensorContainer*> SensorContainer_V;
279 
281  private: SensorContainer_V sensorContainers;
282 
284  private: friend class SingletonT<SensorManager>;
285 
287  private: friend class SensorContainer;
288 
290  private: SimTimeEventHandler *simTimeEventHandler;
291  };
293  }
294 }
295 #endif