> For the complete documentation index, see [llms.txt](https://bakingtray.swcmicroscopy.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bakingtray.swcmicroscopy.com/getting-started/hardware-setup/motor-setup/pi-stage-setup.md).

# PI stage setup

We have had excellent success with PI linear stages for tile scanning; they have outstanding long-term reliability over many years, accumulating millions of tile motions. Most BrainSaw systems run on [PI V-551.4B and V-551.2B direct-drive stages](https://www.physikinstrumente.co.uk/en/products/linear-stages/stages-with-magnetic-direct-drive-linear-motor/v-551-precision-linear-stage-1200204), driven by [C-891 controllers](https://www.physikinstrumente.co.uk/en/products/controllers-and-drivers/motion-controllers-drivers-for-linear-torque-stepper-dc-servo-motors/c-891130300-pimag-motion-controller-for-magnetic-direct-drives-412418462) (BakingTray expects one controller per stage). We have also had good results with the legacy PI M-531.DD and M-605.2DD stages [that came with TissueVision's TissueCyte 1000](https://bakingtray.swcmicroscopy.com/getting-started/tissuecyte_upgrade.md).

## First steps

Before proceeding it is worth checking that your new PI stages move as expected over the travel ranges we care about. You will use PI's [MikroMove](https://www.physikinstrumente.co.uk/en/products/software-suite/motion-control-software) software that came with your stages to check this. Here we assume you have already installed PI's software, set up the stages as a coupled X/Y pair, and hooked up all the cables.

### Connect to the stages in MikroMove

Close BakingTray. Fire up MikroMove and connect the stages such that they appear in the software.

![Two V-551 stages connected in PI MikroMove.](https://3832012848-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAVhQ1tbEzz1UZd9NDb%2Fuploads%2Fgit-blob-c4d17a1bcc4bbaa4885a94eedea4bfabf4f92bbb%2F01_Connected_Stages.png?alt=media)

### Record your settings

First of all take a record of the factory settings of your stages. Right-click on each axis, select "Show Expanded Single-Axis Window" and screen-shot all the settings. Save these for future reference.

![The expanded single axis window for a V-551 stage.](https://3832012848-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAVhQ1tbEzz1UZd9NDb%2Fuploads%2Fgit-blob-993969b383fea62c9d946144cb09ff7b54070007%2F02_Expanded_Single_Axis_Window.png?alt=media)

### Check the step response

Load the stages with the water bath and fire up the data recorder for each axis: click the axis name in the menu and select "Show data recorder". Then click "Configure Curves" near the bottom left. This brings up a window that allows you to select how the data are plotted to screen. The following is a useful configuration: show the actual and target position on the left axis and the position error (the difference between the two) on the right axis:

![The Configure Curves window in MikroMove.](https://3832012848-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAVhQ1tbEzz1UZd9NDb%2Fuploads%2Fgit-blob-476c73528fe811fca2d53d30319e26e75276871f%2F03_Configure_Curves.png?alt=media)

Press "OK" and initiate a step of the size you expect to make during tile-scanning. Here we are making a 1.1 mm step. Our stage achieves this in about 125 ms. You may need to adjust the number of recorded data points or sampling rate (bottom left). In the following case the data duration was reduced from 512 ms to 256 ms.

![The actual and target position of the stage is shown by the orange and blue lines. The magenta line shows the position error. Its axes are on the left hand side and are in mm. At 120 ms the stage is within about 2 microns of the target position.](https://3832012848-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAVhQ1tbEzz1UZd9NDb%2Fuploads%2Fgit-blob-277a269f76a94ff7fd1e2cb7ad6fbdc927446d1a%2F04_Step_Response.png?alt=media)

The goal is to have the stage settle in under about 150 ms with a target velocity of about 25 mm/s (above we used 35 mm/s); the exact number is not very important as the stages will not achieve it over such a short distance. If your stage is settling much later or shows evidence of oscillation, you may need to tweak the PID loops (below) or reduce the acceleration and/or deceleration settings.

## Tuning the PID loops

The parameters for tuning the PID loops can be accessed via the expanded single axis window. The direct-drive stages have separate loops for position and velocity. Try changing the position terms and enable the display of the startup parameters. This will make going back to the original values easier.

To tweak the PID loop:

* Set the I and D terms to zero. Set P to a low number (e.g. 10). Perform a step of the size and speed typical of tile scanning.
* Keep doubling P until you see oscillations, then halve it.
* The I term is used to remove error that has accumulated over time to create an offset between the target and actual positions. Likely it will need to be some small positive number. Set I to some small positive number such as 0.1. If there is a consistent error between the target and true position, double it. Re-test and keep doubling until the error has gone.
* Tweak D if needed, but otherwise leave at zero.
* [More info here](https://www.thorlabs.com/driver-pid-settings?tabName=PID%20Tutorial).

### Other checks

Ensure that the stages are also able to perform larger motions (20 or 30 mm) without issue. We won't be making large fast motions regularly during imaging, but they might take place during sample set up and we want to ensure there is no unexpected behavior.

Once you have done the above for both stages, check all is good by imaging pollen grains. Does the image settle almost right away after the stage motion?

### Troubleshooting

We have had a couple of microscopes where the stages report they have settled, but the image continues to oscillate for a few hundred ms and this is visible in the images. In one case, the problem was somehow related to the PIFOC and somehow went away after firmware updating the controller. In the other case the problem was associated only with the Y stage, so the dominant tiling axis (the one doing most motions) was moved to X. The problem thereby went away. In general, vibrations of this sort can be decreased by accelerating and decelerating more slowly.

The "OTG" light on the C-891 controller indicates "on target". Tile-scanning in BakingTray depends on the controller returning that the stage has reached its target position. If something is preventing that, the OTG light will not illuminate. If that happens during tile-scanning, then the acquisition stalls. Debugging should concentrate on figuring out what is blocking the stage from achieving its target position.

### Soft limits

Once all steps are complete and the microscope is assembled, you can set the soft limits (min and max positions) for the X and Y axes so that even at the firmware level, stages can't accept out of bounds motion commands that would cause hardware damage. BakingTray has its own soft-limits, though, so this step is optional.

## Hints and tips

* Ensure that the stage really is settling as expected. e.g. If you are planning on moving in 1 mm steps and settling well within, say, 200 ms then make sure that you can successfully issue 1 mm motion steps every 200 ms. Ensure that every so often the stages aren't taking much longer to settle. This can happen and it will really slow down your acquisitions. Tweak PID parameters until this is no longer a problem.
