Statistics
| Branch: | Tag: | Revision:

humotion / examples / yarp_icub / src / icub_jointinterface.cpp @ 6491eeeb

History | View | Annotate | Download (19.689 KB)

1 6a2d467f Simon Schulz
/*
2
* This file is part of humotion
3
*
4
* Copyright(c) sschulz <AT> techfak.uni-bielefeld.de
5
* http://opensource.cit-ec.de/projects/humotion
6
*
7
* This file may be licensed under the terms of the
8
* GNU Lesser General Public License Version 3 (the ``LGPL''),
9
* or (at your option) any later version.
10
*
11
* Software distributed under the License is distributed
12
* on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13
* express or implied. See the LGPL for the specific language
14
* governing rights and limitations.
15
*
16
* You should have received a copy of the LGPL along with this
17
* program. If not, go to http://www.gnu.org/licenses/lgpl.html
18
* or write to the Free Software Foundation, Inc.,
19
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
*
21
* The development of this software was supported by the
22
* Excellence Cluster EXC 277 Cognitive Interaction Technology.
23
* The Excellence Cluster EXC 277 is a grant of the Deutsche
24
* Forschungsgemeinschaft (DFG) in the context of the German
25
* Excellence Initiative.
26
*/
27
28
#include "humotion_yarp_icub/icub_faceinterface.h"
29
#include "humotion_yarp_icub/icub_jointinterface.h"
30 0d0f5ca1 Simon Schulz
31 8c6c1163 Simon Schulz
#include <yarp/os/Property.h>
32 6a2d467f Simon Schulz
#include <string>
33 372eec67 Simon Schulz
34 35b3ca25 Simon Schulz
using std::cout;
35 372eec67 Simon Schulz
using std::cerr;
36
using std::string;
37 8c6c1163 Simon Schulz
38
//! constructor
39 372eec67 Simon Schulz
iCubJointInterface::iCubJointInterface(string _scope) : humotion::server::JointInterface() {
40 8c6c1163 Simon Schulz
    scope = _scope;
41 1a35abea Simon Schulz
42 35b3ca25 Simon Schulz
    // add mappings from icub ids to humotion ids
43
    init_id_map();
44
45
    // initialise the pd controller for the velocity and position mixer
46
    init_pv_mix_pid();
47 87b50988 Simon Schulz
48 35b3ca25 Simon Schulz
    // intantiate the face interface
49
    face_interface_ = new iCubFaceInterface(scope);
50 87b50988 Simon Schulz
51 35b3ca25 Simon Schulz
    // intantiate the polydriver
52 372eec67 Simon Schulz
    yarp::os::Property options;
53 8c6c1163 Simon Schulz
    options.put("device", "remote_controlboard");
54
    options.put("local", "/local/head");
55 35b3ca25 Simon Schulz
    options.put("remote", scope + "/head");
56
    yarp_polydriver_.open(options);
57
58
    // fetch yarp views:
59
    bool success = true;
60 6a2d467f Simon Schulz
    // success &= yarp_polydriver_.view(yarp_iencs_);
61
    // success &= yarp_polydriver_.view(yarp_ipos_);
62 35b3ca25 Simon Schulz
    success &= yarp_polydriver_.view(yarp_ivel_);
63
    success &= yarp_polydriver_.view(yarp_ilimits_);
64
    success &= yarp_polydriver_.view(yarp_pid_);
65
    success &= yarp_polydriver_.view(yarp_amp_);
66
67
    if (!success) {
68
        cout << "ERROR: failed to fetch one or more yarp views... exiting\n";
69 8c6c1163 Simon Schulz
        exit(EXIT_FAILURE);
70
    }
71
72
73 6a2d467f Simon Schulz
    // tell humotion about min/max joint values:
74 8c6c1163 Simon Schulz
    init_joints();
75
76 6a2d467f Simon Schulz
    // initialise joint controller
77 35b3ca25 Simon Schulz
    init_controller();
78
}
79
80
//! destructor
81 6a2d467f Simon Schulz
iCubJointInterface::~iCubJointInterface() {
82
    // stop velocity controller on exit
83 18e9b892 Simon Schulz
    yarp_ivel_->stop();
84 35b3ca25 Simon Schulz
}
85
86
//! init the controller that allows to write target angles or velocities
87
void iCubJointInterface::init_controller() {
88
    int number_of_joints;
89 8c6c1163 Simon Schulz
90 372eec67 Simon Schulz
    // use position controller, first fetch no of axes:
91
    yarp_ivel_->getAxes(&number_of_joints);
92 8c6c1163 Simon Schulz
93 372eec67 Simon Schulz
    // set ref acceleration to a value for all axes:
94
    yarp_commands_.resize(number_of_joints);
95 18e9b892 Simon Schulz
    yarp_commands_ = 1e6;
96 372eec67 Simon Schulz
    yarp_ivel_->setRefAccelerations(yarp_commands_.data());
97
    yarp_ivel_->setVelocityMode();
98 8c6c1163 Simon Schulz
}
99
100 35b3ca25 Simon Schulz
//! initialise icub joint id to humotion joint id mappings
101
void iCubJointInterface::init_id_map() {
102
        insert_icupid_to_humotionid_mapping(ICUB_ID_LIP_LEFT_UPPER,   ID_LIP_LEFT_UPPER);
103
        insert_icupid_to_humotionid_mapping(ICUB_ID_LIP_LEFT_LOWER,   ID_LIP_LEFT_LOWER);
104
        insert_icupid_to_humotionid_mapping(ICUB_ID_LIP_CENTER_UPPER, ID_LIP_CENTER_UPPER);
105
        insert_icupid_to_humotionid_mapping(ICUB_ID_LIP_CENTER_LOWER, ID_LIP_CENTER_LOWER);
106
        insert_icupid_to_humotionid_mapping(ICUB_ID_LIP_RIGHT_UPPER,  ID_LIP_RIGHT_UPPER);
107
        insert_icupid_to_humotionid_mapping(ICUB_ID_LIP_RIGHT_LOWER,  ID_LIP_RIGHT_LOWER);
108
        insert_icupid_to_humotionid_mapping(ICUB_ID_NECK_PAN,    ID_NECK_PAN);
109
        insert_icupid_to_humotionid_mapping(ICUB_ID_NECK_TILT,   ID_NECK_TILT);
110
        insert_icupid_to_humotionid_mapping(ICUB_ID_NECK_ROLL,   ID_NECK_ROLL);
111
        // FIXME: remove this hack tha repurposes LEFT/RIGHT eye pan from humotion as vergence/pan
112
        insert_icupid_to_humotionid_mapping(ICUB_ID_EYES_PAN,   ID_EYES_LEFT_LR);
113
        insert_icupid_to_humotionid_mapping(ICUB_ID_EYES_VERGENCE,   ID_EYES_RIGHT_LR);
114
        insert_icupid_to_humotionid_mapping(ICUB_ID_EYES_BOTH_UD,   ID_EYES_BOTH_UD);
115
        insert_icupid_to_humotionid_mapping(ICUB_ID_EYES_LEFT_LID_LOWER, ID_EYES_LEFT_LID_LOWER);
116
        insert_icupid_to_humotionid_mapping(ICUB_ID_EYES_LEFT_LID_UPPER, ID_EYES_LEFT_LID_UPPER);
117
        insert_icupid_to_humotionid_mapping(ICUB_ID_EYES_LEFT_BROW, ID_EYES_LEFT_BROW);
118
        insert_icupid_to_humotionid_mapping(ICUB_ID_EYES_RIGHT_LID_LOWER, ID_EYES_RIGHT_LID_LOWER);
119 6a2d467f Simon Schulz
        insert_icupid_to_humotionid_mapping(ICUB_ID_EYES_RIGHT_LID_UPPER, ID_EYES_RIGHT_LID_UPPER);
120 35b3ca25 Simon Schulz
        insert_icupid_to_humotionid_mapping(ICUB_ID_EYES_RIGHT_BROW, ID_EYES_RIGHT_BROW);
121
}
122 8c6c1163 Simon Schulz
123 35b3ca25 Simon Schulz
//! initialize the position and velocity mixer PD controller
124
void iCubJointInterface::init_pv_mix_pid() {
125
    // init control variables and last error variable for the internal
126
    // position and velocity mixer PD controller:
127
    pv_mix_pid_p_.resize(ICUB_JOINT_ID_ENUM_SIZE);
128
    pv_mix_pid_d_.resize(ICUB_JOINT_ID_ENUM_SIZE);
129
    pv_mix_last_error_.resize(ICUB_JOINT_ID_ENUM_SIZE);
130 8c6c1163 Simon Schulz
131 35b3ca25 Simon Schulz
    enum_id_bimap_t::const_iterator it;
132 6a2d467f Simon Schulz
    for (it = enum_id_bimap.begin(); it != enum_id_bimap.end(); ++it) {
133 35b3ca25 Simon Schulz
        int id = it->left;
134 07e68eb7 sschulz
        pv_mix_pid_p_[id] = 5.5;
135 35b3ca25 Simon Schulz
        pv_mix_pid_d_[id] = 0.3;
136
        pv_mix_last_error_[id] = 0.0;
137 8c6c1163 Simon Schulz
    }
138 35b3ca25 Simon Schulz
}
139 8c6c1163 Simon Schulz
140 35b3ca25 Simon Schulz
//! add mapping from icub joint ids to humotion ids
141
//! this might look strange at the first sight but we need to have a generic
142
//! way to acces joints from libhumotion. therefore the lib uses its enum with ID_* enum ids
143
//! to access the joints. now we need to define a mapping to map those to the icub motor ids.
144
void iCubJointInterface::insert_icupid_to_humotionid_mapping(int icubid, int humotionid) {
145
    enum_id_bimap.insert(enum_id_bimap_entry_t(icubid, humotionid));
146 8c6c1163 Simon Schulz
}
147
148
149 35b3ca25 Simon Schulz
150
151 6a2d467f Simon Schulz
void iCubJointInterface::run() {
152 35b3ca25 Simon Schulz
    float loop_duration_ms = 1000.0 / MAIN_LOOP_FREQUENCY;
153
    iCubDataReceiver *data_receiver = new iCubDataReceiver(loop_duration_ms, this);
154 8c6c1163 Simon Schulz
    data_receiver->start();
155
}
156
157 35b3ca25 Simon Schulz
//! stores the target position & velocity of a given joint
158 8c6c1163 Simon Schulz
//! \param enum id of joint
159
//! \param float value
160 6a2d467f Simon Schulz
void iCubJointInterface::publish_target(int humotion_id, float position, float velocity) {
161 35b3ca25 Simon Schulz
    // special handler for eye joints
162
    if ((humotion_id == JointInterface::ID_EYES_LEFT_LR) ||
163 6a2d467f Simon Schulz
        (humotion_id == JointInterface::ID_EYES_RIGHT_LR)) {
164 35b3ca25 Simon Schulz
        // the icub has a combined pan angle for both eyes, so seperate this:
165
        float target_position_left  = get_target_position(JointInterface::ID_EYES_LEFT_LR);
166
        float target_position_right = get_target_position(JointInterface::ID_EYES_RIGHT_LR);
167
        float target_velocity_left  = get_target_velocity(JointInterface::ID_EYES_LEFT_LR);
168
        float target_velocity_right = get_target_velocity(JointInterface::ID_EYES_RIGHT_LR);
169
170 6c028e11 Simon Schulz
171 35b3ca25 Simon Schulz
        // calculate target angles
172 6c028e11 Simon Schulz
        float target_position_pan      = (target_position_right + target_position_left) / 2;
173
        float target_position_vergence = (target_position_right - target_position_left);
174
175 07e68eb7 sschulz
        /*cout << "LR " << target_position_left << " " << target_position_right <<
176
                " PAN " << target_position_pan << " VERGENCE " << target_position_vergence << "\n";*/
177 35b3ca25 Simon Schulz
178
        // calculate target velocities
179
        // for now just use the same velocity for pan and vergence
180
        float target_velocity_pan = (target_velocity_left + target_velocity_right) / 2.0;
181 18e9b892 Simon Schulz
        float target_velocity_vergence = target_velocity_left - target_velocity_right;
182 35b3ca25 Simon Schulz
183
        store_icub_joint_target(ICUB_ID_EYES_PAN,
184
                                target_position_pan, target_velocity_pan);
185
        store_icub_joint_target(ICUB_ID_EYES_VERGENCE,
186 18e9b892 Simon Schulz
                                target_position_vergence, target_velocity_vergence);
187 6a2d467f Simon Schulz
    } else {
188 35b3ca25 Simon Schulz
        // convert to icub joint id
189
        int icub_id = convert_humotion_jointid_to_icub(humotion_id);
190
        // store target data
191
        store_icub_joint_target(icub_id, position, velocity);
192 8c6c1163 Simon Schulz
    }
193
}
194
195
196 35b3ca25 Simon Schulz
//! set the target data for a given icub joint
197 8c6c1163 Simon Schulz
//! \param id of joint
198
//! \param float value of position
199 35b3ca25 Simon Schulz
void iCubJointInterface::store_icub_joint_target(int icub_id, float position, float velocity) {
200 07e68eb7 sschulz
    //cout << "store_icub_joint_target(" << icub_id << ", " << position << ", ..)\n";
201 d3aa8ab3 Simon Schulz
202 6a2d467f Simon Schulz
    if ((icub_id == ICUB_ID_NECK_PAN) || (icub_id == ICUB_ID_EYES_VERGENCE)) {
203 6c028e11 Simon Schulz
        // icub uses an inverted neck pan specification
204
        position = -position;
205
        velocity = -velocity;
206
    }
207 d3aa8ab3 Simon Schulz
208
    // store values
209 35b3ca25 Simon Schulz
    target_angle_[icub_id] = position;
210
    target_velocity_[icub_id] = velocity;
211 8c6c1163 Simon Schulz
}
212
213
//! execute a move in velocity mode
214
//! \param id of joint
215
//! \param angle
216 35b3ca25 Simon Schulz
void iCubJointInterface::set_target_in_velocitymode(int icub_id) {
217
    // fetch humotion id from icub joint id
218 6c028e11 Simon Schulz
    int humotion_id = convert_icub_jointid_to_humotion(icub_id);
219 7adf90be Simon Schulz
220 35b3ca25 Simon Schulz
    // fetch the target velocity
221
    float target_velocity = target_velocity_[icub_id];
222 7adf90be Simon Schulz
223 18e9b892 Simon Schulz
    float vmax = 350.0;
224 35b3ca25 Simon Schulz
    if (target_velocity > vmax)  target_velocity = vmax;
225
    if (target_velocity < -vmax) target_velocity = -vmax;
226 497d9d24 Simon Schulz
227 6a2d467f Simon Schulz
    // execute:
228 18e9b892 Simon Schulz
    if ((icub_id != ICUB_ID_NECK_PAN)
229 708960ff Simon Schulz
        && (icub_id != ICUB_ID_EYES_BOTH_UD)
230
        && (icub_id != ICUB_ID_NECK_TILT)
231 18e9b892 Simon Schulz
        && (icub_id != ICUB_ID_EYES_PAN)
232
        && (icub_id != ICUB_ID_EYES_VERGENCE)
233 6a2d467f Simon Schulz
        // && (icub_id != ICUB_ID_NECK_TILT)
234
            ) {
235 35b3ca25 Simon Schulz
        // limit to some joints for debugging...
236
        return;
237 18e9b892 Simon Schulz
    }
238 8c6c1163 Simon Schulz
239 35b3ca25 Simon Schulz
    // we now add a pd control loop for velocity moves in order to handle position errors
240 6a2d467f Simon Schulz
    // FIXME: add position interpolation into future. this requires to enable the velocity
241
    //        broadcast in the torso and head ini file and fetch that velocity in the
242
    //        icub_data_receiver as well. TODO: check if the can bus has enough bandwidth for that
243 35b3ca25 Simon Schulz
244
    // first: fetch the timstamp of the last known position
245
    humotion::Timestamp data_ts = get_ts_position(humotion_id).get_last_timestamp();
246
247 18e9b892 Simon Schulz
    // fetch current position & velocity
248
    float current_position = get_ts_position(humotion_id).get_interpolated_value(data_ts);
249
    float current_velocity = get_ts_speed(humotion_id).get_interpolated_value(data_ts);
250
251
    // the icub has a combined eye pan actuator, therefore
252
    // we have to calculate pan angle and vergence based on the left and right target angles:
253
    if (icub_id == ICUB_ID_EYES_PAN) {
254
        // fetch timestamp
255
        data_ts = get_ts_position(ID_EYES_LEFT_LR).get_last_timestamp();
256
257
        // get the left and right positions
258
        float pos_left  = get_ts_position(ID_EYES_LEFT_LR).get_interpolated_value(data_ts);
259
        float pos_right = get_ts_position(ID_EYES_RIGHT_LR).get_interpolated_value(data_ts);
260
        current_position = (pos_left + pos_right) / 2.0;
261
262
        // same for velocities:
263
        float vel_left  = get_ts_speed(ID_EYES_LEFT_LR).get_interpolated_value(data_ts);
264
        float vel_right = get_ts_speed(ID_EYES_RIGHT_LR).get_interpolated_value(data_ts);
265
        current_velocity = (vel_left + vel_right) / 2.0;
266
267 6a2d467f Simon Schulz
    } else if (icub_id == ICUB_ID_EYES_VERGENCE) {
268 18e9b892 Simon Schulz
        // fetch timestamp
269
        data_ts = get_ts_position(ID_EYES_LEFT_LR).get_last_timestamp();
270
271
        // get the left and right positions
272
        float pos_left  = get_ts_position(ID_EYES_LEFT_LR).get_interpolated_value(data_ts);
273
        float pos_right = get_ts_position(ID_EYES_RIGHT_LR).get_interpolated_value(data_ts);
274
275
        current_position = pos_left - pos_right;
276
277
        // same for velocities:
278
        float vel_left  = get_ts_speed(ID_EYES_LEFT_LR).get_interpolated_value(data_ts);
279
        float vel_right = get_ts_speed(ID_EYES_RIGHT_LR).get_interpolated_value(data_ts);
280
        current_velocity = (vel_left - vel_right);
281
    }
282
283 35b3ca25 Simon Schulz
    // calculate position error:
284 18e9b892 Simon Schulz
    float position_error = target_angle_[icub_id] - current_position;
285 35b3ca25 Simon Schulz
286
    // calculate d term
287
    float error_d = (position_error - pv_mix_last_error_[icub_id]) / (framerate*1000.0);
288
    pv_mix_last_error_[icub_id] = position_error;
289
290
    // finally do a PD loop to get the target velocity
291
    float pv_mix_velocity = pv_mix_pid_p_[icub_id] * position_error
292
                            + pv_mix_pid_p_[icub_id]*error_d
293
                            + target_velocity;
294
295 18e9b892 Simon Schulz
296 6a2d467f Simon Schulz
    /*cout << "\n"
297 18e9b892 Simon Schulz
         << current_position << " "
298 d3aa8ab3 Simon Schulz
         << target_angle_[icub_id] << " "
299 18e9b892 Simon Schulz
         << current_velocity << " "
300 d3aa8ab3 Simon Schulz
         << pv_mix_velocity  << " "
301
         << target_velocity  << " "
302
         << position_error << " "
303 6a2d467f Simon Schulz
         << " PID" << icub_id << "\n";*/
304 35b3ca25 Simon Schulz
305
    // execute velocity move
306 18e9b892 Simon Schulz
    bool success;
307
    int count = 0;
308 6a2d467f Simon Schulz
    do {
309 18e9b892 Simon Schulz
        success = yarp_ivel_->velocityMove(icub_id, pv_mix_velocity);
310 6a2d467f Simon Schulz
        if (count++ >= 10) {
311 18e9b892 Simon Schulz
            cerr << "ERROR: failed to send velocity move command! gave up after 10 trials\n";
312
            yarp_ivel_->stop();
313
            exit(EXIT_FAILURE);
314
        }
315 6a2d467f Simon Schulz
    } while (!success);
316 8c6c1163 Simon Schulz
}
317
318
//! actually execute the scheduled motion commands
319 6a2d467f Simon Schulz
void iCubJointInterface::execute_motion() {
320 372eec67 Simon Schulz
    // set up neck and eye motion commands in velocitymode
321 6a2d467f Simon Schulz
    for (int i = ICUB_ID_NECK_TILT; i <= ICUB_ID_EYES_VERGENCE; i++) {
322 372eec67 Simon Schulz
        set_target_in_velocitymode(i);
323 8c6c1163 Simon Schulz
    }
324 0c8d22a5 sschulz
325 1f748ce7 Simon Schulz
    // eyelids: unfortuantely the icub has only 1dof for eyelids
326
    // therefore we can only use an opening value
327
    float opening_left  = target_angle_[ICUB_ID_EYES_LEFT_LID_UPPER]
328
                          - target_angle_[ICUB_ID_EYES_LEFT_LID_LOWER];
329
    float opening_right = target_angle_[ICUB_ID_EYES_RIGHT_LID_UPPER]
330
                          - target_angle_[ICUB_ID_EYES_RIGHT_LID_LOWER];
331
    float opening = (opening_left + opening_right) / 2.0;
332
    // send it to icub face if
333
    face_interface_->set_eyelid_angle(opening);
334 8c6c1163 Simon Schulz
335
    //eyebrows are set using a special command as well:
336 35b3ca25 Simon Schulz
    face_interface_->set_eyebrow_angle(ICUB_ID_EYES_LEFT_BROW, target_angle_);
337
    face_interface_->set_eyebrow_angle(ICUB_ID_EYES_RIGHT_BROW, target_angle_);
338 8c6c1163 Simon Schulz
339
    //mouth
340 35b3ca25 Simon Schulz
    face_interface_->set_mouth(target_angle_);
341 8c6c1163 Simon Schulz
342 6a2d467f Simon Schulz
    // store joint values which we do not handle on icub here:
343 35b3ca25 Simon Schulz
    double timestamp = humotion::Timestamp::now().to_seconds();
344 6a2d467f Simon Schulz
    JointInterface::store_incoming_position(ID_LIP_LEFT_UPPER,
345
                                            target_angle_[ICUB_ID_LIP_LEFT_UPPER], timestamp);
346
    JointInterface::store_incoming_position(ID_LIP_LEFT_LOWER,
347
                                            target_angle_[ICUB_ID_LIP_LEFT_LOWER], timestamp);
348
    JointInterface::store_incoming_position(ID_LIP_CENTER_UPPER,
349
                                            target_angle_[ICUB_ID_LIP_CENTER_UPPER], timestamp);
350
    JointInterface::store_incoming_position(ID_LIP_CENTER_LOWER,
351
                                            target_angle_[ICUB_ID_LIP_CENTER_LOWER], timestamp);
352
    JointInterface::store_incoming_position(ID_LIP_RIGHT_UPPER,
353
                                            target_angle_[ICUB_ID_LIP_RIGHT_UPPER], timestamp);
354
    JointInterface::store_incoming_position(ID_LIP_RIGHT_LOWER,
355
                                            target_angle_[ICUB_ID_LIP_RIGHT_LOWER], timestamp);
356 8c6c1163 Simon Schulz
}
357
358
359 372eec67 Simon Schulz
void iCubJointInterface::set_joint_enable_state(int humotion_id, bool enable) {
360
    int icub_jointid = convert_humotion_jointid_to_icub(humotion_id);
361
/*
362 7adf90be Simon Schulz
    switch(e){
363
        default:
364
            break;
365 8c6c1163 Simon Schulz

366 7adf90be Simon Schulz
    case(ID_NECK_PAN):
367
        icub_jointid = ICUB_ID_NECK_PAN;
368
        break;
369 8c6c1163 Simon Schulz

370 7adf90be Simon Schulz
    case(ID_NECK_TILT):
371
        icub_jointid = ICUB_ID_NECK_TILT;
372
        break;
373 8c6c1163 Simon Schulz

374 7adf90be Simon Schulz
    case(ID_NECK_ROLL):
375
        icub_jointid = ICUB_ID_NECK_ROLL;
376
        break;
377

378
    case(ID_EYES_BOTH_UD):
379
        icub_jointid = ICUB_ID_EYES_BOTH_UD;
380
        break;
381

382
    // icub handles eyes as pan angle + vergence...
383
    // -> hack: left eye enables pan and right eye enables vergence
384
    case(ID_EYES_LEFT_LR):
385
        icub_jointid = ICUB_ID_EYES_PAN;
386
        break;
387

388
    case(ID_EYES_RIGHT_LR):
389
        icub_jointid = ICUB_ID_EYES_VERGENCE;
390
        break;
391 8c6c1163 Simon Schulz
    }
392 372eec67 Simon Schulz
    */
393 8c6c1163 Simon Schulz
394 372eec67 Simon Schulz
    if ((icub_jointid != -1) && (icub_jointid <= ICUB_ID_EYES_VERGENCE)) {
395 7adf90be Simon Schulz
        if (enable) {
396 35b3ca25 Simon Schulz
            yarp_amp_->enableAmp(icub_jointid);
397
            yarp_pid_->enablePid(icub_jointid);
398 7adf90be Simon Schulz
        } else {
399 35b3ca25 Simon Schulz
            yarp_pid_->disablePid(icub_jointid);
400
            yarp_amp_->disableAmp(icub_jointid);
401 7adf90be Simon Schulz
        }
402
    }
403 8c6c1163 Simon Schulz
}
404
405
//! prepare and enable a joint
406
//! NOTE: this should also prefill the min/max positions for this joint
407
//! \param the enum id of a joint
408 6a2d467f Simon Schulz
void iCubJointInterface::enable_joint(int e) {
409 7adf90be Simon Schulz
    set_joint_enable_state(e, true);
410
}
411 8c6c1163 Simon Schulz
412 7adf90be Simon Schulz
//! shutdown and disable a joint
413
//! \param the enum id of a joint
414 6a2d467f Simon Schulz
void iCubJointInterface::disable_joint(int e)  {
415 7adf90be Simon Schulz
    set_joint_enable_state(e, false);
416 8c6c1163 Simon Schulz
}
417
418 6a2d467f Simon Schulz
void iCubJointInterface::store_min_max(yarp::dev::IControlLimits *ilimits, int icub_id) {
419 8c6c1163 Simon Schulz
    double min, max;
420 372eec67 Simon Schulz
    int humotion_id = convert_icub_jointid_to_humotion(icub_id);
421
422 0c8d22a5 sschulz
    if (!ilimits->getLimits(icub_id, &min, &max)) {
423
        cerr << "ERROR: failed to call getLimits for icub joint " << icub_id << "\n";
424
        exit(EXIT_FAILURE);
425
    }
426
427 372eec67 Simon Schulz
    joint_min[humotion_id] = min;
428
    joint_max[humotion_id] = max;
429 8c6c1163 Simon Schulz
}
430
431
//! initialise a joint (set up controller mode etc)
432 6a2d467f Simon Schulz
void iCubJointInterface::init_joints() {
433 372eec67 Simon Schulz
    store_min_max(yarp_ilimits_, ICUB_ID_NECK_TILT);
434
    store_min_max(yarp_ilimits_, ICUB_ID_NECK_ROLL);
435
    store_min_max(yarp_ilimits_, ICUB_ID_NECK_PAN);
436
    store_min_max(yarp_ilimits_, ICUB_ID_EYES_BOTH_UD);
437 8c6c1163 Simon Schulz
438 6a2d467f Simon Schulz
    // icub handles eyes differently, we have to set pan angle + vergence
439 8c6c1163 Simon Schulz
    double pan_min, pan_max, vergence_min, vergence_max;
440 35b3ca25 Simon Schulz
    yarp_ilimits_->getLimits(ICUB_ID_EYES_PAN, &pan_min, &pan_max);
441
    yarp_ilimits_->getLimits(ICUB_ID_EYES_VERGENCE, &vergence_min, &vergence_max);
442 8c6c1163 Simon Schulz
443 6a2d467f Simon Schulz
    // this is not 100% correct, should be fixed
444
    joint_min[ID_EYES_LEFT_LR] = pan_min;  // - vergence_max/2;
445
    joint_max[ID_EYES_LEFT_LR] = pan_max;  // - vergence_max/2;
446 8c6c1163 Simon Schulz
    joint_min[ID_EYES_RIGHT_LR] = joint_min[ID_EYES_LEFT_LR];
447
    joint_max[ID_EYES_RIGHT_LR] = joint_max[ID_EYES_LEFT_LR];
448
449 6a2d467f Simon Schulz
    // eyelids
450
    joint_min[ID_EYES_RIGHT_LID_UPPER] = -50;  // 24-30;
451
    joint_max[ID_EYES_RIGHT_LID_UPPER] = 50;   // 48-30;
452
    // lid_angle = joint_max[ID_EYES_RIGHT_LID_UPPER];
453 8c6c1163 Simon Schulz
454 6a2d467f Simon Schulz
    // eyebrows
455 8c6c1163 Simon Schulz
    joint_min[ID_EYES_LEFT_BROW] = -50;
456
    joint_max[ID_EYES_LEFT_BROW] = 50;
457
    joint_min[ID_EYES_RIGHT_BROW] = joint_min[ID_EYES_LEFT_BROW];
458
    joint_max[ID_EYES_RIGHT_BROW] = joint_max[ID_EYES_LEFT_BROW];
459
460 6a2d467f Simon Schulz
    // mouth
461 8c6c1163 Simon Schulz
    joint_min[ID_LIP_CENTER_UPPER] = 5;
462
    joint_max[ID_LIP_CENTER_UPPER] = 50;
463
    joint_min[ID_LIP_CENTER_LOWER] = 5;
464
    joint_max[ID_LIP_CENTER_LOWER] = 50;
465
    joint_min[ID_LIP_LEFT_UPPER] = 5;
466
    joint_max[ID_LIP_LEFT_UPPER] = 50;
467
    joint_min[ID_LIP_LEFT_LOWER] = 5;
468
    joint_max[ID_LIP_LEFT_LOWER] = 50;
469
    joint_min[ID_LIP_RIGHT_UPPER] = 5;
470
    joint_max[ID_LIP_RIGHT_UPPER] = 50;
471
    joint_min[ID_LIP_RIGHT_LOWER] = 5;
472
    joint_max[ID_LIP_RIGHT_LOWER] = 50;
473 35b3ca25 Simon Schulz
}
474
475
//! conversion table for humotion joint id to icub joint id
476
//! \param int value for humotion joint id from JointInterface::JOINT_ID_ENUM
477
//! \return int value of icub joint id
478 6a2d467f Simon Schulz
int iCubJointInterface::convert_humotion_jointid_to_icub(int humotion_id) {
479 35b3ca25 Simon Schulz
    enum_id_bimap_t::right_const_iterator it = enum_id_bimap.right.find(humotion_id);
480 6a2d467f Simon Schulz
    if (it == enum_id_bimap.right.end()) {
481
        // key does not exist
482
        cerr << "ERROR: invalid humotion joint id (" << humotion_id <<
483
                ") given. can not convert this. exiting\n";
484 35b3ca25 Simon Schulz
        exit(EXIT_FAILURE);
485
    }
486
    return it->second;
487
}
488 8c6c1163 Simon Schulz
489
490 35b3ca25 Simon Schulz
//! conversion table for icub joint id to humotion joint id
491
//! \param  int value of icub joint id
492
//! \return int value of humotion joint id from JointInterface::JOINT_ID_ENUM
493 6a2d467f Simon Schulz
int iCubJointInterface::convert_icub_jointid_to_humotion(int icub_id) {
494 35b3ca25 Simon Schulz
    enum_id_bimap_t::left_const_iterator it = enum_id_bimap.left.find(icub_id);
495 6a2d467f Simon Schulz
    if (it == enum_id_bimap.left.end()) {
496
        // key does not exist
497 35b3ca25 Simon Schulz
        cout << "ERROR: invalid icub joint id given. can not convert this. exiting\n";
498
        exit(EXIT_FAILURE);
499
    }
500
    return it->second;
501 8c6c1163 Simon Schulz
}