Statistics
| Branch: | Tag: | Revision:

humotion / examples / meka / src / mekajointinterface.cpp @ 6353defa

History | View | Annotate | Download (7.042 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
            JointInterface::store_incoming_position(id, msg.position[i], timestamp);
38
            JointInterface::store_incoming_speed(   id, msg.velocity[i], timestamp);
39
        }
40
    }
41

    
42
    //dummy data uses current time
43
    timestamp = get_timestamp_s();
44

    
45
    //store dummy positions for joints we do not know about:
46
    store_dummy_data(ID_LIP_LEFT_UPPER, timestamp);
47
    store_dummy_data(ID_LIP_LEFT_LOWER, timestamp);
48
    store_dummy_data(ID_LIP_CENTER_UPPER, timestamp);
49
    store_dummy_data(ID_LIP_CENTER_LOWER, timestamp);
50
    store_dummy_data(ID_LIP_RIGHT_UPPER, timestamp);
51
    store_dummy_data(ID_LIP_RIGHT_LOWER, timestamp);
52

    
53
    store_dummy_data(ID_NECK_ROLL, timestamp);
54
    store_dummy_data(ID_EYES_BOTH_UD, timestamp);
55
    store_dummy_data(ID_EYES_LEFT_LR, timestamp);
56
    store_dummy_data(ID_EYES_RIGHT_LR, timestamp);
57
    store_dummy_data(ID_EYES_LEFT_LID_LOWER, timestamp);
58
    store_dummy_data(ID_EYES_LEFT_LID_UPPER, timestamp);
59
    store_dummy_data(ID_EYES_LEFT_BROW, timestamp);
60
    store_dummy_data(ID_EYES_RIGHT_LID_LOWER, timestamp);
61
    store_dummy_data(ID_EYES_RIGHT_LID_UPPER, timestamp);
62
    store_dummy_data(ID_EYES_RIGHT_BROW, timestamp);
63

    
64
}
65

    
66
void MekaJointInterface::store_dummy_data(int id, double timestamp){
67
    JointInterface::store_incoming_position(id, 0.0, timestamp);
68
    JointInterface::store_incoming_speed(id, 0.0, timestamp);
69
}
70

    
71
//! constructor
72
MekaJointInterface::MekaJointInterface(string _input_scope, string _output_scope) : humotion::server::JointInterface(){
73
    input_scope = _input_scope;
74
    output_scope = _output_scope;
75

    
76
    //subscribe to meka joint states
77
    int argc = 0;
78
    ros::init(argc, (char**)NULL, "meka_humotion");
79
    ros::NodeHandle n;
80

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

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

    
87
    //tell humotion about min/max joint values:
88
    init_joints();
89
}
90

    
91
//! destructor
92
MekaJointInterface::~MekaJointInterface(){
93
}
94

    
95

    
96

    
97
void MekaJointInterface::run(){
98
   //iCubDataReceiver *data_receiver = new iCubDataReceiver(10/*1000.0 / MAIN_LOOP_FREQUENCY*/, iencs, this);
99
   //data_receiver->start();
100
   ros::spin();
101
}
102

    
103

    
104
double MekaJointInterface::get_timestamp_s(){
105
    struct timespec spec;
106
    clock_gettime(CLOCK_REALTIME, &spec);
107
    return spec.tv_sec + spec.tv_nsec / 1.0e9;
108
}
109

    
110
//! set the target position of a joint
111
//! \param enum id of joint
112
//! \param float value
113
void MekaJointInterface::publish_target_position(int e){
114
   //we do this in execute motion for all joints at once...
115
}
116

    
117

    
118
//! actually execute the scheduled motion commands
119
void MekaJointInterface::execute_motion(){
120
    //build msg
121
    trajectory_msgs::JointTrajectory msg;
122
    msg.joint_names.push_back("head_j0");
123
    msg.joint_names.push_back("head_j1");
124

    
125
    trajectory_msgs::JointTrajectoryPoint p;
126
    p.positions.push_back(joint_target[ID_NECK_TILT] * M_PI / 180.0);
127
    p.positions.push_back(joint_target[ID_NECK_PAN] * M_PI / 180.0);
128

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

    
131
    msg.points.push_back(p);
132

    
133
    target_publisher.publish(msg);
134

    
135
    /*
136
     void MekaJointInterface::store_min_max(int id, float min, float max){
137
header:
138
  seq: 636
139
  stamp:
140
    secs: 0
141
    nsecs: 0
142
  frame_id: ''
143
joint_names: ['head_j0', 'head_j1']
144
points:
145
  -
146
    positions: [-0.31, 0.01954768762234005]
147
    velocities: []
148
    accelerations: []
149
    effort: []
150
    time_from_start:
151
      secs: 1
152
      nsecs: 0
153

154
     */
155

    
156
    #if 0
157
    // set up neck and eye motion commands:
158
    if (POSITION_CONTROL){
159
        //position control
160
        for(int i=ICUB_ID_NECK_TILT; i<=ICUB_ID_EYES_VERGENCE; i++){
161
            set_target_in_positionmode(i, target_angle[i]);
162
        }
163
    }else{
164
        //velocity control
165
        for(int i=ICUB_ID_NECK_TILT; i<=ICUB_ID_EYES_VERGENCE; i++){
166
            set_target_in_velocitymode(i, target_angle[i]);
167
        }
168
    }
169
    //printf("> TARGET PAN = %3.2f\n",target_angle[ICUB_ID_NECK_PAN]);
170

171

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

175
    //eyebrows are set using a special command as well:
176
    set_eyebrow_angle(ICUB_ID_EYES_LEFT_BROW);
177
    set_eyebrow_angle(ICUB_ID_EYES_RIGHT_BROW);
178

179
    //mouth
180
    set_mouth();
181

182
    #endif
183
}
184

