Statistics
| Branch: | Tag: | Revision:

humotion / examples / meka / src / mekajointinterface.cpp @ 3f8fb5a1

History | View | Annotate | Download (7.832 KB)

1 2be6243f Sebastian Meyer zu Borgsen
#include "mekajointinterface.h"
2
using namespace std;
3
4
//WARNING: DO NOT CHANGE THIS; VELOCITYMODE IS NOT YET IMPLEMENTED
5
#define POSITION_CONTROL 1
6
7 473a6a6c Simon Schulz
void MekaJointInterface::incoming_jointstates(const sensor_msgs::JointState & msg){
8 a4795834 Simon Schulz
    //fetch current timestamp
9
    double timestamp = get_timestamp_ms();
10
11
    //iterate through incoming joints and filter out joints we need:
12
    for(int i=0; i<msg.name.size(); i++){
13
        string name = msg.name[i];
14 0b76d42b Simon Schulz
        //printf("incoming data for joint '%s'\n", name.c_str());
15 a4795834 Simon Schulz
16
        int id = -1;
17 e8e20dfe Sebastian Meyer zu Borgsen
        if (name == "head_j1"){
18 a4795834 Simon Schulz
            id = ID_NECK_PAN;
19 e8e20dfe Sebastian Meyer zu Borgsen
        }else if(name == "head_j0"){
20 a4795834 Simon Schulz
            id = ID_NECK_TILT;
21
        }
22
23
        //store data:
24
        if (id != -1){
25
            //printf("> storing joint data for joint id %d\n", id);
26 7115c3a4 Simon Schulz
            if (i >= msg.position.size()){
27 3f8fb5a1 Simon Schulz
                printf("\r> msg is missing position data for joint %s...", name.c_str());
28
                //exit(EXIT_FAILURE);
29 7115c3a4 Simon Schulz
            }
30
            if (i >= msg.velocity.size()){
31 3f8fb5a1 Simon Schulz
                printf("\r> msg is missing velocity data for joint %s...", name.c_str());
32
                //exit(EXIT_FAILURE);
33 7115c3a4 Simon Schulz
            }
34
            //ok, safe to access data
35 a4795834 Simon Schulz
            JointInterface::store_incoming_position(id, msg.position[i], timestamp);
36
            JointInterface::store_incoming_speed(   id, msg.velocity[i], timestamp);
37
        }
38
    }
39
40
    //store dummy positions for joints we do not know about:
41
    store_dummy_data(ID_LIP_LEFT_UPPER, timestamp);
42
    store_dummy_data(ID_LIP_LEFT_LOWER, timestamp);
43
    store_dummy_data(ID_LIP_CENTER_UPPER, timestamp);
44
    store_dummy_data(ID_LIP_CENTER_LOWER, timestamp);
45
    store_dummy_data(ID_LIP_RIGHT_UPPER, timestamp);
46
    store_dummy_data(ID_LIP_RIGHT_LOWER, timestamp);
47
48
    store_dummy_data(ID_NECK_ROLL, timestamp);
49
    store_dummy_data(ID_EYES_BOTH_UD, timestamp);
50
    store_dummy_data(ID_EYES_LEFT_LR, timestamp);
51
    store_dummy_data(ID_EYES_RIGHT_LR, timestamp);
52
    store_dummy_data(ID_EYES_LEFT_LID_LOWER, timestamp);
53
    store_dummy_data(ID_EYES_LEFT_LID_UPPER, timestamp);
54
    store_dummy_data(ID_EYES_LEFT_BROW, timestamp);
55
    store_dummy_data(ID_EYES_RIGHT_LID_LOWER, timestamp);
56
    store_dummy_data(ID_EYES_RIGHT_LID_UPPER, timestamp);
57
    store_dummy_data(ID_EYES_RIGHT_BROW, timestamp);
58
59
}
60
61
void MekaJointInterface::store_dummy_data(int id, double timestamp){
62
    JointInterface::store_incoming_position(id, 0.0, timestamp);
63
    JointInterface::store_incoming_speed(id, 0.0, timestamp);
64 473a6a6c Simon Schulz
}
65
66 2be6243f Sebastian Meyer zu Borgsen
//! constructor
67 e4bb9fd4 Simon Schulz
MekaJointInterface::MekaJointInterface(string _input_scope, string _output_scope) : humotion::server::JointInterface(){
68
    input_scope = _input_scope;
69
    output_scope = _output_scope;
70 2be6243f Sebastian Meyer zu Borgsen
71 473a6a6c Simon Schulz
    //subscribe to meka joint states
72 99ebae32 Simon Schulz
    int argc = 0;
73
    ros::init(argc, (char**)NULL, "meka_humotion");
74 473a6a6c Simon Schulz
    ros::NodeHandle n;
75 2be6243f Sebastian Meyer zu Borgsen
76 e4bb9fd4 Simon Schulz
    printf("> listening on jointstates on '%s'\n",input_scope.c_str());
77
    joint_state_subscriber = n.subscribe(input_scope, 150, &MekaJointInterface::incoming_jointstates , this);
78 2be6243f Sebastian Meyer zu Borgsen
79 e4bb9fd4 Simon Schulz
    printf("> sending targets on '%s'\n", output_scope.c_str());
80
    target_publisher = n.advertise<trajectory_msgs::JointTrajectory>(output_scope, 100);
81 2be6243f Sebastian Meyer zu Borgsen
82 a4795834 Simon Schulz
    //tell humotion about min/max joint values:
83
    init_joints();
84 2be6243f Sebastian Meyer zu Borgsen
}
85
86
//! destructor
87
MekaJointInterface::~MekaJointInterface(){
88
}
89
90 473a6a6c Simon Schulz
91
92
void MekaJointInterface::run(){
93
   //iCubDataReceiver *data_receiver = new iCubDataReceiver(10/*1000.0 / MAIN_LOOP_FREQUENCY*/, iencs, this);
94
   //data_receiver->start();
95 a4795834 Simon Schulz
   ros::spin();
96 473a6a6c Simon Schulz
}
97
98
99 a4795834 Simon Schulz
double MekaJointInterface::get_timestamp_ms(){
100
    struct timespec spec;
101
    clock_gettime(CLOCK_REALTIME, &spec);
102
    return spec.tv_sec*1000 + spec.tv_nsec / 1.0e6;
103
}
104
105 473a6a6c Simon Schulz
//! set the target position of a joint
106
//! \param enum id of joint
107
//! \param float value
108
void MekaJointInterface::publish_target_position(int e){
109
    #if 0
110
    //first: convert humotion enum to our enum:
111
    int id = convert_enum_to_motorid(e);
112
    if (id == -1){
113
        return; //we are not interested in that data, so we just return here
114
    }
115

116
    if (id == ICUB_ID_NECK_PAN){
117
        //PAN seems to be swapped
118
        store_joint(ICUB_ID_NECK_PAN, -joint_target[e]);
119
    }else if ((id == ICUB_ID_EYES_LEFT_LR) || ( id == ICUB_ID_EYES_RIGHT_LR)){
120
        //icub handles eyes differently, we have to set pan angle + vergence
121
        float pan      = (joint_target[ID_EYES_LEFT_LR] + joint_target[ID_EYES_RIGHT_LR]) / 2;
122
        float vergence = (joint_target[ID_EYES_LEFT_LR]  - joint_target[ID_EYES_RIGHT_LR]);
123
        //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);
124

125
        store_joint(ICUB_ID_EYES_PAN, pan);
126
        store_joint(ICUB_ID_EYES_VERGENCE, vergence);
127
    }else{
128
        store_joint(id, joint_target[e]);
129
    }
130
    #endif
131
}
132

