-
Notifications
You must be signed in to change notification settings - Fork 8
/
scanner_support_functions.py
131 lines (100 loc) · 4.23 KB
/
scanner_support_functions.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
import time
import math
import subprocess
import spidev
import Adafruit_BBIO.GPIO as GPIO
def getfilename():
datestamp=str(time.localtime()[0]).zfill(2)+"_"+ str(time.localtime()[1]).zfill(2)+ "_" +str(time.localtime()[2]).zfill(2)
timestamp = " " + str(time.localtime()[3]).zfill(2)+"."+str(time.localtime()[4]).zfill(2) +"."+str(time.localtime()[5]).zfill(2)
path="/home/debian/sdc/"
filename=path+datestamp+timestamp
#filename=datestamp+timestamp+rootname
return filename
#TMC2209 MS2, MS1: 00: 1/8, 01: 1/32, 10: 1/64 11: 1/16
def getzaxisruntime(frequency,motorsteps,driveratio,m1,m2,zaxisindex, Nscancycles):
pi=math.pi #pi constant
#factor =1.021
#factor = 1+(.0000060*zaxisindex) #.0000100 works for 1500 steps/rev
#factor = 1+(.0000050*zaxisindex) #.0000050 works for 4500 steps/rev
factor = 1+(.0000052*zaxisindex) #.0000050 works for 6000 steps/rev
if m2 == 0 and m1 == 0: #1/8 step
multiplier=8
if m2==0 and m1 == 1: #1/2 step
multiplier=32
if m2==1 and m1 == 0: #1/4 step
multiplier=64
if m2==1 and m1 == 1: #1/16 step
multiplier=16
Nstepsperrev=driveratio*motorsteps*multiplier # physical steps per rev 1.8 deg stepper motor drive ratio is 8*200*16 (3200*)
timeperrev=Nstepsperrev/frequency
zaxisanglestep=2*pi/zaxisindex
zaxisruntime=factor*timeperrev/zaxisindex
return zaxisruntime
class anglesensor:
def __init__(self, port0, port1, spimode):
#subprocess call (set gpio pins)
subprocess.check_call("config-pin p9.17 spi_cs",shell=True)
subprocess.check_call("config-pin p9.22 spi_sclk",shell=True)
subprocess.check_call("config-pin p9.21 spi",shell=True)
subprocess.check_call("config-pin p9.18 spi",shell=True)
subprocess.check_call("config-pin p9.24 gpio",shell=True)
subprocess.check_call("clear",shell=True)
self.spi=spidev.SpiDev()
self.spi.close()
self.spi.open(port0,port1)
self.spi.mode=spimode
GPIO.setup("P9_24", GPIO.OUT) #mosi set to 1 for 3 wire mode
def close(self):
self.spi.close()
def getangle(self):
anglestep=0.02197265625 #degrees per count
self.spi.cshigh=False
GPIO.output("P9_24",1) #enable
#time.sleep(.05)
numbertoaverage=5
vread=self.spi.readbytes(2)
summed=int.from_bytes(vread, "big")% 2**14
vreadstart=summed
count=0
count1=0
while count<numbertoaverage:
time.sleep(.001)
self.spi.cshigh=False
vread=self.spi.readbytes(2)
vreadint=int.from_bytes(vread, "big")% 2**14
if vreadstart<6 and vreadstart>=0:
if vreadint<10:
summed=summed+vreadint
count1 +=1
if vreadstart<16384 and vreadstart>16378:
if vreadint>16300:
summed=summed+vreadint
count1 +=1
if vreadstart<=16378 and vreadstart>=6:
summed=summed+vreadint
count1 +=1
count +=1
self.spi.cshigh=True
average=summed/(count1+1)
return (average*anglestep)
""" # this is for old stepper drivers
def getzaxisruntime(frequency,motorsteps,driveratio,m1,m2,m3,zaxisindex, Nscancycles):
pi=math.pi #pi constant
if m1==0 and m2 == 0 and m3==0: #full step
multipier=1
if m1==1 and m2 == 0 and m3==0: #half step
multipier=2
if m1==0 and m2 == 1 and m3==0: #quarter step
multipier=4
if m1==1 and m2 == 1 and m3==0: #eighth step
multiplier=8
if m1==0 and m2 == 0 and m3==1: #16th step
multiplier=16
if m1==1 and m2 == 1 and m3==1: #32nd step (othercombos too)
multiplier=32
Nstepsperrev=driveratio*motorsteps*multiplier # physical steps per rev 1.8 deg stepper motor drive ratio is 8*200*16 (3200*)
timeperrev=Nstepsperrev/frequency
zaxisanglestep=2*pi/zaxisindex
zaxisruntime=timeperrev/zaxisindex
return zaxisruntime
"""