YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
JointsPosMotion.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
7
8#include <yarp/os/Time.h>
9#include <yarp/sig/Vector.h>
11#include <yarp/dev/PolyDriver.h>
13
14#include <robottestingframework/TestAssert.h>
15
16#include <cmath>
17
19
43
44
46{
47 jointsList = 0;
48 n_joints = 0;
49 encoders = 0;
50 speed = 0;
51
52 tolerance = 1.0;
53 timeout = 5.0;
54
55 dd = polydriver;
56 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->isValid(), "Unable to open device driver");
57 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(ienc), "Unable to open encoders interface");
58 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(ipos), "Unable to open position interface");
59 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(icmd), "Unable to open control mode interface");
60 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(iimd), "Unable to open interaction mode interface");
61 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(ilim), "Unable to open limits interface");
62}
63
64
66{
67 for(size_t i = 0; i < n_joints; i++) {
68 if(jointsList[i] == j) {
69 return i;
70 }
71 }
72 return jointsList.size()+1;
73}
74
75
77{
78 max_lims.resize(n_joints);
79 min_lims.resize(n_joints);
80 for (size_t i = 0; i < n_joints; i++) {
81 ilim->getLimits((int)jointsList[i], &min_lims[i], &max_lims[i]);
82 }
83
84}
85
86
87
89 mPriv(new Private)
90{
91 mPriv->init(polydriver);
92
93 mPriv->n_joints = jlist.size();
94 mPriv->jointsList.resize(mPriv->n_joints);
95 mPriv->jointsList = jlist;
96
97 mPriv->encoders.resize(mPriv->n_joints); mPriv->encoders.zero();
98 mPriv->speed = yarp::sig::Vector(mPriv->n_joints, 10.0);
99
100 //send default speed
101 for (size_t i = 0; i < mPriv->n_joints; i++) {
102 mPriv->ipos->setRefSpeed((int)mPriv->jointsList[i], mPriv->speed[i]);
103 }
104 mPriv->readJointsLimits();
105}
106
107
108
113
114
116{
117 mPriv->tolerance = tolerance;
118}
119
120
122{
123 for (size_t i = 0; i < mPriv->jointsList.size(); i++) {
124 mPriv->icmd->setControlMode((int)mPriv->jointsList[i], VOCAB_CM_POSITION);
125 mPriv->iimd->setInteractionMode((int)mPriv->jointsList[i], VOCAB_IM_STIFF);
127 }
128
129 int cmode;
131 double time_started = yarp::os::Time::now();
132
133 while (1) {
134 size_t ok = 0;
135 for (size_t i = 0; i < mPriv->n_joints; i++) {
136 mPriv->icmd->getControlMode ((int)mPriv->jointsList[i], &cmode);
137 mPriv->iimd->getInteractionMode((int)mPriv->jointsList[i], &imode);
138 if (cmode == VOCAB_CM_POSITION && imode == VOCAB_IM_STIFF) {
139 ok++;
140 }
141 }
142 if (ok == mPriv->n_joints) {
143 break;
144 }
145 if (yarp::os::Time::now() - time_started > mPriv->timeout) {
146 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR("Unable to set control mode/interaction mode");
147 }
148
150
151 }
152
153 return true;
154}
155
156
158{
159 mPriv->timeout = timeout;
160}
161
162
163
165{
166 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE((speedlist.size() != mPriv->jointsList.size()), "Speed list has a different size of joint list");
167 mPriv->speed = speedlist;
168 for (size_t i = 0; i < mPriv->n_joints; i++) {
169 mPriv->ipos->setRefSpeed((int)mPriv->jointsList[i], mPriv->speed[i]);
170 }
171}
172
173
174bool yarp::robottestingframework::jointsPosMotion::goToSingle(int j, double pos, double *reached_pos)
175{
176 size_t i = mPriv->getIndexOfJoint(j);
177 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(i < mPriv->n_joints, "Cannot move a joint not in list.");
178
179 mPriv->ipos->positionMove((int)mPriv->jointsList[i], pos);
180 double tmp = 0;
181
182 double time_started = yarp::os::Time::now();
183 bool ret = true;
184 while (1) {
185 mPriv->ienc->getEncoder((int)mPriv->jointsList[i], &tmp);
186 if (fabs(tmp-pos)<mPriv->tolerance) {
187 break;
188 }
189
190 if (yarp::os::Time::now()-time_started > mPriv->timeout) {
191 ret = false;
192 break;
193 }
194
196 }
197
198 if(reached_pos != nullptr) {
199 *reached_pos = tmp;
200 }
201 return(ret);
202}
203
204
206{
207 for (unsigned int i=0; i<mPriv->n_joints; i++) {
208 mPriv->ipos->positionMove((int)mPriv->jointsList[i], positions[i]);
209 }
210
211 double time_started = yarp::os::Time::now();
212 yarp::sig::Vector tmp(mPriv->n_joints);tmp.zero();
213 bool ret = true;
214
215 while (1) {
216 size_t in_position = 0;
217 for (size_t i = 0; i < mPriv->n_joints; i++) {
218 mPriv->ienc->getEncoder((int)mPriv->jointsList[i], &tmp[i]);
219 if (fabs(tmp[i] - positions[i]) < mPriv->tolerance) {
220 in_position++;
221 }
222 }
223 if (in_position == mPriv->n_joints) {
224 break;
225 }
226
227 if (yarp::os::Time::now()-time_started > mPriv->timeout) {
228 ret = false;
229 break;
230 }
232 }
233
234 if(reached_pos != nullptr) {
235 reached_pos->resize(mPriv->n_joints);
236 *reached_pos = tmp;
237 }
238 return(ret);
239}
240
241
242
244{
245 size_t i = mPriv->getIndexOfJoint(j);
246
247 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(i < mPriv->n_joints, "Cannot move a joint not in list.");
248
249 double enc=0;
250 mPriv->ienc->getEncoder((int)mPriv->jointsList[i], &enc);
251 if (fabs(enc-mPriv->max_lims[i]) < mPriv->tolerance ||
252 fabs(enc-mPriv->min_lims[i]) < mPriv->tolerance ) {
253 return true;
254 } else {
255 return false;
256 }
257}
define control board standard interfaces
constexpr yarp::conf::vocab32_t VOCAB_CM_POSITION
bool ret
contains the definition of a Vector type
bool view(T *&x)
Get an interface to the device driver.
Interface for control devices, commands to get/set position and veloity limits.
Interface for setting control mode in control board.
Control board, encoder interface.
Definition IEncoders.h:116
Interface settings the way the robot interacts with the environment: basic interaction types are Stif...
Interface for a generic control board device implementing position control.
virtual bool setRefSpeed(int j, double sp)=0
Set reference speed for a joint, this is the speed used during the interpolation of the trajectory.
A container for a device driver.
Definition PolyDriver.h:23
bool isValid() const
Check if device is valid.
void init(yarp::dev::PolyDriver *polydriver)
void setTolerance(double tolerance)
Sets tolerance used to check if a joint is in position.
void setTimeout(double timeout)
Sets timeout.
bool goToSingle(int j, double pos, double *reached_pos=nullptr)
Moves joint j in position pos and checks if joint reaches target within tolerance range in maximun ti...
bool checkJointLimitsReached(int j)
Checks if joint j has reached its limit within tollerance range.
bool setAndCheckPosControlMode()
Sets all joints in position control mode and checks if all are in position control mode waiting timeo...
bool goTo(yarp::sig::Vector positions, yarp::sig::Vector *reached_pos=nullptr)
Moves joints in corresponding positions specified by positions and checks if all joints reach its tar...
void setSpeed(yarp::sig::Vector &speedlist)
Sets speed of each joint used in position control.
jointsPosMotion(yarp::dev::PolyDriver *polydriver, yarp::sig::Vector &jlist)
Creates an object for control joints specified in @jlist.
void resize(size_t size) override
Resize the vector.
Definition Vector.h:221
size_t size() const
Definition Vector.h:341
void zero()
Zero the elements of the vector.
Definition Vector.h:363
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition Time.cpp:121
void delay(double seconds)
Wait for a certain number of seconds.
Definition Time.cpp:111
VectorOf< double > Vector
Definition Vector.h:36