0

我正在尝试编写一个脚本来控制相机的运动。我在树莓派上用 Thonny 编写它。尝试使用 GUI 按钮移动相机时收到以下错误。我已经四处询问并尝试解决问题,但还没有运气。

如果有人可以提供帮助,那就太好了。

我很欣赏这个脚本有其他工作流程,但我将旧脚本与新 GUI 结合起来,所以我很想看看它们是否可以合并。如果不好,我将实施其他工作流程。

[这是来自 Thonny 的错误消息][1]

from tkinter import *
import RPi.GPIO as GPIO
import time
import datetime, sys, tty, termios

#defining root
root = Tk()
root.title('Operation Little Brother V02')


#setting GPIO board
GPIO.setmode(GPIO.BOARD)

GPIO.setup(3,GPIO.OUT)
servo1 = GPIO.PWM(3, 50)
GPIO.setup(5,GPIO.OUT)
servo2 = GPIO.PWM(5, 50)


#setting servos to off 
servo1.start(0)
servo2.start(0)

#user input section
dummy = input('Initiate project. Press Enter')

#setting servos to 90degrees/startup sequence
print('launching start up sequence')
servo1.ChangeDutyCycle(6)
time.sleep(1)
servo1.ChangeDutyCycle(0)
time.sleep(1)
print ('servo 1 test complete')
servo2.ChangeDutyCycle(6)
time.sleep(1)
servo2.ChangeDutyCycle(0)
print ('servo 2 test complete')


# Defining Functions 
#x axis movement concept
def x_movement(x_dir):
    if x_axis <= limit_x_left and x_axis >= limit_x_right:
        x_axis = x_axis + x_dir
        servo1.ChangeDutyCycle(x_axis)
        time.sleep(0.3)
        servo1.ChangeDutyCycle(0)
        time.sleep(0.5)
        print ('moved camera to position: ' +str(x_axis) + '/' +str(limit_x_left))
        return
    else:
        print ('limits reached')

#y axis movement concept
def y_movement(y_dir):
    if y_axis <= limit_y_down and y_axis >= limit_y_up:
        y_axis = y_axis + y_dir
        servo2.ChangeDutyCycle(y_axis)
        time.sleep(0.3)
        servo2.ChangeDutyCycle(0)
        time.sleep(0.5)

        print('moved camera to position: ' +str(y_axis) + '/' +str(limit_y_up))
        return y_axis
    else:
        print ('limits reached')


#centre movement
def centre_movement():

    y_axis = 6
    servo2.ChangeDutyCycle(6)
    servo2.ChangeDutyCycle(0)
    time.sleep(0.5)

    x_axis = 6
    servo1.ChangeDutyCycle(6)
    servo1.ChangeDutyCycle(0)

    time.sleep(0.5)
    print('camera centered to position 6')
    return

#off sequence
def off():
    servo1.ChangeDutyCycle(0)
    servo2.ChangeDutyCycle(0)
    servo1.stop()
    servo2.stop()
    GPIO.cleanup()
    print('Whoopty fucking doo it\'s finished')
    exit()

#defining limits for x and y axis
limit_x_left = 9
limit_x_right = 3
limit_y_up = 3
limit_y_down = 9
x_axis = 6
y_axis = 6


#frame creation
frame1 = LabelFrame(root, text = 'Welcome to Operation Little Brother', padx = 25, pady = 25, border = 5, fg = '#ffffff', bg = '#3e556b')


#button creation 
button1_left = Button(frame1, text = 'Left', padx = 20, pady = 20, fg = 'purple', bg = '#63778a', command = lambda: x_movement(1))
button2_right = Button(frame1, text = 'Right', padx = 20, pady = 20, fg = 'orange', bg = '#63778a', command = lambda: x_movement(-1))
button3_up = Button(frame1, text = 'Up', padx = 20, pady = 20, fg = 'green', command = lambda: y_movement(1))
button4_down = Button(frame1, text = 'Down', padx = 20, pady = 20, fg = 'blue', command = lambda: y_movement(-1))
button5_centre = Button(frame1, text = 'Centre', padx = 20, pady = 20, fg = 'black', command = lambda: centre_movement())
button6_start = Button(frame1, text = 'Start', padx = 10, pady = 10, fg = 'green')
button7_stop = Button(frame1, text = 'Stop', padx = 10, pady = 10, fg = 'red', command = lambda: off())

#positioning the frame
frame1.pack()

#button positioning
button1_left.grid(row = 3, column = 0,)
button2_right.grid(row = 3, column = 2,)
button3_up.grid(row = 2, column = 1)
button4_down.grid(row = 4, column = 1)
button5_centre.grid(row = 3, column = 1)
button6_start.grid(row = 1, column = 0)
button7_stop.grid(row = 1, column = 2)

#Text formatting
button1_left.configure(font='Calibri 15')
button2_right.configure(font='Calibri 15')
button3_up.configure(font='Calibri 15')
button4_down.configure(font='Calibri 15')
button5_centre.configure(font='Calibri 15')
button6_start.configure(font='Calibri 15')
button7_stop.configure(font='Calibri 15')

# Window Loop
root.mainloop()


  [1]: https://i.stack.imgur.com/xIc0O.png
4

0 回答 0