Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements for mercury driver #213

Merged
merged 20 commits into from
May 17, 2024
Merged
62 changes: 58 additions & 4 deletions basil/HL/mercury.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
# ------------------------------------------------------------
#
from basil.HL.RegisterHardwareLayer import HardwareLayer
import logging
import time

Check warning on line 9 in basil/HL/mercury.py

View check run for this annotation

Codecov / codecov/patch

basil/HL/mercury.py#L8-L9

Added lines #L8 - L9 were not covered by tests

logger = logging.getLogger(__name__)

Check warning on line 11 in basil/HL/mercury.py

View check run for this annotation

Codecov / codecov/patch

basil/HL/mercury.py#L11

Added line #L11 was not covered by tests
# logger.setLevel(logging.DEBUG)


class Mercury(HardwareLayer):
Expand All @@ -15,8 +20,8 @@
Therefore '\x03' has to be set a read termination in the transport layer! And a read termination
of 0.1 s should be set!
Despite the manual telling SCPI compatibility, this is not correct for our devices with our
firmware.
'''
firmware. The manual explaining every "native" command: https://twiki.cern.ch/twiki/pub/ILCBDSColl/Phase2Preparations/MercuryNativeCommands_MS176E101.pdf
The overall manual: https://www.le.infn.it/~chiodini/allow_listing/pi/Manuals/C-863_Benutzerhandbuch_Kurzversion_MS205Dqu200.pdf'''

def __init__(self, intf, conf):
super(Mercury, self).__init__(intf, conf)
Expand Down Expand Up @@ -48,6 +53,21 @@
self._write_command("TB", address)
return self.read()

def motor_on(self, address=None):
self._write_command("MN", address)

Check warning on line 57 in basil/HL/mercury.py

View check run for this annotation

Codecov / codecov/patch

basil/HL/mercury.py#L56-L57

Added lines #L56 - L57 were not covered by tests

def motor_off(self, address=None):
self._write_command("MF", address)

Check warning on line 60 in basil/HL/mercury.py

View check run for this annotation

Codecov / codecov/patch

basil/HL/mercury.py#L59-L60

Added lines #L59 - L60 were not covered by tests

def LL(self, address=None): # logic active low
self._write_command("LL", address)

Check warning on line 63 in basil/HL/mercury.py

View check run for this annotation

Codecov / codecov/patch

basil/HL/mercury.py#L62-L63

Added lines #L62 - L63 were not covered by tests

def set_home(self, address=None): # Defines the current position as 0
self._write_command("DH", address)

Check warning on line 66 in basil/HL/mercury.py

View check run for this annotation

Codecov / codecov/patch

basil/HL/mercury.py#L65-L66

Added lines #L65 - L66 were not covered by tests

def go_home(self, address=None): # Moves motor to zero position
self._write_command("GH", address)

Check warning on line 69 in basil/HL/mercury.py

View check run for this annotation

Codecov / codecov/patch

basil/HL/mercury.py#L68-L69

Added lines #L68 - L69 were not covered by tests

def get_position(self, address=None):
self._write_command("TP", address)
return int(self.read()[2:-3])
Expand All @@ -56,8 +76,42 @@
self._write_command("TS", address)
return self.read()

def set_position(self, value, address=None):
def set_position(self, value, precision=100, address=None, wait=False):

Check warning on line 79 in basil/HL/mercury.py

View check run for this annotation

Codecov / codecov/patch

basil/HL/mercury.py#L79

Added line #L79 was not covered by tests
self._write_command("MA%d" % value, address)
if wait is True:
pos = self._wait(address)
if abs(pos - value) <= precision:
logger.debug("At position {pos}, Target at {target}".format(pos=pos, target=value))

Check warning on line 84 in basil/HL/mercury.py

View check run for this annotation

Codecov / codecov/patch

basil/HL/mercury.py#L81-L84

