Newer
Older
TestStandRepository / Software / AutomatedMeasurements / ElectronicMonkeyFocusing.py
'''

Author: Federica Lionetto
Date: January 5th, 2015

Description:
Automated measurement for focusing. After the measurement, the position of the laser along x and z is reset.

Parameters:
- <firstx>, the first x position;
- <lastx>, the last x position;
- <stepx>, the step size along x;
- <firstz>, the first z position;
- <lastz>, the last z position;
- <stepz>, the step size along z.

How to run it:
python ElectronicMonkeyFocusing.py --fx <firstx> --lx <lastx> --sx <stepx> --fz <firstz> --lz <lastz> --sz <stepz> --date <yyyymmdd>

For example:
python ElectronicMonkeyFocusing.py --fx 0 --lx 50 --sx 2 --fz -400 --lz -200 --sz 20 --date 20150106

Arduino device ID:
DEVICE ID 2341:0042 on Bus 001 Address 045, Communications Device (in hexadecimal)

To get some information on the device:
lsusb -v -d 2341:0042

'''

import argparse
import subprocess
import os

import serial

import time

parser = argparse.ArgumentParser(description='Define the parameters of the automated measurement for focusing.')

parser.add_argument('--fx',type=int,help='first x position')
parser.add_argument('--lx',type=int,help='last x position')
parser.add_argument('--sx',type=int,help='step size along x')

parser.add_argument('--fz',type=int,help='first z position')
parser.add_argument('--lz',type=int,help='last z position')
parser.add_argument('--sz',type=int,help='step size along z')

parser.add_argument('--date',type=int,help='date according to the yyyymmdd format')

args = parser.parse_args()

# Parameters and configuration.
firstx = args.fx
lastx = args.lx
stepx = args.sx

firstz = args.fz
lastz = args.lz
stepz = args.sz

yyyymmdd = args.date

nevents = 10000

folder = '/home/hep/flionett/TestStand/Data'
filename = ''
config = '/home/hep/flionett/TestStand/Repository/TestStandRepository/Software/AutomatedMeasurements/FocusingConfig'

print 'First x position: %i steps' % firstx
print 'Last x position: %i steps' % lastx
print 'Step size along x: %i steps' % stepx

print 'First z position: %i steps' % firstz
print 'Last z position: %i steps' % lastz
print 'Step size along z: %i steps' % stepz

print 'Number of events per DAQ: %i' % nevents

# Number of scanned x positions.
stepsx = (lastx-firstx)/stepx+1
print 'Number of scanned x positions: %i' % stepsx
# Number of scanned z positions.
stepsz = (lastz-firstz)/stepz+1
print 'Number of scanned z positions: %i' % stepsz

# Find Arduino.
ser = serial.Serial('/dev/ttyACM0',9600)

# Perform the measurement.
for posz in range(firstz,lastz+stepz,stepz) :
    for posx in range(firstx,lastx+stepx,stepx) :
        print 'Position along x: %i steps' % posx
        print 'Position along z: %i steps' % posz
        filename = folder+"/Hans410/Test/"+str(yyyymmdd)+"-216-"+str(posx)+"x-"+str(posz)+"z-las.ali"
        printcommand = "echo alibava-gui --no-gui --nevts="+str(nevents)+" --out="+filename+" --laser "+config
        command = "alibava-gui --no-gui --nevts="+str(nevents)+" --out="+filename+" --laser "+config
        print '***'
        subprocess.call(printcommand,shell=True)
        subprocess.call(command,shell=True)
        # Move the stepper motor along x.
        # Send +10 (forward step of 5 microns) stepx times.
        if (posx < lastx) :
            for counter in range(stepx) :
                print '+10 %i' % counter
                ser.write('+10')
                time.sleep(2)
    # Reset the position of the stepper motor along x, to make it ready for the DAQs at the following z position.
    # Send +20 (backward step of 5 microns) stepx*len(range(firstx,lastx,stepx)) times.
    for counter in range(stepx*len(range(firstx,lastx,stepx))) :
        print '+20 %i' % counter
        ser.write('+20')
        time.sleep(2)
    # Move the stepper motor along z.
    # Send +01 (backward step of 5 microns) stepz times.
    if (posz < lastz) :
        for counter in range(stepz) :
            print '+01 %i' % counter
            ser.write('+01')
            time.sleep(2)
# Reset the position of the stepper motor along z, to make it ready for the following measurement.
# Send +02 (forward step of 5 microns) stepz*len(range(firstz,lastz,stepz)) times.
for counter in range(stepz*len(range(firstz,lastz,stepz))) :
    print '+02 %i' % counter
    ser.write('+02')
    time.sleep(2)
print '***'

print 'End of the measurement!'