Skip to content

Commit 0fb2b86

Browse files
committed
calculate fk_vel_recursive based on joint_trajectory_controller state for both desired and actual joint states
1 parent cf7f50b commit 0fb2b86

File tree

2 files changed

+80
-12
lines changed

2 files changed

+80
-12
lines changed

cob_twist_controller/package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<depend>cmake_modules</depend>
3232
<depend>cob_control_msgs</depend>
3333
<depend>cob_srvs</depend>
34+
<depend>control_msgs</depend>
3435
<depend>dynamic_reconfigure</depend>
3536
<depend>eigen_conversions</depend>
3637
<depend>eigen</depend>

cob_twist_controller/src/debug/debug_fk_vel_recursive_node.cpp

Lines changed: 79 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <ros/ros.h>
2222

2323
#include <sensor_msgs/JointState.h>
24+
#include <control_msgs/JointTrajectoryControllerState.h>
2425
#include <std_msgs/Float64.h>
2526
#include <geometry_msgs/Twist.h>
2627

@@ -37,10 +38,12 @@
3738
class DebugFkVelRecursive
3839
{
3940
ros::NodeHandle nh_;
40-
ros::Subscriber jointstate_sub_;
41+
ros::Subscriber joint_states_sub_;
42+
ros::Subscriber controller_state_sub_;
4143
ros::Publisher manipulability_pub_;
4244
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_;
4447

4548
std::string chain_base_link_;
4649
std::string chain_tip_link_;
@@ -97,25 +100,22 @@ class DebugFkVelRecursive
97100
p_jnt2jac_ = new KDL::ChainJntToJacSolver(chain_);
98101

99102
/// 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);
101105
manipulability_pub_ = nh_.advertise<std_msgs::Float64> ("debug/manipulability", 1);
102106
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);
104109

105110
return 0;
106111
}
107112

108-
void jointstateCallback(const sensor_msgs::JointState::ConstPtr& msg)
113+
void jointstatesCallback(const sensor_msgs::JointState::ConstPtr& msg)
109114
{
110115
KDL::JntArray q = KDL::JntArray(chain_.getNrOfJoints());
111116
KDL::JntArray q_dot = KDL::JntArray(chain_.getNrOfJoints());
112117

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
119119
for (unsigned int i = 0; i < chain_.getNrOfSegments(); i++)
120120
{
121121
KDL::Joint joint = chain_.getSegment(i).getJoint();
@@ -160,7 +160,7 @@ class DebugFkVelRecursive
160160
magnitude_msg.name.push_back(chain_.getSegment(i).getName());
161161
magnitude_msg.velocity.push_back(v_FrameVel[i].GetTwist().vel.Norm());
162162
}
163-
twist_magnitude_pub_.publish(magnitude_msg);
163+
twist_magnitude_actual_pub_.publish(magnitude_msg);
164164
}
165165

166166
/// compute manipulability
@@ -173,6 +173,73 @@ class DebugFkVelRecursive
173173
manipulability_msg.data = kappa;
174174
manipulability_pub_.publish(manipulability_msg);
175175
}
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+
176243
};
177244

178245

0 commit comments

Comments
 (0)