Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
kzhao1228 authored May 31, 2020
1 parent fc7f953 commit fc440be
Showing 1 changed file with 31 additions and 160 deletions.
191 changes: 31 additions & 160 deletions stage/motor_ctrl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def set_pos(self, new_value, blocking = False):
new_value : float or int
absolute position the stage will be moved to
blocking : bool
not wait unil completion of moving
wait unil completion of moving
Default: False
Examples
Expand All @@ -285,47 +285,16 @@ def set_pos(self, new_value, blocking = False):
new_value = self.pos
else:
raise TypeError('Position value must be an integer or a float')
# convert position using EncCnt scaling factor
abs_dist = int(new_value * self._EncCnt)

absolute_distance = int(new_value * self._EncCnt)
self._port.send_message(MGMSG_MOT_MOVE_ABSOLUTE_long(chan_ident = self._chan_ident, absolute_distance = absolute_distance))
if not blocking:
# if the desired position is the same as the current position of the stage, no command sent out in this case
if abs_dist == self._state_position:
time.sleep(0.3)
while self.is_in_motion:
pass

else:
pos_ini = self.pos
self._port.send_message(MGMSG_MOT_MOVE_ABSOLUTE_long(chan_ident = self._chan_ident, \
absolute_distance = abs_dist))
time_ini = time.time()
while not self.is_in_motion:
if abs(abs_dist - pos_ini*self._EncCnt) == 1:
break
if time.time() - time_ini >= 2:
break
time_ini = time.time()
while True:
if not bool(sum([self.is_in_motion for i in range(10)])):
break
if time.time() - time_ini >= 20:
raise Exception('Unable to move the stage to the new position')
param_ini = 0
while True:
if sum([self._state_position for i in range(10)])/10 == abs_dist:
break
elif time.time() - time_ini >= 30:
raise Exception('Unable to move the stage to the new position')
else:
if param_ini == 0 and abs_dist != 0:
self._port.send_message(MGMSG_MOT_MOVE_ABSOLUTE_long(chan_ident = \
self._chan_ident, absolute_distance = abs_dist))
if param_ini == 0 and abs_dist == 0:
self.move_home()
param_ini += 1
if blocking:
self._port.send_message(MGMSG_MOT_MOVE_ABSOLUTE_long(chan_ident = self._chan_ident, \
absolute_distance = abs_dist))


time.sleep(0.8)


def move_by(self, new_value, blocking = False):
"""
Move relative to current position.
Expand All @@ -335,75 +304,22 @@ def move_by(self, new_value, blocking = False):
new_value : float or int
relative distance for the stage to travel
blocking : bool
not wait until moving is finished
wait until moving is finished
Default: False
Examples
--------
s.move_by(2)
s.move_by(-2)
"""
rel_dist = int(new_value * self._EncCnt)
# check if the input value for travelling distance is a float or an integer
if isinstance(new_value,(int,float)):
try:
# add a parameter value restriction
assert (self.pos + new_value >= self._conf_min_pos) and \
(self.pos + new_value <= self._conf_max_pos)
except AssertionError:
print('Oops! Try a different value for jogging distance and it should be within [{0},{1}] {2}'. \
format(self._conf_min_pos - self.pos, self._conf_max_pos - self.pos, self.units))
new_value = None
else:
raise TypeError('Position value must be an integer or a float')
assert type(new_value) in (float, int)
relative_distance = int(new_value * self._EncCnt)
self._port.send_message(MGMSG_MOT_MOVE_RELATIVE_long(chan_ident = self._chan_ident, relative_distance = relative_distance))
time.sleep(0.3)
while self.is_in_motion and not blocking:
pass
time.sleep(0.8)

if not blocking:
# if the desired position is the same as the current position of the stage, no command sent out in this case
if new_value == None:
return
if rel_dist == 0:
return

