Add user sensor bindings.

Stop force-setting nusersensor, instead allow MuJoCo to infer the size.

PiperOrigin-RevId: 494783883
Change-Id: Ia2a1b1803b2904f2dded7b790d01eb6f1920dbaf
This commit is contained in:
Tom Erez
2022-12-12 11:42:00 -08:00
committed by Copybara-Service
parent 7a248a8bde
commit 21f10ce1b6
6 changed files with 99 additions and 16 deletions
+10 -5
View File
@@ -401,12 +401,17 @@ public class MjScene : MonoBehaviour {
MjRoot.AppendChild(GenerateMjcfSection(
doc, components.Where(component => component is MjBaseConstraint), "equality"));
MjRoot.AppendChild(GenerateMjcfSection(
doc, components.Where(component => component is MjActuator), "actuator"));
MjRoot.AppendChild(GenerateMjcfSection(
doc, components.Where(component => component is MjBaseSensor), "sensor"));
MjRoot.AppendChild(
GenerateMjcfSection(doc,
components.Where(component => component is MjActuator)
.OrderBy(component => component.transform.GetSiblingIndex()),
"actuator"));
MjRoot.AppendChild(
GenerateMjcfSection(doc,
components.Where(component => component is MjBaseSensor)
.OrderBy(component => component.transform.GetSiblingIndex()),
"sensor"));
// Generate the Mjcf of the runtime dependencies added to the context.
_generationContext.GenerateMjcf(MjRoot);
return doc;
@@ -0,0 +1,46 @@
// Copyright 2019 DeepMind Technologies Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Linq;
using System.Xml;
using UnityEngine;
namespace Mujoco {
public class MjUserSensor : MjBaseSensor {
public String Name;
public int Dimension = 0;
public String UserData;
protected override XmlElement ToMjcf(XmlDocument doc) {
if (Dimension == 0) {
throw new MissingFieldException("Dimension should be larger than 0.");
}
var mjcf = doc.CreateElement("user");
mjcf.SetAttribute("name", Name);
mjcf.SetAttribute("dim", $"{Dimension}");
// TODO: add validation that UserData is a space-separated list of floating numbers?
mjcf.SetAttribute("user", UserData);
return mjcf;
}
protected override void FromMjcf(XmlElement mjcf) {
Name = mjcf.GetAttribute("name");
int.TryParse(mjcf.GetAttribute("dim"), out Dimension);
UserData = mjcf.GetAttribute("user");
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a0b10d0a10ca3ad8cb5fa2b6bcd331c1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+3
View File
@@ -376,6 +376,9 @@ public class MjcfImporter {
break;
}
break;
case "user":
sensorType = typeof(MjUserSensor);
break;
default:
Debug.Log($"The importer does not yet support sensor <{node.Name}>.");
break;
+1 -4
View File
@@ -72,12 +72,9 @@ public class MjcfGenerationContext {
"gravity", MjEngineTool.Vector3ToMjcf(MjEngineTool.MjVector3(Physics.gravity)));
optionMjcf.SetAttribute("timestep", MjEngineTool.MakeLocaleInvariant($"{Time.fixedDeltaTime}"));
var sizeMjcf = (XmlElement)mjcf.AppendChild(doc.CreateElement("size"));
sizeMjcf.SetAttribute("nuser_sensor", $"{_nuserSensor}");
var settings = MjGlobalSettings.Instance;
if (settings) {
settings.OptionSizeToMjcf(optionMjcf, sizeMjcf);
settings.OptionSizeToMjcf(optionMjcf, null);
}
}