Revision 0d0f5ca1 examples/yarp_icub/src/icub_jointinterface.cpp

View differences:

examples/yarp_icub/src/icub_jointinterface.cpp
1 1
#include "icub_jointinterface.h"
2
#include "icub_faceinterface.h"
3

  
2 4
#include <yarp/os/Property.h>
3 5
using namespace yarp::dev;
4 6
using namespace yarp::sig;
......
22 24
//! constructor
23 25
iCubJointInterface::iCubJointInterface(string _scope) : humotion::server::JointInterface(){
24 26
    scope = _scope;
25

  
27
    face_interface = new iCubFaceInterface(scope);
26 28

  
27 29
    //add mapping from ids to enums:
28 30
    //this might look strange at the first sight but we need to have a generic
......
99 101
        ivel->setRefAccelerations(commands.data());
100 102
    }
101 103

  
102
    //attach to facial expressions:
103
    string emotion_scope = scope + "/face/raw/in";
104
    printf("> opening connection to %s\n", emotion_scope.c_str());
105

  
106
    for(int i=0; i<4; i++){
107
        //strange, if we use one output port only the first command is executed?! flushing issues?
108
        string emotion_port_out = "/emotionwriter" + to_string(i);
109
        if (!emotion_port[i].open(emotion_port_out.c_str())){
110
            printf("> ERROR: failed to open to %s\n",emotion_port_out.c_str());
111
            exit(EXIT_FAILURE);
112
        }
113
        if (!Network::connect(emotion_port_out.c_str(), emotion_scope.c_str())){
114
            printf("> ERROR: failed to connect emotion ports\n");
115
            exit(EXIT_FAILURE);
116
        }
117
    }
118 104
}
119 105

  
120 106
//! destructor
......
148 134
    return it->second;
149 135
}
150 136

  
151
//! special command to set eyelid angle
152
//! \param angle in degrees
153
void iCubJointInterface::set_eyelid_angle(double angle){
154
    if (emotion_port[0].getOutputCount()>0){
155
        //try to set the value based on the upper one
156
        //some guesses from the sim: S30 = 0° / S40 = 10°
157
        int opening = (25.0 + 0.8*angle);
158
        opening = min(48, max(24, opening));
159

  
160
        if (opening == lid_opening_previous){
161
            //no update necessary
162
            return;
163
        }
164

  
165
        lid_angle = angle;
166
        lid_opening_previous = opening;
167

  
168
        char buf[20];
169
        sprintf(buf, "S%2d", opening);
170

  
171
        //printf("> SETTING EYELID '%s' (%f -> %d)\n",buf,angle,opening);
172
        Bottle &cmd = emotion_port[0].prepare();
173
        cmd.clear();
174
        cmd.addString(buf);
175
        emotion_port[0].writeStrict();
176
    }else{
177
        printf("> ERROR: no icub emotion output\n");
178
        exit(EXIT_FAILURE);
179
    }
180
}
181

  
182
//! special command to set the eyebrow angle
183
//! \param id {0=left, 1=right)
184
//! \param angle in degrees
185
void iCubJointInterface::set_eyebrow_angle(int id){
186
    int port_id;
187
    if (id == ICUB_ID_EYES_LEFT_BROW){
188
        port_id = 1;
189
    }else{
190
        port_id = 2;
191
    }
192

  
193
    if (emotion_port[port_id].getOutputCount()>0){
194
        double angle = target_angle[id];
195
        int icub_val = 0;
196

  
197
        //swap rotation direction:
198
        if (id==ICUB_ID_EYES_LEFT_BROW) angle = -angle;
199

  
200
        //convert to icub representation
201
        if (angle < -20){
202
            icub_val = 1;
203
        }else if (angle<10){
204
            icub_val = 2;
205
        }else if (angle<20){
206
            icub_val = 4;
207
        }else{
208
            icub_val = 8;
209
        }
210

  
211
        //make sure to update only on new values:
212
        if (icub_val == target_angle_previous[id]){
213
                //no updata necessary
214
                return;
215
        }
216

  
217
        //store actual value:
218
        target_angle_previous[id] = icub_val;
219

  
220

  
221
        string cmd_s;
222
        if (id==ICUB_ID_EYES_LEFT_BROW){
223
            cmd_s = "L0" + to_string(icub_val);
224
        }else{
225
            cmd_s = "R0" + to_string(icub_val);
226
        }
227

  
228
        printf("> SETTING EYEBROW %d (%f -> %s)\n",id,angle,cmd_s.c_str());
229

  
230
        Bottle &cmd = emotion_port[port_id].prepare();
231
        cmd.clear();
232
        cmd.addString(cmd_s);
233
        emotion_port[port_id].writeStrict();
234
    }else{
235
        printf("> ERROR: no icub emotion output\n");
236
        exit(EXIT_FAILURE);
237
    }
238
}
239 137

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

  
......
248 146
void iCubJointInterface::publish_target_position(int e){
249 147
    //first: convert humotion enum to our enum:
250 148
    int id = convert_enum_to_motorid(e);
149

  
251 150
    if (id == -1){
252 151
        return; //we are not interested in that data, so we just return here
253 152
    }
......
273 172
//! \param id of joint
274 173
//! \param float value of position
275 174
void iCubJointInterface::store_joint(int id, float value){
276
    //printf("> set joint %d = %f\n",id,value);
175
    printf("> set joint %d = %f\n",id,value);
277 176
    target_angle[id] = value;
278
    //ipos->positionMove(id, value);
279 177
}
280 178

  
281 179
//! execute a move in position mode
......
323 221
    //execute:
324 222
    //ivel->velocityMove(id, speed);
325 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) ){
326
        if (id == ICUB_ID_NECK_PAN) speed = -speed;
224
        //if (id == ICUB_ID_NECK_PAN) speed = -speed;
327 225
        ivel->velocityMove(id, speed);
328 226
        printf("> VEL now=%3.2f target=%3.2f --> dist=%3.2f speed=%3.2f\n",target_angle_previous[id],value,distance,speed);
329 227
    }
