You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

413 lines
14 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. ## FollowJointTrajectory Commands
  2. Stretch ROS driver offers a [`FollowJointTrajectory`](http://docs.ros.org/en/api/control_msgs/html/action/FollowJointTrajectory.html) action service for its arm. Within this tutorial, we will have a simple FollowJointTrajectory command sent to a Stretch robot to execute.
  3. ## Stow Command Example
  4. <p align="center">
  5. <img src="https://raw.githubusercontent.com/hello-robot/stretch_tutorials/noetic/images/stow_command.gif"/>
  6. </p>
  7. Begin by running the following command in a terminal.
  8. ```{.bash .shell-prompt}
  9. roslaunch stretch_core stretch_driver.launch
  10. ```
  11. In a new terminal, switch the mode to `position` mode using a rosservice call. Then run the stow command node.
  12. ```{.bash .shell-prompt}
  13. rosservice call /switch_to_position_mode
  14. cd catkin_ws/src/stretch_tutorials/src/
  15. python3 stow_command.py
  16. ```
  17. This will send a `FollowJointTrajectory` command to stow Stretch's arm.
  18. ### The Code
  19. ```python
  20. #!/usr/bin/env python3
  21. import rospy
  22. from control_msgs.msg import FollowJointTrajectoryGoal
  23. from trajectory_msgs.msg import JointTrajectoryPoint
  24. import hello_helpers.hello_misc as hm
  25. import time
  26. class StowCommand(hm.HelloNode):
  27. '''
  28. A class that sends a joint trajectory goal to stow the Stretch's arm.
  29. '''
  30. def __init__(self):
  31. hm.HelloNode.__init__(self)
  32. def issue_stow_command(self):
  33. '''
  34. Function that makes an action call and sends stow position goal.
  35. :param self: The self reference.
  36. '''
  37. stow_point = JointTrajectoryPoint()
  38. stow_point.time_from_start = rospy.Duration(0.000)
  39. stow_point.positions = [0.2, 0.0, 3.4]
  40. trajectory_goal = FollowJointTrajectoryGoal()
  41. trajectory_goal.trajectory.joint_names = ['joint_lift', 'wrist_extension', 'joint_wrist_yaw']
  42. trajectory_goal.trajectory.points = [stow_point]
  43. trajectory_goal.trajectory.header.stamp = rospy.Time(0.0)
  44. trajectory_goal.trajectory.header.frame_id = 'base_link'
  45. self.trajectory_client.send_goal(trajectory_goal)
  46. rospy.loginfo('Sent stow goal = {0}'.format(trajectory_goal))
  47. self.trajectory_client.wait_for_result()
  48. def main(self):
  49. '''
  50. Function that initiates stow_command function.
  51. :param self: The self reference.
  52. '''
  53. hm.HelloNode.main(self, 'stow_command', 'stow_command', wait_for_first_pointcloud=False)
  54. rospy.loginfo('stowing...')
  55. self.issue_stow_command()
  56. time.sleep(2)
  57. if __name__ == '__main__':
  58. try:
  59. node = StowCommand()
  60. node.main()
  61. except KeyboardInterrupt:
  62. rospy.loginfo('interrupt received, so shutting down')
  63. ```
  64. ### The Code Explained
  65. Now let's break the code down.
  66. ```python
  67. #!/usr/bin/env python3
  68. ```
  69. Every Python ROS [Node](http://wiki.ros.org/Nodes) will have this declaration at the top. The first line makes sure your script is executed as a Python3 script.
  70. ```python
  71. import rospy
  72. from control_msgs.msg import FollowJointTrajectoryGoal
  73. from trajectory_msgs.msg import JointTrajectoryPoint
  74. import hello_helpers.hello_misc as hm
  75. import time
  76. ```
  77. You need to import `rospy` if you are writing a ROS [Node](http://wiki.ros.org/Nodes). Import the `FollowJointTrajectoryGoal` from the [control_msgs.msg](http://wiki.ros.org/control_msgs) package to control the Stretch robot. Import `JointTrajectoryPoint` from the [trajectory_msgs](http://wiki.ros.org/trajectory_msgs) package to define robot trajectories. The [hello_helpers](https://github.com/hello-robot/stretch_ros/tree/master/hello_helpers) package consists of a module that provides various Python scripts used across [stretch_ros](https://github.com/hello-robot/stretch_ros). In this instance, we are importing the `hello_misc` script.
  78. ```python
  79. class StowCommand(hm.HelloNode):
  80. def __init__(self):
  81. hm.HelloNode.__init__(self)
  82. ```
  83. The `StowCommand` class inherits the `HelloNode` class from `hm` and is initialized.
  84. ```python
  85. def issue_stow_command(self):
  86. stow_point = JointTrajectoryPoint()
  87. stow_point.time_from_start = rospy.Duration(0.000)
  88. stow_point.positions = [0.2, 0.0, 3.4]
  89. ```
  90. The `issue_stow_command()` is the name of the function that will stow Stretch's arm. Within the function, we set *stow_point* as a `JointTrajectoryPoint`and provide desired positions (in meters). These are the positions of the lift, wrist extension, and yaw of the wrist, respectively. These are defined next.
  91. ```python
  92. trajectory_goal = FollowJointTrajectoryGoal()
  93. trajectory_goal.trajectory.joint_names = ['joint_lift', 'wrist_extension', 'joint_wrist_yaw']
  94. trajectory_goal.trajectory.points = [stow_point]
  95. trajectory_goal.trajectory.header.stamp = rospy.Time(0.0)
  96. trajectory_goal.trajectory.header.frame_id = 'base_link'
  97. ```
  98. Set `trajectory_goal` as a `FollowJointTrajectoryGoal` and define the joint names as a list. Then `trajectory_goal.trajectory.points` is defined by the positions set in `stow_point`. Specify the coordinate frame that we want (*base_link*) and set the time to be now.
  99. ```python
  100. self.trajectory_client.send_goal(trajectory_goal)
  101. rospy.loginfo('Sent stow goal = {0}'.format(trajectory_goal))
  102. self.trajectory_client.wait_for_result()
  103. ```
  104. Make the action call and send the goal. The last line of code waits for the result before it exits the python script.
  105. ```python
  106. def main(self):
  107. hm.HelloNode.main(self, 'stow_command', 'stow_command', wait_for_first_pointcloud=False)
  108. rospy.loginfo('stowing...')
  109. self.issue_stow_command()
  110. time.sleep(2)
  111. ```
  112. Create a function, `main()`, to set up the `hm.HelloNode` class and issue the stow command.
  113. ```python
  114. if __name__ == '__main__':
  115. try:
  116. node = StowCommand()
  117. node.main()
  118. except KeyboardInterrupt:
  119. rospy.loginfo('interrupt received, so shutting down')
  120. ```
  121. Declare object, *node*, from the `StowCommand()` class. Then run the `main()` function.
  122. ## Multipoint Command Example
  123. <p align="center">
  124. <img src="https://raw.githubusercontent.com/hello-robot/stretch_tutorials/noetic/images/multipoint.gif"/>
  125. </p>
  126. Begin by running the following command in a terminal:
  127. ```bash
  128. roslaunch stretch_core stretch_driver.launch
  129. ```
  130. In a new terminal, switch the mode to `position` mode using a rosservice call. Then run the multipoint command node.
  131. ```bash
  132. rosservice call /switch_to_position_mode
  133. cd catkin_ws/src/stretch_tutorials/src/
  134. python3 multipoint_command.py
  135. ```
  136. This will send a list of `JointTrajectoryPoint` message types to move Stretch's arm.
  137. ### The Code
  138. ```python
  139. #!/usr/bin/env python3
  140. import rospy
  141. import time
  142. from control_msgs.msg import FollowJointTrajectoryGoal
  143. from trajectory_msgs.msg import JointTrajectoryPoint
  144. import hello_helpers.hello_misc as hm
  145. class MultiPointCommand(hm.HelloNode):
  146. """
  147. A class that sends multiple joint trajectory goals to the stretch robot.
  148. """
  149. def __init__(self):
  150. hm.HelloNode.__init__(self)
  151. def issue_multipoint_command(self):
  152. """
  153. Function that makes an action call and sends multiple joint trajectory goals
  154. to the joint_lift, wrist_extension, and joint_wrist_yaw.
  155. :param self: The self reference.
  156. """
  157. point0 = JointTrajectoryPoint()
  158. point0.positions = [0.2, 0.0, 3.4]
  159. point0.velocities = [0.2, 0.2, 2.5]
  160. point0.accelerations = [1.0, 1.0, 3.5]
  161. point1 = JointTrajectoryPoint()
  162. point1.positions = [0.3, 0.1, 2.0]
  163. point2 = JointTrajectoryPoint()
  164. point2.positions = [0.5, 0.2, -1.0]
  165. point3 = JointTrajectoryPoint()
  166. point3.positions = [0.6, 0.3, 0.0]
  167. point4 = JointTrajectoryPoint()
  168. point4.positions = [0.8, 0.2, 1.0]
  169. point5 = JointTrajectoryPoint()
  170. point5.positions = [0.5, 0.1, 0.0]
  171. trajectory_goal = FollowJointTrajectoryGoal()
  172. trajectory_goal.trajectory.joint_names = ['joint_lift', 'wrist_extension', 'joint_wrist_yaw']
  173. trajectory_goal.trajectory.points = [point0, point1, point2, point3, point4, point5]
  174. trajectory_goal.trajectory.header.stamp = rospy.Time(0.0)
  175. trajectory_goal.trajectory.header.frame_id = 'base_link'
  176. self.trajectory_client.send_goal(trajectory_goal)
  177. rospy.loginfo('Sent list of goals = {0}'.format(trajectory_goal))
  178. self.trajectory_client.wait_for_result()
  179. def main(self):
  180. """
  181. Function that initiates the multipoint_command function.
  182. :param self: The self reference.
  183. """
  184. hm.HelloNode.main(self, 'multipoint_command', 'multipoint_command', wait_for_first_pointcloud=False)
  185. rospy.loginfo('issuing multipoint command...')
  186. self.issue_multipoint_command()
  187. time.sleep(2)
  188. if __name__ == '__main__':
  189. try:
  190. node = MultiPointCommand()
  191. node.main()
  192. except KeyboardInterrupt:
  193. rospy.loginfo('interrupt received, so shutting down')
  194. ```
  195. ### The Code Explained
  196. Seeing that there are similarities between the multipoint and stow command nodes, we will only break down the different components of the `multipoint_command` node.
  197. ```python
  198. point0 = JointTrajectoryPoint()
  199. point0.positions = [0.2, 0.0, 3.4]
  200. ```
  201. Set `point0` as a `JointTrajectoryPoint`and provide desired positions. These are the positions of the lift, wrist extension, and yaw of the wrist, respectively. The lift and wrist extension positions are expressed in meters, whereas the wrist yaw is in radians.
  202. ```python
  203. point0.velocities = [0.2, 0.2, 2.5]
  204. ```
  205. Provide the desired velocity of the lift (m/s), wrist extension (m/s), and wrist yaw (rad/s) for `point0`.
  206. ```python
  207. point0.accelerations = [1.0, 1.0, 3.5]
  208. ```
  209. Provide desired accelerations of the lift (m/s^2), wrist extension (m/s^2), and wrist yaw (rad/s^2).
  210. !!! note
  211. The lift and wrist extension can only go up to 0.2 m/s. If you do not provide any velocities or accelerations for the lift or wrist extension, then they go to their default values. However, the Velocity and Acceleration of the wrist yaw will stay the same from the previous value unless updated.
  212. ```python
  213. trajectory_goal = FollowJointTrajectoryGoal()
  214. trajectory_goal.trajectory.joint_names = ['joint_lift', 'wrist_extension', 'joint_wrist_yaw']
  215. trajectory_goal.trajectory.points = [point0, point1, point2, point3, point4, point5]
  216. trajectory_goal.trajectory.header.stamp = rospy.Time(0.0)
  217. trajectory_goal.trajectory.header.frame_id = 'base_link'
  218. ```
  219. Set `trajectory_goal` as a `FollowJointTrajectoryGoal` and define the joint names as a list. Then `trajectory_goal.trajectory.points` is defined by a list of the 6 points. Specify the coordinate frame that we want (*base_link*) and set the time to be now.
  220. ## Single Joint Actuator
  221. <p align="center">
  222. <img src="https://raw.githubusercontent.com/hello-robot/stretch_tutorials/noetic/images/single_joint_actuator.gif"/>
  223. </p>
  224. You can also actuate a single joint for the Stretch. Below is the list of joints and their position limit.
  225. ```{.bash .no-copy}
  226. ############################# JOINT LIMITS #############################
  227. joint_lift: lower_limit = 0.00, upper_limit = 1.10 # in meters
  228. wrist_extension: lower_limit = 0.00, upper_limit = 0.50 # in meters
  229. joint_wrist_yaw: lower_limit = -1.75, upper_limit = 4.00 # in radians
  230. joint_head_pan: lower_limit = -3.90, upper_limit = 1.50 # in radians
  231. joint_head_tilt: lower_limit = -1.53, upper_limit = 0.79 # in radians
  232. joint_gripper_finger_left: lower_limit = -0.6, upper_limit = 0.6 # in radians
  233. # INCLUDED JOINTS IN POSITION MODE
  234. translate_mobile_base: No lower or upper limit. Defined by a step size in meters
  235. rotate_mobile_base: No lower or upper limit. Defined by a step size in radians
  236. ########################################################################
  237. ```
  238. Begin by running the following command in a terminal.
  239. ```bash
  240. roslaunch stretch_core stretch_driver.launch
  241. ```
  242. In a new terminal, switch the mode to `position` mode using a rosservice call. Then run the single joint actuator node.
  243. ```bash
  244. rosservice call /switch_to_position_mode
  245. cd catkin_ws/src/stretch_tutorials/src/
  246. python3 single_joint_actuator.py
  247. ```
  248. This will send a list of `JointTrajectoryPoint` message types to move Stretch's arm.
  249. The joint, `joint_gripper_finger_left`, is only needed when actuating the gripper.
  250. ### The Code
  251. ```python
  252. #!/usr/bin/env python3
  253. import rospy
  254. import time
  255. from control_msgs.msg import FollowJointTrajectoryGoal
  256. from trajectory_msgs.msg import JointTrajectoryPoint
  257. import hello_helpers.hello_misc as hm
  258. class SingleJointActuator(hm.HelloNode):
  259. """
  260. A class that sends multiple joint trajectory goals to a single joint.
  261. """
  262. def __init__(self):
  263. hm.HelloNode.__init__(self)
  264. def issue_command(self):
  265. """
  266. Function that makes an action call and sends joint trajectory goals
  267. to a single joint
  268. :param self: The self reference.
  269. """
  270. trajectory_goal = FollowJointTrajectoryGoal()
  271. trajectory_goal.trajectory.joint_names = ['joint_head_pan']
  272. point0 = JointTrajectoryPoint()
  273. point0.positions = [0.65]
  274. # point1 = JointTrajectoryPoint()
  275. # point1.positions = [0.5]
  276. trajectory_goal.trajectory.points = [point0]#, point1]
  277. trajectory_goal.trajectory.header.stamp = rospy.Time(0.0)
  278. trajectory_goal.trajectory.header.frame_id = 'base_link'
  279. self.trajectory_client.send_goal(trajectory_goal)
  280. rospy.loginfo('Sent goal = {0}'.format(trajectory_goal))
  281. self.trajectory_client.wait_for_result()
  282. def main(self):
  283. """
  284. Function that initiates the issue_command function.
  285. :param self: The self reference.
  286. """
  287. hm.HelloNode.main(self, 'issue_command', 'issue_command', wait_for_first_pointcloud=False)
  288. rospy.loginfo('issuing command...')
  289. self.issue_command()
  290. time.sleep(2)
  291. if __name__ == '__main__':
  292. try:
  293. node = SingleJointActuator()
  294. node.main()
  295. except KeyboardInterrupt:
  296. rospy.loginfo('interrupt received, so shutting down')
  297. ```
  298. ### The Code Explained
  299. Since the code is quite similar to the `multipoint_command` code, we will only review the parts that differ.
  300. Now let's break the code down.
  301. ```python
  302. trajectory_goal = FollowJointTrajectoryGoal()
  303. trajectory_goal.trajectory.joint_names = ['joint_head_pan']
  304. ```
  305. Here we only input the joint name that we want to actuate. In this instance, we will actuate the `joint_head_pan`.
  306. ```python
  307. point0 = JointTrajectoryPoint()
  308. point0.positions = [0.65]
  309. # point1 = JointTrajectoryPoint()
  310. # point1.positions = [0.5]
  311. ```
  312. Set `point0` as a `JointTrajectoryPoint`and provide the desired position. You also have the option to send multiple point positions rather than one.
  313. ```python
  314. trajectory_goal.trajectory.points = [point0]#, point1]
  315. trajectory_goal.trajectory.header.stamp = rospy.Time(0.0)
  316. trajectory_goal.trajectory.header.frame_id = 'base_link'
  317. ```
  318. Set `trajectory_goal` as a `FollowJointTrajectoryGoal` and define the joint names as a list. Then `trajectory_goal.trajectory.points` set by your list of points. Specify the coordinate frame that we want (*base_link*) and set the time to be now.