Commit Graph

40 Commits

Author SHA1 Message Date
Nimrod Gileadi d567263959 Fix null pointer dereference in mj_loadModel.
mj_makeModel returns NULL if any of the array sizes are negative. Callers of the function need to handle that case.

PiperOrigin-RevId: 457948558
Change-Id: I9a5c8c0d3293ed0a92c5791f00d3ed34c848e024
2022-06-29 05:09:54 -07:00
Yuval Tassa 228264c92b Add efficient finite-difference Jacobians of mj_step.
- Add `qH` and `qHDiagInv` to `mjData` to save factorized modified inertia.
- Add `mj_EulerSkip`, `mj_implicitSkip`, to `engine_forward.c`.
- Using the above functions, implement `mj_stepSkip` in `engine_derivative.c`.
- Add `mjd_stepFD` and `mjd_transitionFD` to `engine_derivative.c` to compute `mj_step` Jacobians.
  - Exploit "Skip" functionality for speed.
  - Correctly handle quaternion derivatives.
  - Handle warmstarts and control limits.

PiperOrigin-RevId: 456584811
Change-Id: Iee8541f11e7b66feb8f431cb102d9bbe65461f79
2022-06-22 12:48:48 -07:00
Yuval Tassa 03c2011463 Enable activation clamping when using implicit integrator.
- Introduced private function `mj_advance()` as single point of state & time advancement.

PiperOrigin-RevId: 456525969
Change-Id: Iae17217e305c7baf07cb5ecd1e1b84b936421097
2022-06-22 08:55:48 -07:00
Nimrod Gileadi 2a26bf1ba5 Disable MSAN poisoning when printing using engine_print.
When using MSAN, mjData->buffer is marked as poisoned, despite being initialized with zeroes.
This is useful for catching computations that accidentally use uninitialized values.

However, when using engine_print, it's OK to assume the buffer is initialized to zeros.

PiperOrigin-RevId: 456085776
Change-Id: I9d85e37fa82958eb54ac413140543a6581aa46ad
2022-06-20 08:57:54 -07:00
Yuval Tassa 08b2db2f62 Allow in-place mju_rotVecMat and mju_rotVecMatT (res=vec)
PiperOrigin-RevId: 456021131
Change-Id: Id38ff5fabd15706d3087bc95d341b4f7a673449f
2022-06-20 01:57:45 -07:00
Nimrod Gileadi b1faec1dc2 Use _Generic instead of string comparisons for code that depends on field type.
This will allow us to add fields that can't be cast to int or double, without breaking print.

PiperOrigin-RevId: 456005913
Change-Id: I1fccfd11466f4745f69172af0ae61be07642927c
2022-06-20 00:03:17 -07:00
Saran Tunyasuvunakool 0e5ce18cff Manually parse inf and nan.
The C++ standard library `std::istringstream` is not guaranteed to parse "inf" and "nan" as valid floating point numbers. In our testing, libc++ does this, but libstdc++ and MSVCRT do not.

PiperOrigin-RevId: 455435431
Change-Id: I99c00303b08c2e4e62acfa3ca52a21a3008f4545
2022-06-16 11:57:52 -07:00
Alessio Quaglino 69a506e6ba Use C++ strcpy_arr in xml_native_reader_test.
PiperOrigin-RevId: 455375707
Change-Id: Ia3dbb11886ab29c4ad3192e4f78b4b60b94baad0
2022-06-16 07:15:14 -07:00
Alessio Quaglino 307cf692e5 Add <damper> actuator shortcut and related mjGAIN_AFFINE.
**Rationale**

A general `mjGAIN_AFFINE` actuator adds the term `force = (c + kp * length + kv * velocity) * ctrl`. The damper shortcut restricts it to `kv * velocity * ctrl` and sets the `ctrllimited` attribute to true.

**API**

```xml
<actuator>
  <damper ctrlrange="0 1" kv="1"/>
</actuator>
```

* Required attributes: `ctrlrange` (>=0)
* Optional attributes: `kv` (>=0)

