-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharm_imitator_with_virtual_boundary.cpp
253 lines (184 loc) · 6.34 KB
/
arm_imitator_with_virtual_boundary.cpp
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
// -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
#include <yarp/os/all.h>
#include <yarp/dev/ControlBoardInterfaces.h>
#include <yarp/dev/CartesianControl.h>
#include <yarp/dev/PolyDriver.h>
#include <yarp/sig/Vector.h>
#include "../include/geometry_msgs_Pose.h"
#include <stdio.h>
#include <string>
#include <iostream>
#include <gsl/gsl_math.h>
using namespace yarp::os;
using namespace yarp::dev;
using namespace yarp::sig;
using namespace std;
class ControlThread: public RateThread
{
PolyDriver dd;
ICartesianControl *icart;
Vector cRot, cOrt; // current rotation and orientation of robot's joint
Vector dRot, dOrt; // desired rotation and orientation of robot's joint
// ROS variables
yarp::os::Node *node;
yarp::os::Subscriber<geometry_msgs_Pose> poseSub;
geometry_msgs_Pose *jointPose;
public:
ControlThread(int period):RateThread(period){}
bool threadInit()
{
//initialize here variables
printf("ControlThread:starting\n");
Property options("(device cartesiancontrollerclient)");
options.put("remote","/icubSim/cartesianController/right_arm");
options.put("local","/cartesian_client/right_arm");
dd.open(options);
if (dd.isValid()) {
dd.view(icart);
if (!icart){
return false;
}
}
// get the torso dofs
Vector newDof, curDof;
icart->getDOF(curDof);
newDof=curDof;
// enable the torso yaw and pitch
// disable the torso roll
newDof[0]=0;
newDof[1]=0;
newDof[2]=0;
// impose some restriction on the torso pitch
limitTorsoPitch();
icart->setTrackingMode(true);
// send the request for dofs reconfiguration
icart->setDOF(newDof,curDof);
// icart->setTrajTime(1.0);
dRot.resize(3);
dOrt.resize(4);
// ROS initialization
node = new yarp::os::Node("/icubSim/poseSub");
if (!poseSub.topic("/icub/jointPose")) {
cerr<< "Failed to subscriber to /icub/jointPose\n";
return -1;
}
return true;
}
void threadRelease()
{
printf("ControlThread:stopping the robot\n");
poseSub.close();
icart->stopControl();
dd.close();
printf("Done, goodbye from ControlThread\n");
}
void run()
{
printICubPoseStatus();
getHumanJointPose();
double tf = 0.60;
double x_limit_max = 0.40;
double x_limit_min = - x_limit_max;
double y_limit_max = 0.50;
double y_limit_min = - y_limit_max;
double z_limit_max = 0.40;
double z_limit_min = - z_limit_max;
dOrt = 0;
if (jointPose) {
if (jointPose->position.x <= x_limit_max && jointPose->position.x > 0){
dRot[0] = tf * jointPose->position.z;
dRot[1] = tf * x_limit_max;
dRot[2] = tf * jointPose->position.y;
}
else if (jointPose->position.x >= x_limit_max && jointPose->position.x < 0){
dRot[0] = tf * jointPose->position.z;
dRot[1] = tf * x_limit_min;
dRot[2] = tf * jointPose->position.y;
}
else if (jointPose->position.y <= y_limit_max && jointPose->position.y > 0){
dRot[0] = tf * jointPose->position.z;
dRot[1] = tf * jointPose->position.x;
dRot[2] = tf * y_limit_max;
}
else if (jointPose->position.y >= y_limit_min && jointPose->position.y < 0){
dRot[0] = tf * jointPose->position.z;
dRot[1] = tf * jointPose->position.x;
dRot[2] = tf * y_limit_min;
}
else if (jointPose->position.z <= z_limit_max && jointPose->position.z > 0){
dRot[0] = tf * z_limit_max;
dRot[1] = tf * jointPose->position.x;
dRot[2] = tf * jointPose->position.y;
}
else if (jointPose->position.z >= z_limit_min && jointPose->position.z < 0){
dRot[0] = tf * z_limit_min;
dRot[1] = tf * jointPose->position.x;
dRot[2] = tf * jointPose->position.y;
}
else {
dRot[0] = tf * jointPose->position.z;
dRot[1] = tf * jointPose->position.x;
dRot[2] = tf * jointPose->position.y;
}
// dOrt[0] = jointPose->orientation.x;
// dOrt[1] = jointPose->orientation.y;
// dOrt[2] = jointPose->orientation.z;
// dOrt[3] = jointPose->orientation.w;
cout << "robot palm dRotation (xyz)[m]: " << dRot.toString().c_str() << endl;
icart->goToPose(dRot,dOrt);
}
else {
std::cout << "couldn't convert human to robot!" << std::endl;
}
// icart->goToPoseSync(X_desired,O_desired); // send request and wait for reply
// icart->waitMotionDone(0.04); // wait until the motion is done and ping at each 0.04 seconds
}
void limitTorsoPitch()
{
int axis=0; // pitch joint
double min, max;
int MAX_TORSO_PITCH = 30.0;
// sometimes it may be helpful to reduce
// the range of variability of the joints;
// for example here we don't want the torso
// to lean out more than 30 degrees forward
// we keep the lower limit
icart->getLimits(axis,&min,&max);
icart->setLimits(axis,min,MAX_TORSO_PITCH);
}
void printICubPoseStatus(){
icart->getPose(cRot, cOrt);
cout << "robot palm rotation (xyz)[m]: " << cRot.toString().c_str() << endl;
// cout << "Current Orientation (O)[m] = " << O_current.toString().c_str() << endl;
}
void getHumanJointPose() {
jointPose = poseSub.read(false);
if (!jointPose) {
cout << "human palm rotation (xyz): no data yet!" << endl;
}
else if (jointPose) {
cout << "human palm rotation (xyz): " << jointPose->position.x << " " << jointPose->position.y << " " << jointPose->position.z << endl; // debug
}
}
};
int main(int argc, char *argv[])
{
Network yarp;
if (!yarp.checkNetwork())
{
printf("No yarp network, quitting\n");
return 1;
}
ControlThread ctrlThread(500);
ctrlThread.start();
int RUN_TIME = 3600; // seconds
bool done=false;
double startTime=Time::now();
while(!done)
{
if ((Time::now()-startTime)>RUN_TIME)
done=true;
}
ctrlThread.stop();
return 0;
}