def __init__(self, host, port): self.camera_pos = (0.0, 0.0) self.wheel_speed = (0.0, 0.0) self.headlights = False self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket.connect((host, port)) self.cs = ControlSocket(self.socket) self.oldcontrol = {}
class ClientInterface(object): def __init__(self, host, port): self.camera_pos = (0.0, 0.0) self.wheel_speed = (0.0, 0.0) self.headlights = False self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket.connect((host, port)) self.cs = ControlSocket(self.socket) self.oldcontrol = {} def __del__(self): self.socket.close() def setCameraPos(self, x, y): self.camera_pos = (x, y) def getCameraPos(self): return self.camera_pos def setWheelSpeed(self, l, r): self.wheel_speed = (l, r) def getWheelSpeed(self): return self.wheel_speed def setHeadlights(self, h): self.headlights = h def getHeadlights(self): return self.headlights def updateControl(self): newcontrol = {"left_motor" : self.wheel_speed[0], "right_motor" : self.wheel_speed[1]} if self.oldcontrol != newcontrol: self.cs.transmitControl(newcontrol) self.oldcontrol = newcontrol
import socket import time import pygame from util.control import ControlSocket pygame.init() screen = pygame.display.set_mode((100, 100)) clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) clientsocket.connect(("192.168.42.1", 1337)) cs = ControlSocket(clientsocket) def clamp(v, l, h): return min((max((v, l)), h)) sendcounter = 0 while (1): pygame.event.pump() pkeys = pygame.key.get_pressed() up, down, left, right, quit = [ (1.0 if pkeys[x] else 0.0) for x in [pygame.K_w, pygame.K_s, pygame.K_a, pygame.K_d, pygame.K_q] ] if quit > 0.5: break
l_motor,r_motor = mc.motors[0:2] # Open listening server socket serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serversocket.bind(("192.168.42.1", 1337)) serversocket.listen(1) sockets.append(serversocket) while(1): clientsocket,address = serversocket.accept() sockets.append(clientsocket) try: cs = ControlSocket(clientsocket) while(1): cdict = cs.receiveControl() print cdict l_motor.retry_write(cdict["left_motor"]) r_motor.retry_write(cdict["right_motor"]) except Exception as e: print e.message continue clientsocket.close()