0

我是编程初学者,所以我想问你,因为我总是得到这个错误代码:

Traceback (most recent call last):
File "/home/pi/GUI/Adafruit_Python_PCA9685-master/GUI_Roboter.py", line 35, in <module>
pwm = Adafruit_PCA9685.PCA9685(address=0x40)
File "/home/pi/GUI/Adafruit_Python_PCA9685-master/Adafruit_PCA9685/PCA9685.py", line 72, in __init__
import Adafruit_GPIO.I2C as I2C
ImportError: No module named 'Adafruit_GPIO'

我已经像 adafruit Homepage 上的指南一样安装了库。

有人可以帮助我吗?

这是我的代码:

import tkinter
from _struct import pack


import time
import RPi.GPIO as GPIO
import Adafruit_PCA9685

import time
import sys
import signal
import logging

# Initalisierung der Speicher-Adresse
pwm = Adafruit_PCA9685.PCA9685(address=0x40)

pwm.set_pwm_freq(50)



  GeschwindigkeitAuto = 0.008

schnell = 0.5
mittel  = 0.3
langsam = 0.1

GeschwindigkeitAutoFaktor = 0.02
Automargin = 0.003
# Wartezeit zwischen den einzelnen Positionen im Automodus
TaktungAutoModus = 0.008


def setLEDBlau():
    pwm.set_pwm(9, 0, 0)
    pwm.set_pwm(10, 0, 0)
    pwm.set_pwm(11, 0 ,4095)

def setLEDGruen():
    pwm.set_pwm(9, 0, 0)
    pwm.set_pwm(10, 0, 4095)
    pwm.set_pwm(11, 0 ,0)

def setLEDRot():
    pwm.set_pwm(9, 0, 4095)
    pwm.set_pwm(10, 0, 0)
    pwm.set_pwm(11, 0 ,0)

# gruene LED wird aktiviert
setLEDGruen()

# Steuerungsfunktion fuer die Servos
# Nimmt Kanal und Pulswert entgegen und bereitet den Pulswert auf
# Uebergibt die aufbereitete Steuerung dann an die pwm.set_pwm Funktion
def set_servo_pulse(channel, pulse):
    pulse_length = 1000000
    pulse_length /= 50
    pulse_length /= 4096
    pulse *= 1000
    pulse /= pulse_length
    pulse = round(pulse)
    pulse = int(pulse)
    pwm.set_pwm(channel, 0, pulse)

# ------------------------------------------------
# Definiert die Pin-Referenzierung auf GPIO Nummern und nicht auf die Pin-Nummern
GPIO.setmode(GPIO.BCM)

# Zuweisung der Pins fuer die einzelnen Motoren
Motor1 = 0
Motor2 = 1
Motor3 = 2
Motor4 = 3
Motor5 = 4
Motor6 = 5

# Initialisierung der Startpositionen der Motoren
# Damit der Roboterarm kein unerwuenschtes Verhalten beim Start aufweist, wird eine zuvor definierte Startposition beim Start zugewiesen
global Motor1StartPosition
global Motor2StartPosition
global Motor3StartPosition
global Motor4StartPosition
global Motor5StartPosition
global Motor6StartPosition

# Startwerte
Motor1StartPosition = 1.5
Motor2StartPosition = 1.5
Motor3StartPosition = 1.5
Motor4StartPosition = 1.5
Motor5StartPosition = 1.5
Motor6StartPosition = 1.6

# Um einen optimalen Ablauf und ein perfektes Ergebnis zu ermoeglichen, werden fuer die einzelnen Motoren Minimal- und Maximalwerte definiert
# Dies bedeutet, dass sich die Motoren nur im Bereich dieser Werte bewegen koennen und somit in Ihrem Bewegungsradius eingestellt sind.

# Minimalwerte
Motor1Minimum = 0.5
Motor2Minimum = 1.1
Motor3Minimum = 0.8
Motor4Minimum = 0.8
Motor5Minimum = 0.5
Motor6Minimum = 1.6

# Maximalwerte
Motor1Maximum = 2.5
Motor2Maximum = 2.2
Motor3Maximum = 1.7
Motor4Maximum = 2.2
Motor5Maximum = 2.5
Motor6Maximum = 2.2



