Fix formatting in documentation code samples.

PiperOrigin-RevId: 652237833
Change-Id: Ia90fadccba39881748a7ead15db3285e00feb08f
This commit is contained in:
Yuval Tassa
2024-07-14 07:07:27 -07:00
committed by Copybara-Service
parent 22a10fd2ff
commit 3d28a1d8c7
2 changed files with 43 additions and 49 deletions
+19 -20
View File
@@ -207,28 +207,26 @@ rendering, is given below.
mjModel* m;
mjData* d;
int main(void)
{
// load model from file and check for errors
m = mj_loadXML("hello.xml", NULL, error, 1000);
if( !m )
{
printf("%s\n", error);
return 1;
}
int main(void) {
// load model from file and check for errors
m = mj_loadXML("hello.xml", NULL, error, 1000);
if (!m) {
printf("%s\n", error);
return 1;
}
// make data corresponding to model
d = mj_makeData(m);
// make data corresponding to model
d = mj_makeData(m);
// run simulation for 10 seconds
while( d->time<10 )
mj_step(m, d);
// run simulation for 10 seconds
while (d->time < 10)
mj_step(m, d);
// free model and data
mj_deleteData(d);
mj_deleteModel(m);
// free model and data
mj_deleteData(d);
mj_deleteModel(m);
return 0;
return 0;
}
This is technically a C file, but it is also a legitimate C++ file. Indeed the MuJoCo API is compatible with both C and
@@ -823,9 +821,10 @@ convention. Suppose we already have ``mjModel* m``. To print the range of a join
.. code:: C
int jntid = mj_name2id(m, mjOBJ_JOINT, "elbow");
if( jntid>=0 )
if (jntid >= 0)
printf("(%f, %f)\n", m->jnt_range[2*jntid], m->jnt_range[2*jntid+1]);
If the name is not found the function returns -1, which is why one should always check for id>=0.
.. _BodyGeomSite:
@@ -943,7 +942,7 @@ can be obtained as:
int qposadr = -1, qveladr = -1;
// make sure we have a floating body: it has a single free joint
if( bodyid>=0 && m->body_jntnum[bodyid]==1 && m->jnt_type[m->body_jntadr[bodyid]]==mjJNT_FREE ) {
if (bodyid >= 0 && m->body_jntnum[bodyid] == 1 && m->jnt_type[m->body_jntadr[bodyid]] == mjJNT_FREE) {
// extract the addresses from the joint specification
qposadr = m->jnt_qposadr[m->body_jntadr[bodyid]];
qveladr = m->jnt_dofadr[m->body_jntadr[bodyid]];
+24 -29
View File
@@ -98,7 +98,7 @@ function :ref:`mj_step` in a loop such as
.. code-block:: C
// simulate until t = 10 seconds
while( d->time<10 )
while (d->time < 10)
mj_step(m, d);
This by itself will simulate the passive dynamics, because we have not provided any control signals or applied forces.
@@ -107,9 +107,8 @@ The default (and recommended) way to control the system is to implement a contro
.. code-block:: C
// simple controller applying damping to each dof
void mycontroller(const mjModel* m, mjData* d)
{
if( m->nu==m->nv )
void mycontroller(const mjModel* m, mjData* d) {
if (m->nu == m->nv)
mju_scl(d->ctrl, d->qvel, -0.1, m->nv);
}
@@ -138,7 +137,7 @@ control callback) would become
.. code-block:: C
while( d->time<10 ) {
while (d->time < 10) {
// set d->ctrl or d->qfrc_applied or d->xfrc_applied
mj_step(m, d);
}
@@ -161,7 +160,7 @@ before the control is needed, and after the control is needed. The simulation lo
.. code-block:: C
while( d->time<10 ) {
while (d->time < 10) {
mj_step1(m, d);
// set d->ctrl or d->qfrc_applied or d->xfrc_applied
mj_step2(m, d);
@@ -186,11 +185,11 @@ omitting some code that computes timing diagnostics. The main simulation functio
mj_checkAcc(m, d);
// compare forward and inverse solutions if enabled
if( mjENABLED(mjENBL_FWDINV) )
if (mjENABLED(mjENBL_FWDINV))
mj_compareFwdInv(m, d);
// use selected integrator
if( m->opt.integrator==mjINT_RK4 )
if (m->opt.integrator == mjINT_RK4)
mj_RungeKutta(m, d, 4);
else
mj_Euler(m, d);
@@ -206,8 +205,7 @@ mj_step2 regardless of the setting of ``mjModel.opt.integrator``.
.. code-block:: C
void mj_step1(const mjModel* m, mjData* d)
{
void mj_step1(const mjModel* m, mjData* d) {
mj_checkPos(m, d);
mj_checkVel(m, d);
mj_fwdPosition(m, d);
@@ -218,12 +216,11 @@ mj_step2 regardless of the setting of ``mjModel.opt.integrator``.
mj_energyVel(m, d);
// if we had a callback we would be using mj_step, but call it anyway
if( mjcb_control )
if (mjcb_control)
mjcb_control(m, d);
}
void mj_step2(const mjModel* m, mjData* d)
{
void mj_step2(const mjModel* m, mjData* d) {
mj_fwdActuation(m, d);
mj_fwdAcceleration(m, d);
mj_fwdConstraint(m, d);
@@ -231,7 +228,7 @@ mj_step2 regardless of the setting of ``mjModel.opt.integrator``.
mj_checkAcc(m, d);
// compare forward and inverse solutions if enabled
if( mjENABLED(mjENBL_FWDINV) )
if (mjENABLED(mjENBL_FWDINV))
mj_compareFwdInv(m, d);
// integrate with Euler; ignore integrator option
@@ -248,7 +245,7 @@ notion of state of a dynamical system. Dynamical systems are usually described i
.. code-block:: Text
dx/dt = f(t,x,u)
dx/dt = f(t, x, u)
where ``t`` is the time, ``x`` is the state vector, ``u`` is the control vector, and ``f`` is the function that
computes the time-derivative of the state. This is a continuous-time formulation, and indeed the physics model
@@ -317,7 +314,7 @@ internal diagnostics which do not affect the simulation). This can be done as
// copy mocap body pose and userdata
mju_copy(dst->mocap_pos, src->mocap_pos, 3*m->nmocap);
mju_copy(dst->mocap_quat, src->mocap_quat, 4*m->nmocap);
mju_copy(dst->userdata, src->userdata, m->nuserdata);
mju_copy(dst->userdata, src->userdata, m->nuserdata);
// copy warm-start acceleration
mju_copy(dst->qacc_warmstart, src->qacc_warmstart, m->nv);
@@ -372,32 +369,30 @@ skip arguments (mjSTAGE_NONE, 0), where the latter function is implemented as
void mj_forwardSkip(const mjModel* m, mjData* d, int skipstage, int skipsensor) {
// position-dependent
if( skipstage<mjSTAGE_POS )
{
if (skipstage < mjSTAGE_POS) {
mj_fwdPosition(m, d);
if( !skipsensor )
if (!skipsensor)
mj_sensorPos(m, d);
if( mjENABLED(mjENBL_ENERGY) )
if (mjENABLED(mjENBL_ENERGY))
mj_energyPos(m, d);
}
// velocity-dependent
if( skipstage<mjSTAGE_VEL )
{
if (skipstage < mjSTAGE_VEL) {
mj_fwdVelocity(m, d);
if( !skipsensor )
if (!skipsensor)
mj_sensorVel(m, d);
if( mjENABLED(mjENBL_ENERGY) )
if (mjENABLED(mjENBL_ENERGY))
mj_energyVel(m, d);
}
// acceleration-dependent
if( mjcb_control )
if (mjcb_control)
mjcb_control(m, d);
mj_fwdActuation(m, d);
mj_fwdAcceleration(m, d);
mj_fwdConstraint(m, d);
if( !skipsensor )
if (!skipsensor)
mj_sensorAcc(m, d);
}
@@ -510,8 +505,8 @@ management.
// allocate per-thread mjData
mjData* d[64];
for( int n=0; n < nthread; n++ )
d[n] = mj_makeData(m);
for (int n=0; n < nthread; n++)
d[n] = mj_makeData(m);
// ... serial code, perhaps using its own mjData* dmain
@@ -527,7 +522,7 @@ management.
}
// delete per-thread mjData
for( int n=0; n<nthread; n++ )
for (int n=0; n < nthread; n++)
mj_deleteData(d[n]);
Since all top-level API functions treat mjModel as ``const``, this multi-threading scheme is safe. Each thread only