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 #include <condition_variable>
26 
27 #include <sdf/sdf.hh>
28 
33 #include "gazebo/util/system.hh"
34 
36 GZ_SINGLETON_DECLARE(GZ_SENSORS_VISIBLE, gazebo, sensors, SensorManager)
37 
38 namespace gazebo
39 {
42  namespace sensors
43  {
46  class GZ_SENSORS_VISIBLE SimTimeEvent
47  {
49  public: common::Time time;
50 
52  public: boost::condition_variable *condition;
53  };
54 
57  class GZ_SENSORS_VISIBLE 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 GZ_SENSORS_VISIBLE 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: bool Running() const;
124 
127  public: void GetSensorTypes(std::vector<std::string> &_types) const;
128 
136  public: std::string CreateSensor(sdf::ElementPtr _elem,
137  const std::string &_worldName,
138  const std::string &_parentName,
139  uint32_t _parentId);
140 
148  public: void OnCreateSensor(sdf::ElementPtr _elem,
149  const std::string &_worldName,
150  const std::string &_parentName,
151  const uint32_t _parentId);
152 
156  public: SensorPtr GetSensor(const std::string &_name) const;
157 
160  public: Sensor_V GetSensors() const;
161 
164  public: void RemoveSensor(const std::string &_name);
165 
167  public: void RemoveSensors();
168 
171  public: bool SensorsInitialized();
172 
174  public: void ResetLastUpdateTimes();
175 
178  private: void AddSensor(SensorPtr _sensor);
179 
186  private: class SensorContainer
187  {
189  public: SensorContainer();
190 
192  public: virtual ~SensorContainer();
193 
195  public: void Init();
196 
198  public: void Fini();
199 
201  public: void Run();
202 
204  public: void Stop();
205 
208  public: bool Running() const;
209 
213  public: virtual void Update(bool _force = false);
214 
217  public: void AddSensor(SensorPtr _sensor);
218 
224  public: SensorPtr GetSensor(const std::string &_name,
225  bool _useLeafName = false) const;
226 
230  public: bool RemoveSensor(const std::string &_name);
231 
233  public: void RemoveSensors();
234 
236  public: void ResetLastUpdateTimes();
237 
240  private: void RunLoop();
241 
243  public: Sensor_V sensors;
244 
246  private: bool stop;
247 
250  private: bool initialized;
251 
253  private: boost::thread *runThread;
254 
256  private: mutable boost::recursive_mutex mutex;
257 
260  private: boost::condition_variable runCondition;
261  };
263 
267  private: class ImageSensorContainer : public SensorContainer
268  {
272  public: virtual void Update(bool _force = false);
273 
275  private: std::condition_variable conditionPrerendered;
276  };
278 
281  private: bool initialized;
282 
284  private: bool removeAllSensors;
285 
287  private: mutable boost::recursive_mutex mutex;
288 
290  private: Sensor_V initSensors;
291 
293  private: std::vector<std::string> removeSensors;
294 
296  private: typedef std::vector<SensorContainer*> SensorContainer_V;
297 
299  private: SensorContainer_V sensorContainers;
300 
302  private: friend class SingletonT<SensorManager>;
303 
305  private: friend class SensorContainer;
306 
308  private: SimTimeEventHandler *simTimeEventHandler;
309 
312  private: std::map<std::string, physics::WorldPtr> worlds;
313 
315  private: event::ConnectionPtr timeResetConnection;
316 
318  private: event::ConnectionPtr createSensorConnection;
319 
321  private: event::ConnectionPtr removeSensorConnection;
322  };
324  }
325 }
326 #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
sensors
Definition: SensorManager.hh:36
Class to manage and update all sensors.
Definition: SensorManager.hh:92
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 GZ_SINGLETON_DECLARE(visibility, n1, n2, singletonType)
Helper to declare typed SingletonT.
Definition: SingletonT.hh:61