humotion / examples / yarp_icub / src / icub_jointinterface.cpp @ 3e3a8bd9
History | View | Annotate | Download (19.411 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 | //WARNING: DO NOT CHANGE THIS; VELOCITYMODE IS NOT YET IMPLEMENTED
 | 
      ||
| 21 | 7adf90be | Simon Schulz | #define POSITION_CONTROL 0  | 
      
| 22 | 8c6c1163 | Simon Schulz | |
| 23 | |||
| 24 | //! constructor
 | 
      ||
| 25 | iCubJointInterface::iCubJointInterface(string _scope) : humotion::server::JointInterface(){
 | 
      ||
| 26 | scope = _scope;  | 
      ||
| 27 | 0d0f5ca1 | Simon Schulz |     face_interface = new iCubFaceInterface(scope);
 | 
      
| 28 | 8c6c1163 | Simon Schulz | |
| 29 |     //add mapping from ids to enums:
 | 
      ||
| 30 |     //this might look strange at the first sight but we need to have a generic
 | 
      ||
| 31 |     //way to acces joints from libhumotion. therefore the lib uses its enum with ID_* enum ids
 | 
      ||
| 32 |     //to access the joints. now we need to define a mapping to map those to our motor ids.
 | 
      ||
| 33 |     //this is what we use the enum bimap for (convertion fro/to motorid is handled
 | 
      ||
| 34 |     //by \sa convert_enum_to_motorid() and \sa convert_motorid_to_enum() lateron
 | 
      ||
| 35 | |||
| 36 |     //MOUTH
 | 
      ||
| 37 | enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_LIP_LEFT_UPPER, ID_LIP_LEFT_UPPER));  | 
      ||
| 38 | enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_LIP_LEFT_LOWER, ID_LIP_LEFT_LOWER));  | 
      ||
| 39 | enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_LIP_CENTER_UPPER, ID_LIP_CENTER_UPPER));  | 
      ||
| 40 | enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_LIP_CENTER_LOWER, ID_LIP_CENTER_LOWER));  | 
      ||
| 41 | enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_LIP_RIGHT_UPPER, ID_LIP_RIGHT_UPPER));  | 
      ||
| 42 | enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_LIP_RIGHT_LOWER, ID_LIP_RIGHT_LOWER));  | 
      ||
| 43 | |||
| 44 |     //NECK
 | 
      ||
| 45 | enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_NECK_PAN, ID_NECK_PAN));  | 
      ||
| 46 | enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_NECK_TILT, ID_NECK_TILT));  | 
      ||
| 47 | enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_NECK_ROLL, ID_NECK_ROLL));  | 
      ||
| 48 | |||
| 49 |     //EYES
 | 
      ||
| 50 | 7adf90be | Simon Schulz | enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_EYES_PAN, ID_EYES_LEFT_LR));  | 
      
| 51 | enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_EYES_VERGENCE, ID_EYES_RIGHT_LR));  | 
      ||
| 52 | 8c6c1163 | Simon Schulz | enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_EYES_BOTH_UD, ID_EYES_BOTH_UD));  | 
      
| 53 | |||
| 54 |     //EYELIDS
 | 
      ||
| 55 | enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_EYES_LEFT_LID_LOWER, ID_EYES_LEFT_LID_LOWER));  | 
      ||
| 56 | enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_EYES_LEFT_LID_UPPER, ID_EYES_LEFT_LID_UPPER));  | 
      ||
| 57 | enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_EYES_LEFT_BROW, ID_EYES_LEFT_BROW));  | 
      ||
| 58 | |||
| 59 | enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_EYES_RIGHT_LID_LOWER, ID_EYES_RIGHT_LID_LOWER));  | 
      ||
| 60 | enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_EYES_RIGHT_LID_UPPER,ID_EYES_RIGHT_LID_UPPER));  | 
      ||
| 61 | enum_id_bimap.insert(enum_id_bimap_entry_t(ICUB_ID_EYES_RIGHT_BROW, ID_EYES_RIGHT_BROW));  | 
      ||
| 62 | |||
| 63 | 87b50988 | Simon Schulz |     //init pd control variables
 | 
      
| 64 | cc3bdc80 | Simon Schulz | enum_id_bimap_t::const_iterator it;  | 
      
| 65 | 1a35abea | Simon Schulz | last_position_error.resize(ICUB_JOINT_ID_ENUM_SIZE);  | 
      
| 66 | PID_P.resize(ICUB_JOINT_ID_ENUM_SIZE);  | 
      ||
| 67 | PID_D.resize(ICUB_JOINT_ID_ENUM_SIZE);  | 
      ||
| 68 | |||
| 69 | cc3bdc80 | Simon Schulz |     for(it = enum_id_bimap.begin(); it != enum_id_bimap.end(); ++it) {
 | 
      
| 70 |         int id = it->left;
 | 
      ||
| 71 | last_position_error[id] = 0.0;  | 
      ||
| 72 | d3da3b33 | Simon Schulz | PID_P[id] = 4.5;  | 
      
| 73 | PID_D[id] = 0.3;  | 
      ||
| 74 | 87b50988 | Simon Schulz | }  | 
      
