123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- import bluetooth
- from robot import Robot
- from tag import TagClient
- from random import randint, random
- import signal, sys
- import math
- import time
- import numpy as np
- serverMACAddress = '00:16:53:18:2E:17'
- port = 1
- s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
- s.connect((serverMACAddress, port))
- rid = 11
- run = False
- board = None
- tagplayer = -1
- client = TagClient('localhost', 10318)
- client.info(rid, 14, 19, 7, 13.5)
- px, py, course = None, None, None
- def signal_handler(signal, frame):
- client.stop()
- sock.close()
- sys.exit(0)
- signal.signal(signal.SIGINT, signal_handler)
- players = {}
- while not client.quit:
- time.sleep(0.4)
-
- if client.getBoard() is None:
- continue
- elif px is None:
- board = client.getBoard()
- px, py = board[0]*random(), board[1]*random()
- course = math.pi*2.0*random()
-
- if client.getStatus() is False:
- players = {}
- s.send(bytes([0x65]))
- continue
-
- if client.getTagInfo() is None:
- continue
-
- newposs = client.getPosition()
- for info in newposs:
- posx, posy, courser, idd, xsize, ysize, xtag, ytag = info
- if idd != rid:
- players[idd] = [posx, posy]
- else:
- px, py, course = posx, posy, courser
- tag, interval = client.getTagInfo()
- pos = [players[p] for p in players]
- x1, y1 = 0, 0
- if time.time() >= interval and len(pos) >= 1 and tag == rid:
- print("Pegando")
- x1 = -py+pos[0][1]
- y1 = -px+pos[0][0]
- elif tag != rid:
- print("Fugindo")
- x1 = py-players[tag][1]
- y1 = px-players[tag][0]
- else:
- print("Esperando")
- s.send(bytes([5]))
- continue
- x2 = math.cos(course)
- y2 = math.sin(course)
- u = np.array([-x1, y1])
- v = np.array([x2, -y2])
- angle = np.math.atan2(np.linalg.det([u,v]),np.dot(u,v))
- print([u,v], angle)
- if abs(angle) < 0.2:
- s.send(bytes([1]))
- elif angle < 0:
- s.send(bytes([4]))
- else:
- s.send(bytes([3]))
- s.send(bytes([0x65]))
- s.send(bytes([0x64]))
|