Skip to content

Commit 4c3317d

Browse files
committed
- Modified how analog input sample times are computed by the Data_logger, to only use the chunk's timestamp if the sample time computed based on the previous sample time and sampling rate does not match the chunk timestamp. This avoids jumps in sample times at chunk boundaries that otherwise occured when sampling rate is > 1KHz.
- Changed the order of function calls at run start in framework.py to ensure that the run_start methods of hardware objects are called as late as possible in the sequence, ensuring sample times for analog inputs are as well aligned as possible with event/state timestamps.
1 parent abcc78a commit 4c3317d

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

com/data_logger.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ def open_data_files(self, session_filepath):
132132
self.d_tempfile_path = self.path_stem + f'.data.1{self.data_type}.temp'
133133
self.time_tempfile = open(self.t_tempfile_path, 'wb')
134134
self.data_tempfile = open(self.d_tempfile_path, 'wb')
135+
self.next_chunk_start_time = 0
135136

136137
def close_files(self):
137138
'''Close data files. Convert temp files to numpy.'''
@@ -148,9 +149,14 @@ def close_files(self):
148149

149150
def save_analog_chunk(self, timestamp, data_array):
150151
'''Save a chunk of analog data to .pca data file.'''
152+
if np.abs(self.next_chunk_start_time - timestamp/1000)<0.001:
153+
chunk_start_time = self.next_chunk_start_time
154+
else:
155+
chunk_start_time = timestamp/1000
151156
times = (np.arange(len(data_array), dtype='float64')
152-
/ self.sampling_rate) + timestamp/1000 # Seconds
157+
/ self.sampling_rate) + chunk_start_time # Seconds
153158
self.time_tempfile.write(times.tobytes())
154159
self.data_tempfile.write(data_array.tobytes())
155160
self.time_tempfile.flush()
156-
self.data_tempfile.flush()
161+
self.data_tempfile.flush()
162+
self.next_chunk_start_time = chunk_start_time+len(data_array)/self.sampling_rate

pyControl/framework.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,15 @@ def run():
126126
event_queue.reset()
127127
data_output_queue.reset()
128128
if not hw.initialised: hw.initialise()
129+
usb_serial.setinterrupt(-1) # Disable 'ctrl+c' on serial raising KeyboardInterrupt.
129130
current_time = 0
130131
ut.print_variables(when='t')
131-
hw.run_start()
132132
start_time = pyb.millis()
133133
clock.init(freq=1000)
134-
clock.callback(_clock_tick)
135-
usb_serial.setinterrupt(-1) # Disable 'ctrl+c' on serial raising KeyboardInterrupt.
136-
running = True
134+
clock.callback(_clock_tick)
137135
sm.start()
136+
hw.run_start()
137+
running = True
138138
# Run
139139
while running:
140140
# Priority 1: Process hardware interrupts.

pyControl/hardware.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,12 @@ def __init__(self, pin, name, sampling_rate, threshold=None, rising_event=None,
235235
assign_ID(self)
236236

237237
def _run_start(self):
238+
# Start sampling timer, initialise threshold, aquire first sample.
238239
self.timer.init(freq=self.Analog_channel.sampling_rate)
239240
self.timer.callback(self._timer_ISR)
240241
if self.threshold:
241242
self.threshold.run_start(self.read_sample())
243+
self._timer_ISR(0)
242244

243245
def _run_stop(self):
244246
self.timer.deinit()

0 commit comments

Comments
 (0)