Gazebo Sensors

API Reference

3.6.0
gz/sensors/Manager.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 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 GZ_SENSORS_MANAGER_HH_
18 #define GZ_SENSORS_MANAGER_HH_
19 
20 #include <memory>
21 #include <string>
22 #include <type_traits>
23 #include <vector>
24 #include <sdf/sdf.hh>
25 #include <gz/common/SuppressWarning.hh>
26 #include <gz/common/Time.hh>
27 #include <gz/common/Console.hh>
28 #include <gz/sensors/config.hh>
29 #include <gz/sensors/Export.hh>
30 #include <gz/sensors/Sensor.hh>
31 
32 namespace ignition
33 {
34  namespace sensors
35  {
36  // Inline bracket to help doxygen filtering.
37  inline namespace IGNITION_SENSORS_VERSION_NAMESPACE {
38  // Forward declarations
39  class ManagerPrivate;
40 
52  class IGNITION_SENSORS_VISIBLE Manager
53  {
55  public: Manager();
56 
58  public: virtual ~Manager();
59 
62  public: bool Init();
63 
81  public: template<typename T>
82  T *CreateSensor(sdf::Sensor _sdf)
83  {
84  gz::sensors::SensorId id = this->CreateSensor(_sdf);
85 
86  if (id != NO_SENSOR)
87  {
88  T *result = dynamic_cast<T*>(this->Sensor(id));
89 
90  if (!result)
91  ignerr << "SDF sensor type does not match template type\n";
92 
93  return result;
94  }
95 
96  ignerr << "Failed to create sensor of type["
97  << _sdf.TypeStr() << "]\n";
98  return nullptr;
99  }
100 
118  public: template<typename T>
119  T *CreateSensor(sdf::ElementPtr _sdf)
120  {
121  gz::sensors::SensorId id = this->CreateSensor(_sdf);
122 
123  if (id != NO_SENSOR)
124  {
125  T *result = dynamic_cast<T*>(this->Sensor(id));
126 
127  if (nullptr == result)
128  {
129  ignerr << "Failed to create sensor [" << id << "] of type ["
130  << _sdf->Get<std::string>("type")
131  << "]. SDF sensor type does not match template type."
132  << std::endl;
133  }
134 
135  return result;
136  }
137 
138  ignerr << "Failed to create sensor of type ["
139  << _sdf->Get<std::string>("type") << "]\n";
140  return nullptr;
141  }
142 
160  public: gz::sensors::SensorId CreateSensor(sdf::ElementPtr _sdf);
161 
179  public: gz::sensors::SensorId CreateSensor(const sdf::Sensor &_sdf);
180 
181 
185  public: gz::sensors::Sensor *Sensor(
187 
191  public: bool Remove(const gz::sensors::SensorId _id);
192 
197  public: void RunOnce(const gz::common::Time &_time,
198  bool _force = false);
199 
201  public: void AddPluginPaths(const std::string &_path);
202 
206  private: gz::sensors::SensorId LoadSensorPlugin(
207  const std::string &_filename, sdf::ElementPtr _sdf);
208 
209  IGN_COMMON_WARN_IGNORE__DLL_INTERFACE_MISSING
211  private: std::unique_ptr<ManagerPrivate> dataPtr;
212  IGN_COMMON_WARN_RESUME__DLL_INTERFACE_MISSING
213  };
214  }
215  }
216 }
217 
218 #endif
T * CreateSensor(sdf::ElementPtr _sdf)
Create a sensor from SDF with a known sensor type.
Definition: gz/sensors/Manager.hh:119
Definition: gz/sensors/AirPressureSensor.hh:32
STL class.
virtual ~Manager()
destructor
Loads and runs sensors.
Definition: gz/sensors/Manager.hh:52
std::size_t SensorId
A string used to identify a sensor.
Definition: gz/sensors/Sensor.hh:49
const SensorId NO_SENSOR
Definition: gz/sensors/Sensor.hh:50
void AddPluginPaths(const std::string &_path)
Adds colon delimited paths sensor plugins may be.
void RunOnce(const gz::common::Time &_time, bool _force=false)
Run the sensor generation one step.
T endl(T... args)
bool Init()
Initialize the sensor library without rendering or physics.
Sensor * Sensor(SensorId _id)
Get an instance of a loaded sensor by sensor id.
a base sensor class
Definition: gz/sensors/Sensor.hh:60
T * CreateSensor(sdf::Sensor _sdf)
Create a sensor from SDF with a known sensor type.
Definition: gz/sensors/Manager.hh:82
bool Remove(const SensorId _id)
Remove a sensor by ID.