diff --git a/config/devices.properties b/config/devices.properties index 6f39198..5e45d8d 100755 --- a/config/devices.properties +++ b/config/devices.properties @@ -36,7 +36,7 @@ image=ch.psi.pshell.imaging.FileSource|/afs/psi.ch/intranet/SF/Applications/conf #cam=ch.psi.pshell.epics.ArraySource|SINEG01-DSCR350:FPICTURE Int16 5529600||-200| #matrix=ch.psi.pshell.epics.GenericMatrix|SINEG01-DSCR350:FPICTURE 2560 2160||| cam1=ch.psi.pshell.epics.PsiCamera|SINEG01-DSCR350||-200|false -cam2=ch.psi.pshell.epics.PsiCamera|SINBD01-DSCR010||-200| +cam2=ch.psi.pshell.epics.PsiCamera|SLG-LCAM-C041||-200| gsx=ch.psi.pshell.epics.Positioner|SINEG01-MSOL130:X_SP SINEG01-MSOL130:X|||true gsy=ch.psi.pshell.epics.Positioner|SINEG01-MSOL130:Y_SP SINEG01-MSOL130:Y|||true gsrx=ch.psi.pshell.epics.Positioner|SINEG01-MSOL130:ROT_X_SP SINEG01-MSOL130:ROT_X|||true diff --git a/devices/CurrentCamera.properties b/devices/CurrentCamera.properties index d3cbcc1..1554007 100644 --- a/devices/CurrentCamera.properties +++ b/devices/CurrentCamera.properties @@ -1,7 +1,7 @@ -#Fri May 26 10:13:54 CEST 2017 +#Fri May 26 15:06:25 CEST 2017 colormap=Flame colormapAutomatic=true -colormapMax=30.0 +colormapMax=2000.0 colormapMin=0.0 flipHorizontally=false flipVertically=false @@ -21,9 +21,9 @@ rotation=0.0 rotationCrop=false scale=1.0 serverURL=localhost\:10000 -spatialCalOffsetX=-642.5156395013756 -spatialCalOffsetY=-609.5079128412647 -spatialCalScaleX=-26.85765405371486 -spatialCalScaleY=-27.12477275985196 +spatialCalOffsetX=-158.5197404227148 +spatialCalOffsetY=-106.41406334370838 +spatialCalScaleX=-14.401765522203947 +spatialCalScaleY=-13.617021560668945 spatialCalUnits=mm transpose=false diff --git a/devices/cam2.properties b/devices/cam2.properties index 5a95c37..cbb8ef7 100644 --- a/devices/cam2.properties +++ b/devices/cam2.properties @@ -1,18 +1,18 @@ -#Thu May 11 14:22:56 CEST 2017 -colormap=Grayscale +#Fri May 26 11:30:37 CEST 2017 +colormap=Flame colormapAutomatic=true colormapMax=255.0 colormapMin=0.0 flipHorizontally=false flipVertically=false grayscale=false -imageHeight=1680 -imageWidth=1744 +imageHeight=1024 +imageWidth=1280 invert=false offsetX=0.0 offsetY=0.0 -regionStartX=433 -regionStartY=241 +regionStartX=1 +regionStartY=1 rescaleFactor=1.0 rescaleOffset=0.0 roiHeight=-1 diff --git a/script/Devices/ImageStats.py b/script/Devices/ImageStats.py index bd33223..8c51219 100644 --- a/script/Devices/ImageStats.py +++ b/script/Devices/ImageStats.py @@ -4,7 +4,46 @@ from ch.psi.pshell.imaging.Overlays import * import ch.psi.pshell.imaging.Pen as Pen import java.awt.Color as Color import random +import ch.psi.pshell.imaging.ImageListener as ImageListener +from operator import add, mul, sub, truediv +def arrmul(a, b): + """Multiply 2 series of the same size. + + Args: + + a(list, tuple, array ...): subscriptable object containing numbers + b(list, tuple, array ...): subscriptable object containing numbers + + Returns: + List + + """ + return map(mul, a, b) + +def center_of_mass(data, x = None): + """Calculate the center of mass of a series, and its rms. + + Args: + + data(list, tuple, array ...): subscriptable object containing numbers + x(list, tuple, array ..., optional): x coordinates + + Returns: + Tuple (com, rms) + + """ + if x is None: + x = Arr.indexesDouble(len(data)) + data_sum = sum(data) + if (data_sum==0): + return float('nan') + xmd = arrmul( x, data) + com = sum(xmd) / data_sum + xmd2 = arrmul( x, xmd) + com2 = sum(xmd2) / data_sum + rms = math.sqrt(abs(com2 - com * com)) + return (com, rms) def get_centroid(source): bi = source.getImage() @@ -83,20 +122,38 @@ class ImageStats(DeviceBase): set_device_alias(self.com_y_stdev, name + " com y stdev") self.bg_en = False self.num_images = 1 - self.initialize() + self.initialize() + + class SourceListener (ImageListener): + def __init__(self, dev): + self.dev=dev + def onImage(self, origin, image, data): + print "On image" + self.dev.doUpdate() + def onError(self, origin, ex): + self.dev.com_x_samples, self.dev.com_y_samples = [], [] + self.listener = SourceListener(self) + self.source.addListener(self.listener) def doUpdate(self): + print "Update" self.com_x_samples, self.com_y_samples = [], [] for i in range(self.num_images): - if type(self.source) is not ch.psi.pshell.imaging.FileSource: - self.source.waitNext(3000) - centroid = get_centroid(self.source) - print "cent ", centroid - if centroid is not None: - self.com_x_samples.append(centroid[0]) - self.com_y_samples.append(centroid[1]) - + #if type(self.source) is not ch.psi.pshell.imaging.FileSource: + # self.source.waitNext(5000) + #centroid = get_centroid(self.source) + #print "cent ", centroid + #if centroid is not None: + # self.com_x_samples.append(centroid[0]) + # self.com_y_samples.append(centroid[1]) + x_profile = self.source.data.integrateVertically(True) + y_profile = self.source.data.integrateHorizontally(True) + com_x,rms_x = center_of_mass(x_profile) + com_y,rms_y = center_of_mass(y_profile) + self.com_x_samples.append(com_x) + self.com_y_samples.append(com_y) + def setNumberOfImages(self, value): self.num_images = value @@ -106,8 +163,9 @@ class ImageStats(DeviceBase): def captureBackground(self, images): self.doInitialize() - def doClose(self): - pass + def doClose(self): + print "close" + self.source.removeListener(self.listener) def start(self): pass @@ -124,14 +182,23 @@ def get_simulated_source(img): show_panel(simulated_source) return simulated_source -if __name__ == "__builtin__": - simulated_source = get_simulated_source(image) - print get_centroid(simulated_source) - - add_device(ImageStats("image_stats", simulated_source), True) - image_stats.enableBackground(False) - for i in range (10): - image_stats.update() - print image_stats.take(), image_stats.com_x_mean.read(), image_stats.com_y_mean.read() - time.sleep(1) + +if __name__ == "__builtin__": + #simulated_source = get_simulated_source(image) + #print get_centroid(simulated_source) + + add_device(ImageStats("image_stats", cam2), True) + cam2.waitNext(5000) + image_stats.enableBackground(False) + #for i in range (10): + # image_stats.update() + # print image_stats.take(), image_stats.com_x_mean.read(), image_stats.com_y_mean.read() + # time.sleep(1) + + image_stats.setNumberOfImages(3) + sensors = [image_stats.com_x_mean, image_stats.com_y_mean, image_stats.com_x_stdev, image_stats.com_y_stdev] + try: + tscan(sensors, 10, 0.1) + finally: + image_stats.close() diff --git a/script/Diagnostics/Laser_position_scan.py b/script/Diagnostics/Laser_position_scan.py new file mode 100644 index 0000000..8ab22fd --- /dev/null +++ b/script/Diagnostics/Laser_position_scan.py @@ -0,0 +1,76 @@ +import ch.psi.pshell.epics.ControlledVariable as ControlledVariable + +#CAMERA = "S10DI01-DSCR020" #"simulation" +CAMERA = "SINDI02-DLAC055" #"simulation" +QUADRUPOLE = "S10CB02-MQUA230" # quadrupole for the scan with S10DI01-DSCR020: S10CB02-MQUA230 +CHARGE_BPM = "SINEG01-DBPM340:Q1" +CHARGE_ICT = "SINEG01-DICT215:B1_CHARGE-OP" +RANGE = (-3.0, 0.0) +STEPS = 100 +SETTLING_TIME = 1 + +#kill_camtool() +check_camtool() +print camtool.getCameras() + +#camtool.start("SINBD01-DSCR010") +camtool.start(CAMERA) +camtool.stream.waitCacheChange(10000) +print camtool.value.identifiers + +#plot(camtool.getValue("y_fit_gauss_function")) +m=camtool.getDataMatrix() +x = camtool.stream.getChild("x_fit_mean") +y = camtool.stream.getChild("y_fit_mean") + +""" +ax = create_averager(x, 5, -1, "X Fit") +ay = create_averager(y, 5, -1, "Y Fit") +ay.monitored = True +""" +set_device_alias(m,"image") + +#Create quadrupole device +quad = ControlledVariable(QUADRUPOLE, QUADRUPOLE + ":I-SET", QUADRUPOLE + ":I-READ") +quad.config.minValue =-10.0 +quad.config.maxValue = 10.0 +quad.config.precision = 3 +quad.config.resolution = 0.007 +quad.config.save() +quad.initialize() + +bpm = Channel(CHARGE_BPM, 'd', alias = "Charge BPM") +ict = Channel(CHARGE_ICT, 'd', alias = "Charge ICT") + +#Metadata + +try: + set_attribute("/", "Camera", CAMERA) + set_attribute("/", "Quadrupole", QUADRUPOLE) + set_attribute("/", "Scan Parameters", RANGE + (STEPS,)) + set_attribute("/", "Screen Position", caget(CAMERA + ":GET_SCREEN1_POS", 's')) + set_attribute("/", "Filter Position", caget(CAMERA + ":GET_FILTER", 's')) +except: + pass + +try: + laser_off() + #save_dataset("/Background", m.read()) + #mscan (camtool.stream, m, 10) #Saves 10 next frames -> For machine at 10 -> 100Hz + tscan(m, 10, 1.0) # 10 samples every 0.2 s -> For machine at 1Hz + laser_on() + + #tscan((m, x, y), 10, 1.0) # 10 samples every 1.0 s + #mscan (camtool.stream,(m, x, y), -1, 5.0) #Saves all frames received in 5s + #mscan (camtool.stream,(m, x, y), 50) # Saves firs 50 frames + #lscan(quad, (m, x, y), RANGE[0], RANGE[1], STEPS, latency=SETTLING_TIME) + + readables = camtool.stream.getReadables().clone() + [bpm, ict] + readables.remove(camtool.stream.getChild("image")) + readables.insert(0,m) + lscan(quad, readables, RANGE[0], RANGE[1], STEPS, latency=SETTLING_TIME) + +finally: + quad.close() + camtool.stop() + diff --git a/script/Diagnostics/Quick_GunScan.py b/script/Diagnostics/Quick_GunScan.py new file mode 100644 index 0000000..037e938 --- /dev/null +++ b/script/Diagnostics/Quick_GunScan.py @@ -0,0 +1,16 @@ +knob = Channel("SIN-LDIO-LAS0041:DAC01_VOLTS") +sensor1 = Channel("SINEG01-DSCR190:pipeline.x_stats.com_egu") +sensor2 = Channel("SINEG01-DSCR190:pipeline.y_stats.com_egu") +sensor3 = Channel("SLG-LCAM-C103:FIT-XPOS") +sensor4 = Channel("SLG-LCAM-C103:FIT-YPOS") +sensor5 = Channel("SINEG01-DSCR190:pipeline.x_stats.rms_egu") +sensor6 = Channel("SINEG01-DSCR190:pipeline.y_stats.rms_egu") + +start = 2 +stop = 6.4 +nstep = 10 +lat = 0.5 + +r = lscan(knob, (sensor1, sensor2, sensor3, sensor4, sensor5, sensor6), start, stop, nstep, lat) + +plot(r.getReadable(0), name = 'plotname', xdata = r.getReadable(1))