SocketServer用于远程控制PiBot(python)

时间:2013-08-09 15:16:57

标签: python raspberry-pi robot socketserver

这是我的第一个问题! (尽管使用该网站找到我曾经有过的编程问题的大部分答案)

我已经创建了一个PiBotController,我打算在我的笔记本电脑上运行,我想将控件(从箭头键输入)传递给控制我机器人的树莓派。现在硬件方面不是问题我创建了一个响应箭头键输入的程序,我可以通过ssh连接控制pi上的电机。

在线搜索我使用socketserver找到了以下基本服务器和客户端代码,我可以使用它来发送一个简单的字符串。

服务器:

import socketserver

class MyTCPHandler(socketserver.BaseRequestHandler):
"""
The RequestHandler class for our server.

It is instantiated once per connection to the server, and must
override the handle() method to implement communication to the
client.
"""

    def handle(self):
        # self.request is the TCP socket connected to the client
        self.data = self.request.recv(1024).strip()
        print("{} wrote:".format(self.client_address[0]))
        print(self.data)
        # just send back the same data, but upper-cased
        self.request.sendall(self.data.upper())

if __name__ == "__main__":
    HOST, PORT = "localhost", 9999

# Create the server, binding to localhost on port 9999
server = socketserver.TCPServer((HOST, PORT), MyTCPHandler)

# Activate the server; this will keep running until you
# interrupt the program with Ctrl-C
server.serve_forever()

客户端:

import socket
import sys

HOST, PORT = "192.168.2.12", 9999
data = "this here data wont send!! "


# Create a socket (SOCK_STREAM means a TCP socket)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
    # Connect to server and send data
    sock.connect((HOST, PORT))
    sock.sendall(bytes(data + "\n", "utf-8"))

    # Receive data from the server and shut down
    received = str(sock.recv(1024), "utf-8")
finally:
    sock.close()

print("Sent:     {}".format(data))
print("Received: {}".format(received))

现在这个工作正常,并在我的Raspberry Pi(服务器)和我的笔记本电脑(客户端)上打印结果但是我已经多次尝试将它组合成一个功能,该功能随着我的按键'和'释放'一样激活我有我的控制器。

PiBotController

#import the tkinter module for the GUI and input control
try:
    # for Python2
    import Tkinter as tk
    from Tkinter import *
except ImportError:
    # for Python3
    import tkinter as tk
    from tkinter import *

import socket
import sys

#variables
Drive = 'idle'
Steering = 'idle'




#setting up the functions to deal with key presses
def KeyUp(event):
    Drive = 'forward'
    drivelabel.set(Drive)
    labeldown.grid_remove()
    labelup.grid(row=2, column=2)
def KeyDown(event):
    Drive = 'reverse'
    drivelabel.set(Drive)
    labelup.grid_remove()
    labeldown.grid(row=4, column=2)
def KeyLeft(event):
    Steering = 'left'
    steeringlabel.set(Steering)
    labelright.grid_remove()
    labelleft.grid(row=3, column=1)
def KeyRight(event):
    Steering = 'right'
    steeringlabel.set(Steering)
    labelleft.grid_remove()
    labelright.grid(row=3, column=3)
def key(event):
    if event.keysym == 'Escape':
        root.destroy()

#setting up the functions to deal with key releases
def KeyReleaseUp(event):
    Drive = 'idle'
    drivelabel.set(Drive)
    labelup.grid_remove()
def KeyReleaseDown(event):
    Drive = 'idle'
    drivelabel.set(Drive)
    labeldown.grid_remove()
def KeyReleaseLeft(event):
    Steering = 'idle'
    steeringlabel.set(Steering)
    labelleft.grid_remove()
def KeyReleaseRight(event):
    Steering = 'idle'
    steeringlabel.set(Steering)
    labelright.grid_remove()

#connection functions
def AttemptConnection():
    connectionmessagetempvar = connectionmessagevar.get()
    connectionmessagevar.set(connectionmessagetempvar + "\n" + "Attempting to        connect...") 

def transmit(event):
    HOST, PORT = "192.168.2.12", 9999
    data = "this here data wont send!! "


    # Create a socket (SOCK_STREAM means a TCP socket)
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    try:
         # Connect to server and send data
         sock.connect((HOST, PORT))
         sock.sendall(bytes(data + "\n", "utf-8"))

         # Receive data from the server and shut down
         received = str(sock.recv(1024), "utf-8")
    finally:
         sock.close()

    print("Sent:     {}".format(data))
    print("Received: {}".format(received))





