robotPoseVisualization.cpp
Go to the documentation of this file.
00001 #include "tidyup_actions/robotPoseVisualization.h"
00002 #include "state_transformer/GetRobotMarker.h"
00003 #include "arm_navigation_msgs/GetRobotState.h"
00004 
00005 RobotPoseVisualization::RobotPoseVisualization()
00006 {
00007 }
00008 
00009 RobotPoseVisualization::~RobotPoseVisualization()
00010 {
00011 }
00012 
00013 bool RobotPoseVisualization::initialize()
00014 {
00015     bool ret = true;
00016     ros::NodeHandle nh;
00017 
00018     // initialize initState from environemtn server
00019     ros::ServiceClient srvGetRobotState =
00020         nh.serviceClient<arm_navigation_msgs::GetRobotState>("/environment_server/get_robot_state");
00021     if(!srvGetRobotState.exists()) {
00022         ROS_FATAL("Service /environment_server/get_robot_state does not seem to exist.");
00023         ret = false;
00024     } else {
00025         arm_navigation_msgs::GetRobotState srv;
00026         if(!srvGetRobotState.call(srv)) {
00027             ROS_FATAL("Call to /environment_server/get_robot_state failed.");
00028             ret = false;
00029         } else {
00030             _initState = srv.response.robot_state;
00031             _currentState = _initState;
00032         }
00033     }
00034 
00035     // setup service client for get robot marker
00036     _srvGetRobotMarker = nh.serviceClient<state_transformer::GetRobotMarker>("/get_robot_marker");
00037     if(!_srvGetRobotMarker.exists()) {
00038         ROS_FATAL("Service /get_robot_marker does not seem to exist");
00039         ret = false;
00040     }
00041 
00042     return ret;
00043 }
00044 
00045 void RobotPoseVisualization::resetRobotState()
00046 {
00047     _currentState = _initState;
00048 }
00049 
00050 void RobotPoseVisualization::updateRobotStateJoints(const sensor_msgs::JointState & js)
00051 {
00052     if(!currentStateInitialized()) {
00053         ROS_ERROR("%s: currentState not initialized", __func__);
00054         return;
00055     }
00056 
00057     sensor_msgs::JointState & curJs = _currentState.joint_state;
00058     for(unsigned int i = 0; i < js.name.size(); i++) {
00059         // find matching joint name in _currentState.joint_state
00060         for(unsigned int j = 0; j < curJs.name.size(); j++) {
00061             if(js.name[i] == curJs.name[j]) {
00062                 curJs.position[j] = js.position[i];
00063             }
00064         }
00065     }
00066 }
00067 
00068 void RobotPoseVisualization::updateRobotStatePose(const geometry_msgs::PoseStamped & pose)
00069 {
00070     if(!currentStateInitialized()) {
00071         ROS_ERROR("%s: currentState not initialized", __func__);
00072         return;
00073     }
00074 
00075     _currentState.multi_dof_joint_state.frame_ids[0] = pose.header.frame_id;
00076     _currentState.multi_dof_joint_state.poses[0] = pose.pose;
00077 
00078     // super nasty hack: I thought the first multi_dof_joint_state being the pose of /base_footprint in /map
00079     // actually gives the robot position.
00080     // This seems to be ignored, but the joints floating_trans_x/y/z and floating_rot_x/y/z/w instead give the pose
00081     sensor_msgs::JointState & js = _currentState.joint_state;
00082     for(unsigned int i = 0; i < js.name.size(); i++) {
00083         if(js.name[i] == "floating_trans_x")
00084             js.position[i] = pose.pose.position.x;
00085         if(js.name[i] == "floating_trans_y")
00086             js.position[i] = pose.pose.position.y;
00087         if(js.name[i] == "floating_trans_z")
00088             js.position[i] = pose.pose.position.z;
00089         if(js.name[i] == "floating_rot_x")
00090             js.position[i] = pose.pose.orientation.x;
00091         if(js.name[i] == "floating_rot_y")
00092             js.position[i] = pose.pose.orientation.y;
00093         if(js.name[i] == "floating_rot_z")
00094             js.position[i] = pose.pose.orientation.z;
00095         if(js.name[i] == "floating_rot_w")
00096             js.position[i] = pose.pose.orientation.w;
00097     }
00098 }
00099 
00100 visualization_msgs::MarkerArray RobotPoseVisualization::getMarkers(
00101         const std_msgs::ColorRGBA & color, const std::string & ns)
00102 {
00103     if(!currentStateInitialized()) {
00104         ROS_ERROR("%s: currentState not initialized", __func__);
00105         return visualization_msgs::MarkerArray();
00106     }
00107 
00108     state_transformer::GetRobotMarker srv;
00109     srv.request.robot_state = _currentState;
00110     srv.request.color = color;
00111     srv.request.ns = ns;
00112     srv.request.scale = 1.0;
00113 
00114     if(!_srvGetRobotMarker.call(srv)) {
00115         ROS_ERROR("Calling GetRobotMarker service failed.");
00116     }
00117     return srv.response.marker_array;
00118 }
00119 
00120 bool RobotPoseVisualization::currentStateInitialized() const
00121 {
00122     return(!_currentState.joint_state.name.empty() && !_currentState.multi_dof_joint_state.poses.empty());
00123 }
00124 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends


tidyup_actions
Author(s): Christian Dornhege, Andreas Hertle
autogenerated on Wed Dec 26 2012 16:11:46