Skip to content

Commit e1ce3c6

Browse files
authored
Merge pull request #161 from utn-mi/juelg/example-and-test-linting
Adds python linting for examples and tests
2 parents 78dc791 + 68689c3 commit e1ce3c6

File tree

10 files changed

+65
-58
lines changed

10 files changed

+65
-58
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
PYSRC = python/rcsss
1+
PYSRC = python
22
CPPSRC = src
33
COMPILE_MODE = Release
44

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ ignore = [
101101
"PLR5501", # elif to reduce indentation
102102
"PT018", # assertion should be broken down into multiple parts
103103
"NPY002",
104+
"G004", # Logging format string does not contain any placeholders
104105
]
105106

106107
[tool.pylint.format]

python/examples/env_cartesian_control.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import logging
22

3-
from dotenv import dotenv_values
43
from rcsss.control.fr3_desk import FCI, Desk, DummyResourceManager
54
from rcsss.control.utils import load_creds_fr3_desk
65
from rcsss.envs.base import ControlMode, RelativeTo, RobotInstance
@@ -45,6 +44,7 @@
4544

4645

4746
def main():
47+
resource_manger: FCI | DummyResourceManager
4848
if ROBOT_INSTANCE == RobotInstance.HARDWARE:
4949
user, pw = load_creds_fr3_desk()
5050
resource_manger = FCI(Desk(ROBOT_IP, user, pw), unlock=False, lock_when_done=False)
@@ -60,7 +60,7 @@ def main():
6060
collision_guard="lab",
6161
gripper_cfg=default_fr3_hw_gripper_cfg(),
6262
max_relative_movement=0.5,
63-
relative_to=RelativeTo.LAST_STEP
63+
relative_to=RelativeTo.LAST_STEP,
6464
)
6565
else:
6666
env_rel = fr3_sim_env(
@@ -70,12 +70,12 @@ def main():
7070
gripper_cfg=default_fr3_sim_gripper_cfg(),
7171
camera_set_cfg=default_mujoco_cameraset_cfg(),
7272
max_relative_movement=0.5,
73-
relative_to=RelativeTo.LAST_STEP
73+
relative_to=RelativeTo.LAST_STEP,
7474
)
7575
env_rel.get_wrapper_attr("sim").open_gui()
7676

7777
env_rel.reset()
78-
print(env_rel.unwrapped.robot.get_cartesian_position())
78+
print(env_rel.unwrapped.robot.get_cartesian_position()) # type: ignore
7979

8080
for _ in range(10):
8181
for _ in range(10):

python/examples/env_joint_control.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import logging
22

3-
import mujoco
43
import numpy as np
5-
import rcsss
6-
from dotenv import dotenv_values
74
from rcsss.control.fr3_desk import FCI, Desk, DummyResourceManager
85
from rcsss.control.utils import load_creds_fr3_desk
96
from rcsss.envs.base import ControlMode, RelativeTo, RobotInstance
@@ -48,6 +45,7 @@
4845

4946

5047
def main():
48+
resource_manger: FCI | DummyResourceManager
5149
if ROBOT_INSTANCE == RobotInstance.HARDWARE:
5250
user, pw = load_creds_fr3_desk()
5351
resource_manger = FCI(Desk(ROBOT_IP, user, pw), unlock=False, lock_when_done=False)

python/examples/fr3.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import logging
22
import sys
3-
from time import sleep
43

54
import numpy as np
65
import rcsss
7-
from dotenv import dotenv_values
86
from rcsss import sim
97
from rcsss._core.hw import FR3Config, IKController
108
from rcsss._core.sim import CameraType
@@ -57,49 +55,54 @@ def main():
5755
if "lab" not in rcsss.scenes:
5856
logger.error("This pip package was not built with the UTN lab models, aborting.")
5957
sys.exit()
58+
resource_manger: FCI | DummyResourceManager
6059
if ROBOT_INSTANCE == RobotInstance.HARDWARE:
6160
user, pw = load_creds_fr3_desk()
6261
resource_manger = FCI(Desk(ROBOT_IP, user, pw), unlock=False, lock_when_done=False)
6362
else:
6463
resource_manger = DummyResourceManager()
6564