#setting up GUI window        
root = tk.Tk()
root.minsize(300,140)
root.maxsize(300,140)
root.title('PiBot Control Centre')
root.grid_columnconfigure(0, minsize=50)
root.grid_columnconfigure(1, minsize=35)
root.grid_columnconfigure(2, minsize=35)
root.grid_columnconfigure(3, minsize=35)
root.grid_rowconfigure(2, minsize=35)
root.grid_rowconfigure(3, minsize=35)
root.grid_rowconfigure(4, minsize=35)
root.configure(background='white')
root.option_add("*background", "white")




#set up the labels to display the current drive states
drivelabel = StringVar()
Label(root, textvariable=drivelabel).grid(row=0, column=1, columnspan=2)
steeringlabel = StringVar()
Label(root, textvariable=steeringlabel).grid(row=1, column=1, columnspan=2)
Label(root, text="Drive: ").grid(row=0, column=0, columnspan=1)
Label(root, text="Steering: ").grid(row=1, column=0, columnspan=1)

#set up the buttons and message for connecting etc..
messages=tk.Frame(root, width=150, height=100)
messages.grid(row=1,column=4, columnspan=2, rowspan=4)


connectionbutton = Button(root, text="Connect", command=AttemptConnection)
connectionbutton.grid(row=0, column=4)
connectionmessagevar = StringVar()
connectionmessage = Message(messages, textvariable=connectionmessagevar, width=100, )
connectionmessage.grid(row=1, column=1, rowspan=1, columnspan=1)
disconnectionbutton = Button(root, text="Disconnect")
disconnectionbutton.grid(row=0, column=5)







#pictures
photodown = PhotoImage(file="down.gif")
labeldown = Label(root, image=photodown)
labeldown.photodown = photodown
#labeldown.grid(row=4, column=1)

photoup = PhotoImage(file="up.gif")
labelup = Label(root, image=photoup)
labelup.photoup = photoup
#labelup.grid(row=2, column=1)

photoleft = PhotoImage(file="left.gif")
labelleft = Label(root, image=photoleft)
labelleft.photoleft = photoleft
#labelleft.grid(row=3, column=0)

photoright = PhotoImage(file="right.gif")
labelright = Label(root, image=photoright)
labelright.photoright = photoright
#labelright.grid(row=3, column=2)

photoupleft = PhotoImage(file="upleft.gif")
labelupleft = Label(root, image=photoupleft)
labelupleft.photoupleft = photoupleft
#labelupleft.grid(row=2, column=0)

photodownleft = PhotoImage(file="downleft.gif")
labeldownleft = Label(root, image=photodownleft)
labeldownleft.photodownleft = photodownleft
#labeldownleft.grid(row=4, column=0)

photoupright = PhotoImage(file="upright.gif")
labelupright = Label(root, image=photoupright)
labelupright.photoupright = photoupright
#labelupright.grid(row=2, column=2)

photodownright = PhotoImage(file="downright.gif")
labeldownright = Label(root, image=photodownright)
labeldownright.photodownright = photodownright
#labeldownright.grid(row=4, column=2)




#bind all key presses and releases to the root window
root.bind_all('<Key-Up>', KeyUp)
root.bind_all('<Key-Down>', KeyDown)
root.bind_all('<Key-Left>', KeyLeft)
root.bind_all('<Key-Right>', KeyRight)

root.bind_all('<KeyRelease-Up>', KeyReleaseUp)
root.bind_all('<KeyRelease-Down>', KeyReleaseDown)
root.bind_all('<KeyRelease-Left>', KeyReleaseLeft)
root.bind_all('<KeyRelease-Right>', KeyReleaseRight)

root.bind_all('<Key>', key)
root.bind_all('<Key>', transmit)




#set the labels to an initial state
steeringlabel.set('idle')
drivelabel.set('idle')
connectionmessagevar.set ('PiBotController Initiated')

#initiate the root window main loop
root.mainloop()

这个程序编译好但后来没有发送任何数据到服务器? (我知道它仍然只是发送一个字符串,但我认为id开始时有一些简单的事情......显然我被卡住了所以它可能是最好的)

任何建议只需发送字符串或发送varibales驱动器并在每次更改时进行操作都会非常感激。

戴夫xx

修改

这是我得到的传输功能,它在每当我按键/释放时发送数据(就像我之前想要的那样)发送数据但是它只发送'idle'变量的初始设置。现在看代码我想我应该把主机和端口信息从每次运行的函数中创建套接字连接?但是我不确定所以这就是我现在所拥有的。

