14#include <yarp/os/LogComponent.h>
15#include <yarp/os/LogStream.h>
16#include <yarp/os/Time.h>
18#define SKIN_THRESHOLD 15.0
22YARP_LOG_COMPONENT(SKINMESHCLIENT,
"yarp.tools.skinmeshclient")
24constexpr double noSkinPatchWarningPeriod = 60.0;
26std::string makeLocalPrefix(yarp::os::Searchable& config)
28 std::string prefix =
"/skinGui";
29 if (config.check(
"name"))
32 prefix += config.find(
"name").asString();
35 if (config.check(
"robotPart"))
38 prefix += config.find(
"robotPart").asString();
48 m_period =
static_cast<double>(period) / 1000.0;
49 m_config.fromString(config.toString());
50 yCDebug(SKINMESHCLIENT) <<
"SkinMeshClient running at" <<
static_cast<int>(1000.0 *
getPeriod()) <<
"ms.";
60bool SkinMeshClient::configureFromSearchable(yarp::os::Searchable& config)
69 const std::string localPrefix = makeLocalPrefix(config);
70 const std::string part_virtual = localPrefix +
"_virtual:i";
80 if (!configureSkinClient(config, localPrefix))
85 int width =config.check(
"width") ? config.find(
"width").asInt32() : 320;
86 int height=config.check(
"height") ? config.find(
"height").asInt32() : 320;
88 bool useCalibration = config.check(
"useCalibration");
91 yCInfo(SKINMESHCLIENT) <<
"Using calibrated skin values (0-255)";
95 yCDebug(SKINMESHCLIENT) <<
"Using raw skin values (255-0)";
98 Bottle *color = config.find(
"color").asList();
99 unsigned char r=255, g=0, b=0;
102 if(color->size()<3 || !color->get(0).isInt32() || !color->get(1).isInt32() || !color->get(2).isInt32())
104 yCError(SKINMESHCLIENT) <<
"Error while reading the parameter color: three integer values should be specified"
105 << color->toString();
109 r = color->get(0).asInt32();
110 g = color->get(1).asInt32();
111 b = color->get(2).asInt32();
112 yCInfo(SKINMESHCLIENT) <<
"Using specified color:" << r << g << b;
117 yCDebug(SKINMESHCLIENT) <<
"Using red as default color.";
124 yCDebug(SKINMESHCLIENT) <<
"Skin threshold set to" <<
skinThreshold;
126 yarp::os::Bottle sensorSetConfig=config.findGroup(
"SENSORS").tail();
128 for (
int t=0; t<sensorSetConfig.size(); ++t)
130 yarp::os::Bottle sensorConfig(sensorSetConfig.get(t).toString());
131 std::string type(sensorConfig.get(0).asString());
133 if (type==
"triangle" ||
135 type==
"fingertip2L" ||
136 type==
"fingertip2R" ||
137 type==
"fingertip3L" ||
138 type==
"fingertip3R" ||
139 type==
"fingertip4L" ||
140 type==
"fingertip4R" ||
141 type==
"fingertipMID" ||
142 type==
"triangle_10pad" ||
147 type ==
"cer_sh_pdl" ||
148 type ==
"cer_sh_pdr" ||
149 type ==
"cer_sh_pp" ||
150 type ==
"cer_sh_td" ||
153 int id=sensorConfig.get(1).asInt32();
154 double xc=sensorConfig.get(2).asFloat64();
155 double yc=sensorConfig.get(3).asFloat64();
156 double th=sensorConfig.get(4).asFloat64();
157 double gain=sensorConfig.get(5).asFloat64();
158 int lrMirror=sensorConfig.get(6).asInt32();
159 int layoutNum=sensorConfig.get(7).asInt32();
161 yCDebug(SKINMESHCLIENT) << type <<
id << gain;
167 yCError(SKINMESHCLIENT) <<
"Triangle" <<
id <<
"already exists.";
171 if (type==
"triangle")
173 sensor[id]=
new Triangle(xc,yc,th,gain,layoutNum,lrMirror);
175 else if (type==
"triangle_10pad")
177 sensor[id]=
new Triangle_10pad(xc,yc,th,gain,layoutNum,lrMirror);
179 else if (type==
"fingertip")
181 sensor[id]=
new Fingertip(xc,yc,th,gain,layoutNum,lrMirror);
183 else if (type==
"fingertip2L")
185 sensor[id]=
new Fingertip2L(xc,yc,th,gain,layoutNum,lrMirror);
187 else if (type==
"fingertip2R")
189 sensor[id]=
new Fingertip2R(xc,yc,th,gain,layoutNum,lrMirror);
191 else if (type==
"fingertip3L")
193 sensor[id]=
new Fingertip3L(xc,yc,th,gain,layoutNum,lrMirror);
195 else if (type==
"fingertip3R")
197 sensor[id]=
new Fingertip3R(xc,yc,th,gain,layoutNum,lrMirror);
199 else if (type==
"fingertip4L")
201 sensor[id]=
new Fingertip4L(xc,yc,th,gain,layoutNum,lrMirror);
203 else if (type==
"fingertip4R")
205 sensor[id]=
new Fingertip4R(xc,yc,th,gain,layoutNum,lrMirror);
207 if (type==
"fingertipMID")
209 sensor[id]=
new FingertipMID(xc,yc,th,gain,layoutNum,lrMirror);
211 else if (type==
"quad16")
213 sensor[id]=
new Quad16(xc,yc,th,gain,layoutNum,lrMirror);
215 else if (type ==
"cer_sh_pdl")
217 sensor[id] =
new CER_SH_PDL(xc, yc, th, gain, layoutNum, lrMirror);
219 else if (type==
"palmR")
221 sensor[id]=
new PalmR(xc,yc,th,gain,layoutNum,lrMirror);
223 else if (type==
"fakePalm")
225 sensor[id]=
new fakePalm(xc,yc,th,gain,layoutNum,lrMirror);
227 else if (type==
"palmL")
229 sensor[id]=
new PalmL(xc,yc,th,gain,layoutNum,lrMirror);
231 else if (type ==
"cer_sh_pdl")
233 sensor[id] =
new CER_SH_PDL(xc, yc, th, gain, layoutNum, lrMirror);
235 else if (type ==
"cer_sh_pdr")
237 sensor[id] =
new CER_SH_PDR(xc, yc, th, gain, layoutNum, lrMirror);
239 else if (type ==
"cer_sh_pp")
241 sensor[id] =
new CER_SH_PP(xc, yc, th, gain, layoutNum, lrMirror);
243 else if (type ==
"cer_sh_td")
245 sensor[id] =
new CER_SH_TD(xc, yc, th, gain, layoutNum, lrMirror);
247 else if (type ==
"cer_sh_tp")
249 sensor[id] =
new CER_SH_TP(xc, yc, th, gain, layoutNum, lrMirror);
252 sensor[id]->setCalibrationFlag(useCalibration);
258 yCWarning(SKINMESHCLIENT) <<
id <<
"is invalid triangle Id [0:" <<
MAX_SENSOR_NUM - 1 <<
"].";
263 yCWarning(SKINMESHCLIENT) <<
"Sensor type" << type <<
"unknown, discarded.";
273 sensor[t]->min_tax=max_tax;
275 sensor[t]->max_tax=max_tax-1;
276 sensor[t]->setColor(r, g, b);
292bool SkinMeshClient::configureSkinClient(yarp::os::Searchable& config,
const std::string& localPrefix)
294 const std::string local = config.check(
"local") ? config.find(
"local").asString() : localPrefix;
295 const bool externalConnection = config.check(
"externalConnection") ? config.find(
"externalConnection").asBool() :
true;
298 if (!externalConnection && !config.check(
"remote"))
300 yCError(SKINMESHCLIENT) <<
"SkinMeshClient: externalConnection false requires a remote MAS server prefix.";
304 yarp::os::Property clientOptions;
305 clientOptions.put(
"device",
"multipleanalogsensorsclient");
306 clientOptions.put(
"local", local);
307 clientOptions.put(
"remote", externalConnection ? local : config.find(
"remote").asString());
308 clientOptions.put(
"timeout", config.check(
"timeout") ? config.find(
"timeout").asFloat64() :
getPeriod() * 2.0);
309 clientOptions.put(
"externalConnection", externalConnection);
310 if (config.check(
"carrier"))
312 clientOptions.put(
"carrier", config.find(
"carrier").asString());
315 if (!_multipleAnalogSensorsClientDevice.open(clientOptions))
317 yCError(SKINMESHCLIENT) <<
"SkinMeshClient: unable to open multipleanalogsensorsclient with options"
318 << clientOptions.toString();
322 yarp::dev::ISkinPatches* clientSkinPatches =
nullptr;
323 if (!_multipleAnalogSensorsClientDevice.view(clientSkinPatches) || clientSkinPatches ==
nullptr)
325 yCError(SKINMESHCLIENT) <<
"SkinMeshClient: unable to view ISkinPatches from multipleanalogsensorsclient.";
329 if (externalConnection)
331 remappedMASInterfaces.iskinPatchesSensors = clientSkinPatches;
332 yCInfo(SKINMESHCLIENT) <<
"Waiting for external MAS streaming connection on" << local +
"/measures:i";
333 yCInfo(SKINMESHCLIENT) <<
"Internal MultipleAnalogSensorsRemapper is not used with externalConnection true"
334 <<
"because MultipleAnalogSensorsClient metadata is unavailable in that mode.";
338 yarp::os::Bottle skinPatchNames;
339 const size_t skinPatchCount = clientSkinPatches->getNrOfSkinPatches();
340 yCDebug(SKINMESHCLIENT) <<
"Number of skin patches available from the multipleanalogsensorsclient:" << skinPatchCount;
342 if (skinPatchCount == 0)
344 yCError(SKINMESHCLIENT) <<
"SkinMeshClient: no skin patches available from the multipleanalogsensorsclient.";
348 for (
size_t skinPatchIndex = 0; skinPatchIndex < skinPatchCount; ++skinPatchIndex)
350 std::string skinPatchName;
351 if (!clientSkinPatches->getSkinPatchName(skinPatchIndex, skinPatchName))
353 yCError(SKINMESHCLIENT) <<
"SkinMeshClient: unable to retrieve name for skin patch"
355 <<
"from multipleanalogsensorsclient.";
359 yCDebug(SKINMESHCLIENT) <<
"Name of found skin patch:" << skinPatchName;
360 skinPatchNames.addString(skinPatchName);
363 if (skinPatchNames.size() !=
static_cast<int>(skinPatchCount))
365 yCError(SKINMESHCLIENT) <<
"SkinMeshClient: inconsistent skin patch metadata. Expected"
367 <<
"names but collected"
368 << skinPatchNames.size();
372 yarp::os::Property remapperOptions;
373 remapperOptions.put(
"device",
"multipleanalogsensorsremapper");
375 yarp::os::Bottle skinPatchNamesOption;
376 yarp::os::Bottle& skinPatchNamesList = skinPatchNamesOption.addList();
377 for (
int i = 0; i < skinPatchNames.size(); ++i)
379 skinPatchNamesList.addString(skinPatchNames.get(i).asString());
381 remapperOptions.put(
"SkinPatchesNames", skinPatchNamesOption.get(0));
383 if (!_multipleAnalogSensorsRemapperDevice.open(remapperOptions))
385 yCError(SKINMESHCLIENT) <<
"SkinMeshClient: unable to open multipleanalogsensorsremapper with options"
386 << remapperOptions.toString();
390 if (!_multipleAnalogSensorsRemapperDevice.view(remappedMASInterfaces.imultwrap) ||
391 remappedMASInterfaces.imultwrap ==
nullptr)
393 yCError(SKINMESHCLIENT) <<
"SkinMeshClient: unable to view IMultipleWrapper from multipleanalogsensorsremapper.";
397 yarp::dev::PolyDriverList clientDeviceList;
398 clientDeviceList.push(&_multipleAnalogSensorsClientDevice,
"skin_mas_client");
399 if (!remappedMASInterfaces.imultwrap->attachAll(clientDeviceList))
401 yCError(SKINMESHCLIENT) <<
"SkinMeshClient: unable to attach multipleanalogsensorsclient to remapper.";
405 if (!_multipleAnalogSensorsRemapperDevice.view(remappedMASInterfaces.iskinPatchesSensors) ||
406 remappedMASInterfaces.iskinPatchesSensors ==
nullptr)
408 yCError(SKINMESHCLIENT) <<
"SkinMeshClient: unable to view ISkinPatches from multipleanalogsensorsremapper.";
412 const size_t remappedSkinPatchCount = remappedMASInterfaces.iskinPatchesSensors->getNrOfSkinPatches();
413 if (remappedSkinPatchCount != skinPatchCount)
415 yCError(SKINMESHCLIENT) <<
"SkinMeshClient: MultipleAnalogSensorsRemapper exposes"
416 << remappedSkinPatchCount
417 <<
"skin patches, expected"
422 yCInfo(SKINMESHCLIENT) <<
"Reading skin patches through MultipleAnalogSensorsRemapper:" << skinPatchNames.toString();
426bool SkinMeshClient::readSkinPatches(yarp::sig::Vector& skinValue)
const
428 if (remappedMASInterfaces.iskinPatchesSensors ==
nullptr)
433 const size_t skinPatchCount = remappedMASInterfaces.iskinPatchesSensors->getNrOfSkinPatches();
434 if (skinPatchCount == 0)
436 const double now = yarp::os::Time::now();
441 yCWarning(SKINMESHCLIENT) <<
"SkinMeshClient: no skin patches available yet from the MAS client."
442 <<
"Waiting for the external streaming connection.";
448 yCWarning(SKINMESHCLIENT) <<
"SkinMeshClient: no skin patches available from the active MAS skin patches interface.";
455 std::vector<size_t> skinPatchSizes(skinPatchCount, 0);
456 size_t totalSkinPatchSize = 0;
457 for (
size_t skinPatchIndex = 0; skinPatchIndex < skinPatchCount; ++skinPatchIndex)
459 skinPatchSizes[skinPatchIndex] = remappedMASInterfaces.iskinPatchesSensors->getSkinPatchSize(skinPatchIndex);
460 totalSkinPatchSize += skinPatchSizes[skinPatchIndex];
463 skinValue.resize(totalSkinPatchSize, 0.0);
464 size_t skinValueOffset = 0;
465 bool gotAnyPatch =
false;
467 for (
size_t skinPatchIndex = 0; skinPatchIndex < skinPatchCount; ++skinPatchIndex)
469 if (remappedMASInterfaces.iskinPatchesSensors->getSkinPatchStatus(skinPatchIndex) != yarp::dev::MAS_OK)
471 skinValueOffset += skinPatchSizes[skinPatchIndex];
475 yarp::sig::Vector skinPatchValue;
476 double timestamp = 0.0;
477 if (!remappedMASInterfaces.iskinPatchesSensors->getSkinPatchMeasure(skinPatchIndex, skinPatchValue, timestamp))
479 skinValueOffset += skinPatchSizes[skinPatchIndex];
483 const size_t taxelsToCopy = std::min(skinPatchSizes[skinPatchIndex], skinPatchValue.size());
484 for (
size_t i = 0; i < taxelsToCopy; ++i)
486 skinValue[skinValueOffset + i] = skinPatchValue[i];
489 skinValueOffset += skinPatchSizes[skinPatchIndex];
498 if (!configureFromSearchable(m_config))
504 return runModuleThreaded() == 0;
514 return configureFromSearchable(rf);
524 std::lock_guard<std::mutex> lck(
mtx);
526 bool gotRealData=
false;
527 bool gotVirtualData=
false;
528 yarp::sig::Vector skin_value;
529 yarp::sig::Vector skin_value_virtual;
530 std::vector<unsigned char> skin_color_virtual;
532 if (readSkinPatches(skin_value))
534 yCTrace(SKINMESHCLIENT) <<
"Reading real contacts from ISkinPatches...";
541 Bottle *data_virtual = input_virtual->get(0).asList();
542 Bottle *color_virtual = input_virtual->get(1).asList();
543 if (data_virtual !=
nullptr && color_virtual !=
nullptr && color_virtual->size() >= 3)
545 yCTrace(SKINMESHCLIENT) <<
"Reading from virtual contacts...";
548 skin_value_virtual.resize(data_virtual->size(),0.0);
550 for (
int i=0; i<data_virtual->size(); i++)
552 skin_value_virtual[i] = data_virtual->get(i).asFloat64();
555 for (
int i = 0; i<color_virtual->size(); i++)
557 skin_color_virtual.push_back(color_virtual->get(i).asInt32());
560 yCTrace(SKINMESHCLIENT) <<
"Virtual contacts:" << skin_value_virtual.toString(3,3);
561 yCTrace(SKINMESHCLIENT) <<
"Virtual contacts color:" << skin_color_virtual[0] << skin_color_virtual[1] << skin_color_virtual[2];
565 if (!gotRealData && !gotVirtualData)
569 if (
sensor[sensorId] !=
nullptr)
571 sensor[sensorId]->clearActivation();
579 if (
sensor[sensorId]==0)
continue;
582 bool isRealSensorOverThreshold=
false;
585 for (
int i=
sensor[sensorId]->min_tax; i<=
sensor[sensorId]->max_tax && i<static_cast<int>(skin_value.size()); i++)
589 isRealSensorOverThreshold =
true;
596 bool isVirtualSensorOverThreshold=
false;
599 for (
int i=
sensor[sensorId]->min_tax; i<=
sensor[sensorId]->max_tax && i<static_cast<int>(skin_value_virtual.size()); i++)
603 isVirtualSensorOverThreshold =
true;
613 if (gotRealData && gotVirtualData)
616 for (
int i=
sensor[sensorId]->min_tax; i<=
sensor[sensorId]->max_tax; i++)
618 int curr_tax = i-
sensor[sensorId]->min_tax;
622 if (isRealSensorOverThreshold && i<
static_cast<int>(skin_value.size()))
624 sensor[sensorId]->setActivationFromPortData(skin_value[i],curr_tax);
627 else if(isVirtualSensorOverThreshold && i<
static_cast<int>(skin_value_virtual.size()))
629 sensor[sensorId]->setActivationFromPortData(skin_value_virtual[i],curr_tax);
630 sensor[sensorId]->setColor(skin_color_virtual[0],skin_color_virtual[1],skin_color_virtual[2]);
632 else if (i<
static_cast<int>(skin_value.size()))
634 sensor[sensorId]->setActivationFromPortData(skin_value[i],curr_tax);
639 else if (gotRealData || gotVirtualData)
641 for (
int i=
sensor[sensorId]->min_tax; i<=
sensor[sensorId]->max_tax; i++)
643 int curr_tax = i-
sensor[sensorId]->min_tax;
645 if (gotRealData && i<
static_cast<int>(skin_value.size()))
647 sensor[sensorId]->setActivationFromPortData(skin_value[i],curr_tax);
650 else if (gotVirtualData && i<
static_cast<int>(skin_value_virtual.size()))
652 sensor[sensorId]->setActivationFromPortData(skin_value_virtual[i],curr_tax);
653 sensor[sensorId]->setColor(skin_color_virtual[0],skin_color_virtual[1],skin_color_virtual[2]);
664 yCDebug(SKINMESHCLIENT) <<
"SkinMeshClient releasing...";
666 if (remappedMASInterfaces.imultwrap !=
nullptr)
668 remappedMASInterfaces.imultwrap->detachAll();
670 remappedMASInterfaces.imultwrap =
nullptr;
671 remappedMASInterfaces.iskinPatchesSensors =
nullptr;
673 _multipleAnalogSensorsRemapperDevice.close();
674 _multipleAnalogSensorsClientDevice.close();
680 yCDebug(SKINMESHCLIENT) <<
"SkinMeshClient released.";
SkinMeshClient(Searchable &config, int period)
BufferedPort< Bottle > skin_port
TouchSensor * sensor[MAX_SENSOR_NUM]
static const int MAX_SENSOR_NUM
virtual bool configure(yarp::os::ResourceFinder &rf) override
bool m_externalConnection
std::vector< unsigned char > defaultColor
virtual double getPeriod() override
void resize(int width, int height)
double m_lastNoSkinPatchWarningTime
virtual bool updateModule() override
BufferedPort< Bottle > skin_port_virtual
virtual bool close() override