| 75 | |||
| 76 | |||
| 77 | 8c6c1163 | Simon Schulz | Property options;  | 
      
| 78 | options.put("device", "remote_controlboard");  | 
      ||
| 79 | options.put("local", "/local/head");  | 
      ||
| 80 | options.put("remote", scope+"/head");  | 
      ||
| 81 | dd.open(options);  | 
      ||
| 82 | |||
| 83 |     //fetch views:
 | 
      ||
| 84 | dd.view(iencs);  | 
      ||
| 85 | dd.view(ipos);  | 
      ||
| 86 | dd.view(ivel);  | 
      ||
| 87 | dd.view(ilimits);  | 
      ||
| 88 | 7adf90be | Simon Schulz | dd.view(pid);  | 
      
| 89 | dd.view(amp);  | 
      ||
| 90 | 8c6c1163 | Simon Schulz | |
| 91 | 7adf90be | Simon Schulz | |
| 92 |     if ( (!iencs) || (!ipos) || (!ilimits) || (!ivel) || (!amp) || (!pid)){
 | 
      ||
| 93 | 8c6c1163 | Simon Schulz |         printf("> ERROR: failed to open icub views\n");
 | 
      
| 94 | exit(EXIT_FAILURE);  | 
      ||
| 95 | }  | 
      ||
| 96 | |||
| 97 |     int joints;
 | 
      ||
| 98 | |||
| 99 |     //tell humotion about min/max joint values:
 | 
      ||
| 100 | init_joints();  | 
      ||
| 101 | |||
| 102 | iencs->getAxes(&joints);  | 
      ||
| 103 | positions.resize(joints);  | 
      ||
| 104 | velocities.resize(joints);  | 
      ||
| 105 | commands.resize(joints);  | 
      ||
| 106 | |||
| 107 |     //set position mode:
 | 
      ||
| 108 |     if (POSITION_CONTROL){
 | 
      ||
| 109 | commands=200000.0;  | 
      ||
| 110 | ipos->setRefAccelerations(commands.data());  | 
      ||
| 111 | ipos->setPositionMode();  | 
      ||
| 112 |     }else{
 | 
      ||
| 113 | ivel->setVelocityMode();  | 
      ||
| 114 | 3e3a8bd9 | Simon Schulz | commands=300.0;  | 
      
| 115 | 8c6c1163 | Simon Schulz | ivel->setRefAccelerations(commands.data());  | 
      
| 116 | }  | 
      ||
| 117 | |||
| 118 | }  | 
      ||
| 119 | |||
| 120 | //! destructor
 | 
      ||
| 121 | iCubJointInterface::~iCubJointInterface(){
 | 
      ||
| 122 | }  | 
      ||
| 123 | |||
| 124 | |||
| 125 | |||
| 126 | //! conversion table for humotion motor ids to our ids:
 | 
      ||
| 127 | //! \param enum from JointInterface::JOINT_ID_ENUM
 | 
      ||
| 128 | //! \return int value of motor id
 | 
      ||
| 129 | int iCubJointInterface::convert_enum_to_motorid(int e){  | 
      ||
| 130 | enum_id_bimap_t::right_const_iterator it = enum_id_bimap.right.find(e);  | 
      ||
| 131 |     if(it == enum_id_bimap.right.end()) {
 | 
      ||
| 132 |         //key does not exists, we are not interested in that dataset, return -1
 | 
      ||
| 133 | return -1;  | 
      ||
| 134 | }  | 
      ||
| 135 |     return it->second;
 | 
      ||
| 136 | }  | 
      ||
| 137 | |||
| 138 | |||
| 139 | //! conversion table for our ids to humotion motor ids:
 | 
      ||
| 140 | //! \param  int value of motor id
 | 
      ||
| 141 | //! \return enum from JointInterface::JOINT_ID_ENUM
 | 
      ||
| 142 | int iCubJointInterface::convert_motorid_to_enum(int id){  | 
      ||
| 143 | enum_id_bimap_t::left_const_iterator it = enum_id_bimap.left.find(id);  | 
      ||
| 144 |     if(it == enum_id_bimap.left.end()) {
 | 
      ||
| 145 |         //key does not exists, we are not interested in that dataset, return -1
 | 
      ||
| 146 | return -1;  | 
      ||
| 147 | }  | 
      ||
| 148 |     return it->second;
 | 
      ||
| 149 | }  | 
      ||
| 150 | |||
| 151 | |||
| 152 | void iCubJointInterface::run(){
 | 
      ||
| 153 | 1efa78e9 | sschulz | iCubDataReceiver *data_receiver = new iCubDataReceiver(0.5 * 1000.0 / MAIN_LOOP_FREQUENCY, iencs, this);  | 
      
| 154 | 8c6c1163 | Simon Schulz | data_receiver->start();  | 
      
| 155 | }  | 
      ||
| 156 | |||
| 157 | //! set the target position of a joint
 | 
      ||
| 158 | //! \param enum id of joint
 | 
      ||