def transmit():
    HOST, PORT = "192.168.2.12", 9999
    DriveSend = drivelabel.get
    SteeringSend = steeringlabel.get


    # Create a socket (SOCK_STREAM means a TCP socket)
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    try:
        # Connect to server and send data
        sock.connect((HOST, PORT))
        sock.sendall(bytes(Drive + "\n", "utf-8"))
        sock.sendall(bytes(Steering + "\n", "utf-8"))

        # Receive data from the server and shut down
        received = str(sock.recv(1024), "utf-8")
    finally:
        sock.close()

    print("Sent:     {}".format(Steering))
    print("Sent:     {}".format(Drive))
    print("Received: {}".format(received))

1 个答案:

答案 0 :(得分:2)

问题是当Tkinter捕获一个键事件时,它首先触发更具体的绑定(例如'Key-Up'),并且该事件永远不会传递给更通用的绑定('Key')。因此,当您按下“向上”键时,会调用KeyUp,但永远不会调用传输。

解决这个问题的一种方法是在所有回调函数(KeyUp,KeyDown等)中调用transmit()。

例如,KeyUp将成为

def KeyUp(event):
    Drive = 'forward'
    drivelabel.set(Drive)
    labeldown.grid_remove()
    labelup.grid(row=2, column=2)
    transmit()

然后你可以摆脱绑定到'Key'的事件。

另一种选择是将“Drive”和“Steering”转换为Tkinter.StringVar对象,然后使用“trace”绑定到写入事件,如下所示:

Drive = tk.StringVar()
Drive.set('idle')
Drive.trace('w', transmit)

请注意,trace会向回调发送一堆参数,因此您必须编辑transmit才能接受它们。

修改

好的,我看到了问题 - 有三个。

1。当你写

Drive = 'forward'

在你的回调函数中,你在模块命名空间中设置变量Drive,你在本地函数命名空间中设置Drive,所以模块-namespace Drive永远不会更改,因此当transmit访问它时,它始终是相同的。

2。在transmit中,您可以写

DriveSend = drivelabel.get
SteeringSend = steeringlabel.get

这是个好主意,但你只是引用这些功能,而不是调用它们。你需要

DriveSend = drivelabel.get()
SteeringSend = steeringlabel.get()

3。在transmit中,您通过套接字发送的值是模块级变量DriveSteering(根据问题#1永远不会更改),而不是DriveSendSteeringSend

<强>解决方案:

我建议完全取消所有DriveSteering变量,并使用StringVars'drivelabel and steeringlabel`。因此,您的回调可以变为:

def KeyUp(event):
#    Drive = 'forward'   (this doesn't actually do any harm, but to avoid confusion I'd just get rid of the Drive variables altogether)
    drivelabel.set('forward')
    labeldown.grid_remove()
    labelup.grid(row=2, column=2)

(等等其余的回调)你的发送功能将变为

def transmit():
    HOST, PORT = "192.168.2.12", 9999
    DriveSend = drivelabel.get()        # Note the ()
    SteeringSend = steeringlabel.get()

    # Create a socket (SOCK_STREAM means a TCP socket)
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    try:
        # Connect to server and send data
        sock.connect((HOST, PORT))
        sock.sendall(bytes(DriveSend + "\n", "utf-8"))    # Note Drive ==> DriveSend
        sock.sendall(bytes(SteeringSend + "\n", "utf-8")) # Note Steering ==> SteeringSend

        # Receive data from the server and shut down
        received = str(sock.recv(1024), "utf-8")
    finally:
        sock.close()

    print("Sent:     {}".format(SteeringSend))   # Note Steering ==> SteeringSend
    print("Sent:     {}".format(DriveSend))      # Note Drive ==> DriveSend
    print("Received: {}".format(received))

修改后的解决方案(来自OP):

由于使用这种方法一段时间后我发现由于按键被按下而每100ms不断更改变量很麻烦并且当我例如向前行驶时导致电机控制的平滑性。为了解决这个问题,我在每个函数中使用了以下编辑

def KeyUp(event):
    if drivelabel.get() == "forward":
        pass
    else:
        drivelabel.set("forward")
        labeldown.grid_remove()
        labelup.grid(row=2, column=2)
        transmit()
        print (drivelabel.get())

代码现在检查varibale是否已经设置为相关方向,如果它什么都不做,否则它会修改它。打印线就在那里,我可以检查它是否正常工作,可以删除或注释掉。