3 #include <sensor_msgs/JointState.h> 8 std::string exec(
const char *cmd)
10 std::shared_ptr<FILE> pipe(popen(cmd,
"r"), pclose);
14 std::string result =
"";
15 while (!feof(pipe.get()))
17 if (fgets(buffer, 128, pipe.get()) != NULL)
28 , action_server_(private_nh_,
"build", false)
29 , run_motion_server_(private_nh_,
"run",
31 , play_motion_client_(
"/play_motion")
45 std::string robot_desc_param =
46 private_nh_.
param(
"robot_description_param", std::string(
"/robot_description"));
49 ROS_ERROR(
"Couldn't find robot description for this robot");
52 std::string semantic_desc_param =
53 private_nh_.
param(
"semantic_description", std::string(
"/robot_description_semantic"));
56 ROS_ERROR(
"Couldn't find semantic description for this robot");
62 "extra_joints_param_name",
63 std::string(
"/play_motion/approach_planner/exclude_from_planning_joints"));
65 if (
nh_.
getParam(extra_joints_param_name, extra_joints_param))
67 for (
int i = 0; i < extra_joints_param.
size(); ++i)
69 extra_joints_.push_back(static_cast<std::string>(extra_joints_param[i]));
93 if (goal->motion !=
"")
97 if (
nh_.
getParam(
"/play_motion/motions/" + goal->motion, motion_cfg))
107 play_motion_builder_msgs::BuildMotionResult result;
109 result.message =
"Couldn't find motion " + goal->motion;
131 play_motion_builder_msgs::BuildMotionResult result;
133 result.message =
"Process was preempted";
137 if (
motion_->getParamName() !=
"")
150 play_motion_builder_msgs::EditMotion::Response &res)
156 case play_motion_builder_msgs::EditMotion::Request::LIST:
159 case play_motion_builder_msgs::EditMotion::Request::APPEND:
160 case play_motion_builder_msgs::EditMotion::Request::EDIT:
162 sensor_msgs::JointStateConstPtr joint_state =
163 ros::topic::waitForMessage<sensor_msgs::JointState>(
"/joint_states");
164 if (req.action == play_motion_builder_msgs::EditMotion::Request::APPEND)
167 motion_->addKeyFrame(joint_state);
172 motion_->updateKeyFrame(joint_state, req.step_id);
176 case play_motion_builder_msgs::EditMotion::Request::COPY_AS_LAST:
177 case play_motion_builder_msgs::EditMotion::Request::COPY_AS_NEXT:
178 motion_->copyFrame(req.step_id,
179 req.action == play_motion_builder_msgs::EditMotion::Request::COPY_AS_LAST ?
183 case play_motion_builder_msgs::EditMotion::Request::REMOVE:
184 motion_->removeKeyFrame(req.step_id);
186 case play_motion_builder_msgs::EditMotion::Request::EDIT_TIME:
187 motion_->changeTime(req.step_id, req.time);
192 res.message =
"Unknown action code";
202 res.message =
"No motion being built";
209 play_motion_builder_msgs::StoreMotion::Response &res)
216 << YAML::Key <<
"play_motion" 217 << YAML::Value << YAML::BeginMap
218 << YAML::Key <<
"motions" 219 << YAML::Value << YAML::BeginMap
220 << YAML::Key << req.ros_name
221 << YAML::Value <<
motion_->print(req.meta.name, req.meta.usage, req.meta.description)
229 if (req.file_path !=
"")
234 file.open(req.file_path);
239 exec((
"rosparam delete /play_motion/motions/" + req.ros_name).c_str());
242 exec((
"rosparam load " + req.file_path).c_str());
246 catch (
const std::ios_base::failure &err)
249 res.message = err.what();
255 res.message = std::string(e.c_str());
260 res.message =
"No motion being built, could not store.";
267 play_motion_builder_msgs::ChangeJoints::Response &res)
276 if (!
motion_->setCurrentGroup(req.group))
279 res.message =
"Couldn't change group to " + req.group +
". ";
283 if (req.joints_to_remove.size() > 0)
285 for (
const auto &joint : req.joints_to_remove)
288 if (!
motion_->setExtraJointUsedState(joint,
false))
291 res.message +=
"Couldn't remove extra joint " + joint +
". ";
296 if (req.joints_to_add.size() > 0)
298 for (
const auto &joint : req.joints_to_add)
301 if (!
motion_->setExtraJointUsedState(joint,
true))
304 res.message +=
"Couldn't add extra joint " + joint +
". ";
310 res.current_group =
motion_->getCurrentGroup();
311 res.used_joints =
motion_->getJoints();
316 res.message =
"No motion being built";
323 play_motion_builder_msgs::ListJointGroups::Response &res)
327 res.groups =
motion_->getJointGroups();
328 res.additional_joints =
motion_->getExtraJoints();
329 res.available_joints =
motion_->getAvailableJoints();
344 if (
motion_->getParamName() ==
"")
347 if (goal->run_mode == play_motion_builder_msgs::RunMotionGoal::RUN_MOTION)
350 motion_->loadYAML(goal->downshift == 0.0 ? 1.0 : goal->downshift);
352 else if (goal->run_mode == play_motion_builder_msgs::RunMotionGoal::GO_TO_STEP)
354 motion_->loadFrame(goal->step_id);
363 play_motion_msgs::PlayMotionGoal motion_goal;
364 motion_goal.motion_name =
motion_->getParamName();
386 motion.joints =
motion_->getJoints();
387 motion.used_group =
motion_->getCurrentGroup();
388 for (
unsigned int i = 0; i <
motion_->size(); ++i)
390 play_motion_builder_msgs::Frame
f;
391 for (
const auto &joint : motion.joints)
393 f.pose.push_back(
motion_->getKeyFrame(i).getJointPosition(joint));
395 f.time_from_last =
motion_->getKeyFrame(i).getTime();
397 motion.keyframes.push_back(f);
boost::shared_ptr< const Goal > acceptNewGoal()
bool storeMotionCb(play_motion_builder_msgs::StoreMotion::Request &req, play_motion_builder_msgs::StoreMotion::Response &res)
ros::NodeHandle private_nh_
bool deleteParam(const std::string &key) const
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
bool editMotionCb(play_motion_builder_msgs::EditMotion::Request &req, play_motion_builder_msgs::EditMotion::Response &res)
void setSucceeded(const Result &result=Result(), const std::string &text=std::string(""))
void motionToROSMsg(play_motion_builder_msgs::Motion &motion)
ros::ServiceServer list_joints_server_
bool param(const std::string ¶m_name, T ¶m_val, const T &default_val) const
void setPreempted(const Result &result=Result(), const std::string &text=std::string(""))
SimpleClientGoalState getState() const
void runMotionPreemptCb()
bool getParam(const std::string &key, std::string &s) const
std::vector< std::string > extra_joints_
RMServer run_motion_server_
void registerPreemptCallback(boost::function< void()> cb)
void executeRunMotionCb(const play_motion_builder_msgs::RunMotionGoalConstPtr &goal)
PMClient play_motion_client_
std::string semantic_description_
bool isPreemptRequested()
ros::ServiceServer change_joints_server_
#define ROS_DEBUG_STREAM(args)
bool changeJointsCb(play_motion_builder_msgs::ChangeJoints::Request &req, play_motion_builder_msgs::ChangeJoints::Response &res)
void setAborted(const Result &result=Result(), const std::string &text=std::string(""))
void sendGoal(const Goal &goal, SimpleDoneCallback done_cb=SimpleDoneCallback(), SimpleActiveCallback active_cb=SimpleActiveCallback(), SimpleFeedbackCallback feedback_cb=SimpleFeedbackCallback())
#define ROS_INFO_STREAM(args)
#define ROS_ERROR_STREAM(args)
void registerGoalCallback(boost::function< void()> cb)
ros::ServiceServer store_motion_server_
bool listJointGroupsCb(play_motion_builder_msgs::ListJointGroups::Request &req, play_motion_builder_msgs::ListJointGroups::Response &res)
ros::ServiceServer edit_motion_server_
std::string robot_description_
void buildMotionPreemptCb()
std::unique_ptr< Motion > motion_