Skip to content

Commit 410fa00

Browse files
Set MuJoCo warning and error callback
1 parent 858538f commit 410fa00

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

src/simulation/mujocoDynamics/_GeneralModuleFiles/MJScene.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ MJScene::MJScene(std::string xml, const std::vector<std::string>& files)
3838
{
3939
this->AddFwdKinematicsToDynamicsTask(MJScene::FWD_KINEMATICS_PRIORITY);
4040
this->integrator = new svIntegratorRK4(this);
41+
42+
// Replace default MuJoCo error/warning handling with our own
43+
mju_user_error = MJBasilisk::detail::logMujocoError;
44+
mju_user_warning = MJBasilisk::detail::logMujocoWarning;
4145
}
4246

4347
MJScene MJScene::fromFile(const std::string& fileName)

src/simulation/mujocoDynamics/_GeneralModuleFiles/MJUtils.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@
1919

2020
#include "MJUtils.h"
2121

22+
using namespace std::string_literals;
23+
2224
namespace MJBasilisk::detail
2325
{
26+
void
27+
logMujocoError(const char* err)
28+
{
29+
logAndThrow<std::runtime_error>("MuJoCo internal error: "s + err);
30+
}
31+
32+
void
33+
logMujocoWarning(const char* err)
34+
{
35+
BSKLogger{}.bskLog(BSK_WARNING, ("MuJoCo internal warning: "s + err).c_str());
36+
}
37+
2438
} // namespace MJBasilisk::detail

src/simulation/mujocoDynamics/_GeneralModuleFiles/MJUtils.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ template <typename T = std::invalid_argument>
6464
throw T(error);
6565
}
6666

67+
/** Calls ``logAndThrow<std::runtime_error>`` with the given input
68+
*
69+
* Meant to be used as an error callback for MuJoCo's ``mju_user_error``.
70+
*/
71+
void logMujocoError(const char* err);
72+
73+
/** Calls ``logAndThrow<std::runtime_error>`` with the given input
74+
*
75+
* Meant to be used as an error callback for MuJoCo's ``mju_user_error``.
76+
*/
77+
void logMujocoWarning(const char* err);
78+
6779
} // namespace MJBasilisk::detail
6880

6981
#endif

0 commit comments

Comments
 (0)