Go to the documentation of this file.00001
00002 import roslib; roslib.load_manifest('pacman_controller')
00003 import rospy
00004 import commands
00005 import string
00006 import sys
00007 import math
00008 import actionlib
00009 import random
00010 from std_srvs.srv import Empty
00011 from std_msgs.msg import String
00012 from geometry_msgs.msg import PoseWithCovarianceStamped,PoseStamped
00013 from socket import gethostname
00014 from collvoid_msgs.msg import PoseTwistWithCovariance
00015 from move_base_msgs.msg import *
00016
00017 import tf
00018
00019 from pacman_controller import game_engine as GE
00020
00021
00022 class ControllerPacman():
00023
00024 def __init__(self):
00025 self.initialize()
00026
00027
00028 def initialize(self):
00029 self.move_base_client = actionlib.SimpleActionClient('move_base', MoveBaseAction)
00030 self.move_base_client.wait_for_server()
00031 self.init_guess_srv = rospy.ServiceProxy("init_guess_pub", Empty)
00032
00033
00034 self.sub_state = rospy.Subscriber("/state", String, self.cb_state)
00035 self.sub_commands_robot = rospy.Subscriber("/commands_robot", String, self.cb_commands_robot)
00036
00037 self.ghostname = rospy.get_param("~ghostname")
00038 self.home = rospy.get_param("/%s/home"%self.ghostname)
00039
00040
00041 rospy.loginfo("I am: %s home %d",self.ghostname, self.home)
00042 self.gameEngine = GE.GameEngine()
00043 self.state = GE.State.STOPPED
00044 self.current_wp = None
00045
00046 def update(self):
00047 new_wp = None
00048 if (self.state == GE.State.STOPPED or self.state == GE.State.PAUSED):
00049 self.move_base_client.cancel_all_goals()
00050 self.current_wp = None
00051 return
00052 if (self.state in [GE.State.INIT, GE.State.GAME_OVER, GE.State.WON]):
00053 new_wp = self.return_home();
00054 if (self.state in [GE.State.RUNNING, GE.State.FLEEING]):
00055 return
00056 if not new_wp == None and not (new_wp == self.current_wp):
00057 self.current_wp = new_wp
00058 self.move_base_client.cancel_all_goals()
00059 self.send_goal(self.current_wp)
00060
00061 def send_goal(self,wp):
00062 goal = MoveBaseGoal()
00063 goal.target_pose.pose.position.x = self.gameEngine.points[wp]["x"]
00064 goal.target_pose.pose.position.y = self.gameEngine.points[wp]["y"]
00065
00066 goal.target_pose.pose.orientation.x = 0
00067 goal.target_pose.pose.orientation.y = 0
00068 goal.target_pose.pose.orientation.z = 0
00069 goal.target_pose.pose.orientation.w = 1.0
00070
00071
00072 goal.target_pose.header.frame_id = "/map"
00073
00074 self.move_base_client.send_goal(goal)
00075 return
00076
00077 def cb_state(self,msg):
00078 self.state = int(msg.data)
00079
00080
00081 def return_home(self):
00082 if GE.dist(self.gameEngine.points[self.home], self.gameEngine.pacman) < self.gameEngine.pacman['radius'] + GE.THRESHOLD:
00083 self.move_base_client.cancel_all_goals()
00084 my_position = self.gameEngine.pacman
00085 my_wp = self.gameEngine.findClosestMapPoint(my_position)
00086
00087 path = self.gameEngine.find_shortest_path(my_wp,self.gameEngine.points[self.home])
00088 index = 1
00089 while index < len(path)-1 and GE.dist(path[index], self.gameEngine.pacman) < self.gameEngine.pacman['radius'] + 1.5 * GE.THRESHOLD :
00090 index += 1;
00091 if index < len(path)-1 and GE.dist(path[index], self.gameEngine.pacman) < 2 * self.gameEngine.pacman['radius'] + 2.0 * GE.THRESHOLD and self.gameEngine.positionBlocked(path[index], self.gameEngine.pacman):
00092 index +=1;
00093 return path[index]['id']
00094
00095 def cb_commands_robot(self,msg):
00096 if (msg.data == "init Guess"):
00097 self.init_guess_srv()
00098
00099 if __name__ == '__main__':
00100
00101
00102
00103
00104
00105 rospy.init_node('controller_pacman')
00106 controller_pacman = ControllerPacman()
00107 while not rospy.is_shutdown():
00108 controller_pacman.update()
00109 rospy.sleep(0.1)
00110
00111