Skip to content

Commit

Permalink
fix: typing
Browse files Browse the repository at this point in the history
  • Loading branch information
engnadeau committed Aug 23, 2022
1 parent da475f5 commit 9b1fecd
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions tests/test_robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import hypothesis
import numpy as np
import numpy.typing as npt
from hypothesis import given
from hypothesis.extra.numpy import arrays
from hypothesis.strategies import floats
Expand All @@ -14,7 +15,7 @@
from pybotics.robot import Robot


def test_fk(resources_path: Path):
def test_fk(resources_path: Path) -> None:
"""
Test robot.
Expand All @@ -23,7 +24,7 @@ def test_fk(resources_path: Path):
"""
# get resource
path = resources_path / "ur10-joints-poses.csv"
data = np.loadtxt(str(path), delimiter=",")
data = np.loadtxt(str(path), delimiter=",") # type: ignore

# load robot
robot = Robot.from_parameters(ur10())
Expand All @@ -38,23 +39,23 @@ def test_fk(resources_path: Path):

# test with position argument
actual_pose = robot.fk(q=joints)
np.testing.assert_allclose(actual_pose, desired_pose, atol=atol)
np.testing.assert_allclose(actual_pose, desired_pose, atol=atol) # type: ignore

# test with internal position attribute
robot.joints = joints
actual_pose = robot.fk()
np.testing.assert_allclose(actual_pose, desired_pose, atol=atol)
np.testing.assert_allclose(actual_pose, desired_pose, atol=atol) # type: ignore


def test_home_position():
def test_home_position() -> None:
"""Test."""
robot = Robot.from_parameters(ur10())
x = np.ones(len(robot))
robot.home_position = x
np.testing.assert_allclose(robot.home_position, x)
np.testing.assert_allclose(robot.home_position, x) # type: ignore


def test_joint_limits():
def test_joint_limits() -> None:
"""Test."""
robot = Robot.from_parameters(ur10())

Expand All @@ -69,7 +70,7 @@ def test_joint_limits():
robot.joints = robot.joint_limits.copy()[1] + 10


def test_compute_joint_torques(planar_robot: Robot):
def test_compute_joint_torques(planar_robot: Robot) -> None:
"""
Test.
Expand All @@ -79,9 +80,9 @@ def test_compute_joint_torques(planar_robot: Robot):
:return:
"""
# set test force and angles
force = [-100, -200, 0]
moment = [0] * 3
wrench = force + moment
force = np.array([-100, -200, 0])
moment = np.zeros(3)
wrench = force + moment # type: npt.NDArray[np.float64]
joint_angles = np.deg2rad([30, 60, 0])

# get link lengths
Expand All @@ -102,11 +103,11 @@ def test_compute_joint_torques(planar_robot: Robot):

# test
actual_torques = planar_robot.compute_joint_torques(q=joint_angles, wrench=wrench)
np.testing.assert_allclose(actual_torques, expected_torques)
np.testing.assert_allclose(actual_torques, expected_torques) # type: ignore

planar_robot.joints = joint_angles
actual_torques = planar_robot.compute_joint_torques(wrench=wrench)
np.testing.assert_allclose(actual_torques, expected_torques)
np.testing.assert_allclose(actual_torques, expected_torques) # type: ignore


@given(
Expand All @@ -118,7 +119,7 @@ def test_compute_joint_torques(planar_robot: Robot):
),
)
)
def test_jacobian_world(q: npt.NDArray[np.float64], planar_robot: Robot):
def test_jacobian_world(q: npt.NDArray[np.float64], planar_robot: Robot) -> None:
"""Test."""
# get link lengths
# FIXME: "Link" has no attribute "a"
Expand All @@ -145,15 +146,15 @@ def test_jacobian_world(q: npt.NDArray[np.float64], planar_robot: Robot):
expected[-1, :] = 1

actual = planar_robot.jacobian_world(q)
np.testing.assert_allclose(actual, expected, atol=1e-3)
np.testing.assert_allclose(actual, expected, atol=1e-3) # type: ignore


@given(
q=arrays(
shape=(3,), dtype=float, elements=floats(allow_nan=False, allow_infinity=False)
)
)
def test_jacobian_flange(q: npt.NDArray[np.float64], planar_robot: Robot):
def test_jacobian_flange(q: npt.NDArray[np.float64], planar_robot: Robot) -> None:
"""Test."""
# get link lengths
# FIXME: "Link" has no attribute "a"
Expand All @@ -176,7 +177,7 @@ def test_jacobian_flange(q: npt.NDArray[np.float64], planar_robot: Robot):
expected[-1, :] = 1

actual = planar_robot.jacobian_flange(q)
np.testing.assert_allclose(actual, expected, atol=1e-6)
np.testing.assert_allclose(actual, expected, atol=1e-6) # type: ignore


@given(
Expand All @@ -199,7 +200,7 @@ def test_jacobian_flange(q: npt.NDArray[np.float64], planar_robot: Robot):
),
)
@hypothesis.settings(deadline=None)
def test_ik(q: npt.NDArray[np.float64], q_offset: npt.NDArray[np.float64]):
def test_ik(q: npt.NDArray[np.float64], q_offset: npt.NDArray[np.float64]) -> None:
"""Test."""
robot = Robot.from_parameters(ur10())
pose = robot.fk(q)
Expand All @@ -216,22 +217,22 @@ def test_ik(q: npt.NDArray[np.float64], q_offset: npt.NDArray[np.float64]):

# test the matrix with lower accuracy
# rotation components are hard to achieve when x0 isn't good
np.testing.assert_allclose(actual_pose, pose, atol=1)
np.testing.assert_allclose(actual_pose, pose, atol=1) # type: ignore

# test the position with higher accuracy
desired_position = pose[:-1, -1]
actual_position = actual_pose[:-1, -1]
np.testing.assert_allclose(actual_position, desired_position, atol=1e-1)
np.testing.assert_allclose(actual_position, desired_position, atol=1e-1) # type: ignore


def test_random_joints():
def test_random_joints() -> None:
"""Test."""
robot = Robot.from_parameters(ur10())
robot.random_joints()
robot.random_joints(in_place=True)


def test_to_json():
def test_to_json() -> None:
"""Test."""
robot = Robot.from_parameters(ur10())
robot.to_json()

0 comments on commit 9b1fecd

Please sign in to comment.