PiperOrigin-RevId: 455366866
Change-Id: I88773abbeccabb6442f215c9fc05e205df63f692
2022-06-16 06:21:17 -07:00
Alessio Quaglino e0b6ba4a13 Raise warning if there are NaNs in the XML.
For Windows, throw an error if NaNs are present.

PiperOrigin-RevId: 455340787
Change-Id: Ibd95e2335ab15b706ee3328816983e5b57b2315e
2022-06-16 03:09:24 -07:00
Kevin Zakka 6048a55daf Add intvelocity actuator shortcut.
PiperOrigin-RevId: 454899823
Change-Id: I9b5494baacfab5a7d03bd680d0ded06a7790e75e
2022-06-14 10:36:58 -07:00
Kevin Zakka 0e1918782a Handle invalid joint type in URDF conversion. Fixes #317.
PiperOrigin-RevId: 454798685
Change-Id: Ic29a3756e78f8714ed8acc70b10e115c3df46561
2022-06-14 01:42:52 -07:00
DeepMind 8dda269c8a Avoid msan reports on globals destructors
Somehow tests load these libs twice, and construct/destruct globals twice.
Can be preproduced even without sanitizers with logging from GlobalModel::~GlobalModel.

themodel does not need to be visible. If it's static each instance will have own a copy.
_mjMap does not need to use string and avoid lifecycle issues at all.

PiperOrigin-RevId: 454678814
Change-Id: I8fca5e1f06cbc2b413beabcece1219445482c0ed
2022-06-13 12:59:09 -07:00
Kevin Zakka ec7133b0d3 More efficient implementation of mju_rotVecQuat.
```
Before:
BM_RotVecQuat_mean           10.1           10.1    833409628  99.055M items/s

After:
BM_RotVecQuat_mean            6.41           6.41  1200000000  156.095M items/s
```
PiperOrigin-RevId: 454601673
Change-Id: Ibb3ce8a6c838a88bdcebd7aaa979b649639edf0b
2022-06-13 07:16:55 -07:00
Copybara-Service d90d50629b Merge pull request #329 from liuliu:liu/fix-memory-issues
PiperOrigin-RevId: 454115420
Change-Id: I741f5f24b67ab79f19051e4e6d67c0a155f639e1
2022-06-10 02:28:18 -07:00
Alessio Quaglino 6ead14614f Adds rendering of ball and free joints actuators using an inflated joint geometry.
PiperOrigin-RevId: 453939612
Change-Id: I621f91d242b2974e6ae6acf239cdcb10efcb2629
2022-06-09 09:10:03 -07:00
Alessio Quaglino e050f36b5a Write sensor reftype and refname (if present) to XML file.
PiperOrigin-RevId: 453926332
Change-Id: I61f4aa2dc6186149b22af71eb8bb0fb2ffd84209
2022-06-09 08:03:47 -07:00
Alessio Quaglino d61a575a8c Replace fabs with std::abs.
Using fabs with int arguments broke the clang build on GitHub Actions, with a `-Wabsolute-value` error.

PiperOrigin-RevId: 453872474
Change-Id: I5c1278e9e628b0ea3d6f26c7ee7d6925c6276850
2022-06-09 02:31:18 -07:00
Alessio Quaglino c7c765b6eb Refactor mjXUtil::WriteAttr using templates.
PiperOrigin-RevId: 453848941
Change-Id: I1f87f0c20f0c89caab89ea4891aee7583a92534e
2022-06-08 23:33:49 -07:00
Alessio Quaglino 066367fcc8 Add visualisation flag for activations.
- Added illustrative model: `test/engine/testdata/activation.xml`

PiperOrigin-RevId: 453628961
Change-Id: I061ec3d17e57c6baffa0fe446a0be3564c44519d
2022-06-08 02:19:03 -07:00
Alessio Quaglino 736e0b391b Added visualization of site actuators.
PiperOrigin-RevId: 453609866
Change-Id: Icde220687f0c0e9e7fbbe0e3e56d995f823ffe07
2022-06-07 23:59:06 -07:00
Liu Liu f748018b28 Fix two asan detected memory bugs
I am running through simulate.swift (my port of simulate.cc) with
address sanitizer to discover related bugs.

