35 #include <trajectory_msgs/JointTrajectory.h> 36 #include <pr2_controllers_msgs/JointTrajectoryAction.h> 37 #include <pr2_controllers_msgs/JointTrajectoryControllerState.h> 60 if (!pn.
getParam(
"joints", joint_names))
70 for (
int i = 0; i < joint_names.
size(); ++i)
73 if (name_value.
getType() != XmlRpcValue::TypeString)
75 ROS_FATAL(
"Array of joint names should contain all strings. (namespace: %s)",
88 std::string ns = std::string(
"constraints/") +
joint_names_[i];
90 pn.
param(ns +
"/goal", g, -1.0);
91 pn.
param(ns +
"/trajectory", t, -1.0);
109 if (started_waiting_for_controller !=
ros::Time(0) &&
112 ROS_WARN(
"Waited for the controller for 30 seconds, but it never showed up.");
113 started_waiting_for_controller =
ros::Time(0);
130 static bool setsEqual(
const std::vector<std::string> &a,
const std::vector<std::string> &b)
132 if (a.size() != b.size())
135 for (
size_t i = 0; i < a.size(); ++i)
137 if (count(b.begin(), b.end(), a[i]) != 1)
140 for (
size_t i = 0; i < b.size(); ++i)
142 if (count(a.begin(), a.end(), b[i]) != 1)
156 bool should_abort =
false;
160 ROS_WARN(
"Aborting goal because we have never heard a controller state message.");
165 ROS_WARN(
"Aborting goal because we haven't heard from the controller in %.3lf seconds",
172 trajectory_msgs::JointTrajectory empty;
188 ROS_ERROR(
"Joints on incoming goal don't match our joints");
197 trajectory_msgs::JointTrajectory empty;
220 trajectory_msgs::JointTrajectory empty;
251 last_controller_state_ = msg;
254 if (!has_active_goal_)
256 if (current_traj_.points.empty())
258 if (now < current_traj_.header.stamp + current_traj_.points[0].time_from_start)
261 if (!
setsEqual(joint_names_, msg->joint_names))
263 ROS_ERROR(
"Joint names from the controller don't match our joint names.");
267 int last = current_traj_.points.size() - 1;
268 ros::Time end_time = current_traj_.header.stamp + current_traj_.points[last].time_from_start;
275 for (
size_t i = 0; i < msg->joint_names.size(); ++i)
277 double abs_error = fabs(msg->error.positions[i]);
278 double constraint = trajectory_constraints_[msg->joint_names[i]];
279 if (constraint >= 0 && abs_error > constraint)
282 trajectory_msgs::JointTrajectory empty;
284 pub_controller_command_.
publish(empty);
287 has_active_goal_ =
false;
288 ROS_WARN(
"Aborting because we would up outside the trajectory constraints");
296 bool inside_goal_constraints =
true;
297 for (
size_t i = 0; i < msg->joint_names.size() && inside_goal_constraints; ++i)
299 double abs_error = fabs(msg->error.positions[i]);
300 double goal_constraint = goal_constraints_[msg->joint_names[i]];
301 if (goal_constraint >= 0 && abs_error > goal_constraint)
302 inside_goal_constraints =
false;
305 if (fabs(msg->desired.velocities[i]) < 1e-6)
307 if (fabs(msg->actual.velocities[i]) > stopped_velocity_tolerance_)
308 inside_goal_constraints =
false;
312 if (inside_goal_constraints)
315 has_active_goal_ =
false;
317 else if (now < end_time +
ros::Duration(goal_time_constraint_))
323 ROS_WARN(
"Aborting because we wound up outside the goal constraints");
325 has_active_goal_ =
false;
333 int main(
int argc,
char** argv)
335 ros::init(argc, argv,
"joint_trajectory_action_node");
void watchdog(const ros::TimerEvent &e)
void publish(const boost::shared_ptr< M > &message) const
ServerGoalHandle< pr2_controllers_msgs::JointTrajectoryAction > GoalHandle
void controllerStateCB(const pr2_controllers_msgs::JointTrajectoryControllerStateConstPtr &msg)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void setRejected(const Result &result=Result(), const std::string &text=std::string(""))
ros::Publisher pub_controller_command_
boost::shared_ptr< const Goal > getGoal() const
std::map< std::string, double > goal_constraints_
Type const & getType() const
JTAS::GoalHandle GoalHandle
void setCanceled(const Result &result=Result(), const std::string &text=std::string(""))
ros::Timer watchdog_timer_
void setAccepted(const std::string &text=std::string(""))
const double DEFAULT_GOAL_THRESHOLD
void goalCB(GoalHandle gh)
void setSucceeded(const Result &result=Result(), const std::string &text=std::string(""))
JointTrajectoryExecuter(ros::NodeHandle &n)
bool param(const std::string ¶m_name, T ¶m_val, const T &default_val) const
trajectory_msgs::JointTrajectory current_traj_
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
const std::string & getNamespace() const
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
pr2_controllers_msgs::JointTrajectoryControllerStateConstPtr last_controller_state_
ros::Subscriber sub_controller_state_
static bool setsEqual(const std::vector< std::string > &a, const std::vector< std::string > &b)
void setAborted(const Result &result=Result(), const std::string &text=std::string(""))
std::vector< std::string > joint_names_
int main(int argc, char **argv)
bool getParam(const std::string &key, std::string &s) const
double stopped_velocity_tolerance_
void cancelCB(GoalHandle gh)
ROSCPP_DECL void spinOnce()
actionlib::ActionServer< pr2_controllers_msgs::JointTrajectoryAction > JTAS
double goal_time_constraint_
std::map< std::string, double > trajectory_constraints_
~JointTrajectoryExecuter()