Skip to content

Commit 293d720

Browse files
committed
refactor(ik): abstract ik class with rl subclass
1 parent 60f8eb8 commit 293d720

File tree

10 files changed

+51
-39
lines changed

10 files changed

+51
-39
lines changed

python/examples/fr3.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def main():
6969
simulation = sim.Sim(rcsss.scenes["fr3_empty_world"])
7070
urdf_path = get_urdf_path(URDF_PATH, allow_none_if_not_found=False)
7171
assert urdf_path is not None
72-
ik = rcsss.common.IK(urdf_path)
72+
ik = rcsss.common.RL(urdf_path)
7373
robot = rcsss.sim.FR3(simulation, "0", ik)
7474
cfg = sim.FR3Config()
7575
cfg.tcp_offset = rcsss.common.Pose(rcsss.common.FrankaHandTCPOffset())
@@ -91,7 +91,7 @@ def main():
9191
else:
9292
urdf_path = get_urdf_path(URDF_PATH, allow_none_if_not_found=False)
9393
assert urdf_path is not None
94-
ik = rcsss.common.IK(urdf_path)
94+
ik = rcsss.common.RL(urdf_path)
9595
robot = rcsss.hw.FR3(ROBOT_IP, ik)
9696
robot_cfg = FR3Config()
9797
robot_cfg.tcp_offset = rcsss.common.Pose(rcsss.common.FrankaHandTCPOffset())