Besides ones in my port, there are two in MuJoCo:

1. in maketext, the logic to find . is not protected against j is less
   than 0 (due to the decreasing logic above), creating out of bound
   access.

2. in mj_printFormattedData, qfrc_applied should use length nv not nq,
   otherwise out of bound access could be triggered.

Test Plan:

Run through the simulate.cc with asan. Before this fix, when presenting
profiler view, it will trigger bug #1. When print data, it will trigger
bug #2. Both are using model/humanoid/22_humanoids.xml.
2022-06-07 18:25:26 -04:00
Kevin Zakka 53d3220fcc Fix quaternion typos.
PiperOrigin-RevId: 453203844
Change-Id: I7d788c21bb00b7746f941fb2cf755f0b046e35f2
2022-06-06 08:24:21 -07:00
Alessio Quaglino 16baac9353 Throw an error if multiple orientation specifiers are used in the same element.
- Applies to [orientation specifiers](https://mujoco.readthedocs.io/en/latest/modeling.html#frame-orientations) in `body`, `inertial`, `geom`, `site`, `camera`.
- Before this change the check was done only for multiple *alternative* specifiers, but not for `quat` and an alternative specifier.
- Moved the check from the compiler to the parser.
- Added tests.

PiperOrigin-RevId: 453201981
Change-Id: I20907361f211dae904e734cd083e9df0efe4f654
2022-06-06 08:13:06 -07:00
Kevin Zakka d7ae7f5433 Improve error message when loading textures that are not PNGs.
PiperOrigin-RevId: 452052514
Change-Id: Ic2f0e8717e43d1a17b2d07ac0e07efe96c5ae1bd
2022-05-31 08:27:05 -07:00
Saran Tunyasuvunakool 5cc73c30bf Align all arrays in mjData's buffer to 64 byte boundaries.
The introduction of additional mjData arrays for the implicit integrator caused in a shift in data alignment in memory. In certain combinations of machines and models, this change resulted in ~5% performance regression that appears to be attributable to L2 cache misses.

This change aligns all arrays in mjData to the boundary of a typical L2 cache line size. Benchmarking shows that this resolves the performance regression on a Broadwell machine.

PiperOrigin-RevId: 451652783
Change-Id: I4ed2c2d03453a83a0b4a9b8e224e2fd7c0dadea9
2022-05-28 13:03:38 -07:00
Yuval Tassa 981fa22302 Don't enforce minimum inertia at mesh loading.
Inertias of moving bodies are enforced to be greater mjMINVAL at model compilation, there is no need to enforce inertias at mesh load time. Tiny meshes could be static or part of a larger geom assembly, in which case their tiny inertia is not a problem.

Also, make error message for small masses/inertias more precise.

PiperOrigin-RevId: 451625216
Change-Id: I4b48d7a881e620d6fbb591ec77fbdacc7c03496c
2022-05-28 06:43:24 -07:00
Yuval Tassa e94a46c20f Indent #ifdef blocks, improve comment for MSVC pragma.
PiperOrigin-RevId: 451614625
Change-Id: I5b46572521955b51da48f183d2195817df6ea28d
2022-05-28 04:49:58 -07:00
Saran Tunyasuvunakool 79082ea1ae Fix an out-of-bound read bug in mj_collision exclusion handling.
PiperOrigin-RevId: 451609651
Change-Id: Ifc10753ee65d176e609a9b742414d2c2f13d8bd0
2022-05-28 04:02:02 -07:00
Saran Tunyasuvunakool 1da4e1c784 Fix ASAN_(UN)POISON_MEMORY_REGION macros for MSVC.
Closes #306.

PiperOrigin-RevId: 451609183
Change-Id: Iadc2db7c5550261050340e38b7f7eb8a806013a1
2022-05-28 03:56:32 -07:00
Saran Tunyasuvunakool b5a6a725e5 Asan-poison padding regions in mjModel and mjData buffers.
This allows us to detect reads and writes that are logically out of bounds.

PiperOrigin-RevId: 451520772
Change-Id: I614a8062c404d97148166bcd37db399420956d8b
2022-05-27 17:34:29 -07:00
Saran Tunyasuvunakool 185b79f664 Make msan treat mjData buffer as uninitialized in mj_resetData.
Indiscriminate memset into d->buffer and m->buffer previously caused msan to not detect uninitialized reads.

Also fix tests with uninitialized read bugs that are detected by msan after this change.

PiperOrigin-RevId: 451508224
Change-Id: I1f4b080a8ef765c34ba7a0adc2c686419f6e5516
2022-05-27 16:34:33 -07:00
Saran Tunyasuvunakool 6faf1d93de Use explicit template instantiation for mjXUtil::ReadAttr.
PiperOrigin-RevId: 451481936
Change-Id: Ib26e41823606e612f017db0f606983262713066e
2022-05-27 14:35:05 -07:00
Alessio Quaglino f3453cf87f Stricter parsing of XML array attributes.
PiperOrigin-RevId: 451456510
Change-Id: I2556d4365762378261c04d3c9f1942166fbd156e
2022-05-27 12:51:02 -07:00
Nimrod Gileadi f713e81297 Explicitly reset xipos for worldbody.
Before this change, xipos[0,1,2] was only set to 0 by mj_resetData zeroing the entire buffer.

When using mj_resetDataDebug, the value would be wrong.

PiperOrigin-RevId: 450939814
Change-Id: I887dd977161c7d132a80b766db7be23350ffb50b
2022-05-25 09:22:06 -07:00
Alessio Quaglino 223874c4ca Correctly handle the case of explicit zero mass in xml_native_writer.
PiperOrigin-RevId: 450901455
Change-Id: I77aaf2a214ac05ed1ed1e6ffe473bf98f2fea8ef
2022-05-25 05:29:25 -07:00
Alessio Quaglino 43e3b3a3f9 If <composite flatinertia="true">, set mjCBody.explicit_inertial so that an <inertial> element is added in mj_saveLastXML. Bug found by the WriteReadCompare test.
PiperOrigin-RevId: 450702639
Change-Id: I978dfb6713d68ce8327f2e8eea1fc31f43a38b5e
2022-05-24 09:38:53 -07:00
Alessio Quaglino 20cc535ba6 xml_native_writer saves geom mass or density, if specified.
Fixes a bug introduced in CL/432455486, which stopped saving explicit inertias if they were inferred from geoms. But if we don't save inertias then we need to save mass/density specifiers.

BEGIN_PUBLIC

xml_native_writer saves geom mass or density, if specified.

END_PUBLIC

PiperOrigin-RevId: 450667775
Change-Id: Ibb5b269b58f1143013091729642bb501243ed943
2022-05-24 06:33:24 -07:00
DeepMind 64bc6d27b2 Add implicit integrator.
Added analytic derivatives of smooth (unconstrained) dynamics forces, with respect to velocities:
  - Centripetal and Coriolis forces computed by the Recursive Newton-Euler algorithm.
  - Damping and fluid-drag passive forces.
  - Actuation forces.

A new implicit-in-velocity integrator is implemented using the analytic derivatives. This integrator lies between the Euler and Runge Kutta integrators in terms of both stability and computational cost.

PiperOrigin-RevId: 450377010
Change-Id: Ie192b441876c22e732fb749333926f296e0a09cc
2022-05-23 01:22:15 -07:00
Saran Tunyasuvunakool 1913a02b40 Initial open sourcing of MuJoCo.
PiperOrigin-RevId: 450374687
Change-Id: Ie3225a46ce095fc28ae8e63c326a640261f562bb
2022-05-23 01:08:49 -07:00