else:
pos_ini = int(self.pos * self._EncCnt)
self._port.send_message(MGMSG_MOT_MOVE_RELATIVE_long(chan_ident = self._chan_ident, \
relative_distance = rel_dist))
time_ini = time.time()
while not self.is_in_motion:
if abs(rel_dist) == 1:
break
if time.time() - time_ini >= 2:
break
time_ini = time.time()
while True:
if not bool(sum([self.is_in_motion for i in range(10)])):
break
if time.time() - time_ini >= 20:
raise Exception('Unable to move the stage to the new position')
param_ini = 0
while True:
if sum([self._state_position for i in range(10)])/10 == rel_dist + pos_ini:
break
elif time.time() - time_ini >= 30:
raise Exception('Unable to move the stage to the new position')
else:
'''
if param_ini == 0 and rel_dist + pos_ini != 0:
self._port.send_message(MGMSG_MOT_MOVE_RELATIVE_long(chan_ident = self._chan_ident, \
relative_distance = rel_dist))
print('1')'''
if param_ini == 0 and rel_dist + pos_ini == 0:
self.move_home()
print('Unknown error occured')
param_ini += 1
if blocking:
if new_value == None:
pass
else:
self._port.send_message(MGMSG_MOT_MOVE_RELATIVE_long(chan_ident = self._chan_ident, \
relative_distance = rel_dist))

@property
def backlash_dist(self):
"""
Expand Down Expand Up @@ -646,9 +562,9 @@ def is_in_motion(self):
True for in motion and False for not in motion
"""
motion_status = self.status_in_motion_forward or self.status_in_motion_reverse or self.status_in_motion_jogging_forward or \
self.status_in_motion_jogging_reverse or self.status_in_motion_homing
return motion_status
status_bits = self._state_status_bits
mask = 0x00000010 or 0x00000020 or 0x00000040 or 0x00000080 or 0x00000200 or 0x00004000
return bool(status_bits & mask)

@property
def status_homed(self):
Expand Down Expand Up @@ -681,61 +597,17 @@ def status_in_motion_homing(self):
@property
def home_check(self):
"""
Returns whether stage homing is completed.
Returns whether motor homing is completed.
Returns
-------
out : bool
True when homing is completely finished.
Information
-----------
there are four conditions to be checked:
- if stage is starting to move
- if homing is completed
- if stage is still
- if controller is faulty
i) 'manually' move stage to home
ii) wait until stage arrives at 0 mm
See also
--------
move_home()
True when homing is completed.
"""
pos_ini = self.pos
# homing command takes some time to reach the controller
# and it also takes some time for the controller to execute
# the command. we let the subsequent motion status check
# happen until the motor starts moving
while (self.pos - pos_ini) == 0:
pass
# then we wait until homing is finished
while not self.status_homed:
pass
# also, we have to check if the motor is still in motion; if it is,
# we wait until it's absolutely static.
while True:
if not bool(sum([self.is_in_motion for i in range(10)])):
break
# unfornately, should the controller becomes slightly faulty,
# namely that the stage is not at position 0 mm, we have to manually
# move the stage home using property set_pos
time_ini = time.time()
param_ini = 0
while (not self.pos == 0.0) or self.is_in_motion:
# type I error: if the controller says it's homed when it's not,
# we move the stage to position 0 'manually'.
if self.status_homed and (not sum([self.is_in_motion for i in range(20)])) \
and (param_ini == 0):
self.set_pos(0)
param_ini += 1
if time.time() - time_ini >= 20:
raise Exception('Homing error: your controller maybe faulty.')
# type II error: maybe the controller is still doing fine adjustment
# on the stage position. In this case, we wait until everything is
# done.
if not self.status_in_motion_forward and not self.status_in_motion_reverse:
pass
return True

##### VEL PARAMS ###################################################################################################################
Expand Down Expand Up @@ -1267,21 +1139,20 @@ def move_home(self, blocking = False):
Parameters
----------
blocking: bool
not wait until homing is completed.
wait until homing is completed.
Default: False
See also
--------
home_check
'''
self._port.send_message(MGMSG_MOT_MOVE_HOME(chan_ident = self._chan_ident))
# we make sure that the controller waits until homing is completed by adding an
# 'if' loop to call property home_check.
# we make sure that the controller waits until homing is completed by adding an 'if' loop.
if not blocking:
# 0.3 sec is the response time between when the command is sent out and when
# the controller is about to execute the command.
time.sleep(0.3)
check_homed = self.home_check
if check_homed:
return None
time.sleep(0.5)
return print('Homed')

##### SYSTEM PARAMETERS ###############################################################################################

Expand Down

0 comments on commit fc440be

Please sign in to comment.