-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathSensorFactory.cc
202 lines (172 loc) · 5.38 KB
/
SensorFactory.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* Copyright (C) 2019 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <ignition/common/SystemPaths.hh>
#include <ignition/common/Console.hh>
#include "ignition/sensors/config.hh"
#include "ignition/sensors/SensorFactory.hh"
/// \brief Private data class for SensorFactory
class ignition::sensors::SensorFactoryPrivate
{
/// \brief Constructor
public: SensorFactoryPrivate();
/// \brief A map of loaded sensor plugins and their type.
public: std::map<std::string, std::shared_ptr<SensorPlugin>> sensorPlugins;
/// \brief Stores paths to search for on file system
public: ignition::common::SystemPaths systemPaths;
/// \brief For loading plugins
public: ignition::plugin::Loader pluginLoader;
};
using namespace ignition;
using namespace sensors;
//////////////////////////////////////////////////
SensorFactoryPrivate::SensorFactoryPrivate()
{
this->systemPaths.AddPluginPaths(IGN_SENSORS_PLUGIN_PATH);
}
//////////////////////////////////////////////////
void SensorFactory::AddPluginPaths(const std::string &_path)
{
this->dataPtr->systemPaths.AddPluginPaths(_path);
}
//////////////////////////////////////////////////
SensorFactory::SensorFactory() : dataPtr(new SensorFactoryPrivate)
{
}
//////////////////////////////////////////////////
SensorFactory::~SensorFactory()
{
}
//////////////////////////////////////////////////
std::shared_ptr<SensorPlugin> SensorFactory::LoadSensorPlugin(
const std::string &_filename)
{
std::string fullPath =
this->dataPtr->systemPaths.FindSharedLibrary(_filename);
if (fullPath.empty())
{
ignerr << "Unable to find sensor plugin path for [" << _filename << "]\n";
return std::shared_ptr<SensorPlugin>();
}
auto pluginNames = this->dataPtr->pluginLoader.LoadLib(fullPath);
if (pluginNames.empty())
{
ignerr << "Unable to load sensor plugin file for [" << fullPath << "]\n";
return std::shared_ptr<SensorPlugin>();
}
// Assume the last plugin is the one we're interested in
std::string pluginName = "";
for (auto name : pluginNames)
{
pluginName = name;
}
plugin::PluginPtr pluginPtr =
this->dataPtr->pluginLoader.Instantiate(pluginName);
auto sensorPlugin = pluginPtr->QueryInterfaceSharedPtr<
ignition::sensors::SensorPlugin>();
return sensorPlugin;
}
/////////////////////////////////////////////////
std::unique_ptr<Sensor> SensorFactory::CreateSensor(const sdf::Sensor &_sdf)
{
std::unique_ptr<Sensor> result;
std::shared_ptr<SensorPlugin> sensorPlugin;
std::string type = _sdf.TypeStr();
std::string fullPath = IGN_SENSORS_PLUGIN_NAME(type);
auto it = this->dataPtr->sensorPlugins.find(type);
if (it != this->dataPtr->sensorPlugins.end())
sensorPlugin = it->second;
else
{
sensorPlugin = this->LoadSensorPlugin(fullPath);
if (!sensorPlugin)
{
ignerr << "Unable to instantiate sensor plugin for [" << fullPath
<< "]\n";
return nullptr;
}
this->dataPtr->sensorPlugins[type] = sensorPlugin;
}
auto sensor = sensorPlugin->New();
if (!sensor)
{
ignerr << "Unable to instantiate sensor for [" << fullPath << "]\n";
return nullptr;
}
if (!sensor->Load(_sdf))
{
ignerr << "Sensor::Load failed for plugin [" << fullPath << "]\n";
return nullptr;
}
if (!sensor->Init())
{
ignerr << "Sensor::Init failed for plugin [" << fullPath << "]\n";
return nullptr;
}
result.reset(sensor);
return std::move(result);
}
/////////////////////////////////////////////////
std::unique_ptr<Sensor> SensorFactory::CreateSensor(sdf::ElementPtr _sdf)
{
std::unique_ptr<Sensor> result;
if (_sdf)
{
if (_sdf->GetName() == "sensor")
{
std::shared_ptr<SensorPlugin> sensorPlugin;
std::string type = _sdf->Get<std::string>("type");
std::string fullPath = IGN_SENSORS_PLUGIN_NAME(type);
auto it = this->dataPtr->sensorPlugins.find(type);
if (it != this->dataPtr->sensorPlugins.end())
sensorPlugin = it->second;
else
{
sensorPlugin = this->LoadSensorPlugin(fullPath);
if (!sensorPlugin)
{
ignerr << "Unable to instantiate sensor plugin for [" << fullPath
<< "]\n";
return nullptr;
}
this->dataPtr->sensorPlugins[type] = sensorPlugin;
}
auto sensor = sensorPlugin->New();
if (!sensor)
{
ignerr << "Unable to instantiate sensor for [" << fullPath << "]\n";
return nullptr;
}
if (!sensor->Load(_sdf))
{
ignerr << "Sensor::Load failed for plugin [" << fullPath << "]\n";
return nullptr;
}
if (!sensor->Init())
{
ignerr << "Sensor::Init failed for plugin [" << fullPath << "]\n";
return nullptr;
}
result.reset(sensor);
return result;
}
else
{
ignerr << "Provided SDF is not a <sensor> element.\n";
}
}
return nullptr;
}