Go to the documentation of this file.00001 #include "robodyn_controllers/RosMsgTreeFk.h"
00002
00003 RosMsgTreeFk::RosMsgTreeFk()
00004 : KdlTreeFk()
00005 {
00006 }
00007
00008 RosMsgTreeFk::~RosMsgTreeFk()
00009 {
00010 }
00011
00012 void RosMsgTreeFk::initialize()
00013 {
00014 KdlTreeFk::initialize();
00015
00016 KdlTreeFk::getJointNames(jointNames);
00017 joints.resize(jointNames.size());
00018 poseMap.clear();
00019
00020 for (KDL::SegmentMap::const_iterator it = tree.getSegments().begin(); it != tree.getSegments().end(); ++it)
00021 {
00022 poseMap.insert(std::make_pair(it->first, KDL::FrameVel()));
00023 }
00024 }
00025
00026 bool RosMsgTreeFk::getPoseState(const sensor_msgs::JointState& jointState, r2_msgs::PoseState& poseState)
00027 {
00028 try
00029 {
00031 RosMsgConverter::JointStateToJntArrayVel(jointState, jointNames, joints);
00032
00034 KdlTreeFk::getVelocities(joints, poseMap);
00035
00037 poseState.header.stamp = jointState.header.stamp;
00038 poseState.header.frame_id = getBaseName();
00039 poseState.name.resize(poseMap.size());
00040 poseState.positions.resize(poseMap.size());
00041 poseState.velocities.resize(poseMap.size());
00042 poseState.accelerations.resize(poseMap.size());
00043
00044 bool clearAcc = true;
00045 unsigned int index = 0;
00046
00047
00048 for (std::map<std::string, KDL::FrameVel>::const_iterator it = poseMap.begin(); it != poseMap.end(); ++it)
00049 {
00050 poseState.name[index] = it->first;
00051
00052 geometry_msgs::Pose& pose = poseState.positions[index];
00053 pose.position.x = it->second.p.p.x();
00054 pose.position.y = it->second.p.p.y();
00055 pose.position.z = it->second.p.p.z();
00056 it->second.M.R.GetQuaternion(pose.orientation.x, pose.orientation.y, pose.orientation.z, pose.orientation.w);
00057
00058
00059 geometry_msgs::Twist& vel = poseState.velocities[index];
00060 vel.linear.x = it->second.p.v.x();
00061 vel.linear.y = it->second.p.v.y();
00062 vel.linear.z = it->second.p.v.z();
00063 vel.angular.x = it->second.M.w.x();
00064 vel.angular.y = it->second.M.w.y();
00065 vel.angular.z = it->second.M.w.z();
00066
00067
00068 prevVel_type::iterator prevIt = prevVel.find(it->first);
00069 clearAcc = true;
00070
00071 geometry_msgs::Twist& acc = poseState.accelerations[index];
00072
00073 if (prevIt != prevVel.end())
00074 {
00075
00076 double elapsedTime = (poseState.header.stamp - prevIt->second.first).toSec();
00077
00078 if (elapsedTime > 0.)
00079 {
00080
00081 acc.linear.x = (vel.linear.x - prevIt->second.second.linear.x) / elapsedTime;
00082 acc.linear.y = (vel.linear.y - prevIt->second.second.linear.y) / elapsedTime;
00083 acc.linear.z = (vel.linear.z - prevIt->second.second.linear.z) / elapsedTime;
00084 acc.angular.x = (vel.angular.x - prevIt->second.second.angular.x) / elapsedTime;
00085 acc.angular.y = (vel.angular.y - prevIt->second.second.angular.y) / elapsedTime;
00086 acc.angular.z = (vel.angular.z - prevIt->second.second.angular.z) / elapsedTime;
00087 clearAcc = false;
00088 }
00089
00090
00091 prevIt->second.first = poseState.header.stamp;
00092 prevIt->second.second = vel;
00093 }
00094 else
00095 {
00096
00097 std::pair<ros::Time, geometry_msgs::Twist>& val = prevVel[it->first];
00098 val.first = poseState.header.stamp;
00099 val.second = vel;
00100 }
00101
00102 if (clearAcc)
00103 {
00104 acc.linear.x = 0.;
00105 acc.linear.y = 0.;
00106 acc.linear.z = 0.;
00107 acc.angular.x = 0.;
00108 acc.angular.y = 0.;
00109 acc.angular.z = 0.;
00110 }
00111
00112 ++index;
00113 }
00114
00115 return true;
00116 }
00117 catch (std::exception& e)
00118 {
00119 return false;
00120 }
00121 }