A constant-velocity, single-joint trajectory is generated with configurable final target, motion speed and period between consecutive points. The yarp::dev::IRemoteVariables
interface is used to configure and switch to interpolated position (pi) mode on the real robot. Although prepared for remote execution, this application could be rewritten to connect to a local instance of CanBusBroker. The techniques showcased here are best aimed at trajectories fully available offline (e.g. loaded from file) that don't assume a constant period between points.
36 #include <yarp/os/LogStream.h>
37 #include <yarp/os/Network.h>
38 #include <yarp/os/Property.h>
39 #include <yarp/os/ResourceFinder.h>
40 #include <yarp/os/SystemClock.h>
41 #include <yarp/os/Timer.h>
43 #include <yarp/dev/IControlMode.h>
44 #include <yarp/dev/IEncoders.h>
45 #include <yarp/dev/IPositionDirect.h>
46 #include <yarp/dev/IRemoteVariables.h>
47 #include <yarp/dev/PolyDriver.h>
49 constexpr
auto DEFAULT_REMOTE =
"/teo/leftArm";
50 constexpr
auto DEFAULT_JOINT = 5;
51 constexpr
auto DEFAULT_SPEED = 2.0;
52 constexpr
auto DEFAULT_TARGET = (-20.0);
53 constexpr
auto DEFAULT_PERIOD_MS = 50;
54 constexpr
auto DEFAULT_IP_MODE =
"pt";
56 int main(
int argc,
char * argv[])
58 yarp::os::ResourceFinder rf;
59 rf.configure(argc, argv);
61 auto remote = rf.check(
"remote", yarp::os::Value(DEFAULT_REMOTE),
"remote port").asString();
62 auto joint = rf.check(
"joint", yarp::os::Value(DEFAULT_JOINT),
"joint id").asInt32();
63 auto speed = rf.check(
"speed", yarp::os::Value(DEFAULT_SPEED),
"trajectory speed (deg/s)").asFloat64();
64 auto target = rf.check(
"target", yarp::os::Value(DEFAULT_TARGET),
"target position (deg)").asFloat64();
65 auto period = rf.check(
"period", yarp::os::Value(DEFAULT_PERIOD_MS),
"command period (ms)").asInt32() * 0.001;
66 auto ipMode = rf.check(
"ip", yarp::os::Value(DEFAULT_IP_MODE),
"interpolation submode [pt|pvt]").asString();
70 yError() <<
"Illegal speed (deg/s):" << speed;
76 yError() <<
"Illegal period (s):" << period;
80 if (ipMode !=
"pt" && ipMode !=
"pvt")
82 yError() <<
"Illegal ipMode:" << ipMode;
86 yarp::os::Network yarp;
88 if (!yarp::os::Network::checkNetwork())
90 yError() <<
"Please start a yarp name server first";
94 yarp::os::Property options {{
"device", yarp::os::Value(
"remote_controlboard")},
95 {
"local", yarp::os::Value(
"/exampleOfflineTrajectoryAsync")},
96 {
"remote", yarp::os::Value(remote)}};
98 yarp::dev::PolyDriver dd(options);
102 yError() <<
"Remote device not available";
106 yarp::dev::IControlMode * mode;
107 yarp::dev::IEncoders * enc;
108 yarp::dev::IPositionDirect * posd;
109 yarp::dev::IRemoteVariables * var;
111 if (!dd.view(mode) || !dd.view(enc) || !dd.view(posd) || !dd.view(var))
113 yError() <<
"Unable to acquire robot interfaces";
118 yarp::os::Bottle & bb = b.addList();
120 bb.addList() = {yarp::os::Value(
"ipMode"), yarp::os::Value(ipMode)};
121 bb.addList() = {yarp::os::Value(
"enableIp"), yarp::os::Value(
true)};
123 if (!var->setRemoteVariable(
"all", b))
125 yError() <<
"Unable to set interpolation mode";
129 if (!mode->setControlMode(joint, VOCAB_CM_POSITION_DIRECT))
131 yError() <<
"Unable to set position direct mode";
138 while (!enc->getEncoder(joint, &initialPos) && retries++ < 10)
140 yarp::os::SystemClock::delaySystem(0.05);
145 yError() <<
"getEncoders() failed";
149 yInfo() <<
"Current ENC value:" << initialPos;
153 yInfo() <<
"Moving joint" << joint <<
"to" << target <<
"degrees...";
155 const double distance = target - initialPos;
156 const double increment = std::copysign(speed * period, distance);
158 yarp::os::Timer::TimerCallback callback = [=](
const auto & event)
160 auto newDistance =
event.runCount * increment;
161 auto position = initialPos + newDistance;
162 yInfo(
"[%d] New target: %f", event.runCount, position);
163 posd->setPosition(joint, position);
164 return std::abs(distance) > std::abs(newDistance);
167 yarp::os::TimerSettings settings(period);
168 yarp::os::Timer timer(settings, callback,
true);
172 yError() <<
"Unable to start trajectory thread";
176 while (timer.isRunning())
178 yarp::os::SystemClock::delaySystem(0.1);