29 #include <controller_manager_msgs/ListControllers.h> 30 #include <sr_utilities/getJointState.h> 31 #include <std_msgs/Float64.h> 32 #include <sr_robot_msgs/sendupdate.h> 47 for (
int i = 1; i <= n; i++)
49 powers[i] = powers[i - 1] * x;
54 double end_pos,
double end_vel,
double end_acc,
double time,
55 std::vector<double> &coefficients)
57 coefficients.resize(6);
61 coefficients[0] = end_pos;
62 coefficients[1] = end_vel;
63 coefficients[2] = 0.5 * end_acc;
64 coefficients[3] = 0.0;
65 coefficients[4] = 0.0;
66 coefficients[5] = 0.0;
73 coefficients[0] = start_pos;
74 coefficients[1] = start_vel;
75 coefficients[2] = 0.5 * start_acc;
76 coefficients[3] = (-20.0 * start_pos + 20.0 * end_pos - 3.0 * start_acc * T[2] + end_acc * T[2] -
77 12.0 * start_vel * T[1] - 8.0 * end_vel * T[1]) / (2.0 * T[3]);
78 coefficients[4] = (30.0 * start_pos - 30.0 * end_pos + 3.0 * start_acc * T[2] - 2.0 * end_acc * T[2] +
79 16.0 * start_vel * T[1] + 14.0 * end_vel * T[1]) / (2.0 * T[4]);
80 coefficients[5] = (-12.0 * start_pos + 12.0 * end_pos - start_acc * T[2] + end_acc * T[2] -
81 6.0 * start_vel * T[1] - 6.0 * end_vel * T[1]) / (2.0 * T[5]);
89 double &position,
double &velocity,
double &acceleration)
95 position = t[0] * coefficients[0] +
96 t[1] * coefficients[1] +
97 t[2] * coefficients[2] +
98 t[3] * coefficients[3] +
99 t[4] * coefficients[4] +
100 t[5] * coefficients[5];
102 velocity = t[0] * coefficients[1] +
103 2.0 * t[1] * coefficients[2] +
104 3.0 * t[2] * coefficients[3] +
105 4.0 * t[3] * coefficients[4] +
106 5.0 * t[4] * coefficients[5];
108 acceleration = 2.0 * t[0] * coefficients[2] +
109 6.0 * t[1] * coefficients[3] +
110 12.0 * t[2] * coefficients[4] +
111 20.0 * t[3] * coefficients[5];
115 double end_pos,
double end_vel,
double time, std::vector<double> &coefficients)
117 coefficients.resize(4);
121 coefficients[0] = end_pos;
122 coefficients[1] = end_vel;
123 coefficients[2] = 0.0;
124 coefficients[3] = 0.0;
131 coefficients[0] = start_pos;
132 coefficients[1] = start_vel;
133 coefficients[2] = (-3.0 * start_pos + 3.0 * end_pos - 2.0 * start_vel * T[1] - end_vel * T[1]) / T[2];
134 coefficients[3] = (2.0 * start_pos - 2.0 * end_pos + start_vel * T[1] + end_vel * T[1]) / T[3];
139 nh_tilde(
"~"), use_sendupdate(false)
145 std::vector<std::string> joint_labels;
148 joint_labels.push_back(
"ShoulderJRotate");
149 joint_labels.push_back(
"ShoulderJSwing");
150 joint_labels.push_back(
"ElbowJSwing");
151 joint_labels.push_back(
"ElbowJRotate");
152 joint_labels.push_back(
"WRJ2");
153 joint_labels.push_back(
"WRJ1");
156 for (
unsigned int i = 0; i < joint_labels.size(); i++)
166 "controller_manager/list_controllers");
167 controller_manager_msgs::ListControllers controller_list;
168 std::string controlled_joint_name;
171 controller_list_client.
call(controller_list);
173 for (
unsigned int i = 0; i < controller_list.response.controller.size(); i++)
175 if (controller_list.response.controller[i].state ==
"running")
177 if (
nh.
getParam(
"/" + controller_list.response.controller[i].name +
"/joint", controlled_joint_name))
179 ROS_DEBUG(
"controller %d:%s controls joint %s\n", i, controller_list.response.controller[i].name.c_str(),
180 controlled_joint_name.c_str());
181 jointControllerMap[controlled_joint_name] = controller_list.response.controller[i].name;
187 for (
unsigned int i = 0; i < joint_labels.size(); i++)
191 if (controller_name.compare(
"") != 0)
200 ROS_WARN(
"Could not find a controller for joint %s", joint_labels[i].c_str());
207 ROS_WARN(
"controller_manager not found, switching back to sendupdates");
213 ROS_INFO(
"waiting for getJointState");
221 ROS_ERROR(
"Cannot access service: Check if joint_state service is launched");
225 ROS_INFO(
"Got getJointState");
235 ROS_INFO(
"Listening to commands");
238 ROS_INFO(
"Action server started");
243 sr_utilities::getJointState getState;
244 sensor_msgs::JointState joint_state_msg;
247 joint_state_msg = getState.response.joint_state;
248 if (joint_state_msg.name.size() > 0)
251 for (
unsigned int i = 0; i < joint_state_msg.name.size(); i++)
253 joint_state_map[joint_state_msg.name[i]] = joint_state_msg.position[i];
261 std::map<std::string, double>::iterator iter =
joint_state_map.find(joint_name);
264 position = iter->second;
269 ROS_ERROR(
"Joint %s not found", joint_name.c_str());
281 ROS_DEBUG(
"Figuring out new trajectory at %.3lf, with data from %.3lf",
282 time.
toSec(), goal->trajectory.header.stamp.toSec());
289 std::vector<double> prev_positions(
joint_names_.size());
290 std::vector<double> prev_velocities(
joint_names_.size());
291 std::vector<double> prev_accelerations(
joint_names_.size());
295 ROS_DEBUG(
"Initial conditions for new set of splines:");
305 ROS_ERROR(
"Cannot get joint_state, not executing trajectory");
308 prev_velocities[i] = 0.0;
309 prev_accelerations[i] = 0.0;
311 ROS_DEBUG(
" %.2lf, %.2lf, %.2lf (%s)", prev_positions[i], prev_velocities[i],
315 std::vector<double> positions;
316 std::vector<double> velocities;
317 std::vector<double> accelerations;
319 std::vector<double> durations(goal->trajectory.points.size());
320 if (goal->trajectory.points.size() > 0)
322 durations[0] = goal->trajectory.points[0].time_from_start.toSec();
324 for (
size_t i = 1; i < goal->trajectory.points.size(); ++i)
326 durations[i] = (goal->trajectory.points[i].time_from_start -
327 goal->trajectory.points[i - 1].time_from_start).toSec();
333 for (
size_t i = 0; i < goal->trajectory.points.size(); ++i)
337 if (goal->trajectory.header.stamp ==
ros::Time(0.0))
339 seg.
start_time = (time + goal->trajectory.points[i].time_from_start).toSec() - durations[i];
344 (goal->trajectory.header.stamp + goal->trajectory.points[i].time_from_start).toSec() - durations[i];
350 if (goal->trajectory.points[i].accelerations.size() != 0 &&
351 goal->trajectory.points[i].accelerations.size() !=
joint_names_.size())
353 ROS_ERROR(
"Command point %d has %d elements for the accelerations", static_cast<int>(i),
354 static_cast<int>(goal->trajectory.points[i].accelerations.size()));
357 if (goal->trajectory.points[i].velocities.size() != 0 &&
358 goal->trajectory.points[i].velocities.size() !=
joint_names_.size())
360 ROS_ERROR(
"Command point %d has %d elements for the velocities", static_cast<int>(i),
361 static_cast<int>(goal->trajectory.points[i].velocities.size()));
364 if (goal->trajectory.points[i].positions.size() !=
joint_names_.size())
366 ROS_ERROR(
"Command point %d has %d elements for the positions", static_cast<int>(i),
367 static_cast<int>(goal->trajectory.points[i].positions.size()));
372 accelerations.resize(goal->trajectory.points[i].accelerations.size());
373 velocities.resize(goal->trajectory.points[i].velocities.size());
374 positions.resize(goal->trajectory.points[i].positions.size());
375 for (
size_t j = 0; j < goal->trajectory.joint_names.size(); ++j)
377 if (!accelerations.empty())
380 goal->trajectory.points[i].accelerations[j];
382 if (!velocities.empty())
384 velocities[
joint_state_idx_map[goal->trajectory.joint_names[j]]] = goal->trajectory.points[i].velocities[j];
386 if (!positions.empty())
388 positions[
joint_state_idx_map[goal->trajectory.joint_names[j]]] = goal->trajectory.points[i].positions[j];
395 if (prev_accelerations.size() > 0 && accelerations.size() > 0)
398 prev_positions[j], prev_velocities[j], prev_accelerations[j],
399 positions[j], velocities[j], accelerations[j],
403 else if (prev_velocities.size() > 0 && velocities.size() > 0)
406 prev_positions[j], prev_velocities[j],
407 positions[j], velocities[j],
410 seg.
splines[j].coef.resize(6, 0.0);
414 seg.
splines[j].coef[0] = prev_positions[j];
415 if (durations[i] == 0.0)
421 seg.
splines[j].coef[1] = (positions[j] - prev_positions[j]) / durations[i];
435 prev_positions = positions;
436 prev_velocities = velocities;
437 prev_accelerations = accelerations;
444 ROS_ERROR(
"The new trajectory was null!");
448 ROS_DEBUG(
"The new trajectory has %d segments", static_cast<int>(traj.size()));
450 std::vector<sr_robot_msgs::joint> joint_vector_traj;
451 unsigned int controller_pub_idx = 0;
453 std_msgs::Float64 target_msg;
454 sr_robot_msgs::sendupdate sendupdate_msg_traj;
459 joint_vector_traj.clear();
463 sr_robot_msgs::joint joint;
465 joint_vector_traj.push_back(joint);
470 sendupdate_msg_traj.sendupdate_length = joint_vector_traj.size();
471 ROS_DEBUG(
"Trajectory received: %d joints / %d msg length", static_cast<int>(goal->trajectory.joint_names.size()),
472 sendupdate_msg_traj.sendupdate_length);
482 ROS_DEBUG(
"Entering the execution loop");
496 while (seg + 1 < static_cast<int>(traj.size()) && traj[seg + 1].start_time < time.
toSec())
502 if ((traj[traj.size() - 1].start_time + traj[traj.size() - 1].duration) < time.
toSec())
504 ROS_DEBUG(
"trajectory is finished %f<%f", (traj[traj.size() - 1].start_time + traj[traj.size() - 1].duration),
511 if (traj.size() == 0)
512 ROS_ERROR(
"No segments in the trajectory");
514 ROS_ERROR(
"No earlier segments. First segment starts at %.3lf (now = %.3lf)", traj[0].start_time,
523 for (
size_t i = 0; i <
q.size(); ++i)
526 time.
toSec() - traj[seg].start_time,
533 ROS_INFO(
"Joint Trajectory Action Preempted");
539 ROS_DEBUG(
"Update the targets");
541 sensor_msgs::JointState desired_joint_state_msg;
544 desired_joint_state_msg.name.push_back(
joint_names_[i]);
545 desired_joint_state_msg.position.push_back(
q[i]);
546 desired_joint_state_msg.velocity.push_back(
qd[i]);
547 desired_joint_state_msg.effort.push_back(0.0);
552 target_msg.data =
q[i];
558 joint_vector_traj[i].joint_target =
q[i] * 57.3;
559 ROS_DEBUG(
"traj[%s]: %f", joint_vector_traj[i].joint_name.c_str(), joint_vector_traj[i].joint_target);
562 ROS_DEBUG(
"Targets updated");
569 sendupdate_msg_traj.sendupdate_list = joint_vector_traj;
574 ROS_DEBUG(
"Now sleep and loop");
575 sleeping_time.
sleep();
577 ROS_DEBUG(
"redo loop");
580 control_msgs::FollowJointTrajectoryResult joint_trajectory_result;
593 const std::vector<double> &coefficients,
double duration,
double time,
594 double &position,
double &velocity,
double &acceleration)
603 else if (time > duration)
613 position, velocity, acceleration);
624 ROS_ERROR(
"Figuring out new trajectory at %.3lf, with data from %.3lf with %zu waypoints",
625 time.
toSec(), msg->header.stamp.toSec(), msg->points.size());
632 std::vector<double> prev_positions(
joint_names_.size());
633 std::vector<double> prev_velocities(
joint_names_.size());
634 std::vector<double> prev_accelerations(
joint_names_.size());
638 ROS_DEBUG(
"Initial conditions for new set of splines:");
644 prev_positions[i] = position;
648 ROS_ERROR(
"Cannot get joint_state, not executing trajectory");
651 prev_velocities[i] = 0.0;
652 prev_accelerations[i] = 0.0;
654 ROS_DEBUG(
" %.2lf, %.2lf, %.2lf (%s)", prev_positions[i], prev_velocities[i],
658 std::vector<double> positions;
659 std::vector<double> velocities;
660 std::vector<double> accelerations;
662 std::vector<double> durations(msg->points.size());
663 if (msg->points.size() > 0)
665 durations[0] = msg->points[0].time_from_start.toSec();
667 for (
size_t i = 1; i < msg->points.size(); ++i)
669 durations[i] = (msg->points[i].time_from_start - msg->points[i - 1].time_from_start).toSec();
675 for (
size_t i = 0; i < msg->points.size(); ++i)
681 seg.
start_time = (time + msg->points[i].time_from_start).toSec() - durations[i];
682 ROS_DEBUG(
"Segment %zu start time A %f,time_from_start %f, duration, %f", i, seg.
start_time,
683 msg->points[i].time_from_start.toSec(), durations[i]);
687 seg.
start_time = (msg->header.stamp + msg->points[i].time_from_start).toSec() - durations[i];
694 if (msg->points[i].accelerations.size() != 0 && msg->points[i].accelerations.size() !=
joint_names_.size())
696 ROS_DEBUG(
"Command point %d has %d elements for the accelerations", static_cast<int>(i),
697 static_cast<int>(msg->points[i].accelerations.size()));
700 if (msg->points[i].velocities.size() != 0 && msg->points[i].velocities.size() !=
joint_names_.size())
702 ROS_DEBUG(
"Command point %d has %d elements for the velocities", static_cast<int>(i),
703 static_cast<int>(msg->points[i].velocities.size()));
706 if (msg->points[i].positions.size() !=
joint_names_.size())
708 ROS_DEBUG(
"Command point %d has %d elements for the positions", static_cast<int>(i),
709 static_cast<int>(msg->points[i].positions.size()));
714 accelerations.resize(msg->points[i].accelerations.size());
715 velocities.resize(msg->points[i].velocities.size());
716 positions.resize(msg->points[i].positions.size());
719 if (!accelerations.empty())
721 accelerations[j] = msg->points[i].accelerations[j];
723 if (!velocities.empty())
725 velocities[j] = msg->points[i].velocities[j];
727 if (!positions.empty())
729 positions[j] = msg->points[i].positions[j];
736 if (prev_accelerations.size() > 0 && accelerations.size() > 0)
739 prev_positions[j], prev_velocities[j], prev_accelerations[j],
740 positions[j], velocities[j], accelerations[j],
744 else if (prev_velocities.size() > 0 && velocities.size() > 0)
747 prev_positions[j], prev_velocities[j],
748 positions[j], velocities[j],
751 seg.
splines[j].coef.resize(6, 0.0);
755 seg.
splines[j].coef[0] = prev_positions[j];
756 if (durations[i] == 0.0)
762 seg.
splines[j].coef[1] = (positions[j] - prev_positions[j]) / durations[i];
776 prev_positions = positions;
777 prev_velocities = velocities;
778 prev_accelerations = accelerations;
785 ROS_ERROR(
"The new trajectory was null!");
789 ROS_DEBUG(
"The new trajectory has %d segments", static_cast<int>(traj.size()));
791 std::vector<sr_robot_msgs::joint> joint_vector_traj;
792 unsigned int controller_pub_idx = 0;
794 std_msgs::Float64 target_msg;
795 sr_robot_msgs::sendupdate sendupdate_msg_traj;
800 joint_vector_traj.clear();
804 sr_robot_msgs::joint joint;
806 joint_vector_traj.push_back(joint);
811 sendupdate_msg_traj.sendupdate_length = joint_vector_traj.size();
812 ROS_DEBUG(
"Trajectory received: %d joints / %d msg length", static_cast<int>(msg->joint_names.size()),
813 sendupdate_msg_traj.sendupdate_length);
820 ROS_DEBUG(
"Entering the execution loop");
833 while (seg + 1 < static_cast<int>(traj.size()) && traj[seg + 1].start_time < time.
toSec())
839 if ((traj[traj.size() - 1].start_time + traj[traj.size() - 1].duration) < time.
toSec())
841 ROS_DEBUG(
"trajectory is finished %f<%f", (traj[traj.size() - 1].start_time + traj[traj.size() - 1].duration),
848 if (traj.size() == 0)
849 ROS_ERROR(
"No segments in the trajectory");
851 ROS_ERROR(
"No earlier segments. First segment starts at %.3lf (now = %.3lf)", traj[0].start_time,
859 for (
size_t i = 0; i <
q.size(); ++i)
862 time.
toSec() - traj[seg].start_time,
869 ROS_INFO(
"Joint Trajectory Stopping");
875 ROS_DEBUG(
"Update the targets");
877 sensor_msgs::JointState desired_joint_state_msg;
880 desired_joint_state_msg.name.push_back(
joint_names_[i]);
881 desired_joint_state_msg.position.push_back(
q[i]);
882 desired_joint_state_msg.velocity.push_back(
qd[i]);
883 desired_joint_state_msg.effort.push_back(0.0);
888 target_msg.data =
q[i];
894 joint_vector_traj[i].joint_target =
q[i] * 57.3;
895 ROS_DEBUG(
"traj[%s]: %f", joint_vector_traj[i].joint_name.c_str(), joint_vector_traj[i].joint_target);
898 ROS_DEBUG(
"Targets updated");
905 sendupdate_msg_traj.sendupdate_list = joint_vector_traj;
910 ROS_DEBUG(
"Now sleep and loop");
911 sleeping_time.
sleep();
913 ROS_DEBUG(
"redo loop");
921 int main(
int argc,
char **argv)
923 ros::init(argc, argv,
"sr_joint_trajectory_action_controller");
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
void commandCB(const trajectory_msgs::JointTrajectoryConstPtr &msg)
static void sampleSplineWithTimeBounds(const std::vector< double > &coefficients, double duration, double time, double &position, double &velocity, double &acceleration)
void publish(const boost::shared_ptr< M > &message) const
ros::Publisher sr_hand_target_pub
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
std::map< std::string, unsigned int > jointPubIdxMap
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool getPosition(std::string joint_name, double &position)
bool call(MReq &req, MRes &res)
actionlib::SimpleActionServer< control_msgs::FollowJointTrajectoryAction > JTAS
ros::Publisher sr_arm_target_pub
ros::Publisher desired_joint_state_pusblisher
ROSCPP_DECL void spin(Spinner &spinner)
JointTrajectoryActionController()
std::map< std::string, std::string > jointControllerMap
std::vector< double > qdd
static void getQuinticSplineCoefficients(double start_pos, double start_vel, double start_acc, double end_pos, double end_vel, double end_acc, double time, std::vector< double > &coefficients)
std::map< std::string, unsigned int > joint_state_idx_map
boost::shared_ptr< JTAS > action_server
int main(int argc, char **argv)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
std::vector< ros::Publisher > controller_publishers
static void generatePowers(int n, double x, double *powers)
static void sampleQuinticSpline(const std::vector< double > &coefficients, double time, double &position, double &velocity, double &acceleration)
Samples a quintic spline segment at a particular time.
std::vector< Spline > splines
ros::Subscriber command_sub
bool getParam(const std::string &key, std::string &s) const
std::vector< Segment > SpecifiedTrajectory
std::vector< std::string > joint_names_
ROSCPP_DECL void shutdown()
ros::ServiceClient joint_state_client
void execute_trajectory(const control_msgs::FollowJointTrajectoryGoalConstPtr &goal)
static void getCubicSplineCoefficients(double start_pos, double start_vel, double end_pos, double end_vel, double time, std::vector< double > &coefficients)
std::map< std::string, double > joint_state_map
Implement an actionlib server to execute a control_msgs::JointTrajectoryAction. Follows the given tra...
ROSCPP_DECL bool waitForService(const std::string &service_name, int32_t timeout)