Fix formatting in documentation code samples.
PiperOrigin-RevId: 652237833 Change-Id: Ia90fadccba39881748a7ead15db3285e00feb08f
This commit is contained in:
committed by
Copybara-Service
parent
22a10fd2ff
commit
3d28a1d8c7
+19
-20
@@ -207,28 +207,26 @@ rendering, is given below.
|
|||||||
mjModel* m;
|
mjModel* m;
|
||||||
mjData* d;
|
mjData* d;
|
||||||
|
|
||||||
int main(void)
|
int main(void) {
|
||||||
{
|
// load model from file and check for errors
|
||||||
// load model from file and check for errors
|
m = mj_loadXML("hello.xml", NULL, error, 1000);
|
||||||
m = mj_loadXML("hello.xml", NULL, error, 1000);
|
if (!m) {
|
||||||
if( !m )
|
printf("%s\n", error);
|
||||||
{
|
return 1;
|
||||||
printf("%s\n", error);
|
}
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// make data corresponding to model
|
// make data corresponding to model
|
||||||
d = mj_makeData(m);
|
d = mj_makeData(m);
|
||||||
|
|
||||||
// run simulation for 10 seconds
|
// run simulation for 10 seconds
|
||||||
while( d->time<10 )
|
while (d->time < 10)
|
||||||
mj_step(m, d);
|
mj_step(m, d);
|
||||||
|
|
||||||
// free model and data
|
// free model and data
|
||||||
mj_deleteData(d);
|
mj_deleteData(d);
|
||||||
mj_deleteModel(m);
|
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
|
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
|
.. code:: C
|
||||||
|
|
||||||
int jntid = mj_name2id(m, mjOBJ_JOINT, "elbow");
|
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]);
|
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.
|
If the name is not found the function returns -1, which is why one should always check for id>=0.
|
||||||
|
|
||||||
.. _BodyGeomSite:
|
.. _BodyGeomSite:
|
||||||
@@ -943,7 +942,7 @@ can be obtained as:
|
|||||||
int qposadr = -1, qveladr = -1;
|
int qposadr = -1, qveladr = -1;
|
||||||
|
|
||||||
// make sure we have a floating body: it has a single free joint
|
// 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
|
// extract the addresses from the joint specification
|
||||||
qposadr = m->jnt_qposadr[m->body_jntadr[bodyid]];
|
qposadr = m->jnt_qposadr[m->body_jntadr[bodyid]];
|
||||||
qveladr = m->jnt_dofadr[m->body_jntadr[bodyid]];
|
qveladr = m->jnt_dofadr[m->body_jntadr[bodyid]];
|
||||||
|
|||||||
@@ -98,7 +98,7 @@ function :ref:`mj_step` in a loop such as
|
|||||||
.. code-block:: C
|
.. code-block:: C
|
||||||
|
|
||||||
// simulate until t = 10 seconds
|
// simulate until t = 10 seconds
|
||||||
while( d->time<10 )
|
while (d->time < 10)
|
||||||
mj_step(m, d);
|
mj_step(m, d);
|
||||||
|
|
||||||
This by itself will simulate the passive dynamics, because we have not provided any control signals or applied forces.
|
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
|
.. code-block:: C
|
||||||
|
|
||||||
// simple controller applying damping to each dof
|
// simple controller applying damping to each dof
|
||||||
void mycontroller(const mjModel* m, mjData* d)
|
void mycontroller(const mjModel* m, mjData* d) {
|
||||||
{
|
if (m->nu == m->nv)
|
||||||
if( m->nu==m->nv )
|
|
||||||
mju_scl(d->ctrl, d->qvel, -0.1, m->nv);
|
mju_scl(d->ctrl, d->qvel, -0.1, m->nv);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,7 +137,7 @@ control callback) would become
|
|||||||
|
|
||||||
.. code-block:: C
|
.. code-block:: C
|
||||||
|
|
||||||
while( d->time<10 ) {
|
while (d->time < 10) {
|
||||||
// set d->ctrl or d->qfrc_applied or d->xfrc_applied
|
// set d->ctrl or d->qfrc_applied or d->xfrc_applied
|
||||||
mj_step(m, d);
|
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
|
.. code-block:: C
|
||||||
|
|
||||||
while( d->time<10 ) {
|
while (d->time < 10) {
|
||||||
mj_step1(m, d);
|
mj_step1(m, d);
|
||||||
// set d->ctrl or d->qfrc_applied or d->xfrc_applied
|
// set d->ctrl or d->qfrc_applied or d->xfrc_applied
|
||||||
mj_step2(m, d);
|
mj_step2(m, d);
|
||||||
@@ -186,11 +185,11 @@ omitting some code that computes timing diagnostics. The main simulation functio
|
|||||||
mj_checkAcc(m, d);
|
mj_checkAcc(m, d);
|
||||||
|
|
||||||
// compare forward and inverse solutions if enabled
|
// compare forward and inverse solutions if enabled
|
||||||
if( mjENABLED(mjENBL_FWDINV) )
|
if (mjENABLED(mjENBL_FWDINV))
|
||||||
mj_compareFwdInv(m, d);
|
mj_compareFwdInv(m, d);
|
||||||
|
|
||||||
// use selected integrator
|
// use selected integrator
|
||||||
if( m->opt.integrator==mjINT_RK4 )
|
if (m->opt.integrator == mjINT_RK4)
|
||||||
mj_RungeKutta(m, d, 4);
|
mj_RungeKutta(m, d, 4);
|
||||||
else
|
else
|
||||||
mj_Euler(m, d);
|
mj_Euler(m, d);
|
||||||
@@ -206,8 +205,7 @@ mj_step2 regardless of the setting of ``mjModel.opt.integrator``.
|
|||||||
|
|
||||||
.. code-block:: C
|
.. code-block:: C
|
||||||
|
|
||||||
void mj_step1(const mjModel* m, mjData* d)
|
void mj_step1(const mjModel* m, mjData* d) {
|
||||||
{
|
|
||||||
mj_checkPos(m, d);
|
mj_checkPos(m, d);
|
||||||
mj_checkVel(m, d);
|
mj_checkVel(m, d);
|
||||||
mj_fwdPosition(m, d);
|
mj_fwdPosition(m, d);
|
||||||
@@ -218,12 +216,11 @@ mj_step2 regardless of the setting of ``mjModel.opt.integrator``.
|
|||||||
mj_energyVel(m, d);
|
mj_energyVel(m, d);
|
||||||
|
|
||||||
// if we had a callback we would be using mj_step, but call it anyway
|
// 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);
|
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_fwdActuation(m, d);
|
||||||
mj_fwdAcceleration(m, d);
|
mj_fwdAcceleration(m, d);
|
||||||
mj_fwdConstraint(m, d);
|
mj_fwdConstraint(m, d);
|
||||||
@@ -231,7 +228,7 @@ mj_step2 regardless of the setting of ``mjModel.opt.integrator``.
|
|||||||
mj_checkAcc(m, d);
|
mj_checkAcc(m, d);
|
||||||
|
|
||||||
// compare forward and inverse solutions if enabled
|
// compare forward and inverse solutions if enabled
|
||||||
if( mjENABLED(mjENBL_FWDINV) )
|
if (mjENABLED(mjENBL_FWDINV))
|
||||||
mj_compareFwdInv(m, d);
|
mj_compareFwdInv(m, d);
|
||||||
|
|
||||||
// integrate with Euler; ignore integrator option
|
// 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
|
.. 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
|
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
|
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
|
// copy mocap body pose and userdata
|
||||||
mju_copy(dst->mocap_pos, src->mocap_pos, 3*m->nmocap);
|
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->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
|
// copy warm-start acceleration
|
||||||
mju_copy(dst->qacc_warmstart, src->qacc_warmstart, m->nv);
|
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) {
|
void mj_forwardSkip(const mjModel* m, mjData* d, int skipstage, int skipsensor) {
|
||||||
// position-dependent
|
// position-dependent
|
||||||
if( skipstage<mjSTAGE_POS )
|
if (skipstage < mjSTAGE_POS) {
|
||||||
{
|
|
||||||
mj_fwdPosition(m, d);
|
mj_fwdPosition(m, d);
|
||||||
if( !skipsensor )
|
if (!skipsensor)
|
||||||
mj_sensorPos(m, d);
|
mj_sensorPos(m, d);
|
||||||
if( mjENABLED(mjENBL_ENERGY) )
|
if (mjENABLED(mjENBL_ENERGY))
|
||||||
mj_energyPos(m, d);
|
mj_energyPos(m, d);
|
||||||
}
|
}
|
||||||
|
|
||||||
// velocity-dependent
|
// velocity-dependent
|
||||||
if( skipstage<mjSTAGE_VEL )
|
if (skipstage < mjSTAGE_VEL) {
|
||||||
{
|
|
||||||
mj_fwdVelocity(m, d);
|
mj_fwdVelocity(m, d);
|
||||||
if( !skipsensor )
|
if (!skipsensor)
|
||||||
mj_sensorVel(m, d);
|
mj_sensorVel(m, d);
|
||||||
if( mjENABLED(mjENBL_ENERGY) )
|
if (mjENABLED(mjENBL_ENERGY))
|
||||||
mj_energyVel(m, d);
|
mj_energyVel(m, d);
|
||||||
}
|
}
|
||||||
|
|
||||||
// acceleration-dependent
|
// acceleration-dependent
|
||||||
if( mjcb_control )
|
if (mjcb_control)
|
||||||
mjcb_control(m, d);
|
mjcb_control(m, d);
|
||||||
mj_fwdActuation(m, d);
|
mj_fwdActuation(m, d);
|
||||||
mj_fwdAcceleration(m, d);
|
mj_fwdAcceleration(m, d);
|
||||||
mj_fwdConstraint(m, d);
|
mj_fwdConstraint(m, d);
|
||||||
if( !skipsensor )
|
if (!skipsensor)
|
||||||
mj_sensorAcc(m, d);
|
mj_sensorAcc(m, d);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -510,8 +505,8 @@ management.
|
|||||||
|
|
||||||
// allocate per-thread mjData
|
// allocate per-thread mjData
|
||||||
mjData* d[64];
|
mjData* d[64];
|
||||||
for( int n=0; n < nthread; n++ )
|
for (int n=0; n < nthread; n++)
|
||||||
d[n] = mj_makeData(m);
|
d[n] = mj_makeData(m);
|
||||||
|
|
||||||
// ... serial code, perhaps using its own mjData* dmain
|
// ... serial code, perhaps using its own mjData* dmain
|
||||||
|
|
||||||
@@ -527,7 +522,7 @@ management.
|
|||||||
}
|
}
|
||||||
|
|
||||||
// delete per-thread mjData
|
// delete per-thread mjData
|
||||||
for( int n=0; n<nthread; n++ )
|
for (int n=0; n < nthread; n++)
|
||||||
mj_deleteData(d[n]);
|
mj_deleteData(d[n]);
|
||||||
|
|
||||||
Since all top-level API functions treat mjModel as ``const``, this multi-threading scheme is safe. Each thread only
|
Since all top-level API functions treat mjModel as ``const``, this multi-threading scheme is safe. Each thread only
|
||||||
|
|||||||
Reference in New Issue
Block a user