| 159 | //! \param float value
 | 
      ||
| 160 | void iCubJointInterface::publish_target_position(int e){  | 
      ||
| 161 |     //first: convert humotion enum to our enum:
 | 
      ||
| 162 |     int id = convert_enum_to_motorid(e);
 | 
      ||
| 163 | 0d0f5ca1 | Simon Schulz | |
| 164 | 8c6c1163 | Simon Schulz | if (id == -1){  | 
      
| 165 | return; //we are not interested in that data, so we just return here  | 
      ||
| 166 | }  | 
      ||
| 167 | |||
| 168 |     if (id == ICUB_ID_NECK_PAN){
 | 
      ||
| 169 |         //PAN seems to be swapped
 | 
      ||
| 170 | store_joint(ICUB_ID_NECK_PAN, -joint_target[e]);  | 
      ||
| 171 | 7adf90be | Simon Schulz | }else if ((id == ICUB_ID_EYES_PAN) || ( id == ICUB_ID_EYES_VERGENCE)){  | 
      
| 172 | 8c6c1163 | Simon Schulz |         //icub handles eyes differently, we have to set pan angle + vergence
 | 
      
| 173 | float pan = (joint_target[ID_EYES_LEFT_LR] + joint_target[ID_EYES_RIGHT_LR]) / 2;  | 
      ||
| 174 |         float vergence = (joint_target[ID_EYES_LEFT_LR]  - joint_target[ID_EYES_RIGHT_LR]);
 | 
      ||
| 175 |         //printf("EYEDBG %3.2f %3.2f --_> pan %3.2f verg=%3.2f\n",joint_target[ID_EYES_LEFT_LR], joint_target[ID_EYES_RIGHT_LR],pan,vergence);
 | 
      ||
| 176 | |||
| 177 | store_joint(ICUB_ID_EYES_PAN, pan);  | 
      ||
| 178 | store_joint(ICUB_ID_EYES_VERGENCE, vergence);  | 
      ||
| 179 |     }else{
 | 
      ||
| 180 | store_joint(id, joint_target[e]);  | 
      ||
| 181 | }  | 
      ||
| 182 | }  | 
      ||
| 183 | |||
| 184 | |||
| 185 | //! set the target position of a joint
 | 
      ||
| 186 | //! \param id of joint
 | 
      ||
| 187 | //! \param float value of position
 | 
      ||
| 188 | void iCubJointInterface::store_joint(int id, float value){  | 
      ||
| 189 | 0d0f5ca1 | Simon Schulz |     printf("> set joint %d = %f\n",id,value);
 | 
      
| 190 | 8c6c1163 | Simon Schulz | target_angle[id] = value;  | 
      
| 191 | }  | 
      ||
| 192 | |||
| 193 | //! execute a move in position mode
 | 
      ||
| 194 | //! \param id of joint
 | 
      ||
| 195 | //! \param angle
 | 
      ||
| 196 | 87b50988 | Simon Schulz | void iCubJointInterface::set_target_in_positionmode(int id){  | 
      
| 197 |     double target = target_angle[id];
 | 
      ||
| 198 | |||
| 199 | 8c6c1163 | Simon Schulz |     if (id>ICUB_ID_EYES_VERGENCE){
 | 
      
| 200 | 87b50988 | Simon Schulz |         printf("> ERROR: set_target_positionmode(id=%d, %3.2f) not supported for this id\n",id,target);
 | 
      
| 201 | 8c6c1163 | Simon Schulz |         return;
 | 
      
| 202 | }  | 
      ||
| 203 | |||
| 204 | 7adf90be | Simon Schulz |     // execute motion as position control cmd
 | 
      
| 205 | 87b50988 | Simon Schulz | ipos->positionMove(id, target);  | 
      
| 206 | 7adf90be | Simon Schulz | |
| 207 | 8c6c1163 | Simon Schulz | }  | 
      
| 208 | |||
| 209 | //! execute a move in velocity mode
 | 
      ||
| 210 | //! \param id of joint
 | 
      ||
| 211 | //! \param angle
 | 
      ||
