-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStepper.py
334 lines (278 loc) · 11.8 KB
/
Stepper.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/usr/bin/env python
"""
A Stepper Motor Driver class for Replicape.
Author: Elias Bakken
email: elias(dot)bakken(at)gmail(dot)com
Website: http://www.thing-printer.com
License: GNU GPL v3: http://www.gnu.org/copyleft/gpl.html
Redeem is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Redeem is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Redeem. If not, see <http://www.gnu.org/licenses/>.
"""
import time
import logging
from Path import Path
from DAC import DAC, PWM_DAC
from ShiftRegister import ShiftRegister
class Stepper(object):
all_steppers = list()
def __init__(self, stepPin, dirPin, faultPin, dac_channel, shiftreg_nr, name, internalStepPin, internalDirPin):
""" Init """
self.dac_channel = dac_channel # Which channel on the dac is connected to this stepper
self.stepPin = stepPin
self.dirPin = dirPin
self.faultPin = faultPin
self.name = name
self.enabled = False
self.in_use = False
self.steps_pr_mm = 1
self.microsteps = 1.0
self.direction = 1
self.internalStepPin = (1 << internalStepPin)
self.internalDirPin = (1 << internalDirPin)
# Set up the Shift register
ShiftRegister.make()
self.shift_reg = ShiftRegister.registers[shiftreg_nr]
def get_state(self):
""" Returns the current state """
return self.state & 0xFF # Return the state of the serial to parallel
def update(self):
""" Commits the changes """
ShiftRegister.commit() # Commit the serial to parallel
# Higher level commands
def set_steps_pr_mm(self, steps_pr_mm):
""" Set the number of steps pr mm. """
self.steps_pr_mm = steps_pr_mm
self.mmPrStep = 1.0 / (steps_pr_mm * self.microsteps)
def get_steps_pr_meter(self):
""" Get the number of steps pr meter """
return self.steps_pr_mm*self.microsteps * 1000.0
def get_step_pin(self):
""" The pin that steps, it looks like GPIO1_31 aso """
return self.internalStepPin
def get_dir_pin(self):
""" Get the dir pin shifted into position """
return self.internalDirPin
def get_direction(self):
return self.direction
@staticmethod
def commit():
pass
"""
The bits in the shift register are as follows (Rev B1):
Bit - name - init val
D0 = - = X (or servo enable)
D1 = CFG5 = 0 (Chopper blank time)
D2 = CFG4 = 0 (Choppper hysteresis)
D3 = CFG0 = 0 (Chopper off time)
D4 = CFG2 = 0 (microstepping)
D5 = CFG2-Z = 0 (microstepping)
D6 = CFG1 = 0 (microstepping)
D7 = CFG1-Z = 0 (microstepping)
"""
class Stepper_00B1(Stepper):
def __init__(self, stepPin, dirPin, faultPin, dac_channel, shiftreg_nr, name, internalStepPin, internalDirPin):
Stepper.__init__(self, stepPin, dirPin, faultPin, dac_channel, shiftreg_nr, name, internalStepPin, internalDirPin)
self.dac = PWM_DAC(dac_channel)
self.state = 0 # The initial state of shift register
def set_microstepping(self, value, force_update=False):
""" Todo: Find an elegant way for this """
EN_CFG1 = (1<<7)
DIS_CFG1 = (0<<7)
EN_CFG2 = (1<<5)
DIS_CFG2 = (0<<5)
CFG2_H = (1<<4)
CFG2_L = (0<<4)
CFG1_H = (1<<6)
CFG1_L = (0<<6)
if value == 0: # GND, GND
state = EN_CFG2 | CFG2_L | EN_CFG1 | CFG1_L
self.microsteps = 1
elif value == 1: # GND, VCC
state = EN_CFG2 | CFG2_L | EN_CFG1 | CFG1_H
self.microsteps = 2
elif value == 2: # GND, open
state = EN_CFG2 | CFG2_L | DIS_CFG1 | CFG1_L
self.microsteps = 2
elif value == 3: # VCC, GND
state = EN_CFG2 | CFG2_H | EN_CFG1 | CFG1_L
self.microsteps = 4
elif value == 4: # VCC, VCC
state = EN_CFG2 | CFG2_H | EN_CFG1 | CFG1_H
self.microsteps = 16
elif value == 5: # VCC, open
state = EN_CFG2 | CFG2_H | DIS_CFG1 | CFG1_L
self.microsteps = 4
elif value == 6: # open, GND
state = DIS_CFG2 | CFG2_L | EN_CFG1 | CFG1_L
self.microsteps = 16
elif value == 7: # open, VCC
state = DIS_CFG2 | CFG2_L | EN_CFG1 | CFG1_H
self.microsteps = 4
elif value == 8: # open, open
state = DIS_CFG2 | CFG2_L | DIS_CFG1 | CFG1_L
self.microsteps = 16
self.shift_reg.set_state(state,0xF0)
self.mmPrStep = 1.0/(self.steps_pr_mm*self.microsteps)
# update the Path class with new values
stepper_num = Path.axis_to_index(self.name)
Path.steps_pr_meter[stepper_num] = self.get_steps_pr_meter()
logging.debug("Updated stepper "+self.name+" to microstepping "+str(value)+" = "+str(self.microsteps))
def set_current_value(self, i_rms):
""" Current chopping limit (This is the value you can change) """
self.current_value = i_rms
r_sense = 0.1020 # External resistors + internal
sqrt_2 = 1.41421356237
v_iref = 2.5*(i_rms/1.92)
if(v_iref > 2.5):
logging.warning("Current ref for stepper "+self.name+" above limit (2.5 V). Setting to 2.5 V")
v_iref = 2.5
logging.debug("Setting votage to "+str(v_iref)+" for "+self.name)
self.dac.set_voltage(v_iref)
def set_disabled(self, force_update=False):
if hasattr(Stepper, "printer"):
Stepper.printer.enable.set_disabled()
def set_enabled(self, force_update=False):
if hasattr(Stepper, "printer"):
Stepper.printer.enable.set_enabled()
def set_decay(self, value):
pass
class Stepper_00B2(Stepper_00B1):
def __init__(self, stepPin, dirPin, faultPin, dac_channel, shiftreg_nr, name, internalStepPin, internalDirPin):
Stepper_00B1.__init__(self, stepPin, dirPin, faultPin, dac_channel, shiftreg_nr, name, internalStepPin, internalDirPin)
self.dac = PWM_DAC(dac_channel)
self.state = 0 # The initial state of shift register
def set_disabled(self, force_update=False):
if not self.enabled:
return
logging.debug("Disabling stepper "+self.name)
# X, Y, Z steppers are on the first shift reg. Extruders have their own.
if self.name in ["X", "Y", "Z"]:
ShiftRegister.registers[0].add_state(0x1)
elif self.name == "E":
ShiftRegister.registers[3].add_state(0x1)
elif self.name == "H":
ShiftRegister.registers[4].add_state(0x1)
self.enabled = False
def set_enabled(self, force_update=False):
if self.enabled:
return
logging.debug("Enabling stepper "+self.name)
# X, Y, Z steppers are on the first shift reg. Extruders have their own.
if self.name in ["X", "Y", "Z"]:
ShiftRegister.registers[0].remove_state(0x1) # First bit low.
elif self.name == "E":
ShiftRegister.registers[3].remove_state(0x1)
elif self.name == "H":
ShiftRegister.registers[4].remove_state(0x1)
self.enabled = True
"""
The bits in the shift register are as follows (Rev A4) :
Bit - name - init val
D0 = - = X
D1 = MODE2 = 0
D2 = MODE1 = 0
D3 = MODE0 = 0
D4 = nENABLE = 0 - Enabled
D5 = DECAY = 0 - Slow decay
D6 = nSLEEP = 1 - Not sleeping
D7 = nRESET = 1 - Not in reset mode
"""
class Stepper_00A4(Stepper):
revision = "A4"
SLEEP = 6
ENABLED = 4
RESET = 7
DECAY = 5
def __init__(self, stepPin, dirPin, faultPin, dac_channel, shiftreg_nr, name, internalStepPin, internalDirPin):
Stepper.__init__(self, stepPin, dirPin, faultPin, dac_channel, shiftreg_nr, name, internalStepPin, internalDirPin)
self.dac = DAC(dac_channel)
self.dacvalue = 0x00 # The voltage value on the VREF
self.state = (1<<Stepper_00A4.SLEEP)|(1<<Stepper_00A4.RESET)| (1<<Stepper_00A4.ENABLED) # The initial state of the inputs
self.update()
def set_enabled(self, force_update=False):
""" Sets the Stepper enabled """
if not self.enabled:
self.state &= ~(1 << Stepper_00A4.ENABLED)
self.enabled = True
self.update()
def set_disabled(self, force_update=False):
""" Sets the Stepper disabled """
if self.enabled:
self.state |= (1 << Stepper_00A4.ENABLED)
self.enabled = False
self.update()
def enable_sleepmode(self, force_update=False):
"""Logic high to enable device, logic low to enter
low-power sleep mode. Internal pulldown."""
self.state &= ~(1 << Stepper_00A4.SLEEP)
self.update()
def disable_sleepmode(self, force_update=False):
""" Disables sleepmode (awake) """
self.state |= (1<<Stepper_00A4.SLEEP)
self.update()
def reset(self, force_update=False):
"""nReset - Active-low reset input initializes the indexer
logic and disables the H-bridge outputs.
Internal pulldown."""
self.state &= ~(1 << Stepper_00A4.RESET)
self.update()
time.sleep(0.001)
self.state |= (1 << Stepper_00A4.RESET)
self.update()
def set_microstepping(self, value, force_update=False):
""" Microstepping (default = 0) 0 to 5 """
if not value in [0, 1, 2, 3, 4, 5]: # Full, half, 1/4, 1/8, 1/16, 1/32.
logging.warning("Tried to set illegal microstepping value: {0} for stepper {1}".format(value, self.name))
return
self.microstepping = value
self.microsteps = 2**value # 2^val
# Keep bit 0, 4, 5, 6 intact but replace bit 1, 2, 3
self.state = int("0b"+bin(self.state)[2:].rjust(8, '0')[:4]+bin(value)[2:].rjust(3, '0')[::-1]+"0", 2)
#self.state = int("0b"+bin(self.state)[2:].rjust(8, '0')[:4]+bin(value)[2:].rjust(3, '0')+bin(self.state)[-1:], 2)
self.mmPrStep = 1.0/(self.steps_pr_mm*self.microsteps)
# update the Path class with new values
stepper_num = Path.axis_to_index(self.name)
Path.steps_pr_meter[stepper_num] = self.get_steps_pr_meter()
logging.debug("Updated stepper "+self.name+" to microstepping "+str(value)+" = "+str(self.microsteps))
self.update()
def set_current_value(self, iChop):
""" Current chopping limit (This is the value you can change) """
self.current_value = iChop
rSense = 0.1 # Resistance for the
v_out = iChop * 5.0 * rSense # Calculated voltage out from the DAC
self.dac.set_voltage(v_out)
def set_decay(self, value, force_update=False):
""" Decay mode, look in the data sheet """
self.decay = value
self.state &= ~(1 << Stepper_00A4.DECAY) # bit 5
self.state |= (value << Stepper_00A4.DECAY)
self.update()
def update(self):
# Invert shizzle
self.shift_reg.set_state(self.state)
#logging.debug("Updated stepper {} to enabled, state: {}".format(self.name, bin(self.state)))
"""
The bits in the shift register are as follows (Rev A3):
D0 = DECAY = X
D1 = MODE0 = X
D2 = MODE1 = X
D3 = MODE2 = X
D4 = nRESET = 1
D5 = nSLEEP = 1
D6 = nENABLE = 0
D7 = - = X
"""
class Stepper_00A3(Stepper_00A4):
Stepper.revision = "A3"
Stepper.ENABLED = 6
Stepper.SLEEP = 5
Stepper.RESET = 4
Stepper.DECAY = 0