# Repeatability

This folder contains the experiment code, analysis code and raw data from the experiment to measure repeatability error of the stage after completing bidirectional movements of different radii. The repeatability error is the difference between the final and initial stage positions. 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 characterises the repeatability of the stage using the 'repeat.py' script inside folder 'Experiment'. This algorithm works by performing a series of bidirectional movements of different radii with the stage, while the camera monitors the stage position in the background. For each displacement radius, the algorithm performs multiple bidirectional movements in randomly generated 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 'repeat.hdf5' file inside folder 'Raw_data' contains a data group named 'repeatability000' which includes repeatability error measurements for ten different displacement radii logarithmically spread between 2^9 steps and 2^16 steps saved in ten different groups. Each group is further divided into subgroups that contain an initial image, a template image, and many datasets. An initial image of the USAF test target was taken for each displacement radius and 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 remaining twenty subgroups contain measurements taken from twenty bidirectional movements in randomly generated directions. The subgroup for each individual bidirectional movement contains six datasets: 'init_cam_position' and 'init_stage_position' measured at the origin position; 'moved_cam_position' and 'moved_stage_position' measured at a position a displacement radius away from the origin; as well as 'final_cam_position' and 'final_stage_position' measured at a position after a bidirectional movement is completed. Each 'cam_position' dataset is a three-elements array where the first array element is measurement time in seconds, the second array element is the stage _x_ position as measured by the camera in pixels, and the third element is the stage _z_ position as measured by the camera in pixels. Each 'stage_position' dataset is a three-element array of stage position obtained by counting the number of steps moved by the stage, where the first array elements is the stage _x_ position in steps, the second array elements is the stage _y_ position in steps, and the third array element is the stage _z_ position in steps.