Add unit tests (WIP)

This commit is contained in:
Remi Cadene
2024-07-09 00:22:30 +02:00
parent 3ba05d53b9
commit a0432f1608
10 changed files with 338 additions and 112 deletions

98
tests/test_cameras.py Normal file
View File

@@ -0,0 +1,98 @@
import numpy as np
import pytest
from lerobot.common.robot_devices.cameras.opencv import OpenCVCamera
from lerobot.common.robot_devices.utils import RobotDeviceNotConnectedError, RobotDeviceAlreadyConnectedError
def test_camera():
# Test instantiating with missing camera index raises an error
with pytest.raises(ValueError):
camera = OpenCVCamera()
# Test instantiating with a wrong camera index raises an error
with pytest.raises(ValueError):
camera = OpenCVCamera(-1)
# Test instantiating
camera = OpenCVCamera(0)
# Test reading, async reading, disconnecting before connecting raises an error
with pytest.raises(RobotDeviceNotConnectedError):
camera.read()
with pytest.raises(RobotDeviceNotConnectedError):
camera.async_read()
with pytest.raises(RobotDeviceNotConnectedError):
camera.disconnect()
# Test deleting the object without connecting first
del camera
# Test connecting
camera = OpenCVCamera(0)
camera.connect()
assert camera.is_connected
assert camera.fps is not None
assert camera.width is not None
assert camera.height is not None
# Test connecting twice raises an error
with pytest.raises(RobotDeviceAlreadyConnectedError):
camera.connect()
# Test reading from the camera
color_image = camera.read()
assert isinstance(color_image, np.ndarray)
assert color_image.ndim == 3
h, w, c = color_image.shape
assert c == 3
assert w > h
# Test reading asynchronously from the camera and image is similar
async_color_image = camera.async_read()
assert np.allclose(color_image, async_color_image)
# Test disconnecting
camera.disconnect()
assert camera.camera is None
assert camera.thread is None
# Test disconnecting with `__del__`
camera = OpenCVCamera(0)
camera.connect()
del camera
# Test acquiring a bgr image
camera = OpenCVCamera(0, color="bgr")
camera.connect()
assert camera.color == "bgr"
bgr_color_image = camera.read()
assert np.allclose(color_image, bgr_color_image[[2,1,0]])
del camera
# Test fps can be set
camera = OpenCVCamera(0, fps=60)
camera.connect()
assert camera.fps == 60
# TODO(rcadene): measure fps in nightly?
del camera
# Test width and height can be set
camera = OpenCVCamera(0, fps=30, width=1280, height=720)
camera.connect()
assert camera.fps == 30
assert camera.width == 1280
assert camera.height == 720
color_image = camera.read()
h, w, c = color_image.shape
assert h == 720
assert w == 1280
assert c == 3
del camera

View File

@@ -0,0 +1,13 @@
def test_teleoperate():
pass
def test_record_dataset():
pass
def test_replay_episode():
pass
def test_run_policy():
pass

73
tests/test_motors.py Normal file
View File

@@ -0,0 +1,73 @@
import time
import numpy as np
import pytest
from lerobot.common.robot_devices.motors.dynamixel import DynamixelMotorsBus
def test_motors_bus():
# Test instantiating a common motors structure.
# Here the one from Alexander Koch follower arm.
motors = {
# name: (index, model)
"shoulder_pan": (1, "xl430-w250"),
"shoulder_lift": (2, "xl430-w250"),
"elbow_flex": (3, "xl330-m288"),
"wrist_flex": (4, "xl330-m288"),
"wrist_roll": (5, "xl330-m288"),
"gripper": (6, "xl330-m288"),
}
motors_bus = DynamixelMotorsBus(
port="/dev/tty.usbmodem575E0032081",
motors=motors,
)
# Test reading and writting before connecting raises an error
with pytest.raises(ValueError):
motors_bus.read("Torque_Enable")
with pytest.raises(ValueError):
motors_bus.write("Torque_Enable")
motors_bus.connect()
# Test connecting twice raises an error
with pytest.raises(ValueError):
motors_bus.connect()
# Test reading torque on all motors and its 0 after first connection
values = motors_bus.read("Torque_Enable")
assert isinstance(values, np.ndarray)
assert len(values) == len(motors)
assert (values == 0).all()
# Test writing torque on a specific motor
motors_bus.write("Torque_Enable", 1, "gripper")
# Test reading torque from this specific motor. It is now 1
values = motors_bus.read("Torque_Enable", "gripper")
assert len(values) == 1
assert values[0] == 1
# Test reading torque from all motors. It is 1 for the specific motor,
# and 0 on the others.
values = motors_bus.read("Torque_Enable")
gripper_index = motors_bus.motor_names.index("gripper")
assert values[gripper_index] == 1
assert values.sum() == 1 # gripper is the only motor to have torque 1
# Test writing torque on all motors and it is 1 for all.
motors_bus.write("Torque_Enable", 1)
values = motors_bus.read("Torque_Enable")
assert (values == 1).all()
# Test ordering the motors to move slightly (+1 value among 4096) and this move
# can be executed and seen by the motor position sensor
values = motors_bus.read("Present_Position")
motors_bus.write("Goal_Position", values + 1)
# Give time for the motors to move to the goal position
time.sleep(1)
new_values = motors_bus.read("Present_Position")
assert new_values == values
# TODO(rcadene): test calibration
# TODO(rcadene): test logs?

0
tests/test_robots.py Normal file
View File