Updating a Position List

Hello,

I developed this script which works fine in MM 2.0 using a Zeiss microscope. Now, I’m trying to use in MM 2.0 for a Nikon Ti microscope and it doesn’t work. Are there Beanshell commands that only work for specific microscope brand? The goal of the script is to update a defined position list on the Z-axis before I click “Acquire” on the MDA. I called Autofocus on the first position, 10th, 20th, etc from my position list, calculate an offset and apply it to the 1-9 positions, 11-19 positions, 21-29, etc. Is there a different way to update a position list?
Thanks

posList = mm.positions().getPositionList();
list = posList.getPositions();
index = posList.getNumberOfPositions();

for (i = 0; i<index; i++) {
  if (i%10 == 0) {
    posi = posList.getPosition(i);
    posi.goToPosition(posi,mmc);
    zpos = mmc.getPosition();
    
    AfM=mm.getAutofocusManager();
    AfMM = mm.getAutofocusMethod();
    AfMM.fullFocus();
    mmc.sleep(1000);

    offset=mmc.getPosition()-zpos
    for (x = i; x < i<=i+9;x++) {
       msp=posList.getPosition(i);
       sp=msp.get(zStageFocus);
       mmc.setPosition(zStageFocus, zpos +offset);
     }
       posList.setPositions(list);
       mm.positions().setPositionList(posList);
 }
}

That should work the same with both microscopes (and MM versions). What is the error?

One problem I see with your script is that there is no mm.getAutofocusMethod. Did you mean to write Afm.getAutofocusMethod()?

Thanks for the correction. Yes, it should be AfMM = AfM.getAutofocusMethod()
The script doesn’t display any error, but the position list is not updated. Ideally, the Z-axis values for each of the positions should have changed, since I’m updating the list on the last commands.

Hi Carlos,

I don’t see anything in your script that would result in a change to your position list.

You get the list of positions at the beginning: list = posList.getPositions();

Then you never modify that list.

So calling:

posList.setPositions(list);
mm.positions().setPositionList(posList);

has no noticeable effect.

Hi Nick,

Thanks for the feedback. How would you modify or update the original list? I thought that with those commands, you will set your new Z-axis position on your list.

Hi Carlos,

I haven’t tested this, but here is a modified version of your script that tries to remove the unused variables and statements and should actually update the positions.

posList = mm.positions().getPositionList();

for (i = 0; i< posList.getNumberOfPositions(); i++) {
  if (i%10 == 0) {
    posi = posList.getPosition(i);
    posi.goToPosition(posi,mmc);
    
    AfMM=mm.getAutofocusManager().getAutofocusMethod();
    AfMM.fullFocus();
    mmc.sleep(1000);  // Is this necessary?
    newZCoord=mmc.getPosition()

    for (x = i; x < i<=i+9;x++) {
       msp=posList.getPosition(i);
       newPos = new MultiStagePosition(
         msp.getDefaultXYStage(),
         msp.getX(),
         msp.getY(),
         msp.getDefaultZStage(),
         newZCoord);
       posList.replacePosition(x, newPos);
     }
  }
}
mm.positions().setPositionList(posList);