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 import argparse
00031 import math
00032 import random
00033
00034 import rospy
00035
00036 from std_msgs.msg import (
00037 UInt16,
00038 )
00039
00040 import baxter_interface
00041
00042 from baxter_interface import CHECK_VERSION
00043
00044
00045 class Wobbler(object):
00046
00047 def __init__(self):
00048 """
00049 'Wobbles' both arms by commanding joint velocities sinusoidally.
00050 """
00051 self._pub_rate = rospy.Publisher('robot/joint_state_publish_rate',
00052 UInt16)
00053 self._left_arm = baxter_interface.limb.Limb("left")
00054 self._right_arm = baxter_interface.limb.Limb("right")
00055 self._left_joint_names = self._left_arm.joint_names()
00056 self._right_joint_names = self._right_arm.joint_names()
00057
00058
00059 self._rate = 500.0
00060
00061 print("Getting robot state... ")
00062 self._rs = baxter_interface.RobotEnable(CHECK_VERSION)
00063 self._init_state = self._rs.state().enabled
00064 print("Enabling robot... ")
00065 self._rs.enable()
00066
00067
00068 self._pub_rate.publish(self._rate)
00069
00070 def _reset_control_modes(self):
00071 rate = rospy.Rate(self._rate)
00072 for _ in xrange(100):
00073 if rospy.is_shutdown():
00074 return False
00075 self._left_arm.exit_control_mode()
00076 self._right_arm.exit_control_mode()
00077 self._pub_rate.publish(100)
00078 rate.sleep()
00079 return True
00080
00081 def set_neutral(self):
00082 """
00083 Sets both arms back into a neutral pose.
00084 """
00085 print("Moving to neutral pose...")
00086 self._left_arm.move_to_neutral()
00087 self._right_arm.move_to_neutral()
00088
00089 def clean_shutdown(self):
00090 print("\nExiting example...")
00091
00092 self._reset_control_modes()
00093 self.set_neutral()
00094 if not self._init_state:
00095 print("Disabling robot...")
00096 self._rs.disable()
00097 return True
00098
00099 def wobble(self):
00100 self.set_neutral()
00101 """
00102 Performs the wobbling of both arms.
00103 """
00104 rate = rospy.Rate(self._rate)
00105 start = rospy.Time.now()
00106
00107 def make_v_func():
00108 """
00109 returns a randomly parameterized cosine function to control a
00110 specific joint.
00111 """
00112 period_factor = random.uniform(0.3, 0.5)
00113 amplitude_factor = random.uniform(0.1, 0.2)
00114
00115 def v_func(elapsed):
00116 w = period_factor * elapsed.to_sec()
00117 return amplitude_factor * math.cos(w * 2 * math.pi)
00118 return v_func
00119
00120 v_funcs = [make_v_func() for _ in self._right_joint_names]
00121
00122 def make_cmd(joint_names, elapsed):
00123 return dict([(joint, v_funcs[i](elapsed))
00124 for i, joint in enumerate(joint_names)])
00125
00126 print("Wobbling. Press Ctrl-C to stop...")
00127 while not rospy.is_shutdown():
00128 self._pub_rate.publish(self._rate)
00129 elapsed = rospy.Time.now() - start
00130 cmd = make_cmd(self._left_joint_names, elapsed)
00131 self._left_arm.set_joint_velocities(cmd)
00132 cmd = make_cmd(self._right_joint_names, elapsed)
00133 self._right_arm.set_joint_velocities(cmd)
00134 rate.sleep()
00135
00136
00137 def main():
00138 """RSDK Joint Velocity Example: Wobbler
00139
00140 Commands joint velocities of randomly parameterized cosine waves
00141 to each joint. Demonstrates Joint Velocity Control Mode.
00142 """
00143 arg_fmt = argparse.RawDescriptionHelpFormatter
00144 parser = argparse.ArgumentParser(formatter_class=arg_fmt,
00145 description=main.__doc__)
00146 parser.parse_args(rospy.myargv()[1:])
00147
00148 print("Initializing node... ")
00149 rospy.init_node("rsdk_joint_velocity_wobbler")
00150
00151 wobbler = Wobbler()
00152 rospy.on_shutdown(wobbler.clean_shutdown)
00153 wobbler.wobble()
00154
00155 print("Done.")
00156
00157 if __name__ == '__main__':
00158 main()