Skip to content

Commit d5fe17a

Browse files
committed
fix(collision guard): hard sim update and identity action
- state of the real robot is setted to the sim in a hard way - support to set hard joint values in sim - if collision is detected the current robot state is set as action and not no action
1 parent d453279 commit d5fe17a

5 files changed

Lines changed: 17 additions & 11 deletions

File tree

python/rcsss/_core/sim.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class FR3(rcsss._core.common.Robot):
8989
def __init__(self, sim: Sim, id: str, ik: rcsss._core.common.IK) -> None: ...
9090
def get_parameters(self) -> FR3Config: ...
9191
def get_state(self) -> FR3State: ...
92+
def set_joints_hard(self, q: numpy.ndarray[typing.Literal[7], numpy.dtype[numpy.float64]]) -> None: ...
9293
def set_parameters(self, cfg: FR3Config) -> bool: ...
9394

9495
class FR3Config(rcsss._core.common.RConfig):

python/rcsss/envs/sim.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,18 @@ def __init__(
6868
self.sim.open_gui()
6969

7070
def step(self, action: dict[str, Any]) -> tuple[dict[str, Any], SupportsFloat, bool, bool, dict[str, Any]]:
71-
# TODO: we should set the state of the sim to the state of the real robot
71+
72+
self.collision_env.get_wrapper_attr("robot").set_joints_hard(self.unwrapped.robot.get_joint_position())
7273
_, _, _, _, info = self.collision_env.step(action)
74+
7375
if self.to_joint_control:
7476
fr3_env = self.collision_env.unwrapped
7577
assert isinstance(fr3_env, FR3Env), "Collision env must be an FR3Env instance."
7678
action[self.unwrapped.joints_key] = fr3_env.robot.get_joint_position()
7779

78-
# modify action to be joint angles down stream
79-
if info["collision"] or not info["ik_success"] or not info["is_sim_converged"]:
80-
# return old obs, with truncated and print warning
81-
self._logger.warning("Collision detected! Truncating episode: %s", info)
82-
if self.last_obs is None:
83-
msg = "Collisions detected and no old observation."
84-
raise RuntimeError(msg)
85-
old_obs, old_info = self.last_obs
86-
old_info.update(info)
87-
return old_obs, 0, False, True, old_info
80+
if info["collision"]:
81+
self._logger.warning("Collision detected! %s", info)
82+
action[self.unwrapped.joints_key] = self.unwrapped.robot.get_joint_position()
8883

8984
obs, reward, done, truncated, info = super().step(action)
9085
self.last_obs = obs, info

src/pybind/rcsss.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ PYBIND11_MODULE(_core, m) {
470470
py::arg("sim"), py::arg("id"), py::arg("ik"))
471471
.def("get_parameters", &rcs::sim::FR3::get_parameters)
472472
.def("set_parameters", &rcs::sim::FR3::set_parameters, py::arg("cfg"))
473+
.def("set_joints_hard", &rcs::sim::FR3::set_joints_hard, py::arg("q"))
473474
.def("get_state", &rcs::sim::FR3::get_state);
474475
py::enum_<rcs::sim::CameraType>(sim, "CameraType")
475476
.value("free", rcs::sim::CameraType::free)

src/sim/FR3.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,14 @@ void FR3::m_reset() {
208208
}
209209
}
210210

211+
void FR3::set_joints_hard(const common::Vector7d& q) {
212+
for (size_t i = 0; i < std::size(this->ids.joints); ++i) {
213+
size_t jnt_id = this->ids.joints[i];
214+
size_t jnt_qposadr = this->sim->m->jnt_qposadr[jnt_id];
215+
this->sim->d->qpos[jnt_qposadr] = q[i];
216+
}
217+
}
218+
211219
common::Pose FR3::get_base_pose_in_world_coordinates() {
212220
auto id = mj_name2id(this->sim->m, mjOBJ_BODY,
213221
(std::string("base_") + this->id).c_str());

src/sim/FR3.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class FR3 : public common::Robot {
5050
common::Pose get_base_pose_in_world_coordinates() override;
5151
std::optional<std::shared_ptr<common::IK>> get_ik() override;
5252
void reset() override;
53+
void set_joints_hard(const common::Vector7d &q);
5354

5455
private:
5556
FR3Config cfg;

0 commit comments

Comments
 (0)