00001 #include "r2_controllers_ros/R2RosTrajectoryManager.hpp" 00002 00003 R2RosTrajectoryManager::R2RosTrajectoryManager(const string name, const string &urdf, const double ×tep, ros::NodeHandle &nh) 00004 :TrajectoryManager(name) 00005 { 00006 urdfFile = urdf; 00007 initialize(urdfFile, timestep); 00008 treeFk.loadFromFile(urdfFile); 00009 00011 jointCommandOut = nh.advertise<sensor_msgs::JointState>("/joint_commands", 1); 00012 poseStatesOut = nh.advertise<nasa_r2_common_msgs::PoseState>("/pose_states", 1); 00013 poseCommandOut = nh.advertise<nasa_r2_common_msgs::PoseState>("/pose_command_refs", 1); 00014 goalStatusOut = nh.advertise<actionlib_msgs::GoalStatusArray>("/goal_status", 10); 00015 00016 jointSettingsIn = nh.subscribe("joint_ref_settings", 1, &R2RosTrajectoryManager::jointSettingsCallback, this); 00017 jointCommandRefsIn = nh.subscribe("/joint_command_refs", 1, &R2RosTrajectoryManager::jointCommandCallback, this); 00018 jointStatesIn = nh.subscribe("/joint_states", 1, &R2RosTrajectoryManager::jointStatesCallback, this); 00019 jointCapabilitiesIn = nh.subscribe("/joint_capabilities", 1, &R2RosTrajectoryManager::jointCapabilitiesCallback, this); 00020 jointRefsIn = nh.subscribe("joint_refs", 32, &R2RosTrajectoryManager::jointRefsCallback, this); 00021 jointTrajIn = nh.subscribe("joint_traj", 32, &R2RosTrajectoryManager::jointTrajCallback, this); 00022 00023 basesIn = nh.subscribe("/base_frames", 1, &R2RosTrajectoryManager::basesCallback, this); 00024 inertiaIn = nh.subscribe("/treeid_inertia", 1, &R2RosTrajectoryManager::inertiaCallback, this); 00025 poseSettingsIn = nh.subscribe("pose_ref_settings", 1, &R2RosTrajectoryManager::poseSettingsCallback, this); 00026 poseStatesIn = nh.subscribe("/pose_states", 1, &R2RosTrajectoryManager::poseStatesCallback, this); 00027 poseCommandRefIn = nh.subscribe("/pose_command_refs", 1, &R2RosTrajectoryManager::poseCommandCallback, this); 00028 poseRefsIn = nh.subscribe("pose_refs", 32, &R2RosTrajectoryManager::poseRefsCallback, this); 00029 } 00030 00031 void R2RosTrajectoryManager::setCartesianParameters(std::vector<double> priorityNum, std::vector<double> linearTol, std::vector<double> angularTol, double momLimit, double taskGain, int ikMaxItr, double ikMaxTwist) 00032 { 00033 setPriorityTol(priorityNum, linearTol, angularTol); 00034 setIkParameters(momLimit, taskGain, ikMaxItr, ikMaxTwist); 00035 nasa_r2_common_msgs::StringArray msg; 00036 msg.data.push_back("/r2/robot_world"); 00037 setBases(msg); 00038 } 00039 00040 void R2RosTrajectoryManager::jointSettingsCallback(const nasa_r2_common_msgs::ControllerJointSettings& msg) 00041 { 00042 ROS_INFO("in JointSettingsCallback"); 00043 setJointSettings(msg); 00044 } 00045 00046 void R2RosTrajectoryManager::jointCommandCallback(const nasa_r2_common_msgs::JointCommand& msg) 00047 { 00048 ROS_DEBUG("in JointCommandCallback"); 00049 newJointCommands = true; 00050 prevJointCommandsMsg = jointCommandsMsg; 00051 jointCommandsMsg = msg; 00052 treeFk.getPoseState(fromJointCommandToJointState(msg), poseCommandOutMsg); 00053 poseCommandOut.publish(poseCommandOutMsg); 00054 } 00055 00056 void R2RosTrajectoryManager::jointStatesCallback(const sensor_msgs::JointState& msg) 00057 { 00058 ROS_DEBUG("in JointStatesCallback"); 00059 prevJointStatesMsg = jointStatesMsg; 00060 jointStatesMsg = msg; 00061 treeFk.getPoseState(msg, poseStatesOutMsg); 00062 poseStatesOut.publish(poseStatesOutMsg); 00063 } 00064 00065 void R2RosTrajectoryManager::jointCapabilitiesCallback(const nasa_r2_common_msgs::JointCapability& msg) 00066 { 00067 ROS_DEBUG("in JointCapabilitiesCallback"); 00068 setJointCapabilities(msg); 00069 } 00070 00071 void R2RosTrajectoryManager::jointRefsCallback(const nasa_r2_common_msgs::JointTrajectoryReplan& jointRefsMsg) 00072 { 00073 ROS_INFO("Joint Ref Trajectory received"); 00074 if(jointRefsMsg.trajectory.joint_names.empty()) 00075 { 00076 ROS_INFO("joint names empty"); 00077 if(newJointCommands) 00078 { 00079 kasquishAll(fromJointCommandToJointState(jointCommandsMsg), fromJointCommandToJointState(jointCommandsMsg), "new joint refs with no joint names"); 00080 } 00081 else 00082 { 00083 resetAll("new joint refs with no joint names"); 00084 } 00085 } 00086 else 00087 { 00088 ROS_INFO("added joint waypoints"); 00089 addJointWaypoints(jointRefsMsg.trajectory, fromJointCommandToJointState(jointCommandsMsg), fromJointCommandToJointState(jointCommandsMsg), fromJointCommandToJointState(prevJointCommandsMsg)); 00090 } 00091 } 00092 00093 void R2RosTrajectoryManager::jointTrajCallback(const trajectory_msgs::JointTrajectory& trajInMsg) 00094 { 00095 ROS_INFO("in JointTrajCallback"); 00096 if (trajInMsg.joint_names.empty()) 00097 { 00098 if (newJointCommands) 00099 { 00100 kasquishAll(fromJointCommandToJointState(jointCommandsMsg), fromJointCommandToJointState(jointCommandsMsg), "new joint trajectory with no joint names"); 00101 } 00102 else 00103 { 00104 resetAll("new joint trajectory with no joint names"); 00105 } 00106 } 00107 else 00108 { 00109 addJointBreadcrumbs(trajInMsg); 00110 } 00111 } 00112 00113 void R2RosTrajectoryManager::basesCallback(const nasa_r2_common_msgs::StringArray &msg) 00114 { 00115 ROS_INFO("set new bases"); 00116 setBases(msg); 00117 } 00118 00119 void R2RosTrajectoryManager::inertiaCallback(const sensor_msgs::JointState &msg) 00120 { 00121 ROS_DEBUG("in inertiaCallback"); 00122 updateInertia(msg); 00123 } 00124 00125 void R2RosTrajectoryManager::poseSettingsCallback(const nasa_r2_common_msgs::ControllerPoseSettings &msg) 00126 { 00127 ROS_INFO("in poseSettingsCallback"); 00128 setPoseSettings(msg); 00129 } 00130 00131 void R2RosTrajectoryManager::poseStatesCallback(const nasa_r2_common_msgs::PoseState &msg) 00132 { 00133 ROS_DEBUG("in poseStatesCallback"); 00134 updateActualPoseState(msg); 00135 } 00136 00137 void R2RosTrajectoryManager::poseCommandCallback(const nasa_r2_common_msgs::PoseState &msg) 00138 { 00139 ROS_DEBUG("in poseCommandCallback"); 00140 poseCommandsMsg = msg; 00141 newPoseCommands = true; 00142 } 00143 00144 void R2RosTrajectoryManager::poseRefsCallback(const nasa_r2_common_msgs::PoseTrajectoryReplan &msg) 00145 { 00146 ROS_INFO("in poseRefsCallback"); 00147 poseRefsMsg = msg; 00148 if (!poseRefsQueue.empty()) 00149 { 00150 poseRefsMsg = poseRefsQueue.front(); 00151 poseRefsQueue.pop(); 00152 } 00153 00154 ROS_INFO("Adding cartesian trajectory"); 00155 addCartesianWaypoints(poseRefsMsg.trajectory, fromJointCommandToJointState(jointCommandsMsg), fromJointCommandToJointState(jointCommandsMsg), fromJointCommandToJointState(prevJointCommandsMsg), poseCommandsMsg, poseCommandsMsg); 00156 00157 } 00158 00159 void R2RosTrajectoryManager::writeStatus(const actionlib_msgs::GoalStatusArray& goalStatusMsg_out) 00160 { 00161 ROS_INFO("wrote status"); 00162 goalStatusOut.publish(goalStatusMsg_out); 00163 } 00164 00165 void R2RosTrajectoryManager::writeJointState(const sensor_msgs::JointState& jointState_out) 00166 { 00167 ROS_DEBUG("wrote joint command"); 00168 jointCommandOutMsg = jointState_out; 00169 jointCommandOut.publish(jointCommandOutMsg); 00170 00171 } 00172 00173 void R2RosTrajectoryManager::writeJointControl(const nasa_r2_common_msgs::JointControlDataArray& jointControl_out) 00174 { 00175 } 00176 00177 void R2RosTrajectoryManager::update() 00178 { 00179 if(newJointCommands) 00180 { 00181 try 00182 { 00183 updateTrajectories(jointCommandsMsg, fromJointCommandToJointControl(jointCommandsMsg), fromJointCommandToJointState(jointCommandsMsg), fromJointCommandToJointState(jointCommandsMsg)); 00184 } 00185 catch (std::exception& e) 00186 { 00187 ROS_ERROR("TrajectoryManagerComponent: something crazy happened in updateTrajectories, moving to error state"); 00188 } 00189 newJointCommands = false; 00190 newPoseCommands = false; 00191 } 00192 } 00193 00194 sensor_msgs::JointState R2RosTrajectoryManager::fromJointCommandToJointState(const nasa_r2_common_msgs::JointCommand& jointCommandMsg) 00195 { 00196 sensor_msgs::JointState jointStateMsg; 00197 00198 jointStateMsg.header = jointCommandMsg.header; 00199 jointStateMsg.name = jointCommandMsg.name; 00200 jointStateMsg.position = jointCommandMsg.desiredPosition; 00201 jointStateMsg.velocity = jointCommandMsg.desiredPositionVelocityLimit; 00202 jointStateMsg.effort = jointCommandMsg.feedForwardTorque; 00203 00204 return jointStateMsg; 00205 } 00206 00207 nasa_r2_common_msgs::JointControlDataArray R2RosTrajectoryManager::fromJointCommandToJointControl(const nasa_r2_common_msgs::JointCommand& jointCommandMsg) 00208 { 00209 nasa_r2_common_msgs::JointControlDataArray jointControlMsg; 00210 if(jointControlMsg.joint != jointCommandMsg.name) 00211 { 00212 jointControlMsg.joint.clear(); 00213 jointControlMsg.data.clear(); 00214 for(unsigned int i = 0; i < jointCommandMsg.name.size(); ++i) 00215 { 00216 jointControlMsg.joint.push_back(jointCommandMsg.name[i]); 00217 nasa_r2_common_msgs::JointControlData jc; 00218 jc.calibrationMode.state = nasa_r2_common_msgs::JointControlCalibrationMode::DISABLE; 00219 jc.clearFaultMode.state = nasa_r2_common_msgs::JointControlClearFaultMode::DISABLE; 00220 jc.coeffState.state = nasa_r2_common_msgs::JointControlCoeffState::LOADED; 00221 jc.commandMode.state = nasa_r2_common_msgs::JointControlCommandMode::MULTILOOPSMOOTH; 00222 jc.controlMode.state = nasa_r2_common_msgs::JointControlMode::DRIVE; 00223 jointControlMsg.data.push_back(jc); 00224 } 00225 } 00226 return jointControlMsg; 00227 }