| 212 | 87b50988 | Simon Schulz | void iCubJointInterface::set_target_in_velocitymode(int id){  | 
      
| 213 | 7adf90be | Simon Schulz |     // set speed cacluated as in velocity + set position -> replicates smoothmotion from flobi?!
 | 
      
| 214 | 8c6c1163 | Simon Schulz |     //first: calculate necessary speed to reach the given target within the next clock tick:
 | 
      
| 215 | cc3bdc80 | Simon Schulz |     double distance = target_angle[id] - target_angle_previous[id];
 | 
      
| 216 | 7adf90be | Simon Schulz | |
| 217 | 8c6c1163 | Simon Schulz |     //make the motion smooth: we want to reach 85% of the target in the next iteration:
 | 
      
| 218 | distance = 0.85 * distance;  | 
      ||
| 219 | 7adf90be | Simon Schulz | |
| 220 |     //distance = -5.0 / 50.0;
 | 
      ||
| 221 | |||
| 222 | 8c6c1163 | Simon Schulz |     //calculate speed
 | 
      
| 223 | 7adf90be | Simon Schulz |     //double speed = distance * ((double)MAIN_LOOP_FREQUENCY);
 | 
      
| 224 | |||
| 225 | |||
| 226 | |||
| 227 |     int e = convert_motorid_to_enum(id);
 | 
      ||
| 228 |     double speed = joint_target_speed[e];
 | 
      ||
| 229 | |||
| 230 | double max = 150.0;  | 
      ||
| 231 |     if (speed > max) speed = max;
 | 
      ||
| 232 |     if (speed < -max) speed = -max;
 | 
      ||
| 233 | |||
| 234 |     //speed = -speed;
 | 
      ||
| 235 | |||
| 236 | |||
| 237 | 497d9d24 | Simon Schulz |     // find out the latency between incoming data and now:
 | 
      
| 238 |     float latency = get_ts_speed(e).get_last_timestamp().to_seconds() - humotion::Timestamp::now().to_seconds();
 | 
      ||
| 239 | printf("TS DIFF %fms\n",latency*1000.0);  | 
      ||
| 240 | |||
| 241 | 8c6c1163 | Simon Schulz |     //execute:
 | 
      
| 242 | 7adf90be | Simon Schulz |     //ivel->velocityMove(id, speed);
 | 
      
| 243 |     if ((id == ICUB_ID_NECK_PAN)  || (id == ICUB_ID_EYES_BOTH_UD) || (id == ICUB_ID_NECK_TILT) || (id == ICUB_ID_EYES_BOTH_UD) ||  (id == ICUB_ID_NECK_TILT) ){
 | 
      ||
| 244 | 87b50988 | Simon Schulz |         //do a pd control for velocity moves that incorporates position errors:
 | 
      
| 245 | adf38895 | Simon Schulz | humotion::Timestamp data_ts = get_ts_position(e).get_last_timestamp();  | 
      
| 246 | 87b50988 | Simon Schulz |         //TODO: add interpolation into future!
 | 
      
| 247 |         //humotion::Timestamp data_ts = humotion::Timestamp::now(); and extend get_interpol value with get_future_value
 | 
      ||
| 248 | adf38895 | Simon Schulz |         double position_error = target_angle[id] - get_ts_position(e).get_interpolated_value(data_ts);
 | 
      
| 249 | cc3bdc80 | Simon Schulz | double error_d = (position_error - last_position_error[id]) / (framerate*1000.0);  | 
      
| 250 | 87b50988 | Simon Schulz | last_position_error[id] = position_error;  | 
      
| 251 |         //finally do a PD loop to get the target velocity
 | 
      ||
| 252 |         double target_velocity = PID_P[id] * position_error + PID_D[id]*error_d + speed;
 | 
      ||
| 253 | |||
| 254 | 196b8635 | Simon Schulz |         printf("%f %f %f %f %f %f PID%d\n",
 | 
      
| 255 | adf38895 | Simon Schulz | get_ts_position(e).get_interpolated_value(data_ts),  | 
      
| 256 | b6d0fdeb | Simon Schulz | target_angle[id],  | 
      
| 257 | adf38895 | Simon Schulz | get_ts_speed(e).get_interpolated_value(data_ts),  | 
      
| 258 | b6d0fdeb | Simon Schulz | target_velocity,  | 
      
| 259 | speed,  | 
      ||
| 260 | 196b8635 | Simon Schulz | position_error,  | 
      
| 261 | id  | 
      ||
| 262 | b6d0fdeb | Simon Schulz | );  | 
      
| 263 | |||
| 264 | |||
| 265 | 0d0f5ca1 | Simon Schulz |         //if (id == ICUB_ID_NECK_PAN) speed = -speed;
 | 
      
| 266 | ba408480 | Simon Schulz | ivel->velocityMove(id, target_velocity);  | 
      
| 267 | cc3bdc80 | Simon Schulz |         printf("> VEL now=%3.2f target=%3.2f --> dist=%3.2f speed=%3.2f\n",target_angle_previous[id],target_angle[id],distance,speed);
 | 
      
| 268 | 7adf90be | Simon Schulz | }  | 
      
| 269 | 8c6c1163 | Simon Schulz | |
| 270 | target_angle_previous[id] = get_ts_position(convert_motorid_to_enum(id)).get_newest_value();  | 
      ||
| 271 | }  | 
      ||
| 272 | |||
| 273 | //! actually execute the scheduled motion commands
 | 
      ||
