Statistics
| Branch: | Tag: | Revision:

humotion / examples / meka / src / mekajointinterface.cpp @ 32327f15

History | View | Annotate | Download (7.617 KB)

1
#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
void MekaJointInterface::incoming_jointstates(const sensor_msgs::JointState & msg){
8
    //fetch current timestamp
9
    double timestamp = msg.header.stamp.toSec();
10

    
11
    //iterate through incoming joints and filter out joints we need:
12
    for(int i=0; i<msg.name.size(); i++){
13

    
14
        string name = msg.name[i];
15
        //printf("incoming data for joint '%s'\n", name.c_str());
16

    
17
        int id = -1;
18
        if (name == "head_j1"){
19
            id = ID_NECK_PAN;
20
        }else if(name == "head_j0"){
21
            id = ID_NECK_TILT;
22
        }
23

    
24
        //store data:
25
        if (id != -1){
26
            //printf("> storing joint data for joint id %d\n", id);
27
            if (i >= msg.position.size()){
28
                printf("> joint state msg is missing position data for joint '%s'...\n", name.c_str());
29
                return;
30
            }
31
            if (i >= msg.velocity.size()){
32
                //printf("> joint state msg is missing velocity data for joint '%s'...\n", name.c_str());
33
                //exit(EXIT_FAILURE);
34
                return;
35
            }
36
            //ok, safe to access data
37
            if (id == ID_NECK_PAN){
38
                //joint is inverted
39
                JointInterface::store_incoming_position(id, -180.0 / M_PI * msg.position[i], timestamp);
40
                JointInterface::store_incoming_speed(   id, -180.0 / M_PI * msg.velocity[i], timestamp);
41
            }else if (id == ID_NECK_TILT){
42
                JointInterface::store_incoming_position(id, 180.0 / M_PI * msg.position[i], timestamp);
43
                JointInterface::store_incoming_speed(   id, 180.0 / M_PI * msg.velocity[i], timestamp);
44
            }
45
        }
46
    }
47

    
48
    //dummy data uses current time
49
    timestamp = get_timestamp_s();
50

    
51
    //store dummy positions for joints we do not know about:
52
    store_dummy_data(ID_LIP_LEFT_UPPER, timestamp);
53
    store_dummy_data(ID_LIP_LEFT_LOWER, timestamp);
54
    store_dummy_data(ID_LIP_CENTER_UPPER, timestamp);
55
    store_dummy_data(ID_LIP_CENTER_LOWER, timestamp);
56
    store_dummy_data(ID_LIP_RIGHT_UPPER, timestamp);
57
    store_dummy_data(ID_LIP_RIGHT_LOWER, timestamp);
58

    
59
    store_dummy_data(ID_NECK_ROLL, timestamp);
60
    store_dummy_data(ID_EYES_BOTH_UD, timestamp);
61
    store_dummy_data(ID_EYES_LEFT_LR, timestamp);
62
    store_dummy_data(ID_EYES_RIGHT_LR, timestamp);
63
    store_dummy_data(ID_EYES_LEFT_LID_LOWER, timestamp);
64
    store_dummy_data(ID_EYES_LEFT_LID_UPPER, timestamp);
65
    store_dummy_data(ID_EYES_LEFT_BROW, timestamp);
66
    store_dummy_data(ID_EYES_RIGHT_LID_LOWER, timestamp);
67
    store_dummy_data(ID_EYES_RIGHT_LID_UPPER, timestamp);
68
    store_dummy_data(ID_EYES_RIGHT_BROW, timestamp);
69

    
70
}
71

    
72
void MekaJointInterface::store_dummy_data(int id, double timestamp){
73
    JointInterface::store_incoming_position(id, 0.0, timestamp);
74
    JointInterface::store_incoming_speed(id, 0.0, timestamp);
75
}
76

    
77
//! constructor
78
MekaJointInterface::MekaJointInterface(string _input_scope, string _output_scope) : humotion::server::JointInterface(){
79
    input_scope = _input_scope;
80
    output_scope = _output_scope;
81

    
82
    //subscribe to meka joint states
83
    int argc = 0;
84
    ros::init(argc, (char**)NULL, "meka_humotion");
85
    ros::NodeHandle n;
86

    
87
    printf("> listening on jointstates on '%s'\n",input_scope.c_str());
88
    joint_state_subscriber = n.subscribe(input_scope, 150, &MekaJointInterface::incoming_jointstates , this);
89

    
90
    printf("> sending targets on '%s'\n", output_scope.c_str());
91
    target_publisher = n.advertise<trajectory_msgs::JointTrajectory>(output_scope, 100);
92

    
93
    //tell humotion about min/max joint values:
94
    init_joints();
95
}
96

    
97
//! destructor
98
MekaJointInterface::~MekaJointInterface(){
99
}
100

    
101

    
102

    
103
void MekaJointInterface::run(){
104
   //iCubDataReceiver *data_receiver = new iCubDataReceiver(10/*1000.0 / MAIN_LOOP_FREQUENCY*/, iencs, this);
105
   //data_receiver->start();
106
   ros::spin();
107
}
108

    
109

    
110
double MekaJointInterface::get_timestamp_s(){
111
    struct timespec spec;
112
    clock_gettime(CLOCK_REALTIME, &spec);
113
    return spec.tv_sec + spec.tv_nsec / 1.0e9;
114
}
115

    
116
//! set the target position of a joint
117
//! \param enum id of joint
118
//! \param float value
119
void MekaJointInterface::publish_target_position(int e){
120
   //we do this in execute motion for all joints at once...
121
}
122

    
123

    
124
//! actually execute the scheduled motion commands
125
void MekaJointInterface::execute_motion(){
126
    //build msg
127
    trajectory_msgs::JointTrajectory msg;
128
    msg.joint_names.push_back("head_j0");
129
    msg.joint_names.push_back("head_j1");
130

    
131
    trajectory_msgs::JointTrajectoryPoint p;
132
    p.positions.push_back(joint_target[ID_NECK_TILT] * M_PI / 180.0);
133
    //pan joint is inverted!
134
    p.positions.push_back(-joint_target[ID_NECK_PAN] * M_PI / 180.0);
135
    //printf("targets pan=%4.1f tilt=%4.1f (eye p %4.1f t %4.2f)\n",joint_target[ID_NECK_TILT],joint_target[ID_NECK_PAN],joint_target[ID_EYES_LEFT_LR],joint_target[ID_EYES_BOTH_UD]);
136

    
137
    p.time_from_start = ros::Duration(1.2 * 1.0 / humotion::server::Server::MOTION_UPDATERATE);
138

    
139
    msg.points.push_back(p);
140

    
141
    target_publisher.publish(msg);
142

    
143
    /*
144
     void MekaJointInterface::store_min_max(int id, float min, float max){
145
header:
146
  seq: 636
147
  stamp:
148
    secs: 0
149
    nsecs: 0
150
  frame_id: ''
151
joint_names: ['head_j0', 'head_j1']
152
points:
153
  -
154
    positions: [-0.31, 0.01954768762234005]
155
    velocities: []
156
    accelerations: []
157
    effort: []
158
    time_from_start:
159
      secs: 1
160
      nsecs: 0
161

162
     */
163

    
164
    #if 0
165
    // set up neck and eye motion commands:
166
    if (POSITION_CONTROL){
167
        //position control
168
        for(int i=ICUB_ID_NECK_TILT; i<=ICUB_ID_EYES_VERGENCE; i++){
169
            set_target_in_positionmode(i, target_angle[i]);
170
        }
171
    }else{
172
        //velocity control
173
        for(int i=ICUB_ID_NECK_TILT; i<=ICUB_ID_EYES_VERGENCE; i++){
174
            set_target_in_velocitymode(i, target_angle[i]);
175
        }
176
    }
177
    //printf("> TARGET PAN = %3.2f\n",target_angle[ICUB_ID_NECK_PAN]);
178

179

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

183
    //eyebrows are set using a special command as well:
184
    set_eyebrow_angle(ICUB_ID_EYES_LEFT_BROW);
185
    set_eyebrow_angle(ICUB_ID_EYES_RIGHT_BROW);
186

187
    //mouth
188
    set_mouth();
189

190
    #endif
191
}
192

