-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_collection.py
More file actions
221 lines (149 loc) · 11 KB
/
data_collection.py
File metadata and controls
221 lines (149 loc) · 11 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
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
from pylablib.devices.Thorlabs import KinesisMotor
from xtralien import Device
import numpy as np
import time
import processing, motor_rotation
def get_background(note: str, background_data:list, experiment_parameters:dict, apparatus_settings:dict):
"""Takes background_data and modifies it directly (modifies the global variable)."""
# Connect to photodetector
with Device(apparatus_settings['pd_led_com']) as device:
print('Connected (photometer)')
# Setup channels for LED & photodetector
device[apparatus_settings["led_channel"]].set.enabled(False, response=0)
device[apparatus_settings["pd_channel"]].set.osr(experiment_parameters["osr"], response=0)
device[apparatus_settings["pd_channel"]].set.enabled(True, response=0)
# Make background mesurement
background_value = np.mean(device[apparatus_settings["pd_channel"]].measure(experiment_parameters["background_sample_count"]))
background_data.append([note, background_value])
print('Background measurement made')
print('Disconnected (photometer)')
print()
def get_data(experiment_data:list, measurement_angles:list, experiment_parameters:dict, apparatus_settings:dict):
"""Takes experiment_data and modifies it directly (modifies the global variable).
### MANUAL ADJUSTMENT ### - Assume is at calibrated position (is at greatest clockwise extream where the led can be seen from the photo detector)"""
## Record measurement type
experiment_parameters["stop_for_measurement"] = True
## Tell user what angles are to be measured
print("Mesurement angles: ", measurement_angles)
## Connect to photodetector & led
with Device(apparatus_settings["pd_led_com"]) as device:
device.connect()
if not device.is_connected():
raise Exception("Faild to connect (photometer & led)")
else:
print('Connected (photometer & led)')
## Setup channels for LED & photodetector
device[apparatus_settings["led_channel"]].set.range(2, response=0)
device[apparatus_settings["led_channel"]].set.osr(experiment_parameters["osr"], response=0)
device[apparatus_settings["pd_channel"]].set.enabled(True, response=0)
device[apparatus_settings["pd_channel"]].set.osr(experiment_parameters["osr"], response=0)
## Connect to motor 1
with KinesisMotor(apparatus_settings["motor1_id"]) as motor1:
print('Connected (motor1)')
if any(isinstance(sub, list) for sub in measurement_angles):
### 2D case
## Connect to motor 2
with KinesisMotor(apparatus_settings["motor2_id"]) as motor2:
print('Connected (motor2)')
for angle1, angle2 in measurement_angles:
## Move
motor_rotation.go_to(apparatus_settings, angle1, angle2, motor1=motor1, motor2=motor2, max_velocity=True)
## Turn on LED & get LED voltage and current (with osr=8, this takes 0.15s per sample)
led_voltage, led_current = device[apparatus_settings["led_channel"]].oneshot(experiment_parameters["target_led_voltage"])[0]
## Get PD_Voltage & motor position
photodetector_votage = np.mean(device[apparatus_settings["pd_channel"]].measure(experiment_parameters["measurement_sample_count"]))
position_1 = processing.custom_modulo(processing.steps_to_degrees(motor1.get_position(),apparatus_settings))
position_2 = processing.steps_to_degrees(motor2.get_position(),apparatus_settings) % 360
# Record data
experiment_data.append([position_1, position_2, photodetector_votage, led_voltage, led_current]) #, ignore_index=True) # TODO test with and without
# Inform user of measurement
print(f'Measurement made, for {position_1}, {position_2}')
else:
### 1D case
for angle1 in measurement_angles:
## Move
motor_rotation.go_to(apparatus_settings, angle1, motor1=motor1, max_velocity=True)
## Turn LED on & get LED voltage and current (with osr=8, this takes 0.15s per sample)
led_voltage, led_current = device[apparatus_settings["led_channel"]].oneshot(experiment_parameters["target_led_voltage"])[0]
## Get PD_Voltage & motor position
photodetector_votage = np.mean(device[apparatus_settings["pd_channel"]].measure(experiment_parameters["measurement_sample_count"]))
position_1 = processing.custom_modulo(processing.steps_to_degrees(motor1.get_position(),apparatus_settings))
## Record data
experiment_data.append([position_1, 0.0, photodetector_votage, led_voltage, led_current])
## Inform user of measurement
print(f'Measurement made, for {position_1}')
## Turn off LED
device[apparatus_settings["led_channel"]].set.enabled(False, response=0)
print("Data collected")
print()
def get_data_fast(experiment_data:list, measurement_interval:float, measeurement_info:tuple, experiment_parameters:dict, apparatus_settings:dict):
angle1_start, angle1_end, angle1_velocity, angle2_start, angle2_end, angle2_velocity = measeurement_info
""" ### MANUAL ADJUSTMENT ### - Assume is at calibrated position (is at greatest clockwise extream where the led can be seen from the photo detector)"""
## Add info to experiment_parameters
experiment_parameters["stop_for_measurement"] = False
experiment_parameters["measurement_sample_count"] = 1
## Alert user to forced overide of settings
print("Overridden 'measurement_sample_count'. measurement_sample_count: 1")
## Show user the sample interval
print("Mesurement interval (sec): ", measurement_interval)
## Connect to photodetector & led
with Device(apparatus_settings["pd_led_com"]) as device:
device.connect()
if not device.is_connected():
raise Exception("Faild to connect (photometer & led)")
else:
print('Connected (photometer & led)')
## Setup channels for LED & photodetector
device[apparatus_settings["led_channel"]].set.enabled(True, response=0)
device[apparatus_settings["led_channel"]].set.range(2, response=0)
device[apparatus_settings["led_channel"]].set.osr(experiment_parameters["osr"], response=0)
device[apparatus_settings["pd_channel"]].set.enabled(True, response=0)
device[apparatus_settings["pd_channel"]].set.osr(experiment_parameters["osr"], response=0)
## Connect to motor 1
with KinesisMotor(apparatus_settings["motor1_id"]) as motor1:
motor1.setup_velocity(0,processing.degrees_to_steps(angle1_velocity,apparatus_settings),processing.degrees_to_steps(angle1_velocity,apparatus_settings))
print('Connected (motor1)')
if not (None in [angle2_start, angle2_end, angle2_velocity]):
### 2D case
## Connect to motor 2
with KinesisMotor(apparatus_settings["motor2_id"]) as motor2:
motor2.setup_velocity(0,processing.degrees_to_steps(angle2_velocity,apparatus_settings),processing.degrees_to_steps(angle2_velocity,apparatus_settings))
print('Connected (motor2)')
## Goto start
motor_rotation.go_to(apparatus_settings, angle1_start, angle2_start, motor1=motor1, motor2=motor2, max_velocity=True)
## Start data collection
print('Data collection started')
motor1.move_to(processing.degrees_to_steps(angle1_end,apparatus_settings))
motor2.move_to(processing.degrees_to_steps(angle2_end,apparatus_settings))
while 'moving_bk' in motor1.get_status() or 'moving_fw' in motor1.get_status() or 'moving_bk' in motor2.get_status() or 'moving_fw' in motor2.get_status():
## Turn on LED & get LED voltage and current (with osr=8, this takes 0.15s per sample)
led_voltage, led_current = device[apparatus_settings["led_channel"]].oneshot(experiment_parameters["target_led_voltage"])[0]
## Get PD_Voltage & motor position
photodetector_votage = np.mean(device[apparatus_settings["pd_channel"]].measure(experiment_parameters["measurement_sample_count"]))
position_1 = processing.custom_modulo(processing.steps_to_degrees(motor1.get_position(),apparatus_settings)) #-motor_base_position_in_steps + measurement_angles_in_steps[0]
position_2 = processing.steps_to_degrees(motor2.get_position(),apparatus_settings) % 360
## Record data
experiment_data.append([position_1, position_2, photodetector_votage, led_voltage, led_current]) #, ignore_index=True) # TODO test with and without
## Inform user of measurement
print(f'Measurement made, for {position_1}, {position_2}')
time.sleep(measurement_interval)
else:
### 1D case
## Goto start
motor_rotation.go_to(apparatus_settings, angle1_start, motor1=motor1, max_velocity=True)
## Start movement for data collection
print('Data collection started')
motor1.move_to(processing.degrees_to_steps(angle1_end,apparatus_settings))
while 'moving_bk' in motor1.get_status() or 'moving_fw' in motor1.get_status():
## Turn on LED & get LED voltage and current (with osr=8, this takes 0.15s per sample)
led_voltage, led_current = device[apparatus_settings["led_channel"]].oneshot(experiment_parameters["target_led_voltage"])[0]
## Get PD_Voltage & motor position
photodetector_votage = np.mean(device[apparatus_settings["pd_channel"]].measure(experiment_parameters["measurement_sample_count"]))
position_1 = processing.custom_modulo(processing.steps_to_degrees(motor1.get_position(),apparatus_settings))
## Record data
experiment_data.append([position_1, 0.0, photodetector_votage, led_voltage, led_current]) #, ignore_index=True) # TODO test with and without
## Inform user of measurement
print(f'Measurement made, for {position_1}')
time.sleep(measurement_interval)
print('Data collection finished')
print()