30 string moduleName = rf.check(
"name", Value(
"pasar")).asString().c_str();
31 setName(moduleName.c_str());
34 pTopDownAppearanceBurst = rf.check(
"parameterTopDownAppearanceBurst",
35 Value(3.)).asDouble();
36 pTopDownDisappearanceBurst = rf.check(
"parameterTopDownDisappearanceBurst",
37 Value(2.)).asDouble();
38 pTopDownAccelerationCoef = rf.check(
"parameterTopDownAccelerationCoef",
39 Value(0.1)).asDouble();
40 pExponentialDecrease = rf.check(
"ExponentialDecrease",
41 Value(0.9)).asDouble();
42 pTopDownWaving = rf.check(
"pTopDownWaving",
43 Value(0.2)).asDouble();
44 dBurstOfPointing = rf.check(
"pBurstOfPointing",
45 Value(0.2)).asDouble();
46 dthresholdAppear = rf.check(
"thresholdAppear", Value(1.)).asDouble();
47 dthresholdDisappear = rf.check(
"thresholdDisappear", Value(2.)).asDouble();
48 thresholdPointing = rf.check(
"thresholdDistancePointing", Value(.5)).asDouble();
51 if (pExponentialDecrease >= 1 || pExponentialDecrease <= 0.0) pExponentialDecrease = 0.95;
53 presentRightHand.first =
false;
54 presentRightHand.second =
false;
55 presentLeftHand.first =
false;
56 presentLeftHand.first =
false;
58 rightHandt1 = Vector(3, 0.0);
59 rightHandt2 = Vector(3, 0.0);
60 leftHandt1 = Vector(3, 0.0);
61 leftHandt2 = Vector(3, 0.0);
63 thresholdMovementAccelAgent = rf.check(
"thresholdMovementAccelAgent",
64 Value(0.02)).asDouble();
65 thresholdMovementAccelObject = rf.check(
"thresholdMovementAccelObject",
66 Value(0.2)).asDouble();
67 thresholdWaving = rf.check(
"thresholdWaving",
68 Value(0.02)).asDouble();
69 thresholdSaliency = rf.check(
"thresholdSaliency",
70 Value(0.005)).asDouble();
72 bool isRFVerbose =
true;
73 iCub =
new ICubClient(moduleName,
"pasar",
"pasar.ini", isRFVerbose);
74 iCub->opc->isVerbose &=
true;
78 yError() <<
"iCubClient : Some dependencies are not running...";
82 checkPointing = rf.find(
"isPointing").asInt() == 1;
83 checkWaving = rf.find(
"isWaving").asInt() == 1;
88 yInfo() <<
" pointing: " << checkPointing;
89 yInfo() <<
" waving: " << checkWaving;
91 if (!handlerPort.open((
"/" + moduleName +
"/rpc").c_str())) {
92 yError() << getName() <<
": Unable to open port rpc";
97 presentObjectsLastStep.clear();
98 initTime = yarp::os::Time::now();
99 initializeMapTiming();
101 for (
auto &it : OPCEntities)
103 yInfo() <<
"start: checking entity: " << it.second.o.name() <<
" opcPresence: " << it.second.o.m_present <<
", pasar presence: " << it.second.present;
106 yInfo() <<
"\n \n" <<
"----------------------------------------------" <<
"\n \n" << moduleName <<
" ready ! \n \n ";
115 yDebug() <<
"Interrupt OPC";
116 iCub->opc->interrupt();
117 yDebug() <<
"Interrupt RPC port";
118 handlerPort.interrupt();
126 yDebug() <<
"Close iCub";
130 yDebug() <<
"Interrupt RPC";
131 handlerPort.interrupt();
132 yDebug() <<
"Close RPC";
140 string helpMessage = string(getName().c_str()) +
141 " commands are: \n" +
143 "pointing on/off: launch or stop pointing\n" +
144 "waving on/off: launch or stop waving\n" +
148 reply.addString(
"ack");
150 yInfo() <<
" rpc command received: " << command.toString();
152 if (command.get(0).asString() ==
"quit") {
153 reply.addString(
"quitting");
156 else if (command.get(0).asString() ==
"help") {
157 reply.addString(helpMessage.c_str());
159 else if (command.get(0).asString() ==
"pointing") {
160 if (command.size() != 2)
162 reply.addString(
"error in PASAR: Bottle 'pointing' misses information (on/off)");
166 if (command.get(1).asString() ==
"off")
168 checkPointing =
false;
169 yInfo() <<
" stop pointing";
170 reply.addString(
"ack");
171 reply.addString(
"stop pointing");
173 else if (command.get(1).asString() ==
"on")
175 checkPointing =
true;
176 yInfo() <<
" start pointing";
177 reply.addString(
"ack");
178 reply.addString(
"start pointing");
182 else if (command.get(0).asString() ==
"waving") {
183 if (command.size() != 2)
185 reply.addString(
"error in PASAR: Bottle 'waving' misses information (on/off)");
189 if (command.get(1).asString() ==
"off")
192 yInfo() <<
" stop waving";
193 reply.addString(
"stop waving");
195 else if (command.get(1).asString() ==
"on")
198 yInfo() <<
" start waving";
199 reply.addString(
"start waving");
203 else if (command.get(0).asString() ==
"set") {
204 reply.addString(
"set");
205 reply.addString(
"ir");
213 iCub->opc->checkout();
214 std::list<std::shared_ptr<icubclient::Entity>> entities = iCub->opc->EntitiesCacheCopy();
216 presentLastSpeed = presentCurrentSpeed;
217 presentCurrentSpeed.clear();
219 for (
auto &entity : entities)
221 if (entity->name() !=
"icub")
227 OPCEntities[entity->opc_id()].o = *ob;
232 if (presentObjectsLastStep.size() > 0)
236 if (checkPointing) saliencyPointing();
237 if (checkWaving) saliencyWaving();
240 saliencyLeakyIntegration();
243 for (
auto it = entities.begin(); it != entities.end(); it++)
245 if (OPCEntities.find((*it)->opc_id()) != OPCEntities.end())
247 Object* o =
dynamic_cast<Object*
>(iCub->opc->getEntity((*it)->name()));
249 o->
m_saliency = OPCEntities[(*it)->opc_id()].o.m_saliency;
250 iCub->opc->commit(o);
257 yInfo(
"no objects present in the OPC");
260 presentObjectsLastStep = OPCEntities;
268 double now = yarp::os::Time::now() - initTime;
270 for (
auto &it : OPCEntities)
272 if (presentObjectsLastStep.find(it.first) != presentObjectsLastStep.end() && it.second.o.name() !=
"cursor_0" && it.second.o.name() !=
"icub")
275 bool appeared, disappeared;
279 disappeared = (now - it.second.lastTimeSeen > dthresholdDisappear) && (it.second.o.m_present == 0.0) && it.second.present;
281 yInfo() <<
"DISAPPEARED since: " << now - it.second.lastTimeSeen;
286 appeared = (now - it.second.lastTimeSeen > dthresholdAppear) && (it.second.o.m_present == 1.0) && !it.second.present;
288 yInfo() <<
"APPEARED since: " << now - it.second.lastTimeSeen;
291 if (it.second.o.m_present == 1.0) it.second.lastTimeSeen = now;
294 Vector lastPos = presentObjectsLastStep[it.first].o.m_ego_position;
295 Vector currentPos = it.second.o.m_ego_position;
296 presentCurrentSpeed[it.first] = pair<double, double>(lastPos[0] - currentPos[0], lastPos[1] - currentPos[1]);
298 double acceleration = sqrt(pow(presentCurrentSpeed[it.first].first - presentLastSpeed[it.first].first, 2.) + pow(presentCurrentSpeed[it.first].second - presentLastSpeed[it.first].second, 2.));
300 OPCEntities[it.first].acceleration = acceleration;
305 it.second.o.m_saliency += pTopDownAppearanceBurst;
306 yInfo() <<
"\t\t APPEARANCE OF: " << it.second.o.name();
307 it.second.present =
true;
312 it.second.o.m_saliency += pTopDownDisappearanceBurst;
313 yInfo() <<
"\t\t DISAPPEARANCE OF: " << it.second.o.name();
314 it.second.present =
false;
318 if (acceleration > thresholdMovementAccelAgent)
320 it.second.o.m_saliency += pTopDownAccelerationCoef;
321 yInfo() <<
" moving agent:" << it.second.o.name() <<
" salience : " << it.second.o.m_saliency <<
" acceleration: " << acceleration;
326 if (acceleration > thresholdMovementAccelObject)
328 it.second.o.m_saliency += pTopDownAccelerationCoef;
329 yInfo() <<
" moving object:" << it.second.o.name() <<
" salience : " << it.second.o.m_saliency <<
" acceleration: " << acceleration;
340 map< int, ObjectModel >::iterator mostSalientObject = OPCEntities.begin();
342 for (map< int, ObjectModel >::iterator it = OPCEntities.begin(); it != OPCEntities.end(); it++)
344 if (it->second.o.m_saliency > mostSalientObject->second.o.m_saliency)
345 mostSalientObject = it;
349 double maxS = mostSalientObject->second.o.m_saliency;
350 if (maxS == 0) maxS = 1.;
351 for (map< int, ObjectModel >::iterator it = OPCEntities.begin(); it != OPCEntities.end(); it++)
353 it->second.o.m_saliency /= maxS;
360 for (
auto &it : OPCEntities)
362 if (it.second.o.name() !=
"" && it.second.o.m_present != 0.5)
364 it.second.o.m_saliency *= pExponentialDecrease;
366 if (it.second.o.m_saliency < thresholdSaliency)
367 it.second.o.m_saliency = 0.0;
380 bool startPointing =
false;
381 bool stopPointing =
false;
383 bool wasPresent =
false;
387 for (
auto &it : OPCEntities){
390 && (it.second.o.name() !=
"iCub")
391 && (it.second.o.name() !=
"icub")){
392 ag =
dynamic_cast<Agent*
>(iCub->opc->getEntity(it.second.o.name()));
399 if (!ag || !wasPresent){
400 yInfo() <<
" in PASAR:saliencyPointing no human agent present";
404 double now = Time::now() - initTime;
410 double closest = thresholdPointing;
411 string objectPointed =
"none";
412 bool pointingNow =
false;
413 for (
auto &it : OPCEntities)
415 if (it.second.o.name() != ag->
name() && it.second.present)
420 (x - it.second.o.m_ego_position[0])*(x - it.second.o.m_ego_position[0]) +
421 (y - it.second.o.m_ego_position[1])*(y - it.second.o.m_ego_position[1]) +
422 (z - it.second.o.m_ego_position[2])*(z - it.second.o.m_ego_position[2])
425 if (distance < closest)
428 objectPointed = it.second.o.name();
434 stopPointing = (now - lastTimePointing > dthresholdDisappear) && isPointing && !pointingNow;
435 startPointing = (now - lastTimePointing > dthresholdAppear) && !isPointing && pointingNow;
438 yInfo() <<
"\t\t STOP POINTING ";
443 yInfo() <<
"\t\t POINTING START " << objectPointed;
447 lastTimePointing = now;
448 yInfo() <<
" pointed object is: \t" << objectPointed;
449 for (
auto &it : OPCEntities)
451 if (it.second.o.name() == objectPointed)
453 it.second.o.m_saliency += dBurstOfPointing;
464 bool wasPresent =
false;
467 for (
auto &it : OPCEntities){
470 && (it.second.o.name() !=
"iCub")
471 && (it.second.o.name() !=
"icub")){
472 ag =
dynamic_cast<Agent*
>(iCub->opc->getEntity(it.second.o.name()));
478 yInfo() <<
" in PASAR:saliencyWaving no human agent present";
483 bool wavingNow =
false;
487 presentRightHand.first = presentRightHand.second;
488 presentLeftHand.first = presentLeftHand.second;
490 presentRightHand.second =
false;
491 presentLeftHand.second =
false;
499 Vector speedRightt1(3);
500 Vector speedRightt2(3);
502 Vector speedLeftt1(3);
503 Vector speedLeftt2(3);
505 Vector accelRight(3);
509 speedRightt2[0] = (rightHandt1[0] - rightHandt2[0]);
510 speedRightt2[1] = (rightHandt1[1] - rightHandt2[1]);
511 speedRightt2[2] = (rightHandt1[2] - rightHandt2[2]);
513 speedRightt1[0] = (vecRight[0] - rightHandt1[0]);
514 speedRightt1[1] = (vecRight[1] - rightHandt1[1]);
515 speedRightt1[2] = (vecRight[2] - rightHandt1[2]);
517 accelRight[0] = (speedRightt1[0] - speedRightt2[0]);
518 accelRight[1] = (speedRightt1[1] - speedRightt2[1]);
519 accelRight[2] = (speedRightt1[2] - speedRightt2[2]);
521 speedLeftt1[0] = (vecLeft[0] - leftHandt1[0]);
522 speedLeftt1[1] = (vecLeft[1] - leftHandt1[1]);
523 speedLeftt1[2] = (vecLeft[2] - leftHandt1[2]);
525 speedLeftt2[0] = (leftHandt1[0] - leftHandt2[0]);
526 speedLeftt2[1] = (leftHandt1[1] - leftHandt2[1]);
527 speedLeftt2[2] = (leftHandt1[2] - leftHandt2[2]);
529 accelLeft[0] = (speedLeftt1[0] - speedLeftt2[0]);
530 accelLeft[1] = (speedLeftt1[1] - speedLeftt2[1]);
531 accelLeft[2] = (speedLeftt1[2] - speedLeftt2[2]);
534 dAccelLeft = sqrt(accelLeft[0] * accelLeft[0] + accelLeft[1] * accelLeft[1] + accelLeft[2] * accelLeft[2]);
535 dAccelRight = sqrt(accelRight[0] * accelRight[0] + accelRight[1] * accelRight[1] + accelRight[2] * accelRight[2]);
537 rightHandt2 = rightHandt1;
538 rightHandt1 = vecRight;
540 leftHandt2 = leftHandt1;
541 leftHandt1 = vecLeft;
543 double now = yarp::os::Time::now() - initTime;
547 if (dAccelRight > thresholdWaving && presentRightHand.first && presentRightHand.second){
548 yInfo() <<
"\tagent is waving right hand lastTime was: " << now - lastTimeWaving;
551 if (dAccelLeft > thresholdWaving && presentLeftHand.first && presentLeftHand.second){
552 yInfo() <<
"\tagent is waving left hand lastTime was: " << now - lastTimeWaving;
556 bool startWaving = (now - lastTimeWaving > dthresholdAppear) && !isWaving && wavingNow;
557 bool stopWaving = (now - lastTimeWaving > dthresholdDisappear) && isWaving && !wavingNow;
562 yInfo() <<
"\t\t START WAVING";
566 yInfo() <<
"\t\t STOP WAVING";
571 lastTimeWaving = now;
572 iCub->opc->commit(ag);
575 presentRightHand.first = presentRightHand.second;
576 presentLeftHand.first = presentLeftHand.second;
578 presentRightHand.second =
true;
579 presentLeftHand.second =
true;
592 iCub->opc->checkout();
593 std::list<std::shared_ptr<icubclient::Entity>> entities = iCub->opc->EntitiesCacheCopy();
594 double now = yarp::os::Time::now() - initTime;
597 lastTimePointing = now - 10;
598 lastTimeWaving = now - 10;
600 for (
auto &entity : entities){
602 if (entity->name() !=
"icub")
610 OPCEntities[entity->opc_id()].o = *ob;
611 OPCEntities[entity->opc_id()].lastTimeSeen = (ob->
m_present == 1.0) ? now : now - 10;
612 OPCEntities[entity->opc_id()].present = (ob->
m_present == 1.0);
bool respond(const yarp::os::Bottle &command, yarp::os::Bottle &reply)
Grants access to high level motor commands (grasp, touch, look, goto, etc) of the robot as well as it...
void saliencyTopDown()
saliencyTopDown Update the salience according to the informations contained in the OPC (acceleration...
double m_present
Is the object present in the scene A value of 1.0 means that the object currently is in the scene A v...
Represent any physical entity (including objects and agents) that can be stored within the OPC...
bool saliencyWaving()
saliencyWaving Increase the saliency of the agent waving
bool configure(yarp::os::ResourceFinder &rf)
bool saliencyPointing()
saliencyPointing Increase the salience of the closest object from the right hand
void saliencyLeakyIntegration()
saliencyLeakyIntegration Decrease of the salience through time.
std::string name() const
Return the name of an entity (which has to be unique within the OPC)
void saliencyNormalize()
saliencyNormalize Normalize salience such that maximum salience = 1
double m_saliency
A measurement of the object saliency [0,1].
void initializeMapTiming()
std::map< std::string, yarp::sig::VectorOf< double > > m_parts