Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 """
00031 Baxter RSDK Joint Position Waypoints Example
00032 """
00033 import argparse
00034 import sys
00035
00036 import rospy
00037
00038 import baxter_interface
00039
00040
00041 class Waypoints(object):
00042 def __init__(self, limb, speed, accuracy):
00043
00044 self._arm = limb
00045 self._limb = baxter_interface.Limb(self._arm)
00046
00047
00048 self._speed = speed
00049 self._accuracy = accuracy
00050
00051
00052 self._waypoints = list()
00053
00054
00055 self._is_recording = False
00056
00057
00058 print("Getting robot state... ")
00059 self._rs = baxter_interface.RobotEnable()
00060 self._init_state = self._rs.state().enabled
00061 print("Enabling robot... ")
00062 self._rs.enable()
00063
00064
00065 self._navigator_io = baxter_interface.Navigator(self._arm)
00066
00067 def _record_waypoint(self, value):
00068 """
00069 Stores joint position waypoints
00070
00071 Navigator 'OK/Wheel' button callback
00072 """
00073 if value:
00074 print("Waypoint Recorded")
00075 self._waypoints.append(self._limb.joint_angles())
00076
00077 def _stop_recording(self, value):
00078 """
00079 Sets is_recording to false
00080
00081 Navigator 'Rethink' button callback
00082 """
00083
00084 if value:
00085 self._is_recording = False
00086
00087 def record(self):
00088 """
00089 Records joint position waypoints upon each Navigator 'OK/Wheel' button
00090 press.
00091 """
00092 rospy.loginfo("Waypoint Recording Started")
00093 print("Press Navigator 'OK/Wheel' button to record a new joint "
00094 "joint position waypoint.")
00095 print("Press Navigator 'Rethink' button when finished recording "
00096 "waypoints to begin playback")
00097
00098
00099 self._navigator_io.button0_changed.connect(self._record_waypoint)
00100
00101 self._navigator_io.button2_changed.connect(self._stop_recording)
00102
00103
00104 self._is_recording = True
00105
00106
00107 while self._is_recording and not rospy.is_shutdown():
00108 rospy.sleep(1.0)
00109
00110
00111 self._navigator_io.button0_changed.disconnect(self._record_waypoint)
00112 self._navigator_io.button2_changed.disconnect(self._stop_recording)
00113
00114 def playback(self):
00115 """
00116 Loops playback of recorded joint position waypoints until program is
00117 exited
00118 """
00119 rospy.sleep(1.0)
00120
00121 rospy.loginfo("Waypoint Playback Started")
00122 print(" Press Ctrl-C to stop...")
00123
00124
00125 self._limb.set_joint_position_speed(self._speed)
00126
00127
00128 loop = 0
00129 while not rospy.is_shutdown():
00130 loop += 1
00131 print("Waypoint playback loop #%d " % (loop,))
00132 for waypoint in self._waypoints:
00133 if rospy.is_shutdown():
00134 break
00135 self._limb.move_to_joint_positions(waypoint, timeout=20.0,
00136 threshold=self._accuracy)
00137
00138 rospy.sleep(3.0)
00139
00140
00141 self._limb.set_joint_position_speed(0.3)
00142
00143 def clean_shutdown(self):
00144 print("\nExiting example...")
00145 if not self._init_state:
00146 print("Disabling robot...")
00147 self._rs.disable()
00148 return True
00149
00150
00151 def main():
00152 """RSDK Joint Position Waypoints Example
00153
00154 Records joint positions each time the navigator 'OK/wheel'
00155 button is pressed.
00156 Upon pressing the navigator 'Rethink' button, the recorded joint positions
00157 will begin playing back in a loop.
00158 """
00159 arg_fmt = argparse.RawDescriptionHelpFormatter
00160 parser = argparse.ArgumentParser(formatter_class=arg_fmt,
00161 description=main.__doc__)
00162 required = parser.add_argument_group('required arguments')
00163 required.add_argument(
00164 '-l', '--limb', required=True, choices=['left', 'right'],
00165 help='limb to record/playback waypoints'
00166 )
00167 parser.add_argument(
00168 '-s', '--speed', default=0.3, type=float,
00169 help='joint position motion speed ratio [0.0-1.0] (default:= 0.3)'
00170 )
00171 parser.add_argument(
00172 '-a', '--accuracy',
00173 default=baxter_interface.settings.JOINT_ANGLE_TOLERANCE, type=float,
00174 help='joint position accuracy (rad) at which waypoints must achieve'
00175 )
00176 args = parser.parse_args(rospy.myargv()[1:])
00177
00178 print("Initializing node... ")
00179 rospy.init_node("rsdk_joint_position_waypoints_%s" % (args.limb,))
00180
00181 waypoints = Waypoints(args.limb, args.speed, args.accuracy)
00182
00183
00184 rospy.on_shutdown(waypoints.clean_shutdown)
00185
00186
00187 waypoints.record()
00188 waypoints.playback()
00189
00190 if __name__ == '__main__':
00191 main()