Skip to content

Commit b495183

Browse files
committed
feat(sim): adds configureable kp and kv gains for sim robots
1 parent 2680bc9 commit b495183

4 files changed

Lines changed: 49 additions & 1 deletion

File tree

‎python/rcs/_core/sim.pyi‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,29 @@ class SimRobotConfig(rcs._core.common.RobotConfig[M]):
263263
"fr3_joint6",
264264
"fr3_joint7",
265265
],
266+
kp: list[float] | None = None,
267+
kv: list[float] | None = None,
266268
base: str = "base",
267269
dof: int = 7,
268270
joint_limits: numpy.ndarray[tuple[typing.Literal[2], M], numpy.dtype[numpy.float64]] = ...,
269271
) -> None: ...
270272
def add_prefix(self, id: str) -> None: ...
273+
@property
274+
def kp(self) -> list[float] | None:
275+
"""
276+
Per-joint position gains; None uses the MuJoCo XML values
277+
"""
278+
279+
@kp.setter
280+
def kp(self, arg0: list[float] | None) -> None: ...
281+
@property
282+
def kv(self) -> list[float] | None:
283+
"""
284+
Per-joint velocity gains; None uses the MuJoCo XML values
285+
"""
286+
287+
@kv.setter
288+
def kv(self, arg0: list[float] | None) -> None: ...
271289

272290
class SimRobotState(rcs._core.common.RobotState):
273291
def __init__(self) -> None: ...

‎src/pybind/rcs.cpp‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,9 @@ PYBIND11_MODULE(_core, m) {
543543
std::vector<std::string> arm_collision_geoms,
544544
std::vector<std::string> joints,
545545
std::optional<rcs::common::VectorXd> q_home,
546-
std::vector<std::string> actuators, std::string base,
546+
std::vector<std::string> actuators,
547+
std::optional<std::vector<double>> kp,
548+
std::optional<std::vector<double>> kv, std::string base,
547549
size_t dof,
548550
const Eigen::Matrix<double, 2, Eigen::Dynamic,
549551
Eigen::ColMajor>& joint_limits) {
@@ -559,6 +561,8 @@ PYBIND11_MODULE(_core, m) {
559561
config.arm_collision_geoms = arm_collision_geoms;
560562
config.joints = joints;
561563
config.actuators = actuators;
564+
config.kp = kp;
565+
config.kv = kv;
562566
config.base = base;
563567
config.dof = dof;
564568
config.joint_limits = joint_limits;
@@ -580,6 +584,7 @@ PYBIND11_MODULE(_core, m) {
580584
py::arg("joints") = default_simrobot_cfg.joints,
581585
py::arg("q_home") = default_simrobot_cfg.q_home,
582586
py::arg("actuators") = default_simrobot_cfg.actuators,
587+
py::arg("kp") = std::nullopt, py::arg("kv") = std::nullopt,
583588
py::arg("base") = default_simrobot_cfg.base,
584589
py::arg("dof") = default_simrobot_cfg.dof,
585590
py::arg("joint_limits") = default_simrobot_cfg.joint_limits)
@@ -594,6 +599,12 @@ PYBIND11_MODULE(_core, m) {
594599
&rcs::sim::SimRobotConfig::arm_collision_geoms)
595600
.def_readwrite("joints", &rcs::sim::SimRobotConfig::joints)
596601
.def_readwrite("actuators", &rcs::sim::SimRobotConfig::actuators)
602+
.def_readwrite(
603+
"kp", &rcs::sim::SimRobotConfig::kp,
604+
"Per-joint position gains; None uses the MuJoCo XML values")
605+
.def_readwrite(
606+
"kv", &rcs::sim::SimRobotConfig::kv,
607+
"Per-joint velocity gains; None uses the MuJoCo XML values")
597608
.def_readwrite("base", &rcs::sim::SimRobotConfig::base)
598609
.def_readwrite("dof", &rcs::sim::SimRobotConfig::dof)
599610
.def_readwrite("joint_limits", &rcs::sim::SimRobotConfig::joint_limits)

‎src/sim/SimRobot.cpp‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ SimRobot::SimRobot(std::shared_ptr<Sim> sim,
2929
bool register_convergence_callback)
3030
: sim{sim}, cfg{cfg}, state{}, m_ik(ik) {
3131
this->init_ids();
32+
this->set_config(cfg);
3233
if (register_convergence_callback) {
3334
this->sim->register_cb(std::bind(&SimRobot::is_arrived_callback, this),
3435
this->cfg.seconds_between_callbacks);
@@ -101,6 +102,22 @@ void SimRobot::init_ids() {
101102
bool SimRobot::set_config(const SimRobotConfig& cfg) {
102103
this->cfg = cfg;
103104
this->state.inverse_tcp_offset = cfg.tcp_offset.inverse();
105+
if (cfg.kp.has_value() != cfg.kv.has_value())
106+
throw std::runtime_error("kp and kv must both be set or unset");
107+
if (cfg.kp.has_value()) {
108+
size_t n = std::size(this->ids.actuators);
109+
if (cfg.kp->size() != n || cfg.kv->size() != n)
110+
throw std::runtime_error("kp/kv size must match number of joints");
111+
for (size_t i = 0; i < n; ++i) {
112+
int act = this->ids.actuators[i];
113+
if (this->sim->m->actuator_gaintype[act] != mjGAIN_FIXED ||
114+
this->sim->m->actuator_biastype[act] != mjBIAS_AFFINE)
115+
throw std::runtime_error("kp/kv require a position actuator");
116+
this->sim->m->actuator_gainprm[act * mjNGAIN + 0] = (*cfg.kp)[i];
117+
this->sim->m->actuator_biasprm[act * mjNBIAS + 1] = -(*cfg.kp)[i];
118+
this->sim->m->actuator_biasprm[act * mjNBIAS + 2] = -(*cfg.kv)[i];
119+
}
120+
}
104121
return true;
105122
}
106123

‎src/sim/SimRobot.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ struct SimRobotConfig : common::RobotConfig {
2929
"fr3_joint1", "fr3_joint2", "fr3_joint3", "fr3_joint4",
3030
"fr3_joint5", "fr3_joint6", "fr3_joint7",
3131
};
32+
std::optional<std::vector<double>> kp;
33+
std::optional<std::vector<double>> kv;
3234
std::string base = "base";
3335

3436
void add_prefix(const std::string& id) {

0 commit comments

Comments
 (0)