mujoco官方文档笔记 20240630

官方文档的内容非常简练 本文添加了部分解释说明

program

检查版本 兼容性

开发者开发版本和现在运行时编译和链接的版本不一样,尽管可能函数之间没有发生变动,还是建议进行版本检查。

To this end, the main header (mujoco.h) defines the symbol mjVERSION_HEADER and the library provides the function mj_version. Thus the header and library versions can be compared with:


// recommended version check
if( mjVERSION_HEADER!=mj_version() )
  complain();

这段话主要解释了如何使用整数来表示软件版本号,并且如何通过这种表示方式来避免浮点数比较带来的复杂性。让我们逐句解释:

  1. Note that only the main header defines this symbol.

    • 这句话指出只有主头文件定义了这个符号(symbol)。这可能是指在软件开发中,只有主要的头文件或者主要的配置文件中定义了特定的符号或者标识符。
  2. We assume that the collection of headers released with each software version will stay together and will not be mixed between versions.

    • 这句话假设每个软件版本发布时的所有头文件集合将保持完整性,并且不会在不同版本之间混合。这意味着每个版本的头文件集合都是完整的,不会在不同的软件版本之间混淆或混合使用。
  3. To avoid complications with floating-point comparisons, the above symbol and function use integers that are 100x the version number, so for example in software version 2.1 the symbol mjVERSION_HEADER is defined as 210.

    • 为了避免使用浮点数进行比较时可能出现的复杂性,上述符号和函数使用的是整数,且这些整数是版本号的100倍。例如,在软件版本2.1中,符号mjVERSION_HEADER被定义为210。这意味着软件版本号被乘以100后作为整数使用,这样可以避免浮点数运算可能带来的精度问题和比较上的复杂性。

综上所述,这段话解释了如何通过整数表示法来处理软件版本号,并且指出了在软件开发过程中确保版本头文件的完整性和一致性的重要性。

Initialization

After the version check, the next step is to allocate and initialize the main data structures needed for simulation, namely mjModel and mjData. Additional initialization steps related to visualization and callbacks will be discussed later.

注意1

这里提到了
Our strategy is to allocate all necessary heap memory at the beginning of the simulation, and free it after the simulation is done, so that we never have to call the C memory allocation and deallocation functions during the simulation. This is done for speed, avoidance of memory fragmentation, future GPU portability, and ease of managing the state of the entire simulator during a reset.

It also means however that the maximal variable-memory allocation given by the memory attribute in the size MJCF element, which affects the allocation of mjData, must be set to a sufficiently large value. If this maximal size is exceeded during simulation, it is not increased dynamically, but instead an error is generated. See also diagnostics below.

意思是这个虽然有一堆好处 但是不可避免地要预先设定一个特别大的值给memory attribute in the size MJCF element

如果仿真中超过这个值,他不会动态的增大,所以会引发报错

注意2

// option 1: parse and compile XML from file
mjModel* m = mj_loadXML("mymodel.xml", NULL, errstr, errstr_sz);

// option 2: parse and compile XML from virtual file system
mjModel* m = mj_loadXML("mymodel.xml", vfs, errstr, errstr_sz);

模型加载出错,会返回一个空指针

All these functions return a NULL pointer if there is an error or warning. In the case of XML parsing and model compilation, a description of the error is returned in the string provided as argument.

注意3 VFS

4 参数检查

In addition to mjModel which holds the model description, we also need mjData which is the workspace where all computations are performed. Note that mjData is specific to a given mjModel.

The API functions generally assume that users know what they are doing, and perform minimal argument checking. If the mjModel and mjData passed to any API function are incompatible (or NULL) the resulting behavior is unpredictable.

5 sensor noise

MuJoCo simulations are deterministic with one exception: sensor noise can be generated when this feature is enabled. This is done by calling the C function rand() internally.

这段文本讨论了MuJoCo仿真中的确定性特性以及随机性的影响。让我们逐句解释:

  1. MuJoCo simulations are deterministic with one exception: sensor noise can be generated when this feature is enabled.

    • MuJoCo仿真通常是确定性的,意味着在相同的初始条件下,每次仿真的结果应该是一致的。然而,当启用传感器噪声功能时,仿真中会引入随机性,这会导致不同的仿真运行结果。
  2. This is done by calling the C function rand() internally.

    • 在MuJoCo中,引入传感器噪声是通过内部调用C语言函数rand()实现的。rand()函数用于生成伪随机数。
  3. To generate the same random number sequence, call srand() with a desired seed after the model is loaded and before the simulation starts.

    • 如果希望生成相同的随机数序列,需要在加载模型后并在仿真开始之前,调用srand()函数设置一个确定的种子(seed)。种子确定了随机数生成器的初始状态,相同种子会导致生成相同的随机数序列。
  4. The model compiler calls srand(123) internally, so as to generate random dots for procedural textures.

    • 模型编译器在内部调用srand(123),以生成用于过程纹理的随机点。这表明在MuJoCo中,编译模型时会使用特定的种子初始化随机数生成器,以便在处理过程中生成一致的随机性元素,如过程纹理中的随机点。
  5. Therefore the noise sequence in the sensor data will change if the specification of procedural textures changes, and the user does not call srand() after model compilation.

    • 因此,如果过程纹理的规格发生变化并且用户没有在模型编译后调用srand()重新设置种子,传感器数据中的噪声序列将会改变。这是因为未指定种子或使用不同的种子会导致不同的随机数序列生成,从而影响传感器噪声的生成。

simulation loop

有多种方式跑一个仿真循环,最简单的方式就是

// simulate until t = 10 seconds
while( d->time<10 )
  mj_step(m, d);

但是这样没有提供任何控制器(控制信号),只有被动的动力学
默认以及推荐的方式为应用一个控制回调函数
简单的阻尼控制

// simple controller applying damping to each dof
void mycontroller(const mjModel* m, mjData* d)
{
   
   
  if( m->nu==m->nv )
    mju_scl(d->ctrl, d->qvel, -0.1, m->nv);
}

注意

检查模型的控制数量 是否 等于 模型自由度
这段文本提供了关于MuJoCo仿真中回调函数和函数库使用的解释。让我们逐句来解释:

  1. In general, the same callback may be used with multiple models depending on how the user code is structured, and so it is a good idea to check the model dimensions in the callback.

    • 通常情况下,相同的回调函数可能会用于多个模型,这取决于用户代码的结构。因此,在回调函数中检查模型的维度是一个好主意。
    • 这句话指出,MuJoCo允许用户在多个模型中使用相同的回调函数,但由于不同模型可能具有不同的维度和参数,因此在回调函数中检查和处理模型的维度和参数是很重要的。
  2. Second, MuJoCo has a library of BLAS-like functions that are very useful; indeed a large part of the code base consists of calling such functions internally.

    • 其次,MuJoCo拥有类似于BLAS的函数库,这些函数非常有用;事实上,MuJoCo
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值