10 std::shared_ptr<actionlib::SimpleActionServer<hebiros::TrajectoryAction>>>
15 trajectory_actions[group_name] = std::make_shared<
20 trajectory_actions[group_name]->
start();
33 std::shared_ptr<actionlib::SimpleActionServer<TrajectoryAction>> action_server =
34 trajectory_actions[group_name];
36 int num_waypoints = goal->waypoints.size();
37 if (num_waypoints < 1) {
41 int num_joints = goal->waypoints[0].names.size();
43 Eigen::MatrixXd positions(num_joints, num_waypoints);
44 Eigen::MatrixXd velocities(num_joints, num_waypoints);
45 Eigen::MatrixXd accelerations(num_joints, num_waypoints);
46 Eigen::VectorXd time(num_waypoints);
48 for (
int i = 0; i < num_waypoints; i++) {
49 time(i) = goal->times[i];
52 for (
int i = 0; i < num_joints; i++) {
53 std::string joint_name = goal->waypoints[0].names[i];
54 int joint_index = group->
joints[joint_name];
56 for (
int j = 0; j < num_waypoints; j++) {
57 double position = goal->waypoints[j].positions[i];
58 double velocity = goal->waypoints[j].velocities[i];
59 double acceleration = goal->waypoints[j].accelerations[i];
61 positions(joint_index, j) = position;
62 velocities(joint_index, j) = velocity;
63 accelerations(joint_index, j) = acceleration;
67 auto trajectory = trajectory::Trajectory::createUnconstrainedQp(
68 time, positions, &velocities, &accelerations);
69 Eigen::VectorXd position_command(num_joints);
70 Eigen::VectorXd velocity_command(num_joints);
72 double trajectory_duration = trajectory->getDuration();
76 TrajectoryFeedback feedback;
80 ROS_INFO(
"Group [%s]: Executing trajectory", group_name.c_str());
82 for (
double t = 0; t < trajectory_duration; t += loop_duration)
84 if (action_server->isPreemptRequested() || !
ros::ok()) {
85 ROS_INFO(
"Group [%s]: Preempted trajectory", group_name.c_str());
86 action_server->setPreempted();
90 feedback.percent_complete = (t / trajectory_duration) * 100;
91 action_server->publishFeedback(feedback);
93 trajectory->getState(t, &position_command, &velocity_command,
nullptr);
94 sensor_msgs::JointState joint_state_msg;
95 joint_state_msg.name.resize(num_joints);
96 joint_state_msg.position.resize(num_joints);
97 joint_state_msg.velocity.resize(num_joints);
99 for (
int i = 0; i < num_joints; i++) {
100 std::string joint_name = goal->waypoints[0].names[i];
101 int joint_index = group->
joints[joint_name];
102 joint_state_msg.name[joint_index] = joint_name;
103 joint_state_msg.position[joint_index] = position_command(i);
104 joint_state_msg.velocity[joint_index] = velocity_command(i);
111 loop_duration = current_time - previous_time;
112 previous_time = current_time;
115 TrajectoryResult result;
117 action_server->setSucceeded(result);
118 ROS_INFO(
"Group [%s]: Finished executing trajectory", group_name.c_str());
static int getInt(std::string name)
static HebirosPublishersPhysical publishers_physical
sensor_msgs::JointState joint_state_msg
static std::map< std::string, std::shared_ptr< actionlib::SimpleActionServer< hebiros::TrajectoryAction > > > trajectory_actions
static std::shared_ptr< ros::NodeHandle > n_ptr
void commandJointState(sensor_msgs::JointState joint_state_msg, std::string group_name)
static HebirosGroupRegistry & Instance()
std::map< std::string, int > joints
void registerGroupActions(std::string group_name)
void trajectory(const hebiros::TrajectoryGoalConstPtr &goal, std::string group_name)
ROSCPP_DECL void spinOnce()