6665
with resource_manger:
66+
robot: rcsss.common.Robot
67+
gripper: rcsss.common.Gripper
6768
if ROBOT_INSTANCE == RobotInstance.SIMULATION:
6869
simulation = sim.Sim(rcsss.scenes["fr3_empty_world"])
6970
urdf_path = get_urdf_path(URDF_PATH, allow_none_if_not_found=False)
71+
assert urdf_path is not None
7072
ik = rcsss.common.IK(urdf_path)
7173
robot = rcsss.sim.FR3(simulation, "0", ik)
7274
cfg = sim.FR3Config()
7375
cfg.tcp_offset = rcsss.common.Pose(rcsss.common.FrankaHandTCPOffset())
7476
cfg.realtime = False
7577
robot.set_parameters(cfg)
7678

77-
gripper_cfg = sim.FHConfig()
78-
gripper = sim.FrankaHand(simulation, "0", gripper_cfg)
79+
gripper_cfg_sim = sim.FHConfig()
80+
gripper = sim.FrankaHand(simulation, "0", gripper_cfg_sim)
7981

8082
# add camera to have a rendering gui
8183
cameras = {
8284
"default_free": SimCameraConfig(identifier="", type=int(CameraType.default_free)),
8385
"wrist": SimCameraConfig(identifier="eye-in-hand_0", type=int(CameraType.fixed)),
8486
}
8587
cam_cfg = SimCameraSetConfig(cameras=cameras, resolution_width=1280, resolution_height=720, frame_rate=20)
86-
camera_set = SimCameraSet(simulation, cam_cfg)
88+
camera_set = SimCameraSet(simulation, cam_cfg) # noqa: F841
8789
simulation.open_gui()
8890

8991
else:
9092
urdf_path = get_urdf_path(URDF_PATH, allow_none_if_not_found=False)
93+
assert urdf_path is not None
9194
ik = rcsss.common.IK(urdf_path)
92-
robot = rcsss.hw.FR3(ROBOT_IP, str(rcsss.scenes["lab"].parent / "fr3.urdf"), ik)
95+
robot = rcsss.hw.FR3(ROBOT_IP, ik)
9396
robot_cfg = FR3Config()
9497
robot_cfg.tcp_offset = rcsss.common.Pose(rcsss.common.FrankaHandTCPOffset())
9598
robot_cfg.controller = IKController.robotics_library
9699
robot.set_parameters(robot_cfg)
97100

98-
gripper_cfg = rcsss.hw.FHConfig()
99-
gripper_cfg.epsilon_inner = gripper_cfg.epsilon_outer = 0.1
100-
gripper_cfg.speed = 0.1
101-
gripper_cfg.force = 30
102-
gripper = rcsss.hw.FrankaHand(ROBOT_IP, gripper_cfg)
101+
gripper_cfg_hw = rcsss.hw.FHConfig()
102+
gripper_cfg_hw.epsilon_inner = gripper_cfg_hw.epsilon_outer = 0.1
103+
gripper_cfg_hw.speed = 0.1
104+
gripper_cfg_hw.force = 30
105+
gripper = rcsss.hw.FrankaHand(ROBOT_IP, gripper_cfg_hw)
103106
input("the robot is going to move, press enter whenever you are ready")
104107

105108
# move to home position and open gripper
@@ -115,7 +118,7 @@ def main():
115118
)
116119
if ROBOT_INSTANCE == RobotInstance.SIMULATION:
117120
simulation.step_until_convergence()
118-
logger.debug(f"IK success: {robot.get_state().ik_success}")
121+
logger.debug(f"IK success: {robot.get_state().ik_success}") # type: ignore
119122
logger.debug(f"sim converged: {simulation.is_converged()}")
120123

