|
@@ -1,18 +1,123 @@
|
|
|
import bluetooth
|
|
|
-
|
|
|
-nearby_devices = bluetooth.discover_devices(lookup_names=True)
|
|
|
-print("found %d devices" % len(nearby_devices))
|
|
|
+from robot import Robot
|
|
|
+from tag import TagClient
|
|
|
+from random import randint, random
|
|
|
+import signal, sys
|
|
|
+import math
|
|
|
+import time
|
|
|
|
|
|
-for addr, name in nearby_devices:
|
|
|
- print(" %s - %s" % (addr, name))
|
|
|
-
|
|
|
-serverMACAddress = '00:16:53:17:ea:9d'
|
|
|
-port = 3
|
|
|
+serverMACAddress = '00:16:53:18:2E:17'
|
|
|
+port = 1
|
|
|
s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
|
|
|
s.connect((serverMACAddress, port))
|
|
|
-while 1:
|
|
|
- text = raw_input()
|
|
|
- if text == "quit":
|
|
|
- break
|
|
|
- s.send(text)
|
|
|
-sock.close()
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+rid = randint(100, 1000)
|
|
|
+run = False
|
|
|
+board = None
|
|
|
+tagplayer = -1
|
|
|
+
|
|
|
+client = TagClient('localhost', 10318)
|
|
|
+client.info(rid, 8, 8, 4, 4)
|
|
|
+
|
|
|
+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.1)
|
|
|
+
|
|
|
+
|
|
|
+ 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 = {}
|
|
|
+ continue
|
|
|
+
|
|
|
+
|
|
|
+ if client.getTagInfo() is None:
|
|
|
+ continue
|
|
|
+
|
|
|
+
|
|
|
+ newposs = client.getPosition()
|
|
|
+ for info in newposs:
|
|
|
+ posx, posy, course, idd, xsize, ysize, xtag, ytag = info
|
|
|
+ if idd != rid:
|
|
|
+ players[idd] = [posx, posy]
|
|
|
+
|
|
|
+ tag, interval = client.getTagInfo()
|
|
|
+ pos = [players[p] for p in players]
|
|
|
+
|
|
|
+ if time.time() <= interval:
|
|
|
+
|
|
|
+ if tag in players and tag != rid:
|
|
|
+
|
|
|
+
|
|
|
+ x1 = px-players[tag][0]
|
|
|
+ y1 = py-players[tag][1]
|
|
|
+ x2 = math.cos(course)
|
|
|
+ y2 = math.sin(course)
|
|
|
+ dot = x1*x2 + y1*y2
|
|
|
+ det = x1*y2 - y1*x2
|
|
|
+ angle = math.atan2(det, dot)
|
|
|
+ if angle < 0.3 or angle > math.pi-0.3:
|
|
|
+ s.send(bytes([1]))
|
|
|
+ elif angle > math.pi:
|
|
|
+ s.send(bytes([3]))
|
|
|
+ else:
|
|
|
+ s.send(bytes([4]))
|
|
|
+
|
|
|
+
|
|
|
+ else:
|
|
|
+
|
|
|
+ if len(pos) >= 1 and tag == rid:
|
|
|
+ x1 = px-pos[0][0]
|
|
|
+ y1 = py-pos[0][1]
|
|
|
+ x2 = math.cos(course)
|
|
|
+ y2 = math.sin(course)
|
|
|
+ dot = x1*x2 + y1*y2
|
|
|
+ det = x1*y2 - y1*x2
|
|
|
+ angle = math.atan2(det, dot)
|
|
|
+ if angle < 0.3 or angle > math.pi-0.3:
|
|
|
+ s.send(bytes([1]))
|
|
|
+ elif angle > math.pi:
|
|
|
+ s.send(bytes([3]))
|
|
|
+ else:
|
|
|
+ s.send(bytes([4]))
|
|
|
+
|
|
|
+ if tag != rid:
|
|
|
+
|
|
|
+ x1 = px-players[tag][0]
|
|
|
+ y1 = py-players[tag][1]
|
|
|
+ x2 = math.cos(course)
|
|
|
+ y2 = math.sin(course)
|
|
|
+ dot = x1*x2 + y1*y2
|
|
|
+ det = x1*y2 - y1*x2
|
|
|
+ angle = math.atan2(det, dot)
|
|
|
+ if angle < 0.3 or angle > math.pi-0.3:
|
|
|
+ s.send(bytes([1]))
|
|
|
+ elif angle > math.pi:
|
|
|
+ s.send(bytes([3]))
|
|
|
+ else:
|
|
|
+ s.send(bytes([4]))
|
|
|
+
|