25#include <yarp/math/Math.h>
33using namespace yarp::os;
35using namespace yarp::sig;
36using namespace yarp::math;
45 if (!options.check(
"name"))
53 name=options.find(
"name").asString();
64 double defaultCalibVel;
65 if ((
name==
"thumb") || (
name==
"index") || (
name==
"middle"))
70 else if ((
name==
"ring") || (
name==
"little"))
79 outputGain=options.check(
"output_gain",Value(1.0)).asFloat64();
80 calibrated=(options.check(
"calibrated",Value(
"false")).asString()==
"true");
82 if (options.check(
"scaler"))
84 Bottle *pB=options.find(
"scaler").asList();
88 if (options.check(
"lssvm"))
90 Bottle *pB=options.find(
"lssvm").asList();
102 options.put(
"name",
name);
105 options.put(
"calibrated",
calibrated?
"true":
"false");
114 str<<
"name "<<
name<<endl;
117 str<<
"calibrated "<<(
calibrated?
"true":
"false")<<endl;
128 map<string,Sensor*>::const_iterator In_0=
sensors.find(
"In_0");
129 map<string,Sensor*>::const_iterator Out_0=
sensors.find(
"Out_0");
130 map<string,Sensor*>::const_iterator Out_1=
sensors.find(
"Out_1");
131 map<string,Sensor*>::const_iterator Out_2=
sensors.find(
"Out_2");
144 Value val_in, val_out[3];
145 In_0->second->getOutput(val_in);
146 Out_0->second->getOutput(val_out[0]);
147 Out_1->second->getOutput(val_out[1]);
150 in[0]=val_in.asFloat64();
153 out[0]=val_out[0].asFloat64();
154 out[1]=val_out[1].asFloat64();
158 Out_2->second->getOutput(val_out[2]);
159 out[2]=val_out[2].asFloat64();
165 b.addList().read(in);
166 prop.put(
"in",b.get(0));
168 b.addList().read(
out);
169 prop.put(
"out",b.get(1));
171 b.addList().read(prop);
186 if (Bottle *b1=
data.asList())
188 if (Bottle *b2=b1->find(
"in").asList())
190 in.resize(b2->size());
191 for (
size_t i=0; i<in.length(); i++)
192 in[i]=b2->get(i).asFloat64();
195 if (Bottle *b2=b1->find(
"out").asList())
197 out.resize(b2->size());
198 for (
size_t i=0; i<
out.length(); i++)
199 out[i]=b2->get(i).asFloat64();
220 for (
size_t j=0; j<pred.length(); j++)
232 if (options.check(
"reset"))
235 if (options.check(
"feed"))
241 for (
size_t i=0; i<
out.length(); i++)
250 if (options.check(
"train"))
270 if (!options.check(
"name") || !options.check(
"type"))
279 name=options.find(
"name").asString();
280 type=options.find(
"type").asString();
281 robot=options.check(
"robot",Value(
"icub")).asString();
282 carrier=options.check(
"carrier",Value(
"udp")).asString();
283 verbosity=options.check(
"verbosity",Value(0)).asInt32();
285 string part_motor=string(type+
"_arm");
286 string part_analog=string(type+
"_hand");
289 propEncs.put(
"device",
"remote_controlboard");
290 propEncs.put(
"remote",
"/"+robot+
"/"+part_motor);
291 propEncs.put(
"local",
"/"+
name+
"/"+part_motor);
292 if (!drvEncs.open(propEncs))
296 propMAIS.put(
"device",
"multipleanalogsensorsclient");
297 propMAIS.put(
"remote",
"/"+robot+
"/"+part_analog+
"/MAIS");
298 propMAIS.put(
"local",
"/"+
name+
"/"+part_analog+
"/MAIS");
299 if (!drvMAIS.open(propMAIS))
307 IEncoders *ienc; drvEncs.view(ienc);
308 int nAxes; ienc->getAxes(&nAxes);
311 propGen.put(
"name",
"In_0");
312 propGen.put(
"size",nAxes);
314 Property propThumb=propGen; propThumb.put(
"index",10);
315 Property propIndex=propGen; propIndex.put(
"index",12);
316 Property propMiddle=propGen; propMiddle.put(
"index",14);
317 Property propRing=propGen; propRing.put(
"index",15);
318 Property propLittle=propGen; propLittle.put(
"index",15);
320 bool sensors_ok=
true;
321 void *pEncs=
static_cast<void*
>(ienc);
322 sensors_ok&=sensEncs[0].
configure(pEncs,propThumb);
323 sensors_ok&=sensEncs[1].
configure(pEncs,propIndex);
324 sensors_ok&=sensEncs[2].
configure(pEncs,propMiddle);
325 sensors_ok&=sensEncs[3].
configure(pEncs,propRing);
326 sensors_ok&=sensEncs[4].
configure(pEncs,propLittle);
329 IEncoderArrays *iencarray; drvMAIS.view(iencarray);
332 propGen.put(
"num_arrays",(
int)iencarray->getNrOfEncoderArrays());
333 propGen.put(
"index_array",0);
335 Property thumb_mp=propGen; thumb_mp.put(
"name",
"Out_0"); thumb_mp.put(
"index_element",
"1");
336 Property thumb_ip=propGen; thumb_ip.put(
"name",
"Out_1"); thumb_ip.put(
"index_element",
"2");
337 Property index_mp=propGen; index_mp.put(
"name",
"Out_0"); index_mp.put(
"index_element",
"4");
338 Property index_ip=propGen; index_ip.put(
"name",
"Out_1"); index_ip.put(
"index_element",
"5");
339 Property middle_mp=propGen; middle_mp.put(
"name",
"Out_0"); middle_mp.put(
"index_element",
"7");
340 Property middle_ip=propGen; middle_ip.put(
"name",
"Out_1"); middle_ip.put(
"index_element",
"8");
341 Property ring_mp=propGen; ring_mp.put(
"name",
"Out_0"); ring_mp.put(
"index_element",
"9");
342 Property ring_pip=propGen; ring_pip.put(
"name",
"Out_1"); ring_pip.put(
"index_element",
"10");
343 Property ring_dip=propGen; ring_dip.put(
"name",
"Out_2"); ring_dip.put(
"index_element",
"11");
344 Property little_mp=propGen; little_mp.put(
"name",
"Out_0"); little_mp.put(
"index_element",
"12");
345 Property little_pip=propGen; little_pip.put(
"name",
"Out_1"); little_pip.put(
"index_element",
"13");
346 Property little_dip=propGen; little_dip.put(
"name",
"Out_2"); little_dip.put(
"index_element",
"14");
348 void *pEncArrays=
static_cast<void*
>(iencarray);
349 sensors_ok&=sensEncArrays[0].
configure(pEncArrays,thumb_mp);
350 sensors_ok&=sensEncArrays[1].
configure(pEncArrays,thumb_ip);
351 sensors_ok&=sensEncArrays[2].
configure(pEncArrays,index_mp);
352 sensors_ok&=sensEncArrays[3].
configure(pEncArrays,index_ip);
353 sensors_ok&=sensEncArrays[4].
configure(pEncArrays,middle_mp);
354 sensors_ok&=sensEncArrays[5].
configure(pEncArrays,middle_ip);
355 sensors_ok&=sensEncArrays[6].
configure(pEncArrays,ring_mp);
356 sensors_ok&=sensEncArrays[7].
configure(pEncArrays,ring_pip);
357 sensors_ok&=sensEncArrays[8].
configure(pEncArrays,ring_dip);
358 sensors_ok&=sensEncArrays[9].
configure(pEncArrays,little_mp);
359 sensors_ok&=sensEncArrays[10].
configure(pEncArrays,little_pip);
360 sensors_ok&=sensEncArrays[11].
configure(pEncArrays,little_dip);
370 Property thumb(options.findGroup(
"thumb").toString().c_str());
371 Property index(options.findGroup(
"index").toString().c_str());
372 Property middle(options.findGroup(
"middle").toString().c_str());
373 Property ring(options.findGroup(
"ring").toString().c_str());
374 Property little(options.findGroup(
"little").toString().c_str());
376 bool fingers_ok=
true;
420 return configured=
true;
438 string thumb=
"(thumb ";
439 thumb+=prop[0].toString();
442 string index=
"(index ";
443 index+=prop[1].toString();
446 string middle=
"(middle ";
447 middle+=prop[2].toString();
450 string ring=
"(ring ";
451 ring+=prop[3].toString();
454 string little=
"(little ";
455 little+=prop[4].toString();
458 options.fromString(thumb+index+middle+ring+little);
459 options.put(
"name",
name);
460 options.put(
"type",type);
461 options.put(
"robot",robot);
472 str<<
"name "<<
name<<endl;
473 str<<
"type "<<type<<endl;
474 str<<
"robot "<<robot<<endl;
478 str<<
"[thumb]"<<endl;
482 str<<
"[index]"<<endl;
486 str<<
"[middle]"<<endl;
494 str<<
"[little]"<<endl;
509 Value fng=options.find(
"finger");
516 IControlMode *imod; drvEncs.view(imod);
517 IControlLimits *ilim; drvEncs.view(ilim);
518 IEncoders *ienc; drvEncs.view(ienc);
519 IPositionControl *ipos; drvEncs.view(ipos);
521 int nAxes; ienc->getAxes(&nAxes);
522 Vector qmin(nAxes),qmax(nAxes),vel(nAxes),
acc(nAxes);
525 for (
int j=7; j<nAxes; j++)
527 imod->setControlMode(j,VOCAB_CM_POSITION);
528 ilim->getLimits(j,&qmin[j],&qmax[j]);
530 ipos->getRefAcceleration(j,&
acc[j]);
531 ipos->getRefSpeed(j,&vel[j]);
533 ipos->setRefAcceleration(j,std::numeric_limits<double>::max());
534 ipos->setRefSpeed(j,60.0);
535 ipos->positionMove(j,(j==8)?qmax[j]:qmin[j]);
543 string tag=options.check(
"finger",Value(
"all")).asString();
546 calibrateFinger(fingers[0],10,qmin[10],qmax[10]);
548 else if (tag==
"index")
550 calibrateFinger(fingers[1],12,qmin[12],qmax[12]);
552 else if (tag==
"middle")
554 calibrateFinger(fingers[2],14,qmin[14],qmax[14]);
556 else if (tag==
"ring")
558 calibrateFinger(fingers[3],15,qmin[15],qmax[15]);
560 else if (tag==
"little")
562 calibrateFinger(fingers[4],15,qmin[15],qmax[15]);
564 else if ((tag==
"all") || (tag==
"all_serial"))
566 calibrateFinger(fingers[0],10,qmin[10],qmax[10]);
567 calibrateFinger(fingers[1],12,qmin[12],qmax[12]);
568 calibrateFinger(fingers[2],14,qmin[14],qmax[14]);
569 calibrateFinger(fingers[3],15,qmin[15],qmax[15]);
570 calibrateFinger(fingers[4],15,qmin[15],qmax[15]);
572 else if (tag==
"all_parallel")
575 Bottle &bl=b.addList();
576 bl.addString(
"thumb");
577 bl.addString(
"index");
578 bl.addString(
"middle");
579 bl.addString(
"ring");
580 bl.addString(
"little");
590 deque<CalibThread*> db;
591 set<string> singletons;
593 Bottle *items=fng.asList();
594 for (
int i=0; i<items->size(); i++)
596 string tag=items->get(i).asString();
599 if (singletons.find(tag)==singletons.end())
602 thr->setInfo(
this,fingers[0],10,qmin[10],qmax[10]);
605 singletons.insert(tag);
608 else if (tag==
"index")
610 if (singletons.find(tag)==singletons.end())
613 thr->setInfo(
this,fingers[1],12,qmin[12],qmax[12]);
616 singletons.insert(tag);
619 else if (tag==
"middle")
621 if (singletons.find(tag)==singletons.end())
624 thr->setInfo(
this,fingers[2],14,qmin[14],qmax[14]);
627 singletons.insert(tag);
630 else if (tag==
"ring")
632 if (singletons.find(tag)==singletons.end())
635 thr->setInfo(
this,fingers[3],15,qmin[15],qmax[15]);
638 singletons.insert(tag);
641 else if (tag==
"little")
643 if (singletons.find(tag)==singletons.end())
646 thr->setInfo(
this,fingers[4],15,qmin[15],qmax[15]);
649 singletons.insert(tag);
663 db.erase(db.begin()+i);
675 if (!fng.isString() && !fng.isList())
681 for (
int j=7; j<nAxes; j++)
683 ipos->setRefAcceleration(j,
acc[j]);
684 ipos->setRefSpeed(j,vel[j]);
706 Bottle bOut; Bottle &ins=bOut.addList();
707 ins.addFloat64(val[0].asFloat64());
708 ins.addFloat64(val[1].asFloat64());
709 ins.addFloat64(val[2].asFloat64());
710 ins.addFloat64(val[3].asFloat64());
711 ins.addFloat64(val[4].asFloat64());
722void SpringyFingersModel::calibrateFinger(
SpringyFinger &finger,
const int joint,
723 const double min,
const double max)
726 double margin=0.1*(max-min);
727 double _min=min+margin;
728 double _max=max-margin;
732 double *tol=&tol_min;
733 double timeout=2.0*(_max-_min)/finger.
getCalibVel();
736 IControlMode *imod; drvEncs.view(imod);
737 IEncoders *ienc; drvEncs.view(ienc);
738 IPositionControl *ipos; drvEncs.view(ipos);
750 Property reset(
"(reset)");
751 Property feed(
"(feed)");
752 Property train(
"(train)");
757 imod->setControlMode(joint,VOCAB_CM_POSITION);
761 for (
int i=0; i<5; i++)
764 ipos->positionMove(joint,*val);
768 double fbOld=std::numeric_limits<double>::max();
769 double t0=Time::now();
775 double fb; ienc->getEncoder(joint,&fb);
778 if (fabs(fb-fbOld)>0.5)
784 done=(fabs(*val-fb)<*tol)||(Time::now()-t0>timeout);
818void SpringyFingersModel::close()
822 if (drvEncs.isValid())
825 if (drvMAIS.isValid())
virtual void setUpperBoundOut(double uo)
Mutator for the desired upper bound.
virtual void setUpperBoundIn(double ui)
Mutator for the expected upper bound.
virtual void setLowerBoundOut(double lo)
Mutator for the desired lower bound.
virtual void setLowerBoundIn(double li)
Mutator for the expected lower bound.
unsigned int getCoDomainSize() const
Returns the size (dimensionality) of the output domain (codomain).
unsigned int getDomainSize() const
Returns the size (dimensionality) of the input domain.
virtual bool fromString(const std::string &str)
Asks the learning machine to initialize from a string serialization.
virtual std::string toString()
Asks the learning machine to return a string serialization.
virtual std::string toString()
Asks the scaler to return a string serialization.
virtual double transform(double val)
Transforms a single sample value according to the state of the scaler.
virtual bool fromString(const std::string &str)
Asks the scaler to initialize from a string serialization.
virtual double unTransform(double val)
Untransforms a single sample value according to the state of the scaler.
virtual void feedSample(const yarp::sig::Vector &input, const yarp::sig::Vector &output)
Provide the learning machine with an example of the desired mapping.
void setCoDomainSize(unsigned int size)
Mutator for the codomain size.
void reset()
Forget everything and start over.
void setDomainSize(unsigned int size)
Mutator for the domain size.
virtual void setC(double C)
Mutator for the regularization parameter C.
Prediction predict(const yarp::sig::Vector &input)
Ask the learning machine to predict the output for a given input.
virtual void train()
Train the learning machine on the examples that have been supplied so far.
virtual RBFKernel * getKernel()
Accessor for the kernel.
yarp::sig::Vector getPrediction()
Accessor for the expected value of the prediction.
virtual void setGamma(double g)
virtual void printMessage(const int logtype, const int level, const char *format,...) const
void attachNode(Node &node)
Attach a node object to the model.
std::map< std::string, Node * > nodes
std::map< std::string, EventCallback * > callbacks
std::map< std::string, Node * > neighbors
std::map< std::string, Sensor * > sensors
void attachSensor(Sensor &sensor)
Attach a sensor object to the node.
std::string getName() const
Retrieve the node name.
bool configure(void *source, const yarp::os::Property &options)
Configure the sensor.
bool configure(void *source, const yarp::os::Property &options)
Configure the sensor.
An implementation of the Node class that represents the springy finger.
bool fromProperty(const yarp::os::Property &options)
Configure the finger taking its parameters from a Property object.
bool getOutput(yarp::os::Value &out) const
Retrieve the finger output.
void toProperty(yarp::os::Property &options) const
Return a Property representation of all the node parameters.
bool toStream(std::ostream &str) const
Similar to the toProperty() method but it operates on output streams (e.g.
iCub::learningmachine::LSSVMLearner lssvm
double getCalibVel() const
Return the finger actuation velocity used while calibrating.
bool getSensorsData(yarp::os::Value &data) const
Retrieve data finger from the joints used both for calibration and normal operation.
bool extractSensorsData(yarp::sig::Vector &in, yarp::sig::Vector &out) const
bool calibrate(const yarp::os::Property &options)
Allow to send calibration commands to the finger.
double calibratingVelocity
iCub::learningmachine::FixedRangeScaler scaler
virtual ~SpringyFingersModel()
Destructor.
void toProperty(yarp::os::Property &options) const
Return a Property representation of all the model parameters.
bool toStream(std::ostream &str) const
Similar to the toProperty() method but it operates on output streams (e.g.
bool isCalibrated() const
Return the internal status of the calibration.
SpringyFingersModel()
Constructor.
bool calibrate(const yarp::os::Property &options)
Execute the fingers calibration.
bool fromProperty(const yarp::os::Property &options)
Configure the model taking its parameters from a Property object.
bool getOutput(yarp::os::Value &out) const
Retrieve the complete output of the model.
double norm(const yarp::sig::Matrix &M, int col)
Returns the norm of the vector given in the form: matrix(:,col).