21
21
#include < ros/ros.h>
22
22
23
23
#include < sensor_msgs/JointState.h>
24
+ #include < control_msgs/JointTrajectoryControllerState.h>
24
25
#include < std_msgs/Float64.h>
25
26
#include < geometry_msgs/Twist.h>
26
27
37
38
class DebugFkVelRecursive
38
39
{
39
40
ros::NodeHandle nh_;
40
- ros::Subscriber jointstate_sub_;
41
+ ros::Subscriber joint_states_sub_;
42
+ ros::Subscriber controller_state_sub_;
41
43
ros::Publisher manipulability_pub_;
42
44
ros::Publisher twist_current_pub_;
43
- ros::Publisher twist_magnitude_pub_;
45
+ ros::Publisher twist_magnitude_desired_pub_;
46
+ ros::Publisher twist_magnitude_actual_pub_;
44
47
45
48
std::string chain_base_link_;
46
49
std::string chain_tip_link_;
@@ -97,25 +100,22 @@ class DebugFkVelRecursive
97
100
p_jnt2jac_ = new KDL::ChainJntToJacSolver (chain_);
98
101
99
102
// / initialize ROS interfaces
100
- jointstate_sub_ = nh_.subscribe (" joint_states" , 1 , &DebugFkVelRecursive::jointstateCallback, this );
103
+ // joint_states_sub_ = nh_.subscribe("joint_states", 1, &DebugFkVelRecursive::jointstatesCallback, this); // analyzing controller_state instead
104
+ controller_state_sub_ = nh_.subscribe (" joint_trajectory_controller/state" , 1 , &DebugFkVelRecursive::controllerstateCallback, this );
101
105
manipulability_pub_ = nh_.advertise <std_msgs::Float64> (" debug/manipulability" , 1 );
102
106
twist_current_pub_ = nh_.advertise <geometry_msgs::Twist> (" debug/twist_current" , 1 );
103
- twist_magnitude_pub_ = nh_.advertise <sensor_msgs::JointState> (" debug/twist_magnitude" , 1 );
107
+ twist_magnitude_desired_pub_ = nh_.advertise <sensor_msgs::JointState> (" debug/twist_magnitude/desired" , 1 );
108
+ twist_magnitude_actual_pub_ = nh_.advertise <sensor_msgs::JointState> (" debug/twist_magnitude/actual" , 1 );
104
109
105
110
return 0 ;
106
111
}
107
112
108
- void jointstateCallback (const sensor_msgs::JointState::ConstPtr& msg)
113
+ void jointstatesCallback (const sensor_msgs::JointState::ConstPtr& msg)
109
114
{
110
115
KDL::JntArray q = KDL::JntArray (chain_.getNrOfJoints ());
111
116
KDL::JntArray q_dot = KDL::JntArray (chain_.getNrOfJoints ());
112
117
113
- // //TODO: sort wrt kinematic order
114
- // for (unsigned int i = 0; i < msg->name.size(); i++)
115
- // {
116
- // q(i) = msg->position[i];
117
- // q_dot(i) = msg->velocity[i];
118
- // }
118
+ // / extract q, q_dot from JointStates
119
119
for (unsigned int i = 0 ; i < chain_.getNrOfSegments (); i++)
120
120
{
121
121
KDL::Joint joint = chain_.getSegment (i).getJoint ();
@@ -160,7 +160,7 @@ class DebugFkVelRecursive
160
160
magnitude_msg.name .push_back (chain_.getSegment (i).getName ());
161
161
magnitude_msg.velocity .push_back (v_FrameVel[i].GetTwist ().vel .Norm ());
162
162
}
163
- twist_magnitude_pub_ .publish (magnitude_msg);
163
+ twist_magnitude_actual_pub_ .publish (magnitude_msg);
164
164
}
165
165
166
166
// / compute manipulability
@@ -173,6 +173,73 @@ class DebugFkVelRecursive
173
173
manipulability_msg.data = kappa;
174
174
manipulability_pub_.publish (manipulability_msg);
175
175
}
176
+
177
+ void controllerstateCallback (const control_msgs::JointTrajectoryControllerState::ConstPtr& msg)
178
+ {
179
+ KDL::JntArray q_desired = KDL::JntArray (chain_.getNrOfJoints ());
180
+ KDL::JntArray q_desired_dot = KDL::JntArray (chain_.getNrOfJoints ());
181
+ KDL::JntArray q_actual = KDL::JntArray (chain_.getNrOfJoints ());
182
+ KDL::JntArray q_actual_dot = KDL::JntArray (chain_.getNrOfJoints ());
183
+
184
+ for (unsigned int i = 0 ; i < chain_.getNrOfSegments (); i++)
185
+ {
186
+ KDL::Joint joint = chain_.getSegment (i).getJoint ();
187
+ std::string joint_name = joint.getName ();
188
+ if (joint.getType () == KDL::Joint::None)
189
+ {
190
+ ROS_DEBUG_STREAM (" Skip fixed Joint '" << joint_name);
191
+ continue ;
192
+ }
193
+ else
194
+ {
195
+ if (std::find (msg->joint_names .begin (), msg->joint_names .end (), joint_name) != msg->joint_names .end ())
196
+ {
197
+ unsigned int index = std::distance (msg->joint_names .begin (), std::find (msg->joint_names .begin (), msg->joint_names .end (), joint_name));
198
+ q_desired (index) = msg->desired .positions [index];
199
+ q_desired_dot (index) = msg->desired .velocities [index];
200
+ q_actual (index) = msg->actual .positions [index];
201
+ q_actual_dot (index) = msg->actual .velocities [index];
202
+ }
203
+ else
204
+ {
205
+ ROS_ERROR_STREAM (" Joint '" << joint_name << " ' not found in JointTrajectoryControllerState" );
206
+ return ;
207
+ }
208
+ }
209
+ }
210
+
211
+ // / compute desired twists recursively
212
+ std::vector< KDL::FrameVel > v_FrameVel_desired;
213
+ KDL::JntArrayVel jntArrayVel_desired = KDL::JntArrayVel (q_desired, q_desired_dot);
214
+ v_FrameVel_desired.resize (chain_.getNrOfSegments ());
215
+ if (p_fksolver_vel_->JntToCart (jntArrayVel_desired, v_FrameVel_desired, chain_.getNrOfSegments ()) >= 0 )
216
+ {
217
+ sensor_msgs::JointState magnitude_desired_msg;
218
+ magnitude_desired_msg.header .stamp = msg->header .stamp ;
219
+ for (unsigned int i = 0 ; i < chain_.getNrOfSegments (); i++)
220
+ {
221
+ magnitude_desired_msg.name .push_back (chain_.getSegment (i).getName ());
222
+ magnitude_desired_msg.velocity .push_back (v_FrameVel_desired[i].GetTwist ().vel .Norm ());
223
+ }
224
+ twist_magnitude_desired_pub_.publish (magnitude_desired_msg);
225
+ }
226
+ // / compute actual twists recursively
227
+ std::vector< KDL::FrameVel > v_FrameVel_actual;
228
+ KDL::JntArrayVel jntArrayVel_actual = KDL::JntArrayVel (q_actual, q_actual_dot);
229
+ v_FrameVel_actual.resize (chain_.getNrOfSegments ());
230
+ if (p_fksolver_vel_->JntToCart (jntArrayVel_actual, v_FrameVel_actual, chain_.getNrOfSegments ()) >= 0 )
231
+ {
232
+ sensor_msgs::JointState magnitude_actual_msg;
233
+ magnitude_actual_msg.header .stamp = msg->header .stamp ;
234
+ for (unsigned int i = 0 ; i < chain_.getNrOfSegments (); i++)
235
+ {
236
+ magnitude_actual_msg.name .push_back (chain_.getSegment (i).getName ());
237
+ magnitude_actual_msg.velocity .push_back (v_FrameVel_actual[i].GetTwist ().vel .Norm ());
238
+ }
239
+ twist_magnitude_actual_pub_.publish (magnitude_actual_msg);
240
+ }
241
+ }
242
+
176
243
};
177
244
178
245
0 commit comments