2
0
mirror of https://github.com/m-labs/artiq.git synced 2024-12-05 09:46:36 +08:00
artiq/examples/master/repository/remote_exec_demo.py

74 lines
2.5 KiB
Python
Raw Normal View History

2016-04-02 22:46:29 +08:00
import time
import inspect
from artiq.experiment import *
2016-04-04 19:10:49 +08:00
from artiq.protocols.remote_exec import connect_global_rpc
2016-04-02 22:46:29 +08:00
import remote_exec_processing
class RemoteExecDemo(EnvExperiment):
def build(self):
self.setattr_device("camera_sim")
self.setattr_device("scheduler")
self.setattr_argument("remote_exec", BooleanValue(False))
self.setattr_argument("show_picture", BooleanValue(True), "Local options")
self.setattr_argument("enable_fit", BooleanValue(True), "Local options")
if self.remote_exec:
self.setattr_device("camera_sim_rexec")
def prepare(self):
if self.remote_exec:
2016-04-04 19:10:49 +08:00
connect_global_rpc(self.camera_sim_rexec)
2016-04-02 22:46:29 +08:00
self.camera_sim_rexec.add_code(
inspect.getsource(remote_exec_processing))
def transfer_parameters(self, parameters):
w, h, cx, cy = parameters
2016-04-04 19:10:49 +08:00
self.set_dataset("rexec_demo.gaussian_w", w, save=False, broadcast=True)
self.set_dataset("rexec_demo.gaussian_h", h, save=False, broadcast=True)
self.set_dataset("rexec_demo.gaussian_cx", cx, save=False, broadcast=True)
self.set_dataset("rexec_demo.gaussian_cy", cy, save=False, broadcast=True)
2016-04-02 22:46:29 +08:00
def fps_meter(self):
t = time.monotonic()
if hasattr(self, "last_pt_update"):
self.iter_count += 1
dt = t - self.last_pt_update
if dt >= 5:
pt = dt/self.iter_count
2016-04-04 19:10:49 +08:00
self.set_dataset("rexec_demo.picture_pt", pt, save=False, broadcast=True)
2016-04-02 22:46:29 +08:00
self.last_pt_update = t
self.iter_count = 0
else:
self.last_pt_update = t
self.iter_count = 0
def run_local(self):
while True:
self.fps_meter()
data = self.camera_sim.get_picture()
if self.show_picture:
2016-04-04 19:10:49 +08:00
self.set_dataset("rexec_demo.picture", data,
save=False, broadcast=True)
2016-04-02 22:46:29 +08:00
if self.enable_fit:
2016-04-04 19:10:49 +08:00
p = remote_exec_processing.fit(data, self.get_dataset)
self.transfer_parameters(p)
2016-04-02 22:46:29 +08:00
self.scheduler.pause()
def run_remote(self):
while True:
self.fps_meter()
2016-04-04 19:10:49 +08:00
p = self.camera_sim_rexec.call("get_and_fit")
self.transfer_parameters(p)
2016-04-02 22:46:29 +08:00
self.scheduler.pause()
def run(self):
try:
if self.remote_exec:
self.run_remote()
else:
self.run_local()
except TerminationRequested:
pass