73 ReplayPort(std::string partName, yarp::dev::IPositionDirect* iPosDir,
74 yarp::dev::IEncoders* iEnc, yarp::dev::IControlMode* iCM,
75 yarp::dev::IPositionControl* iPosControl,
76 yarp::dev::IControlLimits* iControlLimits,
bool simulator=
false) :
77 m_partName(std::move(partName)),
81 m_posControl(iPosControl),
82 m_controlLimits(iControlLimits),
83 m_simulator(simulator)
86 if (m_posDir && m_enc && m_CM && m_controlLimits) {
88 m_posDir->getAxes(&m_numAxes);
90 std::vector<int> cms (m_numAxes, VOCAB_CM_POSITION_DIRECT);
91 m_CM->setControlModes(cms.data());
93 m_currState.resize(m_numAxes);
94 m_nextState.resize(m_numAxes);
95 max.resize(m_numAxes);
96 min.resize(m_numAxes);
97 for (
size_t i = 0; i<m_numAxes; i++) {
98 m_controlLimits->getLimits(i,&min[i],&max[i]);
101 yarp::os::BufferedPort<yarp::os::Bottle>::useCallback();
102 yarp::os::BufferedPort<yarp::os::Bottle>::setStrict(
true);
103 m_isArm = m_partName.find(
"arm") != std::string::npos;
107 void onRead(yarp::os::Bottle& datum)
override
111 bool ok = m_enc->getEncoders(m_currState.data());
113 for (
size_t i=0; i<datum.size(); i++) {
114 m_nextState[i] = datum.get(i).asFloat64();
115 if (m_nextState[i]<min[i] || m_nextState[i]> max[i]) {
116 yWarning()<<
"ReplayPort: trying to move joint"<<i<<
"of"<<m_partName<<
" to "<<m_nextState[i]<<
", it exceeds the limits, skipping...";
119 auto delta = std::fabs(m_nextState[i] - m_currState[i]);
121 if (!
ok && !m_simulator && (!m_isArm || i < 5)) {
122 yWarning()<<
"ReplayPort: joint"<<i<<
"of"<<m_partName<<
"is too far to the target position";
123 yWarning()<<
"Desired: "<<datum.get(i).asFloat64()<<
"current: "<<m_currState[i]
124 <<
"delta: "<<delta<<
"Trying to reach "
125 "it in Position Control, "
126 "the playback will be paused";
134 m_posDir->setPositions(m_nextState.data());
141 std::vector<int> cms (m_numAxes, VOCAB_CM_POSITION);
142 ok &= m_CM->setControlModes(cms.data());
150 ok &= m_posControl->positionMove(m_nextState.data());
158 ok &= m_posControl->checkMotionDone(&
done);
166 ok &= m_posControl->checkMotionDone(&
done);
171 yarp::os::Time::delay(0.01);
174 std::vector<int> cmDir (m_numAxes, VOCAB_CM_POSITION_DIRECT);
175 ok &= m_CM->setControlModes(cmDir.data());