From f1dc9c75113781c7d6ebc0c384a9d1a244203b2e Mon Sep 17 00:00:00 2001 From: voulot_d Date: Thu, 6 Apr 2017 16:57:17 +0200 Subject: [PATCH] Startup --- devices/CurrentCamera.properties | 20 ++-- devices/camtool.properties | 4 +- devices/positioner.properties | 8 ++ script/Alignment/Gun_solenoid_alignment.py | 41 ++++--- script/Alignment/Laser_gun_alignment.py | 29 +++-- script/RFscan/GunScan.py | 4 +- script/local.py | 17 ++- script/test/CamtoolCheckPort.py | 19 +++ script/test/TestAlignmentScan.py | 127 +++++++++++++++++++++ 9 files changed, 216 insertions(+), 53 deletions(-) create mode 100644 devices/positioner.properties create mode 100644 script/test/CamtoolCheckPort.py create mode 100644 script/test/TestAlignmentScan.py diff --git a/devices/CurrentCamera.properties b/devices/CurrentCamera.properties index c315af8..28ec797 100644 --- a/devices/CurrentCamera.properties +++ b/devices/CurrentCamera.properties @@ -1,16 +1,16 @@ -#Tue Apr 04 15:41:38 CEST 2017 +#Thu Apr 06 10:26:03 CEST 2017 colormap=Flame colormapAutomatic=true colormapMax=1000.0 colormapMin=10.0 -flipHorizontally=false +flipHorizontally=true flipVertically=false grayscale=false -imageHeight=1200 -imageWidth=1246 +imageHeight=2160 +imageWidth=2560 invert=false -regionStartX=304 -regionStartY=8 +regionStartX=1 +regionStartY=1 rescaleFactor=1.0 rescaleOffset=0.0 roiHeight=-1 @@ -21,9 +21,9 @@ rotation=0.0 rotationCrop=false scale=1.0 serverURL=localhost\:10000 -spatialCalOffsetX=269.4151234567901 -spatialCalOffsetY=269.5964523281596 -spatialCalScaleX=1.0 -spatialCalScaleY=1.0 +spatialCalOffsetX=-1279.0 +spatialCalOffsetY=-1079.0 +spatialCalScaleX=-53.020711215318485 +spatialCalScaleY=-53.02454840203798 spatialCalUnits=mm transpose=false diff --git a/devices/camtool.properties b/devices/camtool.properties index 031b0ac..0b4355a 100644 --- a/devices/camtool.properties +++ b/devices/camtool.properties @@ -1,5 +1,5 @@ -#Tue Apr 04 16:00:24 CEST 2017 -colormap=Temperature +#Wed Apr 05 17:08:38 CEST 2017 +colormap=Flame colormapAutomatic=true colormapMax=578.797 colormapMin=99.793 diff --git a/devices/positioner.properties b/devices/positioner.properties new file mode 100644 index 0000000..3124a2e --- /dev/null +++ b/devices/positioner.properties @@ -0,0 +1,8 @@ +#Wed Apr 05 09:03:47 CEST 2017 +maxValue=NaN +minValue=NaN +offset=0.0 +precision=-1 +resolution=NaN +scale=1.0 +unit=null diff --git a/script/Alignment/Gun_solenoid_alignment.py b/script/Alignment/Gun_solenoid_alignment.py index 4a22b79..26c4bb6 100755 --- a/script/Alignment/Gun_solenoid_alignment.py +++ b/script/Alignment/Gun_solenoid_alignment.py @@ -12,6 +12,7 @@ import datetime mode = "camtool" # "camtool", "bpm" or "direct" camera_name = "SINEG01-DSCR190" +use_good_region=False if get_exec_pars().source == CommandSource.ui: I1 = 95.0 @@ -28,6 +29,8 @@ if get_exec_pars().source == CommandSource.ui: else: centroid_excursion_plot = False +check_camtool() + laser_was_on = is_laser_on() original_gun_solenoid = gun_solenoid.read() @@ -39,12 +42,12 @@ plot_name = datetime.datetime.fromtimestamp(time.time()).strftime('%H%M%S') if mode == "camtool": - if camtool.getCurrentCamera() != camera_name: - camtool.start(camera_name, None, use_background, None, None, None) if use_background: laser_off() if not multiple_background: - camtool.grabBackground(camera_name, 5) + camtool.stop() + camtool.grabBackground(camera_name, number_backgrounds) + camtool.start(camera_name, 0, use_background, None, 0.0, None) else: if mode == "bpm": add_device(BpmStats("image_stats", camera_name), True) @@ -57,16 +60,21 @@ else: #switch_off_magnets() # add here gun phase setting see wiki page + def before_sample(): - if multiple_background: - camtool.grabBackground(camera_name, 5) - laser_on() - if mode != "camtool": + if mode == "camtool": + if multiple_background: + camtool.stop() + camtool.grabBackground(camera_name, number_backgrounds) + camtool.start(camera_name, 0, use_background, None, 0.0, None) + laser_on() + wait_camtool_message(number_images) #Wait filing the averager cache + else: image_stats.update() def after_sample(): if multiple_background: - laser_off() + laser_off() r = None if not multiple_background: @@ -76,21 +84,12 @@ try: if mode != "camtool": sensors = [image_stats.com_x_mean, image_stats.com_y_mean, image_stats.com_x_stdev, image_stats.com_y_stdev] else: - #sensors = [CamtoolValue("gr_x_fit_mean"), CamtoolValue("gr_y_fit_mean"), CamtoolValue("gr_x_fit_standard_deviation"), CamtoolValue("gr_y_fit_standard_deviation")] - sensors = [] - for ident in ["gr_x_fit_mean", "gr_y_fit_mean", "gr_x_fit_standard_deviation", "gr_y_fit_standard_deviation"]: - child = s.getChild(ident) - av = create_averager(child, number_images, -1) - av.monitored = True - sensors.append(av) - if plot_image and (mode == "camtool"): - sensors.append(camtool.getDataMatrix()) #sensors.append(CamtoolImage()) + sensors = get_camtool_stats(number_images, good_region=use_good_region) + if plot_image: + sensors.append(camtool.getDataMatrix()) r = lscan(gun_solenoid, sensors , I1, I2, dI, settling_time, before_read = before_sample, after_read = after_sample) finally: - #laser_off() - pass - -#image_stats.stop() + if mode == "camtool": camtool.stop() gun_solenoid.write(original_gun_solenoid) if laser_was_on: diff --git a/script/Alignment/Laser_gun_alignment.py b/script/Alignment/Laser_gun_alignment.py index 8fc5521..e0765fa 100755 --- a/script/Alignment/Laser_gun_alignment.py +++ b/script/Alignment/Laser_gun_alignment.py @@ -12,6 +12,7 @@ import datetime mode = "camtool" # "camtool", "bpm" or "direct" camera_name = "SINEG01-DSCR190" +use_good_region=False if get_exec_pars().source == CommandSource.ui: phi1= 95.0 @@ -38,16 +39,16 @@ print "Parameters: ", phi1, phi2, dphi, settling_time, plot_image, number_images plot_name = datetime.datetime.fromtimestamp(time.time()).strftime('%H%M%S') if mode == "camtool": - if camtool.getCurrentCamera() != camera_name: - camtool.start(camera_name, None, use_background, None, None, None) if use_background: laser_off() if not multiple_background: - camtool.grabBackground(camera_name, 5) + camtool.stop() + camtool.grabBackground(camera_name, number_backgrounds) + camtool.start(camera_name, 0, use_background, None, 0.0, None) else: if mode == "bpm": add_device(BpmStats("image_stats", camera_name), True) - else : + else: add_device(ImageStats(PsiCamera("image_stats", camera_name)), True) multiple_background = False use_background = False @@ -58,10 +59,14 @@ else: # add here gun phase setting see wiki page def before_sample(): - if multiple_background: - camtool.grabBackground(camera_name, 5) - laser_on() - if mode != "camtool": + if mode == "camtool": + if multiple_background: + camtool.stop() + camtool.grabBackground(camera_name, number_backgrounds) + camtool.start(camera_name, 0, use_background, None, 0.0, None) + laser_on() + wait_camtool_message(number_images) #Wait filing the averager cache + else: image_stats.update() def after_sample(): @@ -76,12 +81,12 @@ try: if mode != "camtool": sensors = [image_stats.com_x_mean, image_stats.com_y_mean, image_stats.com_x_stdev, image_stats.com_y_stdev] else: - sensors = [CamtoolValue("gr_x_fit_mean"), CamtoolValue("gr_y_fit_mean"), CamtoolValue("gr_x_fit_standard_deviation"), CamtoolValue("gr_y_fit_standard_deviation")] - if plot_image and (mode == "camtool"): - sensors.append(CamtoolImage()) + sensors = get_camtool_stats(number_images, good_region=use_good_region) + if plot_image: + sensors.append(camtool.getDataMatrix()) r = lscan(gun_phase, sensors , phi1, phi2, dphi, settling_time, before_read = before_sample, after_read = after_sample) finally: - pass + if mode == "camtool": camtool.stop() gun_phase.write(original_phase) if laser_was_on: diff --git a/script/RFscan/GunScan.py b/script/RFscan/GunScan.py index f379b61..56d578a 100644 --- a/script/RFscan/GunScan.py +++ b/script/RFscan/GunScan.py @@ -27,9 +27,7 @@ phase.initialize() check_camtool() camtool.start("SINBD01-DSCR010") - -while camtool.stream.getChild("gr_x_fit_mean") == None: - time.sleep(0.1) +wait_camtool_message() x = camtool.stream.getChild("gr_x_fit_mean") dx = camtool.stream.getChild("gr_x_fit_standard_deviation") diff --git a/script/local.py b/script/local.py index 4946a93..7582319 100755 --- a/script/local.py +++ b/script/local.py @@ -227,10 +227,16 @@ class CamtoolImage(ReadableMatrix): def getHeight(self): return camtool.getData().height - -def get_camtool_stats(number_images=1, async = True, interval=-1): +def wait_camtool_message(number_messages = 1, timeout = 10000): + for i in range (number_messages): + if not camtool.stream.waitCacheChange(timeout): raise Exception("Timeout receiving from Camtool") + +def get_camtool_stats(number_images=1, async = True, interval=-1, good_region = False): + #return [CamtoolValue("gr_x_fit_mean"), CamtoolValue("gr_y_fit_mean"), CamtoolValue("gr_x_fit_standard_deviation"), CamtoolValue("gr_y_fit_standard_deviation")] ret = [] - for ident in ["gr_x_fit_mean", "gr_y_fit_mean", "gr_x_fit_standard_deviation", "gr_y_fit_standard_deviation"]: + wait_camtool_message() + prefix = "gr_" if good_region else "" + for ident in [prefix+"x_fit_mean", prefix+"y_fit_mean", prefix+"x_fit_standard_deviation", prefix+"y_fit_standard_deviation"]: child = camtool.stream.getChild(ident) av = create_averager(child, number_images, interval) av.monitored = async @@ -262,8 +268,9 @@ def add_convex_hull_plot(title, x,y, name=None, clear = False, x_range = None, y s = p.getSeries(name) s.setLinesVisible(False) s.setPointSize(3) - s.setData(to_array(x,'d') , to_array(y,'d')) - + x, y = to_array(x,'d') , to_array(y,'d') + s.setData(x, y) + #Convex Hull #In the first time the plot shows, it takes some time for the color to be assigned timeout = 0 diff --git a/script/test/CamtoolCheckPort.py b/script/test/CamtoolCheckPort.py new file mode 100644 index 0000000..ea31e6a --- /dev/null +++ b/script/test/CamtoolCheckPort.py @@ -0,0 +1,19 @@ +import socket; + +url = camtool.getInstance(camtool.getInstances()[0])["stream"] + +srv = url[6:url.rfind(":")] +port = int(url[url.rfind(":")+1:]) + +sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +result = sock.connect_ex((srv,port)) + +if result == 0: + print "Port is open" +elif result == 106: + print "Port is connected" +elif result == 111: + print "Port is not open" +elif result == 111: + print "Error " + result +sock.close() \ No newline at end of file diff --git a/script/test/TestAlignmentScan.py b/script/test/TestAlignmentScan.py new file mode 100644 index 0000000..91205f8 --- /dev/null +++ b/script/test/TestAlignmentScan.py @@ -0,0 +1,127 @@ +# Tool to align the solenoid on the gun. +# S. Bettoni, A. Gobbo, D. Voulot +# 06/06/2016 +#Code ok with the GUI + +# Procedure: + # switch off all the magnets between the gun solenoid and the screen or BPM used for the measurement + # change the current of the gun soleoid + # look at the centroid position (BPM or screen) downstream of the gun. + +import datetime + +mode = "camtool" # "camtool", "bpm" or "direct" +camera_name = "simulation" +#camera_name = "SLG-LCAM-C041" +use_good_region=False + +if True: #get_exec_pars().source == CommandSource.ui: + I1 = 95.0 + I2 = 100.0 + dI = 2.5 + settling_time = 0.1 + plot_image = False + number_images = 3 + use_background = False + multiple_background = False + number_backgrounds = 3 + do_elog = False + centroid_excursion_plot = True +else: + centroid_excursion_plot = False + +check_camtool() + +#laser_was_on = is_laser_on() +positioner = DummyPositioner("positioner") + +original_gun_solenoid = positioner.read() + + +multiple_background = multiple_background and use_background +print "Parameters: ", I1, I2, dI, settling_time, plot_image, number_images, use_background, multiple_background, number_backgrounds + +plot_name = datetime.datetime.fromtimestamp(time.time()).strftime('%H%M%S') + +if mode == "camtool": + if use_background: + #laser_off() + if not multiple_background: + camtool.stop() + camtool.grabBackground(camera_name, number_backgrounds) + #camtool.stop() + #camtool.startPipeline(camera_name, 0, use_background, None, 0.0, None) + #camtool.startReceiver(); + camtool.start(camera_name, 0, use_background, None, 0.0, None) + if camtool.value is not None: print "Started " , camtool.value.getTimestamp() + wait_camtool_message() + print "OK" +else: + if mode == "bpm": + add_device(BpmStats("image_stats", camera_name), True) + else: + add_device(ImageStats(PsiCamera("image_stats", camera_name)), True) + multiple_background = False + use_background = False + image_stats.setNumberOfImages(max(number_images,1)) + +#switch_off_magnets() + +# add here gun phase setting see wiki page +def before_sample(): + if mode == "camtool": + if multiple_background: + camtool.stop() + camtool.grabBackground(camera_name, number_backgrounds) + camtool.start(camera_name, 0, use_background, None, 0.0, None) + #laser_on() + wait_camtool_message(number_images) #Wait filing the averager cache + else: + image_stats.update() + +def after_sample(): + if multiple_background: + #laser_off() + pass + +r = None +#if not multiple_background: +# laser_on() + +try: + if mode == "camtool": + print "Getting sensors" + sensors = get_camtool_stats(number_images, good_region=use_good_region) + print "Sensors ok" + if plot_image: + sensors.append(camtool.getDataMatrix()) + else: + sensors = [image_stats.com_x_mean, image_stats.com_y_mean, image_stats.com_x_stdev, image_stats.com_y_stdev] + r = lscan(positioner, sensors , I1, I2, dI, settling_time, before_read = before_sample, after_read = after_sample) +finally: + if mode == "camtool": camtool.stop() + #pass + +positioner.write(original_gun_solenoid) +#if laser_was_on: +# laser_on() +#else: +# laser_off() + + +# take the result of the scan and generate convex hull plot +if centroid_excursion_plot: + (hx,hy)=add_convex_hull_plot ("Centroid excursion", r.getReadable(0),r.getReadable(1), plot_name) +else: + (hx,hy)= convex_hull(x=r.getReadable(0), y=r.getReadable(1)) + hx.append(hx[0]); hy.append(hy[0]) + +# save the entry in the logbook +gsa_log_msg = "Data file: " + get_exec_pars().path +gsa_log_msg = gsa_log_msg + "\nImages: " + str(number_images) +gsa_log_msg = gsa_log_msg + "\nBackground: enabled=" + str(use_background) + " multiple=" + str(multiple_background) + " number=" + str(number_backgrounds) +gsa_log_msg = gsa_log_msg + "\n\n" + r.print() +if do_elog: + elog("Gun solenoid current scan", gsa_log_msg , get_plot_snapshots()) + +set_return([r, hx, hy])