| 274 | void iCubJointInterface::execute_motion(){
 | 
      ||
| 275 | |||
| 276 |     // set up neck and eye motion commands:
 | 
      ||
| 277 |     if (POSITION_CONTROL){
 | 
      ||
| 278 |         //position control
 | 
      ||
| 279 | for(int i=ICUB_ID_NECK_TILT; i<=ICUB_ID_EYES_VERGENCE; i++){  | 
      ||
| 280 | cc3bdc80 | Simon Schulz | set_target_in_positionmode(i);  | 
      
| 281 | 8c6c1163 | Simon Schulz | }  | 
      
| 282 |     }else{
 | 
      ||
| 283 |         //velocity control
 | 
      ||
| 284 | for(int i=ICUB_ID_NECK_TILT; i<=ICUB_ID_EYES_VERGENCE; i++){  | 
      ||
| 285 | cc3bdc80 | Simon Schulz | set_target_in_velocitymode(i);  | 
      
| 286 | 8c6c1163 | Simon Schulz | }  | 
      
| 287 | }  | 
      ||
| 288 |     //printf("> TARGET PAN = %3.2f\n",target_angle[ICUB_ID_NECK_PAN]);
 | 
      ||
| 289 | |||
| 290 | |||
| 291 |     //eyelids: unfortuantely the icub has only 1dof for eyelids, so we use only one dof here:
 | 
      ||
| 292 | 0d0f5ca1 | Simon Schulz | face_interface->set_eyelid_angle(target_angle[ICUB_ID_EYES_RIGHT_LID_UPPER]);  | 
      
| 293 | 8c6c1163 | Simon Schulz | |
| 294 |     //eyebrows are set using a special command as well:
 | 
      ||
| 295 | 0d0f5ca1 | Simon Schulz | face_interface->set_eyebrow_angle(ICUB_ID_EYES_LEFT_BROW, target_angle);  | 
      
| 296 | face_interface->set_eyebrow_angle(ICUB_ID_EYES_RIGHT_BROW, target_angle);  | 
      ||
| 297 | 8c6c1163 | Simon Schulz | |
| 298 |     //mouth
 | 
      ||
| 299 | 0d0f5ca1 | Simon Schulz | face_interface->set_mouth(target_angle);  | 
      
| 300 | 8c6c1163 | Simon Schulz | |
| 301 | |||
| 302 |     //store joint values which we do not handle on icub here:
 | 
      ||
| 303 |     double timestamp = get_timestamp_ms();
 | 
      ||
| 304 | JointInterface::store_incoming_position(ID_LIP_LEFT_UPPER, target_angle[ICUB_ID_LIP_LEFT_UPPER], timestamp);  | 
      ||
| 305 | JointInterface::store_incoming_position(ID_LIP_LEFT_LOWER, target_angle[ICUB_ID_LIP_LEFT_LOWER], timestamp);  | 
      ||
| 306 | JointInterface::store_incoming_position(ID_LIP_CENTER_UPPER, target_angle[ICUB_ID_LIP_CENTER_UPPER], timestamp);  | 
      ||
| 307 | JointInterface::store_incoming_position(ID_LIP_CENTER_LOWER, target_angle[ICUB_ID_LIP_CENTER_LOWER], timestamp);  | 
      ||
| 308 | JointInterface::store_incoming_position(ID_LIP_RIGHT_UPPER, target_angle[ICUB_ID_LIP_RIGHT_UPPER], timestamp);  | 
      ||
| 309 | JointInterface::store_incoming_position(ID_LIP_RIGHT_LOWER, target_angle[ICUB_ID_LIP_RIGHT_LOWER], timestamp);  | 
      ||
| 310 | }  | 
      ||
| 311 | |||
| 312 | double iCubJointInterface::get_timestamp_ms(){
 | 
      ||
| 313 |     struct timespec spec;
 | 
      ||
| 314 | clock_gettime(CLOCK_REALTIME, &spec);  | 
      ||
| 315 | return spec.tv_sec*1000 + spec.tv_nsec / 1.0e6;  | 
      ||
| 316 | }  | 
      ||
| 317 | |||
| 318 | //! set the current position of a joint
 | 
      ||
| 319 | //! \param id of joint
 | 
      ||
| 320 | //! \param float value of position
 | 
      ||
| 321 | //! \param double timestamp
 | 
      ||
