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