python/rcsss/_core/common.pyi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ __all__ = [
2121
"NRobotsWithGripper",
2222
"Pose",
2323
"RConfig",
24+
"RL",
2425
"RPY",
2526
"RState",
2627
"Robot",
@@ -45,7 +46,6 @@ class Gripper:
4546
def shut(self) -> None: ...
4647

4748
class IK:
48-
def __init__(self, urdf_path: str, max_duration_ms: int = 300) -> None: ...
4949
def ik(
5050
self, pose: Pose, q0: numpy.ndarray[typing.Literal[7], numpy.dtype[numpy.float64]], tcp_offset: Pose = ...
5151
) -> numpy.ndarray[typing.Literal[7], numpy.dtype[numpy.float64]] | None: ...
@@ -128,6 +128,9 @@ class Pose:
128128
class RConfig:
129129
pass
130130

131+
class RL(IK):
132+
def __init__(self, urdf_path: str, max_duration_ms: int = 300) -> None: ...
133+
131134
class RPY:
132135
pitch: float
133136
roll: float

python/rcsss/control/record.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def __init__(
194194
poses: list[Pose] | None = None,
195195
urdf_path: str | None = None,
196196
):
197-
self.ik = common.IK(urdf_path) if urdf_path else None
197+
self.ik = common.RL(urdf_path) if urdf_path else None
198198
self.r: dict[str, hw.FR3] = {key: hw.FR3(ip, self.ik) for key, ip in name2ip.items()}
199199
# TODO: this config should be given to the constructor
200200
cfg = hw.FHConfig()

python/rcsss/envs/factories.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def fr3_hw_env(
113113
gym.Env: The configured hardware environment for the FR3 robot.
114114
"""
115115
urdf_path = get_urdf_path(urdf_path, allow_none_if_not_found=collision_guard is not None)
116-
ik = rcsss.common.IK(str(urdf_path)) if urdf_path is not None else None
116+
ik = rcsss.common.RL(str(urdf_path)) if urdf_path is not None else None
117117
robot = rcsss.hw.FR3(ip, ik)
118118
robot.set_parameters(robot_cfg)
119119

@@ -213,7 +213,7 @@ def fr3_sim_env(
213213
logger.warning("mjcf not found as key in scenes, interpreting mjcf as path the mujoco scene xml")
214214

215215
simulation = sim.Sim(rcsss.scenes.get(str(mjcf), mjcf))
216-
ik = rcsss.common.IK(urdf_path)
216+
ik = rcsss.common.RL(urdf_path)
217217
robot = rcsss.sim.FR3(simulation, "0", ik)
218218
robot.set_parameters(robot_cfg)
219219
env: gym.Env = FR3Env(robot, control_mode)

python/rcsss/envs/sim.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def env_from_xml_paths(
137137
) -> "CollisionGuard":
138138
assert isinstance(env.unwrapped, FR3Env)
139139
simulation = sim.Sim(mjmld)
140-
ik = rcsss.common.IK(urdf, max_duration_ms=300)
140+
ik = rcsss.common.RL(urdf, max_duration_ms=300)
141141
robot = rcsss.sim.FR3(simulation, id, ik)
142142
cfg = sim.FR3Config()
143143
cfg.realtime = False

src/common/IK.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,31 @@
1515
namespace rcs {
1616
namespace common {
1717

18-
IK::IK(const std::string& urdf_path, size_t max_duration_ms) : rl() {
19-
this->rl = RL();
20-
21-
this->rl.mdl = rl::mdl::UrdfFactory().create(urdf_path);
22-
this->rl.kin = std::dynamic_pointer_cast<rl::mdl::Kinematic>(this->rl.mdl);
23-
this->rl.ik =
24-
std::make_shared<rl::mdl::JacobianInverseKinematics>(this->rl.kin.get());
25-
this->rl.ik->setRandomRestarts(0);
26-
this->rl.ik->setEpsilon(1e-3);
27-
this->rl.ik->setDuration(std::chrono::milliseconds(max_duration_ms));
18+
RL::RL(const std::string& urdf_path, size_t max_duration_ms) : rl_data() {
19+
this->rl_data.mdl = rl::mdl::UrdfFactory().create(urdf_path);
20+
this->rl_data.kin =
21+
std::dynamic_pointer_cast<rl::mdl::Kinematic>(this->rl_data.mdl);
22+
this->rl_data.ik = std::make_shared<rl::mdl::JacobianInverseKinematics>(
23+
this->rl_data.kin.get());
24+
this->rl_data.ik->setRandomRestarts(0);
25+
this->rl_data.ik->setEpsilon(1e-3);
26+
this->rl_data.ik->setDuration(std::chrono::milliseconds(max_duration_ms));
2827
}
2928

30-
std::optional<Vector7d> IK::ik(const Pose& pose, const Vector7d& q0,
29+
std::optional<Vector7d> RL::ik(const Pose& pose, const Vector7d& q0,
3130
const Pose& tcp_offset) {
3231
// pose is assumed to be in the robots coordinate frame
33-
this->rl.kin->setPosition(q0);
34-
this->rl.kin->forwardPosition();
32+
this->rl_data.kin->setPosition(q0);
33+
this->rl_data.kin->forwardPosition();
3534
rcs::common::Pose new_pose = pose * tcp_offset.inverse();
3635

37-
this->rl.ik->addGoal(new_pose.affine_matrix(), 0);
38-
bool success = this->rl.ik->solve();
36+
this->rl_data.ik->addGoal(new_pose.affine_matrix(), 0);
37+
bool success = this->rl_data.ik->solve();
3938
if (success) {
4039
// is this forward needed and is it mabye possible to call
4140
// this on the model?
42-
this->rl.kin->forwardPosition();
43-
return this->rl.kin->getPosition();
41+
this->rl_data.kin->forwardPosition();
42+
return this->rl_data.kin->getPosition();
4443
} else {
4544
return std::nullopt;
4645
}

src/common/IK.h

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,29 @@
1515
namespace rcs {
1616
namespace common {
1717

18-
struct RL {
19-
std::shared_ptr<rl::mdl::Model> mdl;
20-
std::shared_ptr<rl::mdl::Kinematic> kin;
21-
std::shared_ptr<rl::mdl::JacobianInverseKinematics> ik;
18+
class IK {
19+
public:
20+
virtual ~IK(){};
21+
virtual std::optional<Vector7d> ik(
22+
const Pose& pose, const Vector7d& q0,
23+
const Pose& tcp_offset = Pose::Identity()) = 0;
2224
};
2325

24-
class IK {
26+
class RL : public IK {
2527
private:
26-
RL rl;
28+
struct RLData {
29+
std::shared_ptr<rl::mdl::Model> mdl;
30+
std::shared_ptr<rl::mdl::Kinematic> kin;
31+
std::shared_ptr<rl::mdl::JacobianInverseKinematics> ik;
32+
};
33+
RLData rl_data;
2734

2835
public:
29-
IK(const std::string& urdf_path, size_t max_duration_ms = 300);
30-
std::optional<Vector7d> ik(const Pose& pose, const Vector7d& q0,
31-
const Pose& tcp_offset = Pose::Identity());
32-
33-
// TODO: set max time
36+
RL(const std::string& urdf_path, size_t max_duration_ms = 300);
37+
~RL() override = default;
38+
std::optional<Vector7d> ik(
39+
const Pose& pose, const Vector7d& q0,
40+
const Pose& tcp_offset = Pose::Identity()) override;
3441
};
3542
} // namespace common
3643
} // namespace rcs

src/hw/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const string urdf_path = "models/urdf/fr3.urdf";
1717

1818
int main() {
1919
try {
20-
auto ik = make_shared<rcs::common::IK>(urdf_path);
20+
auto ik = make_shared<rcs::common::RL>(urdf_path);
2121
rcs::hw::FR3 robot(ip, ik);
2222
robot.automatic_error_recovery();
2323
std::cout << "WARNING: This example will move the robot! "

src/pybind/rcsss.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,14 @@ PYBIND11_MODULE(_core, m) {
212212
}));
213213

214214
py::class_<rcs::common::IK, std::shared_ptr<rcs::common::IK>>(common, "IK")
215-
.def(py::init<const std::string &, size_t>(), py::arg("urdf_path"),
216-
py::arg("max_duration_ms") = 300)
217215
.def("ik", &rcs::common::IK::ik, py::arg("pose"), py::arg("q0"),
218216
py::arg("tcp_offset") = rcs::common::Pose::Identity());
219217

218+
py::class_<rcs::common::RL, rcs::common::IK,
219+
std::shared_ptr<rcs::common::RL>>(common, "RL")
220+
.def(py::init<const std::string &, size_t>(), py::arg("urdf_path"),
221+
py::arg("max_duration_ms") = 300);
222+
220223
py::class_<rcs::common::RConfig>(common, "RConfig");
221224
py::class_<rcs::common::RState>(common, "RState");
222225
py::class_<rcs::common::GConfig>(common, "GConfig");

src/sim/test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ int test_sim() {
126126
sim->set_config(cfg);
127127
std::string id = "0";
128128

129-
auto ik = std::make_shared<rcs::common::IK>(urdf);
129+
auto ik = std::make_shared<rcs::common::RL>(urdf);
130130
auto fr3 = rcs::sim::FR3(sim, id, ik);
131131
auto tcp_offset = rcs::common::Pose(rcs::common::FrankaHandTCPOffset());
132132
rcs::sim::FR3Config fr3_config = *fr3.get_parameters();

0 commit comments

Comments
 (0)