......
350 248

  
351 249

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

  
355 253
    //eyebrows are set using a special command as well:
356
    set_eyebrow_angle(ICUB_ID_EYES_LEFT_BROW);
357
    set_eyebrow_angle(ICUB_ID_EYES_RIGHT_BROW);
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);
358 256

  
359 257
    //mouth
360
    set_mouth();
361

  
362

  
363
}
364

  
365
void iCubJointInterface::set_mouth(){
366
    //convert from 6DOF mouth displacement to icub leds:
367
    int led_value = 0;
368

  
369
    //fetch center opening:
370
    double center_opening = target_angle[ICUB_ID_LIP_CENTER_LOWER] - target_angle[ICUB_ID_LIP_CENTER_UPPER];
371
    bool mouth_open = (center_opening>15.0)?true:false;
372

  
373
    //side of mouth high or low?
374
    double center_avg = (target_angle[ICUB_ID_LIP_CENTER_LOWER] + target_angle[ICUB_ID_LIP_CENTER_UPPER])/2.0;
375
    double left_avg   = (target_angle[ICUB_ID_LIP_LEFT_LOWER] + target_angle[ICUB_ID_LIP_LEFT_UPPER])/2.0;
376
    double right_avg  = (target_angle[ICUB_ID_LIP_RIGHT_LOWER] + target_angle[ICUB_ID_LIP_RIGHT_UPPER])/2.0;
377

  
378
    //happy, neutral or sad?
379
    double diff_l = center_avg - left_avg;
380
    double diff_r = center_avg - right_avg;
381
    double diff   = (diff_l+diff_r)/2.0;
382

  
383
    if (diff > 2.0){
384
        if (mouth_open){
385
            led_value = 0x14;
386
        }else{
387
            if (diff > 2.6){
388
                led_value = 0x0A;
389
            }else{
390
                led_value = 0x0B;
391
            }
392
        }
393
    }else if (diff < -3.0){
394
        if (mouth_open){
395
            led_value = 0x06;
396
        }else{
397
            led_value = 0x18;
398
        }
399
    }else if (diff < -2.0){
400
        if (mouth_open){
401
            led_value = 0x04; //0x25;
402
        }else{
403
            led_value = 0x08;
404
        }
405
    }else{
406
        if (mouth_open){
407
            led_value = 0x16;
408
        }else{
409
            led_value = 0x08;
410
        }
411
    }
412

  
413

  
414
    if (led_value == previous_mouth_state){
415
        //no update necessary
416
        return;
417
    }
418

  
419
    previous_mouth_state = led_value;
420

  
421
    //convert to string:
422
    char buf[10];
423
    sprintf(buf, "M%02X",led_value);
424

  
425
    /*printf("> sending mouth '%s'\n",buf);
426
    printf("> mouth angles: %3.2f %3.2f %3.2f\n",target_angle[ICUB_ID_LIP_LEFT_UPPER],target_angle[ICUB_ID_LIP_CENTER_UPPER],target_angle[ICUB_ID_LIP_RIGHT_UPPER]);
427
    printf("  mouth         %3.2f %3.2f %3.2f\n",target_angle[ICUB_ID_LIP_LEFT_LOWER],target_angle[ICUB_ID_LIP_CENTER_LOWER],target_angle[ICUB_ID_LIP_RIGHT_LOWER]);
428
    printf("  mouth  open=%3.2f diff=%3.2f\n", center_opening, diff);*/
429

  
430
    //add mouth:
431
    Bottle &cmd = emotion_port[3].prepare();
432
    cmd.clear();
433
    cmd.addString(buf);
434
    emotion_port[3].writeStrict();
258
    face_interface->set_mouth(target_angle);
435 259

  
436 260

  
437 261
    //store joint values which we do not handle on icub here:
......
442 266
    JointInterface::store_incoming_position(ID_LIP_CENTER_LOWER, target_angle[ICUB_ID_LIP_CENTER_LOWER], timestamp);
443 267
    JointInterface::store_incoming_position(ID_LIP_RIGHT_UPPER,  target_angle[ICUB_ID_LIP_RIGHT_UPPER], timestamp);
444 268
    JointInterface::store_incoming_position(ID_LIP_RIGHT_LOWER,  target_angle[ICUB_ID_LIP_RIGHT_LOWER], timestamp);
445

  
446 269
}
447 270

  
448 271
double iCubJointInterface::get_timestamp_ms(){
......
463 286
            return;
464 287

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

  
469 292
        case(ICUB_ID_NECK_PAN):
470 293
            //PAN is inverted!
471
            JointInterface::store_incoming_position(ID_NECK_PAN, -value, timestamp);
294
            JointInterface::store_incoming_position(ID_NECK_PAN, value, timestamp);
472 295
            break;
473 296

  
474 297
        case(ICUB_ID_NECK_TILT):
......
523 346

  
524 347
        case(ICUB_ID_NECK_PAN):
525 348
            //PAN IS INVERTED
526
            JointInterface::store_incoming_speed(ID_NECK_PAN, -value, timestamp);
349
            JointInterface::store_incoming_speed(ID_NECK_PAN, value, timestamp);
527 350
            break;
528 351

  
529 352
        case(ICUB_ID_NECK_TILT):
......
650 473
    //eyelids:
651 474
    joint_min[ID_EYES_RIGHT_LID_UPPER] = -50; //24-30;
652 475
    joint_max[ID_EYES_RIGHT_LID_UPPER] = 50; //48-30;
653
    lid_angle = joint_max[ID_EYES_RIGHT_LID_UPPER];
476
    //lid_angle = joint_max[ID_EYES_RIGHT_LID_UPPER];
654 477

  
655 478
    //eyebrows:
656 479
    joint_min[ID_EYES_LEFT_BROW] = -50;

Also available in: Unified diff