Statistics
| Branch: | Tag: | Revision:

humotion / examples / yarp_icub / src / icub_jointinterface.cpp @ 120ea7f8

History | View | Annotate | Download (20.955 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 14831938 sschulz
    // running in sim?
43
    if (scope == "/icubSim") {
44
        cout << "> icub simulation detected\n";
45
        running_in_simulation_ = true;
46
    } else {
47
        running_in_simulation_ = false;
48
    }
49
50
51 35b3ca25 Simon Schulz
    // add mappings from icub ids to humotion ids
52
    init_id_map();
53
54
    // initialise the pd controller for the velocity and position mixer
55
    init_pv_mix_pid();
56 87b50988 Simon Schulz
57 35b3ca25 Simon Schulz
    // intantiate the face interface
58
    face_interface_ = new iCubFaceInterface(scope);
59 87b50988 Simon Schulz
60 35b3ca25 Simon Schulz
    // intantiate the polydriver
61 372eec67 Simon Schulz
    yarp::os::Property options;
62 8c6c1163 Simon Schulz
    options.put("device", "remote_controlboard");
63
    options.put("local", "/local/head");
64 35b3ca25 Simon Schulz
    options.put("remote", scope + "/head");
65
    yarp_polydriver_.open(options);
66
67
    // fetch yarp views:
68
    bool success = true;
69 6a2d467f Simon Schulz
    // success &= yarp_polydriver_.view(yarp_iencs_);
70 14831938 sschulz
    success &= yarp_polydriver_.view(yarp_ipos_);
71 35b3ca25 Simon Schulz
    success &= yarp_polydriver_.view(yarp_ivel_);
72
    success &= yarp_polydriver_.view(yarp_ilimits_);
73
    success &= yarp_polydriver_.view(yarp_pid_);
74
    success &= yarp_polydriver_.view(yarp_amp_);
75
76
    if (!success) {
77
        cout << "ERROR: failed to fetch one or more yarp views... exiting\n";
78 8c6c1163 Simon Schulz
        exit(EXIT_FAILURE);
79
    }
80
81
82 6a2d467f Simon Schulz
    // tell humotion about min/max joint values:
83 8c6c1163 Simon Schulz
    init_joints();
84
85 6a2d467f Simon Schulz
    // initialise joint controller
86 35b3ca25 Simon Schulz
    init_controller();
87
}
88
89
//! destructor
90 6a2d467f Simon Schulz
iCubJointInterface::~iCubJointInterface() {
91 14831938 sschulz
    // stop controller on exit
92 18e9b892 Simon Schulz
    yarp_ivel_->stop();
93 14831938 sschulz
    yarp_ipos_->stop();
94 35b3ca25 Simon Schulz
}
95
96
//! init the controller that allows to write target angles or velocities
97
void iCubJointInterface::init_controller() {
98
    int number_of_joints;
99 8c6c1163 Simon Schulz
100 14831938 sschulz
    if (running_in_simulation_) {
101
        // running in simulation, use position control mode as velocity seems unsupported right now
102
        yarp_ipos_->getAxes(&number_of_joints);
103
104
        // prepare to set ref accel
105
        // yarp_commands_.resize(number_of_joints);
106
        // yarp_commands_ = 200000.0;
107
        // yarp_ipos_->setRefAccelerations(yarp_commands_.data());
108 8c6c1163 Simon Schulz
109 14831938 sschulz
        // enable position control
110
        yarp_ipos_->setPositionMode();
111
    } else {
112
        // use velocity controller, first fetch no of axes:
113
        yarp_ivel_->getAxes(&number_of_joints);
114
115
        // set ref acceleration to a value for all axes:
116
        yarp_commands_.resize(number_of_joints);
117
        yarp_commands_ = 1e6;
118
        yarp_ivel_->setRefAccelerations(yarp_commands_.data());
119
120
        // finally enable velocity control mode
121
        yarp_ivel_->setVelocityMode();
122
    }
123 8c6c1163 Simon Schulz
}
124
125 35b3ca25 Simon Schulz
//! initialise icub joint id to humotion joint id mappings
126
void iCubJointInterface::init_id_map() {
127
        insert_icupid_to_humotionid_mapping(ICUB_ID_LIP_LEFT_UPPER,   ID_LIP_LEFT_UPPER);
128
        insert_icupid_to_humotionid_mapping(ICUB_ID_LIP_LEFT_LOWER,   ID_LIP_LEFT_LOWER);
129
        insert_icupid_to_humotionid_mapping(ICUB_ID_LIP_CENTER_UPPER, ID_LIP_CENTER_UPPER);
130
        insert_icupid_to_humotionid_mapping(ICUB_ID_LIP_CENTER_LOWER, ID_LIP_CENTER_LOWER);
131
        insert_icupid_to_humotionid_mapping(ICUB_ID_LIP_RIGHT_UPPER,  ID_LIP_RIGHT_UPPER);
132
        insert_icupid_to_humotionid_mapping(ICUB_ID_LIP_RIGHT_LOWER,  ID_LIP_RIGHT_LOWER);
133
        insert_icupid_to_humotionid_mapping(ICUB_ID_NECK_PAN,    ID_NECK_PAN);
134
        insert_icupid_to_humotionid_mapping(ICUB_ID_NECK_TILT,   ID_NECK_TILT);
135
        insert_icupid_to_humotionid_mapping(ICUB_ID_NECK_ROLL,   ID_NECK_ROLL);
136 f311c844 sschulz
        //
137 35b3ca25 Simon Schulz
        insert_icupid_to_humotionid_mapping(ICUB_ID_EYES_PAN,   ID_EYES_LEFT_LR);
138
        insert_icupid_to_humotionid_mapping(ICUB_ID_EYES_VERGENCE,   ID_EYES_RIGHT_LR);
139
        insert_icupid_to_humotionid_mapping(ICUB_ID_EYES_BOTH_UD,   ID_EYES_BOTH_UD);
140
        insert_icupid_to_humotionid_mapping(ICUB_ID_EYES_LEFT_LID_LOWER, ID_EYES_LEFT_LID_LOWER);
141
        insert_icupid_to_humotionid_mapping(ICUB_ID_EYES_LEFT_LID_UPPER, ID_EYES_LEFT_LID_UPPER);
142
        insert_icupid_to_humotionid_mapping(ICUB_ID_EYES_LEFT_BROW, ID_EYES_LEFT_BROW);
143
        insert_icupid_to_humotionid_mapping(ICUB_ID_EYES_RIGHT_LID_LOWER, ID_EYES_RIGHT_LID_LOWER);
144 6a2d467f Simon Schulz
        insert_icupid_to_humotionid_mapping(ICUB_ID_EYES_RIGHT_LID_UPPER, ID_EYES_RIGHT_LID_UPPER);
145 35b3ca25 Simon Schulz
        insert_icupid_to_humotionid_mapping(ICUB_ID_EYES_RIGHT_BROW, ID_EYES_RIGHT_BROW);
146
}
147 8c6c1163 Simon Schulz
148 35b3ca25 Simon Schulz
//! initialize the position and velocity mixer PD controller
149
void iCubJointInterface::init_pv_mix_pid() {
150
    // init control variables and last error variable for the internal
151
    // position and velocity mixer PD controller:
152
    pv_mix_pid_p_.resize(ICUB_JOINT_ID_ENUM_SIZE);
153
    pv_mix_pid_d_.resize(ICUB_JOINT_ID_ENUM_SIZE);
154
    pv_mix_last_error_.resize(ICUB_JOINT_ID_ENUM_SIZE);
155 8c6c1163 Simon Schulz
156 35b3ca25 Simon Schulz
    enum_id_bimap_t::const_iterator it;
157 6a2d467f Simon Schulz
    for (it = enum_id_bimap.begin(); it != enum_id_bimap.end(); ++it) {
158 35b3ca25 Simon Schulz
        int id = it->left;
159 07e68eb7 sschulz
        pv_mix_pid_p_[id] = 5.5;
160 35b3ca25 Simon Schulz
        pv_mix_pid_d_[id] = 0.3;
161
        pv_mix_last_error_[id] = 0.0;
162 8c6c1163 Simon Schulz
    }
163 35b3ca25 Simon Schulz
}
164 8c6c1163 Simon Schulz
165 35b3ca25 Simon Schulz
//! add mapping from icub joint ids to humotion ids
166
//! this might look strange at the first sight but we need to have a generic
167
//! way to acces joints from libhumotion. therefore the lib uses its enum with ID_* enum ids
168
//! to access the joints. now we need to define a mapping to map those to the icub motor ids.
169
void iCubJointInterface::insert_icupid_to_humotionid_mapping(int icubid, int humotionid) {
170
    enum_id_bimap.insert(enum_id_bimap_entry_t(icubid, humotionid));
171 8c6c1163 Simon Schulz
}
172
173
174 35b3ca25 Simon Schulz
175
176 6a2d467f Simon Schulz
void iCubJointInterface::run() {
177 35b3ca25 Simon Schulz
    float loop_duration_ms = 1000.0 / MAIN_LOOP_FREQUENCY;
178
    iCubDataReceiver *data_receiver = new iCubDataReceiver(loop_duration_ms, this);
179 8c6c1163 Simon Schulz
    data_receiver->start();
180
}
181
182 35b3ca25 Simon Schulz
//! stores the target position & velocity of a given joint
183 8c6c1163 Simon Schulz
//! \param enum id of joint
184
//! \param float value
185 6a2d467f Simon Schulz
void iCubJointInterface::publish_target(int humotion_id, float position, float velocity) {
186 35b3ca25 Simon Schulz
    // special handler for eye joints
187
    if ((humotion_id == JointInterface::ID_EYES_LEFT_LR) ||
188 6a2d467f Simon Schulz
        (humotion_id == JointInterface::ID_EYES_RIGHT_LR)) {
189 35b3ca25 Simon Schulz
        // the icub has a combined pan angle for both eyes, so seperate this:
190
        float target_position_left  = get_target_position(JointInterface::ID_EYES_LEFT_LR);
191
        float target_position_right = get_target_position(JointInterface::ID_EYES_RIGHT_LR);
192
        float target_velocity_left  = get_target_velocity(JointInterface::ID_EYES_LEFT_LR);
193
        float target_velocity_right = get_target_velocity(JointInterface::ID_EYES_RIGHT_LR);
194
195 6c028e11 Simon Schulz
196 35b3ca25 Simon Schulz
        // calculate target angles
197 6c028e11 Simon Schulz
        float target_position_pan      = (target_position_right + target_position_left) / 2;
198
        float target_position_vergence = (target_position_right - target_position_left);
199
200 07e68eb7 sschulz
        /*cout << "LR " << target_position_left << " " << target_position_right <<
201
                " PAN " << target_position_pan << " VERGENCE " << target_position_vergence << "\n";*/
202 35b3ca25 Simon Schulz
203
        // calculate target velocities
204
        // for now just use the same velocity for pan and vergence
205
        float target_velocity_pan = (target_velocity_left + target_velocity_right) / 2.0;
206 18e9b892 Simon Schulz
        float target_velocity_vergence = target_velocity_left - target_velocity_right;
207 35b3ca25 Simon Schulz
208
        store_icub_joint_target(ICUB_ID_EYES_PAN,
209
                                target_position_pan, target_velocity_pan);
210
        store_icub_joint_target(ICUB_ID_EYES_VERGENCE,
211 18e9b892 Simon Schulz
                                target_position_vergence, target_velocity_vergence);
212 6a2d467f Simon Schulz
    } else {
213 35b3ca25 Simon Schulz
        // convert to icub joint id
214
        int icub_id = convert_humotion_jointid_to_icub(humotion_id);
215
        // store target data
216
        store_icub_joint_target(icub_id, position, velocity);
217 8c6c1163 Simon Schulz
    }
218
}
219
220
221 35b3ca25 Simon Schulz
//! set the target data for a given icub joint
222 8c6c1163 Simon Schulz
//! \param id of joint
223
//! \param float value of position
224 35b3ca25 Simon Schulz
void iCubJointInterface::store_icub_joint_target(int icub_id, float position, float velocity) {
225 25400c71 sschulz
    // cout << "store_icub_joint_target(" << icub_id << ", " << position << ", ..)\n";
226 d3aa8ab3 Simon Schulz
227 6a2d467f Simon Schulz
    if ((icub_id == ICUB_ID_NECK_PAN) || (icub_id == ICUB_ID_EYES_VERGENCE)) {
228 6c028e11 Simon Schulz
        // icub uses an inverted neck pan specification
229
        position = -position;
230
        velocity = -velocity;
231
    }
232 d3aa8ab3 Simon Schulz
233
    // store values
234 35b3ca25 Simon Schulz
    target_angle_[icub_id] = position;
235
    target_velocity_[icub_id] = velocity;
236 8c6c1163 Simon Schulz
}
237
238
//! execute a move in velocity mode
239
//! \param id of joint
240
//! \param angle
241 35b3ca25 Simon Schulz
void iCubJointInterface::set_target_in_velocitymode(int icub_id) {
242
    // fetch humotion id from icub joint id
243 6c028e11 Simon Schulz
    int humotion_id = convert_icub_jointid_to_humotion(icub_id);
244 7adf90be Simon Schulz
245 35b3ca25 Simon Schulz
    // fetch the target velocity
246
    float target_velocity = target_velocity_[icub_id];
247 7adf90be Simon Schulz
248 18e9b892 Simon Schulz
    float vmax = 350.0;
249 35b3ca25 Simon Schulz
    if (target_velocity > vmax)  target_velocity = vmax;
250
    if (target_velocity < -vmax) target_velocity = -vmax;
251 497d9d24 Simon Schulz
252 6a2d467f Simon Schulz
    // execute:
253 18e9b892 Simon Schulz
    if ((icub_id != ICUB_ID_NECK_PAN)
254 708960ff Simon Schulz
        && (icub_id != ICUB_ID_EYES_BOTH_UD)
255
        && (icub_id != ICUB_ID_NECK_TILT)
256 18e9b892 Simon Schulz
        && (icub_id != ICUB_ID_EYES_PAN)
257
        && (icub_id != ICUB_ID_EYES_VERGENCE)
258 6a2d467f Simon Schulz
        // && (icub_id != ICUB_ID_NECK_TILT)
259
            ) {
260 35b3ca25 Simon Schulz
        // limit to some joints for debugging...
261
        return;
262 18e9b892 Simon Schulz
    }
263 8c6c1163 Simon Schulz
264 35b3ca25 Simon Schulz
    // we now add a pd control loop for velocity moves in order to handle position errors
265 6a2d467f Simon Schulz
    // FIXME: add position interpolation into future. this requires to enable the velocity
266
    //        broadcast in the torso and head ini file and fetch that velocity in the
267
    //        icub_data_receiver as well. TODO: check if the can bus has enough bandwidth for that
268 35b3ca25 Simon Schulz
269
    // first: fetch the timstamp of the last known position
270
    humotion::Timestamp data_ts = get_ts_position(humotion_id).get_last_timestamp();
271
272 18e9b892 Simon Schulz
    // fetch current position & velocity
273
    float current_position = get_ts_position(humotion_id).get_interpolated_value(data_ts);
274
    float current_velocity = get_ts_speed(humotion_id).get_interpolated_value(data_ts);
275
276
    // the icub has a combined eye pan actuator, therefore
277
    // we have to calculate pan angle and vergence based on the left and right target angles:
278
    if (icub_id == ICUB_ID_EYES_PAN) {
279
        // fetch timestamp
280
        data_ts = get_ts_position(ID_EYES_LEFT_LR).get_last_timestamp();
281
282
        // get the left and right positions
283
        float pos_left  = get_ts_position(ID_EYES_LEFT_LR).get_interpolated_value(data_ts);
284
        float pos_right = get_ts_position(ID_EYES_RIGHT_LR).get_interpolated_value(data_ts);
285
        current_position = (pos_left + pos_right) / 2.0;
286
287
        // same for velocities:
288
        float vel_left  = get_ts_speed(ID_EYES_LEFT_LR).get_interpolated_value(data_ts);
289
        float vel_right = get_ts_speed(ID_EYES_RIGHT_LR).get_interpolated_value(data_ts);
290
        current_velocity = (vel_left + vel_right) / 2.0;
291
292 6a2d467f Simon Schulz
    } else if (icub_id == ICUB_ID_EYES_VERGENCE) {
293 18e9b892 Simon Schulz
        // fetch timestamp
294
        data_ts = get_ts_position(ID_EYES_LEFT_LR).get_last_timestamp();
295
296
        // get the left and right positions
297
        float pos_left  = get_ts_position(ID_EYES_LEFT_LR).get_interpolated_value(data_ts);
298
        float pos_right = get_ts_position(ID_EYES_RIGHT_LR).get_interpolated_value(data_ts);
299
300
        current_position = pos_left - pos_right;
301
302
        // same for velocities:
303
        float vel_left  = get_ts_speed(ID_EYES_LEFT_LR).get_interpolated_value(data_ts);
304
        float vel_right = get_ts_speed(ID_EYES_RIGHT_LR).get_interpolated_value(data_ts);
305
        current_velocity = (vel_left - vel_right);
306
    }
307
308 35b3ca25 Simon Schulz
    // calculate position error:
309 18e9b892 Simon Schulz
    float position_error = target_angle_[icub_id] - current_position;
310 35b3ca25 Simon Schulz
311
    // calculate d term
312
    float error_d = (position_error - pv_mix_last_error_[icub_id]) / (framerate*1000.0);
313
    pv_mix_last_error_[icub_id] = position_error;
314
315
    // finally do a PD loop to get the target velocity
316
    float pv_mix_velocity = pv_mix_pid_p_[icub_id] * position_error
317
                            + pv_mix_pid_p_[icub_id]*error_d
318
                            + target_velocity;
319
320 18e9b892 Simon Schulz
321 6a2d467f Simon Schulz
    /*cout << "\n"
322 18e9b892 Simon Schulz
         << current_position << " "
323 d3aa8ab3 Simon Schulz
         << target_angle_[icub_id] << " "
324 18e9b892 Simon Schulz
         << current_velocity << " "
325 d3aa8ab3 Simon Schulz
         << pv_mix_velocity  << " "
326
         << target_velocity  << " "
327
         << position_error << " "
328 6a2d467f Simon Schulz
         << " PID" << icub_id << "\n";*/
329 35b3ca25 Simon Schulz
330 14831938 sschulz
331
    if (icub_id > ICUB_ID_EYES_VERGENCE) {
332
        cerr << "> ERROR: set_target_positionmode(id=" << icub_id << ", ...) not supported\n";
333
        return;
334
    }
335
336
337 35b3ca25 Simon Schulz
    // execute velocity move
338 18e9b892 Simon Schulz
    bool success;
339
    int count = 0;
340 6a2d467f Simon Schulz
    do {
341 14831938 sschulz
        if (running_in_simulation_) {
342
            // running in sim, use position control
343
            success = yarp_ipos_->positionMove(icub_id, target_angle_[icub_id]);
344
        } else {
345
            // use smooth velocity control on real robot
346
            success = yarp_ivel_->velocityMove(icub_id, pv_mix_velocity);
347
        }
348
349 6a2d467f Simon Schulz
        if (count++ >= 10) {
350 14831938 sschulz
            cerr << "ERROR: failed to send motion command! gave up after 10 trials\n";
351 18e9b892 Simon Schulz
            yarp_ivel_->stop();
352 14831938 sschulz
            yarp_ipos_->stop();
353 18e9b892 Simon Schulz
            exit(EXIT_FAILURE);
354
        }
355 6a2d467f Simon Schulz
    } while (!success);
356 8c6c1163 Simon Schulz
}
357
358
//! actually execute the scheduled motion commands
359 6a2d467f Simon Schulz
void iCubJointInterface::execute_motion() {
360 372eec67 Simon Schulz
    // set up neck and eye motion commands in velocitymode
361 6a2d467f Simon Schulz
    for (int i = ICUB_ID_NECK_TILT; i <= ICUB_ID_EYES_VERGENCE; i++) {
362 372eec67 Simon Schulz
        set_target_in_velocitymode(i);
363 8c6c1163 Simon Schulz
    }
364 0c8d22a5 sschulz
365 1f748ce7 Simon Schulz
    // eyelids: unfortuantely the icub has only 1dof for eyelids
366
    // therefore we can only use an opening value
367
    float opening_left  = target_angle_[ICUB_ID_EYES_LEFT_LID_UPPER]
368
                          - target_angle_[ICUB_ID_EYES_LEFT_LID_LOWER];
369
    float opening_right = target_angle_[ICUB_ID_EYES_RIGHT_LID_UPPER]
370
                          - target_angle_[ICUB_ID_EYES_RIGHT_LID_LOWER];
371
    float opening = (opening_left + opening_right) / 2.0;
372
    // send it to icub face if
373
    face_interface_->set_eyelid_angle(opening);
374 8c6c1163 Simon Schulz
375 25400c71 sschulz
    // eyebrows are set using a special command as well:
376 35b3ca25 Simon Schulz
    face_interface_->set_eyebrow_angle(ICUB_ID_EYES_LEFT_BROW, target_angle_);
377
    face_interface_->set_eyebrow_angle(ICUB_ID_EYES_RIGHT_BROW, target_angle_);
378 8c6c1163 Simon Schulz
379 25400c71 sschulz
    // mouth
380 35b3ca25 Simon Schulz
    face_interface_->set_mouth(target_angle_);
381 8c6c1163 Simon Schulz
382 6a2d467f Simon Schulz
    // store joint values which we do not handle on icub here:
383 25400c71 sschulz
    humotion::Timestamp timestamp = humotion::Timestamp::now();
384
385 6a2d467f Simon Schulz
    JointInterface::store_incoming_position(ID_LIP_LEFT_UPPER,
386
                                            target_angle_[ICUB_ID_LIP_LEFT_UPPER], timestamp);
387
    JointInterface::store_incoming_position(ID_LIP_LEFT_LOWER,
388
                                            target_angle_[ICUB_ID_LIP_LEFT_LOWER], timestamp);
389
    JointInterface::store_incoming_position(ID_LIP_CENTER_UPPER,
390
                                            target_angle_[ICUB_ID_LIP_CENTER_UPPER], timestamp);
391
    JointInterface::store_incoming_position(ID_LIP_CENTER_LOWER,
392
                                            target_angle_[ICUB_ID_LIP_CENTER_LOWER], timestamp);
393
    JointInterface::store_incoming_position(ID_LIP_RIGHT_UPPER,
394
                                            target_angle_[ICUB_ID_LIP_RIGHT_UPPER], timestamp);
395
    JointInterface::store_incoming_position(ID_LIP_RIGHT_LOWER,
396
                                            target_angle_[ICUB_ID_LIP_RIGHT_LOWER], timestamp);
397 8c6c1163 Simon Schulz
}
398
399
400 372eec67 Simon Schulz
void iCubJointInterface::set_joint_enable_state(int humotion_id, bool enable) {
401
    int icub_jointid = convert_humotion_jointid_to_icub(humotion_id);
402
/*
403 7adf90be Simon Schulz
    switch(e){
404
        default:
405
            break;
406 8c6c1163 Simon Schulz

407 7adf90be Simon Schulz
    case(ID_NECK_PAN):
408
        icub_jointid = ICUB_ID_NECK_PAN;
409
        break;
410 8c6c1163 Simon Schulz

411 7adf90be Simon Schulz
    case(ID_NECK_TILT):
412
        icub_jointid = ICUB_ID_NECK_TILT;
413
        break;
414 8c6c1163 Simon Schulz

415 7adf90be Simon Schulz
    case(ID_NECK_ROLL):
416
        icub_jointid = ICUB_ID_NECK_ROLL;
417
        break;
418

419
    case(ID_EYES_BOTH_UD):
420
        icub_jointid = ICUB_ID_EYES_BOTH_UD;
421
        break;
422

423
    // icub handles eyes as pan angle + vergence...
424
    // -> hack: left eye enables pan and right eye enables vergence
425
    case(ID_EYES_LEFT_LR):
426
        icub_jointid = ICUB_ID_EYES_PAN;
427
        break;
428

429
    case(ID_EYES_RIGHT_LR):
430
        icub_jointid = ICUB_ID_EYES_VERGENCE;
431
        break;
432 8c6c1163 Simon Schulz
    }
433 372eec67 Simon Schulz
    */
434 8c6c1163 Simon Schulz
435 372eec67 Simon Schulz
    if ((icub_jointid != -1) && (icub_jointid <= ICUB_ID_EYES_VERGENCE)) {
436 14831938 sschulz
        if (!running_in_simulation_) {
437
            // only set amp status on real robot. simulation crashes during this call ... :-X
438
            if (enable) {
439
                yarp_amp_->enableAmp(icub_jointid);
440
                yarp_pid_->enablePid(icub_jointid);
441
            } else {
442
                yarp_pid_->disablePid(icub_jointid);
443
                yarp_amp_->disableAmp(icub_jointid);
444
            }
445 7adf90be Simon Schulz
        }
446
    }
447 8c6c1163 Simon Schulz
}
448
449
//! prepare and enable a joint
450
//! NOTE: this should also prefill the min/max positions for this joint
451
//! \param the enum id of a joint
452 6a2d467f Simon Schulz
void iCubJointInterface::enable_joint(int e) {
453 7adf90be Simon Schulz
    set_joint_enable_state(e, true);
454
}
455 8c6c1163 Simon Schulz
456 7adf90be Simon Schulz
//! shutdown and disable a joint
457
//! \param the enum id of a joint
458 6a2d467f Simon Schulz
void iCubJointInterface::disable_joint(int e)  {
459 7adf90be Simon Schulz
    set_joint_enable_state(e, false);
460 8c6c1163 Simon Schulz
}
461
462 6a2d467f Simon Schulz
void iCubJointInterface::store_min_max(yarp::dev::IControlLimits *ilimits, int icub_id) {
463 8c6c1163 Simon Schulz
    double min, max;
464 372eec67 Simon Schulz
    int humotion_id = convert_icub_jointid_to_humotion(icub_id);
465
466 0c8d22a5 sschulz
    if (!ilimits->getLimits(icub_id, &min, &max)) {
467
        cerr << "ERROR: failed to call getLimits for icub joint " << icub_id << "\n";
468
        exit(EXIT_FAILURE);
469
    }
470
471 372eec67 Simon Schulz
    joint_min[humotion_id] = min;
472
    joint_max[humotion_id] = max;
473 8c6c1163 Simon Schulz
}
474
475
//! initialise a joint (set up controller mode etc)
476 6a2d467f Simon Schulz
void iCubJointInterface::init_joints() {
477 372eec67 Simon Schulz
    store_min_max(yarp_ilimits_, ICUB_ID_NECK_TILT);
478
    store_min_max(yarp_ilimits_, ICUB_ID_NECK_ROLL);
479
    store_min_max(yarp_ilimits_, ICUB_ID_NECK_PAN);
480
    store_min_max(yarp_ilimits_, ICUB_ID_EYES_BOTH_UD);
481 8c6c1163 Simon Schulz
482 6a2d467f Simon Schulz
    // icub handles eyes differently, we have to set pan angle + vergence
483 8c6c1163 Simon Schulz
    double pan_min, pan_max, vergence_min, vergence_max;
484 35b3ca25 Simon Schulz
    yarp_ilimits_->getLimits(ICUB_ID_EYES_PAN, &pan_min, &pan_max);
485
    yarp_ilimits_->getLimits(ICUB_ID_EYES_VERGENCE, &vergence_min, &vergence_max);
486 8c6c1163 Simon Schulz
487 6a2d467f Simon Schulz
    // this is not 100% correct, should be fixed
488
    joint_min[ID_EYES_LEFT_LR] = pan_min;  // - vergence_max/2;
489
    joint_max[ID_EYES_LEFT_LR] = pan_max;  // - vergence_max/2;
490 8c6c1163 Simon Schulz
    joint_min[ID_EYES_RIGHT_LR] = joint_min[ID_EYES_LEFT_LR];
491
    joint_max[ID_EYES_RIGHT_LR] = joint_max[ID_EYES_LEFT_LR];
492
493 6a2d467f Simon Schulz
    // eyelids
494
    joint_min[ID_EYES_RIGHT_LID_UPPER] = -50;  // 24-30;
495
    joint_max[ID_EYES_RIGHT_LID_UPPER] = 50;   // 48-30;
496
    // lid_angle = joint_max[ID_EYES_RIGHT_LID_UPPER];
497 8c6c1163 Simon Schulz
498 6a2d467f Simon Schulz
    // eyebrows
499 8c6c1163 Simon Schulz
    joint_min[ID_EYES_LEFT_BROW] = -50;
500
    joint_max[ID_EYES_LEFT_BROW] = 50;
501
    joint_min[ID_EYES_RIGHT_BROW] = joint_min[ID_EYES_LEFT_BROW];
502
    joint_max[ID_EYES_RIGHT_BROW] = joint_max[ID_EYES_LEFT_BROW];
503
504 6a2d467f Simon Schulz
    // mouth
505 8c6c1163 Simon Schulz
    joint_min[ID_LIP_CENTER_UPPER] = 5;
506
    joint_max[ID_LIP_CENTER_UPPER] = 50;
507
    joint_min[ID_LIP_CENTER_LOWER] = 5;
508
    joint_max[ID_LIP_CENTER_LOWER] = 50;
509
    joint_min[ID_LIP_LEFT_UPPER] = 5;
510
    joint_max[ID_LIP_LEFT_UPPER] = 50;
511
    joint_min[ID_LIP_LEFT_LOWER] = 5;
512
    joint_max[ID_LIP_LEFT_LOWER] = 50;
513
    joint_min[ID_LIP_RIGHT_UPPER] = 5;
514
    joint_max[ID_LIP_RIGHT_UPPER] = 50;
515
    joint_min[ID_LIP_RIGHT_LOWER] = 5;
516
    joint_max[ID_LIP_RIGHT_LOWER] = 50;
517 35b3ca25 Simon Schulz
}
518
519
//! conversion table for humotion joint id to icub joint id
520
//! \param int value for humotion joint id from JointInterface::JOINT_ID_ENUM
521
//! \return int value of icub joint id
522 6a2d467f Simon Schulz
int iCubJointInterface::convert_humotion_jointid_to_icub(int humotion_id) {
523 35b3ca25 Simon Schulz
    enum_id_bimap_t::right_const_iterator it = enum_id_bimap.right.find(humotion_id);
524 6a2d467f Simon Schulz
    if (it == enum_id_bimap.right.end()) {
525
        // key does not exist
526
        cerr << "ERROR: invalid humotion joint id (" << humotion_id <<
527
                ") given. can not convert this. exiting\n";
528 35b3ca25 Simon Schulz
        exit(EXIT_FAILURE);
529
    }
530
    return it->second;
531
}
532 8c6c1163 Simon Schulz
533
534 35b3ca25 Simon Schulz
//! conversion table for icub joint id to humotion joint id
535
//! \param  int value of icub joint id
536
//! \return int value of humotion joint id from JointInterface::JOINT_ID_ENUM
537 6a2d467f Simon Schulz
int iCubJointInterface::convert_icub_jointid_to_humotion(int icub_id) {
538 35b3ca25 Simon Schulz
    enum_id_bimap_t::left_const_iterator it = enum_id_bimap.left.find(icub_id);
539 6a2d467f Simon Schulz
    if (it == enum_id_bimap.left.end()) {
540
        // key does not exist
541 35b3ca25 Simon Schulz
        cout << "ERROR: invalid icub joint id given. can not convert this. exiting\n";
542
        exit(EXIT_FAILURE);
543
    }
544
    return it->second;
545 8c6c1163 Simon Schulz
}