Exemplo n.º 1
0
 def test_i2c_sensorXYZ_value(self, mock_robot_init, caplog):
     robot = BaseRobot(**mock_robot_init['i2crobot'])
     robot.start()
     s = robot.sensors['gyro']
     d = robot.devices['imu']
     exp_x = - d.word_g_x.value
     exp_y = d.word_g_y.value + 256
     exp_z = - d.word_g_z.value + 1024
     assert s.x == exp_x
     assert s.y == exp_y
     assert s.z == exp_z
     assert s.value == (exp_x, exp_y, exp_z)
     # byte sensor
     s = robot.sensors['accel_byte']
     exp_x = d.byte_xl_x.value + 128
     exp_y = - d.byte_xl_y.value -128
     exp_z = d.byte_xl_z.value + 64
     assert s.x == exp_x
     assert s.y == exp_y
     assert s.z == exp_z
     assert s.value == (exp_x, exp_y, exp_z)
     assert s.active
     # activate w/o register
     caplog.clear()
     s.active = True
     assert len(caplog.records) == 1
     assert 'attempted to change activation of sensor' in caplog.text
Exemplo n.º 2
0
 def test_i2c_sensor(self, mock_robot_init, caplog):
     robot = BaseRobot(**mock_robot_init['i2crobot'])
     robot.start()
     s = robot.sensors['temp']
     d = robot.devices['imu']
     assert s.device == d
     assert s.read_register == d.temp
     assert s.activate_register == d.activate_temp
     assert not s.active
     # removed the masking in sensor
     # assert s.bits is None
     assert s.offset == 0
     assert s.inverse
     assert s.auto_activate
     s.active = True
     assert s.active
     assert s.value == -10.0
     # masks
     assert robot.sensors['status0'].value 
     assert not robot.sensors['status1'].value
     # setting active for register w/o active register
     caplog.clear()
     robot.sensors['status0'].active = True
     assert len(caplog.records) == 1
     assert 'attempted to change activation of sensor' in caplog.text
Exemplo n.º 3
0
 def test_i2c_write2_with_errors(self, mock_robot_init, caplog):
     robot = BaseRobot(**mock_robot_init['i2crobot'])
     robot.start()
     device = robot.devices['imu']
     for _ in range(20):     # so that we also get some errors
         device.word_control_2.value = 0x2121
     assert device.word_control_2.value == 0x2121
Exemplo n.º 4
0
 def test_dynamixel_bus_closed(self, mock_robot_init, caplog):
     robot = BaseRobot(**mock_robot_init['dynamixel'])
     bus = robot.buses['ttys1']
     # ping
     caplog.clear()
     bus.ping(11)
     assert len(caplog.records) == 1
     assert 'Ping invoked with a bus not opened' in caplog.text
     # scan
     caplog.clear()
     bus.scan(range(10))
     assert len(caplog.records) == 1
     assert 'Scan invoked with a bus not opened' in caplog.text
     # read
     dev = robot.devices['d11']
     caplog.clear()
     bus.read(dev.return_delay_time)
     assert len(caplog.records) == 1
     assert 'Attempt to use closed bus "ttys1"' in caplog.text
     # write
     caplog.clear()
     bus.write(dev.return_delay_time, 10)
     assert len(caplog.records) == 1
     assert 'Attempt to use closed bus "ttys1"' in caplog.text
     robot.stop()
Exemplo n.º 5
0
 def test_dynamixel_register_4Bytes(self, mock_robot_init):
     robot = BaseRobot(**mock_robot_init['dynamixel'])
     robot.start()
     dev = robot.devices['d11']
     register = BaseRegister(name='test', device=dev, address=150, size=4,
              access='RW')
     register.value = 100
     assert register.value == 100
Exemplo n.º 6
0
    def test_incomplete_robot(self, caplog):
        # we now allow creating robots with no buses
        caplog.clear()
        _ = BaseRobot.from_yaml('tests/no_buses.yml')

        # we now allow creating robots with no devices
        caplog.clear()
        _ = BaseRobot.from_yaml('tests/no_devices.yml')

        caplog.clear()
        _ = BaseRobot.from_yaml('tests/dummy_robot.yml')
        assert 'Only the first robot will be considered' in caplog.text
