Herunterladen Diese Seite drucken

Joy-it RB-MOTO2 Bedienungsanleitung Seite 5

Raspberry pi schrittmotor-steuerung
4. CODEBEISPIEL PI5
# -*- coding: utf-8 -*-
import
gpiod
import
time
coil_A_1_pin =
24
# pink
coil_A_2_pin =
4
# orange
coil_B_1_pin =
23
# blau
coil_B_2_pin =
25
# gelb
coil2_A_1_pin =
18
# pink
coil2_A_2_pin =
22
# orange
coil2_B_1_pin =
17
# blau
coil2_B_2_pin =
27
# gelb
chip = gpiod.Chip('gpiochip4')
coil_A_1 = chip.get_line(coil_A_1_pin)
coil_A_2 = chip.get_line(coil_A_2_pin)
coil_B_1 = chip.get_line(coil_B_1_pin)
coil_B_2 = chip.get_line(coil_B_2_pin)
coil2_A_1 = chip.get_line(coil2_A_1_pin)
coil2_A_2 = chip.get_line(coil2_A_2_pin)
coil2_B_1 = chip.get_line(coil2_B_1_pin)
coil2_B_2 = chip.get_line(coil2_B_2_pin)
coil_A_1.request(consumer="setstep", type=gpiod.LINE_REQ_DIR_OUT)
coil_A_2.request(consumer="setstep", type=gpiod.LINE_REQ_DIR_OUT)
coil_B_1.request(consumer="setstep", type=gpiod.LINE_REQ_DIR_OUT)
coil_B_2.request(consumer="setstep", type=gpiod.LINE_REQ_DIR_OUT)
coil2_A_1.request(consumer="setstep", type=gpiod.LINE_REQ_DIR_OUT)
coil2_A_2.request(consumer="setstep", type=gpiod.LINE_REQ_DIR_OUT)
coil2_B_1.request(consumer="setstep", type=gpiod.LINE_REQ_DIR_OUT)
coil2_B_2.request(consumer="setstep", type=gpiod.LINE_REQ_DIR_OUT)
StepCount =
8
Seq = list(range(0, StepCount))
Seq[0] = [0,1,0,0]
Seq[1] = [0,1,0,1]
Seq[2] = [0,0,0,1]
Seq[3] = [1,0,0,1]
Seq[4] = [1,0,0,0]
Seq[5] = [1,0,1,0]
Seq[6] = [0,0,1,0]
Seq[7] = [0,1,1,0]
def
setStep(w1, w2, w3, w4):
coil_A_1.set_value(w1)
coil2_A_1.set_value(w1)
coil_A_2.set_value(w2)
coil2_A_2.set_value(w2)
coil_B_1.set_value(w3)
coil2_B_1.set_value(w3)
coil_B_2.set_value(w4)
coil2_B_2.set_value(w4)
def
forward(delay, steps):
for
i
in
range(steps):
for
j
in
range(StepCount):
setStep(Seq[j][0], Seq[j][1], Seq[j][2], Seq[j][3])
time.sleep(delay)
def
backwards(delay, steps):
for
i
in
range(steps):
for
j
in
reversed(range(StepCount)):
setStep(Seq[j][0], Seq[j][1], Seq[j][2], Seq[j][3])
time.sleep(delay)
# 20 langsame Schritte Vorwärts
delay =
20
steps =
100
forward(int(delay) / 1000.0, int(steps))
# 200 schnelle Schritte Rückwärts
delay =
1
steps =
200
backwards(int(delay) / 1000.0, int(steps))
www.joy-it.net
Pascalstr. 8 47506 Neukirchen-Vluyn
loading