humotion / examples / meka / src / mekajointinterface.cpp @ b0d48d96
History | View | Annotate | Download (7.588 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 |
p.positions.push_back(joint_target[ID_NECK_PAN] * M_PI / 180.0); |
134 |
//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]);
|
135 |
|
136 |
p.time_from_start = ros::Duration(1.2 * 1.0 / humotion::server::Server::MOTION_UPDATERATE); |
137 |
|
138 |
msg.points.push_back(p); |
139 |
|
140 |
target_publisher.publish(msg); |
141 |
|
142 |
/*
|
143 |
void MekaJointInterface::store_min_max(int id, float min, float max){
|
144 |
header:
|
145 |
seq: 636
|
146 |
stamp:
|
147 |
secs: 0
|
148 |
nsecs: 0
|
149 |
frame_id: ''
|
150 |
joint_names: ['head_j0', 'head_j1']
|
151 |
points:
|
152 |
-
|
153 |
positions: [-0.31, 0.01954768762234005]
|
154 |
velocities: []
|
155 |
accelerations: []
|
156 |
effort: []
|
157 |
time_from_start:
|
158 |
secs: 1
|
159 |
nsecs: 0
|
160 |
|
161 |
*/
|
162 |
|
163 |
#if 0
|
164 |
// set up neck and eye motion commands:
|
165 |
if (POSITION_CONTROL){
|
166 |
//position control
|
167 |
for(int i=ICUB_ID_NECK_TILT; i<=ICUB_ID_EYES_VERGENCE; i++){
|
168 |
set_target_in_positionmode(i, target_angle[i]);
|
169 |
}
|
170 |
}else{
|
171 |
//velocity control
|
172 |
for(int i=ICUB_ID_NECK_TILT; i<=ICUB_ID_EYES_VERGENCE; i++){
|
173 |
set_target_in_velocitymode(i, target_angle[i]);
|
174 |
}
|
175 |
}
|
176 |
//printf("> TARGET PAN = %3.2f\n",target_angle[ICUB_ID_NECK_PAN]);
|
177 |
|
178 |
|
179 |
//eyelids: unfortuantely the icub has only 1dof for eyelids, so we use only one dof here:
|
180 |
set_eyelid_angle(target_angle[ICUB_ID_EYES_RIGHT_LID_UPPER]);
|
181 |
|
182 |
//eyebrows are set using a special command as well:
|
183 |
set_eyebrow_angle(ICUB_ID_EYES_LEFT_BROW);
|
184 |
set_eyebrow_angle(ICUB_ID_EYES_RIGHT_BROW);
|
185 |
|
186 |
//mouth
|
187 |
set_mouth();
|
188 |
|
189 |
#endif
|
190 |
}
|
191 |
|
192 |
|
193 |
//! prepare and enable a joint
|
194 |
//! NOTE: this should also prefill the min/max positions for this joint
|
195 |
//! \param the enum id of a joint
|
196 |
void MekaJointInterface::enable_joint(int e){
|
197 |
//meka does not support this, joints are always enabled
|
198 |
}
|
199 |
|
200 |
//! shutdown and disable a joint
|
201 |
//! \param the enum id of a joint
|
202 |
void MekaJointInterface::disable_joint(int e){
|
203 |
//meka does not support this, joints are always enabled
|
204 |
}
|
205 |
|
206 |
void MekaJointInterface::store_min_max(int id, float min, float max){
|
207 |
joint_min[id] = min;
|
208 |
joint_max[id] = max;
|
209 |
}
|
210 |
|
211 |
void MekaJointInterface::init_joints(){
|
212 |
store_min_max(ID_NECK_TILT, -18, 1);
|
213 |
store_min_max(ID_NECK_PAN, -56, 56);
|
214 |
|
215 |
store_min_max(ID_NECK_ROLL, -1, 1);
|
216 |
store_min_max(ID_EYES_BOTH_UD, -1, 1);
|
217 |
store_min_max(ID_EYES_LEFT_LR, -1, 1);
|
218 |
store_min_max(ID_EYES_RIGHT_LR, -1, 1);
|
219 |
store_min_max(ID_EYES_LEFT_LID_UPPER, -1, 1);
|
220 |
store_min_max(ID_EYES_LEFT_LID_LOWER, -1, 1);
|
221 |
store_min_max(ID_EYES_RIGHT_LID_UPPER, -1, 1);
|
222 |
store_min_max(ID_EYES_RIGHT_LID_LOWER, -1, 1);
|
223 |
store_min_max(ID_EYES_LEFT_BROW, -1, 1);
|
224 |
store_min_max(ID_EYES_RIGHT_BROW, -1, 1);
|
225 |
store_min_max(ID_LIP_CENTER_UPPER, -1, 1);
|
226 |
store_min_max(ID_LIP_CENTER_LOWER, -1, 1);
|
227 |
store_min_max(ID_LIP_LEFT_UPPER, -1, 1);
|
228 |
store_min_max(ID_LIP_LEFT_LOWER, -1, 1);
|
229 |
store_min_max(ID_LIP_RIGHT_UPPER, -1, 1);
|
230 |
store_min_max(ID_LIP_RIGHT_LOWER, -1, 1);
|
231 |
}
|
232 |
|
233 |
|