Exemplo n.º 7
0
 def test_register_with_dynamic_conversion(self, mock_robot_init):
     mock_robot_init['i2crobot']['buses']['i2c2']['err'] = 0
     robot = BaseRobot(**mock_robot_init['i2crobot'])
     robot.start()
     d = robot.devices['imu']
     # set the control register
     d.range_control.value = 0b110000
     assert d.range_control_conv.value == 3
     assert d.dynamic_register.value == 1024.0 / 10.0 * 3.0
     d.range_control_conv.value = 6
     assert d.range_control.value == 0b1100000
     assert d.dynamic_register.value == 1024.0 / 10.0 * 6.0
Exemplo n.º 8
0
 def test_protocol1_bulkwrite(self, mock_robot_init):
     mock_robot_init['dynamixel']['buses']['ttys1']['protocol'] = 1.0
     # we remove the bulkwrite so that the error will refer to syncread
     del mock_robot_init['dynamixel']['syncs']['syncread']
     with pytest.raises(ValueError) as excinfo:
         _ = BaseRobot(**mock_robot_init['dynamixel'])
     assert 'BulkWrite only supported for Dynamixel Protocol 2.0' \
         in str(excinfo.value)
Exemplo n.º 9
0
 def test_i2c_robot_bus_error(self, mock_robot_init, caplog):
     mock_robot_init['i2crobot']['buses']['i2c2']['mock'] = False
     mock_robot_init['i2crobot']['buses']['i2c2']['auto'] = False
     mock_robot_init['i2crobot']['buses']['i2c2']['port'] = 42
     robot = BaseRobot(**mock_robot_init['i2crobot'])
     caplog.clear()
     robot.buses['i2c2'].open()
     assert len(caplog.records) >= 2
     assert 'failed to open I2C bus' in caplog.text
Exemplo n.º 10
0
 def test_robot_from_yaml(self, mock_robot):
     mock_robot.stop()       # to avoid conflicts on the bus
     new_robot = BaseRobot.from_yaml('tests/dummy_robot.yml')
     new_robot.start()
     new_robot.syncs['write'].start()
     time.sleep(2)           # for loops to run
     new_robot.stop()
     mock_robot.start()      # restart for other cases
     assert mock_robot.name == 'dummy'
Exemplo n.º 11
0
 def test_i2c_register_with_sign(self, mock_robot_init):
     robot = BaseRobot(**mock_robot_init['i2crobot'])
     robot.start()
     d = robot.devices['imu']
     # no factor
     d.word_xl_x.value = 10
     assert d.word_xl_x.value == 10
     assert d.word_xl_x.int_value == 10
     d.word_xl_x.value = -10
     assert d.word_xl_x.value == -10
     assert d.word_xl_x.int_value == (65536 - 10)
     # with factor
     d.word_xl_y.value = 100
     assert d.word_xl_y.value == 100
     assert d.word_xl_y.int_value == 1000
     d.word_xl_y.value = -100
     assert d.word_xl_y.value == -100
     assert d.word_xl_y.int_value == (65536 - 1000)
Exemplo n.º 12
0
 def test_i2c_loop_failed_acquire(self, mock_robot_init, caplog):
     robot = BaseRobot(**mock_robot_init['i2crobot'])
     robot.start()
     # lock the bus
     robot.buses['i2c2'].can_use()
     # read
     caplog.clear()
     robot.syncs['read_g'].start()
     time.sleep(1)
     assert len(caplog.records) >= 1
     assert 'failed to acquire bus' in caplog.text
     robot.syncs['read_g'].stop()    
     # write
     caplog.clear()
     robot.syncs['write_xl'].start()
     time.sleep(1)
     assert len(caplog.records) >= 1
     assert 'failed to acquire bus' in caplog.text
     robot.syncs['read_g'].stop()    
Exemplo n.º 13
0
 def test_manual_device(self, caplog):
     rob = BaseRobot()
     bus = SharedFileBus(name='busA', port='/tmp/busA.log')
     rob.add_bus(bus)
     dev = BaseDevice(name='dev1', bus=bus, dev_id=42, model='DUMMY')
     rob.add_device(dev)
     assert len(rob.devices) == 1
     bus.open()
     # re-add
     caplog.clear()
     rob.add_device(dev)
     assert len(caplog.records) == 1
     assert len(rob.devices) == 1      
