forked from pageauc/motion-track
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_stepper2.py
115 lines (96 loc) · 2.77 KB
/
test_stepper2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# from time import sleep
# import pigpio
# DIR = 17 # Direction GPIO Pin
# STEP = 27 # Step GPIO Pin
# #SWITCH = 16 # GPIO pin of switch
# # Connect to pigpiod daemon
# pi = pigpio.pi()
# # Set up pins as an output
# pi.set_mode(DIR, pigpio.OUTPUT)
# pi.set_mode(STEP, pigpio.OUTPUT)
# # Set up input switch
# #pi.set_mode(SWITCH, pigpio.INPUT)
# #pi.set_pull_up_down(SWITCH, pigpio.PUD_UP)
# # Set duty cycle and frequency
# pi.set_PWM_dutycycle(STEP, 128) # PWM 1/2 On 1/2 Off
# pi.set_PWM_frequency(STEP, 2400) # 500 pulses per second
# try:
# while True:
# pi.write(DIR, 1) # Set direction
# sleep(.1)
# except KeyboardInterrupt:
# print ("\nCtrl-C pressed. Stopping PIGPIO and exiting...")
# finally:
# pi.set_PWM_dutycycle(STEP, 0) # PWM off
# pi.stop()
import pigpio
DIR = 17 # Direction GPIO Pin
STEP = 27 # Step GPIO Pin
# Connect to pigpiod daemon
pi = pigpio.pi()
# Set up pins as an output
pi.set_mode(DIR, pigpio.OUTPUT)
pi.set_mode(STEP, pigpio.OUTPUT)
def generate_ramp(ramp):
"""Generate ramp wave forms.
ramp: List of [Frequency, Steps]
"""
#pi.wave_clear() # clear existing waves
length = len(ramp) # number of ramp levels
wid = [-1] * length
# Generate a wave per ramp level
for i in range(length):
frequency = ramp[i][0]
micros = int(500000 / frequency)
wf = []
wf.append(pigpio.pulse(1 << STEP, 0, micros)) # pulse on
wf.append(pigpio.pulse(0, 1 << STEP, micros)) # pulse off
pi.wave_add_generic(wf)
wid[i] = pi.wave_create()
# Generate a chain of waves
chain = []
for i in range(length):
steps = ramp[i][1]
x = steps & 255
y = steps >> 8
chain += [255, 0, wid[i], 255, 1, x, y]
pi.wave_chain(chain) # Transmit chain
while pi.wave_tx_busy():
time.sleep(0.2)
for id_ in wid:
pi.wave_delete(id_)
RAMP_UP = (
(250, 30),
(320, 40),
(400, 45),
(500, 60),
(800, 90),
(1000, 200),
(1600, 160),
(2000, 200),
)
import time
def move_stepper(total_steps, direction):
pi.write(DIR, direction)
time.sleep(0.1)
ramp = []
steps_left = total_steps / 2
# Build acceleration
for frequency, steps in RAMP_UP:
if steps > steps_left:
ramp.append((frequency, steps_left))
steps_left = 0
break
else:
ramp.append((frequency, steps))
steps_left -= steps
if steps_left:
# Continue for the rest of the total steps at max speed
ramp.append((RAMP_UP[-1][0], steps_left))
# build deceleration
full_ramp = list(ramp)
while ramp:
full_ramp.append(ramp.pop())
generate_ramp(full_ramp)
move_stepper(4000, 1)
move_stepper(4000, 0)