19 from final_assignment.srv
import switch_service, switch_serviceRequest, switch_serviceResponse
20 from final_assignment.srv
import user_target, user_targetRequest, user_targetResponse
21 from final_assignment.srv
import position_defined, position_definedRequest, position_definedResponse
22 from final_assignment.srv
import bug0_status, bug0_statusRequest, bug0_statusResponse
23 from geometry_msgs.msg
import Point
30 node_name =
"user_console"
33 name_wall_follower_switch =
"/wall_follower_switch"
36 name_reach_random_pos_switch =
"/reach_random_pos_switch"
39 name_user_target =
"/user_target"
42 name_position_defined =
"/position_defined"
45 name_bug0_switch =
"/bug0_switch"
48 name_bug0_status =
"/bug0_status"
56 "change_motion_planning_algorithm", \
61 commands_info = [
"", \
62 "periodically ask a position to the server, then try and reach it.", \
63 "ask to the user a position, then try and reach it. ", \
64 "use the component wall_follower.", \
65 "Stop any ongoing movement", \
66 "select your motion planning algorithm between 'move_base' and 'bug0'", \
68 "Close this program." ]
77 reach_random_pos_active =
False
80 wall_follow_active =
False
101 global node_name, use_bug0
102 global reach_random_pos_active, robot_busy
103 global name_reach_random_pos_switch, srv_reach_random_pos_switch
104 global name_bug0_switch, srv_bug0_switch
106 if reach_random_pos_active:
108 rospy.logwarn(
" [%s] ATTENTION: each_random_pos already active. ", node_name )
113 rospy.logwarn(
" [%s] ATTENTION: the robot is busy! ", node_name )
114 print(
"\tPlease turn off the previous command (use last_pos) before calling this one. " )
124 reach_random_pos_active =
True
126 rospy.loginfo(
" [%s] reach_random_pos STARTED using '%s'", node_name, (
"bug0" if use_bug0
else "move_base" ) )
139 global robot_busy, reach_random_pos_active
140 global name_bug0_switch, srv_bug0_switch
141 global name_reach_random_pos_switch, srv_reach_random_pos_switch
144 rospy.loginfo(
" [%s] sending the stop signal... ", node_name )
152 if res.success
or use_bug0:
157 rospy.loginfo(
" [%s] waiting for the robot to end the path... ", node_name )
158 while res.in_progress:
159 rospy.sleep( rospy.Duration( 0, 500 ) )
163 reach_random_pos_active =
False
165 rospy.loginfo(
" [%s] reach_random_pos STOPPED. ", node_name )
183 global name_position_defined, srv_position_defined
184 global name_user_target, srv_user_target
186 global name_bug0_switch, srv_bug0_switch
187 global name_bug0_status, srv_bug0_status
190 rospy.logwarn(
" [%s] ATTENTION: the robot is busy now! ", node_name )
191 print(
"\tPlease turn off the previous command (use last_pos) before calling this one. " )
195 rospy.loginfo(
" [user console] Give me a target:" )
196 print(
"available: [(-4,-3);(-4,2);(-4,7);(5,-7);(5,-3);(5,1)]" )
198 target.x = float(input(
"\tx : " ))
199 target.y = float(input(
"\ty : " ))
204 rospy.loginfo(
" [user console] selected: ( %f, %f )", target.x, target.y )
206 rospy.logerr(
" [user console] ERROR: not a valid position. " )
210 rospy.loginfo(
" [%s] reaching the position... ", node_name )
216 rospy.loginfo(
" [%s] Arrived. ", node_name )
235 global name_user_target, srv_user_target
236 global name_bug0_switch, srv_bug0_switch
237 global name_bug0_status, srv_bug0_status
240 rospy.set_param(
"des_pos_x", target.x)
241 rospy.set_param(
"des_pos_y", target.y)
248 while not goal_reached:
249 rospy.sleep( rospy.Duration( 0, 500 ) )
269 global name_wall_follower_switch, srv_wall_follower_switch
270 global robot_busy, wall_follow_active
272 if val
and robot_busy:
273 if wall_follow_active:
274 rospy.loginfo(
" [%s] wall_follow already active. ", node_name )
276 rospy.logwarn(
" [%s] ATTENTION: the robot is busy now! ", node_name )
277 print(
"\tPlease turn off the previous command (use last_pos) before calling this one. " )
283 wall_follow_active = val
286 rospy.loginfo(
" [%s] wall follow is %s ", node_name, (
"ON" if val
else "OFF" ) )
303 global reach_random_pos_active, wall_follow_active
306 if reach_random_pos_active:
309 elif wall_follow_active:
314 rospy.logwarn(
" [%s] WARNING: no activity to stop. ", node_name )
329 global node_name, use_bug0, robot_busy
333 rospy.logwarn(
" [%s] ATTENTION: the robot is busy now! ", node_name )
334 print(
"\tPlease turn off the previous command (use last_pos) before calling this one. " )
338 rospy.loginfo(
"[%s] actually you're using this motion planning algorithm: '%s' ", node_name, (
'bug0' if use_bug0
else 'move_base') )
339 print(
"[%s] would you like to change with this? '%s' [Y/n]" % ( node_name, (
'bug0' if not use_bug0
else 'move_base') ) )
340 answer = input(
"[Y/n]" )
342 if answer ==
"Y" or answer ==
"y":
344 use_bug0 =
not use_bug0
349 rospy.loginfo(
"[%s] using algorithm: '%s' ", node_name, (
'bug0' if use_bug0
else 'move_base') )
365 global commands, commands_info
367 print(
"\nAvailable commands:" )
369 for i
in range( 1, len( commands ) ):
370 print(
"\t [no." + str(i) +
"] " + commands[i] +
":" )
371 print(
"\t\t" + commands_info[i] )
378 srv_wall_follower_switch =
None
381 srv_reach_random_pos_switch =
None
384 srv_user_target =
None
387 srv_position_defined =
None
390 srv_bug0_switch =
None
393 srv_bug0_status =
None
412 global commands, last_command_idx
414 print(
"\n\tready!\n" )
416 while not rospy.is_shutdown():
430 if i > 0
and i < len( commands ):
438 cmd_idx = commands.index( cmd )
442 last_command_idx = cmd_idx
446 rospy.loginfo(
" [user console] ERROR: not a valid command!" )
447 print(
"need infos? write ''help" )
463 rospy.loginfo(
" [user console] exit command received" )
475 rospy.loginfo(
" [%s] is OFFLINE", node_name )
479 if __name__ ==
"__main__":
480 rospy.init_node( node_name )
481 rospy.on_shutdown( cbk_on_shutdown )
484 rospy.loginfo(
" [%s] getting service %s ...", node_name, name_wall_follower_switch )
485 rospy.wait_for_service( name_wall_follower_switch )
486 srv_wall_follower_switch = rospy.ServiceProxy( name_wall_follower_switch, SetBool )
487 rospy.loginfo(
" [%s] service %s ... OK", node_name, name_wall_follower_switch )
490 rospy.loginfo(
" [%s] getting service %s ...", node_name, name_reach_random_pos_switch )
491 rospy.wait_for_service( name_reach_random_pos_switch )
492 srv_reach_random_pos_switch = rospy.ServiceProxy( name_reach_random_pos_switch, switch_service )
493 rospy.loginfo(
" [%s] service %s ... OK", node_name, name_reach_random_pos_switch )
496 rospy.loginfo(
" [%s] getting service %s ...", node_name, name_user_target )
497 rospy.wait_for_service( name_user_target )
498 srv_user_target = rospy.ServiceProxy( name_user_target, user_target )
499 rospy.loginfo(
" [%s] service %s ... OK", node_name, name_user_target )
502 rospy.loginfo(
" [%s] getting service %s ...", node_name, name_position_defined )
503 rospy.wait_for_service( name_position_defined )
504 srv_position_defined = rospy.ServiceProxy( name_position_defined, position_defined )
505 rospy.loginfo(
" [%s] service %s ... OK", node_name, name_position_defined )
508 rospy.loginfo(
" [%s] getting service %s ...", node_name, name_bug0_switch )
509 rospy.wait_for_service( name_bug0_switch )
510 srv_bug0_switch = rospy.ServiceProxy( name_bug0_switch, switch_service )
511 rospy.loginfo(
" [%s] service %s ... OK", node_name, name_bug0_switch )
514 rospy.loginfo(
" [%s] getting service %s ...", node_name, name_bug0_status )
515 rospy.wait_for_service( name_bug0_status )
516 srv_bug0_status = rospy.ServiceProxy( name_bug0_status, bug0_status )
517 rospy.loginfo(
" [%s] service %s ... OK", node_name, name_bug0_status )
520 rospy.loginfo(
" [%s] is ONLINE", node_name )
522 except rospy.ROSException:
523 rospy.logwarn(
" [%s] Raised an Exception ... ", node_name )
srv_bug0_status
call-point of the service 'bug0_status'
def print_help()
Implementation of the command help [no.6].
def reach_user_pos_bug0(target)
Send manually a goal to bug0.
srv_bug0_switch
call-point of the service 'bug0_switch'
def last_pos()
Implementation of the command last_pos [no.4].
def cbk_on_shutdown()
Called when the shutdown signal is raised.
def stop_reach_random_pos()
Stop the background process reach_random_pos
srv_wall_follower_switch
call point of the server 'wall_follower_switch'
srv_user_target
entry point of the service 'user_target'
def main()
Ask the next command to the user.
def wall_follow(val=True)
Implementation of the command wall_follow [no.3].
srv_position_defined
call-point of the service 'position_defined'
def change_motion_planning_algorithm()
Implementation of the command change_motion_planning_algorithm [no.5].
def reach_random_pos()
Implementation of the command reach_random_pos [no.1].
def reach_user_pos()
Implementation of the command reach_user_pos [no.2].
srv_reach_random_pos_switch
call-point of the service 'reach_random_pos_switch'