YARP
Yet Another Robot Platform
JointsPosMotion.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
10 
11 #include <yarp/os/Time.h>
12 #include <yarp/sig/Vector.h>
14 #include <yarp/dev/PolyDriver.h>
15 
16 #include <robottestingframework/TestAssert.h>
17 
18 #include <cmath>
19 
21 
23 {
24 public:
25  void init(yarp::dev::PolyDriver *polydriver);
26  size_t getIndexOfJoint(int j);
27  void readJointsLimits();
28 
34  double tolerance;
35  double timeout;
36  size_t n_joints;
37 
44 };
45 
46 
48 {
49  jointsList = 0;
50  n_joints = 0;
51  encoders = 0;
52  speed = 0;
53 
54  tolerance = 1.0;
55  timeout = 5.0;
56 
57  dd = polydriver;
58  ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->isValid(), "Unable to open device driver");
59  ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(ienc), "Unable to open encoders interface");
60  ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(ipos), "Unable to open position interface");
61  ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(icmd), "Unable to open control mode interface");
62  ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(iimd), "Unable to open interaction mode interface");
63  ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(ilim), "Unable to open limits interface");
64 }
65 
66 
68 {
69  for(size_t i = 0; i < n_joints; i++) {
70  if(jointsList[i] == j) {
71  return i;
72  }
73  }
74  return jointsList.size()+1;
75 }
76 
77 
79 {
80  max_lims.resize(n_joints);
81  min_lims.resize(n_joints);
82  for (size_t i = 0; i < n_joints; i++) {
83  ilim->getLimits((int)jointsList[i], &min_lims[i], &max_lims[i]);
84  }
85 
86 }
87 
88 
89 
91  mPriv(new Private)
92 {
93  mPriv->init(polydriver);
94 
95  mPriv->n_joints = jlist.size();
96  mPriv->jointsList.resize(mPriv->n_joints);
97  mPriv->jointsList = jlist;
98 
99  mPriv->encoders.resize(mPriv->n_joints); mPriv->encoders.zero();
100  mPriv->speed = yarp::sig::Vector(mPriv->n_joints, 10.0);
101 
102  //send default speed
103  for (size_t i = 0; i < mPriv->n_joints; i++) {
104  mPriv->ipos->setRefSpeed((int)mPriv->jointsList[i], mPriv->speed[i]);
105  }
106  mPriv->readJointsLimits();
107 }
108 
109 
110 
112 {
113  delete mPriv;
114 }
115 
116 
118 {
119  mPriv->tolerance = tolerance;
120 }
121 
122 
124 {
125  for (size_t i = 0; i < mPriv->jointsList.size(); i++) {
126  mPriv->icmd->setControlMode((int)mPriv->jointsList[i], VOCAB_CM_POSITION);
127  mPriv->iimd->setInteractionMode((int)mPriv->jointsList[i], VOCAB_IM_STIFF);
128  yarp::os::Time::delay(0.010);
129  }
130 
131  int cmode;
133  double time_started = yarp::os::Time::now();
134 
135  while (1) {
136  size_t ok = 0;
137  for (size_t i = 0; i < mPriv->n_joints; i++) {
138  mPriv->icmd->getControlMode ((int)mPriv->jointsList[i], &cmode);
139  mPriv->iimd->getInteractionMode((int)mPriv->jointsList[i], &imode);
140  if (cmode == VOCAB_CM_POSITION && imode == VOCAB_IM_STIFF) {
141  ok++;
142  }
143  }
144  if (ok == mPriv->n_joints) {
145  break;
146  }
147  if (yarp::os::Time::now() - time_started > mPriv->timeout) {
148  ROBOTTESTINGFRAMEWORK_ASSERT_ERROR("Unable to set control mode/interaction mode");
149  }
150 
152 
153  }
154 
155  return true;
156 }
157 
158 
160 {
161  mPriv->timeout = timeout;
162 }
163 
164 
165 
167 {
168  ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE((speedlist.size() != mPriv->jointsList.size()), "Speed list has a different size of joint list");
169  mPriv->speed = speedlist;
170  for (size_t i = 0; i < mPriv->n_joints; i++) {
171  mPriv->ipos->setRefSpeed((int)mPriv->jointsList[i], mPriv->speed[i]);
172  }
173 }
174 
175 
176 bool yarp::robottestingframework::jointsPosMotion::goToSingle(int j, double pos, double *reached_pos)
177 {
178  size_t i = mPriv->getIndexOfJoint(j);
179  ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(i < mPriv->n_joints, "Cannot move a joint not in list.");
180 
181  mPriv->ipos->positionMove((int)mPriv->jointsList[i], pos);
182  double tmp = 0;
183 
184  double time_started = yarp::os::Time::now();
185  bool ret = true;
186  while (1) {
187  mPriv->ienc->getEncoder((int)mPriv->jointsList[i], &tmp);
188  if (fabs(tmp-pos)<mPriv->tolerance) {
189  break;
190  }
191 
192  if (yarp::os::Time::now()-time_started > mPriv->timeout) {
193  ret = false;
194  break;
195  }
196 
198  }
199 
200  if(reached_pos != nullptr) {
201  *reached_pos = tmp;
202  }
203  return(ret);
204 }
205 
206 
208 {
209  for (unsigned int i=0; i<mPriv->n_joints; i++) {
210  mPriv->ipos->positionMove((int)mPriv->jointsList[i], positions[i]);
211  }
212 
213  double time_started = yarp::os::Time::now();
214  yarp::sig::Vector tmp(mPriv->n_joints);tmp.zero();
215  bool ret = true;
216 
217  while (1) {
218  size_t in_position = 0;
219  for (size_t i = 0; i < mPriv->n_joints; i++) {
220  mPriv->ienc->getEncoder((int)mPriv->jointsList[i], &tmp[i]);
221  if (fabs(tmp[i]-positions[i])<mPriv->tolerance)
222  in_position++;
223  }
224  if (in_position == mPriv->n_joints)
225  break;
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
Definition: IControlMode.h:128
bool ret
contains the definition of a Vector type
bool view(T *&x)
Get an interface to the device driver.
Definition: DeviceDriver.h:77
Interface for control devices, commands to get/set position and veloity limits.
Interface for setting control mode in control board.
Definition: IControlMode.h:28
Control board, encoder interface.
Definition: IEncoders.h:121
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:27
bool isValid() const
Check if device is valid.
Definition: PolyDriver.cpp:199
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:254
size_t size() const
Definition: Vector.h:355
void zero()
Zero the elements of the vector.
Definition: Vector.h:377
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition: Time.cpp:124
void delay(double seconds)
Wait for a certain number of seconds.
Definition: Time.cpp:114
VectorOf< double > Vector
Definition: Vector.h:33