133

134
//! actually execute the scheduled motion commands
135
void MekaJointInterface::execute_motion(){
136 a4795834 Simon Schulz
    //build msg
137
    trajectory_msgs::JointTrajectory msg;
138
    msg.joint_names.push_back("head_j0");
139
    msg.joint_names.push_back("head_j1");
140

141 e8e20dfe Sebastian Meyer zu Borgsen
    trajectory_msgs::JointTrajectoryPoint p;
142
    p.positions.push_back(joint_target[ID_NECK_TILT] * M_PI / 180.0);
143
    p.positions.push_back(joint_target[ID_NECK_PAN] * M_PI / 180.0);
144 a4795834 Simon Schulz

145 e8e20dfe Sebastian Meyer zu Borgsen
    p.time_from_start = ros::Duration(1.2 * 1.0 / humotion::server::Server::MOTION_UPDATERATE);
146 a4795834 Simon Schulz

147 e8e20dfe Sebastian Meyer zu Borgsen
    msg.points.push_back(p);
148 a4795834 Simon Schulz

149
    target_publisher.publish(msg);
150

151
    /*
152
     void MekaJointInterface::store_min_max(int id, float min, float max){
153
header:
154
  seq: 636
155
  stamp:
156
    secs: 0
157
    nsecs: 0
158
  frame_id: ''
159
joint_names: ['head_j0', 'head_j1']
160
points:
161
  -
162
    positions: [-0.31, 0.01954768762234005]
163
    velocities: []
164
    accelerations: []
165
    effort: []
166
    time_from_start:
167
      secs: 1
168
      nsecs: 0
169

170
     */
171

172 473a6a6c Simon Schulz
    #if 0
173
    // set up neck and eye motion commands:
174
    if (POSITION_CONTROL){
175
        //position control
176
        for(int i=ICUB_ID_NECK_TILT; i<=ICUB_ID_EYES_VERGENCE; i++){
177
            set_target_in_positionmode(i, target_angle[i]);
178
        }
179
    }else{
180
        //velocity control
181
        for(int i=ICUB_ID_NECK_TILT; i<=ICUB_ID_EYES_VERGENCE; i++){
182
            set_target_in_velocitymode(i, target_angle[i]);
183
        }
184
    }
185
    //printf("> TARGET PAN = %3.2f\n",target_angle[ICUB_ID_NECK_PAN]);
186

187

188
    //eyelids: unfortuantely the icub has only 1dof for eyelids, so we use only one dof here:
189
    set_eyelid_angle(target_angle[ICUB_ID_EYES_RIGHT_LID_UPPER]);
190

191
    //eyebrows are set using a special command as well:
192
    set_eyebrow_angle(ICUB_ID_EYES_LEFT_BROW);
193
    set_eyebrow_angle(ICUB_ID_EYES_RIGHT_BROW);
194

195
    //mouth
196
    set_mouth();
197

198
    #endif
199
}
200

