@@ -79,11 +79,12 @@ def _callback_motor_status(self, data):
7979 try :
8080 nSteppers = len (data ["steppers" ])
8181 stepSizes = np .array ((self .stepSizeA , self .stepSizeX , self .stepSizeY , self .stepSizeZ ))
82- offSets = np .array ((self .offsetA , self .offsetX , self .offsetY , self .offsetZ ))
82+ _physicalStepSizes = np .array ((self .stepSizeA , self .stepSizeX , self .stepSizeY , self .stepSizeZ ))
83+ _physicalOffsets = np .array ((self .offsetA , self .offsetX , self .offsetY , self .offsetZ ))
8384 for iMotor in range (nSteppers ):
8485 stepperID = data ["steppers" ][iMotor ]["stepperid" ]
85- # FIXME: smart to re-update this variable? Will be updated by motor-sender too
86- self .currentPosition [stepperID ] = data ["steppers" ][iMotor ]["position" ] * stepSizes [stepperID ] - offSets [stepperID ]
86+ # Hardware returns steps, convert to physical units: (steps * stepSize - offset)
87+ self .currentPosition [stepperID ] = data ["steppers" ][iMotor ]["position" ] * stepSizes [stepperID ] - _physicalOffsets [stepperID ]
8788 if callable (self ._callbackPerKey [0 ]):
8889 self ._callbackPerKey [0 ](self .currentPosition ) # we call the function with the value
8990 except Exception as e :
@@ -438,6 +439,9 @@ def move_stepper(self, steps=(0,0,0,0), speed=(1000,1000,1000,1000), is_absolute
438439 if isAbsoluteArray [3 ]:
439440 steps [3 ] += self .offsetZ
440441
442+ # Store the target position in physical units BEFORE conversion to hardware steps
443+ targetPositionPhysical = steps .copy ()
444+
441445 # convert from physical units to steps
442446 steps [0 ] *= 1 / self .stepSizeA
443447 steps [1 ] *= 1 / self .stepSizeX
@@ -449,11 +453,12 @@ def move_stepper(self, steps=(0,0,0,0), speed=(1000,1000,1000,1000), is_absolute
449453 for iMotor in range (4 ):
450454 # for absolute motion:
451455 if isAbsoluteArray [iMotor ]:
452- self .currentDirection [iMotor ] = 1 if (self .currentPosition [iMotor ] > steps [iMotor ]) else - 1
456+ # Compare current position (physical) with target (physical, already includes offset)
457+ self .currentDirection [iMotor ] = 1 if (self .currentPosition [iMotor ] > targetPositionPhysical [iMotor ]) else - 1
453458 else :
454459 self .currentDirection [iMotor ] = np .sign (steps [iMotor ])
455460 if self .lastDirection [iMotor ] != self .currentDirection [iMotor ]:
456- # we want to overshoot a bit
461+ # we want to overshoot a bit (backlash is in steps, so apply before division)
457462 steps [iMotor ] = steps [iMotor ] + self .currentDirection [iMotor ]* self .backlash [iMotor ]
458463
459464
@@ -491,8 +496,9 @@ def move_stepper(self, steps=(0,0,0,0), speed=(1000,1000,1000,1000), is_absolute
491496 for iMotor in range (4 ):
492497 if isAbsoluteArray [iMotor ] or abs (steps [iMotor ])> 0 :
493498 # if we are absolute and the last target position is the same as the current one, we don't need to move
494- if isAbsoluteArray [iMotor ] and abs (steps [iMotor ] - self .currentPosition [iMotor ])< 1 :
495- if self ._parent .serial .DEBUG : self ._parent .logger .debug (f"Motor { iMotor } is already at target position { steps [iMotor ]} " )
499+ # Compare in physical units: targetPositionPhysical vs currentPosition
500+ if isAbsoluteArray [iMotor ] and abs (targetPositionPhysical [iMotor ] - self .currentPosition [iMotor ])< 1 :
501+ if self ._parent .serial .DEBUG : self ._parent .logger .debug (f"Motor { iMotor } is already at target position { targetPositionPhysical [iMotor ]} " )
496502 continue
497503 motorProp = { "stepperid" : int (self .motorAxisOrder [iMotor ]),
498504 "position" : int (steps [iMotor ]),
@@ -516,14 +522,18 @@ def move_stepper(self, steps=(0,0,0,0), speed=(1000,1000,1000,1000), is_absolute
516522 "steppers" : motorPropList
517523 }
518524 }
519- # safe steps to track direction for backlash compensatio
520- _physicalStepSizes = np .array ((self .stepSizeA , self .stepSizeX , self .stepSizeY , self .stepSizeZ ))
521- _physicalOffsets = np .array ((self .offsetA , self .offsetX , self .offsetY , self .offsetZ ))
525+
526+ # Update currentPosition to track expected position in physical units
527+ # steps are now in hardware units, so we need to convert back to physical
528+ stepSizes = np .array ((self .stepSizeA , self .stepSizeX , self .stepSizeY , self .stepSizeZ ))
529+ offSets = np .array ((self .offsetA , self .offsetX , self .offsetY , self .offsetZ ))
522530 for iMotor in range (self .nMotors ):
523531 if isAbsoluteArray [iMotor ]:
524- self .currentPosition [iMotor ] = steps [iMotor ] * _physicalStepSizes [iMotor ] - _physicalOffsets [iMotor ]
532+ # For absolute: convert hardware steps back to physical units: (steps * stepSize - offset)
533+ self .currentPosition [iMotor ] = steps [iMotor ] * stepSizes [iMotor ] - offSets [iMotor ]
525534 else :
526- self .currentPosition [iMotor ] = self .currentPosition [iMotor ] + steps [iMotor ] * _physicalStepSizes [iMotor ]
535+ # For relative: convert step delta to physical delta and add to current position
536+ self .currentPosition [iMotor ] = self .currentPosition [iMotor ] + (steps [iMotor ] * stepSizes [iMotor ])
527537
528538 # drive motor
529539 self .isRunning = True
0 commit comments