RobotInfo.cpp
Go to the documentation of this file.
00001 #include <convenience_ros_functions/RobotInfo.h>
00002 #include <convenience_ros_functions/ROSFunctions.h>
00003 
00004 using convenience_ros_functions::RobotInfo;
00005 using convenience_ros_functions::ROSFunctions;
00006 
00007 RobotInfo::RobotInfo(const std::string& _robot_pose_topic,
00008     const std::string& _joint_states_topic):
00009     robot_pose_topic(_robot_pose_topic),
00010     joint_states_topic(_joint_states_topic)
00011 {}
00012 
00013 RobotInfo::~RobotInfo(){}
00014 
00015 geometry_msgs::PoseStamped RobotInfo::getCurrentRobotPose(ros::NodeHandle& n) {
00016     return getCurrentRobotPose(robot_pose_topic,n);
00017 }
00018 
00019 geometry_msgs::PoseStamped RobotInfo::getCurrentRobotPose(const std::string& topicName, ros::NodeHandle& n) {
00020     geometry_msgs::PoseWithCovarianceStamped p=getCurrentRobotPoseWithCovariance(topicName,n);
00021     geometry_msgs::PoseStamped ret;
00022     ret.header=p.header;
00023     ret.pose=p.pose.pose;
00024     return ret;
00025 }
00026 
00027 geometry_msgs::PoseWithCovarianceStamped RobotInfo::getCurrentRobotPoseWithCovariance(const std::string& topicName, ros::NodeHandle& n) {
00028     ros::Subscriber jsub = n.subscribe(topicName, 1000, &RobotInfo::poseCallback,this);
00029     //ROS_DEBUG("Waiting for robot pos to come in.");
00030     // Spin on subscription
00031     poseMutex.lock();
00032     received_rp=false; 
00033     poseMutex.unlock();
00034 
00035     ros::Duration spinWait(0.05);
00036     while (!receivedRobotPose()) {
00037         ros::spinOnce();
00038         spinWait.sleep();
00039     }
00040 
00041     //ROS_DEBUG("Received pose.");
00042     poseMutex.lock();
00043     geometry_msgs::PoseWithCovarianceStamped p=pose;
00044     poseMutex.unlock();
00045     return p;
00046 }
00047 
00048 geometry_msgs::PoseWithCovarianceStamped RobotInfo::getCurrentRobotPoseWithCovariance(ros::NodeHandle& n) {
00049     return getCurrentRobotPoseWithCovariance(robot_pose_topic,n);
00050 }
00051         
00052 sensor_msgs::JointState RobotInfo::getCurrentJointState(const std::string& topicName, ros::NodeHandle& n){
00053     jointStateMutex.lock();
00054     received_js=false;
00055     jointStateMutex.unlock();
00056 
00057     ros::Subscriber jsub = n.subscribe(topicName, 10, &RobotInfo::jointStateCallback,this);
00058     //ROS_INFO("Waiting until current joint state arrives...");
00059 
00060     ros::Duration spinWait(0.05);
00061     while (!receivedJointState()) {
00062         ros::spinOnce();
00063         spinWait.sleep();
00064     }
00065     
00066     //ROS_INFO("Joint state received.");
00067 
00068     jointStateMutex.lock();
00069     sensor_msgs::JointState js=jointState;
00070     jointStateMutex.unlock();
00071     return js;
00072 }
00073 
00074 sensor_msgs::JointState RobotInfo::getCurrentJointState(ros::NodeHandle& n){
00075     return getCurrentJointState(joint_states_topic,n);
00076 }
00077 
00078 
00079 
00080 sensor_msgs::MultiDOFJointState RobotInfo::getVirtualJointState(const geometry_msgs::PoseStamped& robotPose,
00081     const std::string& virtualJointName, const std::string& frame_id) {
00082 
00083     //ROS_INFO_STREAM("Getting virtual joint state for robot pose "<<robotPose);
00084 
00085     geometry_msgs::PoseStamped _robotPose=robotPose;    
00086     _robotPose.header.stamp=ros::Time(0); //get the earliest possible transform, so set the time to 0
00087     if (_robotPose.header.frame_id!=frame_id) {
00088         int ret=ROSFunctions::Singleton()->transformPose(_robotPose,frame_id,_robotPose,2,true);
00089         if (ret!=0){
00090             ROS_WARN("getVirtualJointState(): Failed to transform robot pose into %s.",frame_id.c_str());
00091         }
00092     }
00093 
00094 
00095     sensor_msgs::MultiDOFJointState j;
00096     j.header=_robotPose.header;
00097     //j.header.stamp=ros::Time::now();
00098     //j.header.frame_id=_robotPose.header.frame_id;
00099     j.joint_names.push_back(virtualJointName);
00100     geometry_msgs::Transform t;
00101     t.translation.x=_robotPose.pose.position.x;
00102     t.translation.y=_robotPose.pose.position.y;
00103     t.translation.z=_robotPose.pose.position.z;
00104     t.rotation=_robotPose.pose.orientation;
00105     j.transforms.push_back(t);
00106 
00107     //calculate theta from quaternion
00108     /*Eigen::Quaterniond targetOri;
00109     tf::quaternionMsgToEigen (_robotPose.pose.pose.orientation, targetOri);
00110     Eigen::Vector3d targetVector=targetOri*origVector;
00111     double dist = angularDistanceInXYSigned(origVector,targetVector);
00112     j.position.push_back(dist);
00113     */
00114     return j;
00115 
00116 }
00117 
00118 sensor_msgs::MultiDOFJointState RobotInfo::getCurrentVirtualJointState(const std::string& poseTopicName, ros::NodeHandle& n, const std::string& virtualJointName, const std::string& frame_id) {
00119     
00120     geometry_msgs::PoseStamped robPos=getCurrentRobotPose(poseTopicName, n);
00121     return getVirtualJointState(robPos,virtualJointName,frame_id);
00122 }
00123 
00124 bool RobotInfo::receivedJointState(){
00125     unique_lock lock(jointStateMutex);
00126     return received_js;
00127 }
00128 
00129 void RobotInfo::jointStateCallback(const sensor_msgs::JointState& jointstate) {
00130     unique_lock lock(jointStateMutex);
00131     jointState=jointstate;
00132     received_js=true;
00133 }
00134 
00135 bool RobotInfo::receivedRobotPose(){
00136     unique_lock lock(poseMutex);
00137     return received_rp;
00138 }
00139 
00140 void RobotInfo::poseCallback(const geometry_msgs::PoseWithCovarianceStamped& msg) {
00141     unique_lock lock(poseMutex);
00142     //ROS_INFO_STREAM("got current pose: "<<msg.pose.pose.position.x<<", "<<msg.pose.pose.position.y<<", "<<msg.pose.pose.position.z);
00143     pose=msg;
00144     received_rp=true;
00145 }
00146 


convenience_ros_functions
Author(s): Jennifer Buehler
autogenerated on Sat Mar 2 2019 03:21:42