diff --git a/script/Alignment/Gun_solenoid_alignment.py b/script/Alignment/Gun_solenoid_alignment.py index 0f9c6ad..098b4cc 100755 --- a/script/Alignment/Gun_solenoid_alignment.py +++ b/script/Alignment/Gun_solenoid_alignment.py @@ -11,7 +11,6 @@ import org.apache.commons.math3.linear.Array2DRowRealMatrix as Matrix -import ch.psi.utils.Convert.toBidimensional as mono_to_bidi import datetime from camtool import CamTool #run("camtool") @@ -38,29 +37,16 @@ plot_name = datetime.datetime.fromtimestamp(time.time()).strftime('%H%M%S') add_device(CamTool("camtool") , True) +camtool.enableBackground (use_background) + +number_images = 1 +use_background = True +multiple_background = True +number_backgrounds = 5 + cam_x =camtool.cam_x # Channel("SINEG01-DSCR190:profile.X_stats.com", alias = "cam_x") cam_y = camtool.cam_y #Channel("SINEG01-DSCR190:profile.Y_stats.com", alias = "cam_y") -profile_x = camtool.profile_x #Channel("SINEG01-DSCR190:profile.X", alias = "profile_x") -profile_y = camtool.profile_y #Channel("SINEG01-DSCR190:profile.Y", alias = "profile_y") -cam_raw_data = camtool.cam_image #Channel("SINEG01-DSCR190:data") - - -class CameraImage(ReadableMatrix): - def read(self): - raw = cam_raw_data.read() - return Matrix(mono_to_bidi(raw, self.getHeight(), self.getWidth())).transpose().getData() #data is transposed - #return mono_to_bidi(raw, self.getWidth(), self.getHeight()) - - def getWidth(self): - return self._width - - def getHeight(self): - return self._height - -cam_img = CameraImage() -cam_img._width = len(profile_x.read()) -cam_img._height = len(profile_y.read()) def ccr(mag): @@ -117,7 +103,7 @@ def ar(): r = None laser_on() try: - sensors = [cam_x, cam_y, cam_img] if plot_image else [cam_x, cam_y] + sensors = [cam_x, cam_y, camtool.image] if plot_image else [cam_x, cam_y] r = lscan(gun_solenoid, sensors , I1, I2, dI, settling_time, before_read = br, after_read = ar) finally: laser_off() @@ -136,10 +122,8 @@ if do_elog: msg = msg + "\n\n" + r.print() elog("Gun solenoid current scan", msg , get_plot_snapshots()) -print "QUITING" set_return([r, hx,hy]) -print "QUIT" diff --git a/script/camtool.py b/script/camtool.py index 13d3426..f84d981 100644 --- a/script/camtool.py +++ b/script/camtool.py @@ -1,5 +1,27 @@ #import ch.psi.pshell.device.DeviceBase as DeviceBase from startup import * +import ch.psi.utils.Convert.toBidimensional as mono_to_bidi + + +class CamToolImage(ReadableMatrix): + def __init__(self, camtool): + self.camtool = camtool + shape = camtool.cam_shape.read() + self.width = shape[1] #len(camtool.profile_x.read()) + self.height = shape[0] #len(camtool.(profile_y.read()) + + def read(self): + raw = self.camtool.cam_data.read() + return Matrix(mono_to_bidi(raw, self.getHeight(), self.getWidth())).transpose().getData() #data is transposed + #return mono_to_bidi(raw, self.getWidth(), self.getHeight()) + + def getWidth(self): + return self.width + + def getHeight(self): + return self.height + + class CamTool(DeviceBase): def __init__(self, name, prefix = "cam:", latch = False): @@ -16,13 +38,15 @@ class CamTool(DeviceBase): self.cam_y = Channel(self.data_prefix + "y_stats.com", alias = "cam_y") self.profile_x = Channel(self.data_prefix + "profile.x", alias = "profile_x") self.profile_y = Channel(self.data_prefix + "profile.y", alias = "profile_y") - self.cam_image = Channel(self.data_prefix + "image", alias = "cam_image") - self.cam_image_shape = Channel(self.data_prefix + "image.shape", alias = "cam_image_shape") #[D1, D2, ...] + self.cam_data = Channel(self.data_prefix + "image", alias = "cam_data") + self.cam_shape = Channel(self.data_prefix + "image.shape", alias = "cam_image_shape") #[D1, D2, ...] self.cam_bg_image = Channel(self.data_prefix + "bg_image", alias = "cam_bg_image") #[D1, D2, ...] self.cam_bg_en = Channel(self.prefix + "bg.enabled", type = 'l', alias = "cam_bg_en") self.cam_bg_capture = Channel(self.prefix + "bg.capture", type = 'l', alias = "cam_bg_capture") self.cam_bg_capture_remain = Channel(self.prefix + "bg.capture_remain", alias = "cam_bg_capture_remain") + + self.image = CamToolImage(self) def doInitialize(self): @@ -39,13 +63,16 @@ class CamTool(DeviceBase): def capture(self): timestamp = self.timestamp.read() - if self.latch: - self.cam_latch.write(1) - else: + if not self.latch: self.cam_run.write(1) - while(timestamp == self.timestamp.read()): - time.sleep(0.001) - self.triggerValueChanged(timestamp, None) + while(True): + val = self.timestamp.read() + if timestamp != val: + self.setCache(val) + break + if self.latch: + self.cam_latch.write(1) + time.sleep(0.001) def doUpdate(self): self.capture() @@ -73,8 +100,8 @@ class CamTool(DeviceBase): self.cam_y.close() self.profile_x.close() self.profile_y.close() - self.cam_image.close() - self.cam_image_shape.close() + self.cam_data.close() + self.cam_shape.close() self.cam_bg_image.close() self.cam_bg_en.close() self.cam_bg_capture.close() @@ -89,8 +116,9 @@ if __name__ == "__builtin__": camera_tool.enableBackground(False) camera_tool.enableBackground(True) camera_tool.captureBackground(5) - - camera_tool.capture() + for i in range (100): + camera_tool.capture() + print camera_tool.take() print camera_tool.cam_x.read(), camera_tool.cam_y.read() add_device(camera_tool, True) \ No newline at end of file diff --git a/script/local.py b/script/local.py index 8def918..03db6fb 100755 --- a/script/local.py +++ b/script/local.py @@ -189,7 +189,7 @@ def add_convex_hull_plot(title, x,y, name=None, clear = False, x_range = None, y (hx,hy) = convex_hull(x=x, y=y) hx.append(hx[0]); hy.append(hy[0]) hull.setLineWidth(2) - hull.setData(hx,hy) + hull.setData(to_array(hx,'d') , to_array(hy,'d')) hull.setColor(s.color) return [hx,hy]