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 #include "gazebo/util/system.hh"
38 
39 namespace gazebo
40 {
43  namespace sensors
44  {
47  class GAZEBO_VISIBLE SimTimeEvent
48  {
50  public: common::Time time;
51 
53  public: boost::condition_variable *condition;
54  };
55 
58  class GAZEBO_VISIBLE SimTimeEventHandler
59  {
61  public: SimTimeEventHandler();
62 
64  public: virtual ~SimTimeEventHandler();
65 
71  public: void AddRelativeEvent(const common::Time &_time,
72  boost::condition_variable *_var);
73 
76  private: void OnUpdate(const common::UpdateInfo &_info);
77 
79  private: boost::mutex mutex;
80 
82  private: std::list<SimTimeEvent*> events;
83 
85  private: event::ConnectionPtr updateConnection;
86  };
88 
93  class GAZEBO_VISIBLE SensorManager : public SingletonT<SensorManager>
94  {
97  private: SensorManager();
98 
100  private: virtual ~SensorManager();
101 
107  public: void Update(bool _force = false);
108 
110  public: void Init();
111 
114  public: void RunThreads();
115 
117  public: void Stop();
118 
120  public: void Fini();
121 
124  public: void GetSensorTypes(std::vector<std::string> &_types) const;
125 
127  public: std::string CreateSensor(sdf::ElementPtr _elem,
128  const std::string &_worldName,
129  const std::string &_parentName) GAZEBO_DEPRECATED(2.0);
130 
138  public: std::string CreateSensor(sdf::ElementPtr _elem,
139  const std::string &_worldName,
140  const std::string &_parentName,
141  uint32_t _parentId);
142 
146  public: SensorPtr GetSensor(const std::string &_name) const;
147 
150  public: Sensor_V GetSensors() const;
151 
154  public: void RemoveSensor(const std::string &_name);
155 
157  public: void RemoveSensors();
158 
161  public: bool SensorsInitialized();
162 
164  public: void ResetLastUpdateTimes();
165 
168  private: void AddSensor(SensorPtr _sensor);
169 
176  private: class SensorContainer
177  {
179  public: SensorContainer();
180 
182  public: virtual ~SensorContainer();
183 
185  public: void Init();
186 
188  public: void Fini();
189 
191  public: void Run();
192 
194  public: void Stop();
195 
199  public: virtual void Update(bool _force = false);
200 
203  public: void AddSensor(SensorPtr _sensor);
204 
210  public: SensorPtr GetSensor(const std::string &_name,
211  bool _useLeafName = false) const;
212 
216  public: bool RemoveSensor(const std::string &_name);
217 
219  public: void RemoveSensors();
220 
222  public: void ResetLastUpdateTimes();
223 
226  private: void RunLoop();
227 
229  public: Sensor_V sensors;
230 
232  private: bool stop;
233 
236  private: bool initialized;
237 
239  private: boost::thread *runThread;
240 
242  private: mutable boost::recursive_mutex mutex;
243 
246  private: boost::condition_variable runCondition;
247  };
249 
253  private: class ImageSensorContainer : public SensorContainer
254  {
258  public: virtual void Update(bool _force = false);
259  };
261 
264  private: bool initialized;
265 
267  private: bool removeAllSensors;
268 
270  private: mutable boost::recursive_mutex mutex;
271 
273  private: Sensor_V initSensors;
274 
276  private: std::vector<std::string> removeSensors;
277 
279  private: typedef std::vector<SensorContainer*> SensorContainer_V;
280 
282  private: SensorContainer_V sensorContainers;
283 
285  private: friend class SingletonT<SensorManager>;
286 
288  private: friend class SensorContainer;
289 
291  private: SimTimeEventHandler *simTimeEventHandler;
292  };
294  }
295 }
296 #endif