77
88from controller import Controller
99from geometry_msgs .msg import PoseStamped
10+ from dynamixel_workbench_msgs .srv import SetPID
1011
1112# Initialize controller
1213ctrl = Controller ()
@@ -133,12 +134,19 @@ def execute(self, userdata):
133134# define state ChangePID
134135class ChangePID (smach .State ):
135136 def __init__ (self ):
136- smach .State .__init__ (self , outcomes = ['changed' ])
137+ smach .State .__init__ (self , outcomes = ['changed' , 'not_changed' ], input_keys = [ 'joint_nums' , 'PID' ])
137138
138139 def execute (self , userdata ):
139- ## Service call to change the PID values
140- rospy .loginfo ('Executing state ChangePID' )
141- return 'changed'
140+ rospy .wait_for_service ('SetPID' )
141+ try :
142+ set_PID = rospy .ServiceProxy ('SetPID' , SetPID )
143+ P ,I ,D = userdata .PID
144+ for joint_num in userdata .joint_nums :
145+ response = set_PID (joint_num , P , I , D )
146+ return 'changed'
147+ except rospy .ServiceException as e :
148+ rospy .logwarn ("Service call failed:{0}" .format (e ))
149+ return 'not_changed'
142150
143151# define state OpenGripper
144152class OpenGripper (smach .State ):
@@ -158,6 +166,8 @@ def main():
158166 # Create a SMACH state machine
159167 sm = smach .StateMachine (outcomes = ['stop' ])
160168 sm .userdata .tool_id = - 1
169+ sm .userdata .joint_nums = [1 ,2 ,3 ]
170+ sm .userdata .PID = [0 ,0 ,0 ]
161171 sis = smach_ros .IntrospectionServer ('server_name' , sm , '/SM_ROOT' )
162172 sis .start ()
163173 # Open the container
@@ -183,8 +193,9 @@ def main():
183193 transitions = {'foundIK' :'CHANGEPID' })
184194 # smach.StateMachine.add('MOVEGIVE', MoveGive(),
185195 # transitions={'reached':'CHANGEPID'})
196+
186197 smach .StateMachine .add ('CHANGEPID' , ChangePID (),
187- transitions = {'changed' :'OPENGRIPPER' })
198+ transitions = {'changed' :'OPENGRIPPER' , 'notchanged' : 'CHANGEPID' })
188199 smach .StateMachine .add ('OPENGRIPPER' , OpenGripper (),
189200 transitions = {'opened' :'IDLE' })
190201
0 commit comments