|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2024 FZI Forschungszentrum Informatik |
| 3 | +# |
| 4 | +# Redistribution and use in source and binary forms, with or without |
| 5 | +# modification, are permitted provided that the following conditions are met: |
| 6 | +# |
| 7 | +# * Redistributions of source code must retain the above copyright |
| 8 | +# notice, this list of conditions and the following disclaimer. |
| 9 | +# |
| 10 | +# * Redistributions in binary form must reproduce the above copyright |
| 11 | +# notice, this list of conditions and the following disclaimer in the |
| 12 | +# documentation and/or other materials provided with the distribution. |
| 13 | +# |
| 14 | +# * Neither the name of the {copyright_holder} nor the names of its |
| 15 | +# contributors may be used to endorse or promote products derived from |
| 16 | +# this software without specific prior written permission. |
| 17 | +# |
| 18 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 22 | +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | +# POSSIBILITY OF SUCH DAMAGE. |
| 29 | + |
| 30 | +# |
| 31 | +# Author: Felix Exner |
| 32 | + |
| 33 | +# This is an example of how to interface the robot without any additional ROS components. For |
| 34 | +# real-life applications, we do recommend to use something like MoveIt! |
| 35 | + |
| 36 | +import time |
| 37 | + |
| 38 | +import rclpy |
| 39 | +from rclpy.action import ActionClient |
| 40 | + |
| 41 | +from builtin_interfaces.msg import Duration |
| 42 | +from trajectory_msgs.msg import JointTrajectory, JointTrajectoryPoint |
| 43 | +from control_msgs.action import FollowJointTrajectory |
| 44 | + |
| 45 | +TRAJECTORIES = { |
| 46 | + "traj0": [ |
| 47 | + { |
| 48 | + "positions": [0.043128, -1.28824, 1.37179, -1.82208, -1.63632, -0.18], |
| 49 | + "velocities": [0, 0, 0, 0, 0, 0], |
| 50 | + "time_from_start": Duration(sec=4, nanosec=0), |
| 51 | + }, |
| 52 | + { |
| 53 | + "positions": [-0.195016, -1.70093, 0.902027, -0.944217, -1.52982, -0.195171], |
| 54 | + "velocities": [0, 0, 0, 0, 0, 0], |
| 55 | + "time_from_start": Duration(sec=8, nanosec=0), |
| 56 | + }, |
| 57 | + ], |
| 58 | + "traj1": [ |
| 59 | + { |
| 60 | + "positions": [-0.195016, -1.70094, 0.902027, -0.944217, -1.52982, -0.195171], |
| 61 | + "velocities": [0, 0, 0, 0, 0, 0], |
| 62 | + "time_from_start": Duration(sec=0, nanosec=0), |
| 63 | + }, |
| 64 | + { |
| 65 | + "positions": [0.30493, -0.982258, 0.955637, -1.48215, -1.72737, 0.204445], |
| 66 | + "velocities": [0, 0, 0, 0, 0, 0], |
| 67 | + "time_from_start": Duration(sec=8, nanosec=0), |
| 68 | + }, |
| 69 | + ], |
| 70 | +} |
| 71 | + |
| 72 | + |
| 73 | +class JTCClient(rclpy.node.Node): |
| 74 | + """Small test client for the jtc.""" |
| 75 | + |
| 76 | + def __init__(self): |
| 77 | + super().__init__("jtc_client") |
| 78 | + self.declare_parameter("controller_name", "scaled_joint_trajectory_controller") |
| 79 | + self.declare_parameter( |
| 80 | + "joints", |
| 81 | + [ |
| 82 | + "shoulder_pan_joint", |
| 83 | + "shoulder_lift_joint", |
| 84 | + "elbow_joint", |
| 85 | + "wrist_1_joint", |
| 86 | + "wrist_2_joint", |
| 87 | + "wrist_3_joint", |
| 88 | + ], |
| 89 | + ) |
| 90 | + |
| 91 | + controller_name = self.get_parameter("controller_name").value + "/follow_joint_trajectory" |
| 92 | + self.joints = self.get_parameter("joints").value |
| 93 | + |
| 94 | + if self.joints is None or len(self.joints) == 0: |
| 95 | + raise Exception('"joints" parameter is required') |
| 96 | + |
| 97 | + self._action_client = ActionClient(self, FollowJointTrajectory, controller_name) |
| 98 | + self.get_logger().info(f"Waiting for action server on {controller_name}") |
| 99 | + self._action_client.wait_for_server() |
| 100 | + |
| 101 | + self.parse_trajectories() |
| 102 | + self.i = 0 |
| 103 | + self._send_goal_future = None |
| 104 | + self._get_result_future = None |
| 105 | + self.execute_next_trajectory() |
| 106 | + |
| 107 | + def parse_trajectories(self): |
| 108 | + self.goals = {} |
| 109 | + |
| 110 | + for traj_name in TRAJECTORIES: |
| 111 | + goal = JointTrajectory() |
| 112 | + goal.joint_names = self.joints |
| 113 | + for pt in TRAJECTORIES[traj_name]: |
| 114 | + point = JointTrajectoryPoint() |
| 115 | + point.positions = pt["positions"] |
| 116 | + point.velocities = pt["velocities"] |
| 117 | + point.time_from_start = pt["time_from_start"] |
| 118 | + goal.points.append(point) |
| 119 | + |
| 120 | + self.goals[traj_name] = goal |
| 121 | + |
| 122 | + def execute_next_trajectory(self): |
| 123 | + if self.i >= len(self.goals): |
| 124 | + self.get_logger().info("Done with all trajectories") |
| 125 | + raise SystemExit |
| 126 | + traj_name = list(self.goals)[self.i] |
| 127 | + self.i = self.i + 1 |
| 128 | + if traj_name: |
| 129 | + self.execute_trajectory(traj_name) |
| 130 | + |
| 131 | + def execute_trajectory(self, traj_name): |
| 132 | + self.get_logger().info(f"Executing trajectory {traj_name}") |
| 133 | + goal = FollowJointTrajectory.Goal() |
| 134 | + goal.trajectory = self.goals[traj_name] |
| 135 | + |
| 136 | + goal.goal_time_tolerance = Duration(sec=0, nanosec=1000) |
| 137 | + |
| 138 | + self._send_goal_future = self._action_client.send_goal_async(goal) |
| 139 | + self._send_goal_future.add_done_callback(self.goal_response_callback) |
| 140 | + |
| 141 | + def goal_response_callback(self, future): |
| 142 | + goal_handle = future.result() |
| 143 | + if not goal_handle.accepted: |
| 144 | + self.get_logger().error("Goal rejected :(") |
| 145 | + raise RuntimeError("Goal rejected :(") |
| 146 | + |
| 147 | + self.get_logger().debug("Goal accepted :)") |
| 148 | + |
| 149 | + self._get_result_future = goal_handle.get_result_async() |
| 150 | + self._get_result_future.add_done_callback(self.get_result_callback) |
| 151 | + |
| 152 | + def get_result_callback(self, future): |
| 153 | + result = future.result().result |
| 154 | + self.get_logger().info(f"Done with result: {self.error_code_to_str(result.error_code)}") |
| 155 | + if result.error_code == FollowJointTrajectory.Result.SUCCESSFUL: |
| 156 | + time.sleep(2) |
| 157 | + self.execute_next_trajectory() |
| 158 | + else: |
| 159 | + raise RuntimeError("Executing trajectory failed") |
| 160 | + |
| 161 | + @staticmethod |
| 162 | + def error_code_to_str(error_code): |
| 163 | + if error_code == FollowJointTrajectory.Result.SUCCESSFUL: |
| 164 | + return "SUCCESSFUL" |
| 165 | + if error_code == FollowJointTrajectory.Result.INVALID_GOAL: |
| 166 | + return "INVALID_GOAL" |
| 167 | + if error_code == FollowJointTrajectory.Result.INVALID_JOINTS: |
| 168 | + return "INVALID_JOINTS" |
| 169 | + if error_code == FollowJointTrajectory.Result.OLD_HEADER_TIMESTAMP: |
| 170 | + return "OLD_HEADER_TIMESTAMP" |
| 171 | + if error_code == FollowJointTrajectory.Result.PATH_TOLERANCE_VIOLATED: |
| 172 | + return "PATH_TOLERANCE_VIOLATED" |
| 173 | + if error_code == FollowJointTrajectory.Result.GOAL_TOLERANCE_VIOLATED: |
| 174 | + return "GOAL_TOLERANCE_VIOLATED" |
| 175 | + |
| 176 | + |
| 177 | +def main(args=None): |
| 178 | + rclpy.init(args=args) |
| 179 | + |
| 180 | + jtc_client = JTCClient() |
| 181 | + try: |
| 182 | + rclpy.spin(jtc_client) |
| 183 | + except SystemExit: |
| 184 | + rclpy.logging.get_logger("jtc_client").info("Done") |
| 185 | + |
| 186 | + rclpy.shutdown() |
| 187 | + |
| 188 | + |
| 189 | +if __name__ == "__main__": |
| 190 | + main() |
0 commit comments