Skip to content

Commit 2da99ef

Browse files
committed
Merge branch 'master' into mergemaster
2 parents e0ebdd6 + ed34f8b commit 2da99ef

2 files changed

Lines changed: 27 additions & 17 deletions

File tree

uc2rest/motor.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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

uc2rest/mserial.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,9 @@ def tryToConnect(self, port):
219219
def _write(self, serialdevice, payload):
220220
if type(payload) == dict:
221221
payload = json.dumps(payload)
222-
serialdevice.write(payload.encode('utf-8'))
222+
with self.serialLock:
223+
serialdevice.write(payload.encode('utf-8'))
224+
time.sleep(0.02)
223225
if self.cmdWriteCallBackFct is not None:
224226
self.cmdWriteCallBackFct(payload)
225227

@@ -275,8 +277,7 @@ def checkFirmware(self, ser):
275277

276278
def _generate_identifier(self):
277279
self.identifier_counter += 1
278-
# get modulo 512 to avoid too large numbers
279-
return self.identifier_counter % 512
280+
return self.identifier_counter
280281

281282
def _enqueue_command(self, json_command):
282283
"""Add a command to the queue."""
@@ -466,8 +467,7 @@ def sendMessage(self, command, nResponses=1, timeout = 20):
466467
json_command = json.dumps(command)+"\n"
467468
#self.serialdevice.flush()
468469
if self.DEBUG and json_command!="": self._logger.debug("[SendingCommands]:"+str(json_command))
469-
with self.serialLock:
470-
self._write(self.serialdevice, json_command)
470+
self._write(self.serialdevice, json_command)
471471
#time.sleep(1)
472472
# we have to queue the commands and give it some time to process
473473
#self._enqueue_command(json_command)

0 commit comments

Comments
 (0)