| 322 | void iCubJointInterface::fetch_position(int id, double value, double timestamp){  | 
      ||
| 323 |     //store joint based on id:
 | 
      ||
| 324 |     switch(id){
 | 
      ||
| 325 |         default:
 | 
      ||
| 326 |             printf("> ERROR: unhandled joint id %d\n",id);
 | 
      ||
| 327 |             return;
 | 
      ||
| 328 | |||
| 329 | case(100):  | 
      ||
| 330 | 0d0f5ca1 | Simon Schulz |             //JointInterface::store_incoming_position(ID_EYES_RIGHT_LID_UPPER, lid_angle, timestamp);
 | 
      
| 331 | 8c6c1163 | Simon Schulz |             break;
 | 
      
| 332 | |||
| 333 | 7adf90be | Simon Schulz |         case(ICUB_ID_NECK_PAN):
 | 
      
| 334 | 8c6c1163 | Simon Schulz |             //PAN is inverted!
 | 
      
| 335 | 0d0f5ca1 | Simon Schulz | JointInterface::store_incoming_position(ID_NECK_PAN, value, timestamp);  | 
      
| 336 | 8c6c1163 | Simon Schulz |             break;
 | 
      
| 337 | |||
| 338 | 7adf90be | Simon Schulz |         case(ICUB_ID_NECK_TILT):
 | 
      
| 339 | 8c6c1163 | Simon Schulz | JointInterface::store_incoming_position(ID_NECK_TILT, value, timestamp);  | 
      
| 340 |             break;
 | 
      ||
| 341 | |||
| 342 | 7adf90be | Simon Schulz |         case(ICUB_ID_NECK_ROLL):
 | 
      
| 343 | 8c6c1163 | Simon Schulz | JointInterface::store_incoming_position(ID_NECK_ROLL, value, timestamp);  | 
      
| 344 |             break;
 | 
      ||
| 345 | |||
| 346 | 7adf90be | Simon Schulz |         case(ICUB_ID_EYES_BOTH_UD):
 | 
      
| 347 | 8c6c1163 | Simon Schulz | JointInterface::store_incoming_position(ID_EYES_BOTH_UD, value, timestamp);  | 
      
| 348 |             break;
 | 
      ||
| 349 | |||
| 350 |         //icub handles eyes differently, we have to set pan angle + vergence
 | 
      ||
| 351 | 7adf90be | Simon Schulz | case(ICUB_ID_EYES_PAN): {//pan  | 
      
| 352 | 8c6c1163 | Simon Schulz | last_pos_eye_pan = value;  | 
      
| 353 | float left = last_pos_eye_pan + last_pos_eye_vergence/2.0;  | 
      ||
| 354 | float right = last_pos_eye_pan - last_pos_eye_vergence/2.0;  | 
      ||
| 355 | |||
| 356 |             //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);
 | 
      ||
| 357 | JointInterface::store_incoming_position(ID_EYES_LEFT_LR, left, timestamp);  | 
      ||
| 358 | JointInterface::store_incoming_position(ID_EYES_RIGHT_LR, right, timestamp);  | 
      ||
| 359 |             break;
 | 
      ||
| 360 | }  | 
      ||
| 361 | |||
| 362 | 7adf90be | Simon Schulz | case(ICUB_ID_EYES_VERGENCE): { //vergence  | 
      
| 363 | 8c6c1163 | Simon Schulz | last_pos_eye_vergence = 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 | |||
| 374 | |||
| 375 | }  | 
      ||
| 376 | |||
| 377 | //! set the current speed of a joint
 | 
      ||
| 378 | //! \param enum id of joint
 | 
      ||
| 379 | //! \param float value of speed
 | 
      ||
| 380 | //! \param double timestamp
 | 
      ||
| 381 | void iCubJointInterface::fetch_speed(int id, double value, double timestamp){  | 
      ||
| 382 | |||
| 383 |     switch(id){
 | 
      ||
| 384 |         default:
 | 
      ||
| 385 |             printf("> ERROR: unhandled joint id %d\n",id);
 | 
      ||
| 386 |             return;
 | 
      ||
| 387 | |||
| 388 | 7adf90be | Simon Schulz |         case(ICUB_ID_NECK_PAN):
 | 
      
| 389 |             //PAN IS INVERTED
 | 
      ||
| 390 | 0d0f5ca1 | Simon Schulz | JointInterface::store_incoming_speed(ID_NECK_PAN, value, timestamp);  | 
      
| 391 | 8c6c1163 | Simon Schulz |             break;
 | 
      
| 392 | |||
| 393 | 7adf90be | Simon Schulz |         case(ICUB_ID_NECK_TILT):
 | 
      
| 394 | 8c6c1163 | Simon Schulz | JointInterface::store_incoming_speed(ID_NECK_TILT, value, timestamp);  | 
      
| 395 |             break;
 | 
      ||
| 396 | |||
| 397 | 7adf90be | Simon Schulz |         case(ICUB_ID_NECK_ROLL):
 | 
      
| 398 | 8c6c1163 | Simon Schulz | JointInterface::store_incoming_speed(ID_NECK_ROLL, value, timestamp);  | 
      
| 399 |             break;
 | 
      ||
| 400 | |||
| 401 | 7adf90be | Simon Schulz |         case(ICUB_ID_EYES_BOTH_UD):
 | 
      
| 402 | 8c6c1163 | Simon Schulz | JointInterface::store_incoming_speed(ID_EYES_BOTH_UD, value, timestamp);  | 
      
| 403 |             break;
 | 
      ||
| 404 | |||
| 405 |         //icub handles eyes differently, we have to set pan angle + vergence
 | 
      ||
| 406 | 7adf90be | Simon Schulz | case(ICUB_ID_EYES_PAN): {//pan  | 
      
| 407 | 8c6c1163 | Simon Schulz | last_vel_eye_pan = value;  | 
      
| 408 | float left = last_vel_eye_pan + last_vel_eye_vergence/2.0;  | 
      ||
| 409 | float right = last_vel_eye_pan - last_vel_eye_vergence/2.0;  | 
      ||
| 410 | |||
| 411 |             //printf("> eye: velocity pan=%3.2f vergence=%3.2f --> L=%3.2f R=%3.2f\n", last_vel_eye_pan, last_vel_eye_vergence, left, right);
 | 
      ||
| 412 | JointInterface::store_incoming_speed(ID_EYES_LEFT_LR, left, timestamp);  | 
      ||
| 413 | JointInterface::store_incoming_speed(ID_EYES_RIGHT_LR, right, timestamp);  | 
      ||
| 414 |             break;
 | 
      ||
| 415 | }  | 
      ||
| 416 | |||
| 417 | 7adf90be | Simon Schulz | case(ICUB_ID_EYES_VERGENCE): { //vergence  | 
      
| 418 | 8c6c1163 | Simon Schulz | last_vel_eye_pan = value;  | 
      
| 419 | float left = last_vel_eye_pan + last_vel_eye_vergence/2.0;  | 
      ||
| 420 | float right = last_vel_eye_pan - last_vel_eye_vergence/2.0;  | 
      ||
| 421 | |||
| 422 |             //printf("> eye: velocity pan=%3.2f vergence=%3.2f --> L=%3.2f R=%3.2f\n", last_vel_eye_pan, last_vel_eye_vergence, left, right);
 | 
      ||
| 423 | JointInterface::store_incoming_speed(ID_EYES_LEFT_LR, left, timestamp);  | 
      ||
| 424 | JointInterface::store_incoming_speed(ID_EYES_RIGHT_LR, right, timestamp);  | 
      ||
| 425 |             break;
 | 
      ||
| 426 | }  | 
      ||
| 427 | }  | 
      ||
| 428 | 7adf90be | Simon Schulz | }  | 
      
| 429 | 8c6c1163 | Simon Schulz | |
| 430 | 7adf90be | Simon Schulz | void iCubJointInterface::set_joint_enable_state(int e, bool enable) {  | 
      
| 431 | int icub_jointid = -1;  | 
      ||
| 432 | 8c6c1163 | Simon Schulz | |
| 433 | 7adf90be | Simon Schulz |     switch(e){
 | 
      
| 434 |         default:
 | 
      ||
| 435 |             break;
 | 
      ||
| 436 | 8c6c1163 | Simon Schulz | |
| 437 | 7adf90be | Simon Schulz |     case(ID_NECK_PAN):
 | 
      
| 438 | icub_jointid = ICUB_ID_NECK_PAN;  | 
      ||
| 439 |         break;
 | 
      ||
| 440 | 8c6c1163 | Simon Schulz | |
| 441 | 7adf90be | Simon Schulz |     case(ID_NECK_TILT):
 | 
      
| 442 | icub_jointid = ICUB_ID_NECK_TILT;  | 
      ||
| 443 |         break;
 | 
      ||
| 444 | 8c6c1163 | Simon Schulz | |
| 445 | 7adf90be | Simon Schulz |     case(ID_NECK_ROLL):
 | 
      
| 446 | icub_jointid = ICUB_ID_NECK_ROLL;  | 
      ||
| 447 |         break;
 | 
      ||
| 448 | |||
| 449 |     case(ID_EYES_BOTH_UD):
 | 
      ||
| 450 | icub_jointid = ICUB_ID_EYES_BOTH_UD;  | 
      ||
| 451 |         break;
 | 
      ||
| 452 | |||
| 453 |     // icub handles eyes as pan angle + vergence...
 | 
      ||
| 454 |     // -> hack: left eye enables pan and right eye enables vergence
 | 
      ||
| 455 |     case(ID_EYES_LEFT_LR):
 | 
      ||
| 456 | icub_jointid = ICUB_ID_EYES_PAN;  | 
      ||
| 457 |         break;
 | 
      ||
| 458 | |||
| 459 |     case(ID_EYES_RIGHT_LR):
 | 
      ||
| 460 | icub_jointid = ICUB_ID_EYES_VERGENCE;  | 
      ||
| 461 |         break;
 | 
      ||
| 462 | 8c6c1163 | Simon Schulz | }  | 
      
| 463 | |||
| 464 | 7adf90be | Simon Schulz | if (icub_jointid != -1) {  | 
      
| 465 |         if (enable) {
 | 
      ||
| 466 | amp->enableAmp(icub_jointid);  | 
      ||
| 467 | pid->enablePid(icub_jointid);  | 
      ||
| 468 |         } else {
 | 
      ||
| 469 | pid->disablePid(icub_jointid);  | 
      ||
| 470 | amp->disableAmp(icub_jointid);  | 
      ||
| 471 | }  | 
      ||
| 472 | }  | 
      ||
| 473 | 8c6c1163 | Simon Schulz | }  | 
      
| 474 | |||
| 475 | //! prepare and enable a joint
 | 
      ||
| 476 | //! NOTE: this should also prefill the min/max positions for this joint
 | 
      ||
| 477 | //! \param the enum id of a joint
 | 
      ||
| 478 | void iCubJointInterface::enable_joint(int e){  | 
      ||
| 479 | 7adf90be | Simon Schulz |     set_joint_enable_state(e, true);
 | 
      
| 480 | }  | 
      ||
| 481 | 8c6c1163 | Simon Schulz | |
| 482 | 7adf90be | Simon Schulz | //! shutdown and disable a joint
 | 
      
| 483 | //! \param the enum id of a joint
 | 
      ||
| 484 | void iCubJointInterface::disable_joint(int e){  | 
      ||
| 485 |     set_joint_enable_state(e, false);
 | 
      ||
| 486 | 8c6c1163 | Simon Schulz | }  | 
      
| 487 | |||
| 488 | void iCubJointInterface::store_min_max(IControlLimits *ilimits, int id, int e){  | 
      ||
| 489 |     double min, max;
 | 
      ||
| 490 | ilimits->getLimits(id, &min, &max);  | 
      ||
| 491 | joint_min[e] = min;  | 
      ||
| 492 | joint_max[e] = max;  | 
      ||
| 493 | }  | 
      ||
| 494 | |||
| 495 | //! initialise a joint (set up controller mode etc)
 | 
      ||
| 496 | //! \param joint enum
 | 
      ||
| 497 | void iCubJointInterface::init_joints(){
 | 
      ||
| 498 | 7adf90be | Simon Schulz | store_min_max(ilimits, ICUB_ID_NECK_TILT, ID_NECK_TILT);  | 
      
| 499 | store_min_max(ilimits, ICUB_ID_NECK_ROLL, ID_NECK_ROLL);  | 
      ||
| 500 | store_min_max(ilimits, ICUB_ID_NECK_PAN, ID_NECK_PAN);  | 
      ||
| 501 | store_min_max(ilimits, ICUB_ID_EYES_BOTH_UD, ID_EYES_BOTH_UD);  | 
      ||
| 502 | 8c6c1163 | Simon Schulz | |
| 503 |     //icub handles eyes differently, we have to set pan angle + vergence
 | 
      ||
| 504 |     double pan_min, pan_max, vergence_min, vergence_max;
 | 
      ||
| 505 | 7adf90be | Simon Schulz | ilimits->getLimits(ICUB_ID_EYES_PAN, &pan_min, &pan_max);  | 
      
| 506 | ilimits->getLimits(ICUB_ID_EYES_VERGENCE, &vergence_min, &vergence_max);  | 
      ||
| 507 | 8c6c1163 | Simon Schulz | |
| 508 |     //this is not 100% correct, should be fixed:
 | 
      ||
| 509 |     joint_min[ID_EYES_LEFT_LR] = pan_min; // - vergence_max/2;
 | 
      ||
| 510 |     joint_max[ID_EYES_LEFT_LR] = pan_max; // - vergence_max/2;
 | 
      ||
| 511 | joint_min[ID_EYES_RIGHT_LR] = joint_min[ID_EYES_LEFT_LR];  | 
      ||
| 512 | joint_max[ID_EYES_RIGHT_LR] = joint_max[ID_EYES_LEFT_LR];  | 
      ||
| 513 | |||
| 514 |     //eyelids:
 | 
      ||
| 515 | joint_min[ID_EYES_RIGHT_LID_UPPER] = -50; //24-30;  | 
      ||
| 516 | joint_max[ID_EYES_RIGHT_LID_UPPER] = 50; //48-30;  | 
      ||
| 517 | 0d0f5ca1 | Simon Schulz |     //lid_angle = joint_max[ID_EYES_RIGHT_LID_UPPER];
 | 
      
| 518 | 8c6c1163 | Simon Schulz | |
| 519 |     //eyebrows:
 | 
      ||
| 520 |     joint_min[ID_EYES_LEFT_BROW] = -50;
 | 
      ||
| 521 |     joint_max[ID_EYES_LEFT_BROW] = 50;
 | 
      ||
| 522 | joint_min[ID_EYES_RIGHT_BROW] = joint_min[ID_EYES_LEFT_BROW];  | 
      ||
| 523 | joint_max[ID_EYES_RIGHT_BROW] = joint_max[ID_EYES_LEFT_BROW];  | 
      ||
| 524 | |||
| 525 |     //mouth:
 | 
      ||
| 526 |     joint_min[ID_LIP_CENTER_UPPER] = 5;
 | 
      ||
| 527 |     joint_max[ID_LIP_CENTER_UPPER] = 50;
 | 
      ||
| 528 |     joint_min[ID_LIP_CENTER_LOWER] = 5;
 | 
      ||
| 529 |     joint_max[ID_LIP_CENTER_LOWER] = 50;
 | 
      ||
| 530 |     joint_min[ID_LIP_LEFT_UPPER] = 5;
 | 
      ||
| 531 |     joint_max[ID_LIP_LEFT_UPPER] = 50;
 | 
      ||
| 532 |     joint_min[ID_LIP_LEFT_LOWER] = 5;
 | 
      ||
| 533 |     joint_max[ID_LIP_LEFT_LOWER] = 50;
 | 
      ||
| 534 |     joint_min[ID_LIP_RIGHT_UPPER] = 5;
 | 
      ||
| 535 |     joint_max[ID_LIP_RIGHT_UPPER] = 50;
 | 
      ||
| 536 |     joint_min[ID_LIP_RIGHT_LOWER] = 5;
 | 
      ||
| 537 |     joint_max[ID_LIP_RIGHT_LOWER] = 50;
 | 
      ||
| 538 | |||
| 539 | |||
| 540 | }  |