28 using namespace yarp::os;
29 using namespace yarp::sig;
34 bool TactileFinger::fromProperty(
const Property &options)
36 if (!options.check(
"name"))
43 name=options.find(
"name").asString();
44 directLogic=(options.check(
"logic",Value(
"direct")).asString()==
"direct");
45 outputGain=options.check(
"output_gain",Value(1.0)).asFloat64();
47 if ((name==
"thumb") || (name==
"index") || (name==
"middle") ||
48 (name==
"ring") || (name==
"little"))
56 void TactileFinger::toProperty(Property &options)
const
59 options.put(
"name",name);
60 options.put(
"logic",directLogic?
"direct":
"inverse");
61 options.put(
"output_gain",outputGain);
66 bool TactileFinger::toStream(ostream &str)
const
68 str<<
"name "<<name<<endl;
69 str<<
"logic "<<(directLogic?
"direct":
"inverse")<<endl;
70 str<<
"output_gain "<<outputGain<<endl;
82 yWarning(
"TactileFinger: calibration not available");
88 bool TactileFinger::isCalibrated()
const
90 yWarning(
"TactileFinger: calibration not available");
96 bool TactileFinger::getSensorsData(Value &
data)
const
99 for (
int j=0; j<12; j++)
104 map<string,Sensor*>::const_iterator In=sensors.find(tag.str());
105 if (In==sensors.end())
108 Value val_in; In->second->getOutput(val_in);
109 in[j]=val_in.asFloat64();
115 b.addList().read(in);
116 prop.put(
"in",b.get(0));
118 b.addList().read(prop);
126 bool TactileFinger::extractSensorsData(Vector &in)
const
131 if (getSensorsData(
data))
133 if (Bottle *b1=
data.asList())
135 if (Bottle *b2=b1->find(
"in").asList())
137 in.resize(b2->size());
138 for (
size_t i=0; i<in.length(); i++)
139 in[i]=b2->get(i).asFloat64();
151 bool TactileFinger::getOutput(Value &
out)
const
154 if (!extractSensorsData(i))
158 for (
size_t j=0; j<i.length(); j++)
159 ret=
std::max(ret,directLogic?(255.0-i[j]):i[j]);
161 out=Value(outputGain*ret);
168 TactileFingersModel::TactileFingersModel()
176 bool TactileFingersModel::fromProperty(
const Property &options)
178 if (!options.check(
"name") || !options.check(
"type"))
180 printMessage(
log::error,1,
"missing mandatory options \"name\" and/or \"type\"");
187 name=options.find(
"name").asString();
188 type=options.find(
"type").asString();
189 robot=options.check(
"robot",Value(
"icub")).asString();
190 carrier=options.check(
"carrier",Value(
"udp")).asString();
191 compensation=(options.check(
"compensation",Value(
"false")).asString()==
"true");
192 verbosity=options.check(
"verbosity",Value(0)).asInt32();
194 port->open(
"/"+name+
"/"+type+
"_hand:i");
195 string skinPortName(
"/"+robot+
"/skin/"+type+
"_hand");
197 skinPortName+=
"_comp";
199 if (!Network::connect(skinPortName,port->getName(),carrier))
201 printMessage(
log::error,1,
"unable to connect to %s",skinPortName.c_str());
206 printMessage(
log::info,1,
"configuring port-based sensors ...");
207 void *pPort=
static_cast<void*
>(port);
208 for (
int j=0; j<60; j++)
214 prop.put(
"name",tag.str());
217 if (!sensPort[j].configure(pPort,prop))
219 printMessage(
log::error,1,
"some errors occured");
225 printMessage(
log::info,1,
"configuring fingers ...");
226 Property thumb(options.findGroup(
"thumb").toString().c_str());
227 Property index(options.findGroup(
"index").toString().c_str());
228 Property middle(options.findGroup(
"middle").toString().c_str());
229 Property ring(options.findGroup(
"ring").toString().c_str());
230 Property little(options.findGroup(
"little").toString().c_str());
232 bool fingers_ok=
true;
233 fingers_ok&=fingers[0].fromProperty(thumb);
234 fingers_ok&=fingers[1].fromProperty(index);
235 fingers_ok&=fingers[2].fromProperty(middle);
236 fingers_ok&=fingers[3].fromProperty(ring);
237 fingers_ok&=fingers[4].fromProperty(little);
241 printMessage(
log::error,1,
"some errors occured");
246 printMessage(
log::info,1,
"attaching sensors to fingers ...");
247 for (
int j=0; j<12; j++)
249 fingers[0].attachSensor(sensPort[j+4*12]);
250 fingers[1].attachSensor(sensPort[j]);
251 fingers[2].attachSensor(sensPort[j+12]);
252 fingers[3].attachSensor(sensPort[j+2*12]);
253 fingers[4].attachSensor(sensPort[j+3*12]);
256 attachNode(fingers[0]);
257 attachNode(fingers[1]);
258 attachNode(fingers[2]);
259 attachNode(fingers[3]);
260 attachNode(fingers[4]);
262 printMessage(
log::info,1,
"configuration complete");
263 return configured=
true;
268 void TactileFingersModel::toProperty(Property &options)
const
275 fingers[0].toProperty(prop[0]);
276 fingers[1].toProperty(prop[1]);
277 fingers[2].toProperty(prop[2]);
278 fingers[3].toProperty(prop[3]);
279 fingers[4].toProperty(prop[4]);
281 string thumb=
"(thumb ";
282 thumb+=prop[0].toString();
285 string index=
"(index ";
286 index+=prop[1].toString();
289 string middle=
"(middle ";
290 middle+=prop[2].toString();
293 string ring=
"(ring ";
294 ring+=prop[3].toString();
297 string little=
"(little ";
298 little+=prop[4].toString();
301 options.fromString(thumb+index+middle+ring+little);
302 options.put(
"name",name);
303 options.put(
"type",type);
304 options.put(
"robot",robot);
305 options.put(
"compensation",compensation?
"true":
"false");
312 bool TactileFingersModel::toStream(ostream &str)
const
316 str<<
"name "<<name<<endl;
317 str<<
"type "<<type<<endl;
318 str<<
"robot "<<robot<<endl;
319 str<<
"compensation "<<(compensation?
"true":
"false")<<endl;
323 str<<
"[thumb]"<<endl;
324 fingers[0].toStream(str);
327 str<<
"[index]"<<endl;
328 fingers[1].toStream(str);
331 str<<
"[middle]"<<endl;
332 fingers[2].toStream(str);
336 fingers[3].toStream(str);
339 str<<
"[little]"<<endl;
340 fingers[4].toStream(str);
352 printMessage(
log::warning,1,
"calibration not available");
358 bool TactileFingersModel::isCalibrated()
const
360 printMessage(
log::warning,1,
"calibration not available");
366 bool TactileFingersModel::getOutput(Value &
out)
const
371 fingers[0].getOutput(val[0]);
372 fingers[1].getOutput(val[1]);
373 fingers[2].getOutput(val[2]);
374 fingers[3].getOutput(val[3]);
375 fingers[4].getOutput(val[4]);
377 Bottle bOut; Bottle &ins=bOut.addList();
378 ins.addFloat64(val[0].asFloat64());
379 ins.addFloat64(val[1].asFloat64());
380 ins.addFloat64(val[2].asFloat64());
381 ins.addFloat64(val[3].asFloat64());
382 ins.addFloat64(val[4].asFloat64());
393 void TactileFingersModel::close()
397 if (!port->isClosed())
407 TactileFingersModel::~TactileFingersModel()