# Raster

This folder contains the experiment code, analysis code and raw data from the experiment to measure average motion per step of the stepper motor and the angle between the _x_ and _z_ axes of the stage by scanning the stage over a 2D square grid.


This experiment was performed using a [Raspberry Pi 3 model B+](https://www.raspberrypi.org/products/raspberry-pi-3-model-b-plus/). Motion of the stage was monitored using a [Raspberry Pi camera module v2](https://www.raspberrypi.org/products/camera-module-v2/) by tracking a [R3L1S4P positive 1951 USAF wheel pattern test target](https://www.thorlabs.com/thorproduct.cfm?partnumber=R3L1S4P). Motion of the stage was also monitored by counting the number of steps moved by the three [micro-geared 28BYJ-48 stepper motors](https://build.openflexure.org/openflexure-microscope/v6.0.0/docs/#/parts/electronics/stepper_motors) used to control the motion of the stage through an Arduino. We removed the original lens that came with the Raspberry Pi camera module and used an [AC127-050-A lens](https://www.thorlabs.com/thorproduct.cfm?partnumber=AC127-050-A) instead. A 4 times objective lens was used for magnification and the USAF test target was illuminated by a white LED. Light from the LED was diffused by some white tape.

The 3D printed accessories required by the setup can be found in the 'Accessories' folder. The AC127-050-A lens and the camera module are assembled using the 'microscope_module_25mm.stl' lens tube. The camera module with exposed sensor is attached to one end of the camera tube using two M2 screws and the lens is push fitted to the other end of the camera tube with the more convex side facing out. The camera tube and the objective lens are assembled using the 'rms_to_stage_25mm.stl' part. The lens tube is attached to the non-threaded end of the part using three M3 screws and the objective lens is screwed into the threaded end of the part. We then mounted this entire component on one stationary platform of the stage using two M3 screws, two M3 washers, and two M3 cleats. The USAF test target is held on the moving platform of the stage using two 'vertical_sample_clips.stl' parts and two M3 screws. The LED is held on the other fixed platform of the stage using the 'LED_to_microblock' part mounted with two M3 screws. The 'diffuser.scad' part was mounted in front of the LED using two M3 screws, this part has some white tape stuck on it to diffuse the white LED light.

This experiment is performed using the 'raster.py' script inside folder 'Experiment'. This code script contains two raster scanning methods, standard raster and snake raster. Both raster scanning methods takes as input the scanning range along _x_ and _z_ in steps, and the movement distance between measurements in steps. Each set of measurement contains three readings. The measurement time in seconds, the position of the stage as measured by the camera in pixels, and the stage position obtained by counting the number of steps moved by the stage. Both raster scanning methods scans the 2D plane row by row, recording a set of measurements every predefined movement distance. The difference is that the standard raster scans all rows in the positive direction, but the snake raster scans neighbouring rows in alternating directions.

Data collected were saved in HDF5 format due to its advantage at handling big datasets of different datatypes. A HDF5 file works like a python dictionary, containing nested groups of datasets. The data can be viewed using [hdfview](https://www.hdfgroup.org/downloads/hdfview/) or packages such as [h5py in Python](http://docs.h5py.org/en/stable/index.html).

The data collected can be found in folder 'Raw_data'. The analysis code used to analyse the collected data can be found in folder 'Analysis'.

The 'raster.hdf5' file inside folder 'Raw_data' contains two data groups, 'raster000' and 'raster001', which includes data collected by performing the raster scan without backlash compensation and with backlash compensation set to 256 steps respectively. Both data groups are further divided into subgroups that contain an initial image, a template image and two raster scan datasets. An initial image of the USAF test target taken at the start of the experiment is saved inside a subgroup named 'sample_image'. A cropped version of this initial image used as the template for position tracking is saved inside a subgroup named 'template_image'. The 'snake_raster000' subgroup contains data collected using the snake raster scanning method and the 'standard_raster000' subgroup contains data collected using the standard raster scanning method. Both 'standard_raster000' and 'snake_raster000' subgroups are further divided into 441 sets of measurements taken at the grid points of a 21-by-21 grid. Each set of measurement includes two datasets named 'cam_position' and 'stage_position'. The 'cam_position' dataset contains a three-element array, where the first array element is measurement time in seconds, the second array element is stage _x_ position as measured by the camera in pixels, and the third array element is stage _z_ position as measured by the camera in pixels. The 'stage_position' dataset contains a three-element of array of the stage position obtained by counting the number of steps moved by the stage, where the first array element is stage _x_ position in steps, the second element is the stage _y_ position in steps and the third element is the stage _z_ position in steps.