Skip to content

Commit c79f892

Browse files
committed
refactor(sim): generalize gripper to use a single joint
1 parent 09b42c7 commit c79f892

File tree

4 files changed

+11
-22
lines changed

4 files changed

+11
-22
lines changed

python/rcs/_core/sim.pyi

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ class SimGripperConfig(rcs._core.common.GripperConfig):
116116
epsilon_inner: float
117117
epsilon_outer: float
118118
ignored_collision_geoms: list[str]
119-
joint1: str
120-
joint2: str
119+
joint: str
121120
seconds_between_callbacks: float
122121
def __init__(self) -> None: ...
123122
def add_id(self, id: str) -> None: ...

src/pybind/rcs.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,8 +467,7 @@ PYBIND11_MODULE(_core, m) {
467467
&rcs::sim::SimGripperConfig::collision_geoms)
468468
.def_readwrite("collision_geoms_fingers",
469469
&rcs::sim::SimGripperConfig::collision_geoms_fingers)
470-
.def_readwrite("joint1", &rcs::sim::SimGripperConfig::joint1)
471-
.def_readwrite("joint2", &rcs::sim::SimGripperConfig::joint2)
470+
.def_readwrite("joint", &rcs::sim::SimGripperConfig::joint)
472471
.def_readwrite("actuator", &rcs::sim::SimGripperConfig::actuator)
473472
.def("add_id", &rcs::sim::SimGripperConfig::add_id, py::arg("id"));
474473
py::class_<rcs::sim::SimGripperState, rcs::common::GripperState>(

src/sim/SimGripper.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,10 @@ SimGripper::SimGripper(std::shared_ptr<Sim> sim, const SimGripperConfig &cfg)
2121
}
2222
// this->tendon_id =
2323
// mj_name2id(this->sim->m, mjOBJ_TENDON, ("split_" + id).c_str());
24-
this->joint_id_1 = this->sim->m->jnt_qposadr[mj_name2id(
25-
this->sim->m, mjOBJ_JOINT, this->cfg.joint1.c_str())];
26-
if (this->joint_id_1 == -1) {
27-
throw std::runtime_error(std::string("No joint named " + this->cfg.joint1));
28-
}
29-
this->joint_id_2 = this->sim->m->jnt_qposadr[mj_name2id(
30-
this->sim->m, mjOBJ_JOINT, this->cfg.joint2.c_str())];
31-
if (this->joint_id_2 == -1) {
32-
throw std::runtime_error(std::string("No joint named " + this->cfg.joint2));
24+
this->joint_id = this->sim->m->jnt_qposadr[mj_name2id(
25+
this->sim->m, mjOBJ_JOINT, this->cfg.joint.c_str())];
26+
if (this->joint_id == -1) {
27+
throw std::runtime_error(std::string("No joint named " + this->cfg.joint));
3328
}
3429
// Collision geoms
3530
this->add_collision_geoms(this->cfg.collision_geoms, this->cgeom, false);
@@ -95,7 +90,7 @@ void SimGripper::set_normalized_width(double width, double force) {
9590
double SimGripper::get_normalized_width() {
9691
// TODO: maybe we should use the mujoco sensors? Not sure what the difference
9792
// is between reading out from qpos and reading from the sensors.
98-
double width = this->sim->d->qpos[this->joint_id_1] / this->MAX_JOINT_WIDTH;
93+
double width = this->sim->d->qpos[this->joint_id] / this->MAX_JOINT_WIDTH;
9994
// sometimes the joint is slightly outside of the bounds
10095
if (width < 0) {
10196
width = 0;
@@ -157,8 +152,7 @@ void SimGripper::m_reset() {
157152
this->state = SimGripperState();
158153
this->state.max_unnormalized_width = this->MAX_WIDTH;
159154
// reset state hard
160-
this->sim->d->qpos[this->joint_id_1] = this->MAX_JOINT_WIDTH;
161-
this->sim->d->qpos[this->joint_id_2] = this->MAX_JOINT_WIDTH;
155+
this->sim->d->qpos[this->joint_id] = this->MAX_JOINT_WIDTH;
162156
this->sim->d->ctrl[this->actuator_id] = this->MAX_WIDTH;
163157
}
164158

src/sim/SimGripper.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ struct SimGripperConfig : common::GripperConfig {
2323

2424
std::vector<std::string> collision_geoms_fingers{"finger_0_left",
2525
"finger_0_right"};
26-
std::string joint1 = "finger_joint1";
27-
std::string joint2 = "finger_joint2";
26+
std::string joint = "finger_joint1";
2827
std::string actuator = "actuator8";
2928

3029
void add_id(const std::string &id) {
@@ -37,8 +36,7 @@ struct SimGripperConfig : common::GripperConfig {
3736
for (auto &s : this->ignored_collision_geoms) {
3837
s = s + "_" + id;
3938
}
40-
this->joint1 = this->joint1 + "_" + id;
41-
this->joint2 = this->joint2 + "_" + id;
39+
this->joint = this->joint + "_" + id;
4240
this->actuator = this->actuator + "_" + id;
4341
}
4442
};
@@ -58,8 +56,7 @@ class SimGripper : public common::Gripper {
5856
SimGripperConfig cfg;
5957
std::shared_ptr<Sim> sim;
6058
int actuator_id;
61-
int joint_id_1;
62-
int joint_id_2;
59+
int joint_id;
6360
SimGripperState state;
6461
bool convergence_callback();
6562
bool collision_callback();

0 commit comments

Comments
 (0)