Add functions for setting an mjsActuator to a specific shortcut type.
PiperOrigin-RevId: 769049331 Change-Id: If5bb04966bd29a57569a2c17d712b9ae3d33324d
This commit is contained in:
committed by
Copybara-Service
parent
dd18dd2024
commit
f75772587a
@@ -9551,6 +9551,266 @@ FUNCTIONS: Mapping[str, FunctionDecl] = dict([
|
||||
),
|
||||
doc='Add default.',
|
||||
)),
|
||||
('mjs_setToMotor',
|
||||
FunctionDecl(
|
||||
name='mjs_setToMotor',
|
||||
return_type=PointerType(
|
||||
inner_type=ValueType(name='char', is_const=True),
|
||||
),
|
||||
parameters=(
|
||||
FunctionParameterDecl(
|
||||
name='actuator',
|
||||
type=PointerType(
|
||||
inner_type=ValueType(name='mjsActuator'),
|
||||
),
|
||||
),
|
||||
),
|
||||
doc='Set actuator to motor, return error if any.',
|
||||
)),
|
||||
('mjs_setToPosition',
|
||||
FunctionDecl(
|
||||
name='mjs_setToPosition',
|
||||
return_type=PointerType(
|
||||
inner_type=ValueType(name='char', is_const=True),
|
||||
),
|
||||
parameters=(
|
||||
FunctionParameterDecl(
|
||||
name='actuator',
|
||||
type=PointerType(
|
||||
inner_type=ValueType(name='mjsActuator'),
|
||||
),
|
||||
),
|
||||
FunctionParameterDecl(
|
||||
name='kp',
|
||||
type=ValueType(name='double'),
|
||||
),
|
||||
FunctionParameterDecl(
|
||||
name='kv',
|
||||
type=ArrayType(
|
||||
inner_type=ValueType(name='double'),
|
||||
extents=(1,),
|
||||
),
|
||||
),
|
||||
FunctionParameterDecl(
|
||||
name='dampratio',
|
||||
type=ArrayType(
|
||||
inner_type=ValueType(name='double'),
|
||||
extents=(1,),
|
||||
),
|
||||
),
|
||||
FunctionParameterDecl(
|
||||
name='timeconst',
|
||||
type=ArrayType(
|
||||
inner_type=ValueType(name='double'),
|
||||
extents=(1,),
|
||||
),
|
||||
),
|
||||
FunctionParameterDecl(
|
||||
name='inheritrange',
|
||||
type=ValueType(name='double'),
|
||||
),
|
||||
),
|
||||
doc='Set actuator to position, return error if any.',
|
||||
)),
|
||||
('mjs_setToIntVelocity',
|
||||
FunctionDecl(
|
||||
name='mjs_setToIntVelocity',
|
||||
return_type=PointerType(
|
||||
inner_type=ValueType(name='char', is_const=True),
|
||||
),
|
||||
parameters=(
|
||||
FunctionParameterDecl(
|
||||
name='actuator',
|
||||
type=PointerType(
|
||||
inner_type=ValueType(name='mjsActuator'),
|
||||
),
|
||||
),
|
||||
FunctionParameterDecl(
|
||||
name='kp',
|
||||
type=ValueType(name='double'),
|
||||
),
|
||||
FunctionParameterDecl(
|
||||
name='kv',
|
||||
type=ArrayType(
|
||||
inner_type=ValueType(name='double'),
|
||||
extents=(1,),
|
||||
),
|
||||
),
|
||||
FunctionParameterDecl(
|
||||
name='dampratio',
|
||||
type=ArrayType(
|
||||
inner_type=ValueType(name='double'),
|
||||
extents=(1,),
|
||||
),
|
||||
),
|
||||
FunctionParameterDecl(
|
||||
name='timeconst',
|
||||
type=ArrayType(
|
||||
inner_type=ValueType(name='double'),
|
||||
extents=(1,),
|
||||
),
|
||||
),
|
||||
FunctionParameterDecl(
|
||||
name='inheritrange',
|
||||
type=ValueType(name='double'),
|
||||
),
|
||||
),
|
||||
doc='Set actuator to integrated velocity, return error if any.',
|
||||
)),
|
||||
('mjs_setToVelocity',
|
||||
FunctionDecl(
|
||||
name='mjs_setToVelocity',
|
||||
return_type=PointerType(
|
||||
inner_type=ValueType(name='char', is_const=True),
|
||||
),
|
||||
parameters=(
|
||||
FunctionParameterDecl(
|
||||
name='actuator',
|
||||
type=PointerType(
|
||||
inner_type=ValueType(name='mjsActuator'),
|
||||
),
|
||||
),
|
||||
FunctionParameterDecl(
|
||||
name='kv',
|
||||
type=ValueType(name='double'),
|
||||
),
|
||||
),
|
||||
doc='Set actuator to velocity servo, return error if any.',
|
||||
)),
|
||||
('mjs_setToDamper',
|
||||
FunctionDecl(
|
||||
name='mjs_setToDamper',
|
||||
return_type=PointerType(
|
||||
inner_type=ValueType(name='char', is_const=True),
|
||||
),
|
||||
parameters=(
|
||||
FunctionParameterDecl(
|
||||
name='actuator',
|
||||
type=PointerType(
|
||||
inner_type=ValueType(name='mjsActuator'),
|
||||
),
|
||||
),
|
||||
FunctionParameterDecl(
|
||||
name='kv',
|
||||
type=ValueType(name='double'),
|
||||
),
|
||||
),
|
||||
doc='Set actuator to activate damper, return error if any.',
|
||||
)),
|
||||
('mjs_setToCylinder',
|
||||
FunctionDecl(
|
||||
name='mjs_setToCylinder',
|
||||
return_type=PointerType(
|
||||
inner_type=ValueType(name='char', is_const=True),
|
||||
),
|
||||
parameters=(
|
||||
FunctionParameterDecl(
|
||||
name='actuator',
|
||||
type=PointerType(
|
||||
inner_type=ValueType(name='mjsActuator'),
|
||||
),
|
||||
),
|
||||
FunctionParameterDecl(
|
||||
name='timeconst',
|
||||
type=ValueType(name='double'),
|
||||
),
|
||||
FunctionParameterDecl(
|
||||
name='bias',
|
||||
type=ValueType(name='double'),
|
||||
),
|
||||
FunctionParameterDecl(
|
||||
name='area',
|
||||
type=ValueType(name='double'),
|
||||
),
|
||||
FunctionParameterDecl(
|
||||
name='diameter',
|
||||
type=ValueType(name='double'),
|
||||
),
|
||||
),
|
||||
doc='Set actuator to hydraulic or pneumatic cylinder, return error if any.', # pylint: disable=line-too-long
|
||||
)),
|
||||
('mjs_setToMuscle',
|
||||
FunctionDecl(
|
||||
name='mjs_setToMuscle',
|
||||
return_type=PointerType(
|
||||
inner_type=ValueType(name='char', is_const=True),
|
||||
),
|
||||
parameters=(
|
||||
FunctionParameterDecl(
|
||||
name='actuator',
|
||||
type=PointerType(
|
||||
inner_type=ValueType(name='mjsActuator'),
|
||||
),
|
||||
),
|
||||
FunctionParameterDecl(
|
||||
name='timeconst',
|
||||
type=ArrayType(
|
||||
inner_type=ValueType(name='double'),
|
||||
extents=(2,),
|
||||
),
|
||||
),
|
||||
FunctionParameterDecl(
|
||||
name='tausmooth',
|
||||
type=ValueType(name='double'),
|
||||
),
|
||||
FunctionParameterDecl(
|
||||
name='range',
|
||||
type=ArrayType(
|
||||
inner_type=ValueType(name='double'),
|
||||
extents=(2,),
|
||||
),
|
||||
),
|
||||
FunctionParameterDecl(
|
||||
name='force',
|
||||
type=ValueType(name='double'),
|
||||
),
|
||||
FunctionParameterDecl(
|
||||
name='scale',
|
||||
type=ValueType(name='double'),
|
||||
),
|
||||
FunctionParameterDecl(
|
||||
name='lmin',
|
||||
type=ValueType(name='double'),
|
||||
),
|
||||
FunctionParameterDecl(
|
||||
name='lmax',
|
||||
type=ValueType(name='double'),
|
||||
),
|
||||
FunctionParameterDecl(
|
||||
name='vmax',
|
||||
type=ValueType(name='double'),
|
||||
),
|
||||
FunctionParameterDecl(
|
||||
name='fpmax',
|
||||
type=ValueType(name='double'),
|
||||
),
|
||||
FunctionParameterDecl(
|
||||
name='fvmax',
|
||||
type=ValueType(name='double'),
|
||||
),
|
||||
),
|
||||
doc='Set actuator to muscle, return error if any.a',
|
||||
)),
|
||||
('mjs_setToAdhesion',
|
||||
FunctionDecl(
|
||||
name='mjs_setToAdhesion',
|
||||
return_type=PointerType(
|
||||
inner_type=ValueType(name='char', is_const=True),
|
||||
),
|
||||
parameters=(
|
||||
FunctionParameterDecl(
|
||||
name='actuator',
|
||||
type=PointerType(
|
||||
inner_type=ValueType(name='mjsActuator'),
|
||||
),
|
||||
),
|
||||
FunctionParameterDecl(
|
||||
name='gain',
|
||||
type=ValueType(name='double'),
|
||||
),
|
||||
),
|
||||
doc='Set actuator to active adhesion, return error if any.',
|
||||
)),
|
||||
('mjs_addMesh',
|
||||
FunctionDecl(
|
||||
name='mjs_addMesh',
|
||||
|
||||
@@ -1010,6 +1010,95 @@ PYBIND11_MODULE(_specs, m) {
|
||||
[](raw::MjsActuator& self, raw::MjsDefault& default_) -> void {
|
||||
mjs_setDefault(self.element, &default_);
|
||||
});
|
||||
mjsActuator.def("set_to_motor", [](raw::MjsActuator* self) {
|
||||
std::string err = mjs_setToMotor(self);
|
||||
if (!err.empty()) {
|
||||
throw pybind11::value_error(err);
|
||||
}
|
||||
});
|
||||
mjsActuator.def(
|
||||
"set_to_position",
|
||||
[](raw::MjsActuator* self, double kp, double kv, double dampratio,
|
||||
double timeconst, bool inheritrange) {
|
||||
std::string err = mjs_setToPosition(
|
||||
self, kp, kv == -1 ? nullptr : &kv,
|
||||
dampratio == -1 ? nullptr : &dampratio,
|
||||
timeconst == -1 ? nullptr : &timeconst, inheritrange);
|
||||
if (!err.empty()) {
|
||||
throw pybind11::value_error(err);
|
||||
}
|
||||
},
|
||||
py::arg("kp"), py::arg("kv") = -1, py::arg("dampratio") = -1,
|
||||
py::arg("timeconst") = -1, py::arg("inheritrange") = false);
|
||||
mjsActuator.def(
|
||||
"set_to_intvelocity",
|
||||
[](raw::MjsActuator* self, double kp, double kv, double dampratio,
|
||||
double timeconst, bool inheritrange) {
|
||||
std::string err = mjs_setToIntVelocity(
|
||||
self, kp, kv == -1 ? nullptr : &kv,
|
||||
dampratio == -1 ? nullptr : &dampratio,
|
||||
timeconst == -1 ? nullptr : &timeconst, inheritrange);
|
||||
if (!err.empty()) {
|
||||
throw pybind11::value_error(err);
|
||||
}
|
||||
},
|
||||
py::arg("kp"), py::arg("kv") = -1, py::arg("dampratio") = -1,
|
||||
py::arg("timeconst") = -1, py::arg("inheritrange") = false);
|
||||
mjsActuator.def(
|
||||
"set_to_velocity",
|
||||
[](raw::MjsActuator* self, double kv) {
|
||||
std::string err = mjs_setToVelocity(self, kv);
|
||||
if (!err.empty()) {
|
||||
throw pybind11::value_error(err);
|
||||
}
|
||||
},
|
||||
py::arg("kv"));
|
||||
mjsActuator.def(
|
||||
"set_to_damper",
|
||||
[](raw::MjsActuator* self, double kv) {
|
||||
std::string err = mjs_setToDamper(self, kv);
|
||||
if (!err.empty()) {
|
||||
throw pybind11::value_error(err);
|
||||
}
|
||||
},
|
||||
py::arg("kv"));
|
||||
mjsActuator.def(
|
||||
"set_to_cylinder",
|
||||
[](raw::MjsActuator* self, double timeconst, double bias, double area,
|
||||
double diameter) {
|
||||
std::string err =
|
||||
mjs_setToCylinder(self, timeconst, bias, area, diameter);
|
||||
if (!err.empty()) {
|
||||
throw pybind11::value_error(err);
|
||||
}
|
||||
},
|
||||
py::arg("timeconst"), py::arg("bias"), py::arg("area"),
|
||||
py::arg("diameter") = -1);
|
||||
mjsActuator.def(
|
||||
"set_to_muscle",
|
||||
[](raw::MjsActuator* self, double timeconst[2], double tausmooth,
|
||||
double range[2], double force, double scale, double lmin, double lmax,
|
||||
double vmax, double fpmax, double fvmax) {
|
||||
std::string err =
|
||||
mjs_setToMuscle(self, timeconst, tausmooth, range, force, scale,
|
||||
lmin, lmax, vmax, fpmax, fvmax);
|
||||
if (!err.empty()) {
|
||||
throw pybind11::value_error(err);
|
||||
}
|
||||
},
|
||||
py::arg("timeconst") = -1, py::arg("tausmooth"),
|
||||
py::arg("range") = std::array<double, 2>{-1, -1}, py::arg("force") = -1,
|
||||
py::arg("scale") = -1, py::arg("lmin") = -1, py::arg("lmax") = -1,
|
||||
py::arg("vmax") = -1, py::arg("fpmax") = -1, py::arg("fvmax") = -1);
|
||||
mjsActuator.def(
|
||||
"set_to_adhesion",
|
||||
[](raw::MjsActuator* self, double gain) {
|
||||
std::string err = mjs_setToAdhesion(self, gain);
|
||||
if (!err.empty()) {
|
||||
throw pybind11::value_error(err);
|
||||
}
|
||||
},
|
||||
py::arg("gain"));
|
||||
|
||||
// ============================= MJSTENDON ===================================
|
||||
mjsTendon.def("delete",
|
||||
|
||||
@@ -1232,5 +1232,61 @@ class SpecsTest(absltest.TestCase):
|
||||
self.assertGreater(spec3._address, 0)
|
||||
self.assertLen({spec1._address, spec2._address, spec3._address}, 3)
|
||||
|
||||
def test_actuator_shortname(self):
|
||||
spec = mujoco.MjSpec()
|
||||
actuator = spec.add_actuator(
|
||||
gainprm=np.zeros((10, 1)),
|
||||
dyntype=mujoco.mjtDyn.mjDYN_FILTER,
|
||||
gaintype=mujoco.mjtGain.mjGAIN_AFFINE,
|
||||
biastype=mujoco.mjtBias.mjBIAS_AFFINE,
|
||||
)
|
||||
actuator.set_to_motor()
|
||||
self.assertEqual(actuator.gainprm[0], 1)
|
||||
self.assertEqual(actuator.dyntype, mujoco.mjtDyn.mjDYN_NONE)
|
||||
self.assertEqual(actuator.gaintype, mujoco.mjtGain.mjGAIN_FIXED)
|
||||
self.assertEqual(actuator.biastype, mujoco.mjtBias.mjBIAS_NONE)
|
||||
|
||||
actuator.set_to_position(kp=2.0, kv=3.0, timeconst=4.0, inheritrange=True)
|
||||
self.assertEqual(actuator.gainprm[0], 2)
|
||||
self.assertEqual(actuator.biasprm[1], -2)
|
||||
self.assertEqual(actuator.biasprm[2], -3)
|
||||
self.assertEqual(actuator.dynprm[0], 4)
|
||||
self.assertEqual(actuator.dyntype, mujoco.mjtDyn.mjDYN_FILTEREXACT)
|
||||
self.assertEqual(actuator.gaintype, mujoco.mjtGain.mjGAIN_FIXED)
|
||||
self.assertEqual(actuator.biastype, mujoco.mjtBias.mjBIAS_AFFINE)
|
||||
self.assertEqual(actuator.inheritrange, True)
|
||||
|
||||
actuator.set_to_intvelocity(
|
||||
kp=2.0, kv=3.0, timeconst=4.0, inheritrange=True
|
||||
)
|
||||
self.assertEqual(actuator.gainprm[0], 2)
|
||||
self.assertEqual(actuator.biasprm[1], -2)
|
||||
self.assertEqual(actuator.biasprm[2], -3)
|
||||
self.assertEqual(actuator.dynprm[0], 4)
|
||||
self.assertEqual(actuator.dyntype, mujoco.mjtDyn.mjDYN_INTEGRATOR)
|
||||
self.assertEqual(actuator.gaintype, mujoco.mjtGain.mjGAIN_FIXED)
|
||||
self.assertEqual(actuator.biastype, mujoco.mjtBias.mjBIAS_AFFINE)
|
||||
self.assertEqual(actuator.inheritrange, True)
|
||||
|
||||
actuator.set_to_velocity(kv=5.0)
|
||||
self.assertEqual(actuator.gainprm[0], 5)
|
||||
self.assertEqual(actuator.biasprm[2], -5)
|
||||
self.assertEqual(actuator.dyntype, mujoco.mjtDyn.mjDYN_NONE)
|
||||
self.assertEqual(actuator.gaintype, mujoco.mjtGain.mjGAIN_FIXED)
|
||||
self.assertEqual(actuator.biastype, mujoco.mjtBias.mjBIAS_AFFINE)
|
||||
|
||||
actuator.set_to_damper(kv=6.0)
|
||||
self.assertEqual(actuator.gainprm[0], 0)
|
||||
self.assertEqual(actuator.gainprm[2], -6)
|
||||
self.assertEqual(actuator.dyntype, mujoco.mjtDyn.mjDYN_NONE)
|
||||
self.assertEqual(actuator.gaintype, mujoco.mjtGain.mjGAIN_AFFINE)
|
||||
self.assertEqual(actuator.biastype, mujoco.mjtBias.mjBIAS_NONE)
|
||||
|
||||
actuator.set_to_adhesion(gain=7.0)
|
||||
self.assertEqual(actuator.gainprm[0], 7)
|
||||
self.assertEqual(actuator.dyntype, mujoco.mjtDyn.mjDYN_NONE)
|
||||
self.assertEqual(actuator.gaintype, mujoco.mjtGain.mjGAIN_FIXED)
|
||||
self.assertEqual(actuator.biastype, mujoco.mjtBias.mjBIAS_NONE)
|
||||
|
||||
if __name__ == '__main__':
|
||||
absltest.main()
|
||||
|
||||
Reference in New Issue
Block a user