Robot Game  v1.0
Research Track part 1 - Assignment
rg_controller_py.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 
21 
22 import rospy
23 from robot_game import rg_data
24 from geometry_msgs.msg import Twist, Point
25 from nav_msgs.msg import Odometry
26 from robot_game import srv
27 
28 
29 sim_input = None
30 
31 
32 sim_output = None
33 
34 
35 srv_check_target = None
36 
37 
38 srv_get_target = None
39 
40 
41 srv_get_vel = None
42 
43 
44 xt = None
45 
46 
52  request = srv.rg_get_target_srvRequest()
53  request.xmin = -6
54  request.xmax = 6
55  request.ymin = -6
56  request.ymax = 6
57  recv = srv_get_target( request )
58 
59  rospy.loginfo( "Next target: [%f, %f]", recv.xt.x, recv.xt.y )
60 
61  return recv.xt
62 
63 
77 
79  global xt, srv_check_target, srv_get_vel, sim_output
80 
81  # actual position
82  x = pose.pose.pose.position
83 
84  # target reached?
85  req = srv.rg_check_srvRequest( x=x, xt=xt )
86  if( ( srv_check_target( req ) ).reached ):
87  rospy.loginfo( "Target Reached!" )
88  xt = get_new_target()
89 
90  # get the velocity
91  vel_data = srv_get_vel( x, xt )
92 
93  # send the new twist
94  sim_output.publish( vel_data.vel )
95 
96 
100  rospy.loginfo( "(Robot Game) Controller node closed." )
101 
102 
103 if __name__ == "__main__":
104  rospy.init_node( "rg_controller_py" )
105  rospy.on_shutdown( controller_on_shutdown )
106 
107  # input and output
108  rospy.loginfo( "(Controller) subscribing to: %s", rg_data.input_topic )
109  sim_input = rospy.Subscriber( rg_data.input_topic, Odometry, rg_controller_callback )
110  rospy.loginfo( "(Controller) creating topic : %s", rg_data.output_topic )
111  sim_output = rospy.Publisher( rg_data.output_topic, Twist, queue_size=1 )
112 
113  # services
114  rospy.wait_for_service( rg_data.srv_name_check_target )
115  rospy.loginfo( "(Controller) server : %s", rg_data.srv_name_check_target )
116  srv_check_target = rospy.ServiceProxy( rg_data.srv_name_check_target, srv.rg_check_srv )
117 
118  rospy.wait_for_service( rg_data.srv_name_get_target )
119  rospy.loginfo( "(Controller) server : %s", rg_data.srv_name_get_target )
120  srv_get_target = rospy.ServiceProxy( rg_data.srv_name_get_target, srv.rg_get_target_srv )
121 
122  rospy.wait_for_service( rg_data.srv_name_get_velocity )
123  rospy.loginfo( "(Controller) server : %s", rg_data.srv_name_get_velocity )
124  srv_get_vel = rospy.ServiceProxy( rg_data.srv_name_get_velocity, srv.rg_get_vel_srv )
125 
126  # target init
127  rospy.wait_for_service( rg_data.srv_name_get_target )
128  rospy.loginfo( "(Controller) getting initial target" )
129  xt = get_new_target()
130 
131  rospy.loginfo( "(Robot Game) Controller started! Spinning..." )
132  rospy.spin()
srv_get_vel
Service entry point for getting a linear planar twist.
srv_check_target
Service entry point for checking if the target is reached.
srv_get_target
Service entry point for requiring a nwe random target.
def get_new_target()
Ask the service for a new target to reach.
def controller_on_shutdown()
Called when shutdown signal is raised.
def rg_controller_callback(pose)
The main callback of the node.