# Weist die definierten, virtuellen Werte den reellen Werten zu
# Dies erleichtert das weitere Arbeiten und das Rechnen mit den Werten
Motor1Pulse = Motor1StartPosition
Motor2Pulse = Motor2StartPosition
Motor3Pulse = Motor3StartPosition
Motor4Pulse = Motor4StartPosition
Motor5Pulse = Motor5StartPosition
Motor6Pulse = Motor6StartPosition

# ----------------------------------------------------------------------------
#Initialisierungsfunktion (begibt Motoren in die definierten Startpositionen)
def setStartPosition():

    global Motor1Pulse
    global Motor2Pulse
    global Motor3Pulse
    global Motor4Pulse
    global Motor5Pulse
    global Motor6Pulse

    # Bewegt die einzelnen Motoren in die Startpositionen
    # Erster Wert: Motor der angesprochen wird
    # Zweiter Wert: Position die uebergeben wird (hier in den Variablen der StartPositionen referenziert)
    set_servo_pulse(Motor1, Motor1StartPosition)
    set_servo_pulse(Motor2, Motor2StartPosition)
    set_servo_pulse(Motor3, Motor3StartPosition)
    set_servo_pulse(Motor4, Motor4StartPosition)
    set_servo_pulse(Motor5, Motor5StartPosition)
    set_servo_pulse(Motor6, Motor6StartPosition)

    Motor1Pulse = Motor1StartPosition
    Motor2Pulse = Motor2StartPosition
    Motor3Pulse = Motor3StartPosition
    Motor4Pulse = Motor4StartPosition
    Motor5Pulse = Motor5StartPosition
    Motor6Pulse = Motor6StartPosition



# Definierung der Geschwindikeitswerte
# Die Motoren koennen mit verschiedenen Geschwindigkeiten an die gewuenschte Position bewegt werden
# Die einzelnen Geschwindigkeiten (schnell, langsam & mittel) werden hier definiert
# Starke Abweichungen fuehren zu schnelleren Bewegungen
GeschwindigkeitAuto = 0.008

schnell = 0.5
mittel  = 0.3
langsam = 0.1

GeschwindigkeitAutoFaktor = 0.02
Automargin = 0.003
# Wartezeit zwischen den einzelnen Positionen im Automodus
TaktungAutoModus = 0.008

def Stopp():
    Enable = False
    global Motor1Pulse
    global Motor2Pulse
    global Motor3Pulse
    global Motor4Pulse
    global Motor5Pulse
    global Motor6Pulse

    Motor1Pulse = Motor1Pulse
    Motor2Pulse = Motor2Pulse
    Motor3Pulse = Motor3Pulse
    Motor4Pulse = Motor4Pulse
    Motor5Pulse = Motor5Pulse
    Motor6Pulse = Motor6Pulse
    set_servo_pulse(Motor1, Motor1StartPosition)
    set_servo_pulse(Motor2, Motor2StartPosition)
    set_servo_pulse(Motor3, Motor3StartPosition)
    set_servo_pulse(Motor4, Motor4StartPosition)
    set_servo_pulse(Motor5, Motor5StartPosition)
    set_servo_pulse(Motor6, Motor6StartPosition)
    setLEDRot()

def MotorBewegungAuto (Motor,MotorPulse,MotorMinimum,MotorMaximum,MotorAutoWert,GeschwindigkeitAuto):

    if abs(MotorPulse-MotorAutoWert) <= Automargin:
        MotorPulse = MotorAutoWert
        return MotorPulse

    # Meldet, wenn sich die Motoren unter ihr Minimum/Maximum bewegen
    if MotorPulse < MotorMinimum:
        print ('Motor',Motor)
        print ('Minimum Erreicht')
        print (MotorPulse)
        return MotorPulse

    if MotorPulse > MotorMaximum:
        print ('Motor',Motor)
        print ('Maximum Erreicht')
        print  (MotorPulse)
        return MotorPulse

    # Definiert die Bewegungsrichtung indem er den Motor-Sollwert mit dem aktuellen MotorPulse gegenprueft
    if MotorAutoWert < MotorPulse:
        Richtung = True

    elif MotorAutoWert > MotorPulse:
        Richtung = False

    GeschwindigkeitAuto = GeschwindigkeitAuto * GeschwindigkeitAutoFaktor

    # Steuert den Motor
    if not Richtung and not (MotorPulse >= MotorMaximum):
            MotorPulse = MotorPulse + GeschwindigkeitAuto
            set_servo_pulse(Motor, MotorPulse)
            return MotorPulse

    elif Richtung and not (MotorPulse < MotorMinimum):
            MotorPulse = MotorPulse - GeschwindigkeitAuto
            set_servo_pulse(Motor, MotorPulse)
            return MotorPulse



