Skip to content

Commit 50de1b3

Browse files
ChuangTseucopybara-github
authored andcommitted
Read new Mujoco-specific JointAPI when converting USD.
PiperOrigin-RevId: 778037519 Change-Id: Idd81dc5dc14e2dfb40ba7db4404fd09f9ff50a9f
1 parent 9dc67b5 commit 50de1b3

File tree

1 file changed

+169
-0
lines changed

1 file changed

+169
-0
lines changed

src/experimental/usd/usd_to_mjspec.cc

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include <mujoco/experimental/usd/mjcPhysics/actuatorAPI.h>
2727
#include <mujoco/experimental/usd/mjcPhysics/collisionAPI.h>
28+
#include <mujoco/experimental/usd/mjcPhysics/jointAPI.h>
2829
#include <mujoco/experimental/usd/mjcPhysics/meshCollisionAPI.h>
2930
#include <mujoco/experimental/usd/mjcPhysics/sceneAPI.h>
3031
#include <mujoco/experimental/usd/mjcPhysics/siteAPI.h>
@@ -717,6 +718,170 @@ void ParseMjcPhysicsGeneralActuatorAPI(mjSpec* spec,
717718
}
718719
}
719720

721+
void ParseMjcPhysicsJointAPI(mjsJoint* mj_joint,
722+
const pxr::MjcPhysicsJointAPI& joint_api) {
723+
auto springdamper_attr = joint_api.GetMjcSpringdamperAttr();
724+
if (springdamper_attr.HasAuthoredValue()) {
725+
pxr::VtDoubleArray springdamper;
726+
springdamper_attr.Get(&springdamper);
727+
if (springdamper.size() == 2) {
728+
mj_joint->springdamper[0] = springdamper[0];
729+
mj_joint->springdamper[1] = springdamper[1];
730+
} else {
731+
mju_warning(
732+
"springdamper attribute for joint %s has incorrect size %zu, "
733+
"expected 2.",
734+
mj_joint->name->c_str(), springdamper.size());
735+
}
736+
}
737+
738+
auto solreflimit_attr = joint_api.GetMjcSolreflimitAttr();
739+
if (solreflimit_attr.HasAuthoredValue()) {
740+
pxr::VtDoubleArray solreflimit;
741+
solreflimit_attr.Get(&solreflimit);
742+
if (solreflimit.size() == mjNREF) {
743+
for (int i = 0; i < mjNREF; ++i) {
744+
mj_joint->solref_limit[i] = solreflimit[i];
745+
}
746+
} else {
747+
mju_warning(
748+
"solreflimit attribute for joint %s has incorrect size %zu, "
749+
"expected %d.",
750+
mj_joint->name->c_str(), solreflimit.size(), mjNREF);
751+
}
752+
}
753+
754+
auto solimplimit_attr = joint_api.GetMjcSolimplimitAttr();
755+
if (solimplimit_attr.HasAuthoredValue()) {
756+
pxr::VtDoubleArray solimplimit;
757+
solimplimit_attr.Get(&solimplimit);
758+
if (solimplimit.size() == mjNIMP) {
759+
for (int i = 0; i < mjNIMP; ++i) {
760+
mj_joint->solimp_limit[i] = solimplimit[i];
761+
}
762+
} else {
763+
mju_warning(
764+
"solimplimit attribute for joint %s has incorrect size %zu, "
765+
"expected %d.",
766+
mj_joint->name->c_str(), solimplimit.size(), mjNIMP);
767+
}
768+
}
769+
770+
auto solreffriction_attr = joint_api.GetMjcSolreffrictionAttr();
771+
if (solreffriction_attr.HasAuthoredValue()) {
772+
pxr::VtDoubleArray solreffriction;
773+
solreffriction_attr.Get(&solreffriction);
774+
if (solreffriction.size() == mjNREF) {
775+
for (int i = 0; i < mjNREF; ++i) {
776+
mj_joint->solref_friction[i] = solreffriction[i];
777+
}
778+
} else {
779+
mju_warning(
780+
"solreffriction attribute for joint %s has incorrect size %zu, "
781+
"expected %d.",
782+
mj_joint->name->c_str(), solreffriction.size(), mjNREF);
783+
}
784+
}
785+
786+
auto solimpfriction_attr = joint_api.GetMjcSolimpfrictionAttr();
787+
if (solimpfriction_attr.HasAuthoredValue()) {
788+
pxr::VtDoubleArray solimpfriction;
789+
solimpfriction_attr.Get(&solimpfriction);
790+
if (solimpfriction.size() == mjNIMP) {
791+
for (int i = 0; i < mjNIMP; ++i) {
792+
mj_joint->solimp_friction[i] = solimpfriction[i];
793+
}
794+
} else {
795+
mju_warning(
796+
"solimpfriction attribute for joint %s has incorrect size %zu, "
797+
"expected %d.",
798+
mj_joint->name->c_str(), solimpfriction.size(), mjNIMP);
799+
}
800+
}
801+
802+
auto stiffness_attr = joint_api.GetMjcStiffnessAttr();
803+
if (stiffness_attr.HasAuthoredValue()) {
804+
double stiffness;
805+
stiffness_attr.Get(&stiffness);
806+
mj_joint->stiffness = stiffness;
807+
}
808+
809+
auto actuatorfrcrange_min_attr = joint_api.GetMjcActuatorfrcrangeMinAttr();
810+
if (actuatorfrcrange_min_attr.HasAuthoredValue()) {
811+
double min_val;
812+
actuatorfrcrange_min_attr.Get(&min_val);
813+
mj_joint->actfrcrange[0] = min_val;
814+
}
815+
auto actuatorfrcrange_max_attr = joint_api.GetMjcActuatorfrcrangeMaxAttr();
816+
if (actuatorfrcrange_max_attr.HasAuthoredValue()) {
817+
double max_val;
818+
actuatorfrcrange_max_attr.Get(&max_val);
819+
mj_joint->actfrcrange[1] = max_val;
820+
}
821+
822+
auto actuatorfrclimited_attr = joint_api.GetMjcActuatorfrclimitedAttr();
823+
if (actuatorfrclimited_attr.HasAuthoredValue()) {
824+
pxr::TfToken limited;
825+
actuatorfrclimited_attr.Get(&limited);
826+
if (limited == MjcPhysicsTokens->true_) {
827+
mj_joint->actfrclimited = mjLIMITED_TRUE;
828+
} else if (limited == MjcPhysicsTokens->false_) {
829+
mj_joint->actfrclimited = mjLIMITED_FALSE;
830+
} else if (limited == MjcPhysicsTokens->auto_) {
831+
mj_joint->actfrclimited = mjLIMITED_AUTO;
832+
}
833+
}
834+
835+
auto actuatorgravcomp_attr = joint_api.GetMjcActuatorgravcompAttr();
836+
if (actuatorgravcomp_attr.HasAuthoredValue()) {
837+
bool gravcomp;
838+
actuatorgravcomp_attr.Get(&gravcomp);
839+
mj_joint->actgravcomp = gravcomp;
840+
}
841+
842+
auto margin_attr = joint_api.GetMjcMarginAttr();
843+
if (margin_attr.HasAuthoredValue()) {
844+
double margin;
845+
margin_attr.Get(&margin);
846+
mj_joint->margin = margin;
847+
}
848+
849+
auto ref_attr = joint_api.GetMjcRefAttr();
850+
if (ref_attr.HasAuthoredValue()) {
851+
double ref;
852+
ref_attr.Get(&ref);
853+
mj_joint->ref = ref;
854+
}
855+
856+
auto springref_attr = joint_api.GetMjcSpringrefAttr();
857+
if (springref_attr.HasAuthoredValue()) {
858+
double springref;
859+
springref_attr.Get(&springref);
860+
mj_joint->springref = springref;
861+
}
862+
863+
auto armature_attr = joint_api.GetMjcArmatureAttr();
864+
if (armature_attr.HasAuthoredValue()) {
865+
double armature;
866+
armature_attr.Get(&armature);
867+
mj_joint->armature = armature;
868+
}
869+
870+
auto damping_attr = joint_api.GetMjcDampingAttr();
871+
if (damping_attr.HasAuthoredValue()) {
872+
double damping;
873+
damping_attr.Get(&damping);
874+
mj_joint->damping = damping;
875+
}
876+
877+
auto frictionloss_attr = joint_api.GetMjcFrictionlossAttr();
878+
if (frictionloss_attr.HasAuthoredValue()) {
879+
double frictionloss;
880+
frictionloss_attr.Get(&frictionloss);
881+
mj_joint->frictionloss = frictionloss;
882+
}
883+
}
884+
720885
void ParseUsdPhysicsCollider(mjSpec* spec,
721886
const pxr::UsdPhysicsCollisionAPI& collision_api,
722887
const pxr::UsdPrim& parent_prim, mjsBody* parent,
@@ -959,6 +1124,10 @@ void ParseUsdPhysicsJoint(mjSpec* spec, const pxr::UsdPrim& prim, mjsBody* body,
9591124
ParseMjcPhysicsGeneralActuatorAPI(spec, pxr::MjcPhysicsActuatorAPI(prim),
9601125
mjtTrn::mjTRN_JOINT, mj_joint->name);
9611126
}
1127+
1128+
if (prim.HasAPI<pxr::MjcPhysicsJointAPI>()) {
1129+
ParseMjcPhysicsJointAPI(mj_joint, pxr::MjcPhysicsJointAPI(prim));
1130+
}
9621131
}
9631132

9641133
void ParseMjcPhysicsSite(mjSpec* spec, const pxr::MjcPhysicsSiteAPI& site_api,

0 commit comments

Comments
 (0)