forked from M-Labs/artiq
1
0
Fork 0

TimeManager: use units further, add now/at functionality

This commit is contained in:
Sebastien Bourdeauducq 2014-05-30 19:01:27 +02:00
parent 732412e4c9
commit 13acb57179
3 changed files with 32 additions and 19 deletions

View File

@ -44,6 +44,8 @@ class _DummyTimeManager:
enter_sequential = _not_implemented enter_sequential = _not_implemented
exit = _not_implemented exit = _not_implemented
take_time = _not_implemented take_time = _not_implemented
get_time = _not_implemented
set_time = _not_implemented
_time_manager = _DummyTimeManager() _time_manager = _DummyTimeManager()
@ -70,4 +72,10 @@ class _Parallel:
parallel = _Parallel() parallel = _Parallel()
def delay(duration): def delay(duration):
_time_manager.take_time(duration.amount) _time_manager.take_time(duration)
def now():
return _time_manager.get_time()
def at(time):
_time_manager.set_time(time)

View File

@ -19,13 +19,12 @@ class Input:
def wait_edge(self): def wait_edge(self):
duration = self.prng.randrange(self.wait_min, self.wait_max)*units.ms duration = self.prng.randrange(self.wait_min, self.wait_max)*units.ms
time.manager.event(("wait_edge", self.name, duration)) time.manager.event(("wait_edge", self.name, duration))
time.manager.take_time(duration.amount) time.manager.take_time(duration)
def count_gate(self, duration): def count_gate(self, duration):
result = self.prng.randrange(self.count_min, self.count_max) result = self.prng.randrange(self.count_min, self.count_max)
units.check_unit(duration, units.base_s_unit)
time.manager.event(("count_gate", self.name, duration, result)) time.manager.event(("count_gate", self.name, duration, result))
time.manager.take_time(duration.amount) time.manager.take_time(duration)
return result return result
class WaveOutput: class WaveOutput:
@ -33,10 +32,8 @@ class WaveOutput:
self.name = name self.name = name
def pulse(self, frequency, duration): def pulse(self, frequency, duration):
units.check_unit(frequency, units.base_Hz_unit)
units.check_unit(duration, units.base_s_unit)
time.manager.event(("pulse", self.name, frequency, duration)) time.manager.event(("pulse", self.name, frequency, duration))
time.manager.take_time(duration.amount) time.manager.take_time(duration)
class VoltageOutput: class VoltageOutput:
def __init__(self, name): def __init__(self, name):

View File

@ -1,11 +1,12 @@
from operator import itemgetter from operator import itemgetter
from artiq.language import units, experiment from artiq.language.units import *
from artiq.language import experiment
class SequentialTimeContext: class SequentialTimeContext:
def __init__(self, current_time): def __init__(self, current_time):
self.current_time = current_time self.current_time = current_time
self.block_duration = 0 self.block_duration = 0*ps
def take_time(self, amount): def take_time(self, amount):
self.current_time += amount self.current_time += amount
@ -14,23 +15,23 @@ class SequentialTimeContext:
class ParallelTimeContext: class ParallelTimeContext:
def __init__(self, current_time): def __init__(self, current_time):
self.current_time = current_time self.current_time = current_time
self.block_duration = 0 self.block_duration = 0*ps
def take_time(self, amount): def take_time(self, amount):
if amount > self.block_duration: if amount > self.block_duration:
self.block_duration = amount self.block_duration = amount
class Manager: class Manager:
def __init__(self): def __init__(self):
self.stack = [SequentialTimeContext(0)] self.stack = [SequentialTimeContext(0*ps)]
self.timeline = [] self.timeline = []
def enter_sequential(self): def enter_sequential(self):
new_context = SequentialTimeContext(self.stack[-1].current_time) new_context = SequentialTimeContext(self.get_time())
self.stack.append(new_context) self.stack.append(new_context)
def enter_parallel(self): def enter_parallel(self):
new_context = ParallelTimeContext(self.stack[-1].current_time) new_context = ParallelTimeContext(self.get_time())
self.stack.append(new_context) self.stack.append(new_context)
def exit(self): def exit(self):
@ -40,16 +41,23 @@ class Manager:
def take_time(self, duration): def take_time(self, duration):
self.stack[-1].take_time(duration) self.stack[-1].take_time(duration)
def get_time(self):
return self.stack[-1].current_time
def set_time(self, t):
dt = t - self.get_time()
if dt < 0*ps:
raise ValueError("Attempted to go back in time")
self.take_time(dt)
def event(self, description): def event(self, description):
self.timeline.append((self.stack[-1].current_time, description)) self.timeline.append((self.get_time(), description))
def format_timeline(self): def format_timeline(self):
r = "" r = ""
prev_time = 0 prev_time = 0*ps
for time, description in sorted(self.timeline, key=itemgetter(0)): for time, description in sorted(self.timeline, key=itemgetter(0)):
t = units.Quantity(time, units.base_s_unit) r += "@{:10} (+{:10}) ".format(str(time), str(time-prev_time))
dt = units.Quantity(time-prev_time, units.base_s_unit)
r += "@{:10} (+{:10}) ".format(str(t), str(dt))
for item in description: for item in description:
r += "{:16}".format(str(item)) r += "{:16}".format(str(item))
r += "\n" r += "\n"