Statistics
| Branch: | Tag: | Revision:

humotion / examples / yarp_icub / src / icub_jointinterface.cpp @ 79d4e516

History | View | Annotate | Download (20.952 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 6a2d467f Simon Schulz
void iCubJointInterface::run() {
174 35b3ca25 Simon Schulz
    float loop_duration_ms = 1000.0 / MAIN_LOOP_FREQUENCY;
175
    iCubDataReceiver *data_receiver = new iCubDataReceiver(loop_duration_ms, this);
176 8c6c1163 Simon Schulz
    data_receiver->start();
177
}
178
179 35b3ca25 Simon Schulz
//! stores the target position & velocity of a given joint
180 8c6c1163 Simon Schulz
//! \param enum id of joint
181
//! \param float value
182 6a2d467f Simon Schulz
void iCubJointInterface::publish_target(int humotion_id, float position, float velocity) {
183 35b3ca25 Simon Schulz
    // special handler for eye joints
184
    if ((humotion_id == JointInterface::ID_EYES_LEFT_LR) ||
185 6a2d467f Simon Schulz
        (humotion_id == JointInterface::ID_EYES_RIGHT_LR)) {
186 35b3ca25 Simon Schulz
        // the icub has a combined pan angle for both eyes, so seperate this:
187
        float target_position_left  = get_target_position(JointInterface::ID_EYES_LEFT_LR);
188
        float target_position_right = get_target_position(JointInterface::ID_EYES_RIGHT_LR);
189
        float target_velocity_left  = get_target_velocity(JointInterface::ID_EYES_LEFT_LR);
190
        float target_velocity_right = get_target_velocity(JointInterface::ID_EYES_RIGHT_LR);
191
192 6c028e11 Simon Schulz
193 35b3ca25 Simon Schulz
        // calculate target angles
194 6c028e11 Simon Schulz
        float target_position_pan      = (target_position_right + target_position_left) / 2;
195
        float target_position_vergence = (target_position_right - target_position_left);
196
197 07e68eb7 sschulz
        /*cout << "LR " << target_position_left << " " << target_position_right <<
198
                " PAN " << target_position_pan << " VERGENCE " << target_position_vergence << "\n";*/
199 35b3ca25 Simon Schulz
200
        // calculate target velocities
201
        // for now just use the same velocity for pan and vergence
202
        float target_velocity_pan = (target_velocity_left + target_velocity_right) / 2.0;
203 18e9b892 Simon Schulz
        float target_velocity_vergence = target_velocity_left - target_velocity_right;
204 35b3ca25 Simon Schulz
205
        store_icub_joint_target(ICUB_ID_EYES_PAN,
206
                                target_position_pan, target_velocity_pan);
207
        store_icub_joint_target(ICUB_ID_EYES_VERGENCE,
208 18e9b892 Simon Schulz
                                target_position_vergence, target_velocity_vergence);
209 6a2d467f Simon Schulz
    } else {
210 35b3ca25 Simon Schulz
        // convert to icub joint id
211
        int icub_id = convert_humotion_jointid_to_icub(humotion_id);
212
        // store target data
213
        store_icub_joint_target(icub_id, position, velocity);
214 8c6c1163 Simon Schulz
    }
215
}
216
217
218 35b3ca25 Simon Schulz
//! set the target data for a given icub joint
219 8c6c1163 Simon Schulz
//! \param id of joint
220
//! \param float value of position
221 35b3ca25 Simon Schulz
void iCubJointInterface::store_icub_joint_target(int icub_id, float position, float velocity) {
222 25400c71 sschulz
    // cout << "store_icub_joint_target(" << icub_id << ", " << position << ", ..)\n";
223 d3aa8ab3 Simon Schulz
224 6a2d467f Simon Schulz
    if ((icub_id == ICUB_ID_NECK_PAN) || (icub_id == ICUB_ID_EYES_VERGENCE)) {
225 6c028e11 Simon Schulz
        // icub uses an inverted neck pan specification
226
        position = -position;
227
        velocity = -velocity;
228
    }
229 d3aa8ab3 Simon Schulz
230
    // store values
231 35b3ca25 Simon Schulz
    target_angle_[icub_id] = position;
232
    target_velocity_[icub_id] = velocity;
233 8c6c1163 Simon Schulz
}
234
235
//! execute a move in velocity mode
236
//! \param id of joint
237
//! \param angle
238 35b3ca25 Simon Schulz
void iCubJointInterface::set_target_in_velocitymode(int icub_id) {
239
    // fetch humotion id from icub joint id
240 6c028e11 Simon Schulz
    int humotion_id = convert_icub_jointid_to_humotion(icub_id);
241 7adf90be Simon Schulz
242 35b3ca25 Simon Schulz
    // fetch the target velocity
243
    float target_velocity = target_velocity_[icub_id];
244 7adf90be Simon Schulz
245 18e9b892 Simon Schulz
    float vmax = 350.0;
246 35b3ca25 Simon Schulz
    if (target_velocity > vmax)  target_velocity = vmax;
247
    if (target_velocity < -vmax) target_velocity = -vmax;
248 497d9d24 Simon Schulz
249 6a2d467f Simon Schulz
    // execute:
250 18e9b892 Simon Schulz
    if ((icub_id != ICUB_ID_NECK_PAN)
251 708960ff Simon Schulz
        && (icub_id != ICUB_ID_EYES_BOTH_UD)
252
        && (icub_id != ICUB_ID_NECK_TILT)
253 18e9b892 Simon Schulz
        && (icub_id != ICUB_ID_EYES_PAN)
254
        && (icub_id != ICUB_ID_EYES_VERGENCE)
255 6a2d467f Simon Schulz
        // && (icub_id != ICUB_ID_NECK_TILT)
256
            ) {
257 35b3ca25 Simon Schulz
        // limit to some joints for debugging...
258
        return;
259 18e9b892 Simon Schulz
    }
260 8c6c1163 Simon Schulz
261 35b3ca25 Simon Schulz
    // we now add a pd control loop for velocity moves in order to handle position errors
262 6a2d467f Simon Schulz
    // FIXME: add position interpolation into future. this requires to enable the velocity
263
    //        broadcast in the torso and head ini file and fetch that velocity in the
264
    //        icub_data_receiver as well. TODO: check if the can bus has enough bandwidth for that
265 35b3ca25 Simon Schulz
266
    // first: fetch the timstamp of the last known position
267
    humotion::Timestamp data_ts = get_ts_position(humotion_id).get_last_timestamp();
268
269 18e9b892 Simon Schulz
    // fetch current position & velocity
270
    float current_position = get_ts_position(humotion_id).get_interpolated_value(data_ts);
271
    float current_velocity = get_ts_speed(humotion_id).get_interpolated_value(data_ts);
272
273
    // the icub has a combined eye pan actuator, therefore
274
    // we have to calculate pan angle and vergence based on the left and right target angles:
275
    if (icub_id == ICUB_ID_EYES_PAN) {
276
        // fetch timestamp
277
        data_ts = get_ts_position(ID_EYES_LEFT_LR).get_last_timestamp();
278
279
        // get the left and right positions
280
        float pos_left  = get_ts_position(ID_EYES_LEFT_LR).get_interpolated_value(data_ts);
281
        float pos_right = get_ts_position(ID_EYES_RIGHT_LR).get_interpolated_value(data_ts);
282
        current_position = (pos_left + pos_right) / 2.0;
283
284
        // same for velocities:
285
        float vel_left  = get_ts_speed(ID_EYES_LEFT_LR).get_interpolated_value(data_ts);
286
        float vel_right = get_ts_speed(ID_EYES_RIGHT_LR).get_interpolated_value(data_ts);
287
        current_velocity = (vel_left + vel_right) / 2.0;
288
289 6a2d467f Simon Schulz
    } else if (icub_id == ICUB_ID_EYES_VERGENCE) {
290 18e9b892 Simon Schulz
        // fetch timestamp
291
        data_ts = get_ts_position(ID_EYES_LEFT_LR).get_last_timestamp();
292
293
        // get the left and right positions
294
        float pos_left  = get_ts_position(ID_EYES_LEFT_LR).get_interpolated_value(data_ts);
295
        float pos_right = get_ts_position(ID_EYES_RIGHT_LR).get_interpolated_value(data_ts);
296
297
        current_position = pos_left - pos_right;
298
299
        // same for velocities:
300
        float vel_left  = get_ts_speed(ID_EYES_LEFT_LR).get_interpolated_value(data_ts);
301
        float vel_right = get_ts_speed(ID_EYES_RIGHT_LR).get_interpolated_value(data_ts);
302
        current_velocity = (vel_left - vel_right);
303
    }
304
305 35b3ca25 Simon Schulz
    // calculate position error:
306 18e9b892 Simon Schulz
    float position_error = target_angle_[icub_id] - current_position;
307 35b3ca25 Simon Schulz
308
    // calculate d term
309
    float error_d = (position_error - pv_mix_last_error_[icub_id]) / (framerate*1000.0);
310
    pv_mix_last_error_[icub_id] = position_error;
311
312
    // finally do a PD loop to get the target velocity
313
    float pv_mix_velocity = pv_mix_pid_p_[icub_id] * position_error
314
                            + pv_mix_pid_p_[icub_id]*error_d
315
                            + target_velocity;
316
317 18e9b892 Simon Schulz
318 6a2d467f Simon Schulz
    /*cout << "\n"
319 18e9b892 Simon Schulz
         << current_position << " "
320 d3aa8ab3 Simon Schulz
         << target_angle_[icub_id] << " "
321 18e9b892 Simon Schulz
         << current_velocity << " "
322 d3aa8ab3 Simon Schulz
         << pv_mix_velocity  << " "
323
         << target_velocity  << " "
324
         << position_error << " "
325 6a2d467f Simon Schulz
         << " PID" << icub_id << "\n";*/
326 35b3ca25 Simon Schulz
327 14831938 sschulz
328
    if (icub_id > ICUB_ID_EYES_VERGENCE) {
329
        cerr << "> ERROR: set_target_positionmode(id=" << icub_id << ", ...) not supported\n";
330
        return;
331
    }
332
333
334 35b3ca25 Simon Schulz
    // execute velocity move
335 18e9b892 Simon Schulz
    bool success;
336
    int count = 0;
337 6a2d467f Simon Schulz
    do {
338 14831938 sschulz
        if (running_in_simulation_) {
339
            // running in sim, use position control
340
            success = yarp_ipos_->positionMove(icub_id, target_angle_[icub_id]);
341
        } else {
342
            // use smooth velocity control on real robot
343
            success = yarp_ivel_->velocityMove(icub_id, pv_mix_velocity);
344
        }
345
346 6a2d467f Simon Schulz
        if (count++ >= 10) {
347 14831938 sschulz
            cerr << "ERROR: failed to send motion command! gave up after 10 trials\n";
348 18e9b892 Simon Schulz
            yarp_ivel_->stop();
349 14831938 sschulz
            yarp_ipos_->stop();
350 18e9b892 Simon Schulz
            exit(EXIT_FAILURE);
351
        }
352 6a2d467f Simon Schulz
    } while (!success);
353 8c6c1163 Simon Schulz
}
354
355
//! actually execute the scheduled motion commands
356 6a2d467f Simon Schulz
void iCubJointInterface::execute_motion() {
357 372eec67 Simon Schulz
    // set up neck and eye motion commands in velocitymode
358 6a2d467f Simon Schulz
    for (int i = ICUB_ID_NECK_TILT; i <= ICUB_ID_EYES_VERGENCE; i++) {
359 372eec67 Simon Schulz
        set_target_in_velocitymode(i);
360 8c6c1163 Simon Schulz
    }
361 0c8d22a5 sschulz
362 1f748ce7 Simon Schulz
    // eyelids: unfortuantely the icub has only 1dof for eyelids
363
    // therefore we can only use an opening value
364
    float opening_left  = target_angle_[ICUB_ID_EYES_LEFT_LID_UPPER]
365
                          - target_angle_[ICUB_ID_EYES_LEFT_LID_LOWER];
366
    float opening_right = target_angle_[ICUB_ID_EYES_RIGHT_LID_UPPER]
367
                          - target_angle_[ICUB_ID_EYES_RIGHT_LID_LOWER];
368
    float opening = (opening_left + opening_right) / 2.0;
369
    // send it to icub face if
370
    face_interface_->set_eyelid_angle(opening);
371 8c6c1163 Simon Schulz
372 25400c71 sschulz
    // eyebrows are set using a special command as well:
373 35b3ca25 Simon Schulz
    face_interface_->set_eyebrow_angle(ICUB_ID_EYES_LEFT_BROW, target_angle_);
374
    face_interface_->set_eyebrow_angle(ICUB_ID_EYES_RIGHT_BROW, target_angle_);
375 8c6c1163 Simon Schulz
376 25400c71 sschulz
    // mouth
377 35b3ca25 Simon Schulz
    face_interface_->set_mouth(target_angle_);
378 8c6c1163 Simon Schulz
379 6a2d467f Simon Schulz
    // store joint values which we do not handle on icub here:
380 25400c71 sschulz
    humotion::Timestamp timestamp = humotion::Timestamp::now();
381
382 6a2d467f Simon Schulz
    JointInterface::store_incoming_position(ID_LIP_LEFT_UPPER,
383
                                            target_angle_[ICUB_ID_LIP_LEFT_UPPER], timestamp);
384
    JointInterface::store_incoming_position(ID_LIP_LEFT_LOWER,
385
                                            target_angle_[ICUB_ID_LIP_LEFT_LOWER], timestamp);
386
    JointInterface::store_incoming_position(ID_LIP_CENTER_UPPER,
387
                                            target_angle_[ICUB_ID_LIP_CENTER_UPPER], timestamp);
388
    JointInterface::store_incoming_position(ID_LIP_CENTER_LOWER,
389
                                            target_angle_[ICUB_ID_LIP_CENTER_LOWER], timestamp);
390
    JointInterface::store_incoming_position(ID_LIP_RIGHT_UPPER,
391
                                            target_angle_[ICUB_ID_LIP_RIGHT_UPPER], timestamp);
392
    JointInterface::store_incoming_position(ID_LIP_RIGHT_LOWER,
393
                                            target_angle_[ICUB_ID_LIP_RIGHT_LOWER], timestamp);
394 8c6c1163 Simon Schulz
}
395
396
397 372eec67 Simon Schulz
void iCubJointInterface::set_joint_enable_state(int humotion_id, bool enable) {
398
    int icub_jointid = convert_humotion_jointid_to_icub(humotion_id);
399
/*
400 7adf90be Simon Schulz
    switch(e){
401
        default:
402
            break;
403 8c6c1163 Simon Schulz

404 7adf90be Simon Schulz
    case(ID_NECK_PAN):
405
        icub_jointid = ICUB_ID_NECK_PAN;
406
        break;
407 8c6c1163 Simon Schulz

408 7adf90be Simon Schulz
    case(ID_NECK_TILT):
409
        icub_jointid = ICUB_ID_NECK_TILT;
410
        break;
411 8c6c1163 Simon Schulz

412 7adf90be Simon Schulz
    case(ID_NECK_ROLL):
413
        icub_jointid = ICUB_ID_NECK_ROLL;
414
        break;
415

416
    case(ID_EYES_BOTH_UD):
417
        icub_jointid = ICUB_ID_EYES_BOTH_UD;
418
        break;
419

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

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