from tcp import mysocket try: server = mysocket() #New socket instance server.server('192.168.1.192', 4200) #Setup server in IP:PORT (localhost for testing) server.accept() while True: #Accept client connection data = server.receive() #Receive data from client (ALWAYS A STRING) print "From client: ", data #Print data (again because mysocket().receive() print it) finally: server.closesocket() #Close connection
return True else: return False def axe_move(axe): #Return axe's value when its move if event.type == ecodes.EV_ABS: #Check if it is an absolute axis related event if event.code == axe: #If 'axe' moved: return event.value #This is the main part of the program. It connects to a TCP network created by Raspberry. #From the PS3 controller, it detects the axes R2 and L2, then send the values to Raspberry and #and finally it shows in screen which axe has been pressed and the value sent to Raspberry try: client = tcp.mysocket() client.connect("192.168.1.192", 4200) gamepad1 = search_devices('Sony PLAYSTATION(R)3 Controller') for event in gamepad1.read_loop(): axe = axe_move(r2_axe) if axe is not None: client.send("RIGHT") client.send(str(axe)) print "RIGHT: ", axe axe = axe_move(l2_axe) if axe is not None: client.send("LEFT") client.send(str(axe)) print "LEFT: ", axe
import pwm #Function that converts the value recived from laptop to PWM signal value def map(bar, from_min, from_max, to_min, to_max): return (bar - from_min) * (to_max - to_min) / (from_max - from_min) + to_min try: right_pin = pwm.__init__(20, 50) left_pin = pwm.__init__(21, 50) pwm.start(right_pin, 0) pwm.start(left_pin, 0) server = tcp.mysocket() server.server("192.168.1.192", 4200) #RPI IP server.accept() exit = False while not exit: tcp_input = server.receive() if tcp_input == "RIGHT": right_motor = map(int(server.receive()), 0, 255, 0, 100) pwm.changedc(right_pin, right_motor) elif tcp_input == "LEFT": left_motor = map(int(server.receive()), 0, 255, 0, 100) pwm.changedc(left_pin, left_motor) elif tcp_input == "EXIT": exit = True
from tcp import mysocket try: client = mysocket() #New socket instance client.connect() #Conect to IP:PORT (locahost for testing) while True: client.send( raw_input()) #Send a string to the server (IT MUST BE A STRING) finally: client.closesocket() #Close connection