Exemplo n.º 14
0
 def test_i2c_sensorXYZ(self, mock_robot_init):
     robot = BaseRobot(**mock_robot_init['i2crobot'])
     robot.start()
     s = robot.sensors['gyro']
     d = robot.devices['imu']
     assert s.device == d
     assert s.x_register == d. word_g_x
     assert s.y_register == d. word_g_y
     assert s.z_register == d. word_g_z
     assert s.activate_register == d.activate_g
     assert not s.active
     assert s.x_offset == 0
     assert s.x_inverse
     assert s.y_offset == 256
     assert not s.y_inverse
     assert s.z_offset == 1024
     assert s.z_inverse        
     assert s.auto_activate
     s.active = True
     assert s.active
Exemplo n.º 15
0
 def test_dynamixel_scan(self, mock_robot_init):
     robot = BaseRobot(**mock_robot_init['dynamixel'])
     robot.start()
     ids = robot.buses['ttys1'].scan()
     assert 11 in ids
     assert 12 in ids
     robot.stop()
Exemplo n.º 16
0
 def test_open_device_with_sync_items(self, mock_robot_init):
     robot = BaseRobot(**mock_robot_init['dynamixel'])
     robot.start()
     dev = robot.devices['d11']
     dev.present_position_deg.sync = True
     dev.open()
     robot.stop()
Exemplo n.º 17
0
 def test_i2c_bus_closed(self, mock_robot_init, caplog):
     robot = BaseRobot(**mock_robot_init['i2crobot'])
     dev = robot.devices['imu']
     # write to closed bus
     caplog.clear()
     dev.byte_xl_x.value = 20
     assert len(caplog.records) >= 1
     assert 'attempted to write to a closed bus' in caplog.text
     # read from closed bus
     caplog.clear()
     _ = dev.byte_xl_x.value
     assert len(caplog.records) >= 1
     assert 'attempted to read from a closed bus' in caplog.text
Exemplo n.º 18
0
 def test_dynamixel_register_low_endian(self, mock_robot_init, caplog):
     robot = BaseRobot(**mock_robot_init['dynamixel'])
     dev = robot.devices['d11']
     assert dev.register_low_endian(123, 4) == [123, 0, 0, 0]
     num = 12 * 256 + 42
     assert dev.register_low_endian(num, 4) == [42, 12, 0, 0]
     num = 39 * 65536 + 12 * 256 + 42
     assert dev.register_low_endian(num, 4) == [42, 12, 39, 0]
     num = 45 * 16777216 + 39 * 65536 + 12 * 256 + 42
     assert dev.register_low_endian(num, 4) == [42, 12, 39, 45]
     caplog.clear()
     _ = dev.register_low_endian(num, 6)
     assert len(caplog.records) == 1
     assert 'Unexpected register size' in caplog.text
Exemplo n.º 19
0
 def test_i2c_sharedbus_closed(self, mock_robot_init, caplog):
     robot = BaseRobot(**mock_robot_init['i2crobot'])
     # we haven't started the bus
     # read
     caplog.clear()
     robot.syncs['read_g'].start()
     assert len(caplog.records) == 1
     assert 'attempt to start with a bus not open' in caplog.text
     robot.syncs['read_g'].stop()    
     # write
     caplog.clear()
     robot.syncs['write_xl'].start()
     assert len(caplog.records) == 1
     assert 'attempt to start with a bus not open' in caplog.text
     robot.syncs['read_g'].stop()
Exemplo n.º 20
0
 def test_dynamixel_write(self, mock_robot_init):
     robot = BaseRobot(**mock_robot_init['dynamixel'])
     robot.start()
     dev = robot.devices['d11']
     for _ in range(100):           # so that we also hit some comm errors
         dev.temperature_limit.value = 85
         assert dev.temperature_limit.value == 85
         dev.cw_angle_limit_deg.value = 10
         assert (dev.cw_angle_limit_deg.value - 10) < 0.2
     robot.stop()
Exemplo n.º 21
0
 def test_manual_bus(self, caplog):
     rob = BaseRobot()
     bus = SharedFileBus(name='busA', port='/tmp/busA.log')
     rob.add_bus(bus)
     assert len(rob.buses) == 1
     bus.open()
     # re-add
     caplog.clear()
     rob.add_bus(bus)
     assert len(caplog.records) == 1
     assert len(rob.buses) == 1