121124
# 5cm in y direction
@@ -124,7 +127,7 @@ def main():
124127
)
125128
if ROBOT_INSTANCE == RobotInstance.SIMULATION:
126129
simulation.step_until_convergence()
127-
logger.debug(f"IK success: {robot.get_state().ik_success}")
130+
logger.debug(f"IK success: {robot.get_state().ik_success}") # type: ignore
128131
logger.debug(f"sim converged: {simulation.is_converged()}")
129132

130133
# 5cm in z direction
@@ -133,7 +136,7 @@ def main():
133136
)
134137
if ROBOT_INSTANCE == RobotInstance.SIMULATION:
135138
simulation.step_until_convergence()
136-
logger.debug(f"IK success: {robot.get_state().ik_success}")
139+
logger.debug(f"IK success: {robot.get_state().ik_success}") # type: ignore
137140
logger.debug(f"sim converged: {simulation.is_converged()}")
138141

139142
# rotate the arm 90 degrees around the inverted y and z axis
@@ -143,7 +146,7 @@ def main():
143146
robot.set_cartesian_position(new_pose)
144147
if ROBOT_INSTANCE == RobotInstance.SIMULATION:
145148
simulation.step_until_convergence()
146-
logger.debug(f"IK success: {robot.get_state().ik_success}")
149+
logger.debug(f"IK success: {robot.get_state().ik_success}") # type: ignore
147150
logger.debug(f"sim converged: {simulation.is_converged()}")
148151

149152
if ROBOT_INSTANCE == RobotInstance.HARDWARE:
@@ -157,7 +160,7 @@ def main():
157160
)
158161
if ROBOT_INSTANCE == RobotInstance.SIMULATION:
159162
simulation.step_until_convergence()
160-
logger.debug(f"IK success: {robot.get_state().ik_success}")
163+
logger.debug(f"IK success: {robot.get_state().ik_success}") # type: ignore
161164
logger.debug(f"sim converged: {simulation.is_converged()}")
162165

163166
# grasp the object
@@ -172,7 +175,7 @@ def main():
172175
)
173176
if ROBOT_INSTANCE == RobotInstance.SIMULATION:
174177
simulation.step_until_convergence()
175-
logger.debug(f"IK success: {robot.get_state().ik_success}")
178+
logger.debug(f"IK success: {robot.get_state().ik_success}") # type: ignore
176179
logger.debug(f"sim converged: {simulation.is_converged()}")
177180

178181
if ROBOT_INSTANCE == RobotInstance.HARDWARE:

python/examples/grasp_demo.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import logging
2+
from typing import Any, cast
23

34
import gymnasium as gym
45
import mujoco
56
import numpy as np
67
from rcsss._core.common import Pose
7-
from rcsss._core.sim import FR3State
8-
from rcsss.envs.base import GripperWrapper
8+
from rcsss.envs.base import FR3Env, GripperWrapper
99

1010
logger = logging.getLogger(__name__)
1111
logger.setLevel(logging.INFO)
@@ -14,9 +14,10 @@
1414
class PickUpDemo:
1515
def __init__(self, env: gym.Env):
1616
self.env = env
17-
self.home_pose = self.env.unwrapped.robot.get_cartesian_position()
17+
self.unwrapped: FR3Env = cast(FR3Env, self.env.unwrapped)
18+
self.home_pose = self.unwrapped.robot.get_cartesian_position()
1819

19-
def _action(self, pose: Pose, gripper: float) -> np.ndarray:
20+
def _action(self, pose: Pose, gripper: float) -> dict[str, Any]:
2021
return {"xyzrpy": pose.xyzrpy(), "gripper": gripper}
2122