185

186
//! prepare and enable a joint
187
//! NOTE: this should also prefill the min/max positions for this joint
188
//! \param the enum id of a joint
189
void MekaJointInterface::enable_joint(int e){
190
    //meka does not support this, joints are always enabled
191
}
192

193
//! shutdown and disable a joint
194
//! \param the enum id of a joint
195
void MekaJointInterface::disable_joint(int e){
196
    //meka does not support this, joints are always enabled
197
}
198

199
void MekaJointInterface::store_min_max(int id, float min, float max){
200
    joint_min[id] = min;
201
    joint_max[id] = max;
202
}
203

204
void MekaJointInterface::init_joints(){
205
    store_min_max(ID_NECK_TILT, -10, 10);
206
    store_min_max(ID_NECK_PAN, -10, 10);
207

208
    store_min_max(ID_NECK_ROLL, -1, 1);
209
    store_min_max(ID_EYES_BOTH_UD, -1, 1);
210
    store_min_max(ID_EYES_LEFT_LR, -1, 1);
211
    store_min_max(ID_EYES_RIGHT_LR, -1, 1);
212
    store_min_max(ID_EYES_LEFT_LID_UPPER, -1, 1);
213
    store_min_max(ID_EYES_LEFT_LID_LOWER, -1, 1);
214
    store_min_max(ID_EYES_RIGHT_LID_UPPER, -1, 1);
215
    store_min_max(ID_EYES_RIGHT_LID_LOWER, -1, 1);
216
    store_min_max(ID_EYES_LEFT_BROW, -1, 1);
217
    store_min_max(ID_EYES_RIGHT_BROW, -1, 1);
218
    store_min_max(ID_LIP_CENTER_UPPER, -1, 1);
219
    store_min_max(ID_LIP_CENTER_LOWER, -1, 1);
220
    store_min_max(ID_LIP_LEFT_UPPER, -1, 1);
221
    store_min_max(ID_LIP_LEFT_LOWER, -1, 1);
222
    store_min_max(ID_LIP_RIGHT_UPPER, -1, 1);
223
    store_min_max(ID_LIP_RIGHT_LOWER, -1, 1);
224
}
225

226