Skip to content

Commit 6faf038

Browse files
authored
Merge pull request #211 from robotology/remapper_local_port
2 parents 2e9199d + 77c8b8f commit 6faf038

File tree

2 files changed

+52
-20
lines changed

2 files changed

+52
-20
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ CMakeLists.txt.*
1010
*.*~
1111
.vscode/c_cpp_properties.json
1212
.vscode/settings.json
13+
.vs/*
14+
CMakeSettings.json

devices/IWearRemapper/src/IWearRemapper.cpp

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class IWearRemapper::impl
3636
bool firstRun = true;
3737
bool terminationCall = false;
3838
bool inputDataPorts = false;
39-
39+
4040
bool allowDynamicData = true;
4141

4242
// Flag to wait for first data received
@@ -148,7 +148,7 @@ IWearRemapper::~IWearRemapper() = default;
148148
bool IWearRemapper::open(yarp::os::Searchable& config)
149149
{
150150
// =====================
151-
// CHECK THE INPUT PORTS
151+
// CHECK THE INPUT PORTS
152152
// =====================
153153

154154
// wait for attachAll
@@ -169,7 +169,7 @@ bool IWearRemapper::open(yarp::os::Searchable& config)
169169
pImpl->allowDynamicData = config.find("allowDynamicData").asBool();
170170
}
171171
yInfo() << logPrefix << "Using allowDynamicData parameter:"<<pImpl->allowDynamicData;
172-
172+
173173
pImpl->inputDataPorts = config.check("wearableDataPorts");
174174

175175
if (pImpl->inputDataPorts) {
@@ -183,16 +183,46 @@ bool IWearRemapper::open(yarp::os::Searchable& config)
183183
yarp::os::Bottle* inputDataPortsNamesList = config.find("wearableDataPorts").asList();
184184

185185
if (inputDataPortsNamesList->size() == 0) {
186-
pImpl->inputDataPorts = false;
186+
pImpl->inputDataPorts = false;
187187
}
188188
else {
189189
for (unsigned i = 0; i < inputDataPortsNamesList->size(); ++i) {
190190
if (!inputDataPortsNamesList->get(i).isString()) {
191-
yError() << logPrefix << "ith entry of wearableDataPorts list is not a string";
191+
yError() << logPrefix << i << "th entry of wearableDataPorts list is not a string";
192192
return false;
193193
}
194194
}
195195

196+
std::vector<std::string> localNames;
197+
if (config.check("wearableDataLocals")) {
198+
if (!config.find("wearableDataLocals").isList()) {
199+
yError() << logPrefix << "wearableDataLocals option found, but it is not a list.";
200+
return false;
201+
}
202+
yarp::os::Bottle* localDataPortsNamesList =
203+
config.find("wearableDataLocals").asList();
204+
if (localDataPortsNamesList->size() != inputDataPortsNamesList->size()) {
205+
yError() << logPrefix
206+
<< "wearableDataLocals and wearableDataPorts have different sizes.";
207+
return false;
208+
}
209+
for (unsigned i = 0; i < localDataPortsNamesList->size(); ++i) {
210+
if (!localDataPortsNamesList->get(i).isString()) {
211+
yError() << logPrefix << i
212+
<< "th entry of wearableDataLocals list is not a string.";
213+
return false;
214+
}
215+
localNames.push_back(localDataPortsNamesList->get(i).asString());
216+
}
217+
}
218+
else {
219+
yInfo() << logPrefix
220+
<< "wearableDataLocals option not found, using temporary port names for the local ports.";
221+
for (unsigned i = 0; i < inputDataPortsNamesList->size(); ++i) {
222+
localNames.push_back("..."); // use temporary port names
223+
}
224+
}
225+
196226
// ===============================
197227
// PARSE THE CONFIGURATION OPTIONS
198228
// ===============================
@@ -222,11 +252,11 @@ bool IWearRemapper::open(yarp::os::Searchable& config)
222252
// ==========================
223253
yDebug() << logPrefix << "Configuring input data ports";
224254

225-
for (unsigned i = 0; i < config.find("wearableDataPorts").asList()->size(); ++i) {
255+
for (unsigned i = 0; i < inputDataPortsNamesList->size(); ++i) {
226256
pImpl->inputPortsWearData.emplace_back(new yarp::os::BufferedPort<msg::WearableData>());
227257
pImpl->inputPortsWearData.back()->useCallback(*this);
228258

229-
if (!pImpl->inputPortsWearData.back()->open("...")) {
259+
if (!pImpl->inputPortsWearData.back()->open(localNames[i])) {
230260
yError() << logPrefix << "Failed to open local input port";
231261
return false;
232262
}
@@ -238,7 +268,14 @@ bool IWearRemapper::open(yarp::os::Searchable& config)
238268
// ================
239269
yDebug() << logPrefix << "Opening input ports";
240270

241-
for (unsigned i = 0; i < config.find("wearableDataPorts").asList()->size(); ++i) {
271+
// Initialize the network
272+
pImpl->network = yarp::os::Network();
273+
if (!yarp::os::Network::initialized() || !yarp::os::Network::checkNetwork(5.0)) {
274+
yError() << logPrefix << "YARP server wasn't found active.";
275+
return false;
276+
}
277+
278+
for (unsigned i = 0; i < inputDataPortsNamesList->size(); ++i) {
242279
if (!yarp::os::Network::connect(inputDataPortsNamesVector[i],
243280
pImpl->inputPortsWearData[i]->getName(),
244281
carrier)) {
@@ -248,22 +285,15 @@ bool IWearRemapper::open(yarp::os::Searchable& config)
248285
}
249286
}
250287

251-
// Initialize the network
252-
pImpl->network = yarp::os::Network();
253-
if (!yarp::os::Network::initialized() || !yarp::os::Network::checkNetwork(5.0)) {
254-
yError() << logPrefix << "YARP server wasn't found active.";
255-
return false;
256-
}
257-
258288
// If it not necessary to wait for the attachAll start the callbacks
259289
// We use callbacks on the input ports, the loop is a no-op
260290
if (!pImpl->waitForAttachAll) {
261291
start();
262292
}
263-
293+
264294
}
265295
}
266-
296+
267297

268298
yDebug() << logPrefix << "Opened correctly";
269299
return true;
@@ -311,7 +341,7 @@ bool IWearRemapper::impl::updateData(msg::WearableData& receivedWearData, bool c
311341
// ====================
312342
// EXPOSE THE INTERFACE
313343
// ====================
314-
344+
315345
auto isensor = getOrCreateSensor<const sensor::IAccelerometer, sensor::impl::Accelerometer>(
316346
inputSensorName, sensor::SensorType::Accelerometer, accelerometers, create);
317347

@@ -751,7 +781,7 @@ void IWearRemapper::onRead(msg::WearableData& wearData, const yarp::os::TypedRea
751781
allRead = false;
752782
}
753783
}
754-
784+
755785
if(allRead)
756786
{
757787
pImpl->firstRun = false;
@@ -985,7 +1015,7 @@ IWearRemapper::getSensors(const sensor::SensorType type) const
9851015
{
9861016
pImpl->mutex.lock();
9871017
}
988-
1018+
9891019
switch (type) {
9901020
case sensor::SensorType::Accelerometer:
9911021
for (const auto& s : pImpl->accelerometers) {

0 commit comments

Comments
 (0)