193

194
//! prepare and enable a joint
195
//! NOTE: this should also prefill the min/max positions for this joint
196
//! \param the enum id of a joint
197
void MekaJointInterface::enable_joint(int e){
198
    //meka does not support this, joints are always enabled
199
}
200

201
//! shutdown and disable a joint
202
//! \param the enum id of a joint
203
void MekaJointInterface::disable_joint(int e){
204
    //meka does not support this, joints are always enabled
205
}
206

207
void MekaJointInterface::store_min_max(int id, float min, float max){
208
    joint_min[id] = min;
209
    joint_max[id] = max;
210
}
211

212
void MekaJointInterface::init_joints(){
213
    store_min_max(ID_NECK_TILT, -37, 1);
214
    store_min_max(ID_NECK_PAN, -70, 70);
215

216
    store_min_max(ID_NECK_ROLL, -1, 1);
217
    store_min_max(ID_EYES_BOTH_UD, -1, 1);
218
    store_min_max(ID_EYES_LEFT_LR, -1, 1);
219
    store_min_max(ID_EYES_RIGHT_LR, -1, 1);
220
    store_min_max(ID_EYES_LEFT_LID_UPPER, -1, 1);
221
    store_min_max(ID_EYES_LEFT_LID_LOWER, -1, 1);
222
    store_min_max(ID_EYES_RIGHT_LID_UPPER, -1, 1);
223
    store_min_max(ID_EYES_RIGHT_LID_LOWER, -1, 1);
224
    store_min_max(ID_EYES_LEFT_BROW, -1, 1);
225
    store_min_max(ID_EYES_RIGHT_BROW, -1, 1);
226
    store_min_max(ID_LIP_CENTER_UPPER, -1, 1);
227
    store_min_max(ID_LIP_CENTER_LOWER, -1, 1);
228
    store_min_max(ID_LIP_LEFT_UPPER, -1, 1);
229
    store_min_max(ID_LIP_LEFT_LOWER, -1, 1);
230
    store_min_max(ID_LIP_RIGHT_UPPER, -1, 1);
231
    store_min_max(ID_LIP_RIGHT_LOWER, -1, 1);
232
}
233

234