1

这是我的第一个问题!(尽管使用该网站找到了我曾经遇到的大多数编程问题的答案)

我创建了一个 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(服务器)和我的笔记本电脑(客户端)上打印结果但是我多次尝试将它组合成一个功能,该功能与我的按键'和释放'一起激活,就像我在我的控制器。

PiBot控制器

#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 从一些简单的东西开始......而且很明显我被卡住了,所以它可能是最好的)

任何建议使其在每次更改时仅发送字符串或发送变量驱动和转向都将不胜感激。

戴夫XX

编辑

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

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))
4

1 回答 1

2

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

解决此问题的一种方法是在所有回调函数(KeyUp、KeyDown 等)中调用 transfer()。

例如,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在本地函数命名空间中设置,所以模块命名空间Drive永远不会改变,所以当transmit访问它时,它总是一样的。

2. 在transmit中,你写

DriveSend = drivelabel.get
SteeringSend = steeringlabel.get

这是个好主意,但您只是在引用函数,而不是调用它们。你需要

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

3. 在transmit中,您通过套接字发送的值是模块级变量Driveand Steering(根据问题 #1 永远不会改变),而不是DriveSendand SteeringSend

解决方案:

我建议完全取消所有的DriveSteering变量,而只使用StringVars“驱动标签and转向标签”。因此,您的回调可以变成:

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):

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

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())

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

于 2013-08-09T15:25:51.467 回答