Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.20 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import rospy
  3. import smach
  4. import actionlib
  5. # move the arm and base
  6. import mir_states.common.manipulation_states as gms # move the arm
  7.  
  8. # for send and receive event combined
  9. import mcr_states.common.basic_states as gbs
  10.  
  11. # action lib
  12. from smach_ros import ActionServerWrapper
  13. from mir_yb_action_msgs.msg import AlignWithWorkspaceAction
  14. from mir_yb_action_msgs.msg import AlignWithWorkspaceFeedback
  15. from mir_yb_action_msgs.msg import AlignWithWorkspaceResult
  16. from mir_navigation_msgs.msg import OrientToBaseAction, OrientToBaseActionGoal
  17. import dynamic_reconfigure.server
  18. import mir_align_with_workspace.cfg.WorkspaceDistanceConfig as WorkspaceDistanceConfig
  19.  
  20. #===============================================================================
  21.  
  22. class SetupAlignWithWorkspace(smach.State): # inherit from the State base class
  23. def __init__(self):
  24. smach.State.__init__(self, outcomes=['succeeded','failed','preempted'],
  25. input_keys=['align_with_workspace_goal', 'align_with_workspace_result'],
  26. output_keys=['align_with_workspace_feedback', 'align_with_workspace_result'])
  27.  
  28. self.client = actionlib.SimpleActionClient('/mir_navigation/base_placement/adjust_to_workspace', OrientToBaseAction)
  29. rospy.loginfo('Waiting for action server to start.')
  30. self.client.wait_for_server()
  31. self.received_align_result = False
  32. self.goal = OrientToBaseActionGoal()
  33. self.dynamic_reconfigure = dynamic_reconfigure.server.Server(WorkspaceDistanceConfig, self.reconfigure_callback)
  34.  
  35. def reconfigure_callback(self,config,params):
  36. self.config = config
  37. return config
  38.  
  39. def execute(self, userdata):
  40. if self.preempt_requested():
  41. rospy.logwarn('preemption requested!!!')
  42. # reset preemption flag for next request
  43. self.recall_preempt()
  44. return 'preempted'
  45.  
  46. userdata.align_with_workspace_result = AlignWithWorkspaceResult()
  47. workspace_goal = userdata.align_with_workspace_goal
  48.  
  49. platform ='/mcr_perception/place_pose_selector/'+workspace_goal.destination_location
  50. print platform+" workspace_goal"
  51. height = rospy.get_param(str(platform), None)
  52. if (height==0):
  53. return 'succeeded'
  54.  
  55. self.goal.goal.distance =self.assign_threshold(height)
  56. timeout = 10.0
  57. rospy.loginfo('Action server started, sending goal')
  58. # self.client.send_goal(goal.goal, done_cb = self.align_done_cb)
  59. self.client.send_goal(self.goal.goal)
  60. self.client.wait_for_result(rospy.Duration.from_sec(int(timeout)))
  61. userdata.align_with_workspace_result = self.client.get_result()
  62.  
  63. if(userdata.align_with_workspace_result is None):
  64. self.client.cancel_goal()
  65. return 'failed'
  66.  
  67. feedback = AlignWithWorkspaceFeedback()
  68. feedback.current_state = 'ALIGN_BASE'
  69. feedback.text='[align_with_workspace] Aligning with the workspace: '
  70. userdata.align_with_workspace_feedback = feedback
  71.  
  72. if(userdata.align_with_workspace_result):
  73. return 'succeeded'
  74. else:
  75. return 'failed'
  76.  
  77. # def align_done_cb(self,state,result):
  78. # self.received_align_result = result.succeed
  79. def assign_threshold(self,height):
  80. threshold = 0.7
  81. if height == 20:
  82. threshold = self.config.height_20
  83. elif height== 15:
  84. threshold = self.config.height_15
  85. elif height == 10:
  86. threshold = self.config.height_10
  87. elif height == 5:
  88. threshold = self.config.height_5
  89. elif height == 50:
  90. threshold = self.config.height_50
  91. else:
  92. threshold = 0.07
  93.  
  94. print str(threshold) + " Tthreshold"
  95. return threshold
  96.  
  97. class SetActionLibResult(smach.State):
  98. def __init__(self, result):
  99. smach.State.__init__(self, outcomes=['succeeded'],
  100. input_keys=['align_with_workspace_goal', 'align_with_workspace_result'],
  101. output_keys=['align_with_workspace_feedback', 'align_with_workspace_result'])
  102. self.result = result
  103.  
  104. def execute(self, userdata):
  105. result = AlignWithWorkspaceResult()
  106. result.success = self.result
  107. userdata.align_with_workspace_result = result
  108. return 'succeeded'
  109.  
  110.  
  111. #===============================================================================
  112.  
  113. def main():
  114. rospy.init_node('align_with_workspace_server')
  115. # Construct state machine
  116. sm = smach.StateMachine(
  117. outcomes=['OVERALL_SUCCESS','OVERALL_FAILED','OVERALL_PREEMPTED'],
  118. input_keys = ['align_with_workspace_goal', 'align_with_workspace_result'],
  119. output_keys = ['align_with_workspace_feedback', 'align_with_workspace_result'])
  120. with sm:
  121. smach.StateMachine.add('ALIGN_BASE', SetupAlignWithWorkspace(),
  122. transitions={'succeeded': 'SET_ACTION_LIB_SUCCESS',
  123. 'failed': 'SET_ACTION_LIB_FAILURE',
  124. 'preempted':'OVERALL_PREEMPTED'})
  125.  
  126. smach.StateMachine.add('SET_ACTION_LIB_SUCCESS', SetActionLibResult(True),
  127. #transitions={'succeeded':'RESET_STATIC_TRANSFORM_FOR_PERCEPTION'})
  128. transitions={'succeeded':'OVERALL_SUCCESS'})
  129.  
  130. smach.StateMachine.add('SET_ACTION_LIB_FAILURE', SetActionLibResult(False),
  131. transitions={'succeeded':'OVERALL_FAILED'})
  132.  
  133. # Construct action server wrapper
  134. asw = ActionServerWrapper(
  135. server_name = 'align_with_workspace_server',
  136. action_spec = AlignWithWorkspaceAction,
  137. wrapped_container = sm,
  138. succeeded_outcomes = ['OVERALL_SUCCESS'],
  139. aborted_outcomes = ['OVERALL_FAILED'],
  140. preempted_outcomes = ['OVERALL_PREEMPTED'],
  141. goal_key = 'align_with_workspace_goal',
  142. feedback_key = 'align_with_workspace_feedback',
  143. result_key = 'align_with_workspace_result')
  144. asw.run_server()
  145. rospy.spin()
  146.  
  147. if __name__ == '__main__':
  148. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement