hebiros_actions.cpp
Go to the documentation of this file.
1 #include "hebiros_actions.h"
2 
3 #include "hebiros.h"
4 
6 
7 using namespace hebiros;
8 
9 std::map<std::string,
10  std::shared_ptr<actionlib::SimpleActionServer<hebiros::TrajectoryAction>>>
12 
13 void HebirosActions::registerGroupActions(std::string group_name) {
14 
15  trajectory_actions[group_name] = std::make_shared<
17  *HebirosNode::n_ptr, "hebiros/"+group_name+"/trajectory",
18  boost::bind(&HebirosActions::trajectory, this, _1, group_name), false);
19 
20  trajectory_actions[group_name]->start();
21 }
22 
23 void HebirosActions::trajectory(const TrajectoryGoalConstPtr& goal, std::string group_name) {
24 
25  auto& registry = HebirosGroupRegistry::Instance();
26  HebirosGroup* group = registry.getGroup(group_name);
27 
28  if (!group) {
29  ROS_WARN("Group not found.");
30  return;
31  }
32 
33  std::shared_ptr<actionlib::SimpleActionServer<TrajectoryAction>> action_server =
34  trajectory_actions[group_name];
35 
36  int num_waypoints = goal->waypoints.size();
37  if (num_waypoints < 1) {
38  ROS_WARN("No waypoints sent.");
39  return;
40  }
41  int num_joints = goal->waypoints[0].names.size();
42 
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);
47 
48  for (int i = 0; i < num_waypoints; i++) {
49  time(i) = goal->times[i];
50  }
51 
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];
55 
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];
60 
61  positions(joint_index, j) = position;
62  velocities(joint_index, j) = velocity;
63  accelerations(joint_index, j) = acceleration;
64  }
65  }
66 
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);
71 
72  double trajectory_duration = trajectory->getDuration();
73  double previous_time;
74  double current_time;
75  double loop_duration;
76  TrajectoryFeedback feedback;
77 
78  ros::Rate loop_rate(HebirosParameters::getInt("hebiros/action_frequency"));
79 
80  ROS_INFO("Group [%s]: Executing trajectory", group_name.c_str());
81  previous_time = ros::Time::now().toSec();
82  for (double t = 0; t < trajectory_duration; t += loop_duration)
83  {
84  if (action_server->isPreemptRequested() || !ros::ok()) {
85  ROS_INFO("Group [%s]: Preempted trajectory", group_name.c_str());
86  action_server->setPreempted();
87  return;
88  }
89 
90  feedback.percent_complete = (t / trajectory_duration) * 100;
91  action_server->publishFeedback(feedback);
92 
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);
98 
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);
105  }
106  HebirosNode::publishers_physical.commandJointState(joint_state_msg, group_name);
107 
108  ros::spinOnce();
109  loop_rate.sleep();
110  current_time = ros::Time::now().toSec();
111  loop_duration = current_time - previous_time;
112  previous_time = current_time;
113  }
114 
115  TrajectoryResult result;
116  result.final_state = group->joint_state_msg;
117  action_server->setSucceeded(result);
118  ROS_INFO("Group [%s]: Finished executing trajectory", group_name.c_str());
119 }
static int getInt(std::string name)
static HebirosPublishersPhysical publishers_physical
Definition: hebiros.h:80
sensor_msgs::JointState joint_state_msg
Definition: hebiros_group.h:19
static std::map< std::string, std::shared_ptr< actionlib::SimpleActionServer< hebiros::TrajectoryAction > > > trajectory_actions
#define ROS_WARN(...)
static std::shared_ptr< ros::NodeHandle > n_ptr
Definition: hebiros.h:78
void commandJointState(sensor_msgs::JointState joint_state_msg, std::string group_name)
#define ROS_INFO(...)
static HebirosGroupRegistry & Instance()
ROSCPP_DECL bool ok()
std::map< std::string, int > joints
Definition: hebiros_group.h:18
void registerGroupActions(std::string group_name)
bool sleep()
void trajectory(const hebiros::TrajectoryGoalConstPtr &goal, std::string group_name)
static Time now()
ROSCPP_DECL void spinOnce()


hebiros
Author(s): Xavier Artache , Matthew Tesch
autogenerated on Thu Sep 3 2020 04:08:14