45 #include <yarp/os/BufferedPort.h>
46 #include <yarp/os/LogStream.h>
47 #include <yarp/os/RFModule.h>
48 #include <yarp/dev/IPositionDirect.h>
49 #include <yarp/dev/IPositionControl.h>
50 #include <yarp/dev/IControlMode.h>
51 #include <yarp/dev/IEncoders.h>
52 #include <yarp/dev/IControlLimits.h>
53 #include <yarp/dev/PolyDriver.h>
60 #include <yarp/os/RpcClient.h>
64 enum class state : std::uint8_t {
70 class ReplayPort :
public yarp::os::BufferedPort<yarp::os::Bottle>
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;
106 using yarp::os::TypedReaderCallback<yarp::os::Bottle>::onRead;
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());
186 std::vector<double> min, max;
187 std::string m_partName{};
188 yarp::dev::IPositionDirect* m_posDir{
nullptr};
189 yarp::dev::IEncoders* m_enc{
nullptr};
190 yarp::dev::IPositionControl* m_posControl{
nullptr};
191 yarp::dev::IControlMode* m_CM{
nullptr};
192 yarp::dev::IControlLimits* m_controlLimits{
nullptr};
196 std::vector<double> m_currState, m_nextState;
206 bool open(
const std::string& robot,
const std::string& part,
const std::string& moduleName=
"wholeBodyPlayer") {
207 yarp::os::Property conf {{
"device", yarp::os::Value(
"remote_controlboard")},
208 {
"remote", yarp::os::Value(
"/"+robot+
"/"+part)},
209 {
"local", yarp::os::Value(
"/"+moduleName+
"/"+part+
"/remoteControlBoard")}};
211 yarp::dev::IControlMode* iCM{
nullptr};
212 yarp::dev::IPositionDirect* iPosDir{
nullptr};
213 yarp::dev::IPositionControl* iPosControl{
nullptr};
214 yarp::dev::IEncoders*
iEnc{
nullptr};
215 yarp::dev::IControlLimits* iControlLimits{
nullptr};
216 bool simulator{
false};
218 if(robot ==
"icubSim")
225 yError()<<
"Replayer: failed to open the remote control board for part"<<part;
236 m_replayPort = std::make_unique<ReplayPort>(part, iPosDir,
iEnc, iCM, iPosControl,iControlLimits, simulator);
257 bool configure(yarp::os::ResourceFinder& rf)
override;
259 bool respond(
const yarp::os::Bottle& command, yarp::os::Bottle& reply)
override;
263 bool close()
override;
266 std::vector<Replayer> m_replayerVec;
267 yarp::os::RpcClient m_rpcPort;
268 yarp::os::Bottle reqPause{
"pause"}, reqPlay{
"play"}, response;
constexpr double tolerance
void onRead(yarp::os::Bottle &datum) override
ReplayPort(std::string partName, yarp::dev::IPositionDirect *iPosDir, yarp::dev::IEncoders *iEnc, yarp::dev::IControlMode *iCM, yarp::dev::IPositionControl *iPosControl, yarp::dev::IControlLimits *iControlLimits, bool simulator=false)
std::atomic< state > m_state
bool positionMoveFallback()
bool updateModule() override
bool configure(yarp::os::ResourceFinder &rf) override
~WholeBodyPlayerModule() override=default
bool respond(const yarp::os::Bottle &command, yarp::os::Bottle &reply) override
double getPeriod() override
bool interruptModule() override
std::unique_ptr< ReplayPort > m_replayPort
bool open(const std::string &robot, const std::string &part, const std::string &moduleName="wholeBodyPlayer")
std::unique_ptr< yarp::dev::PolyDriver > m_remoteControlBoard