Added lines #L81 - L84 were not covered by tests
else:
logger.warning("Target not reached! Target: {target}, actual position: {pos}, precision: {pre}".format(target=value, pos=pos, pre=precision))

Check warning on line 86 in basil/HL/mercury.py

View check run for this annotation

Codecov / codecov/patch

basil/HL/mercury.py#L86

Added line #L86 was not covered by tests

def move_relative(self, value, address=None):
def move_relative(self, value, precision=100, address=None, wait=False):
target = self.get_position(address=1) + value

Check warning on line 89 in basil/HL/mercury.py

View check run for this annotation

Codecov / codecov/patch

basil/HL/mercury.py#L88-L89

Added lines #L88 - L89 were not covered by tests
self._write_command("MR%d" % value, address)
if wait is True:
pos = self._wait(address)
if abs(pos - target) <= precision:
logger.debug("At position {pos}, Target at {target}".format(pos=pos, target=target))

Check warning on line 94 in basil/HL/mercury.py

View check run for this annotation

Codecov / codecov/patch

basil/HL/mercury.py#L91-L94

Added lines #L91 - L94 were not covered by tests
else:
logger.warning("Target not reached! Target: {target}, actual position: {pos}, precision: {pre}".format(target=target, pos=pos, pre=precision))

Check warning on line 96 in basil/HL/mercury.py

View check run for this annotation

Codecov / codecov/patch

basil/HL/mercury.py#L96

Added line #L96 was not covered by tests

def abort(self, address=None):
self._write_command("AB", address)

Check warning on line 99 in basil/HL/mercury.py

View check run for this annotation

Codecov / codecov/patch

basil/HL/mercury.py#L98-L99

Added lines #L98 - L99 were not covered by tests

def find_edge(self, n, address=None):
self._write_command("FE%d" % n, address)
pos = self._wait(address)
logger.debug("Edge found at position: {pos}".format(pos=pos))

Check warning on line 104 in basil/HL/mercury.py

View check run for this annotation

Codecov / codecov/patch

basil/HL/mercury.py#L101-L104

Added lines #L101 - L104 were not covered by tests

def _wait(self, address=None): # waits until motor stops moving
logger.debug("Moving! Starting position: {pos}".format(pos=self.get_position(address)))
done = False
while done is False:
a = self.get_position(address)
time.sleep(1)
b = self.get_position(address)
if a == b:
SilasM2001 marked this conversation as resolved.
Show resolved Hide resolved
done = True

Check warning on line 114 in basil/HL/mercury.py

View check run for this annotation

Codecov / codecov/patch

basil/HL/mercury.py#L106-L114

Added lines #L106 - L114 were not covered by tests
else:
time.sleep(0.5)
return b

Check warning on line 117 in basil/HL/mercury.py

View check run for this annotation

Codecov / codecov/patch

basil/HL/mercury.py#L116-L117

Added lines #L116 - L117 were not covered by tests
29 changes: 26 additions & 3 deletions examples/lab_devices/MotorStage.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,33 @@
''' This script shows how to use a Motor Stage
'''


import time
from basil.dut import Dut

dut = Dut('mercury_pyserial.yaml')
dut.init()
print(dut["MotorStage"].get_position())
# dut["MotorStage"].set_position(100000)

# setup (for c-862)
# needed if mercury is connected the first time after power up
# MN Motor=on
# LL: switch logic active low (hardware)

dut["MotorStage"].motor_on(address=1)
time.sleep(0.1)
dut["MotorStage"].LL(address=1)
time.sleep(0.1)

# move to absolute position 10000:
# dut["MotorStage"].set_position(10000, address=1, wait=True)

# get position:
# print(dut["MotorStage"].get_position(address=1))

# move relative 10000:
dut["MotorStage"].move_relative(10000, address=1, wait=True)

# finding edge example:
# dut["MotorStage"].find_edge(1,address=1) # 0 or 1 indicates direction of movement

# abort any movement abruptly:
# dut["MotorStage"].abort(address=1)
Loading