2223
def get_object_pose(self, geom_name) -> Pose:
@@ -33,22 +34,19 @@ def generate_waypoints(self, start_pose: Pose, end_pose: Pose, num_waypoints: in
3334
waypoints.append(start_pose.interpolate(end_pose, t))
3435
return waypoints
3536

36-
def step(self, action: np.ndarray) -> dict:
37-
re = self.env.step(action)
38-
s: FR3State = self.env.unwrapped.robot.get_state()
39-
return re[0]
37+
def step(self, action: dict) -> dict:
38+
return self.env.step(action)[0]
4039

4140
def plan_linear_motion(self, geom_name: str, delta_up: float, num_waypoints: int = 20) -> list[Pose]:
42-
end_eff_pose = self.env.unwrapped.robot.get_cartesian_position()
41+
end_eff_pose = self.unwrapped.robot.get_cartesian_position()
4342

4443
goal_pose = self.get_object_pose(geom_name=geom_name)
4544
# goal pose is above the object and gripper coordinate must flip z-axis (end effector base rotation is [1, 0, 0, 0])
4645
# be careful we define identity quaternion as as [0, 0, 0, 1]
4746
# this does not work if the object is flipped
48-
goal_pose *= Pose(translation=[0, 0, delta_up], quaternion=[1, 0, 0, 0])
47+
goal_pose *= Pose(translation=np.array([0, 0, delta_up]), quaternion=np.array([1, 0, 0, 0]))
4948

50-
waypoints = self.generate_waypoints(end_eff_pose, goal_pose, num_waypoints=num_waypoints)
51-
return waypoints
49+
return self.generate_waypoints(end_eff_pose, goal_pose, num_waypoints=num_waypoints)
5250

5351
def execute_motion(self, waypoints: list[Pose], gripper: float = GripperWrapper.BINARY_GRIPPER_OPEN) -> dict:
5452
for i in range(1, len(waypoints)):
@@ -72,7 +70,7 @@ def grasp(self, geom_name: str):
7270
self.execute_motion(waypoints=waypoints, gripper=GripperWrapper.BINARY_GRIPPER_CLOSED)
7371

7472
def move_home(self):
75-
end_eff_pose = self.env.unwrapped.robot.get_cartesian_position()
73+
end_eff_pose = self.unwrapped.robot.get_cartesian_position()
7674
waypoints = self.generate_waypoints(end_eff_pose, self.home_pose, num_waypoints=10)
7775
self.execute_motion(waypoints=waypoints, gripper=GripperWrapper.BINARY_GRIPPER_CLOSED)
7876

python/rcsss/envs/factories.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
ControlMode,
1919
FR3Env,
2020
GripperWrapper,
21-
LimitedJointsRelDictType,
22-
ObsArmsGrCam,
2321
RelativeActionSpace,
2422
RelativeTo,
2523
)
@@ -186,7 +184,7 @@ def fr3_sim_env(
186184
urdf_path: str | PathLike | None = None,
187185
mjcf: str | PathLike = "fr3_empty_world",
188186
sim_wrapper: Type[SimWrapper] | None = None,
189-
) -> gym.Env[ObsArmsGrCam, LimitedJointsRelDictType]:
187+
) -> gym.Env:
190188
"""
191189
Creates a simulation environment for the FR3 robot.
192190

python/tests/test_common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class TestPose:
1010
This class tests the methods of the Pose class and its multiple constructors.
1111
"""
1212

13-
@pytest.fixture
13+
@pytest.fixture()
1414
def identity_pose(self):
1515
"""This fixture can be reused wherever if no transformation pose is needed"""
1616
return common.Pose()

python/tests/test_envs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class Composed(AdvancedNestedSpaceWithLambda, SimpleSpace): ...
7777
class TestGetSpace:
7878

7979
def test_simple_space(self):
80-
get_space(SimpleSpace) == gym.spaces.Dict(
80+
assert get_space(SimpleSpace) == gym.spaces.Dict(
8181
{
8282
"my_int": gym.spaces.Discrete(1),
8383
"my_float": gym.spaces.Box(low=0, high=1, shape=(1,), dtype=np.float32),
@@ -196,4 +196,4 @@ def test_composed_space(self):
196196
)
197197

198198
def test_get_space_keys(self):
199-
get_space_keys(SimpleSpace) == {"my_int", "my_float"}
199+
assert set(get_space_keys(SimpleSpace)) == {"my_int", "my_float"}

0 commit comments

Comments
 (0)