Hello everyone,
The algorithm is to trigger the laser and detector and then read the number of pulses from the two TTL inputs. The operating frequency was set to 50 kHz (project assumption). The goal is to read and write to file 100 million bits.
The problem is saving the collected data. I tried to solve this with the @rpc command on the "saveData" function but without introducing the 60 us delay there was a problem except underrun. A similar situation occurred when using the append_to_dataset and mutate_dataset methods. One-time saving of the collected data on the list is limited to about 100,000 (with larger lists, Sinara crashes).
Could you give me any advice, how to solve this problem? Is there another way to save the collected data without introducing additional delays?
Thanks in advice.
I put my code below
from artiq.experiment import *
import numpy as np
class ProjectGC(EnvExperiment):
def build(self):
self.setattr_device("core")
self.setattr_device("ccb")
self.setattr_device("ttl0") # detector trigger
self.setattr_device("ttl1") # laser trigger
self.setattr_device("ttl4") # detector 1
self.setattr_device("ttl5") # detector 2
# Variables
# Number of bits in one loop
self.number_of_bits = 100000
# Loops number
self.loops_number = 100000
# output Variables
self.bits_out = [np.int64(0) for _ in range(self.number_of_bits)]
def run(self):
self.run_kernel()
@kernel
def run_kernel(self):
loop_num = 0
self.core.reset()
self.ttl0.output()
self.ttl1.output()
self.ttl4.input()
self.ttl5.input()
while loop_num < self.loops_number:
self.looping(loop_num)
#self.saveData()
#delay(60 * us)
loop_num += 1
@kernel
def looping(self, loop_nr):
bit_nr = 0
while bit_nr < self.number_of_bits:
try:
with parallel:
self.detector_trigger()
self.laser_trigger()
ed4 = self.get_photon_count_4()
ed5 = self.get_photon_count_5()
delay(5 * us)
self.count_fotons(ed4, ed5, bit_nr, loop_nr) # delay 3 us
delay(12 * us)
except RTIOUnderflow:
print("Problem, pos: ", bit_nr, " loop: ", loop_nr)
self.core.break_realtime()
bit_nr += 1
@kernel
def get_photon_count_4(self):
delay(0 * ns)
edgedetect_4 = self.ttl4.gate_rising(1 * us)
return edgedetect_4
@kernel
def get_photon_count_5(self):
delay(0 * ns)
edgedetect_5 = self.ttl5.gate_rising(1 * us)
return edgedetect_5
@kernel
def count_fotons(self, edgedetect_4, edgedetect_5, bit_nr, loop_nr):
b1 = self.ttl4.count(edgedetect_4)
b2 = self.ttl5.count(edgedetect_5)
# 0
if b1 == 1 and b2 == 0:
self.bits_out[bit_nr] += 0
# 1
elif b1 == 0 and b2 == 1:
self.bits_out[bit_nr] += 1
# 2
elif b1 == 0 and b2 == 0:
self.bits_out[bit_nr] += 2
# 3
else:
self.bits_out[bit_nr] += 3
delay(3 * us)
@rpc(flags={"async"})
def saveData(self):
self.bits_out_file = open("bits_out_new", 'a')
bits_out_vals = np.array(self.bits_out)
self.bits_out_file.write(np.array2string(bits_out_vals))
@kernel
def detector_trigger(self):
delay(511 * ns)
self.ttl0.on()
delay(16 * ns)
self.ttl0.off()
# delay(1 * us)
@kernel
def laser_trigger(self):
delay(300 * ns)
self.ttl1.on()
delay(16 * ns)
self.ttl1.off()