def measure_txy(n, start_time, camera, templ8): """Measure position n times and return a t,x,y array.""" pos = np.zeros((3, samples)) #create some variables for j in range(samples): frame = get_numpy_image(camera, True) pos[1:, j] = find_template(templ8, frame) #measures the initial position pos[0, j] = time.time() - start_time return pos
def measure_txy( n, start_time, camera, templ8 ): #everything used in a definition should be put in as an argument """Measure position n times and return a t,x,y array.""" result = np.zeros((3, n)) #creates an empty array of zeros for i in range(n): frame = get_numpy_image(camera, True) #get frame result[1:, i] = find_template(templ8, frame) #measures position result[0, i] = time.time() - start_time #measures time return result
640, 480)) as camera, OpenFlexureStage('/dev/ttyUSB0') as stage: #authorises the program to acces the twitter account auth = tweepy.OAuthHandler(twitter_keys.consumer_key, twitter_keys.consumer_secret) auth.set_access_token(twitter_keys.access_token, twitter_keys.access_token_secret) api = tweepy.API(auth) N_frames = 100 counter = 0 #loads camera from picamera and set the stage as the arduino, lower resolution can speed up the programme ms = microscope.Microscope(camera, stage) ms.freeze_camera_settings() frame = get_numpy_image(camera, True) df = data_file.Datafile(filename="drift_81217.hdf5" ) #creates the .hdf5 file to save the data data_gr = df.new_group( "test_data", "A file of data collected to test this code works") #puts the data file into a group with description image = get_numpy_image(camera, greyscale=True) #takes template photo templ8 = image[100:-100, 100:-100] #crops image loop = True tweet = True start_t = time.time() #measure starting time
from contextlib import closing if __name__ == "__main__": with picamera.PiCamera(resolution = (640, 480)) as camera, \ OpenFlexureStage('/dev/ttyUSB0') as stage, \ closing(data_file.Datafile(filename = "orthogonality_15123.hdf5")) as df: side_length = 4000 points = 10 stage.backlash = 256 camera.start_preview() image = get_numpy_image(camera, greyscale=True).astype(np.float32) background = cv2.GaussianBlur(image, (41, 41), 0) templ8 = (image - background)[100:-100, 100:-100] stage_centre = stage.position data_stage = df.new_group( "stage position", "orthogonality measurments, moves in a square") data_cam = df.new_group( "camera position", "orthogonality measurments, moves in a square") stage_pos = np.zeros((2 * points, 3)) cam_pos = np.zeros((2 * points, 2)) stage.move_rel( np.array([-1, -1, 0]) * side_length / 2 - stage.backlash)
stage.backlash = 0 #increased backlash correction value to get more accurate data move = 5000 #maximum move distance without the templete travelling off field of view of the lens #larger move distance can reduce error stage_position = stage.position df = data_file.Datafile(filename="step_size.hdf5") data_step = df.new_group("step size", "step size measurments") data = np.zeros((2, 50)) camera.start_preview() image = get_numpy_image(camera, greyscale=True) templ8 = image[100:-100, 100:-100] stage.move_rel([(-move), 0, 0]) frame = get_numpy_image(camera, True) spot_coord = find_template(templ8, frame) x = spot_coord[0] y = spot_coord[1] for i in range(data.shape[1]): stage.move_rel([(2 * move / data.shape[1]), 0, 0]) #a range of move distances to see how step size changes as the move distance increases time.sleep(1) #sleeps before frame saves to make sure the right image is saved frame = get_numpy_image(camera, True)
stage.backlash = 248 #corrects for backlash side_length = 500 #defines distance moved for each side of the sqaure camera.start_preview() #shows preview of camera stage_centre = stage.position #saves the starting position of stage stage.move_rel( [side_length / 2, side_length / 2, 0]) #moves the stage to the 'top left' corner of the square top_left = stage.position try: for step in [ 1, 10, 25 ]: #runs for the given number of steps per side of the square start_time = time.time() image = get_numpy_image(camera, greyscale=True) #gets template templ8 = image[100:-100, 100:-100] #crops image data = sqre_move(step, side_length, start_time, camera, templ8) sq_group = df.new_group("step_%d" % step) df.add_data(data, sq_group, "Square with %d steps each side" % step) #writes data to .hdf5 file stage.move_abs(top_left) finally: stage.move_abs( stage_centre) #reuturns stage to centre once program has run