def pacman_controller_py(): rospy.init_node('pacman_controller_py', anonymous=True) pub = rospy.Publisher('pacmanActions1', actions, queue_size=10) rospy.Subscriber('pacmanCoord1', pacmanPos, pacmanPosCallback) rospy.Subscriber('ghostsCoord', ghostsPos, ghostsPosCallback) rospy.Subscriber('cookiesCoord', cookiesPos, cookiesPosCallback) rospy.Subscriber('bonusCoord', bonusPos, bonusPosCallback) rospy.Subscriber('gameState', game, gameStateCallback) rospy.Subscriber('performanceEval', performance, performanceCallback) try: mapRequestClient = rospy.ServiceProxy('pacman_world', mapService) mapa = mapRequestClient("Controller2 py") rospy.loginfo("# Obs: {}".format(mapa.nObs)) rospy.loginfo("minX : {} maxX : {}".format(mapa.minX, mapa.maxX)) rospy.loginfo("minY : {} maxY : {}".format(mapa.minY, mapa.maxY)) rate = rospy.Rate(10) # 10hz msg = actions() while not rospy.is_shutdown(): msg.action = random.choice([0, 1, 2, 3, 4]) #print(msg.action) pub.publish(msg.action) rate.sleep() except rospy.ServiceException as e: print("Error!! Make sure pacman_world node is running ")
def pacman_controller_py_sol(): global posx global posy global ghosts global mode global cookies global pills rospy.init_node('pacman_controller_py_sol', anonymous=True) pub1 = rospy.Publisher('pacmanActions0', actions, queue_size=10) pub2 = rospy.Publisher('pacmanActions1', actions, queue_size=50) rospy.Subscriber('pacmanCoord0', pacmanPos, pacmanPosCallback) rospy.Subscriber('ghostsCoord', ghostsPos, ghostsPosCallback) rospy.Subscriber('cookiesCoord', cookiesPos, cookiesPosCallback) rospy.Subscriber('bonusCoord', bonusPos, bonusPosCallback) rospy.Subscriber('gameState', game, gameStateCallback) rospy.Subscriber('performanceEval', performance, performanceCallback) try: mapRequestClient = rospy.ServiceProxy('pacman_world', mapService) mapa = mapRequestClient("Controlador AI") rospy.loginfo("# Obs: {}".format(mapa.nObs)) rospy.loginfo("minX : {} maxX : {}".format(mapa.minX, mapa.maxX)) rospy.loginfo("minY : {} maxY : {}".format(mapa.minY, mapa.maxY)) msg = actions() rate = rospy.Rate(50) # 10hz threading.Thread(target=pacman2, args=(msg, pub2, rate)).start() threading.Thread(target=pacman1, args=(msg, pub1, rate, mapa)).start() except rospy.ServiceException as e: print("Error!! Make sure pacman_world node is running ")
def pacman_controller_py(): rospy.init_node('pacman_controller_py', anonymous=True) pub = rospy.Publisher('pacmanActions0', actions, queue_size=10) rospy.Subscriber('pacmanCoord0', pacmanPos, pacmanPosCallback) rospy.Subscriber('ghostsCoord', ghostsPos, ghostsPosCallback) rospy.Subscriber('cookiesCoord', cookiesPos, cookiesPosCallback) rospy.Subscriber('bonusCoord', bonusPos, bonusPosCallback) rospy.Subscriber('gameState', game, gameStateCallback) rospy.Subscriber('performanceEval', performance, performanceCallback) try: mapRequestClient = rospy.ServiceProxy('pacman_world', mapService) mapa = mapRequestClient("Controller py") rospy.loginfo("# Obs: {}".format(mapa.nObs)) rospy.loginfo("minX : {} maxX : {}".format(mapa.minX, mapa.maxX)) rospy.loginfo("minY : {} maxY : {}".format(mapa.minY, mapa.maxY)) global min_X, min_Y, max_X, max_Y, obstx, obsty obstx = np.zeros(mapa.nObs,dtype=int) obsty = np.zeros(mapa.nObs,dtype=int) for i in range(mapa.nObs): obstx[i] = mapa.obs[i].x obsty[i] = mapa.obs[i].y min_X = mapa.minX min_Y = mapa.minY max_X = mapa.maxX max_Y = mapa.maxY nm = 0 rate = rospy.Rate(10) # 10hz msg = actions(); actfl = open('/home/fulloa10/shared/action.txt','r+') while not rospy.is_shutdown(): if 'y_P' in globals(): createM(nm) a = actfl.read() if a: if a[-1] == '-': a = random.randint(0, 3) print('**************** a was generated randomly **********************') elif a[-1].isdigit() a = int(a[-1]) actfl.write('-') msg.action = a; pub.publish(msg.action) rate.sleep() nm+=1 except rospy.ServiceException as e: print ("Error!! Make sure pacman_world node is running ")
def pacman_controller_py_sol(): ser = serial.Serial('/dev/ttyACM1', 9600) rospy.init_node('pacman_controller_py_sol', anonymous=True) pub = rospy.Publisher('pacmanActions0', actions, queue_size=10) rospy.Subscriber('pacmanCoord0', pacmanPos, pacmanPosCallback) rospy.Subscriber('ghostsCoord', ghostsPos, ghostsPosCallback) rospy.Subscriber('cookiesCoord', cookiesPos, cookiesPosCallback) rospy.Subscriber('bonusCoord', bonusPos, bonusPosCallback) rospy.Subscriber('gameState', game, gameStateCallback) rospy.Subscriber('performanceEval', performance, performanceCallback) try: mapRequestClient = rospy.ServiceProxy('pacman_world', mapService) mapa = mapRequestClient("Controlador Remoto") rospy.loginfo("# Obs: {}".format(mapa.nObs)) rospy.loginfo("minX : {} maxX : {}".format(mapa.minX, mapa.maxX)) rospy.loginfo("minY : {} maxY : {}".format(mapa.minY, mapa.maxY)) rate = rospy.Rate(20) # 10hz msg = actions() msg.action = 0 joysitckY = 511 joysitckX = 511 while not rospy.is_shutdown(): leyo = False while not leyo: try: posicionJoystick = ser.readline() joysitckY = int((posicionJoystick.split(","))[0]) joysitckX = int((posicionJoystick.split(","))[1]) if (joysitckY == 1023): msg.action = 2 elif (joysitckY == 0): msg.action = 3 if (joysitckX == 1023): msg.action = 0 elif (joysitckX == 0): msg.action = 1 leyo = True except: pass pub.publish(msg.action) rate.sleep() except rospy.ServiceException as e: print("Error!! Make sure pacman_world node is running ")
def pacman_controller_py_sol(): global posx global posy global ghosts global mode global cookies global pills rospy.init_node('pacman_controller_py_sol', anonymous=True) pub = rospy.Publisher('pacmanActions0', actions, queue_size=10) rospy.Subscriber('pacmanCoord0', pacmanPos, pacmanPosCallback) rospy.Subscriber('ghostsCoord', ghostsPos, ghostsPosCallback) rospy.Subscriber('cookiesCoord', cookiesPos, cookiesPosCallback) rospy.Subscriber('bonusCoord', bonusPos, bonusPosCallback) rospy.Subscriber('gameState', game, gameStateCallback) rospy.Subscriber('performanceEval', performance, performanceCallback) try: mapRequestClient = rospy.ServiceProxy('pacman_world', mapService) mapa = mapRequestClient("Controlador AI") rospy.loginfo("# Obs: {}".format(mapa.nObs)) rospy.loginfo("minX : {} maxX : {}".format(mapa.minX, mapa.maxX)) rospy.loginfo("minY : {} maxY : {}".format(mapa.minY, mapa.maxY)) g = game2(mapa.minX, mapa.minY, mapa.maxX, mapa.maxY, mapa.nObs) for o in mapa.obs: x = o.x y = o.y g.add_obs([x, y]) g.init_matrix() g.init_mapn() g.floyd_warshall() rate = rospy.Rate(10) # 10hz msg = actions() while not rospy.is_shutdown(): neighs = g.neighbours([posx - g.xmin, posy - g.ymin]) optimalindex = 0 optimal = -np.inf for i in range(len(neighs)): eval = g.evaluate(neighs[i], cookies, pills, ghosts, mode) rospy.loginfo('eval: {}'.format(eval)) if eval > optimal: optimal = eval optimalindex = i msg.action = eval_mov([posx - g.xmin, posy - g.ymin], neighs[optimalindex]) pub.publish(msg.action) rate.sleep() except rospy.ServiceException as e: print("Error!! Make sure pacman_world node is running ")
def pacman_controller_py(): rospy.init_node('pacman_controller_py', anonymous=True) pub = rospy.Publisher('pacmanActions0', actions, queue_size=10) rospy.Subscriber('pacmanCoord0', pacmanPos, pacmanPosCallback) rospy.Subscriber('ghostsCoord', ghostsPos, ghostsPosCallback) rospy.Subscriber('cookiesCoord', cookiesPos, cookiesPosCallback) rospy.Subscriber('bonusCoord', bonusPos, bonusPosCallback) rospy.Subscriber('gameState', game, gameStateCallback) rospy.Subscriber('performanceEval', performance, performanceCallback) try: mapRequestClient = rospy.ServiceProxy('pacman_world', mapService) mapa = mapRequestClient("Controller py") rospy.loginfo("# Obs: {}".format(mapa.nObs)) rospy.loginfo("minX : {} maxX : {}".format(mapa.minX, mapa.maxX)) rospy.loginfo("minY : {} maxY : {}".format(mapa.minY, mapa.maxY)) rate = rospy.Rate(10) # 10hz msg = actions() msg.action = 4 while not rospy.is_shutdown(): #random controller #msg.action = random.choice([0,1,2,3,4]); #Joystic Controller # move = joystickReader.readPad() # if(move!=4): # msg.action = move #Serial controler move = serialReader.serialKey() print("Action move: {}".format(move)) if (move != 4): msg.action = move print("Action msg.action: {}".format(msg.action)) pub.publish(msg.action) rate.sleep() except rospy.ServiceException as e: print("Error!! Make sure pacman_world node is running ")
def control_pacman(): rospy.init_node('control_pacman', anonymous=True) pub = rospy.Publisher('pacmanActions0', actions, queue_size=10) rospy.Subscriber('performanceEval', performance, callback_performance) try: mapRequestClient = rospy.ServiceProxy('pacman_world', mapService) resp = mapRequestClient("Eyberth") mapa = Mapa(resp) print(mapa.obs[0].x, mapa.obs[0].y) rate = rospy.Rate(10) # 10hz msg = actions() while not rospy.is_shutdown(): msg.action = random.choice([0, 1, 2, 3, 4]) pub.publish(msg.action) rate.sleep() except rospy.ServiceException as e: print("Service call failed")
def talker(): rospy.init_node('talker', anonymous=True) pub = rospy.Publisher('pacmanActions0', actions, queue_size=10) mapRequestclient=rospy.ServiceProxy('pacman_world', mapService) mapa=mapRequestclient("jugador1") rate = rospy.Rate(10) # 10hz msg = actions() #Codigo de pynput def on_press(key): #Funcion al detectar una tecla presionada print('{0} pressed'.format(key)) def on_release(key): #Funcion al soltar una tecla print('{0} release'.format(key)) if key == Key.esc: #Con ESC finaliza este thread return False if key == key.right: msg.action= 2 if key == key.up: msg.action= 0 if key == key.down: msg.action= 1 if key == key.left: msg.action= 3 def ThreadInputs(): #Listener de Pynput en otro thread with Listener(on_press=on_press, on_release=on_release) as listener: listener.join() #Inicia el hilo de pynput threading.Thread(target=ThreadInputs).start() while not rospy.is_shutdown(): #msg.action = 1 pub.publish(msg.action) rate.sleep()
def pacman_maper_py(): ingr = [] global nPacmanM, pacmanPosMx, pacmanPosMy, optimo, IND, unaVez, pac_pos_Y, pac_pos_X, nPacmanM1, pacmanPosMx1, pacmanPosMy1, nCookiesM, cookiesPosM, eatenCookies rospy.init_node('controller_py', anonymous=True) rospy.Subscriber('pacmanCoord0', pacmanPos, pacmanPosCallback) rospy.Subscriber('cookiesCoord', cookiesPos, cookiesPosCallback) #Creacion del publicador pub = rospy.Publisher('pacmanActions0', actions, queue_size=10) try: if (nCookiesM == 0): print("FIN DE JUEGO") sys.exit() rate = rospy.Rate(1 / 0.15) # 1/(150ms) Hz msg = actions() mapRequestClient = rospy.ServiceProxy('pacman_world', mapService) mapa = mapRequestClient("Santi") #matriz A = [[' ' for x in range(1 + mapa.maxX - mapa.minX)] for y in range(1 + mapa.maxY - mapa.minY)] #obstacles for o in range(mapa.nObs): A[-mapa.obs[o].y - mapa.minY][mapa.obs[o].x - mapa.minX] = '#' #plot matrix 1 #for i in range(1 + mapa.maxY - mapa.minY): # print(A[i]) time.sleep( 6 ) #Se da un tiempo de gracia de 6 seg pues la matriz de juego cambia entre while not rospy.is_shutdown(): if (nCookiesM == 0): print("FIN DE JUEGO") sys.exit() print('INICIO while not shut down') A = [[' ' for x in range(1 + mapa.maxX - mapa.minX)] for y in range(1 + mapa.maxY - mapa.minY)] #obstacles for o in range(mapa.nObs): A[-mapa.obs[o].y - mapa.minY][mapa.obs[o].x - mapa.minX] = '#' #pacman A[-pacmanPosMy - mapa.minY][pacmanPosMx - mapa.minX] = 'P' #Pacman if (nPacmanM == 2): A[-pacmanPosMy1 - mapa.minY][pacmanPosMx1 - mapa.minX] = 'p' #MsPacman #cookies for c in range(nCookiesM): A[-cookiesPosM[c].y - mapa.minY][cookiesPosM[c].x - mapa.minX] = 'c' #plot matrix inf #for i in range(1 + mapa.maxY - mapa.minY): # print(A[i]) #Definicion numero de filas y columnas para crear matrices de apoyo filas = len(A) columnas = len(A[1]) #Se crea la matriz de indices de tamano filasxcolumnas print("Una vez(1):", unaVez) if unaVez: IND = [[0 for x in range(columnas)] for y in range(filas)] unaVez = False print("Entro") q = queue.Queue() pac_pos_Y = -pacmanPosMy - mapa.minY pac_pos_X = pacmanPosMx - mapa.minX #line division print('') q.put("") nuevo = "" ingr = [] t0_cookie = time.time() while not finalizo(A, nuevo) and nCookiesM != 0: nuevo = q.get() for j in ["L", "R", "U", "D"]: ingr = nuevo + j #print(ingr) #print("la lista es",list(q.queue)) if mov_valido(A, ingr): K = notVisited(IND, ingr) IND = K[1] if K[0]: q.put(ingr) tf_cookie = time.time() - t0_cookie print("camino optimo es:", optimo) print("El tiempo en encontrarlo fue:", tf_cookie) comandos = [0 for i in range(len(optimo))] cont = 0 for j in optimo: if j == "U": comandos[cont] = 0 if j == "D": comandos[cont] = 1 if j == "R": comandos[cont] = 2 if j == "L": comandos[cont] = 3 cont = cont + 1 #print(comandos) print("Conteo Mov") t0_mov = time.time() for i in comandos: msg.action = i pub.publish(msg.action) time.sleep(0.15) msg.action = 4 pub.publish(msg.action) msg.action = 4 pub.publish(msg.action) tf_map = time.time() - t0_mov print("El tiempo en recorrer el mapa fue:", tf_map) rate.sleep() except rospy.ServiceException as e: print("Error!! Make sure pacman_world node is running ")
# Variable para mantener el nodo corriendo running = True #---------------------------------------------------------------------- # LOGICA DEL NODO #---------------------------------------------------------------------- # Inicializamos el nodo, creamos un publisher y un suscriptor rospy.init_node('grupo15_punto1', anonymous=True) pub = rospy.Publisher('pacmanActions0', actions, queue_size=10) rospy.Subscriber('pacmanCoord0', pacmanPos, posCallback) # Enviamos la solicitud para iniciar el juego mapRequestClient = rospy.ServiceProxy('pacman_world', mapService) mapa = mapRequestClient(nombre) # Enviamos el comando para controlar a Pacman rate = rospy.Rate(60) msg = actions() print('Para terminar la ejecucion presione ESC y luego CTRL + C') # Lanzamos el hilo que revisa que tecla esta siendo presionada. key.start() # Ciclo en el cual trabaja el nodo. while not rospy.is_shutdown(): msg.action = key.getNumber() pub.publish(msg.action) rate.sleep()
def right_hand(): rospy.init_node('right_hand', anonymous=True) pub = rospy.Publisher('pacmanActions0', actions, queue_size=10) rospy.Subscriber('pacmanCoord0', pacmanPos, pacmanPosCallback) rospy.Subscriber('ghostsCoord', ghostsPos, ghostsPosCallback) rospy.Subscriber('cookiesCoord', cookiesPos, cookiesPosCallback) rospy.Subscriber('bonusCoord', bonusPos, bonusPosCallback) rospy.Subscriber('gameState', game, gameStateCallback) rospy.Subscriber('performanceEval', performance, performanceCallback) #rospy.spin() try: mapRequestClient = rospy.ServiceProxy('pacman_world', mapService) mapa = mapRequestClient("Leyendo mapa") obs = mapa.obs rate = rospy.Rate(6.666666666666666666666666666666666666) msg = actions() global actualAction actualAction = 2 tiempo = 0.00000000000000000000000000000000001 while not rospy.is_shutdown(): #pacmanPosCallback0(pacmanPos()) print(pacmanX) print(pacmanY) #actionsCallback(actions) paredIzquierda = False paredDerecha = False paredAbajo = False paredArriba = False for i in obs: obsX = i.x obsY = i.y if ((obsX == pacmanX - 1) and (obsY == pacmanY)): paredIzquierda = True if ((obsX == pacmanX + 1) and (obsY == pacmanY)): paredDerecha = True if ((obsX == pacmanX) and (obsY == pacmanY + 1)): paredArriba = True if ((obsX == pacmanX) and (obsY == pacmanY - 1)): paredAbajo = True if (actualAction == 0): print("Ciclo 0") if not (paredDerecha): nextAction = 2 elif not (paredArriba): nextAction = 0 elif not (paredIzquierda): nextAction = 3 else: nextAction = 1 elif (actualAction == 1): print("Ciclo 1") if not (paredIzquierda): nextAction = 3 elif not (paredAbajo): nextAction = 1 elif not (paredDerecha): nextAction = 2 else: nextAction = 0 elif (actualAction == 2): print("Ciclo 2") if not (paredAbajo): nextAction = 1 elif not (paredDerecha): nextAction = 2 elif not (paredArriba): nextAction = 0 else: nextAction = 3 elif (actualAction == 3): print("Ciclo 3") if not (paredArriba): nextAction = 0 elif not (paredIzquierda): nextAction = 3 elif not (paredAbajo): nextAction = 1 else: nextAction = 2 msg.action = nextAction actualAction = nextAction print(msg.action) pub.publish(msg.action) rate.sleep() except rospy.ServiceException as e: print("Error!! Make sure pacman_world node is running", e)