humotion / examples / meka / src / mekajointinterface.cpp @ 5fba4e15
History | View | Annotate | Download (7.524 KB)
1 | 2be6243f | Sebastian Meyer zu Borgsen | #include "mekajointinterface.h" |
---|---|---|---|
2 | 5e4bc615 | sschulz | |
3 | using std::cout;
|
||
4 | using std::cerr;
|
||
5 | |||
6 | using humotion::Timestamp;
|
||
7 | 2be6243f | Sebastian Meyer zu Borgsen | |
8 | //WARNING: DO NOT CHANGE THIS; VELOCITYMODE IS NOT YET IMPLEMENTED
|
||
9 | #define POSITION_CONTROL 1 |
||
10 | |||
11 | 6168b08c | Simon Schulz | void MekaJointInterface::incoming_controlstate(const m3meka_msgs::M3ControlStates &control_state){ |
12 | //incoming controller status
|
||
13 | for (unsigned int i = 0; i<control_state.group_name.size(); i++){ |
||
14 | if (control_state.group_name[i] == "head"){ |
||
15 | //enable/disable based on controller status
|
||
16 | if (control_state.state[i] == m3meka_msgs::M3ControlStates::START){
|
||
17 | //controller up and running
|
||
18 | if (!controller_enabled){
|
||
19 | printf("> incoming control state (%d), enabling jointstate output\n",control_state.state[i]);
|
||
20 | controller_enabled = true;
|
||
21 | } |
||
22 | }else{
|
||
23 | //controller in estop/stopped etc
|
||
24 | if (controller_enabled){
|
||
25 | printf("> incoming control state (%d), DISABLING jointstate output\n",control_state.state[i]);
|
||
26 | controller_enabled = false;
|
||
27 | } |
||
28 | } |
||
29 | } |
||
30 | } |
||
31 | } |
||
32 | |||
33 | 473a6a6c | Simon Schulz | void MekaJointInterface::incoming_jointstates(const sensor_msgs::JointState & msg){ |
34 | a4795834 | Simon Schulz | //fetch current timestamp
|
35 | 5e4bc615 | sschulz | Timestamp timestamp = Timestamp(msg.header.stamp.toSec()); |
36 | a4795834 | Simon Schulz | |
37 | //iterate through incoming joints and filter out joints we need:
|
||
38 | for(int i=0; i<msg.name.size(); i++){ |
||
39 | 6353defa | Simon Schulz | |
40 | 83864a21 | sschulz | std::string name = msg.name[i];
|
41 | 0b76d42b | Simon Schulz | //printf("incoming data for joint '%s'\n", name.c_str());
|
42 | a4795834 | Simon Schulz | |
43 | int id = -1; |
||
44 | e8e20dfe | Sebastian Meyer zu Borgsen | if (name == "head_j1"){ |
45 | a4795834 | Simon Schulz | id = ID_NECK_PAN; |
46 | e8e20dfe | Sebastian Meyer zu Borgsen | }else if(name == "head_j0"){ |
47 | a4795834 | Simon Schulz | id = ID_NECK_TILT; |
48 | } |
||
49 | |||
50 | //store data:
|
||
51 | if (id != -1){ |
||
52 | //printf("> storing joint data for joint id %d\n", id);
|
||
53 | 7115c3a4 | Simon Schulz | if (i >= msg.position.size()){
|
54 | 2c4bbc89 | Simon Schulz | printf("> joint state msg is missing position data for joint '%s'...\n", name.c_str());
|
55 | return;
|
||
56 | 7115c3a4 | Simon Schulz | } |
57 | if (i >= msg.velocity.size()){
|
||
58 | 2c4bbc89 | Simon Schulz | //printf("> joint state msg is missing velocity data for joint '%s'...\n", name.c_str());
|
59 | 3f8fb5a1 | Simon Schulz | //exit(EXIT_FAILURE);
|
60 | 2c4bbc89 | Simon Schulz | return;
|
61 | 7115c3a4 | Simon Schulz | } |
62 | //ok, safe to access data
|
||
63 | b0d48d96 | Sebastian Meyer zu Borgsen | if (id == ID_NECK_PAN){
|
64 | //joint is inverted
|
||
65 | JointInterface::store_incoming_position(id, -180.0 / M_PI * msg.position[i], timestamp); |
||
66 | ade7a207 | sschulz | JointInterface::store_incoming_velocity(id, -180.0 / M_PI * msg.velocity[i], timestamp); |
67 | b0d48d96 | Sebastian Meyer zu Borgsen | }else if (id == ID_NECK_TILT){ |
68 | JointInterface::store_incoming_position(id, 180.0 / M_PI * msg.position[i], timestamp); |
||
69 | ade7a207 | sschulz | JointInterface::store_incoming_velocity(id, 180.0 / M_PI * msg.velocity[i], timestamp); |
70 | b0d48d96 | Sebastian Meyer zu Borgsen | } |
71 | a4795834 | Simon Schulz | } |
72 | } |
||
73 | |||
74 | 6353defa | Simon Schulz | //dummy data uses current time
|
75 | 5e4bc615 | sschulz | timestamp = Timestamp::now(); |
76 | 6353defa | Simon Schulz | |
77 | a4795834 | Simon Schulz | //store dummy positions for joints we do not know about:
|
78 | store_dummy_data(ID_LIP_LEFT_UPPER, timestamp); |
||
79 | store_dummy_data(ID_LIP_LEFT_LOWER, timestamp); |
||
80 | store_dummy_data(ID_LIP_CENTER_UPPER, timestamp); |
||
81 | store_dummy_data(ID_LIP_CENTER_LOWER, timestamp); |
||
82 | store_dummy_data(ID_LIP_RIGHT_UPPER, timestamp); |
||
83 | store_dummy_data(ID_LIP_RIGHT_LOWER, timestamp); |
||
84 | |||
85 | store_dummy_data(ID_NECK_ROLL, timestamp); |
||
86 | store_dummy_data(ID_EYES_BOTH_UD, timestamp); |
||
87 | store_dummy_data(ID_EYES_LEFT_LR, timestamp); |
||
88 | store_dummy_data(ID_EYES_RIGHT_LR, timestamp); |
||
89 | store_dummy_data(ID_EYES_LEFT_LID_LOWER, timestamp); |
||
90 | store_dummy_data(ID_EYES_LEFT_LID_UPPER, timestamp); |
||
91 | store_dummy_data(ID_EYES_LEFT_BROW, timestamp); |
||
92 | store_dummy_data(ID_EYES_RIGHT_LID_LOWER, timestamp); |
||
93 | store_dummy_data(ID_EYES_RIGHT_LID_UPPER, timestamp); |
||
94 | store_dummy_data(ID_EYES_RIGHT_BROW, timestamp); |
||
95 | |||
96 | } |
||
97 | |||
98 | 5e4bc615 | sschulz | void MekaJointInterface::store_dummy_data(int id, Timestamp timestamp){ |
99 | fcc5e139 | Sebastian Meyer zu Borgsen | JointInterface::store_incoming_position(id, 0.0, timestamp); |
100 | ade7a207 | sschulz | JointInterface::store_incoming_velocity(id, 0.0, timestamp); |
101 | 473a6a6c | Simon Schulz | } |
102 | |||
103 | 2be6243f | Sebastian Meyer zu Borgsen | //! constructor
|
104 | 83864a21 | sschulz | MekaJointInterface::MekaJointInterface(std::string _input_scope, std::string control_scope, std::string _output_scope) : humotion::server::JointInterface(){ |
105 | e4bb9fd4 | Simon Schulz | input_scope = _input_scope; |
106 | output_scope = _output_scope; |
||
107 | 2be6243f | Sebastian Meyer zu Borgsen | |
108 | 6168b08c | Simon Schulz | controller_enabled = false;
|
109 | |||
110 | 473a6a6c | Simon Schulz | //subscribe to meka joint states
|
111 | 99ebae32 | Simon Schulz | int argc = 0; |
112 | ros::init(argc, (char**)NULL, "meka_humotion"); |
||
113 | 473a6a6c | Simon Schulz | ros::NodeHandle n; |
114 | 2be6243f | Sebastian Meyer zu Borgsen | |
115 | e4bb9fd4 | Simon Schulz | printf("> listening on jointstates on '%s'\n",input_scope.c_str());
|
116 | 6168b08c | Simon Schulz | joint_state_subscriber = n.subscribe(input_scope, 150, &MekaJointInterface::incoming_jointstates, this); |
117 | |||
118 | printf("> listening on controlstates on '%s'\n",control_scope.c_str());
|
||
119 | control_state_subscriber = n.subscribe(control_scope, 150, &MekaJointInterface::incoming_controlstate, this); |
||
120 | 2be6243f | Sebastian Meyer zu Borgsen | |
121 | e4bb9fd4 | Simon Schulz | printf("> sending targets on '%s'\n", output_scope.c_str());
|
122 | target_publisher = n.advertise<trajectory_msgs::JointTrajectory>(output_scope, 100);
|
||
123 | 2be6243f | Sebastian Meyer zu Borgsen | |
124 | a4795834 | Simon Schulz | //tell humotion about min/max joint values:
|
125 | init_joints(); |
||
126 | 2be6243f | Sebastian Meyer zu Borgsen | } |
127 | |||
128 | //! destructor
|
||
129 | MekaJointInterface::~MekaJointInterface(){ |
||
130 | } |
||
131 | |||
132 | 473a6a6c | Simon Schulz | |
133 | |||
134 | void MekaJointInterface::run(){
|
||
135 | a4795834 | Simon Schulz | ros::spin(); |
136 | 473a6a6c | Simon Schulz | } |
137 | |||
138 | //! set the target position of a joint
|
||
139 | //! \param enum id of joint
|
||
140 | //! \param float value
|
||
141 | e50b15da | Simon Schulz | void MekaJointInterface::publish_target(int e, float position, float velocity){ |
142 | 6353defa | Simon Schulz | //we do this in execute motion for all joints at once...
|
143 | 473a6a6c | Simon Schulz | } |
144 | |||
145 | |||
146 | //! actually execute the scheduled motion commands
|
||
147 | void MekaJointInterface::execute_motion(){
|
||
148 | 6168b08c | Simon Schulz | if (controller_enabled){
|
149 | //build msg
|
||
150 | trajectory_msgs::JointTrajectory msg; |
||
151 | msg.joint_names.push_back("head_j0");
|
||
152 | msg.joint_names.push_back("head_j1");
|
||
153 | 473a6a6c | Simon Schulz | |
154 | 6168b08c | Simon Schulz | trajectory_msgs::JointTrajectoryPoint p; |
155 | e50b15da | Simon Schulz | p.positions.push_back(get_target_position(ID_NECK_TILT) * M_PI / 180.0); |
156 | 6168b08c | Simon Schulz | //pan joint is inverted!
|
157 | e50b15da | Simon Schulz | p.positions.push_back(-get_target_position(ID_NECK_PAN) * M_PI / 180.0); |
158 | 6168b08c | Simon Schulz | //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]);
|
159 | 473a6a6c | Simon Schulz | |
160 | 6168b08c | Simon Schulz | p.time_from_start = ros::Duration(1.2 * 1.0 / humotion::server::Server::MOTION_UPDATERATE); |
161 | 473a6a6c | Simon Schulz | |
162 | 6168b08c | Simon Schulz | msg.points.push_back(p); |
163 | 473a6a6c | Simon Schulz | |
164 | 6168b08c | Simon Schulz | target_publisher.publish(msg); |
165 | } |
||
166 | 473a6a6c | Simon Schulz | } |
167 | |||
168 | |||
169 | //! prepare and enable a joint
|
||
170 | //! NOTE: this should also prefill the min/max positions for this joint
|
||
171 | //! \param the enum id of a joint
|
||
172 | void MekaJointInterface::enable_joint(int e){ |
||
173 | ddccf279 | Simon Schulz | //meka does not support this, joints are always enabled
|
174 | 473a6a6c | Simon Schulz | } |
175 | |||
176 | //! shutdown and disable a joint
|
||
177 | //! \param the enum id of a joint
|
||
178 | void MekaJointInterface::disable_joint(int e){ |
||
179 | ddccf279 | Simon Schulz | //meka does not support this, joints are always enabled
|
180 | 473a6a6c | Simon Schulz | } |
181 | |||
182 | a4795834 | Simon Schulz | void MekaJointInterface::store_min_max(int id, float min, float max){ |
183 | joint_min[id] = min; |
||
184 | joint_max[id] = max; |
||
185 | } |
||
186 | |||
187 | void MekaJointInterface::init_joints(){
|
||
188 | f10ec142 | Sebastian Meyer zu Borgsen | store_min_max(ID_NECK_TILT, -37, 1); |
189 | store_min_max(ID_NECK_PAN, -70, 70); |
||
190 | a4795834 | Simon Schulz | |
191 | store_min_max(ID_NECK_ROLL, -1, 1); |
||
192 | store_min_max(ID_EYES_BOTH_UD, -1, 1); |
||
193 | store_min_max(ID_EYES_LEFT_LR, -1, 1); |
||
194 | store_min_max(ID_EYES_RIGHT_LR, -1, 1); |
||
195 | store_min_max(ID_EYES_LEFT_LID_UPPER, -1, 1); |
||
196 | store_min_max(ID_EYES_LEFT_LID_LOWER, -1, 1); |
||
197 | store_min_max(ID_EYES_RIGHT_LID_UPPER, -1, 1); |
||
198 | store_min_max(ID_EYES_RIGHT_LID_LOWER, -1, 1); |
||
199 | store_min_max(ID_EYES_LEFT_BROW, -1, 1); |
||
200 | store_min_max(ID_EYES_RIGHT_BROW, -1, 1); |
||
201 | store_min_max(ID_LIP_CENTER_UPPER, -1, 1); |
||
202 | store_min_max(ID_LIP_CENTER_LOWER, -1, 1); |
||
203 | store_min_max(ID_LIP_LEFT_UPPER, -1, 1); |
||
204 | store_min_max(ID_LIP_LEFT_LOWER, -1, 1); |
||
205 | store_min_max(ID_LIP_RIGHT_UPPER, -1, 1); |
||
206 | store_min_max(ID_LIP_RIGHT_LOWER, -1, 1); |
||
207 | } |