examples/remote_exec: dataset access

This commit is contained in:
Sebastien Bourdeauducq 2016-04-04 19:10:49 +08:00
parent 4ce00e3b7a
commit 342ab639fc
3 changed files with 40 additions and 15 deletions

View File

@ -2,6 +2,7 @@ import time
import inspect import inspect
from artiq.experiment import * from artiq.experiment import *
from artiq.protocols.remote_exec import connect_global_rpc
import remote_exec_processing import remote_exec_processing
@ -18,19 +19,16 @@ class RemoteExecDemo(EnvExperiment):
def prepare(self): def prepare(self):
if self.remote_exec: if self.remote_exec:
connect_global_rpc(self.camera_sim_rexec)
self.camera_sim_rexec.add_code( self.camera_sim_rexec.add_code(
inspect.getsource(remote_exec_processing)) 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): def transfer_parameters(self, parameters):
w, h, cx, cy = parameters w, h, cx, cy = parameters
self.set_dataset("gaussian_w", w, save=False, broadcast=True) self.set_dataset("rexec_demo.gaussian_w", w, save=False, broadcast=True)
self.set_dataset("gaussian_h", h, save=False, broadcast=True) self.set_dataset("rexec_demo.gaussian_h", h, save=False, broadcast=True)
self.set_dataset("gaussian_cx", cx, save=False, broadcast=True) self.set_dataset("rexec_demo.gaussian_cx", cx, save=False, broadcast=True)
self.set_dataset("gaussian_cy", cy, save=False, broadcast=True) self.set_dataset("rexec_demo.gaussian_cy", cy, save=False, broadcast=True)
def fps_meter(self): def fps_meter(self):
t = time.monotonic() t = time.monotonic()
@ -39,7 +37,7 @@ class RemoteExecDemo(EnvExperiment):
dt = t - self.last_pt_update dt = t - self.last_pt_update
if dt >= 5: if dt >= 5:
pt = dt/self.iter_count pt = dt/self.iter_count
self.set_dataset("picture_pt", pt, save=False, broadcast=True) self.set_dataset("rexec_demo.picture_pt", pt, save=False, broadcast=True)
self.last_pt_update = t self.last_pt_update = t
self.iter_count = 0 self.iter_count = 0
else: else:
@ -51,15 +49,18 @@ class RemoteExecDemo(EnvExperiment):
self.fps_meter() self.fps_meter()
data = self.camera_sim.get_picture() data = self.camera_sim.get_picture()
if self.show_picture: if self.show_picture:
self.set_dataset("picture", data, save=False, broadcast=True) self.set_dataset("rexec_demo.picture", data,
save=False, broadcast=True)
if self.enable_fit: if self.enable_fit:
self.transfer_parameters(remote_exec_processing.fit(data)) p = remote_exec_processing.fit(data, self.get_dataset)
self.transfer_parameters(p)
self.scheduler.pause() self.scheduler.pause()
def run_remote(self): def run_remote(self):
while True: while True:
self.fps_meter() self.fps_meter()
self.transfer_parameters(self.camera_sim_rexec.call("get_and_fit")) p = self.camera_sim_rexec.call("get_and_fit")
self.transfer_parameters(p)
self.scheduler.pause() self.scheduler.pause()
def run(self): def run(self):

View File

@ -1,6 +1,10 @@
import numpy as np import numpy as np
from numba import jit from numba import jit
from scipy.optimize import least_squares from scipy.optimize import least_squares
import logging
logger = logging.getLogger(__name__)
@jit(nopython=True) @jit(nopython=True)
@ -14,17 +18,35 @@ def compute_gaussian(r, img_w, img_h,
r[x, y] = np.exp(-ds/2) r[x, y] = np.exp(-ds/2)
def fit(data): def fit(data, get_dataset):
img_w, img_h = data.shape img_w, img_h = data.shape
def err(parameters): def err(parameters):
r = np.empty((img_w, img_h)) r = np.empty((img_w, img_h))
compute_gaussian(r, img_w, img_h, *parameters) compute_gaussian(r, img_w, img_h, *parameters)
r -= data r -= data
return r.ravel() return r.ravel()
guess = [12, 15, img_w/2, img_h/2] guess = [
get_dataset("rexec_demo.gaussian_w", 12),
get_dataset("rexec_demo.gaussian_h", 15),
get_dataset("rexec_demo.gaussian_cx", img_w/2),
get_dataset("rexec_demo.gaussian_cy", img_h/2)
]
res = least_squares(err, guess) res = least_squares(err, guess)
return res.x return res.x
def get_and_fit(): def get_and_fit():
return fit(controller_driver.get_picture()) if "dataset_db" in globals():
logger.info("using dataset DB for Gaussian fit guess")
def get_dataset(name, default):
from artiq.protocols import pc_rpc
try:
return dataset_db.get(name)
except (KeyError, pc_rpc.RemoteError): # TODO: serializable exceptions
return default
else:
logger.info("using defaults for Gaussian fit guess")
def get_dataset(name, default):
return default
get_dataset = lambda name, default: default
return fit(controller_driver.get_picture(), get_dataset)

View File

@ -2,6 +2,7 @@
import numpy as np import numpy as np
from numba import jit from numba import jit
import logging
from artiq.protocols.remote_exec import simple_rexec_server_loop from artiq.protocols.remote_exec import simple_rexec_server_loop
@ -51,6 +52,7 @@ class CameraSimulation:
def main(): def main():
logging.basicConfig(level=logging.INFO)
simple_rexec_server_loop("camera_sim", CameraSimulation(), simple_rexec_server_loop("camera_sim", CameraSimulation(),
"::1", 6283) "::1", 6283)