joint_trajectory_action_controller.cpp
Go to the documentation of this file.
1 /*
2 * Copyright 2011 Shadow Robot Company Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16 
30 #include <std_msgs/Float64.h>
31 #include <boost/algorithm/string.hpp>
32 #include <string>
33 #include <vector>
34 
35 namespace shadowrobot
36 {
38  {
40  new JTAS("/r_arm_controller/joint_trajectory_action",
42  false));
43 
44  // Create a map of joint names to their command publishers
45  // Hand joints
46  // @todo this could be read from the controller manager
47  // rosservice call /controller_manager/list_controllers
48  std::string hand_names[] =
49  {
50  "ffj0", "ffj3", "ffj4",
51  "lfj0", "lfj3", "lfj4", "lfj5",
52  "mfj0", "mfj3", "mfj4",
53  "rfj0", "rfj3", "rfj4",
54  "thj1", "thj2", "thj3", "thj4", "thj5",
55  "wrj1", "wrj2"
56  };
57  for (size_t i = 0; i < 20; i++)
58  {
59  joint_pub[hand_names[i]] = nh.advertise<std_msgs::Float64>(
60  "/sh_" + hand_names[i] + "_mixed_position_velocity_controller/command", 2);
61 
62  joint_pub[boost::to_upper_copy(hand_names[i])] = nh.advertise<std_msgs::Float64>(
63  "/sh_" + hand_names[i] + "_mixed_position_velocity_controller/command", 2);
64  }
65 
66  // Arm joints
67  std::string arm_names[] = {"sr", "ss", "es", "er"};
68  for (size_t i = 0; i < 4; i++)
69  {
70  joint_pub[arm_names[i]] = nh.advertise<std_msgs::Float64>(
71  "/sa_" + arm_names[i] + "_position_controller/command", 2);
72  }
73 
74  // Arm joints: 2 naming conventions
75  std::string arm_names_2[] = {"ShoulderJRotate", "ShoulderJSwing", "ElbowJSwing", "ElbowJRotate"};
76  for (size_t i = 0; i < 4; i++)
77  {
78  joint_pub[arm_names_2[i]] = nh.advertise<std_msgs::Float64>(
79  "/sa_" + arm_names[i] + "_position_controller/command", 2);
80  }
81 
82  ROS_DEBUG("Starting JointTrajectoryActionController server");
83  action_server->start();
84  }
85 
87  const control_msgs::FollowJointTrajectoryGoalConstPtr &goal
88  )
89  {
90  bool success = true;
91  std::vector<std::string> joint_names = goal->trajectory.joint_names;
92  JointTrajectoryPointVec trajectory_points = goal->trajectory.points;
93  trajectory_msgs::JointTrajectoryPoint trajectory_step;
94 
95  // @todo We should probably be looking at goal->trajectory.header.stamp to
96  // work out what time to start the action.
97  // std::cout << goal->trajectory.header.stamp << " - " << ros::Time::now() << std::endl;
98 
99  // loop through the steps
100  ros::Duration sleeping_time(0.0), last_time(0.0);
101  for (size_t index_step = 0; index_step < trajectory_points.size(); ++index_step)
102  {
103  trajectory_step = trajectory_points[index_step];
104 
105  // check if preempted (cancelled), bail out if we are
106  if (action_server->isPreemptRequested() || !ros::ok())
107  {
108  ROS_INFO("Joint Trajectory Action Preempted");
109  action_server->setPreempted();
110  success = false;
111  break;
112  }
113 
114  // send out the positions for this step to the joints
115  for (size_t i = 0; i < joint_names.size(); i++)
116  {
117  ROS_DEBUG_STREAM("trajectory: " << joint_names[i] << " " << trajectory_step.positions[i] << " / sleep: " <<
118  sleeping_time.toSec());
119  ros::Publisher pub = joint_pub[joint_names[i]];
120  std_msgs::Float64 msg;
121  msg.data = trajectory_step.positions[i];
122  pub.publish(msg);
123  }
124  // Wait until this step is supposed to be completed.
125  // @todo This assumes that the movement will be instant! We should really
126  // be working out how long the movement will take?
127  sleeping_time = trajectory_step.time_from_start - last_time;
128  sleeping_time.sleep();
129  last_time = trajectory_step.time_from_start;
130  }
131 
132  // send the result back
133  control_msgs::FollowJointTrajectoryResult joint_trajectory_result;
134  if (success)
135  {
136  action_server->setSucceeded(joint_trajectory_result);
137  }
138  else
139  {
140  action_server->setAborted(joint_trajectory_result);
141  }
142  }
143 } // namespace shadowrobot
144 
145 
146 int main(int argc, char **argv)
147 {
148  ros::init(argc, argv, "sr_joint_trajectory_action_controller");
149 
150  ros::AsyncSpinner spinner(1); // Use 1 thread
151  spinner.start();
153  ros::spin();
154  // ros::waitForShutdown();
155 
156  return 0;
157 }
158 
159 
160 /* For the emacs weenies in the crowd.
161 Local Variables:
162  c-basic-offset: 2
163 End:
164 */
165 // vim: sw=2:ts=2
std::vector< trajectory_msgs::JointTrajectoryPoint > JointTrajectoryPointVec
list joint_names
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
actionlib::SimpleActionServer< control_msgs::FollowJointTrajectoryAction > JTAS
void spinner()
#define ROS_DEBUG(...)
Implement an actionlib server to execute a control_msgs::JointTrajectoryAction. Follows the given tra...
#define ROS_INFO(...)
int main(int argc, char **argv)
ROSCPP_DECL bool ok()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ROSCPP_DECL void spin()
#define ROS_DEBUG_STREAM(args)
void execute_trajectory(const control_msgs::FollowJointTrajectoryGoalConstPtr &goal)


sr_mechanism_controllers
Author(s): Ugo Cupcic
autogenerated on Mon Feb 28 2022 23:52:30