Final Assignment  1.0
Research Track part 1 - Assignment 2 - RobEng A.A. 2020/2021
reach_user_pos_service.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 
23 
24 import rospy
25 from final_assignment.srv import user_target, user_targetRequest, user_targetResponse
26 from move_base_msgs.msg import MoveBaseActionGoal
27 from geometry_msgs.msg import Point
28 from final_assignment.srv import check_position, check_positionRequest, check_positionResponse
29 
30 
31 # --------------------------------- DATA
32 
33 
34 node_name = "reach_user_pos_service"
35 
36 
37 name_move_base = "/move_base/goal"
38 
39 # name of the service 'user_target'
40 name_user_target = "/user_target"
41 
42 
43 topic_move_base = None
44 
45 
46 name_check_position = "/check_position"
47 
48 
49 
50 min_distance_from_the_target = 2
51 
52 
53 cycle_time = rospy.Duration( 0, 500 )
54 
55 
56 
57 # --------------------------------- FUNCTIONS
58 
59 
68 def new_target_to_move_base( target_position ):
69  global node_name
70  global name_move_base, srv_move_base
71 
72  # send it to move_base
73  msg = MoveBaseActionGoal( )
74  msg.goal.target_pose.header.frame_id = 'map'
75  msg.goal.target_pose.pose.position.x = target_position.x
76  msg.goal.target_pose.pose.position.y = target_position.y
77  msg.goal.target_pose.pose.orientation.w = 1.0
78 
79  topic_move_base.publish(msg)
80 
81 
82 
83 # --------------------------------- SERVICES
84 
85 
86 srv_check_position = None
87 
88 
89 
99 def srv_user_target( data ):
100  global node_name, min_distance_from_the_target
101  global name_check_position, srv_check_position
102 
103  response = user_targetResponse( )
104 
105  # send the target to Move_Base
106  rospy.loginfo( " [%s] sending target to MoveBase ... ", node_name )
107  new_target_to_move_base( data.target )
108 
109  # wait until the target is reached
110  rospy.loginfo( " [%s] reaching the position... ", node_name )
111  # get the actual position
112  req = check_positionRequest( )
113  req.check_only = False
114  req.tol = min_distance_from_the_target
115  req.target = data.target
116  while not ( srv_check_position( req ) ).reached:
117  rospy.sleep( cycle_time )
118 
119  rospy.loginfo( " [%s] position reached! ", node_name )
120 
121  response.success = True
122  return response
123 
124 
125 
126 # --------------------------------- WORKING CYCLE
127 
128 
129 def main():
130  rospy.spin()
131 
132 
133 
134 # --------------------------------- NODE
135 
136 
138  global node_name
139 
140  rospy.loginfo( " [%s] is OFFLINE", node_name )
141 
142 
143 
144 if __name__ == "__main__":
145  rospy.init_node( node_name )
146  rospy.on_shutdown( cbk_on_shutdown )
147 
148  # require the server 'user_target'
149  rospy.loginfo( " [%s] advertising service %s ...", node_name, name_user_target )
150  rospy.Service( name_user_target, user_target, srv_user_target )
151  rospy.loginfo( " [%s] service %s ... OK", node_name, name_user_target )
152 
153  # require the topic 'move_base/goal'
154  rospy.loginfo( " [%s] topic (out) %s ...", node_name, name_move_base )
155  topic_move_base = rospy.Publisher( name_move_base, MoveBaseActionGoal, queue_size=1 )
156  rospy.loginfo( " [%s] topic (out) %s ... OK", node_name, name_move_base )
157 
158  # service 'check_position'
159  rospy.loginfo( " [%s] getting service %s ...", node_name, name_check_position )
160  rospy.wait_for_service( name_check_position )
161  srv_check_position = rospy.ServiceProxy( name_check_position, check_position )
162  rospy.loginfo( " [%s] service %s ... OK", node_name, name_check_position )
163 
164  try:
165  rospy.loginfo( " [%s] is ONLINE", node_name )
166  main()
167  except rospy.ROSException:
168  rospy.loginfo( " [%s] Raised an Exception ... ", node_name )
srv_check_position
call-point of the service 'check_position'
def new_target_to_move_base(target_position)
send the goal to MoveBase.
def srv_user_target(data)
implementation of the service '/user_target'
def cbk_on_shutdown()
This is called on the shutdown of the node.