# Automated fibre coupling

This folder contains the scripts used to perform automatic fibre alignment with the OpenFlexure Block Stage, the scripts used to perform the analysis, and the raw data.

The automated fibre coupling process is split into two parts, a coarse alignment that searches for the initial signal above a certain power threshold in an expanding square spiral, and a gradient ascent fine alignment that moves the stage to the position where intensity maximises. The setup uses a [HNL050L HeNe laser](https://www.thorlabs.com/thorproduct.cfm?partnumber=HNL050L#ad-image-0) as light source. The laser beam bounces off two [PF10-03-P01-10 protected silver mirrors](https://www.thorlabs.com/thorproduct.cfm?partnumber=PF10-03-P01-10) embedded inside two [KM100 kinematic mirror mounts](https://www.thorlabs.com/thorproduct.cfm?partnumber=KM100#ad-image-0) into a 20 times objective lens mounted on the stationary platform of the stage. The objective lens focuses the laser beam into a single mode fibre ([ThorLabs P1-460B-FC-1](https://www.thorlabs.com/thorproduct.cfm?partnumber=P1-460B-FC-1)) mounted on the moving platform of the stage using a [HFB004 fiber holder](https://www.thorlabs.com/thorproduct.cfm?partnumber=HFB004), two M3 screws, two M3 washers, and two [M3 cleats](https://www.thorlabs.com/thorproduct.cfm?partnumber=AMA010/M).

The power throughput was monitored using a [TSL2591 high dynamic range digital light sensor](https://www.adafruit.com/product/1980), which supports four different programmable analogue gains with 37888 counts at each gain. The four configurable gains are 1, 25, 428, 9876. The relative power measured with the digital light sensor is worked out by dividing the number of counts by the gain. Motion of the stage was 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.

The 3D printed accessories required by the setup can be found in the 'Accessories' folder. The fibre and the digital light sensor are connected using the 'LightSensor.scad' and 'LightSensor_cover.scad' parts. The digital light sensor is embedded inside the connector and pressed in place using the cover piece, which attaches to the main piece using four M3 screws. A fibre holder is embedded in the circular indent on the outside of the connector, and secured in place using four M3 screws and four M3 washers. Furthermore, the objective lens is mounted on the stationary platform of the stage using the 'rms_to_stage_25mm.stl' part with two M3 screws, two M3 washers, and two M3 cleats.

Since datasets collected in this experiment are small with a simple structure, they were saved in DAT format. A DAT file can be easily opened with a text editor such as 'notepad' and loaded into Python using the ['loadtxt' function in the NUMPY package](https://docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html).

The experiment code script used to automate the stage to perform fibre coupling can be found in folder 'Experiment'. 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'.

## Coarse alignment

The '20190925_223232_rasterToLight.dat' file inside folder 'Raw_data' contains data collected from an example coarse alignment run. Since the purpose of this coarse alignment is to find the faint initial signal required by the fine gradient ascent alignment algorithm, the highest analogue gain of the digital light sensor was used. This algorithm works by scanning the stage in an expanding square spiral in the _xz_ plane, taking an intensity measurement every 100 steps. The algorithm stops upon reaching an intensity threshold, at which time the fine gradient ascent alignment algorithm takes over.

The example coarse alignment run was performed with an initial spiral length of 500 steps, and the spiral expands by 100 steps at a time. The intensity threshold was set to 80 counts as measured by the digital light sensor.

The data file contains a N-by-five matrix where the first column is measurement time in seconds, the second column is stage _x_ position in steps, the third column is stage _y_ position in steps, the fourth column is stage _z_ position in steps, and the fourth column is count reading measured at the maximum gain of the light sensor. The first four lines of the file contains some attributes specifying parameters used in this experiment. The first line specifies the gain setting of the light sensor, the second line specifies the initial length of the spiral pattern in steps, the third line specifies the spiral expansion in steps, and the fourth line specifies the count reading threshold at which coarse alignment stops, and fine alignment takes over.

## Gradient ascent alignment

The '20190925_223232_rasterToLight.dat' file inside folder 'Raw_data' contains data collected from an example gradient ascent fine alignment run. The alignment process works by measuring the light intensity at different positions along an axis and fitting a parabola to the intensity measurements. If the fitted parabola has a peak, the stage moves to the peak position and moves on to the next axis. Otherwise, a straight line is fitted and the stage moves up the intensity gradient. The process is then repeated for the same axis until a peak is found. This methodology loops between the _x_, _y_, and _z_ axes, and the distance between intensity measurements is halved after every cycle. Throughout the alignment process, the analogue gain of the digital light sensor is automatically adjusted if the count reading falls below 10 or goes beyond 2<sup>25</sup>. Furthermore, each intensity measurement used for line fitting is an average of 20 repeated measurements, this is to mitigate for power fluctuations in the laser beam.

This example gradient ascent fine alignment run was performed with an initial distance of 400 steps between intensity measurements along _x_ and _z_, and 2000 steps along _y_. The algorithm alternates between the three axes until distance between intensity measurements along any axis falls below 20 steps when halved. The number of intensity measurements for each line fit was set to 9.

The file contains a N-by-seven matrix where the first column is measurement time in seconds, the second column is stage _x_ position in steps, the third column is stage _y_ position in steps, the fourth column is stage _z_ position in steps, the fifth column is light sensor gain setting, the sixth column is the count reading measured at the gain specified by the fifth column, and the seventh column is movement size along the specified axis in steps. The first two lines of the file specifies the number of measurements used to find each intensity peak, and the second line specifies the number of repeated intensity readings taken for each measurement to mitigate for power fluctuations in the laser beam.

## Power sensor calibration

We attempted to calibration the TSL2591 high dynamic range digital light sensor using a [ThorLabs PM160 Si sensor power meter](https://www.thorlabs.com/thorproduct.cfm?partnumber=PM160). The calibration was done by first aligning the optical fibre with the laser beam using our gradient ascent alignment algorithm. We then used 10 different ND filters from [NDK01 box of 10 reflective ND filters](https://www.thorlabs.com/thorproduct.cfm?partnumber=NDK01) to attenuate the laser power by individually placing them directly in front of the laser. The ND filters contained inside this box have optical densities 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 1.0, 2.0, 3.0, 4.0. For each gain of the digital light sensor, we first applied the ND filter with the largest possible optical density that allows us to measure a count reading above the minimum. We then sequentially reduced the filter optical density by using different ND filters, until the count reading saturated. For each gain and ND filter combination, readings measured by both the digital light sensor and the ThorLabs power meter were recorded. This was done by manually switching the output end of the optical fibre between the digital light sensor and the ThorLabs power meter. Before each reading was recorded, a background was measured and subtracted from the reading. The background noise was measured with the laser beam blocked by a flip mirror formed of a [PF10-03-P01-10 protected silver mirror](https://www.thorlabs.com/thorproduct.cfm?partnumber=PF10-03-P01-10) embedded inside a [Newport 9891 flipper optic mount](https://www.newport.com/p/9891). This calibration experiment was repeated three times. The calibration factor obtained is different for each repeat of the experiment because the initial alignment coupled different amounts of light into the fibre each time. Nevertheless, each set of measurements does fit to a straight line.