def Manuel1plus():

    global Motor1Pulse
    global Motor2Pulse
    global Motor3Pulse
    global Motor4Pulse
    global Motor5Pulse
    global Motor6Pulse

    Motor1Pulse += 0.05
    set_servo_pulse(Motor1, Motor1Pulse)

    if Motor1Pulse > Motor1Maximum:
        Motor1Pulse = Motor1Maximum
    #lblM1.configure(text = f'M1: {Motor1Pulse:.2f}')



def Manuel1minus():

    global Motor1Pulse
    global Motor2Pulse
    global Motor3Pulse
    global Motor4Pulse
    global Motor5Pulse
    global Motor6Pulse

    Motor1Pulse -= 0.05
    set_servo_pulse(Motor1, Motor1Pulse)

    if Motor1Pulse < Motor1Minimum:
        Motor1Pulse = Motor1Minimum
    #lblM1.configure(text = f'M1: {Motor1Pulse:.2f}')





def Manuel2plus():

    global Motor1Pulse
    global Motor2Pulse
    global Motor3Pulse
    global Motor4Pulse
    global Motor5Pulse
    global Motor6Pulse

    Motor2Pulse += 0.05
    set_servo_pulse(Motor2, Motor2Pulse)
    if Motor2Pulse > Motor2Maximum:
        Motor2Pulse = Motor2Maximum
    #lblM2.configure(text = f'M2: {Motor2Pulse:.2f}')



def Manuel2minus():

    global Motor1Pulse
    global Motor2Pulse
    global Motor3Pulse
    global Motor4Pulse
    global Motor5Pulse
    global Motor6Pulse

    Motor2Pulse -= 0.05
    set_servo_pulse(Motor2, Motor2Pulse)
    if Motor2Pulse < Motor2Minimum:
        Motor2Pulse = Motor2Minimum
    #lblM2.configure(text = f'M2: {Motor2Pulse:.2f}')





def Manuel3plus():

    global Motor1Pulse
    global Motor2Pulse
    global Motor3Pulse
    global Motor4Pulse
    global Motor5Pulse
    global Motor6Pulse

    Motor3Pulse += 0.05
    set_servo_pulse(Motor3, Motor3Pulse)
    if Motor3Pulse > Motor3Maximum:
        Motor3Pulse = Motor3Maximum
    #lblM3.configure(text = f'M3: {Motor3Pulse:.2f}')



def Manuel3minus():

    global Motor1Pulse
    global Motor2Pulse
    global Motor3Pulse
    global Motor4Pulse
    global Motor5Pulse
    global Motor6Pulse

    Motor3Pulse -= 0.05
    set_servo_pulse(Motor3, Motor3Pulse)
    if Motor3Pulse < Motor3Minimum:
        Motor3Pulse = Motor3Minimum
    #lblM3.configure(text = f'M3: {Motor3Pulse:.2f}')


def Manuel4plus():

    global Motor1Pulse
    global Motor2Pulse
    global Motor3Pulse
    global Motor4Pulse
    global Motor5Pulse
    global Motor6Pulse

    Motor4Pulse += 0.05
    set_servo_pulse(Motor4, Motor4Pulse)
    if Motor4Pulse > Motor4Maximum:
        Motor4Pulse = Motor4Maximum
    #lblM4.configure(text = f'M4: {Motor4Pulse:.2f}')



def Manuel4minus():

    global Motor1Pulse
    global Motor2Pulse
    global Motor3Pulse
    global Motor4Pulse
    global Motor5Pulse
    global Motor6Pulse

    Motor4Pulse -= 0.05
    set_servo_pulse(Motor4, Motor4Pulse)
    if Motor4Pulse < Motor4Minimum:
        Motor4Pulse = Motor4Minimum
    #lblM4.configure(text = f'M4: {Motor4Pulse:.2f}')



def Manuel5plus():

    global Motor1Pulse
    global Motor2Pulse
    global Motor3Pulse
    global Motor4Pulse
    global Motor5Pulse
    global Motor6Pulse

    Motor5Pulse += 0.05
    set_servo_pulse(Motor5, Motor5Pulse)
    if Motor5Pulse > Motor5Maximum:
        Motor5Pulse = Motor5Maximum
    #lblM5.configure(text = f'M5: {Motor5Pulse:.2f}')



def Manuel5minus():

    global Motor1Pulse
    global Motor2Pulse
    global Motor3Pulse
    global Motor4Pulse
    global Motor5Pulse
    global Motor6Pulse

    Motor5Pulse -= 0.05
    set_servo_pulse(Motor5, Motor5Pulse)
    if Motor5Pulse < Motor5Minimum:
        Motor5Pulse = Motor5Minimum
    #lblM5.configure(text = f'M5: {Motor5Pulse:.2f}')



def Manuel6plus():

    global Motor1Pulse
    global Motor2Pulse
    global Motor3Pulse
    global Motor4Pulse
    global Motor5Pulse
    global Motor6Pulse

    Motor6Pulse += 0.05
    set_servo_pulse(Motor6, Motor6Pulse)
    if Motor6Pulse > Motor6Maximum:
        Motor6Pulse = Motor6Maximum

    #lblM6.configure(text = f'M6: {Motor6Pulse:.2f}')



def Manuel6minus():

    global Motor1Pulse
    global Motor2Pulse
    global Motor3Pulse
    global Motor4Pulse
    global Motor5Pulse
    global Motor6Pulse

    Motor6Pulse -= 0.05
    set_servo_pulse(Motor6, Motor6Pulse)
    if Motor6Pulse < Motor6Minimum:
        Motor6Pulse = Motor6Minimum

    #lblM6.configure(text = f'M6: {Motor6Pulse:.2f}')

def ende():
    main.destroy()

main = tkinter.Tk()
main.title('6-Achs Robocontrol')
main.geometry('800x600')

Enable = True
#Frame links
fr1=tkinter.Frame (main, width=200, height=100,relief ='sunken', bd=1)
fr1.pack(side='left')

#Frame rechts
fr2=tkinter.Frame (main, width=200, height=100,relief ='sunken', bd=1)
fr2.pack(side='right')

buttonEnde= tkinter.Button(main, text='Ende', command = ende)
buttonEnde.pack()

startpos = tkinter.Button(main,text ='Startposition', command = setStartPosition)
startpos.pack()
stopp = tkinter.Button(main, text ='Stopp', command = Stopp)
stopp.pack()

#Manuell Button:
lblM1 = tkinter.Label(fr1, text = 'M1:'+str(Motor1Pulse))
buttonM1plus = tkinter.Button(fr1, text ='+', command = Manuel1plus)
buttonM1minus = tkinter.Button (fr1, text ='-', command = Manuel1minus)
lblM1.pack()
buttonM1plus.pack()
buttonM1minus.pack()

lblM2 = tkinter.Label(fr1, text = 'M2:'+str(Motor2Pulse))
buttonM2plus = tkinter.Button(fr1, text ='+', command = Manuel2plus)
buttonM2minus = tkinter.Button (fr1, text ='-', command = Manuel2minus)
lblM2.pack()
buttonM2plus.pack()
buttonM2minus.pack()

lblM3 = tkinter.Label(fr1, text = 'M3:'+str(Motor3Pulse))
buttonM3plus = tkinter.Button(fr1, text ='+', command = Manuel3plus)
buttonM3minus = tkinter.Button (fr1, text ='-', command = Manuel3minus)
lblM3.pack()
buttonM3plus.pack()
buttonM3minus.pack()

lblM4 = tkinter.Label(fr2, text = 'M4:'+str(Motor4Pulse))
buttonM4plus = tkinter.Button(fr2, text ='+', command = Manuel4plus)
buttonM4minus = tkinter.Button (fr2, text ='-', command = Manuel4minus)
lblM4.pack()
buttonM4plus.pack()
buttonM4minus.pack()

lblM5 = tkinter.Label(fr2, text = 'M5:'+str(Motor5Pulse))
buttonM5plus = tkinter.Button(fr2, text ='+', command = Manuel5plus)
buttonM5minus = tkinter.Button (fr2, text ='-', command = Manuel5minus)
lblM5.pack()
buttonM5plus.pack()
buttonM5minus.pack()

lblM6 = tkinter.Label(fr2, text = 'M6:'+str(Motor6Pulse))
buttonM6plus = tkinter.Button(fr2, text ='+', command = Manuel6plus)
buttonM6minus = tkinter.Button (fr2, text ='-', command = Manuel6minus)
lblM6.pack()
buttonM6plus.pack()
buttonM6minus.pack()

main.mainloop()
4

0 回答 0