Statistics
| Branch: | Tag: | Revision:

humotion / examples / yarp_icub / src / icub_jointinterface.cpp @ 0d0f5ca1

History | View | Annotate | Download (17.757 KB)

1
#include "icub_jointinterface.h"
2
#include "icub_faceinterface.h"
3

    
4
#include <yarp/os/Property.h>
5
using namespace yarp::dev;
6
using namespace yarp::sig;
7
using namespace yarp::os;
8
using namespace std;
9
/*running:
10
/media/local_data/sschulz/iros15/icub-nightly/share/iCub/contexts/simConfig:> iCub_SIM
11
/media/local_data/sschulz/iros15/icub-nightly/share/iCub/contexts/simFaceExpressions:> ../../../../bin/simFaceExpressions
12
yarp connect /face/eyelids /icubSim/face/eyelids
13
yarp connect /face/image/out /icubSim/texture/face
14

15
TEST: yarp write /writer /icubSim/face/raw/in
16

17
http://wiki.icub.org/wiki/Motor_control
18
*/
19

    
20
//WARNING: DO NOT CHANGE THIS; VELOCITYMODE IS NOT YET IMPLEMENTED
21
#define POSITION_CONTROL 0
22

    
23

    
24
//! constructor
25
iCubJointInterface::iCubJointInterface(string _scope) : humotion::server::JointInterface(){
26
    scope = _scope;
27
    face_interface = new iCubFaceInterface(scope);
28

    
29
    //add mapping from ids to enums:
30
    //this might look strange at the first sight but we need to have a generic
31
    //way to acces joints from libhumotion. therefore the lib uses its enum with ID_* enum ids
32
    //to access the joints. now we need to define a mapping to map those to our motor ids.
33
    //this is what we use the enum bimap for (convertion fro/to motorid is handled
34
    //by \sa convert_enum_to_motorid() and \sa convert_motorid_to_enum() lateron
35

    
36
    //MOUTH
37
    enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_LIP_LEFT_UPPER,   ID_LIP_LEFT_UPPER));
38
    enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_LIP_LEFT_LOWER,   ID_LIP_LEFT_LOWER));
39
    enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_LIP_CENTER_UPPER, ID_LIP_CENTER_UPPER));
40
    enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_LIP_CENTER_LOWER, ID_LIP_CENTER_LOWER));
41
    enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_LIP_RIGHT_UPPER,  ID_LIP_RIGHT_UPPER));
42
    enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_LIP_RIGHT_LOWER,  ID_LIP_RIGHT_LOWER));
43

    
44
    //NECK
45
    enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_NECK_PAN,    ID_NECK_PAN));
46
    enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_NECK_TILT,   ID_NECK_TILT));
47
    enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_NECK_ROLL,   ID_NECK_ROLL));
48

    
49
    //EYES
50
    enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_EYES_PAN,   ID_EYES_LEFT_LR));
51
    enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_EYES_VERGENCE,   ID_EYES_RIGHT_LR));
52
    enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_EYES_BOTH_UD,   ID_EYES_BOTH_UD));
53

    
54
    //EYELIDS
55
    enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_EYES_LEFT_LID_LOWER, ID_EYES_LEFT_LID_LOWER));
56
    enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_EYES_LEFT_LID_UPPER, ID_EYES_LEFT_LID_UPPER));
57
    enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_EYES_LEFT_BROW, ID_EYES_LEFT_BROW));
58

    
59
    enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_EYES_RIGHT_LID_LOWER, ID_EYES_RIGHT_LID_LOWER));
60
    enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_EYES_RIGHT_LID_UPPER,ID_EYES_RIGHT_LID_UPPER));
61
    enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_EYES_RIGHT_BROW, ID_EYES_RIGHT_BROW));
62

    
63
    Property options;
64
    options.put("device", "remote_controlboard");
65
    options.put("local", "/local/head");
66
    options.put("remote", scope+"/head");
67
    dd.open(options);
68

    
69
    //fetch views:
70
    dd.view(iencs);
71
    dd.view(ipos);
72
    dd.view(ivel);
