خرید بک لینک

I am trying to make a small robot with raspberry pi 3, Arduino uno and python sockets. Here is my code :

On windows, i open the server socket and i send the command like so :

def coect_client_and_get_id(server):
    client, _ = server.accept()
    full_id = ''
    first_message = True

    while 1:
        msg = client.recv(16)
        if first_message:
            message_length = int(msg[:HEADERSIZE])
            msg = msg[HEADERSIZE:]
            first_message = False

        full_id += msg.decode("utf-8")
        if len(full_id) >= message_length:
            full_id = full_id[:message_length]
            break

    print(f'full id decoded : {full_id}')
    return (full_id, client)

def onkeypress(keyevent):
    print('Key pressed')
    key = keyevent.name
    
    # wheels
    if key=="up":
        sendCommand('t')
    elif key=="down":
        sendCommand('g')
    elif key=="right":
        sendCommand('f')
    elif key=="left":
        sendCommand('h')
        
    # control
    elif key=="q":
        sendCommand('q')
    elif key=="c":
        sendCommand('c')
        
    # head
    elif key=="w":
        sendCommand('w')
    elif key=="a":
        sendCommand('a')
    elif key=="s":
        sendCommand('s')
    elif key=="d":
        sendCommand('d')

def onkeyrelease(keyevent):
    print('Key released')
    sendCommand('x')

def sendCommand(message):
    encoded_message=message.encode('utf-8')
    pi_slave_client.sendall(encoded_message)

if __name__ == '__main__':
    control_server = None
    pi_slave_client = None
    
    try:
        # coect control client to windows server 
        control_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        control_server.bind((WIN_HOST_IP, CONTROL_PORT))
        control_server.listen(1)
        
        print('Control server listening ...')
        while pi_slave_client is None:
            print('Coecting pi slave client ...')
            client_id, client = coect_client_and_get_id(control_server)
            if client_id == 'pi_slave_client':
                pi_slave_client = client 
                print(' Pi slave client coected')
                break

        keyboard.on_press(onkeypress)
        keyboard.on_release_key("up", onkeyrelease)
        keyboard.on_release_key("down", onkeyrelease)
        keyboard.on_release_key("left", onkeyrelease)
        keyboard.on_release_key("right", onkeyrelease)
        
        keyboard.on_release_key("w", onkeyrelease)
        keyboard.on_release_key("a", onkeyrelease)
        keyboard.on_release_key("s", onkeyrelease)
        keyboard.on_release_key("d", onkeyrelease)
        
        while 1:
            pass
    finally:
        if control_server:
            control_server.close()
        if pi_slave_client:
            pi_slave_client.close()

And on pi, i have this code :

try:
    control_client_id = 'pi_slave_client'
    print('coecting to windows control server ...')
    control_client=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    control_client.coect((WIN_HOST_IP,CONTROL_PORT))
    print(" Control client coected")
    
    control_client_id = f'{len(control_client_id):<{HEADERSIZE}}'+control_client_id
    control_client.sendall(bytes(control_client_id, "utf-8"))
    
    while True:
        received_char = control_client.recv(32).decode('utf-8')
        print(received_char)
        
        if   received_char=="a":  #head left
            GPIO.output(7,True)
            GPIO.output(11,False)
            GPIO.output(12,False)
            GPIO.output(13,False)
        elif received_char=="d":  #head right
            GPIO.output(7,True)
            GPIO.output(11,False)
            GPIO.output(12,False)
            GPIO.output(13,True)
        elif received_char=="w":  #head up
            GPIO.output(7,True)
            GPIO.output(11,False)
            GPIO.output(12,True)
            GPIO.output(13,False)
        elif received_char=="s":  #head down
            GPIO.output(7,True)
            GPIO.output(11,False)
            GPIO.output(12,True)
            GPIO.output(13,True)
        elif received_char=="c":  #head center
            GPIO.output(7,True)
            GPIO.output(11,True)
        elif received_char=="h":  #turn right     (1,1)
            GPIO.output(7,False)
            GPIO.output(11,True)
            GPIO.output(12,True)
            GPIO.output(13,True)
        elif received_char=="f":  #turn left      (1,0)
            GPIO.output(7,False)
            GPIO.output(11,True)
            GPIO.output(12,True)
            GPIO.output(13,False)
        elif received_char=="t":  #move forwards  (0,1)
            GPIO.output(7,False)
            GPIO.output(11,True)
            GPIO.output(12,False)
            GPIO.output(13,True)
        elif received_char=="g":  #move backwards (0,0)
            GPIO.output(7,False)
            GPIO.output(11,True)
            GPIO.output(12,False)
            GPIO.output(13,False)
        elif received_char=="q":  #terminate everything
            GPIO.output(7,False)
            GPIO.output(11,False)
            break
        elif received_char=="x":  #end command
            GPIO.output(7,False)
            GPIO.output(11,False)
finally:
    GPIO.output(7,False)
    GPIO.output(11,False)
    GPIO.cleanup()
    control_client.close()

But it is not working right. The first few commands end up moving it properly, but then it stopps responding all together.

Do you know what i am doing wrong ?

برچسب: نویسنده: استخدام کار تاريخ: شنبه 7 شهريور 1405 ساعت: 2:40

صفحه بندی