-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmicrocontroller.py
More file actions
112 lines (89 loc) · 3.18 KB
/
Copy pathmicrocontroller.py
File metadata and controls
112 lines (89 loc) · 3.18 KB
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
from machine import Pin
import utime
import select
import sys
import machine
firmware = 'v1.1'
deviceID = 'RTLC-02'
def main():
# GPIO settings
ENABLE_PIN = 13
PULSE_PIN = 14
DIRECTION_PIN = 15
PHOTOMULTIPLIER_PIN = 16
LIMIT_SWITCH_START_PIN = 17
LIMIT_SWITCH_END_PIN = 18
# Linear actuator settings
ENABLE = Pin(ENABLE_PIN, Pin.OUT)
PULSE = Pin(PULSE_PIN, Pin.OUT)
DIRECTION = Pin(DIRECTION_PIN, Pin.OUT)
# Photomultiplier and limit switches settings
PHOTOMULTIPLIER = Pin(PHOTOMULTIPLIER_PIN, Pin.IN)
LIMIT_SWITCH_START = Pin(LIMIT_SWITCH_START_PIN, Pin.IN, Pin.PULL_UP)
LIMIT_SWITCH_END = Pin(LIMIT_SWITCH_END_PIN, Pin.IN, Pin.PULL_UP)
# Serial reading through USB
poll_obj = select.poll()
poll_obj.register(sys.stdin, 1)
while True:
# Verify data in sys.stdin
if poll_obj.poll(0):
# Reads a sys.stdin line
adquisition = sys.stdin.readline().strip()
batch, operator, distance, adquisition_time=adquisition.split(',')
break
utime.sleep(0.1)
# Variables
STEP_DELAY = 0.001 # 1 ms between stepper pulses
STEPS_PER_MM = 200 # Stepper pulses per mm
MAX_POSITION=int(distance) # Scanning distance
MEASUREMENT_TIME=int(adquisition_time)*60/(MAX_POSITION*2)
# Stepper motor control
def step_motor(steps, direction, delay=STEP_DELAY):
DIRECTION.value(direction)
for _ in range(steps):
# Limit switches check
if direction and LIMIT_SWITCH_END.value():
print(f'end_{firmware}_{deviceID}')
while not LIMIT_SWITCH_START.value():
step_motor(STEPS_PER_MM, False)
ENABLE.value(1)
main()
elif not direction and LIMIT_SWITCH_START.value():
break
PULSE.value(1)
utime.sleep(delay)
PULSE.value(0)
utime.sleep(delay)
# Enables stepper
ENABLE.value(0)
# Photomultiplier reading
def measure_pulses(duration):
start_time = utime.ticks_ms()
pulse_count = 0
while utime.ticks_diff(utime.ticks_ms(), start_time) < duration * 1000:
if PHOTOMULTIPLIER.value():
pulse_count += 1
while PHOTOMULTIPLIER.value(): # Waits until pulse ends
pass
return pulse_count
# Main cycle
try:
# Returns to origin
while not LIMIT_SWITCH_START.value():
step_motor(STEPS_PER_MM, False)
# 0,5mm steps scanning
for position in [i*0.5 for i in range(0,int((MAX_POSITION+0.5)*2))]:
pulses = measure_pulses(MEASUREMENT_TIME)
print(f"{int(position*10)};",f"{pulses}")
if position < MAX_POSITION:
move_step = STEPS_PER_MM / 2
step_motor(move_step, True)
# Returns to origin
print(f'end_{firmware}_{deviceID}')
while not LIMIT_SWITCH_START.value():
step_motor(STEPS_PER_MM, False)
ENABLE.value(1) # Disables stepper
except Exception as e:
print(f"Error: {e}")
while True:
main()