20 #ifdef FARSA_USE_YARP_AND_ICUB
23 #warning ================================================================================================================================================================================================================= PENSARE A DIVIDERE wVector IN UNA CLASSE PER I VETTORI (QUARTO ELEMENTO = 0) E UNA PER I PUNTI (QUARTO ELEMENTO = 1) =================================================================================================================================================================================================================
26 #include "phyicubhelpers.h"
29 #include "phycylinder.h"
30 #include "physphere.h"
33 #include "phyuniversal.h"
34 #include "phycompoundobject.h"
35 #include "motorcontrollers.h"
39 #pragma GCC diagnostic ignored "-Wunused-parameter"
41 #include "yarp/os/ResourceFinder.h"
42 #include "yarp/os/Bottle.h"
43 #include "yarp/os/Property.h"
44 #include "yarp/dev/PolyDriverList.h"
45 #include "yarp/dev/Wrapper.h"
46 #include "yarp/os/RateThread.h"
47 #include "yarp/os/Network.h"
49 #pragma GCC diagnostic warning "-Wunused-parameter"
52 using namespace yarp::os;
53 using namespace yarp::dev;
54 using namespace iCub::iKin;
60 std::ostream& operator<<(std::ostream &os,
const wMatrix &m)
63 os.setf(std::ostream::fixed);
64 os.unsetf(std::ostream::scientific);
66 <<
"\t" << m[0][0] <<
"\t" << m[0][1] <<
"\t" << m[0][2] <<
"\t" << m[0][3] << std::endl
67 <<
"\t" << m[1][0] <<
"\t" << m[1][1] <<
"\t" << m[1][2] <<
"\t" << m[1][3] << std::endl
68 <<
"\t" << m[2][0] <<
"\t" << m[2][1] <<
"\t" << m[2][2] <<
"\t" << m[2][3] << std::endl
69 <<
"\t" << m[3][0] <<
"\t" << m[3][1] <<
"\t" << m[3][2] <<
"\t" << m[3][3] << std::endl;
74 std::ostream& operator<<(std::ostream &os,
const wVector &v)
77 os.setf(std::ostream::fixed);
78 os.unsetf(std::ostream::scientific);
79 os <<
"[" << v.x <<
", " << v.y <<
", " << v.z <<
", " << v.w <<
"]";
84 void convertYarpMatrixToWorldMatrix(
const yarp::sig::Matrix &y, wMatrix &w)
106 void convertWorldMatrixToYarpMatrix(
const wMatrix &w, yarp::sig::Matrix &y)
128 DOFStatusListener::DOFStatusListener(
const PhyDOF* dof, Qt::ConnectionType connType) :
131 m_curPosition(dof->position()),
132 m_desideredPosition(dof->desiredPosition()),
133 m_velocity(dof->desiredVelocity()),
134 m_motionMode(dof->motion())
137 connect(
m_dof, SIGNAL(changedPosition(real)),
this, SLOT(setLinkAngle(real)), connType);
138 connect(
m_dof, SIGNAL(changedDesiredPosition(real)),
this, SLOT(setDesideredPosition(real)), connType);
139 connect(
m_dof, SIGNAL(changedDesiredVelocity(real)),
this, SLOT(setDesideredVelocity(real)), connType);
159 real lowLimit, highLimit;
179 void DOFStatusListener::setLinkAngle(real newAngle)
184 void DOFStatusListener::setDesideredPosition(real desideredAngle)
190 void DOFStatusListener::setDesideredVelocity(real velocity)
198 m_headObject(headObject),
209 for (QMap<
ChainIDType, QVector<DOFAndListener> >::iterator it = m_joints.begin(); it != m_joints.end(); it++) {
210 for (
int i = 0; i < it.value().size(); i++) {
211 delete it.value()[i].listener;
220 m_objects[chainID].push_back(ObjectAndMatrix(obj, obj->
matrix() * invHeadMatrix));
222 return (m_objects[chainID].size() - 1);
227 Q_ASSERT_X(joint->
numDofs() > dofID,
"PhyObjectsGroup::addJointDOF",
"Joint has not the given dof");
230 return (m_joints[chainID].size() - 1);
237 for (QMap<
ChainIDType, QVector<ObjectAndMatrix> >::iterator it = m_objects.begin(); it != m_objects.end(); it++) {
238 for (
int i = 0; i < it.value().size(); i++) {
239 it.value()[i].object->setKinematic(b);
248 for (QMap<
ChainIDType, QVector<ObjectAndMatrix> >::iterator it = m_objects.begin(); it != m_objects.end(); it++) {
249 for (
int i = 0; i < it.value().size(); i++) {
250 it.value()[i].object->setStatic(b);
258 for (QMap<
ChainIDType, QVector<DOFAndListener> >::iterator it = m_joints.begin(); it != m_joints.end(); it++) {
259 for (
int i = 0; i < it.value().size(); i++) {
260 it.value()[i].joint->enable(b);
269 for (QMap<
ChainIDType, QVector<ObjectAndMatrix> >::iterator it = m_objects.begin(); it != m_objects.end(); it++) {
270 for (
int i = 0; i < it.value().size(); i++) {
271 it.value()[i].matrix = it.value()[i].object->matrix() * invHeadMatrix;
279 Q_ASSERT_X(m_joints.contains(chainID),
"PhyObjectsGroup::setDOFPosition",
"No joint chain with the given ID");
280 Q_ASSERT_X(m_joints[chainID].size() > dof,
"PhyObjectsGroup::setDOFPosition",
"No joint with the given ID in the given chain");
281 Q_ASSERT_X(m_joints[chainID][dof].joint->dofs()[m_joints[chainID][dof].dofID]->rotate(),
"PhyObjectsGroup::setDOFPosition",
"Linear joints are not supported");
282 Q_ASSERT_X(m_objects.contains(chainID),
"PhyObjectsGroup::setDOFPosition",
"No object chain with the given ID");
283 Q_ASSERT_X(m_objects[chainID].size() > dof,
"PhyObjectsGroup::setDOFPosition",
"No object with the given ID in the given chain");
285 QVector<ObjectAndMatrix>& objChain = m_objects[chainID];
286 QVector<DOFAndListener>& jointChain = m_joints[chainID];
287 PhyDOF* phydof = jointChain[dof].
joint->
dofs()[m_joints[chainID][dof].dofID];
292 objChain[i].matrix = newMatrixWorldCoordinate *
matrix().
inverse();
298 objChain[i].object->setMatrix(objChain[i].
matrix *
matrix());
301 jointChain[i].joint->updateJointInfo();
312 for (QMap<
ChainIDType, QVector<DOFAndListener> >::iterator it = m_joints.begin(); it != m_joints.end(); it++) {
313 for (
int i = 0; i < it.value().size(); i++) {
314 it.value()[i].listener->update();
328 void PhyObjectsGroup::changedMatrix()
335 for (QMap<
ChainIDType, QVector<ObjectAndMatrix> >::iterator it = m_objects.begin(); it != m_objects.end(); it++) {
336 for (
int i = 0; i < it.value().size(); i++) {
337 it.value()[i].object->setMatrix(it.value()[i].matrix *
matrix());
342 for (QMap<
ChainIDType, QVector<DOFAndListener> >::iterator it = m_joints.begin(); it != m_joints.end(); it++) {
343 for (
int i = 0; i < it.value().size(); i++) {
344 it.value()[i].joint->updateJointInfo();
352 m_isMasterBuddy(true),
356 m_objectMatrix(
wMatrix::identity()),
361 m_worldTm(&s_identityMatrix),
364 m_invertedJointAxis(true),
367 m_motionMode(
PhyDOF::off),
368 m_linkAngleUpdated(true)
375 m_isMasterBuddy(true),
379 m_objectMatrix(
wMatrix::identity()),
384 m_worldTm(&s_identityMatrix),
387 m_invertedJointAxis(true),
390 m_motionMode(
PhyDOF::off),
391 m_linkAngleUpdated(true)
398 m_buddies(other.m_buddies),
399 m_isMasterBuddy(other.m_isMasterBuddy),
400 m_chain(other.m_chain),
401 m_linkID(other.m_linkID),
402 m_wObject(other.m_wObject),
403 m_objectMatrix(other.m_objectMatrix),
404 m_axis(other.m_axis),
405 m_bottom(other.m_bottom),
407 m_height(other.m_height),
408 m_worldTm(other.m_worldTm),
409 m_phyJoint(other.m_phyJoint),
410 m_phyJointDOF(other.m_phyJointDOF),
411 m_invertedJointAxis(other.m_invertedJointAxis),
412 m_nextAngle(other.m_nextAngle),
413 m_velocity(other.m_velocity),
414 m_motionMode(other.m_motionMode),
415 m_linkAngleUpdated(other.m_linkAngleUpdated)
418 m_buddies.insert(const_cast<KinematicLinkInfo*>(&other));
419 m_buddies.remove(
this);
423 buddy->m_buddies.insert(
this);
429 if (&other ==
this) {
433 m_chain = other.m_chain;
434 m_isMasterBuddy = other.m_isMasterBuddy;
435 m_linkID = other.m_linkID;
436 m_wObject = other.m_wObject;
437 m_objectMatrix = other.m_objectMatrix;
438 m_axis = other.m_axis;
439 m_bottom = other.m_bottom;
441 m_height = other.m_height;
442 m_worldTm = other.m_worldTm;
443 m_phyJoint = other.m_phyJoint;
444 m_phyJointDOF = other.m_phyJointDOF;
445 m_invertedJointAxis = other.m_invertedJointAxis;
446 m_nextAngle = other.m_nextAngle;
447 m_velocity = other.m_velocity;
448 m_motionMode = other.m_motionMode;
449 m_linkAngleUpdated = other.m_linkAngleUpdated;
452 m_buddies = other.m_buddies;
455 m_buddies.insert(const_cast<KinematicLinkInfo*>(&other));
456 m_buddies.remove(
this);
460 buddy->m_buddies.insert(
this);
470 buddy->m_buddies.remove(
this);
477 m_isMasterBuddy =
true;
481 m_buddies.remove(
this);
485 buddy->m_buddies = m_buddies;
486 buddy->m_isMasterBuddy =
false;
489 buddy->m_buddies.remove(buddy);
490 buddy->m_buddies.insert(
this);
493 buddy->m_wObject = m_wObject;
494 buddy->m_objectMatrix = m_objectMatrix;
495 buddy->m_axis = m_axis;
496 buddy->m_bottom = m_bottom;
497 buddy->m_top = m_top;
498 buddy->m_height = m_height;
499 buddy->m_worldTm = m_worldTm;
500 buddy->m_phyJoint = m_phyJoint;
501 buddy->m_phyJointDOF = m_phyJointDOF;
502 buddy->m_invertedJointAxis = m_invertedJointAxis;
503 buddy->m_nextAngle = m_nextAngle;
504 buddy->m_velocity = m_velocity;
505 buddy->m_motionMode = m_motionMode;
506 buddy->m_linkAngleUpdated = m_linkAngleUpdated;
513 setWObject_NB(wObject, m);
515 buddy->setWObject_NB(wObject, m);
525 setWorldMatrix_NB(mtr);
527 buddy->setWorldMatrix_NB(mtr);
537 updateInformationFromiKin_NB();
539 buddy->updateInformationFromiKin_NB();
546 updateMatrixFromiKin_NB();
548 buddy->updateMatrixFromiKin_NB();
555 setJoint_NB(joint, invertedJointAxis, dof, doConnection, connType);
559 buddy->setJoint_NB(joint, invertedJointAxis, dof,
false);
566 setLinkAngle_NB(newAngle);
568 buddy->setLinkAngle_NB(newAngle);
575 setDesideredPosition_NB(desideredAngle);
577 buddy->setDesideredPosition_NB(desideredAngle);
584 setDesideredVelocity_NB(velocity);
586 buddy->setDesideredVelocity_NB(velocity);
593 setiKinLinkLimits_NB(min, max);
595 buddy->setiKinLinkLimits_NB(min, max);
604 buddy->updateLink_NB();
608 void KinematicLinkInfo::setWObject_NB(
WObject* wObject,
const wMatrix &m)
614 void KinematicLinkInfo::setWorldMatrix_NB(
const wMatrix *mtr)
617 m_worldTm = &s_identityMatrix;
623 void KinematicLinkInfo::updateInformationFromiKin_NB()
629 convertYarpMatrixToWorldMatrix(m_chain->getH(m_linkID,
true), thisMatrix);
633 wMatrix prevLinkMatrix;
635 convertYarpMatrixToWorldMatrix(m_chain->getH0(), prevLinkMatrix);
637 convertYarpMatrixToWorldMatrix(m_chain->getH(m_linkID - 1,
true), prevLinkMatrix);
641 m_bottom = prevLinkMatrix.w_pos;
642 m_top = thisMatrix.w_pos;
643 m_axis = m_top - m_bottom;
644 m_height = m_axis.
norm();
645 m_rotAxis = thisMatrix.unrotateVector(prevLinkMatrix.z_ax);
646 m_rotCenter = thisMatrix.untransformVector(prevLinkMatrix.w_pos);
649 void KinematicLinkInfo::updateMatrixFromiKin_NB()
651 if ((m_isMasterBuddy) && (m_wObject != NULL)) {
654 convertYarpMatrixToWorldMatrix(m_chain->getH(m_linkID,
true), thisMatrix);
655 thisMatrix = m_objectMatrix * thisMatrix * (*m_worldTm);
660 void KinematicLinkInfo::setJoint_NB(PhyJoint *joint,
bool invertedJointAxis,
unsigned int dof,
bool doConnection, Qt::ConnectionType connType)
663 if (m_phyJoint != NULL) {
664 disconnect(m_phyJoint->
dofs()[m_phyJointDOF], SIGNAL(changedPosition(real)),
this, SLOT(
setLinkAngle(real)));
665 disconnect(m_phyJoint->
dofs()[m_phyJointDOF], SIGNAL(changedDesiredPosition(real)),
this, SLOT(
setDesideredPosition(real)));
666 disconnect(m_phyJoint->
dofs()[m_phyJointDOF], SIGNAL(changedDesiredVelocity(real)),
this, SLOT(
setDesideredVelocity(real)));
667 disconnect(m_phyJoint->
dofs()[m_phyJointDOF], SIGNAL(changedLimits(real, real)),
this, SLOT(
setiKinLinkLimits(real, real)));
672 m_invertedJointAxis = invertedJointAxis;
675 connect(m_phyJoint->
dofs()[m_phyJointDOF], SIGNAL(changedPosition(real)),
this, SLOT(
setLinkAngle(real)), connType);
676 connect(m_phyJoint->
dofs()[m_phyJointDOF], SIGNAL(changedDesiredPosition(real)),
this, SLOT(
setDesideredPosition(real)), connType);
677 connect(m_phyJoint->
dofs()[m_phyJointDOF], SIGNAL(changedDesiredVelocity(real)),
this, SLOT(
setDesideredVelocity(real)), connType);
678 connect(m_phyJoint->
dofs()[m_phyJointDOF], SIGNAL(changedLimits(real, real)),
this, SLOT(
setiKinLinkLimits(real, real)), connType);
682 void KinematicLinkInfo::setLinkAngle_NB(real newAngle)
684 if (!m_invertedJointAxis) {
685 newAngle = -newAngle;
687 (*(m_chain->asChain()))[m_linkID].setAng(newAngle);
689 m_nextAngle = newAngle;
692 void KinematicLinkInfo::setDesideredPosition_NB(real desideredAngle)
695 if (m_invertedJointAxis) {
696 m_nextAngle = desideredAngle;
698 m_nextAngle = -desideredAngle;
700 m_motionMode = PhyDOF::pos;
701 m_linkAngleUpdated =
false;
704 void KinematicLinkInfo::setDesideredVelocity_NB(real velocity)
707 if (fabs(velocity) > fabs(m_phyJoint->
dofs()[m_phyJointDOF]->maxVelocity())) {
708 velocity = (velocity > 0) ? fabs(m_phyJoint->
dofs()[m_phyJointDOF]->maxVelocity()) : -fabs(m_phyJoint->
dofs()[m_phyJointDOF]->maxVelocity());
711 if (m_invertedJointAxis) {
712 m_velocity = velocity;
714 m_velocity = -velocity;
716 m_motionMode = PhyDOF::vel;
717 m_linkAngleUpdated =
false;
720 void KinematicLinkInfo::setiKinLinkLimits_NB(real
min, real
max)
722 if (m_invertedJointAxis) {
723 (*(m_chain->asChain()))[m_linkID].setMin(min);
724 (*(m_chain->asChain()))[m_linkID].setMax(max);
726 (*(m_chain->asChain()))[m_linkID].setMin(-max);
727 (*(m_chain->asChain()))[m_linkID].setMax(-min);
731 void KinematicLinkInfo::updateLink_NB()
735 if (m_linkAngleUpdated) {
741 real position = (*(m_chain->asChain()))[m_linkID].getAng();
742 switch (m_motionMode) {
755 const real deltaAngle = m_velocity * m_phyJoint->
world()->
timeStep();
756 velocity = m_velocity;
757 position = (*(m_chain->asChain()))[m_linkID].getAng() + deltaAngle;
758 if (m_invertedJointAxis) {
759 setLinkAngle_NB(position);
761 setLinkAngle_NB(-position);
771 velocity = (m_nextAngle - (*(m_chain->asChain()))[m_linkID].getAng()) / m_phyJoint->
world()->
timeStep();
772 position = m_nextAngle;
773 if (m_invertedJointAxis) {
774 setLinkAngle_NB(position);
776 setLinkAngle_NB(-position);
788 m_phyJoint->
dofs()[m_phyJointDOF]->setPosition(position);
789 m_phyJoint->
dofs()[m_phyJointDOF]->setVelocity(velocity);
790 m_linkAngleUpdated =
true;
796 #endif // FARSA_USE_YARP_AND_ICUB