Skip to content

Commit

Permalink
attempts fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
julienbarrier committed Aug 18, 2023
1 parent 2123dbe commit b07c5bc
Showing 1 changed file with 58 additions and 19 deletions.
77 changes: 58 additions & 19 deletions mesoscopy/instrument/motion_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import qcodes.utils.validators as vals
from qcodes import Instrument, Parameter, VisaInstrument
from qcodes.utils.helpers import strip_attrs

from qcodes_contrib_drivers.drivers.Thorlabs.APT import Thorlabs_APT, ThorlabsHWType
from . import _Thorlabs_error_codes as _error_codes
Expand Down Expand Up @@ -476,23 +477,31 @@ def get_hw_serial_num_ex(self, hw_type: Union[int, ThorlabsHWType], index: int)
return c_serial_number.value


class arduino2ch_stage(VisaInstrument):
class arduino2ch_stage(Instrument):
"""
Class to represent a 2-channel arduino controller (X-Y stage)
"""
default_timeout = 1.0
_default_timeout = 5.0

def __init__(self, name: str, address: str, reverse_x=False, reverse_y=False, **kwargs) -> None:
def __init__(self, name: str, address: str, timeout: float = None,
reverse_x: bool = False, reverse_y: bool = False, **kwargs) -> None:
"""
Args:
name (str): Name to use internally
address (str): VISA string describing the serial port,
for example "ASRL3" for COM3.
address (str): Serial port (e.g. 'COM3').
timeout: Serial conection timeout
"""
super().__init__(name, address, timeout=self.default_timeout,
terminator='\n',**kwargs)
assert isinstance(self.visa_handle, SerialInstrument)
self.visa_handle.baud_rate = 9600
super().__init__(name, **kwargs)
self._address = address
if timeout:
self._timeout = timeout
else: self._timeout = self._default_timeout

self._open_serial()
#super().__init__(name, address, timeout=self.default_timeout,
# terminator='\n',**kwargs)
#assert isinstance(self.visa_handle, SerialInstrument)
#self.visa_handle.baud_rate = 9600

self._path = "C:/arduinoXYstage/"
self._path_x = self._path + "stepper_position_x.txt"
Expand Down Expand Up @@ -542,6 +551,36 @@ def __init__(self, name: str, address: str, reverse_x=False, reverse_y=False, **
self._init_device(self._reverse_x, self._reverse_y)
self.connect_message()


def _open_serial(self):
try:
ser = getattr(serial,self._address)
except AttributeError:
ser = serial.Serial(port=self._address,
baudrate=9600,
timeout=self._timeout)
setattr(serial, self._address, ser)

if not ser.isOpen():
ser.open()
self._ser = ser
print(f'Connected to {self._address}')

def get_idn(self):
id = ''
self._ser.write(str.encode('*IDN?\r'))
id = self._ser.readline().decode()
return id

def close(self):
if hasattr(self, 'connection') and hasattr(self.connection, 'close'):
self.connection.close()
self._ser.close()

strip_attrs(self, whitelist=['name'])
self.remove_instance(self)


def _init_device(self, reverse_x: bool, reverse_y: bool):
"""initialize device
Expand Down Expand Up @@ -662,20 +701,20 @@ def _go_x_steps(self, steps):
ss = 'x' + self.x_p + str(abs(steps))
else:
ss = 'x' + self.x_n + str(abs(steps))
self.visa_handle.write(ss)
self._ser.write(ss)
finished = None
while finished != "0":
finished = self.visa_handle.read_bytes(1)
while finished != '0':
finished = self._ser.read(1).decode()

def _go_y_steps(self, steps):
if steps >= 0:
ss = 'y' + self.y_p + str(abs(steps))
else:
ss = 'y' + self.y_n + str(abs(steps))
self.visa_handle.write(ss)
self._ser.write(ss)
finished = None
while finished != "0":
finished = self.visa_handle.read_bytes(1)
while finished != '0':
finished = self._ser.read(1).decode()

def _read_file(self, path):
file = open(path, 'r')
Expand All @@ -688,9 +727,9 @@ def _write_file(self, path, val):
val = file.write(str(val))
file.close()

def get_idn(self):
return { "vendor": "arduino 2ch", "model": None,
"serial": None, "firmware": None,}
#def get_idn(self):
# return { "vendor": "arduino 2ch", "model": None,
# "serial": None, "firmware": None,}


class arduino1ch_stage(Instrument):
Expand Down Expand Up @@ -798,7 +837,7 @@ def get_z(self):
Z_in_um = (float(self._read_file(self._path_y))*max_um_Z)/max_steps_Z
return Z_in_um/1e6

def _go_j_steps(self, steps):
def _go_z_steps(self, steps):
if steps >= 0:
ss = 'y' + self.z_p + str(abs(steps))
else:
Expand Down

0 comments on commit b07c5bc

Please sign in to comment.