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 }
yarp::sig::VectorOf::resize
void resize(size_t size) override
Resize the vector.
Definition: Vector.h:254
yarp::robottestingframework::jointsPosMotion::checkJointLimitsReached
bool checkJointLimitsReached(int j)
Checks if joint j has reached its limit within tollerance range.
Definition: JointsPosMotion.cpp:243
Vector.h
contains the definition of a Vector type
yarp::robottestingframework::jointsPosMotion::Private::dd
yarp::dev::PolyDriver * dd
Definition: JointsPosMotion.cpp:38
yarp::robottestingframework::jointsPosMotion::Private::tolerance
double tolerance
Definition: JointsPosMotion.cpp:34
yarp::dev::IEncoders
Control board, encoder interface.
Definition: IEncoders.h:121
yarp::dev::PolyDriver::isValid
bool isValid() const
Check if device is valid.
Definition: PolyDriver.cpp:199
yarp::robottestingframework::jointsPosMotion::setTolerance
void setTolerance(double tolerance)
Sets tolerance used to check if a joint is in position.
Definition: JointsPosMotion.cpp:117
yarp::robottestingframework::jointsPosMotion::Private::encoders
yarp::sig::Vector encoders
Definition: JointsPosMotion.cpp:30
yarp::robottestingframework::jointsPosMotion::Private::min_lims
yarp::sig::Vector min_lims
Definition: JointsPosMotion.cpp:33
yarp::robottestingframework::jointsPosMotion::Private::readJointsLimits
void readJointsLimits()
Definition: JointsPosMotion.cpp:78
yarp::robottestingframework::jointsPosMotion::goToSingle
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...
Definition: JointsPosMotion.cpp:176
yarp::robottestingframework::jointsPosMotion::Private::ienc
yarp::dev::IEncoders * ienc
Definition: JointsPosMotion.cpp:42
yarp::dev::DeviceDriver::view
bool view(T *&x)
Get an interface to the device driver.
Definition: DeviceDriver.h:77
yarp::robottestingframework::jointsPosMotion::~jointsPosMotion
virtual ~jointsPosMotion()
Definition: JointsPosMotion.cpp:111
ret
bool ret
Definition: ImplementAxisInfo.cpp:72
ControlBoardInterfaces.h
define control board standard interfaces
yarp::os::Time::now
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition: Time.cpp:124
yarp::dev::IPositionControl::setRefSpeed
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.
yarp::robottestingframework::jointsPosMotion::Private::timeout
double timeout
Definition: JointsPosMotion.cpp:35
VOCAB_CM_POSITION
constexpr yarp::conf::vocab32_t VOCAB_CM_POSITION
Definition: IControlMode.h:128
yarp::robottestingframework::jointsPosMotion::Private::icmd
yarp::dev::IControlMode * icmd
Definition: JointsPosMotion.cpp:40
yarp::sig::VectorOf< double >
yarp::dev::IInteractionMode
Interface settings the way the robot interacts with the environment: basic interaction types are Stif...
Definition: IInteractionMode.h:43
yarp::robottestingframework::jointsPosMotion::setAndCheckPosControlMode
bool setAndCheckPosControlMode()
Sets all joints in position control mode and checks if all are in position control mode waiting timeo...
Definition: JointsPosMotion.cpp:123
yarp::dev::IControlMode
Interface for setting control mode in control board.
Definition: IControlMode.h:28
PolyDriver.h
yarp::robottestingframework::jointsPosMotion::Private::getIndexOfJoint
size_t getIndexOfJoint(int j)
Definition: JointsPosMotion.cpp:67
yarp::dev::IControlLimits
Interface for control devices, commands to get/set position and veloity limits.
Definition: IControlLimits.h:33
yarp::dev::PolyDriver
A container for a device driver.
Definition: PolyDriver.h:27
yarp::dev::InteractionModeEnum
InteractionModeEnum
Definition: IInteractionMode.h:21
yarp::sig::VectorOf::zero
void zero()
Zero the elements of the vector.
Definition: Vector.h:377
yarp::robottestingframework::jointsPosMotion::Private
Definition: JointsPosMotion.cpp:23
JointsPosMotion.h
yarp::robottestingframework::jointsPosMotion::goTo
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...
Definition: JointsPosMotion.cpp:207
yarp::robottestingframework::jointsPosMotion::Private::max_lims
yarp::sig::Vector max_lims
Definition: JointsPosMotion.cpp:32
yarp::robottestingframework::jointsPosMotion::setSpeed
void setSpeed(yarp::sig::Vector &speedlist)
Sets speed of each joint used in position control.
Definition: JointsPosMotion.cpp:166
yarp::robottestingframework::jointsPosMotion::Private::iimd
yarp::dev::IInteractionMode * iimd
Definition: JointsPosMotion.cpp:41
yarp::robottestingframework::jointsPosMotion::Private::ilim
yarp::dev::IControlLimits * ilim
Definition: JointsPosMotion.cpp:43
yarp::sig::Vector
VectorOf< double > Vector
Definition: Vector.h:33
yarp::robottestingframework::jointsPosMotion::Private::jointsList
yarp::sig::Vector jointsList
Definition: JointsPosMotion.cpp:29
yarp::dev::VOCAB_IM_STIFF
@ VOCAB_IM_STIFF
Definition: IInteractionMode.h:22
yarp::robottestingframework::jointsPosMotion::Private::ipos
yarp::dev::IPositionControl * ipos
Definition: JointsPosMotion.cpp:39
yarp::robottestingframework::jointsPosMotion::Private::speed
yarp::sig::Vector speed
Definition: JointsPosMotion.cpp:31
yarp::robottestingframework::jointsPosMotion::Private::init
void init(yarp::dev::PolyDriver *polydriver)
Definition: JointsPosMotion.cpp:47
Time.h
yarp::robottestingframework::jointsPosMotion::jointsPosMotion
jointsPosMotion(yarp::dev::PolyDriver *polydriver, yarp::sig::Vector &jlist)
Creates an object for control joints specified in @jlist.
Definition: JointsPosMotion.cpp:90
yarp::sig::VectorOf::size
size_t size() const
Definition: Vector.h:355
yarp::robottestingframework::jointsPosMotion::setTimeout
void setTimeout(double timeout)
Sets timeout.
Definition: JointsPosMotion.cpp:159
yarp::dev::IPositionControl
Interface for a generic control board device implementing position control.
Definition: IPositionControl.h:257
yarp::os::Time::delay
void delay(double seconds)
Wait for a certain number of seconds.
Definition: Time.cpp:114
yarp::robottestingframework::jointsPosMotion::Private::n_joints
size_t n_joints
Definition: JointsPosMotion.cpp:36