-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathModule.cpp
More file actions
306 lines (241 loc) · 10.2 KB
/
Module.cpp
File metadata and controls
306 lines (241 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include <Module.h>
#include <cmath>
#include <yarp/os/ResourceFinder.h>
#include <yarp/os/Property.h>
#include <yarp/os/LogStream.h>
#include <yarp/sig/Vector.h>
// iDynTree headers
#include <iDynTree/Core/VectorDynSize.h>
#include <iDynTree/Model/FreeFloatingState.h>
#include <iDynTree/Model/Model.h>
#include <iDynTree/ModelIO/ModelLoader.h>
#include <iDynTree/yarp/YARPConversions.h>
void addVectorOfStringToProperty(yarp::os::Property& prop, std::string key, std::vector<std::string> & list)
{
prop.addGroup(key);
yarp::os::Bottle & bot = prop.findGroup(key).addList();
for(size_t i=0; i < list.size(); i++)
{
bot.addString(list[i].c_str());
}
return;
}
void convertDegToRad(const yarp::sig::Vector& vecDeg, yarp::sig::Vector& vecRad)
{
if (vecDeg.size() != vecRad.size()) {
yError() << "convertDegToRad: wrong vector size";
return;
}
for (size_t i=0; i < vecDeg.size(); i++) {
vecRad[i] = (M_PI/180.0)*vecDeg[i];
}
}
void convertRadToDeg(const yarp::sig::Vector& vecRad, yarp::sig::Vector& vecDeg)
{
if (vecDeg.size() != vecRad.size()) {
yError() << "convertDegToRad: wrong vector size";
return;
}
for (size_t i=0; i < vecRad.size(); i++) {
vecDeg[i] = (180.0/M_PI)*vecRad[i];
}
}
double Module::getPeriod () { return 0.01; }
bool Module::updateModule ()
{
//read state
ienc->getEncoders(positionsInDeg.data());
convertDegToRad(positionsInDeg, positionsInRad);
ienc->getEncoderSpeeds(velocitiesInDegS.data());
convertDegToRad(velocitiesInDegS, velocitiesInRadS);
// Compute the bias term of the inverse dynamics, passing data to iDynTree
// Note: for the sake of simplicity we are allocate dynamically this iDynTree
// quantities here, that in general is not real time safe.
// We are considering the "fixed base" case, i.e. the base is always fixed to the ground
iDynTree::Transform w_H_b = iDynTree::Transform::Identity(); //identity + zero vector
iDynTree::Twist baseVel = iDynTree::Twist::Zero();
iDynTree::VectorDynSize jointPos(positionsInRad.size()), jointVelSetToZero(velocitiesInRadS.size());
jointVelSetToZero.zero();
// Convert measured values to iDynTree quantities
iDynTree::toiDynTree(positionsInRad, jointPos);
// Set all other input quantities of the inverse dynamics to zero
iDynTree::Vector3 gravity;
gravity.zero();
gravity(2) = -9.81;
kinDynModel.setRobotState(w_H_b, jointPos, baseVel, jointVelSetToZero, gravity);
// The computeGeneralizedGravityForces method is computing the g(q) term, for both the base and the joint
iDynTree::FreeFloatingGeneralizedTorques g_q(kinDynModel.model());
kinDynModel.generalizedGravityForces(g_q);
// We extract the joint part to a YARP vector
iDynTree::toYarp(g_q.jointTorques(), gravityCompensation);
//compute control
for (size_t i = 0; i < positionsInRad.size(); i++) {
errorInRad(i) = referencePositionsInRad(i) - positionsInRad(i);
torquesInNm(i) = gravityCompensation(i) + kp(i) * errorInRad(i) - kd(i) * velocitiesInRadS(i);
}
itrq->setRefTorques(torquesInNm.data());
return true;
}
bool Module::configure (yarp::os::ResourceFinder &rf)
{
using namespace yarp::os;
using namespace yarp::sig;
///////////////////////////////////////////////////////
//// Open the remotecontrolboardremapper YARP device
///////////////////////////////////////////////////////
Property options;
options.put("device","remotecontrolboardremapper");
// Note: this joint list are tipically loaded from configuration
// file and they are not harcoded in the code. However, to reduce
// the complexity of the example we are hardcoding the lists.
// Torso Joints
std::vector<std::string> axesList;
axesList.push_back("torso_pitch");
axesList.push_back("torso_roll");
axesList.push_back("torso_yaw");
// Left arm
axesList.push_back("l_shoulder_pitch");
axesList.push_back("l_shoulder_roll");
axesList.push_back("l_shoulder_yaw");
axesList.push_back("l_elbow");
// Right arm
axesList.push_back("r_shoulder_pitch");
axesList.push_back("r_shoulder_roll");
axesList.push_back("r_shoulder_yaw");
axesList.push_back("r_elbow");
// Left leg
axesList.push_back("l_hip_pitch");
axesList.push_back("l_hip_roll");
axesList.push_back("l_hip_yaw");
axesList.push_back("l_knee");
axesList.push_back("l_ankle_pitch");
axesList.push_back("l_ankle_roll");
// Right leg
axesList.push_back("r_hip_pitch");
axesList.push_back("r_hip_roll");
axesList.push_back("r_hip_yaw");
axesList.push_back("r_knee");
axesList.push_back("r_ankle_pitch");
axesList.push_back("r_ankle_roll");
addVectorOfStringToProperty(options, "axesNames", axesList);
Bottle remoteControlBoards;
Bottle & remoteControlBoardsList = remoteControlBoards.addList();
std::string robotPortPrefix = rf.check("robot", yarp::os::Value("icubSim"), "Port prefix used for the controlboards").asString();
remoteControlBoardsList.addString("/"+robotPortPrefix+"/torso");
remoteControlBoardsList.addString("/"+robotPortPrefix+"/left_arm");
remoteControlBoardsList.addString("/"+robotPortPrefix+"/right_arm");
remoteControlBoardsList.addString("/"+robotPortPrefix+"/left_leg");
remoteControlBoardsList.addString("/"+robotPortPrefix+"/right_leg");
options.put("remoteControlBoards",remoteControlBoards.get(0));
options.put("localPortPrefix","/test");
Property & remoteControlBoardsOpts = options.addGroup("REMOTE_CONTROLBOARD_OPTIONS");
remoteControlBoardsOpts.put("writeStrict","on");
size_t actuatedDOFs = axesList.size();
// Actually open the device
bool ok = robotDevice.open(options);
if (!ok) {
std::cout << "Could not open remotecontrolboardremapper object.\n";
return false;
}
// Try to obtain the interfaces
ok=ok && robotDevice.view(ilim);
ok=ok && robotDevice.view(ienc);
ok=ok && robotDevice.view(imod);
ok=ok && robotDevice.view(itrq);
if (!ok) {
yError()<<"Unable to open interfaces";
return false;
}
///////////////////////////////////////////////////////////////
//// Load the model in the iDynTree::KinDynComputations
///////////////////////////////////////////////////////////////
// We assume that the model.urdf can be found by the ResourceFinder:
// this means either that the file is in the current working directory,
// or it is found using the ResourceFinder search hierarchy, documented in
// http://www.yarp.it/yarp_data_dirs.html
std::string modelFullPath = rf.findFileByName("model.urdf");
// We use the iDynTree::ModelLoader class to extract from the URDF file
// a model containing only the joint we are interested in controlling, and
// in the same order with which we configured the remotecontrolboardremapper
// device, to avoid complicated remapping between the vectors used in the YARP
// devices and the one used by the iDynTree model .
iDynTree::ModelLoader mdlLoader;
ok = mdlLoader.loadReducedModelFromFile(modelFullPath, axesList);
// Once we loaded the model, we pass it to the KinDynComputations class to
// compute dynamics quantities such as the vector of gravity torques
ok = ok && kinDynModel.loadRobotModel(mdlLoader.model());
if (!ok) {
yError()<<"Unable to open model " << modelFullPath;
return false;
}
const iDynTree::Model& model = kinDynModel.model();
///////////////////////////////////////////////////////////////
//// Resize buffers
///////////////////////////////////////////////////////////////
std::cout << "Number of DOFs: " << actuatedDOFs << "\n";
positionsInDeg.resize(actuatedDOFs, 0.0);
positionsInRad.resize(actuatedDOFs, 0.0);
velocitiesInDegS.resize(actuatedDOFs, 0.0);
velocitiesInRadS.resize(actuatedDOFs, 0.0);
gravityCompensation.resize(actuatedDOFs, 0.0);
referencePositionsInRad.resize(actuatedDOFs, 0.0);
// Make sure that we are reading data from the robot before proceeding
bool readEncoderSuccess = false;
for (int i=0; i < 10 && !readEncoderSuccess; i++) {
readEncoderSuccess = ienc->getEncoders(positionsInDeg.data());
if (!readEncoderSuccess) {
yarp::os::Time::delay(0.1);
}
}
if (!readEncoderSuccess) {
yError()<<"Unable to read encoders, exiting.";
return false;
}
convertDegToRad(positionsInDeg, referencePositionsInRad);
iDynTree::IJointConstPtr joint = model.getJoint(model.getJointIndex("l_shoulder_pitch"));
if (!joint) {
std::cout << "Could not retrieve index of l_shoulder_pitch.\n";
return false;
}
referencePositionsInRad(joint->getDOFsOffset()) += 1.5;
joint = model.getJoint(model.getJointIndex("l_elbow"));
if (!joint) {
std::cout << "Could not retrieve index of l_elbow.\n";
return false;
}
referencePositionsInRad(joint->getDOFsOffset()) += 1.5;
joint = model.getJoint(model.getJointIndex("r_shoulder_pitch"));
if (!joint) {
std::cout << "Could not retrieve index of r_shoulder_pitch.\n";
return false;
}
referencePositionsInRad(joint->getDOFsOffset()) += 1.5;
joint = model.getJoint(model.getJointIndex("r_elbow"));
if (!joint) {
std::cout << "Could not retrieve index of r_elbow.\n";
return false;
}
referencePositionsInRad(joint->getDOFsOffset()) += 1.5;
//write
errorInRad.resize(actuatedDOFs, 0.0);
kp.resize(actuatedDOFs, 1.5);
kd.resize(actuatedDOFs, 0.5);
torquesInNm.resize(actuatedDOFs, 0.0);
zeroDofs.resize(actuatedDOFs, 0.0);
baseZeroDofs.resize(6, 0.0);
grav.resize(3, 0.0);
grav(2) = -9.81;
// Setting the control mode of all the controlled joints to torque control mode
// See http://wiki.icub.org/wiki/Control_Modes for more info about the control modes
std::vector<int> ctrlModes(actuatedDOFs, VOCAB_CM_TORQUE);
imod->setControlModes(ctrlModes.data());
return true;
}
bool Module::close ()
{
std::vector<int> ctrlModes(positionsInDeg.size(), VOCAB_CM_POSITION);
imod->setControlModes(ctrlModes.data());
//cleanup stuff
robotDevice.close();
return true;
}