Exemplo n.º 22
0
 def test_i2c_robot(self, mock_robot_init, caplog):
     robot = BaseRobot(**mock_robot_init['i2crobot'])
     robot.start()
     dev = robot.devices['imu']
     # 1 Byte registers
     for _ in range(5):              # to account for possible comm errs
         dev.byte_xl_x.value = 20
         assert dev.byte_xl_x.value == 20
         # word register
         dev.word_xl_x.value = 12345
         assert dev.word_xl_x.value == 12345      
     robot.stop()
Exemplo n.º 23
0
 def test_dynamixel_bus_acquire_bulkread(self, mock_robot_init, caplog):
     # syncs
     robot = BaseRobot(**mock_robot_init['dynamixel'])
     robot.start()
     robot.buses['ttys1'].can_use()      # lock the bus
     caplog.clear()
     robot.syncs['bulkread'].start()
     time.sleep(1)
     assert len(caplog.records) >= 1
     assert 'failed to acquire bus ttys1' in caplog.text
     robot.syncs['bulkread'].stop()
     # release bus
     robot.buses['ttys1'].stop_using()
     robot.stop()
Exemplo n.º 24
0
 def test_i2c_closed_bus(self, mock_robot_init, caplog):
     robot = BaseRobot(**mock_robot_init['i2crobot'])
     device = robot.devices['imu']
     caplog.clear()
     _ = device.word_control_2.value
     assert len(caplog.records) >= 1
     assert 'attempted to read from a closed bus' in caplog.text
     caplog.clear()
     device.word_control_2.value = 0x4242
     assert len(caplog.records) >= 1
     assert 'attempted to write to a closed bus' in caplog.text
     bus = robot.buses['i2c2']
     caplog.clear()
     _ = bus.read_block(device, 1, 6)
     assert len(caplog.records) >= 1
     assert 'attempted to read from a closed bus' in caplog.text
     caplog.clear()
     bus.write_block(device, 1, [1,2,3,4,5,6])
     assert len(caplog.records) >= 1
     assert 'attempted to write to a closed bus' in caplog.text
Exemplo n.º 25
0
 def test_dynamixel_bus_acquire(self, mock_robot_init, caplog):
     robot = BaseRobot(**mock_robot_init['dynamixel'])
     robot.start()
     dev = robot.devices['d11']
     bus = robot.buses['ttys1']
     bus.can_use()      # lock the bus
     # read
     caplog.clear()
     bus.read(dev.return_delay_time)
     assert len(caplog.records) >= 1
     assert 'failed to acquire bus ttys1' in caplog.text
     # write
     caplog.clear()
     bus.write(dev.return_delay_time, 10)
     assert len(caplog.records) >= 1
     assert 'failed to acquire bus ttys1' in caplog.text
     # release bus
     bus.stop_using()
     robot.stop()
Exemplo n.º 26
0
 def test_dynamixel_bus_params(self, mock_robot_init):
     robot = BaseRobot(**mock_robot_init['dynamixel'])
     assert robot.buses['ttys1'].baudrate == 19200
     assert not robot.buses['ttys1'].rs485
     robot.stop()
Exemplo n.º 27
0
 def mock_robot(self):   
     robot = BaseRobot.from_yaml('tests/dummy_robot.yml')
     robot.start()
     yield robot
     robot.stop()
Exemplo n.º 28
0
 def test_dynamixel_bulkwrite(self, mock_robot_init):
     robot = BaseRobot(**mock_robot_init['dynamixel'])
     robot.start()
     robot.syncs['bulkwrite'].start()
     time.sleep(1)
     robot.stop()
Exemplo n.º 29
0
 def test_dynamixel_rangeread(self, mock_robot_init):
     robot = BaseRobot(**mock_robot_init['dynamixel'])
     robot.start()
     robot.syncs['rangeread'].start()
     time.sleep(1)
     robot.stop()
Exemplo n.º 30
0
 def test_dynamixel_ping(self, mock_robot_init):
     robot = BaseRobot(**mock_robot_init['dynamixel'])
     robot.start()
     assert robot.buses['ttys1'].ping(11) == True
     robot.stop()