Skip to content

Commit

Permalink
Merge 3be11f6 into 9c167d4
Browse files Browse the repository at this point in the history
  • Loading branch information
trappitsch committed Mar 15, 2022
2 parents 9c167d4 + 3be11f6 commit b2eb740
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
68 changes: 68 additions & 0 deletions instruments/tests/test_thorlabs/test_thorlabs_apt.py
Expand Up @@ -1174,6 +1174,74 @@ def test_apt_mc_position(init_kdc101):
)


def test_apt_mc_backlash_correction(init_kdc101):
"""Get / set backlash correction."""
with expected_protocol(
ik.thorlabs.APTMotorController,
[
init_kdc101[0],
ThorLabsPacket(
message_id=ThorLabsCommands.MOT_REQ_GENMOVEPARAMS,
param1=0x01,
param2=0x00,
dest=0x50,
source=0x01,
data=None,
).pack(),
ThorLabsPacket(
message_id=ThorLabsCommands.MOT_SET_GENMOVEPARAMS,
param1=None,
param2=None,
dest=0x50,
source=0x01,
data=struct.pack("<Hl", 0x01, 1000),
).pack(),
ThorLabsPacket(
message_id=ThorLabsCommands.MOT_SET_GENMOVEPARAMS,
param1=None,
param2=None,
dest=0x50,
source=0x01,
data=struct.pack("<Hl", 0x01, 1000),
).pack(),
ThorLabsPacket(
message_id=ThorLabsCommands.MOT_SET_GENMOVEPARAMS,
param1=None,
param2=None,
dest=0x50,
source=0x01,
data=struct.pack("<Hl", 0x01, 1919),
).pack(),
],
[
init_kdc101[1],
ThorLabsPacket(
message_id=ThorLabsCommands.MOT_GET_GENMOVEPARAMS,
param1=None,
param2=None,
dest=0x50,
source=0x01,
data=struct.pack("<Hl", 0x01, -20000),
).pack(),
],
sep="",
) as apt:
assert (
apt.channel[0].backlash_correction
== u.Quantity(-20000, "counts") / apt.channel[0].scale_factors[0]
)
apt.channel[0].backlash_correction = 1000
apt.channel[0].backlash_correction = 1000 * u.counts

# unitful backlash correction
apt.channel[0].motor_model = "PRM1-Z8"
apt.channel[0].backlash_correction = 1 * u.deg

# bad units
with pytest.raises(ValueError):
apt.channel[0].backlash_correction = 10 * u.mm


def test_apt_mc_position_encoder(init_kdc101):
"""Get unitful position of encoder, in counts."""
with expected_protocol(
Expand Down
51 changes: 51 additions & 0 deletions instruments/thorlabs/thorlabsapt.py
Expand Up @@ -1267,6 +1267,57 @@ def motor_model(self, newval):

# MOTOR COMMANDS #

@property
def backlash_correction(self):
"""Get the backlash correction, if stage is defined, unitful.
:return: Unitful quantity of backlash correction.
"""
pkt = _packets.ThorLabsPacket(
message_id=_cmds.ThorLabsCommands.MOT_REQ_GENMOVEPARAMS,
param1=self._idx_chan,
param2=0x00,
dest=self._apt.destination,
source=0x01,
data=None,
)
response = self._apt.querypacket(
pkt,
expect=_cmds.ThorLabsCommands.MOT_GET_GENMOVEPARAMS,
expect_data_len=6,
)
# chan, pos
_, pos = struct.unpack("<Hl", response.data)
return u.Quantity(pos, "counts") / self.scale_factors[0]

@backlash_correction.setter
def backlash_correction(self, pos):
if not isinstance(pos, u.Quantity):
pos_ec = int(pos)
else:
if pos.units == u.counts:
pos_ec = int(pos.magnitude)
else:
scaled_pos = pos * self.scale_factors[0]
# Force a unit error.
try:
pos_ec = int(scaled_pos.to(u.counts).magnitude)
except:
raise ValueError(
"Provided units are not compatible "
"with current motor scale factor."
)
# create package to send
pkt = _packets.ThorLabsPacket(
message_id=_cmds.ThorLabsCommands.MOT_SET_GENMOVEPARAMS,
param1=None,
param2=None,
dest=self._apt.destination,
source=0x01,
data=struct.pack("<Hl", self._idx_chan, pos_ec),
)
self._apt.sendpacket(pkt)

@property
def status_bits(self):
"""
Expand Down

0 comments on commit b2eb740

Please sign in to comment.