Merge pull request #1696 from Balint-H:fix/unity-position-kv

PiperOrigin-RevId: 639720779
Change-Id: Ic70d9e5fed5e03bdb247a045e13c915ebfeecd9b
This commit is contained in:
Copybara-Service
2024-06-03 03:44:29 -07:00
3 changed files with 18 additions and 6 deletions
@@ -35,6 +35,7 @@ namespace Mujoco {
private SerializedProperty _biasPrm;
// Position actuator properties.
private SerializedProperty _kp;
private SerializedProperty _kvp;
// Velocity actuator properties.
private SerializedProperty _kv;
// Cylinder actuator properties.
@@ -70,6 +71,7 @@ namespace Mujoco {
_biasPrm = customParams.FindPropertyRelative("BiasPrm");
// Position actuator properties.
_kp = customParams.FindPropertyRelative("Kp");
_kvp = customParams.FindPropertyRelative("Kvp");
// Velocity actuator properties.
_kv = customParams.FindPropertyRelative("Kv");
// Cylinder actuator properties.
@@ -166,6 +168,7 @@ namespace Mujoco {
private void EditPositionParams(MjActuator.CustomParameters parameters) {
EditorGUILayout.PropertyField(_kp);
EditorGUILayout.PropertyField(_kvp, new GUIContent("Kv"));
}
private void EditVelocityParams(MjActuator.CustomParameters parameters) {
+5
View File
@@ -164,11 +164,16 @@ public class MjActuator : MjComponent {
[AbsoluteValue]
public float Kp = 1.0f;
[AbsoluteValue]
public float Kvp;
public void PositionToMjcf(XmlElement mjcf) {
mjcf.SetAttribute("kp", MjEngineTool.MakeLocaleInvariant($"{Math.Abs(Kp)}"));
mjcf.SetAttribute("kv", MjEngineTool.MakeLocaleInvariant($"{Math.Abs(Kvp)}"));
}
public void PositionFromMjcf(XmlElement mjcf) {
Kp = mjcf.GetFloatAttribute("kp", defaultValue: 1.0f);
Kvp = mjcf.GetFloatAttribute("kv", defaultValue: 0f);
}
//// Velocity actuator parameters.
@@ -279,19 +279,22 @@ public class MjPositionActuatorTests {
Assert.That(_doc.OuterXml, Does.Contain($"<position"));
}
[TestCase(3, "3")]
[TestCase(-2, "2")]
public void BiasParamsMjcf(float value, string expected) {
_actuator.CustomParams.Kp = value;
[TestCase(3, 2, "3", "2")]
[TestCase(-2, -1, "2", "1")]
public void BiasParamsMjcf(float valueP, float valueV, string expectedP, string expectedV) {
_actuator.CustomParams.Kp = valueP;
_actuator.CustomParams.Kvp = valueV;
_doc.AppendChild(_actuator.GenerateMjcf("name", _doc));
Assert.That(_doc.OuterXml, Does.Contain($"kp=\"{expected}\""));
Assert.That(_doc.OuterXml, Does.Contain($"kp=\"{expectedP}\""));
Assert.That(_doc.OuterXml, Does.Contain($"kv=\"{expectedV}\""));
}
[Test]
public void ParseAllSettings() {
_doc.LoadXml("<position joint=\"my_joint\" kp=\"2\"/>");
_doc.LoadXml("<position joint=\"my_joint\" kp=\"2\" kv=\"1\"/>");
_actuator.ParseMjcf(_doc.GetElementsByTagName("position")[0] as XmlElement);
Assert.That(_actuator.CustomParams.Kp, Is.EqualTo(2));
Assert.That(_actuator.CustomParams.Kvp, Is.EqualTo(1));
}
[Test]
@@ -299,6 +302,7 @@ public class MjPositionActuatorTests {
_doc.LoadXml("<position joint=\"my_joint\"/>");
_actuator.ParseMjcf(_doc.GetElementsByTagName("position")[0] as XmlElement);
Assert.That(_actuator.CustomParams.Kp, Is.EqualTo(1));
Assert.That(_actuator.CustomParams.Kvp, Is.EqualTo(0));
}
}