def test_read_truncates_data_when_using_message_id(binaryserial, fake): fake.send(BinaryCommand(3, 55, -1, 1)) reply = binaryserial.read(True) assert (reply.device_number == 3) assert (reply.command_number == 55) assert (reply.data == 16777215) assert (reply.message_id == 1)
def test_send_overwrites_device_number(device, fake): t = threading.Thread(target=device.send, args=(BinaryCommand(device.number + 1, 1, 2), )) t.start() fake.expect(pack(device.number, 1, 2)) fake.send(pack(device.number, 1, 0)) t.join()
def test_read_reads_message_id(binaryserial, fake): fake.send(BinaryCommand(1, 55, 34, 22)) reply = binaryserial.read(message_id=True) assert (reply.device_number == 1) assert (reply.command_number == 55) assert (reply.data == 34) assert (reply.message_id == 22)
def test_message_id_clobbers_4th_data_bit(): bc = BinaryCommand(1, 2, 2038004089, 9) s = bc.encode().decode() assert (s[0] == '\x01') assert (s[1] == '\x02') assert (s[2] == s[3] == s[4] == '\x79') assert (s[5] == '\x09')
def AddDevice(self, axis): self._motors[axis] = True # change setting of devices, because they are non-volatile # disable auto-reply 1*2^0 = 1 # enable backlash correction 1*2^1 = 2 command_number = 40 # set device mode command = BinaryCommand(axis, command_number, 3) self.con.write(command)
def test_encode_with_message_id(): bc = BinaryCommand(1, 2, 3, 4) s = bc.encode().decode() assert (s[0] == '\x01') assert (s[1] == '\x02') assert (s[2] == '\x03') assert (s[3] == s[4] == '\x00') assert (s[5] == '\x04')
def test_encode(): bc = BinaryCommand(1, 2, 3) # In this case encode() is not the inverse of decode(). # encode() will convert from a BinaryCommand to "bytes", # and then decode will convert from bytes to unicode str. # The "decode()" call is only necessary to make this test # consistent in both Python 2 and 3. s = bc.encode().decode() assert (s[0] == '\x01') assert (s[1] == '\x02') assert (s[2] == '\x03') assert (s[3] == s[4] == s[5] == '\x00')
def ReadOne(self, axis): command_number = 60 # return current position command = BinaryCommand(axis, command_number) for i in range(50): self.con.write(command) reply = self.con.read() if (reply.command_number != command_number) & (reply.device_number != axis): time.sleep(0.05) else: break return int(reply.data)
def command(self, device, command, data, returns): while True: if self.port.can_read(): self.port.read() else: break self.port.write(BinaryCommand(device, command, data)) if returns: msg = [] for _ in range(returns): msg.append(self.port.read()) return msg else: return None
def StateOne(self, axis): limit_switches = MotorController.NoLimitSwitch command_number = 54 # return status command = BinaryCommand(axis, command_number) self.con.write(command) reply = self.con.read() while (reply.command_number != command_number) & (reply.device_number != axis): self.con.write(command) reply = self.con.read() time.sleep(0.2) if reply.data == 0: # idle return self.StateMap[1], 'Zaber is idle', limit_switches elif (reply.data >= 1) & (reply.data <= 23): return self.StateMap[2], 'Zaber is moving', limit_switches else: return self.StateMap[3], 'Zaber is faulty', limit_switches
def SendToCtrl(self, cmd): """ Send custom native commands. The cmd is a space separated string containing the command information. Parsing this string one gets the command name and the following are the arguments for the given command i.e.command_name, [arg1, arg2...] :param cmd: string :return: string (MANDATORY to avoid OMNI ORB exception) """ # Get the process to send mode = cmd.split(' ')[0].lower() args = cmd.strip().split(' ')[1:] if mode == 'homing': try: if len(args) == 1: axis = args[0] axis = int(axis) else: raise ValueError('Invalid number of arguments') except Exception as e: self._log.error(e) self._log.info('Starting homing for Zaber mirror') try: command_number = 1 # homing command = BinaryCommand(axis, command_number) self.con.write(command) except Exception as e: self._log.error(e) print(e) return 'Error' self._log.info('Homing was finished') return "[DONE]" else: self._log.warning('Invalid command') return 'ERROR: Invalid command requested.'
def movedist(distance, direction_sign, device): # removed speed for now (third variable) # where device = device number. from src.check_command_succeeded import check_command_succeeded from zaber.serial import BinarySerial, BinaryDevice, BinaryCommand # , BinaryReply #Added import time # Added # ------------------------------------------------------------------------- # TODO: Create device # ------------------------------------------------------------------------- # command = BinaryCommand(device, 21, steps) # Added. 21 = move rel port = BinarySerial(device) # Added # ------------------------------------------------------------------------- # TODO: Convert distances to steps. Put in the true conversion. # ------------------------------------------------------------------------- converter = 1 # converts distance to steps. (steps/distance) steps = int(distance) * converter * int( direction_sign) #number of microsteps to move # ------------------------------------------------------------------------- # TODO: Insert motor move command # ------------------------------------------------------------------------- command = BinaryCommand(0, 21, steps) port.write(command) # Added # device.move_rel(speed,blocking=True) # Added Commented Out #r eply = device.move_rel(steps) # move rel 2000 microsteps #A dded Commented out everything below # if not check_command_succeeded(reply): #print("Device move failed.") #exit(1) # else: # print("Motor moved", distance, "microsteps") return
# nrow = 5 # ncol = 5 from zaber.serial import BinarySerial, BinaryDevice, BinaryCommand # , BinaryReply import time # Assume that our serial port can be found at COM1 port = BinarySerial( "COM13" ) # be mindful of which COM port to use; check device manager; may need to restart port # with BinarySerial("COM13") as port: # Device number 0, command number 1. 1 is Home. 0 is Reset, move absolute is 20, relative is 21, 23 is stop #max relative move is 20,000, 42 sets speed, can be changed during a move; calculate spd w/ https://www.zaber.com/documents/ZaberSpeedSetting.xls command = BinaryCommand(0, 1) #command = BinaryCommand(0, 20, 0) #Goes at least to 50,000. Assume moving 10,000 = moving 0.01in #command = BinaryCommand(0, 21, 20000) #Can use negative distances with relative #devicenum = 0 #distance = 20000 #command = BinaryCommand(devicenum, 21, distance) #command = BinaryCommand(0, 21, 20000) # note: Binary motor commands are blocking. Syntax: BinaryCommand(device#, command#, dataValue) # List of command #s can be found at: https://www.zaber.com/wiki/Manuals/Binary_Protocol_Manual#Command_Reference port.write(command) # Assume that we have 1 devices connected. #ndevices = 1 # for i in range(0, ndevices):
def StartOne(self, axis, position): command_number = 20 # move absolute command = BinaryCommand(axis, command_number, int(position)) self.con.write(command)
def test_negative_data_does_not_raise_exceptions(): BinaryCommand(1, 55, -12312)
def test_3_argument_constructor(): bc = BinaryCommand(1, 5, 334) assert (bc.device_number == 1) assert (bc.command_number == 5) assert (bc.data == 334)
def test_no_arguments_throws_typeerror(): with pytest.raises(TypeError): BinaryCommand()
def test_negative_device_or_command_number_raises_valueerror(): with pytest.raises(ValueError): BinaryCommand(-1, 1, 0) with pytest.raises(ValueError): BinaryCommand(3, -2, 123, 3)
def test_2_argument_constructor(): bc = BinaryCommand(1, 0) assert (bc.device_number == 1) assert (bc.command_number == 0) assert (bc.data == 0) assert (bc.message_id == None)
def test_message_id_is_none(): bc = BinaryCommand(2, 54, 34) assert (bc.device_number == 2) assert (bc.command_number == 54) assert (bc.data == 34) assert (bc.message_id == None)
def test_message_id_in_4th_arg(): bc = BinaryCommand(1, 2, 3, 4) assert (bc.device_number == 1) assert (bc.command_number == 2) assert (bc.data == 3) assert (bc.message_id == 4)
def test_write(binaryserial, fake): cmd = BinaryCommand(1, 54) binaryserial.write(cmd) fake.expect(cmd.encode())
def __init__(self, time_offset, COM_port, dev1_axis, dev2_axis, coordinates_fname): self.time_offset = time_offset self.COM_port = COM_port self.coordinates_fname = coordinates_fname with h5py.File('drivers/' + coordinates_fname, 'r') as f: self.coordinates_random = f['all_points'][()] np.random.shuffle(self.coordinates_random) # shape and type of the array of returned data from ReadValue self.dtype = ('f4', 'int32', 'int32') self.shape = (3, ) try: self.port = BinarySerial(COM_port) msg = self.command(0, 50, 0, 2) msg = [d.data for d in msg] if not all(elem == msg[0] for elem in msg): raise ValueError( 'ZaberTMM warning in verification : Device IDs not equal') except Exception as err: logging.warning("ZaberTMM error in initial connection : " + str(err)) self.verification_string = "False" self.__exit__() return None try: self.port.write(BinaryCommand(0, 55, 123)) msg1 = self.port.read() msg2 = self.port.read() if msg1.data != 123: logging.warning( "ZaberTMM warning in verification : motor {0} connection error" .format(msg1.device_number)) if msg2.data != 123: logging.warning( "ZaberTMM warning in verification : motor {0} connection error" .format(msg2.device_number)) self.verification_string = "True" except Exception as err: logging.warning('ZaberTMM warning in verification : ' + str(err)) self.verification_string = "False" self.warnings = [] self.position = ZaberCoordinates(dev1_axis, dev2_axis) if dev1_axis == 'x': self.devx = 1 self.devy = 2 elif dev1_axis == 'y': self.devx = 2 self.devy = 1 if not self.ReadDeviceModeX()[7]: warning = 'Home Status bit not set in Dev{0}, home device'.format( self.devx) logging.warning('ZaberTMM warning: ' + warning) self.CreateWarning(warning) if not self.ReadDeviceModeY()[7]: warning = 'Home Status bit not set in Dev{0}, home device'.format( self.devx) logging.warning('ZaberTMM warning: ' + warning) self.CreateWarning(warning) self.sweep_thread = None self.running_sweep = False self.sweep_start_position = 'current' self.sweep_square_params = {} self.step_rectangle = None self.GetPosition() # HDF attributes generated when constructor is run self.new_attributes = [ ('dev1_axis', dev1_axis), ('dev2_axis', dev2_axis), ('x_speed', str(self.ReadTargetSpeedX())), ('y_speed', str(self.ReadTargetSpeedY())), ('x_acceleration', str(self.ReadAccelerationX())), ('y_acceleration', str(self.ReadAccelerationY())) ]
def test_sending_BinaryCommand_raises_error(asciiserial): with pytest.raises(TypeError): asciiserial.write(BinaryCommand(1, 55, 23423))
def AbortOne(self, axis): command_number = 23 # move absolute command = BinaryCommand(axis, command_number) self.con.write(command)