73
    dd.view(ilimits);
74
    dd.view(pid);
75
    dd.view(amp);
76

    
77

    
78
    if ( (!iencs) || (!ipos) || (!ilimits) || (!ivel) || (!amp) || (!pid)){
79
        printf("> ERROR: failed to open icub views\n");
80
        exit(EXIT_FAILURE);
81
    }
82

    
83
    int joints;
84

    
85
    //tell humotion about min/max joint values:
86
    init_joints();
87

    
88
    iencs->getAxes(&joints);
89
    positions.resize(joints);
90
    velocities.resize(joints);
91
    commands.resize(joints);
92

    
93
    //set position mode:
94
    if (POSITION_CONTROL){
95
        commands=200000.0;
96
        ipos->setRefAccelerations(commands.data());
97
        ipos->setPositionMode();
98
    }else{
99
        ivel->setVelocityMode();
100
        commands=100.0;
101
        ivel->setRefAccelerations(commands.data());
102
    }
103

    
104
}
105

    
106
//! destructor
107
iCubJointInterface::~iCubJointInterface(){
108
}
109

    
110

    
111

    
112
//! conversion table for humotion motor ids to our ids:
113
//! \param enum from JointInterface::JOINT_ID_ENUM
114
//! \return int value of motor id
115
int iCubJointInterface::convert_enum_to_motorid(int e){
116
    enum_id_bimap_t::right_const_iterator it = enum_id_bimap.right.find(e);
117
    if(it == enum_id_bimap.right.end()) {
118
        //key does not exists, we are not interested in that dataset, return -1
119
        return -1;
120
    }
121
    return it->second;
122
}
123

    
124

    
125
//! conversion table for our ids to humotion motor ids:
126
//! \param  int value of motor id
127
//! \return enum from JointInterface::JOINT_ID_ENUM
128
int iCubJointInterface::convert_motorid_to_enum(int id){
129
    enum_id_bimap_t::left_const_iterator it = enum_id_bimap.left.find(id);
130
    if(it == enum_id_bimap.left.end()) {
131
        //key does not exists, we are not interested in that dataset, return -1
132
        return -1;
133
    }
134
    return it->second;
135
}
136

    
137

    
138
void iCubJointInterface::run(){
139
    iCubDataReceiver *data_receiver = new iCubDataReceiver(1000.0 / MAIN_LOOP_FREQUENCY, iencs, this);
140
    data_receiver->start();
141
}
142

    
143
//! set the target position of a joint
144
//! \param enum id of joint
145
//! \param float value
146
void iCubJointInterface::publish_target_position(int e){
147
    //first: convert humotion enum to our enum:
148
    int id = convert_enum_to_motorid(e);
149

    
150
    if (id == -1){
151
        return; //we are not interested in that data, so we just return here
152
    }
153

    
154
    if (id == ICUB_ID_NECK_PAN){
155
        //PAN seems to be swapped
156
        store_joint(ICUB_ID_NECK_PAN, -joint_target[e]);
157
    }else if ((id == ICUB_ID_EYES_PAN) || ( id == ICUB_ID_EYES_VERGENCE)){
158
        //icub handles eyes differently, we have to set pan angle + vergence
159
        float pan      = (joint_target[ID_EYES_LEFT_LR] + joint_target[ID_EYES_RIGHT_LR]) / 2;
160
        float vergence = (joint_target[ID_EYES_LEFT_LR]  - joint_target[ID_EYES_RIGHT_LR]);
161
        //printf("EYEDBG %3.2f %3.2f --_> pan %3.2f verg=%3.2f\n",joint_target[ID_EYES_LEFT_LR], joint_target[ID_EYES_RIGHT_LR],pan,vergence);
162

    
163
        store_joint(ICUB_ID_EYES_PAN, pan);
164
        store_joint(ICUB_ID_EYES_VERGENCE, vergence);
165
    }else{
166
        store_joint(id, joint_target[e]);
167
    }
168
}
169

    
170

    
171
//! set the target position of a joint
172
//! \param id of joint
173
//! \param float value of position
174
void iCubJointInterface::store_joint(int id, float value){
175
    printf("> set joint %d = %f\n",id,value);
176
    target_angle[id] = value;
177
}
178

    
179
//! execute a move in position mode
180
//! \param id of joint
181
//! \param angle
182
void iCubJointInterface::set_target_in_positionmode(int id, double value){
183
    if (id>ICUB_ID_EYES_VERGENCE){
184
        printf("> ERROR: set_target_positionmode(id=%d, %3.2f) not supported for this id\n",id,value);
185
        return;
186
    }
187

    
188
    // execute motion as position control cmd
189
    ipos->positionMove(id, value);
190

    
191
}
192

    
193
//! execute a move in velocity mode
194
//! \param id of joint
195
//! \param angle
196
void iCubJointInterface::set_target_in_velocitymode(int id, double value){
197
    // set speed cacluated as in velocity + set position -> replicates smoothmotion from flobi?!
198
    //first: calculate necessary speed to reach the given target within the next clock tick:
199
    double distance = value - target_angle_previous[id];
200

    
201
    //make the motion smooth: we want to reach 85% of the target in the next iteration:
202
    distance = 0.85 * distance;
203

    
204
    //distance = -5.0 / 50.0;
205

    
206
    //calculate speed
207
    //double speed = distance * ((double)MAIN_LOOP_FREQUENCY);
208

    
209

    
210

    
211
    int e = convert_motorid_to_enum(id);
212
    double speed = joint_target_speed[e];
213

    
214
    double max = 150.0;
215
    if (speed > max) speed = max;
216
    if (speed < -max) speed = -max;
217

    
218
    //speed = -speed;
219

    
220

    
221
    //execute:
222
    //ivel->velocityMove(id, speed);
223
    if ((id == ICUB_ID_NECK_PAN)  || (id == ICUB_ID_EYES_BOTH_UD) || (id == ICUB_ID_NECK_TILT) || (id == ICUB_ID_EYES_BOTH_UD) ||  (id == ICUB_ID_NECK_TILT) ){
224
        //if (id == ICUB_ID_NECK_PAN) speed = -speed;
225
        ivel->velocityMove(id, speed);
226
        printf("> VEL now=%3.2f target=%3.2f --> dist=%3.2f speed=%3.2f\n",target_angle_previous[id],value,distance,speed);
227
    }
228

    
229
    target_angle_previous[id] = get_ts_position(convert_motorid_to_enum(id)).get_newest_value();
230
}
231

    
232
//! actually execute the scheduled motion commands
233
void iCubJointInterface::execute_motion(){
234

    
235
    // set up neck and eye motion commands:
236
    if (POSITION_CONTROL){
237
        //position control
238
        for(int i=ICUB_ID_NECK_TILT; i<=ICUB_ID_EYES_VERGENCE; i++){
239
            set_target_in_positionmode(i, target_angle[i]);
240
        }
241
    }else{
242
        //velocity control
243
        for(int i=ICUB_ID_NECK_TILT; i<=ICUB_ID_EYES_VERGENCE; i++){
244
            set_target_in_velocitymode(i, target_angle[i]);
245
        }
246
    }
247
    //printf("> TARGET PAN = %3.2f\n",target_angle[ICUB_ID_NECK_PAN]);
248

    
249

    
250
    //eyelids: unfortuantely the icub has only 1dof for eyelids, so we use only one dof here:
251
    face_interface->set_eyelid_angle(target_angle[ICUB_ID_EYES_RIGHT_LID_UPPER]);
252

    
253
    //eyebrows are set using a special command as well:
254
    face_interface->set_eyebrow_angle(ICUB_ID_EYES_LEFT_BROW, target_angle);
255
    face_interface->set_eyebrow_angle(ICUB_ID_EYES_RIGHT_BROW, target_angle);
256

    
257
    //mouth
258
    face_interface->set_mouth(target_angle);
259

    
260

    
261
    //store joint values which we do not handle on icub here:
262
    double timestamp = get_timestamp_ms();
263
    JointInterface::store_incoming_position(ID_LIP_LEFT_UPPER,   target_angle[ICUB_ID_LIP_LEFT_UPPER], timestamp);
264
    JointInterface::store_incoming_position(ID_LIP_LEFT_LOWER,   target_angle[ICUB_ID_LIP_LEFT_LOWER], timestamp);
265
    JointInterface::store_incoming_position(ID_LIP_CENTER_UPPER, target_angle[ICUB_ID_LIP_CENTER_UPPER], timestamp);
266
    JointInterface::store_incoming_position(ID_LIP_CENTER_LOWER, target_angle[ICUB_ID_LIP_CENTER_LOWER], timestamp);
267
    JointInterface::store_incoming_position(ID_LIP_RIGHT_UPPER,  target_angle[ICUB_ID_LIP_RIGHT_UPPER], timestamp);
268
    JointInterface::store_incoming_position(ID_LIP_RIGHT_LOWER,  target_angle[ICUB_ID_LIP_RIGHT_LOWER], timestamp);
269
}
270

    
271
double iCubJointInterface::get_timestamp_ms(){
272
    struct timespec spec;
273
    clock_gettime(CLOCK_REALTIME, &spec);
274
    return spec.tv_sec*1000 + spec.tv_nsec / 1.0e6;
275
}
276

    
277
//! set the current position of a joint
278
//! \param id of joint
279
//! \param float value of position
280
//! \param double timestamp
281
void iCubJointInterface::fetch_position(int id, double value, double timestamp){
282
    //store joint based on id:
283
    switch(id){
284
        default:
285
            printf("> ERROR: unhandled joint id %d\n",id);
286
            return;
287

    
288
        case(100):
289
            //JointInterface::store_incoming_position(ID_EYES_RIGHT_LID_UPPER, lid_angle, timestamp);
290
            break;
291

    
292
        case(ICUB_ID_NECK_PAN):
293
            //PAN is inverted!
294
            JointInterface::store_incoming_position(ID_NECK_PAN, value, timestamp);
295
            break;
296

    
297
        case(ICUB_ID_NECK_TILT):
298
            JointInterface::store_incoming_position(ID_NECK_TILT, value, timestamp);
299
            break;
300

    
301
        case(ICUB_ID_NECK_ROLL):
302
            JointInterface::store_incoming_position(ID_NECK_ROLL, value, timestamp);
303
            break;
304

    
305
        case(ICUB_ID_EYES_BOTH_UD):
306
            JointInterface::store_incoming_position(ID_EYES_BOTH_UD, value, timestamp);
307
            break;
308

    
309
        //icub handles eyes differently, we have to set pan angle + vergence
310
        case(ICUB_ID_EYES_PAN): {//pan
311
            last_pos_eye_pan = value;
312
            float left  = last_pos_eye_pan + last_pos_eye_vergence/2.0;
313
            float right = last_pos_eye_pan - last_pos_eye_vergence/2.0;
314

    
315
            //printf("> eye: pan=%3.2f vergence=%3.2f --> L=%3.2f R=%3.2f\n", last_pos_eye_pan, last_pos_eye_vergence, left, right);
316
            JointInterface::store_incoming_position(ID_EYES_LEFT_LR, left, timestamp);
317
            JointInterface::store_incoming_position(ID_EYES_RIGHT_LR, right, timestamp);
318
            break;
319
        }
320

    
321
        case(ICUB_ID_EYES_VERGENCE): { //vergence
322
            last_pos_eye_vergence = value;
323
            float left  = last_pos_eye_pan + last_pos_eye_vergence/2.0;
324
            float right = last_pos_eye_pan - last_pos_eye_vergence/2.0;
325

    
326
            //printf("> eye: pan=%3.2f vergence=%3.2f --> L=%3.2f R=%3.2f\n", last_pos_eye_pan, last_pos_eye_vergence, left, right);
327
            JointInterface::store_incoming_position(ID_EYES_LEFT_LR, left, timestamp);
328
            JointInterface::store_incoming_position(ID_EYES_RIGHT_LR, right, timestamp);
329
            break;
330
        }
331
    }
332

    
333

    
334
}
335

    
336
//! set the current speed of a joint
337
//! \param enum id of joint
338
//! \param float value of speed
339
//! \param double timestamp
340
void iCubJointInterface::fetch_speed(int id, double value, double timestamp){
341

    
342
    switch(id){
343
        default:
344
            printf("> ERROR: unhandled joint id %d\n",id);
345
            return;
346

    
347
        case(ICUB_ID_NECK_PAN):
348
            //PAN IS INVERTED
349
            JointInterface::store_incoming_speed(ID_NECK_PAN, value, timestamp);
350
            break;
351

    
352
        case(ICUB_ID_NECK_TILT):
353
            JointInterface::store_incoming_speed(ID_NECK_TILT, value, timestamp);
354
            break;
355

    
356
        case(ICUB_ID_NECK_ROLL):
357
            JointInterface::store_incoming_speed(ID_NECK_ROLL, value, timestamp);
358
            break;
359

    
360
        case(ICUB_ID_EYES_BOTH_UD):
361
            JointInterface::store_incoming_speed(ID_EYES_BOTH_UD, value, timestamp);
362
            break;
363

    
364
        //icub handles eyes differently, we have to set pan angle + vergence
365
        case(ICUB_ID_EYES_PAN): {//pan
366
            last_vel_eye_pan = value;
367
            float left  = last_vel_eye_pan + last_vel_eye_vergence/2.0;
368
            float right = last_vel_eye_pan - last_vel_eye_vergence/2.0;
369

    
370
            //printf("> eye: velocity pan=%3.2f vergence=%3.2f --> L=%3.2f R=%3.2f\n", last_vel_eye_pan, last_vel_eye_vergence, left, right);
371
            JointInterface::store_incoming_speed(ID_EYES_LEFT_LR, left, timestamp);
372
            JointInterface::store_incoming_speed(ID_EYES_RIGHT_LR, right, timestamp);
373
            break;
374
        }
375

    
376
        case(ICUB_ID_EYES_VERGENCE): { //vergence
377
            last_vel_eye_pan = value;
378
            float left  = last_vel_eye_pan + last_vel_eye_vergence/2.0;
379
            float right = last_vel_eye_pan - last_vel_eye_vergence/2.0;
380

    
381
            //printf("> eye: velocity pan=%3.2f vergence=%3.2f --> L=%3.2f R=%3.2f\n", last_vel_eye_pan, last_vel_eye_vergence, left, right);
382
            JointInterface::store_incoming_speed(ID_EYES_LEFT_LR, left, timestamp);
383
            JointInterface::store_incoming_speed(ID_EYES_RIGHT_LR, right, timestamp);
384
            break;
385
        }
386
    }
387
}
388

    
389
void iCubJointInterface::set_joint_enable_state(int e, bool enable) {
390
    int icub_jointid = -1;
391

    
392
    switch(e){
393
        default:
394
            break;
395

    
396
    case(ID_NECK_PAN):
397
        icub_jointid = ICUB_ID_NECK_PAN;
398
        break;
399

    
400
    case(ID_NECK_TILT):
401
        icub_jointid = ICUB_ID_NECK_TILT;
402
        break;
403

    
404
    case(ID_NECK_ROLL):
405
        icub_jointid = ICUB_ID_NECK_ROLL;
406
        break;
407

    
408
    case(ID_EYES_BOTH_UD):
409
        icub_jointid = ICUB_ID_EYES_BOTH_UD;
410
        break;
411

    
412
    // icub handles eyes as pan angle + vergence...
413
    // -> hack: left eye enables pan and right eye enables vergence
414
    case(ID_EYES_LEFT_LR):
415
        icub_jointid = ICUB_ID_EYES_PAN;
416
        break;
417

    
418
    case(ID_EYES_RIGHT_LR):
419
        icub_jointid = ICUB_ID_EYES_VERGENCE;
420
        break;
421
    }
422

    
423
    if (icub_jointid != -1) {
424
        if (enable) {
425
            amp->enableAmp(icub_jointid);
426
            pid->enablePid(icub_jointid);
427
        } else {
428
            pid->disablePid(icub_jointid);
429
            amp->disableAmp(icub_jointid);
430
        }
431
    }
432
}
433

    
434
//! prepare and enable a joint
435
//! NOTE: this should also prefill the min/max positions for this joint
436
//! \param the enum id of a joint
437
void iCubJointInterface::enable_joint(int e){
438
    set_joint_enable_state(e, true);
439
}
440

    
441
//! shutdown and disable a joint
442
//! \param the enum id of a joint
443
void iCubJointInterface::disable_joint(int e){
444
    set_joint_enable_state(e, false);
445
}
446

    
447
void iCubJointInterface::store_min_max(IControlLimits *ilimits, int id, int e){
448
    double min, max;
449
    ilimits->getLimits(id, &min, &max);
450
    joint_min[e] = min;
451
    joint_max[e] = max;
452
}
453

    
454
//! initialise a joint (set up controller mode etc)
455
//! \param joint enum
456
void iCubJointInterface::init_joints(){
457
    store_min_max(ilimits, ICUB_ID_NECK_TILT, ID_NECK_TILT);
458
    store_min_max(ilimits, ICUB_ID_NECK_ROLL, ID_NECK_ROLL);
459
    store_min_max(ilimits, ICUB_ID_NECK_PAN, ID_NECK_PAN);
460
    store_min_max(ilimits, ICUB_ID_EYES_BOTH_UD, ID_EYES_BOTH_UD);
461

    
462
    //icub handles eyes differently, we have to set pan angle + vergence
463
    double pan_min, pan_max, vergence_min, vergence_max;
464
    ilimits->getLimits(ICUB_ID_EYES_PAN, &pan_min, &pan_max);
465
    ilimits->getLimits(ICUB_ID_EYES_VERGENCE, &vergence_min, &vergence_max);
466

    
467
    //this is not 100% correct, should be fixed:
468
    joint_min[ID_EYES_LEFT_LR] = pan_min; // - vergence_max/2;
469
    joint_max[ID_EYES_LEFT_LR] = pan_max; // - vergence_max/2;
470
    joint_min[ID_EYES_RIGHT_LR] = joint_min[ID_EYES_LEFT_LR];
471
    joint_max[ID_EYES_RIGHT_LR] = joint_max[ID_EYES_LEFT_LR];
472

    
473
    //eyelids:
474
    joint_min[ID_EYES_RIGHT_LID_UPPER] = -50; //24-30;
475
    joint_max[ID_EYES_RIGHT_LID_UPPER] = 50; //48-30;
476
    //lid_angle = joint_max[ID_EYES_RIGHT_LID_UPPER];
477

    
478
    //eyebrows:
479
    joint_min[ID_EYES_LEFT_BROW] = -50;
480
    joint_max[ID_EYES_LEFT_BROW] = 50;
481
    joint_min[ID_EYES_RIGHT_BROW] = joint_min[ID_EYES_LEFT_BROW];
482
    joint_max[ID_EYES_RIGHT_BROW] = joint_max[ID_EYES_LEFT_BROW];
483

    
484
    //mouth:
485
    joint_min[ID_LIP_CENTER_UPPER] = 5;
486
    joint_max[ID_LIP_CENTER_UPPER] = 50;
487
    joint_min[ID_LIP_CENTER_LOWER] = 5;
488
    joint_max[ID_LIP_CENTER_LOWER] = 50;
489
    joint_min[ID_LIP_LEFT_UPPER] = 5;
490
    joint_max[ID_LIP_LEFT_UPPER] = 50;
491
    joint_min[ID_LIP_LEFT_LOWER] = 5;
492
    joint_max[ID_LIP_LEFT_LOWER] = 50;
493
    joint_min[ID_LIP_RIGHT_UPPER] = 5;
494
    joint_max[ID_LIP_RIGHT_UPPER] = 50;
495
    joint_min[ID_LIP_RIGHT_LOWER] = 5;
496
    joint_max[ID_LIP_RIGHT_LOWER] = 50;
497

    
498

    
499
}