mirror of https://github.com/m-labs/artiq.git
examples: add remote exec demo
This commit is contained in:
parent
e7925fccd2
commit
a563a00bc2
|
@ -0,0 +1,72 @@
|
|||
import time
|
||||
import inspect
|
||||
|
||||
from artiq.experiment import *
|
||||
|
||||
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:
|
||||
self.camera_sim_rexec.add_code(
|
||||
inspect.getsource(remote_exec_processing))
|
||||
|
||||
def set_dataset(self, name, *args, **kwargs):
|
||||
EnvExperiment.set_dataset(self, "rexec_demo." + name,
|
||||
*args, **kwargs)
|
||||
|
||||
def transfer_parameters(self, parameters):
|
||||
w, h, cx, cy = parameters
|
||||
self.set_dataset("gaussian_w", w, save=False, broadcast=True)
|
||||
self.set_dataset("gaussian_h", h, save=False, broadcast=True)
|
||||
self.set_dataset("gaussian_cx", cx, save=False, broadcast=True)
|
||||
self.set_dataset("gaussian_cy", cy, save=False, broadcast=True)
|
||||
|
||||
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
|
||||
self.set_dataset("picture_pt", pt, save=False, broadcast=True)
|
||||
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:
|
||||
self.set_dataset("picture", data, save=False, broadcast=True)
|
||||
if self.enable_fit:
|
||||
self.transfer_parameters(remote_exec_processing.fit(data))
|
||||
self.scheduler.pause()
|
||||
|
||||
def run_remote(self):
|
||||
while True:
|
||||
self.fps_meter()
|
||||
self.transfer_parameters(self.camera_sim_rexec.call("get_and_fit"))
|
||||
self.scheduler.pause()
|
||||
|
||||
def run(self):
|
||||
try:
|
||||
if self.remote_exec:
|
||||
self.run_remote()
|
||||
else:
|
||||
self.run_local()
|
||||
except TerminationRequested:
|
||||
pass
|
|
@ -0,0 +1,30 @@
|
|||
import numpy as np
|
||||
from numba import jit
|
||||
from scipy.optimize import least_squares
|
||||
|
||||
|
||||
@jit(nopython=True)
|
||||
def compute_gaussian(r, img_w, img_h,
|
||||
gaussian_w, gaussian_h,
|
||||
gaussian_cx, gaussian_cy):
|
||||
for y in range(img_h):
|
||||
for x in range(img_w):
|
||||
ds = ((gaussian_cx-x)/gaussian_w)**2
|
||||
ds += ((gaussian_cy-y)/gaussian_h)**2
|
||||
r[x, y] = np.exp(-ds/2)
|
||||
|
||||
|
||||
def fit(data):
|
||||
img_w, img_h = data.shape
|
||||
def err(parameters):
|
||||
r = np.empty((img_w, img_h))
|
||||
compute_gaussian(r, img_w, img_h, *parameters)
|
||||
r -= data
|
||||
return r.ravel()
|
||||
guess = [12, 15, img_w/2, img_h/2]
|
||||
res = least_squares(err, guess)
|
||||
return res.x
|
||||
|
||||
|
||||
def get_and_fit():
|
||||
return fit(controller_driver.get_picture())
|
Loading…
Reference in New Issue