201

202
//! prepare and enable a joint
203
//! NOTE: this should also prefill the min/max positions for this joint
204
//! \param the enum id of a joint
205
void MekaJointInterface::enable_joint(int e){
206 ddccf279 Simon Schulz
    //meka does not support this, joints are always enabled
207 473a6a6c Simon Schulz
}
208

209
//! shutdown and disable a joint
210
//! \param the enum id of a joint
211
void MekaJointInterface::disable_joint(int e){
212 ddccf279 Simon Schulz
    //meka does not support this, joints are always enabled
213 473a6a6c Simon Schulz
}
214

215 a4795834 Simon Schulz
void MekaJointInterface::store_min_max(int id, float min, float max){
216
    joint_min[id] = min;
217
    joint_max[id] = max;
218
}
219

220
void MekaJointInterface::init_joints(){
221
    store_min_max(ID_NECK_TILT, -10, 10);
222
    store_min_max(ID_NECK_PAN, -10, 10);
223

224
    store_min_max(ID_NECK_ROLL, -1, 1);
225
    store_min_max(ID_EYES_BOTH_UD, -1, 1);
226
    store_min_max(ID_EYES_LEFT_LR, -1, 1);
227
    store_min_max(ID_EYES_RIGHT_LR, -1, 1);
228
    store_min_max(ID_EYES_LEFT_LID_UPPER, -1, 1);
229
    store_min_max(ID_EYES_LEFT_LID_LOWER, -1, 1);
230
    store_min_max(ID_EYES_RIGHT_LID_UPPER, -1, 1);
231
    store_min_max(ID_EYES_RIGHT_LID_LOWER, -1, 1);
232
    store_min_max(ID_EYES_LEFT_BROW, -1, 1);
233
    store_min_max(ID_EYES_RIGHT_BROW, -1, 1);
234
    store_min_max(ID_LIP_CENTER_UPPER, -1, 1);
235
    store_min_max(ID_LIP_CENTER_LOWER, -1, 1);
236
    store_min_max(ID_LIP_LEFT_UPPER, -1, 1);
237
    store_min_max(ID_LIP_LEFT_LOWER, -1, 1);
238
    store_min_max(ID_LIP_RIGHT_UPPER, -1, 1);
239
    store_min_max(ID_LIP_RIGHT_LOWER, -1, 1);
240
}
241