RosMsgTreeIkTrajectoryFactory.cpp
Go to the documentation of this file.
00001 #include <robodyn_controllers/RosMsgTreeIkTrajectoryFactory.h>
00002 
00003 boost::shared_ptr<RosMsgJointTrajectory> RosMsgTreeIkTrajectoryFactory::getTrajectory(const sensor_msgs::JointState& jointPositions,
00004         const sensor_msgs::JointState& jointVels,
00005         const sensor_msgs::JointState& prevJointVels,
00006         const r2_msgs::PoseState& poseState,
00007         const r2_msgs::PoseState& poseVels,
00008         const r2_msgs::PoseTrajectory& goalTraj) const
00009 {
00010     boost::shared_ptr<RosMsgJointTrajectory> trajPtr(new RosMsgJointTrajectory);
00011     trajPtr->setGoalId(goalTraj.header.stamp, goalTraj.header.frame_id);
00012     std::vector<std::string> jointNames;
00013     treeIk->getJointNames(jointNames);
00014     trajPtr->setJointNames(jointNames);
00015 
00016     boost::shared_ptr<TreeIkTrajectory> ikTraj(new TreeIkTrajectory);
00017     ikTraj->setTreeIk(treeIk);
00018 
00019     // get startJoints
00020     KDL::JntArrayVel startVel(jointNames.size());
00021     KDL::JntArrayVel prevVel(jointNames.size());
00022     KDL::JntArrayAcc startJoints(jointNames.size());
00023 
00024     // these can throw but we want to leave the catch up to the caller
00025     RosMsgConverter::JointStateToJntArray(jointPositions, jointNames, startJoints.q);
00026     RosMsgConverter::JointStateToJntArrayVel(jointVels, jointNames, startVel);
00027     RosMsgConverter::JointStateToJntArrayVel(prevJointVels, jointNames, prevVel);
00028 
00029     startJoints.qdot = startVel.qdot;
00030     ros::Duration elapsedTime(jointVels.header.stamp - prevJointVels.header.stamp);
00031 
00032     if (elapsedTime > ros::Duration(0.))
00033     {
00034         KDL::Subtract(startVel.qdot, prevVel.qdot, startJoints.qdotdot);
00035         KDL::Divide(startJoints.qdotdot, elapsedTime.toSec(), startJoints.qdotdot);
00036     }
00037 
00038     // get refPoses
00039     std::vector<KDL::FrameAcc> refPoses(goalTraj.nodes.size());
00040 
00041     for (unsigned int i = 0; i < goalTraj.nodes.size(); ++i)
00042     {
00043         // these can throw but we want to leave the catch up to the caller
00044         if (goalTraj.refFrames.size() == 1)
00045         {
00046             RosMsgConverter::PoseStateToFrameAcc(poseState, treeIk->getBaseName(), goalTraj.refFrames[0], refPoses[i]);
00047         }
00048         else
00049         {
00050             RosMsgConverter::PoseStateToFrameAcc(poseState, treeIk->getBaseName(), goalTraj.refFrames[i], refPoses[i]);
00051         }
00052     }
00053 
00054     // get startPose
00055     std::vector<KDL::FrameAcc> startPose(goalTraj.nodes.size());
00056     KDL::Frame                 frame;
00057 
00058     for (unsigned int i = 0; i < goalTraj.nodes.size(); ++i)
00059     {
00060 
00061         // these can throw but we want to leave the catch up to the caller
00062         RosMsgConverter::PoseStateToFrameAcc(poseVels, treeIk->getBaseName(), goalTraj.nodes[i], startPose[i]);
00063         RosMsgConverter::PoseStateToFrame(poseState, treeIk->getBaseName(), goalTraj.nodes[i], frame);
00064 
00065         startPose[i].M.R = frame.M;
00066         startPose[i].p.p = frame.p;
00067     }
00068 
00069     // get goalPoses
00070     std::vector<std::vector<KDL::FrameAcc> > goalPoses(goalTraj.points.size(), std::vector<KDL::FrameAcc>(goalTraj.nodes.size()));
00071     std::vector<double> durationTargets(goalTraj.points.size(), -1.);
00072 
00073     for (unsigned int i = 0; i < goalPoses.size(); ++i)
00074     {
00075         if (goalTraj.points[i].positions.size() != goalTraj.nodes.size())
00076         {
00077             std::stringstream err;
00078             err << "RosMsgTreeIkTrajectoryFactory::getTrajectory() - A goal trajectory pose has mismatched size for nodes list";
00079             NasaCommonLogging::Logger::log("gov.nasa.controllers.RosMsgTreeIkTrajectoryFactory", log4cpp::Priority::ERROR, err.str());
00080             throw std::runtime_error(err.str());
00081         }
00082 
00083         for (unsigned int j = 0; j < goalTraj.nodes.size(); ++j)
00084         {
00085             goalPoses[i][j].p.p.x(goalTraj.points[i].positions[j].position.x);
00086             goalPoses[i][j].p.p.y(goalTraj.points[i].positions[j].position.y);
00087             goalPoses[i][j].p.p.z(goalTraj.points[i].positions[j].position.z);
00088             goalPoses[i][j].M.R = KDL::Rotation::Quaternion(goalTraj.points[i].positions[j].orientation.x,
00089                                   goalTraj.points[i].positions[j].orientation.y,
00090                                   goalTraj.points[i].positions[j].orientation.z,
00091                                   goalTraj.points[i].positions[j].orientation.w);
00092 
00093             // velocity
00094             if (goalTraj.points[i].velocities.size() == goalTraj.nodes.size())
00095             {
00096                 goalPoses[i][j].p.v.x(goalTraj.points[i].velocities[j].linear.x);
00097                 goalPoses[i][j].p.v.y(goalTraj.points[i].velocities[j].linear.y);
00098                 goalPoses[i][j].p.v.z(goalTraj.points[i].velocities[j].linear.z);
00099                 goalPoses[i][j].M.w.x(goalTraj.points[i].velocities[j].angular.x);
00100                 goalPoses[i][j].M.w.y(goalTraj.points[i].velocities[j].angular.y);
00101                 goalPoses[i][j].M.w.z(goalTraj.points[i].velocities[j].angular.z);
00102             }
00103 
00104             // acceleration
00105             if (goalTraj.points[i].accelerations.size() == goalTraj.nodes.size())
00106             {
00107                 goalPoses[i][j].p.dv.x(goalTraj.points[i].accelerations[j].linear.x);
00108                 goalPoses[i][j].p.dv.y(goalTraj.points[i].accelerations[j].linear.y);
00109                 goalPoses[i][j].p.dv.z(goalTraj.points[i].accelerations[j].linear.z);
00110                 goalPoses[i][j].M.dw.x(goalTraj.points[i].accelerations[j].angular.x);
00111                 goalPoses[i][j].M.dw.y(goalTraj.points[i].accelerations[j].angular.y);
00112                 goalPoses[i][j].M.dw.z(goalTraj.points[i].accelerations[j].angular.z);
00113             }
00114 
00115             goalPoses[i][j] = refPoses[j] * goalPoses[i][j];
00116         }
00117 
00118         durationTargets[i] = goalTraj.points[i].time_from_start.toSec();
00119     }
00120 
00121     // get priorities
00122     std::vector<KdlTreeIk::NodePriority> priorities(goalTraj.nodes.size());
00123 
00124     if (goalTraj.node_priorities.size() != 0 && goalTraj.node_priorities.size() != goalTraj.nodes.size())
00125     {
00126         std::stringstream err;
00127         err << "invalid node_priorities size";
00128         NasaCommonLogging::Logger::log("gov.nasa.controllers.RosMsgTreeIkTrajectoryFactory", log4cpp::Priority::ERROR, err.str());
00129         throw std::runtime_error(err.str());
00130     }
00131 
00132     for (unsigned int nodeIndex = 0; nodeIndex < goalTraj.node_priorities.size(); ++nodeIndex)
00133     {
00134         if (goalTraj.node_priorities[nodeIndex].axis_priorities.size() == 1)
00135         {
00136             for (unsigned int axisIndex = 0; axisIndex < 6; ++axisIndex)
00137             {
00138                 priorities[nodeIndex][axisIndex] = static_cast<int>(goalTraj.node_priorities[nodeIndex].axis_priorities[0]);
00139             }
00140         }
00141         else
00142         {
00143             if (goalTraj.node_priorities[nodeIndex].axis_priorities.size() != 6)
00144             {
00145                 std::stringstream err;
00146                 err << "invalid axis_priorities size at node_priorities[" << nodeIndex << "]";
00147                 NasaCommonLogging::Logger::log("gov.nasa.controllers.RosMsgTreeIkTrajectoryFactory", log4cpp::Priority::ERROR, err.str());
00148                 throw std::runtime_error(err.str());
00149             }
00150 
00151             for (unsigned int axisIndex = 0; axisIndex < 6; ++axisIndex)
00152             {
00153                 priorities[nodeIndex][axisIndex] = static_cast<int>(goalTraj.node_priorities[nodeIndex].axis_priorities[axisIndex]);
00154             }
00155         }
00156     }
00157 
00158     ikTraj->setCartesianTrajectory(sequenceFactory->getTrajectory(startPose, goalPoses, durationTargets));
00159     ikTraj->setInitialJoints(startJoints);
00160     ikTraj->setNodeNames(goalTraj.nodes);
00161     ikTraj->setNodePriorities(priorities);
00162 
00163     trajPtr->setTrajectory(ikTraj);
00164 
00165     return trajPtr;
00166 }


robodyn_controllers
Author(s):
autogenerated on Sat Jun 8 2019 20:20:53