humotion / examples / yarp_icub / src / icub_jointinterface.cpp @ 6c028e11
History | View | Annotate | Download (19.762 KB)
| 1 | 8c6c1163 | Simon Schulz | #include "icub_jointinterface.h" |
|---|---|---|---|
| 2 | 0d0f5ca1 | Simon Schulz | #include "icub_faceinterface.h" |
| 3 | |||
| 4 | 8c6c1163 | Simon Schulz | #include <yarp/os/Property.h> |
| 5 | using namespace yarp::dev; |
||
| 6 | using namespace yarp::sig; |
||
| 7 | using namespace yarp::os; |
||
| 8 | using namespace std; |
||
| 9 | /*running:
|
||
| 10 | /media/local_data/sschulz/iros15/icub-nightly/share/iCub/contexts/simConfig:> iCub_SIM
|
||
| 11 | /media/local_data/sschulz/iros15/icub-nightly/share/iCub/contexts/simFaceExpressions:> ../../../../bin/simFaceExpressions
|
||
| 12 | yarp connect /face/eyelids /icubSim/face/eyelids
|
||
| 13 | yarp connect /face/image/out /icubSim/texture/face
|
||
| 14 | |||
| 15 | TEST: yarp write /writer /icubSim/face/raw/in
|
||
| 16 | |||
| 17 | http://wiki.icub.org/wiki/Motor_control
|
||
| 18 | */
|
||
| 19 | |||
| 20 | 7adf90be | Simon Schulz | #define POSITION_CONTROL 0 |
| 21 | 35b3ca25 | Simon Schulz | using std::cout;
|
| 22 | 8c6c1163 | Simon Schulz | |
| 23 | //! constructor
|
||
| 24 | iCubJointInterface::iCubJointInterface(string _scope) : humotion::server::JointInterface(){
|
||
| 25 | scope = _scope; |
||
| 26 | 1a35abea | Simon Schulz | |
| 27 | 35b3ca25 | Simon Schulz | // add mappings from icub ids to humotion ids
|
| 28 | init_id_map(); |
||
| 29 | |||
| 30 | // initialise the pd controller for the velocity and position mixer
|
||
| 31 | init_pv_mix_pid(); |
||
| 32 | 87b50988 | Simon Schulz | |
| 33 | 35b3ca25 | Simon Schulz | // intantiate the face interface
|
| 34 | face_interface_ = new iCubFaceInterface(scope);
|
||
| 35 | 87b50988 | Simon Schulz | |
| 36 | 35b3ca25 | Simon Schulz | // intantiate the polydriver
|
| 37 | 8c6c1163 | Simon Schulz | Property options; |
| 38 | options.put("device", "remote_controlboard"); |
||
| 39 | options.put("local", "/local/head"); |
||
| 40 | 35b3ca25 | Simon Schulz | options.put("remote", scope + "/head"); |
| 41 | yarp_polydriver_.open(options); |
||
| 42 | |||
| 43 | // fetch yarp views:
|
||
| 44 | bool success = true; |
||
| 45 | //success &= yarp_polydriver_.view(yarp_iencs_);
|
||
| 46 | success &= yarp_polydriver_.view(yarp_ipos_); |
||
| 47 | success &= yarp_polydriver_.view(yarp_ivel_); |
||
| 48 | success &= yarp_polydriver_.view(yarp_ilimits_); |
||
| 49 | success &= yarp_polydriver_.view(yarp_pid_); |
||
| 50 | success &= yarp_polydriver_.view(yarp_amp_); |
||
| 51 | |||
| 52 | if (!success) {
|
||
| 53 | cout << "ERROR: failed to fetch one or more yarp views... exiting\n";
|
||
| 54 | 8c6c1163 | Simon Schulz | exit(EXIT_FAILURE); |
| 55 | } |
||
| 56 | |||
| 57 | |||
| 58 | //tell humotion about min/max joint values:
|
||
| 59 | init_joints(); |
||
| 60 | |||
| 61 | 35b3ca25 | Simon Schulz | //initialise joint controller
|
| 62 | init_controller(); |
||
| 63 | } |
||
| 64 | |||
| 65 | //! destructor
|
||
| 66 | iCubJointInterface::~iCubJointInterface(){
|
||
| 67 | } |
||
| 68 | |||
| 69 | //! init the controller that allows to write target angles or velocities
|
||
| 70 | void iCubJointInterface::init_controller() {
|
||
| 71 | int number_of_joints;
|
||
| 72 | 8c6c1163 | Simon Schulz | |
| 73 | //set position mode:
|
||
| 74 | if (POSITION_CONTROL){
|
||
| 75 | 35b3ca25 | Simon Schulz | // use position controller, first fetch no of axes:
|
| 76 | yarp_ipos_->getAxes(&number_of_joints); |
||
| 77 | |||
| 78 | // set ref acceleration to a value for all axes:
|
||
| 79 | yarp_commands_.resize(number_of_joints); |
||
| 80 | yarp_commands_ = 200000.0; |
||
| 81 | yarp_ipos_->setRefAccelerations(yarp_commands_.data()); |
||
| 82 | yarp_ipos_->setPositionMode(); |
||
| 83 | 8c6c1163 | Simon Schulz | }else{
|
| 84 | 35b3ca25 | Simon Schulz | // use position controller, first fetch no of axes:
|
| 85 | yarp_ivel_->getAxes(&number_of_joints); |
||
| 86 | |||
| 87 | // set ref acceleration to a value for all axes:
|
||
| 88 | yarp_commands_.resize(number_of_joints); |
||
| 89 | yarp_commands_=300.0; |
||
| 90 | yarp_ivel_->setRefAccelerations(yarp_commands_.data()); |
||
| 91 | yarp_ivel_->setVelocityMode(); |
||
| 92 | 8c6c1163 | Simon Schulz | } |
| 93 | |||
| 94 | |||
| 95 | } |
||
| 96 | |||
| 97 | 35b3ca25 | Simon Schulz | //! initialise icub joint id to humotion joint id mappings
|
| 98 | void iCubJointInterface::init_id_map() {
|
||
| 99 | insert_icupid_to_humotionid_mapping(ICUB_ID_LIP_LEFT_UPPER, ID_LIP_LEFT_UPPER); |
||
| 100 | insert_icupid_to_humotionid_mapping(ICUB_ID_LIP_LEFT_LOWER, ID_LIP_LEFT_LOWER); |
||
| 101 | insert_icupid_to_humotionid_mapping(ICUB_ID_LIP_CENTER_UPPER, ID_LIP_CENTER_UPPER); |
||
| 102 | insert_icupid_to_humotionid_mapping(ICUB_ID_LIP_CENTER_LOWER, ID_LIP_CENTER_LOWER); |
||
| 103 | insert_icupid_to_humotionid_mapping(ICUB_ID_LIP_RIGHT_UPPER, ID_LIP_RIGHT_UPPER); |
||
| 104 | insert_icupid_to_humotionid_mapping(ICUB_ID_LIP_RIGHT_LOWER, ID_LIP_RIGHT_LOWER); |
||
| 105 | insert_icupid_to_humotionid_mapping(ICUB_ID_NECK_PAN, ID_NECK_PAN); |
||
| 106 | insert_icupid_to_humotionid_mapping(ICUB_ID_NECK_TILT, ID_NECK_TILT); |
||
| 107 | insert_icupid_to_humotionid_mapping(ICUB_ID_NECK_ROLL, ID_NECK_ROLL); |
||
| 108 | // FIXME: remove this hack tha repurposes LEFT/RIGHT eye pan from humotion as vergence/pan
|
||
| 109 | insert_icupid_to_humotionid_mapping(ICUB_ID_EYES_PAN, ID_EYES_LEFT_LR); |
||
| 110 | insert_icupid_to_humotionid_mapping(ICUB_ID_EYES_VERGENCE, ID_EYES_RIGHT_LR); |
||
| 111 | insert_icupid_to_humotionid_mapping(ICUB_ID_EYES_BOTH_UD, ID_EYES_BOTH_UD); |
||
| 112 | insert_icupid_to_humotionid_mapping(ICUB_ID_EYES_LEFT_LID_LOWER, ID_EYES_LEFT_LID_LOWER); |
||
| 113 | insert_icupid_to_humotionid_mapping(ICUB_ID_EYES_LEFT_LID_UPPER, ID_EYES_LEFT_LID_UPPER); |
||
| 114 | insert_icupid_to_humotionid_mapping(ICUB_ID_EYES_LEFT_BROW, ID_EYES_LEFT_BROW); |
||
| 115 | insert_icupid_to_humotionid_mapping(ICUB_ID_EYES_RIGHT_LID_LOWER, ID_EYES_RIGHT_LID_LOWER); |
||
| 116 | insert_icupid_to_humotionid_mapping(ICUB_ID_EYES_RIGHT_LID_UPPER,ID_EYES_RIGHT_LID_UPPER); |
||
| 117 | insert_icupid_to_humotionid_mapping(ICUB_ID_EYES_RIGHT_BROW, ID_EYES_RIGHT_BROW); |
||
| 118 | } |
||
| 119 | 8c6c1163 | Simon Schulz | |
| 120 | 35b3ca25 | Simon Schulz | //! initialize the position and velocity mixer PD controller
|
| 121 | void iCubJointInterface::init_pv_mix_pid() {
|
||
| 122 | // init control variables and last error variable for the internal
|
||
| 123 | // position and velocity mixer PD controller:
|
||
| 124 | pv_mix_pid_p_.resize(ICUB_JOINT_ID_ENUM_SIZE); |
||
| 125 | pv_mix_pid_d_.resize(ICUB_JOINT_ID_ENUM_SIZE); |
||
| 126 | pv_mix_last_error_.resize(ICUB_JOINT_ID_ENUM_SIZE); |
||
| 127 | 8c6c1163 | Simon Schulz | |
| 128 | 35b3ca25 | Simon Schulz | enum_id_bimap_t::const_iterator it; |
| 129 | for(it = enum_id_bimap.begin(); it != enum_id_bimap.end(); ++it) {
|
||
| 130 | int id = it->left;
|
||
| 131 | pv_mix_pid_p_[id] = 4.5; |
||
| 132 | pv_mix_pid_d_[id] = 0.3; |
||
| 133 | pv_mix_last_error_[id] = 0.0; |
||
| 134 | 8c6c1163 | Simon Schulz | } |
| 135 | |||
| 136 | 35b3ca25 | Simon Schulz | } |
| 137 | 8c6c1163 | Simon Schulz | |
| 138 | 35b3ca25 | Simon Schulz | //! add mapping from icub joint ids to humotion ids
|
| 139 | //! this might look strange at the first sight but we need to have a generic
|
||
| 140 | //! way to acces joints from libhumotion. therefore the lib uses its enum with ID_* enum ids
|
||
| 141 | //! to access the joints. now we need to define a mapping to map those to the icub motor ids.
|
||
| 142 | void iCubJointInterface::insert_icupid_to_humotionid_mapping(int icubid, int humotionid) { |
||
| 143 | enum_id_bimap.insert(enum_id_bimap_entry_t(icubid, humotionid)); |
||
| 144 | 8c6c1163 | Simon Schulz | } |
| 145 | |||
| 146 | |||
| 147 | 35b3ca25 | Simon Schulz | |
| 148 | |||
| 149 | 8c6c1163 | Simon Schulz | void iCubJointInterface::run(){
|
| 150 | 35b3ca25 | Simon Schulz | float loop_duration_ms = 1000.0 / MAIN_LOOP_FREQUENCY; |
| 151 | iCubDataReceiver *data_receiver = new iCubDataReceiver(loop_duration_ms, this); |
||
| 152 | 8c6c1163 | Simon Schulz | data_receiver->start(); |
| 153 | } |
||
| 154 | |||
| 155 | 35b3ca25 | Simon Schulz | //! stores the target position & velocity of a given joint
|
| 156 | 8c6c1163 | Simon Schulz | //! \param enum id of joint
|
| 157 | //! \param float value
|
||
| 158 | 35b3ca25 | Simon Schulz | void iCubJointInterface::publish_target(int humotion_id, float position, float velocity){ |
| 159 | // special handler for eye joints
|
||
| 160 | if ((humotion_id == JointInterface::ID_EYES_LEFT_LR) ||
|
||
| 161 | (humotion_id == JointInterface::ID_EYES_RIGHT_LR)){
|
||
| 162 | // the icub has a combined pan angle for both eyes, so seperate this:
|
||
| 163 | float target_position_left = get_target_position(JointInterface::ID_EYES_LEFT_LR);
|
||
| 164 | float target_position_right = get_target_position(JointInterface::ID_EYES_RIGHT_LR);
|
||
| 165 | float target_velocity_left = get_target_velocity(JointInterface::ID_EYES_LEFT_LR);
|
||
| 166 | float target_velocity_right = get_target_velocity(JointInterface::ID_EYES_RIGHT_LR);
|
||
| 167 | |||
| 168 | 6c028e11 | Simon Schulz | |
| 169 | 35b3ca25 | Simon Schulz | // calculate target angles
|
| 170 | 6c028e11 | Simon Schulz | float target_position_pan = (target_position_right + target_position_left) / 2; |
| 171 | float target_position_vergence = (target_position_right - target_position_left);
|
||
| 172 | |||
| 173 | cout << "LR " << target_position_left << " " << target_position_right << |
||
| 174 | " PAN " << target_position_pan << " VERGENCE " << target_position_vergence << "\n"; |
||
| 175 | 35b3ca25 | Simon Schulz | |
| 176 | // calculate target velocities
|
||
| 177 | // for now just use the same velocity for pan and vergence
|
||
| 178 | float target_velocity_pan = (target_velocity_left + target_velocity_right) / 2.0; |
||
| 179 | float target_velocity_tilt = target_velocity_pan;
|
||
| 180 | |||
| 181 | store_icub_joint_target(ICUB_ID_EYES_PAN, |
||
| 182 | target_position_pan, target_velocity_pan); |
||
| 183 | store_icub_joint_target(ICUB_ID_EYES_VERGENCE, |
||
| 184 | target_position_vergence, target_velocity_tilt); |
||
| 185 | 8c6c1163 | Simon Schulz | }else{
|
| 186 | 35b3ca25 | Simon Schulz | // convert to icub joint id
|
| 187 | int icub_id = convert_humotion_jointid_to_icub(humotion_id);
|
||
| 188 | // store target data
|
||
| 189 | store_icub_joint_target(icub_id, position, velocity); |
||
| 190 | 8c6c1163 | Simon Schulz | } |
| 191 | } |
||
| 192 | |||
| 193 | |||
| 194 | 35b3ca25 | Simon Schulz | //! set the target data for a given icub joint
|
| 195 | 8c6c1163 | Simon Schulz | //! \param id of joint
|
| 196 | //! \param float value of position
|
||
| 197 | 35b3ca25 | Simon Schulz | void iCubJointInterface::store_icub_joint_target(int icub_id, float position, float velocity) { |
| 198 | printf("> set icub joint %d -> p = %f, v = %f\n",icub_id,position,velocity);
|
||
| 199 | 6c028e11 | Simon Schulz | if (icub_id == ICUB_ID_NECK_PAN) {
|
| 200 | // icub uses an inverted neck pan specification
|
||
| 201 | position = -position; |
||
| 202 | velocity = -velocity; |
||
| 203 | } |
||
| 204 | 35b3ca25 | Simon Schulz | target_angle_[icub_id] = position; |
| 205 | target_velocity_[icub_id] = velocity; |
||
| 206 | 8c6c1163 | Simon Schulz | } |
| 207 | |||
| 208 | //! execute a move in position mode
|
||
| 209 | //! \param id of joint
|
||
| 210 | //! \param angle
|
||
| 211 | 35b3ca25 | Simon Schulz | void iCubJointInterface::set_target_in_positionmode(int id) { |
| 212 | assert(POSITION_CONTROL); |
||
| 213 | assert(id<=ICUB_ID_EYES_VERGENCE); |
||
| 214 | 87b50988 | Simon Schulz | |
| 215 | 35b3ca25 | Simon Schulz | // fetch the current target position
|
| 216 | float target = target_angle_[id];
|
||
| 217 | 8c6c1163 | Simon Schulz | |
| 218 | 7adf90be | Simon Schulz | // execute motion as position control cmd
|
| 219 | 35b3ca25 | Simon Schulz | yarp_ipos_->positionMove(id, target); |
| 220 | 8c6c1163 | Simon Schulz | } |
| 221 | |||
| 222 | //! execute a move in velocity mode
|
||
| 223 | //! \param id of joint
|
||
| 224 | //! \param angle
|
||
| 225 | 35b3ca25 | Simon Schulz | void iCubJointInterface::set_target_in_velocitymode(int icub_id) { |
| 226 | assert(!POSITION_CONTROL); |
||
| 227 | 7adf90be | Simon Schulz | |
| 228 | 35b3ca25 | Simon Schulz | // fetch humotion id from icub joint id
|
| 229 | 6c028e11 | Simon Schulz | int humotion_id = convert_icub_jointid_to_humotion(icub_id);
|
| 230 | 7adf90be | Simon Schulz | |
| 231 | 35b3ca25 | Simon Schulz | // fetch the target velocity
|
| 232 | float target_velocity = target_velocity_[icub_id];
|
||
| 233 | 7adf90be | Simon Schulz | |
| 234 | 35b3ca25 | Simon Schulz | float vmax = 150.0; |
| 235 | if (target_velocity > vmax) target_velocity = vmax;
|
||
| 236 | if (target_velocity < -vmax) target_velocity = -vmax;
|
||
| 237 | 497d9d24 | Simon Schulz | |
| 238 | 8c6c1163 | Simon Schulz | //execute:
|
| 239 | 7adf90be | Simon Schulz | //ivel->velocityMove(id, speed);
|
| 240 | 6c028e11 | Simon Schulz | /*if ((icub_id != ICUB_ID_NECK_PAN) &&
|
| 241 | 35b3ca25 | Simon Schulz | (icub_id != ICUB_ID_EYES_BOTH_UD) &&
|
| 242 | (icub_id != ICUB_ID_NECK_TILT) &&
|
||
| 243 | (icub_id != ICUB_ID_EYES_BOTH_UD) &&
|
||
| 244 | (icub_id != ICUB_ID_NECK_TILT)) {
|
||
| 245 | // limit to some joints for debugging...
|
||
| 246 | return;
|
||
| 247 | 6c028e11 | Simon Schulz | }*/
|
| 248 | 8c6c1163 | Simon Schulz | |
| 249 | 35b3ca25 | Simon Schulz | // we now add a pd control loop for velocity moves in order to handle position errors
|
| 250 | // TODO: add position interpolation into future. this requires to enable the velocity
|
||
| 251 | // broadcast in the torso and head ini file and fetch that velocity in the
|
||
| 252 | // icub_data_receiver as well. TODO: check if the can bus has enough bandwidth for that...
|
||
| 253 | |||
| 254 | // first: fetch the timstamp of the last known position
|
||
| 255 | humotion::Timestamp data_ts = get_ts_position(humotion_id).get_last_timestamp(); |
||
| 256 | |||
| 257 | // calculate position error:
|
||
| 258 | float position_error = target_angle_[icub_id] - get_ts_position(humotion_id).get_interpolated_value(data_ts);
|
||
| 259 | |||
| 260 | // calculate d term
|
||
| 261 | float error_d = (position_error - pv_mix_last_error_[icub_id]) / (framerate*1000.0); |
||
| 262 | pv_mix_last_error_[icub_id] = position_error; |
||
| 263 | |||
| 264 | // finally do a PD loop to get the target velocity
|
||
| 265 | float pv_mix_velocity = pv_mix_pid_p_[icub_id] * position_error
|
||
| 266 | + pv_mix_pid_p_[icub_id]*error_d |
||
| 267 | + target_velocity; |
||
| 268 | |||
| 269 | printf("%f %f %f %f %f %f PID%d\n",
|
||
| 270 | get_ts_position(humotion_id).get_interpolated_value(data_ts), |
||
| 271 | target_angle_[icub_id], |
||
| 272 | 123.4, //NOT USED ANYMORE//get_ts_speed(humotion_id).get_interpolated_value(data_ts), |
||
| 273 | pv_mix_velocity, |
||
| 274 | target_velocity, |
||
| 275 | position_error, |
||
| 276 | icub_id |
||
| 277 | ); |
||
| 278 | |||
| 279 | // execute velocity move
|
||
| 280 | yarp_ivel_->velocityMove(icub_id, pv_mix_velocity); |
||
| 281 | 8c6c1163 | Simon Schulz | } |
| 282 | |||
| 283 | //! actually execute the scheduled motion commands
|
||
| 284 | void iCubJointInterface::execute_motion(){
|
||
| 285 | |||
| 286 | // set up neck and eye motion commands:
|
||
| 287 | if (POSITION_CONTROL){
|
||
| 288 | //position control
|
||
| 289 | for(int i=ICUB_ID_NECK_TILT; i<=ICUB_ID_EYES_VERGENCE; i++){ |
||
| 290 | cc3bdc80 | Simon Schulz | set_target_in_positionmode(i); |
| 291 | 8c6c1163 | Simon Schulz | } |
| 292 | }else{
|
||
| 293 | //velocity control
|
||
| 294 | for(int i=ICUB_ID_NECK_TILT; i<=ICUB_ID_EYES_VERGENCE; i++){ |
||
| 295 | cc3bdc80 | Simon Schulz | set_target_in_velocitymode(i); |
| 296 | 8c6c1163 | Simon Schulz | } |
| 297 | } |
||
| 298 | //printf("> TARGET PAN = %3.2f\n",target_angle[ICUB_ID_NECK_PAN]);
|
||
| 299 | |||
| 300 | |||
| 301 | //eyelids: unfortuantely the icub has only 1dof for eyelids, so we use only one dof here:
|
||
| 302 | 35b3ca25 | Simon Schulz | face_interface_->set_eyelid_angle(target_angle_[ICUB_ID_EYES_RIGHT_LID_UPPER]); |
| 303 | 8c6c1163 | Simon Schulz | |
| 304 | //eyebrows are set using a special command as well:
|
||
| 305 | 35b3ca25 | Simon Schulz | face_interface_->set_eyebrow_angle(ICUB_ID_EYES_LEFT_BROW, target_angle_); |
| 306 | face_interface_->set_eyebrow_angle(ICUB_ID_EYES_RIGHT_BROW, target_angle_); |
||
| 307 | 8c6c1163 | Simon Schulz | |
| 308 | //mouth
|
||
| 309 | 35b3ca25 | Simon Schulz | face_interface_->set_mouth(target_angle_); |
| 310 | 8c6c1163 | Simon Schulz | |
| 311 | |||
| 312 | //store joint values which we do not handle on icub here:
|
||
| 313 | 35b3ca25 | Simon Schulz | double timestamp = humotion::Timestamp::now().to_seconds();
|
| 314 | JointInterface::store_incoming_position(ID_LIP_LEFT_UPPER, target_angle_[ICUB_ID_LIP_LEFT_UPPER], timestamp); |
||
| 315 | JointInterface::store_incoming_position(ID_LIP_LEFT_LOWER, target_angle_[ICUB_ID_LIP_LEFT_LOWER], timestamp); |
||
| 316 | JointInterface::store_incoming_position(ID_LIP_CENTER_UPPER, target_angle_[ICUB_ID_LIP_CENTER_UPPER], timestamp); |
||
| 317 | JointInterface::store_incoming_position(ID_LIP_CENTER_LOWER, target_angle_[ICUB_ID_LIP_CENTER_LOWER], timestamp); |
||
| 318 | JointInterface::store_incoming_position(ID_LIP_RIGHT_UPPER, target_angle_[ICUB_ID_LIP_RIGHT_UPPER], timestamp); |
||
| 319 | JointInterface::store_incoming_position(ID_LIP_RIGHT_LOWER, target_angle_[ICUB_ID_LIP_RIGHT_LOWER], timestamp); |
||
| 320 | 8c6c1163 | Simon Schulz | } |
| 321 | |||
| 322 | |||
| 323 | //! set the current position of a joint
|
||
| 324 | //! \param id of joint
|
||
| 325 | //! \param float value of position
|
||
| 326 | //! \param double timestamp
|
||
| 327 | 6c028e11 | Simon Schulz | void iCubJointInterface::store_incoming_position(int humotion_id, double position, double timestamp){ |
| 328 | cout << "iCubJointInterface::store_incoming_position(humotionid=" << humotion_id <<
|
||
| 329 | ", " << position << ", ...)\n"; |
||
| 330 | |||
| 331 | JointInterface::store_incoming_position(humotion_id, position, timestamp); |
||
| 332 | bb35ea6e | Simon Schulz | } |
| 333 | /*
|
||
| 334 | 8c6c1163 | Simon Schulz | //store joint based on id:
|
| 335 | switch(id){
|
||
| 336 | default:
|
||
| 337 | c6c9fe2c | Simon Schulz | //printf("> ERROR: unhandled joint id %d\n",id);
|
| 338 | 8c6c1163 | Simon Schulz | return;
|
| 339 | |||
| 340 | case(100):
|
||
| 341 | 0d0f5ca1 | Simon Schulz | //JointInterface::store_incoming_position(ID_EYES_RIGHT_LID_UPPER, lid_angle, timestamp);
|
| 342 | 8c6c1163 | Simon Schulz | break;
|
| 343 | |||
| 344 | 7adf90be | Simon Schulz | case(ICUB_ID_NECK_PAN):
|
| 345 | 8c6c1163 | Simon Schulz | //PAN is inverted!
|
| 346 | 0d0f5ca1 | Simon Schulz | JointInterface::store_incoming_position(ID_NECK_PAN, value, timestamp);
|
| 347 | 8c6c1163 | Simon Schulz | break;
|
| 348 | |||
| 349 | 7adf90be | Simon Schulz | case(ICUB_ID_NECK_TILT):
|
| 350 | 8c6c1163 | Simon Schulz | JointInterface::store_incoming_position(ID_NECK_TILT, value, timestamp);
|
| 351 | break;
|
||
| 352 | |||
| 353 | 7adf90be | Simon Schulz | case(ICUB_ID_NECK_ROLL):
|
| 354 | 8c6c1163 | Simon Schulz | JointInterface::store_incoming_position(ID_NECK_ROLL, value, timestamp);
|
| 355 | break;
|
||
| 356 | |||
| 357 | 7adf90be | Simon Schulz | case(ICUB_ID_EYES_BOTH_UD):
|
| 358 | 8c6c1163 | Simon Schulz | JointInterface::store_incoming_position(ID_EYES_BOTH_UD, value, timestamp);
|
| 359 | break;
|
||
| 360 | |||
| 361 | //icub handles eyes differently, we have to set pan angle + vergence
|
||
| 362 | 7adf90be | Simon Schulz | case(ICUB_ID_EYES_PAN): {//pan
|
| 363 | 8c6c1163 | Simon Schulz | last_pos_eye_pan = value;
|
| 364 | float left = last_pos_eye_pan + last_pos_eye_vergence/2.0;
|
||
| 365 | float right = last_pos_eye_pan - last_pos_eye_vergence/2.0;
|
||
| 366 | |||
| 367 | //printf("> eye: pan=%3.2f vergence=%3.2f --> L=%3.2f R=%3.2f\n", last_pos_eye_pan, last_pos_eye_vergence, left, right);
|
||
| 368 | JointInterface::store_incoming_position(ID_EYES_LEFT_LR, left, timestamp);
|
||
| 369 | JointInterface::store_incoming_position(ID_EYES_RIGHT_LR, right, timestamp);
|
||
| 370 | break;
|
||
| 371 | }
|
||
| 372 | |||
| 373 | 7adf90be | Simon Schulz | case(ICUB_ID_EYES_VERGENCE): { //vergence
|
| 374 | 8c6c1163 | Simon Schulz | last_pos_eye_vergence = value;
|
| 375 | float left = last_pos_eye_pan + last_pos_eye_vergence/2.0;
|
||
| 376 | float right = last_pos_eye_pan - last_pos_eye_vergence/2.0;
|
||
| 377 | |||
| 378 | //printf("> eye: pan=%3.2f vergence=%3.2f --> L=%3.2f R=%3.2f\n", last_pos_eye_pan, last_pos_eye_vergence, left, right);
|
||
| 379 | JointInterface::store_incoming_position(ID_EYES_LEFT_LR, left, timestamp);
|
||
| 380 | JointInterface::store_incoming_position(ID_EYES_RIGHT_LR, right, timestamp);
|
||
| 381 | break;
|
||
| 382 | }
|
||
| 383 | }
|
||
| 384 | |||
| 385 | |||
| 386 | 6e23ab1a | Simon Schulz | }*/
|
| 387 | 8c6c1163 | Simon Schulz | |
| 388 | 7adf90be | Simon Schulz | void iCubJointInterface::set_joint_enable_state(int e, bool enable) { |
| 389 | int icub_jointid = -1; |
||
| 390 | 8c6c1163 | Simon Schulz | |
| 391 | 7adf90be | Simon Schulz | switch(e){
|
| 392 | default:
|
||
| 393 | break;
|
||
| 394 | 8c6c1163 | Simon Schulz | |
| 395 | 7adf90be | Simon Schulz | case(ID_NECK_PAN):
|
| 396 | icub_jointid = ICUB_ID_NECK_PAN; |
||
| 397 | break;
|
||
| 398 | 8c6c1163 | Simon Schulz | |
| 399 | 7adf90be | Simon Schulz | case(ID_NECK_TILT):
|
| 400 | icub_jointid = ICUB_ID_NECK_TILT; |
||
| 401 | break;
|
||
| 402 | 8c6c1163 | Simon Schulz | |
| 403 | 7adf90be | Simon Schulz | case(ID_NECK_ROLL):
|
| 404 | icub_jointid = ICUB_ID_NECK_ROLL; |
||
| 405 | break;
|
||
| 406 | |||
| 407 | case(ID_EYES_BOTH_UD):
|
||
| 408 | icub_jointid = ICUB_ID_EYES_BOTH_UD; |
||
| 409 | break;
|
||
| 410 | |||
| 411 | // icub handles eyes as pan angle + vergence...
|
||
| 412 | // -> hack: left eye enables pan and right eye enables vergence
|
||
| 413 | case(ID_EYES_LEFT_LR):
|
||
| 414 | icub_jointid = ICUB_ID_EYES_PAN; |
||
| 415 | break;
|
||
| 416 | |||
| 417 | case(ID_EYES_RIGHT_LR):
|
||
| 418 | icub_jointid = ICUB_ID_EYES_VERGENCE; |
||
| 419 | break;
|
||
| 420 | 8c6c1163 | Simon Schulz | } |
| 421 | |||
| 422 | 7adf90be | Simon Schulz | if (icub_jointid != -1) { |
| 423 | if (enable) {
|
||
| 424 | 35b3ca25 | Simon Schulz | yarp_amp_->enableAmp(icub_jointid); |
| 425 | yarp_pid_->enablePid(icub_jointid); |
||
| 426 | 7adf90be | Simon Schulz | } else {
|
| 427 | 35b3ca25 | Simon Schulz | yarp_pid_->disablePid(icub_jointid); |
| 428 | yarp_amp_->disableAmp(icub_jointid); |
||
| 429 | 7adf90be | Simon Schulz | } |
| 430 | } |
||
| 431 | 8c6c1163 | Simon Schulz | } |
| 432 | |||
| 433 | //! prepare and enable a joint
|
||
| 434 | //! NOTE: this should also prefill the min/max positions for this joint
|
||
| 435 | //! \param the enum id of a joint
|
||
| 436 | void iCubJointInterface::enable_joint(int e){ |
||
| 437 | 7adf90be | Simon Schulz | set_joint_enable_state(e, true);
|
| 438 | } |
||
| 439 | 8c6c1163 | Simon Schulz | |
| 440 | 7adf90be | Simon Schulz | //! shutdown and disable a joint
|
| 441 | //! \param the enum id of a joint
|
||
| 442 | void iCubJointInterface::disable_joint(int e){ |
||
| 443 | set_joint_enable_state(e, false);
|
||
| 444 | 8c6c1163 | Simon Schulz | } |
| 445 | |||
| 446 | void iCubJointInterface::store_min_max(IControlLimits *ilimits, int id, int e){ |
||
| 447 | double min, max;
|
||
| 448 | ilimits->getLimits(id, &min, &max); |
||
| 449 | joint_min[e] = min; |
||
| 450 | joint_max[e] = max; |
||
| 451 | } |
||
| 452 | |||
| 453 | //! initialise a joint (set up controller mode etc)
|
||
| 454 | //! \param joint enum
|
||
| 455 | void iCubJointInterface::init_joints(){
|
||
| 456 | 35b3ca25 | Simon Schulz | store_min_max(yarp_ilimits_, ICUB_ID_NECK_TILT, ID_NECK_TILT); |
| 457 | store_min_max(yarp_ilimits_, ICUB_ID_NECK_ROLL, ID_NECK_ROLL); |
||
| 458 | store_min_max(yarp_ilimits_, ICUB_ID_NECK_PAN, ID_NECK_PAN); |
||
| 459 | store_min_max(yarp_ilimits_, ICUB_ID_EYES_BOTH_UD, ID_EYES_BOTH_UD); |
||
| 460 | 8c6c1163 | Simon Schulz | |
| 461 | //icub handles eyes differently, we have to set pan angle + vergence
|
||
| 462 | double pan_min, pan_max, vergence_min, vergence_max;
|
||
| 463 | 35b3ca25 | Simon Schulz | yarp_ilimits_->getLimits(ICUB_ID_EYES_PAN, &pan_min, &pan_max); |
| 464 | yarp_ilimits_->getLimits(ICUB_ID_EYES_VERGENCE, &vergence_min, &vergence_max); |
||
| 465 | 8c6c1163 | Simon Schulz | |
| 466 | //this is not 100% correct, should be fixed:
|
||
| 467 | joint_min[ID_EYES_LEFT_LR] = pan_min; // - vergence_max/2;
|
||
| 468 | joint_max[ID_EYES_LEFT_LR] = pan_max; // - vergence_max/2;
|
||
| 469 | joint_min[ID_EYES_RIGHT_LR] = joint_min[ID_EYES_LEFT_LR]; |
||
| 470 | joint_max[ID_EYES_RIGHT_LR] = joint_max[ID_EYES_LEFT_LR]; |
||
| 471 | |||
| 472 | //eyelids:
|
||
| 473 | joint_min[ID_EYES_RIGHT_LID_UPPER] = -50; //24-30; |
||
| 474 | joint_max[ID_EYES_RIGHT_LID_UPPER] = 50; //48-30; |
||
| 475 | 0d0f5ca1 | Simon Schulz | //lid_angle = joint_max[ID_EYES_RIGHT_LID_UPPER];
|
| 476 | 8c6c1163 | Simon Schulz | |
| 477 | //eyebrows:
|
||
| 478 | joint_min[ID_EYES_LEFT_BROW] = -50;
|
||
| 479 | joint_max[ID_EYES_LEFT_BROW] = 50;
|
||
| 480 | joint_min[ID_EYES_RIGHT_BROW] = joint_min[ID_EYES_LEFT_BROW]; |
||
| 481 | joint_max[ID_EYES_RIGHT_BROW] = joint_max[ID_EYES_LEFT_BROW]; |
||
| 482 | |||
| 483 | //mouth:
|
||
| 484 | joint_min[ID_LIP_CENTER_UPPER] = 5;
|
||
| 485 | joint_max[ID_LIP_CENTER_UPPER] = 50;
|
||
| 486 | joint_min[ID_LIP_CENTER_LOWER] = 5;
|
||
| 487 | joint_max[ID_LIP_CENTER_LOWER] = 50;
|
||
| 488 | joint_min[ID_LIP_LEFT_UPPER] = 5;
|
||
| 489 | joint_max[ID_LIP_LEFT_UPPER] = 50;
|
||
| 490 | joint_min[ID_LIP_LEFT_LOWER] = 5;
|
||
| 491 | joint_max[ID_LIP_LEFT_LOWER] = 50;
|
||
| 492 | joint_min[ID_LIP_RIGHT_UPPER] = 5;
|
||
| 493 | joint_max[ID_LIP_RIGHT_UPPER] = 50;
|
||
| 494 | joint_min[ID_LIP_RIGHT_LOWER] = 5;
|
||
| 495 | joint_max[ID_LIP_RIGHT_LOWER] = 50;
|
||
| 496 | 35b3ca25 | Simon Schulz | } |
| 497 | |||
| 498 | //! conversion table for humotion joint id to icub joint id
|
||
| 499 | //! \param int value for humotion joint id from JointInterface::JOINT_ID_ENUM
|
||
| 500 | //! \return int value of icub joint id
|
||
| 501 | int iCubJointInterface::convert_humotion_jointid_to_icub(int humotion_id){ |
||
| 502 | enum_id_bimap_t::right_const_iterator it = enum_id_bimap.right.find(humotion_id); |
||
| 503 | if(it == enum_id_bimap.right.end()) {
|
||
| 504 | //key does not exist
|
||
| 505 | 318729f7 | Simon Schulz | cout << "ERROR: invalid humotion joint id (" << humotion_id << ") given. can not convert this. exiting\n"; |
| 506 | 35b3ca25 | Simon Schulz | exit(EXIT_FAILURE); |
| 507 | } |
||
| 508 | return it->second;
|
||
| 509 | } |
||
| 510 | 8c6c1163 | Simon Schulz | |
| 511 | |||
| 512 | 35b3ca25 | Simon Schulz | //! conversion table for icub joint id to humotion joint id
|
| 513 | //! \param int value of icub joint id
|
||
| 514 | //! \return int value of humotion joint id from JointInterface::JOINT_ID_ENUM
|
||
| 515 | int iCubJointInterface::convert_icub_jointid_to_humotion(int icub_id){ |
||
| 516 | enum_id_bimap_t::left_const_iterator it = enum_id_bimap.left.find(icub_id); |
||
| 517 | if(it == enum_id_bimap.left.end()) {
|
||
| 518 | //key does not exist
|
||
| 519 | cout << "ERROR: invalid icub joint id given. can not convert this. exiting\n";
|
||
| 520 | exit(EXIT_FAILURE); |
||
| 521 | } |
||
| 522 | return it->second;
|
||
| 523 | 8c6c1163 | Simon Schulz | } |