def web_socket_transfer_data(request): #function is called when a websocket connection is established sp = sphero.Sphero() print "Connected to Web-Client" while True: try: data = json.loads(msgutil.receive_message( request)) #read websocket and deserialize the JSON object command = data["command"] listDevicesThread = None connectionThread = None if (command == "listDevices"): print 'list spheros' devices = sp.findSpheros() jDev = [] for d in devices: jDev.append({'name': d[1], 'address': d[0]}) print "found: ", jDev if len(jDev) == 0: print "Pair Sphero to computer via Bluetooth Manager first." print "Then, try to select address again." msgutil.send_message(request, json.dumps( jDev)) #serialize list into JSON and send over the socket elif (command == "cancelListDevices"): print "cancel device search" listDevicesThread.terminate() listDevicesThread = None elif (command == "connectToDevice"): name = data["name"] address = data["address"] print "connect to: ", name, address try: sp.connect(address) msgutil.send_message(request, json.dumps({'connected': True})) print "connection successful" except Exception, e: print str(e) msgutil.send_message(request, json.dumps({'connected': False})) print "connection failed" elif (command == "cancelConnection"): print "cancel attempted connection" connectionThread.terminate() connectionThread = None elif (command == "disconnect"): try: sp.disconnect() except: pass
xcord, ycord = np.indices(values.shape) bright = np.zeros(values.shape, values.dtype) prod = np.zeros(values.shape, np.int64) overlaid = np.zeros(small.shape, small.dtype) red = [0, 0, 255] cv2.imshow('hsv', values) cv2.namedWindow("overlay") cv2.setMouseCallback("overlay", recordTarget) cv2.imshow('overlay', overlaid) targetX = bright.shape[0] / 2 targetY = bright.shape[1] / 2 if doSphero: s = sphero.Sphero() connect() last = startTime = datetime.datetime.now() frames = 0 fps = 0 while True: flag, frame = cam.read() cv2.resize(frame, smallSize, small, interpolation=cv2.INTER_LINEAR) #small = cv2.pyrDown(frame) cv2.cvtColor(small, cv2.COLOR_BGR2HSV, hsv) values = hsv[:, :, 2] cv2.imshow('hsv', values)
#%% Imports import sphero #it allows you to conect with your sphero, you have this library in your RBMS folder import numpy as np import astar #%% sph = sphero.Sphero( '68:86:E7:07:5A:69' ) # connection with the sphero (you should look for the adress in your bluetooth setings) #%% sph.set_rgb( 255, 0, 0) # with this function you can change the colours(RGB) of your sphero import time #%% #nmap= #path = astar.find_path(nmap, (), (0,0)) path = [(0, 36), (36, 36), (35, 0), (36, 1)] current = (0, 0) for i in range(0, len(path)): nextp = path[i] vel = np.sqrt((current[1] - nextp[1])**2 + (nextp[0] - current[0])**2) #dev=float(nextp[1]-current[1])/(nextp[0]-current[0]) alfa = (np.pi) / 2 - (np.arctan2(nextp[1] - current[1], (nextp[0] - current[0]))) print(alfa)
from os import sys, path sys.path.append(path.dirname(path.dirname(path.abspath(__file__)))) import sphero import pyglet from pyglet.window import key window = pyglet.window.Window(360, 360) s = sphero.Sphero('/dev/rfcomm0') print("connect sphero") try: s.connect() except: print("err!") s.close() print("""Bluetooth info:name: %s \nbta: %s """ % (s.get_bluetooth_info().name, s.get_bluetooth_info().bta)) @window.event def on_key_press(symbol, modifiers): if symbol == key.SPACE: print("space") s.set_rotation_rate(0x00) elif symbol == key.UP: print("UP") s.roll(0xFF, 0) elif symbol == key.RIGHT: print("RIGHT") s.roll(0xFF, 90) elif symbol == key.DOWN: