Multithreading Verzweiflung (Bitte um Bsp in meinem Programm)
Verfasst: Donnerstag 9. Januar 2020, 18:28
Ich habe hier ein Python Programm für einen Schritmotor Robter (tut nichts zur Sache) und ich möchte das die befehle R1.vor(), R2.vor(), ... gleichzeitig ausgeführt werden und nicht nach einander. Alle Tutorials und Anleitungen sind an mir gescheitert (ich bin gescheitert und bitte um den Korrekten Programmcode um mir Multithreading zu erschließen.)
#### Es müsste egal sein was das Programm macht ## möchte nur das R1,R2,... mit den argumenten gleichzeitg ausgeführt werden ####
Code: Alles auswählen
import time
from RPi import GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
control_pins_1 = [12, 15, 13, 11] # rv
control_pins_2 = [37, 35, 33, 31] # rh
control_pins_3 = [18, 22, 24, 26] # lv
control_pins_4 = [40, 38, 36, 32] # lh
for pin in control_pins_1:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, 0)
for pin in control_pins_2:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, 0)
for pin in control_pins_3:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, 0)
for pin in control_pins_4:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, 0)
vorstep_seq = [
[1,0,0,0],
[1,1,0,0],
[0,1,0,0],
[0,1,1,0],
[0,0,1,0],
[0,0,1,1],
[0,0,0,1],
[1,0,0,1]
]
rueckstep_seq = [
[0,0,0,1],
[0,0,1,1],
[0,0,1,0],
[0,1,1,0],
[0,1,0,0],
[1,1,0,0],
[1,0,0,0],
[1,0,0,1]
]
class Rad():
def __init__(self, controlpins, seg, speed):
self.speed = speed
self.controlpins = controlpins
self.seg = seg
def vor(self):
for i in range(200):
for vorstep in range(8):
for pin in range(4):
GPIO.output(self.controlpins[pin], self.seg[vorstep][pin])
time.sleep(0.0009)
def rueck(self):
for i in range(200):
for rueckstep in range(8):
for pin in range(4):
GPIO.output(self.controlpins[pin], self.seg[rueckstep][pin])
time.scleep(0.0009)
def getdata(self):
print('Speed : ', self.speed , ', Pins : ', self.controlpins , ', Seq : ', self.seg)
if __name__ == "__main__":
R1 = Rad(control_pins_1, vorstep_seq, 100)
R2 = Rad(control_pins_2, vorstep_seq, 100)
R3 = Rad(control_pins_3, vorstep_seq, 100)
R4 = Rad(control_pins_4, vorstep_seq, 100)
R1.getdata()
R1.vor()
R2.vor()
R3.vor()
R4.cor()
GPIO.cleanup()