Go to the documentation of this file.00001 #include "planner_modules_pr2/navstack_module_full_body.h"
00002 #include "planner_modules_pr2/navstack_module.h"
00003 #include "tidyup_utils/arm_state.h"
00004 #include <ros/ros.h>
00005 #include <nav_msgs/GetPlan.h>
00006 #include <geometry_msgs/PoseStamped.h>
00007 #include <topic_tools/MuxSelect.h>
00008 #include <map>
00009 using std::map;
00010 #include <utility>
00011 using std::pair; using std::make_pair;
00012 #include <boost/foreach.hpp>
00013 #ifdef __CDT_PARSER__
00014 #define forEach(a, b) for(a : b)
00015 #else
00016 #define forEach BOOST_FOREACH
00017 #endif
00018 #include <sys/times.h>
00019 #include <tf/tf.h>
00020 #include <tf/transform_listener.h>
00021
00022 VERIFY_CONDITIONCHECKER_DEF(fullbody_pathCost);
00023
00024 static ros::ServiceClient s_SwitchJointTopicClient;
00025 static ros::Subscriber s_JointStateSubscriber;
00026 static ros::Publisher s_PlanningJointStatePublisher;
00027 static sensor_msgs::JointState s_CurrentState;
00028 bool receivedJointState;
00029
00030 void jointStateCallback(const sensor_msgs::JointState& msg)
00031 {
00032 receivedJointState = true;
00033 s_CurrentState = msg;
00034 }
00035
00036 void publishPlanningArmState()
00037 {
00038 if (!s_SwitchJointTopicClient)
00039 {
00040 ROS_ERROR("Persistent service connection to %s failed.", s_SwitchJointTopicClient.getService().c_str());
00041 return;
00042 }
00043 topic_tools::MuxSelect switchSrv;
00044 switchSrv.request.topic = "/joint_states_tfd";
00045 if (s_SwitchJointTopicClient.call(switchSrv))
00046 {
00047 ROS_INFO("%s: switched to topic \"%s\".", __FUNCTION__, switchSrv.request.topic.c_str());
00048 ArmState::get("/arm_configurations/side_tuck/position/", "right_arm").replaceJointPositions(s_CurrentState);
00049 ArmState::get("/arm_configurations/side_tuck/position/", "left_arm").replaceJointPositions(s_CurrentState);
00050 s_PlanningJointStatePublisher.publish(s_CurrentState);
00051 ROS_INFO("Publishing planning arm states...");
00052 ros::Rate rate = 1.0;
00053 rate.sleep();
00054 }
00055 else
00056 {
00057 ROS_ERROR("%s Could not switch to topic \"%s\".", __PRETTY_FUNCTION__, switchSrv.request.topic.c_str());
00058 }
00059 }
00060
00061 void switchToExecutionTopic()
00062 {
00063 if (!s_SwitchJointTopicClient)
00064 {
00065 ROS_ERROR("Persistent service connection to %s failed.", s_SwitchJointTopicClient.getService().c_str());
00066 return;
00067 }
00068 topic_tools::MuxSelect switchSrv;
00069 switchSrv.request.topic = "/joint_states_throttle";
00070 if (s_SwitchJointTopicClient.call(switchSrv))
00071 {
00072 ROS_INFO("%s: switched to topic \"%s\".", __FUNCTION__, switchSrv.request.topic.c_str());
00073 }
00074 else
00075 {
00076 ROS_ERROR("%s Could not switch to topic \"%s\".", __PRETTY_FUNCTION__, switchSrv.request.topic.c_str());
00077 }
00078 }
00079
00080 void fullbody_navstack_init(int argc, char** argv)
00081 {
00082 navstack_init(argc, argv);
00083
00084
00085 std::string service_name = "/mux_joint_states/select";
00086 while (!ros::service::waitForService(service_name, ros::Duration(3.0)))
00087 {
00088 ROS_ERROR("Service %s not available - waiting.", service_name.c_str());
00089 }
00090 s_SwitchJointTopicClient = g_NodeHandle->serviceClient<topic_tools::MuxSelect>(service_name, true);
00091 if (!s_SwitchJointTopicClient)
00092 {
00093 ROS_FATAL("Could not initialize get plan service from %s (client name: %s)", service_name.c_str(), s_SwitchJointTopicClient.getService().c_str());
00094 }
00095
00096
00097 receivedJointState = false;
00098 s_JointStateSubscriber = g_NodeHandle->subscribe("/joint_states_throttle", 3, jointStateCallback);
00099 s_PlanningJointStatePublisher = g_NodeHandle->advertise<sensor_msgs::JointState>("/joint_states_tfd", 5, false);
00100
00101 ROS_INFO("Initialized full body navstack module.");
00102 }
00103
00104 double fullbody_pathCost(const ParameterList & parameterList,
00105 predicateCallbackType predicateCallback, numericalFluentCallbackType numericalFluentCallback, int relaxed)
00106 {
00107 nav_msgs::GetPlan srv;
00108 if (!fillPathRequest(parameterList, numericalFluentCallback, srv.request))
00109 {
00110 return INFINITE_COST;
00111 }
00112
00113
00114 double cost = 0;
00115 if (g_PathCostCache.get(computePathCacheKey(parameterList[0].value, parameterList[1].value, srv.request.start.pose, srv.request.goal.pose), cost))
00116 {
00117 return cost;
00118 }
00119 publishPlanningArmState();
00120 cost = pathCost(parameterList, predicateCallback, numericalFluentCallback, relaxed);
00121 switchToExecutionTopic();
00122 return cost;
00123 }
00124