Final Assignment  1.0
Research Track part 1 - Assignment 2 - RobEng A.A. 2020/2021
check_position.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 
21 
22 import rospy
23 from nav_msgs.msg import Odometry
24 from geometry_msgs.msg import PoseWithCovariance, Pose, Point, Quaternion
25 from final_assignment.srv import check_position, check_positionRequest, check_positionResponse
26 
27 
28 
29 # --------------------------------- DATA
30 
31 
32 node_name = "check_position"
33 
34 
35 name_odom = "/odom"
36 
37 
38 name_check_position = "/check_position"
39 
40 
41 actual_pose = Pose()
42 
43 
44 
45 # --------------------------------- FUNCTIONS
46 
47 
58 def srv_check_position( data ):
59  global actual_pose
60 
61  to_return = check_positionResponse( )
62 
63  if data.check_only:
64  to_return.reached = False
65  to_return.success = True
66  to_return.actual_position = actual_pose.position
67  to_return.distance = -1
68  else:
69  target = data.target
70  if ( data.tol > 0 ):
71  to_return.distance = ( ( target.x - actual_pose.position.x )**2 + ( target.y - actual_pose.position.y )**2 )
72  to_return.reached = ( to_return.distance <= data.tol )
73  to_return.success = True
74  to_return.actual_position = actual_pose.position
75  else:
76  to_return.reached = False
77  to_return.success = False
78  to_return.actual_position = actual_pose.position
79  to_return.distance = -1
80 
81  return to_return
82 
83 
84 
85 # --------------------------------- SERVICES
86 
87 
92 def get_odom_from_topic( data ):
93  global actual_pose
94  actual_pose = data.pose.pose
95 
96 
97 
98 # --------------------------------- TOPICS
99 
100 
101 topic_odom = None
102 
103 
104 
105 # --------------------------------- NODE
106 
107 if __name__ == "__main__":
108  rospy.init_node( node_name )
109 
110  # topic '/odom' as Subscriber
111  rospy.loginfo( " [check_position] topic (sub) %s ...", name_odom )
112  topic_odom = rospy.Subscriber( name_odom, Odometry, get_odom_from_topic )
113  rospy.loginfo( " [check_position] topic (sub) %s ... OK", name_odom )
114 
115  # service '/check_position'
116  rospy.loginfo( " [check_position] advertising %s ...", name_check_position )
117  rospy.Service( name_check_position, check_position, srv_check_position )
118  rospy.loginfo( " [check_position] advertising %s ... OK", name_check_position )
119 
120  rospy.spin()
def srv_check_position(data)
Implementation of the service check_position
def get_odom_from_topic(data)
Store the actual pose.