-
Notifications
You must be signed in to change notification settings - Fork 4.4k
[MLA-1141] Rigidbody and ArticulationBody sensors #4192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
14a1f59
727fdbb
000f0e7
deb5bb5
28a7e8b
c4f4f97
4deda2f
a0e541c
6d2dac3
7c3736f
94a6917
413e613
1ecd2b8
5435ff9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,13 +5,20 @@ | |
|
||
namespace Unity.MLAgents.Extensions.Sensors | ||
{ | ||
|
||
/// <summary> | ||
/// Utility class to track a hierarchy of ArticulationBodies. | ||
/// </summary> | ||
public class ArticulationBodyPoseExtractor : PoseExtractor | ||
{ | ||
ArticulationBody[] m_Bodies; | ||
|
||
public ArticulationBodyPoseExtractor(ArticulationBody rootBody) | ||
{ | ||
if (rootBody == null) | ||
{ | ||
return; | ||
} | ||
|
||
if (!rootBody.isRoot) | ||
{ | ||
Debug.Log("Must pass ArticulationBody.isRoot"); | ||
|
@@ -38,23 +45,32 @@ public ArticulationBodyPoseExtractor(ArticulationBody rootBody) | |
|
||
for (var i = 1; i < numBodies; i++) | ||
{ | ||
var body = m_Bodies[i]; | ||
var parent = body.GetComponentInParent<ArticulationBody>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would always return itself. Fixed and added checks in the unit test. |
||
parentIndices[i] = bodyToIndex[parent]; | ||
var currentArticBody = m_Bodies[i]; | ||
// Component.GetComponentInParent will consider the provided object as well. | ||
// So start looking from the parent. | ||
var currentGameObject = currentArticBody.gameObject; | ||
var parentGameObject = currentGameObject.transform.parent; | ||
var parentArticBody = parentGameObject.GetComponentInParent<ArticulationBody>(); | ||
parentIndices[i] = bodyToIndex[parentArticBody]; | ||
} | ||
|
||
SetParentIndices(parentIndices); | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override Vector3 GetLinearVelocityAt(int index) | ||
{ | ||
return m_Bodies[index].velocity; | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override Pose GetPoseAt(int index) | ||
{ | ||
var body = m_Bodies[index]; | ||
var go = body.gameObject; | ||
var t = go.transform; | ||
return new Pose { rotation = t.rotation, position = t.position }; | ||
} | ||
|
||
|
||
} | ||
} | ||
#endif // UNITY_2020_1_OR_NEWER |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#if UNITY_2020_1_OR_NEWER | ||
using UnityEngine; | ||
using Unity.MLAgents.Sensors; | ||
|
||
namespace Unity.MLAgents.Extensions.Sensors | ||
{ | ||
public class ArticulationBodySensorComponent : SensorComponent | ||
{ | ||
public ArticulationBody RootBody; | ||
|
||
[SerializeField] | ||
public PhysicsSensorSettings Settings = PhysicsSensorSettings.Default(); | ||
public string sensorName; | ||
|
||
/// <summary> | ||
/// Creates a PhysicsBodySensor. | ||
/// </summary> | ||
/// <returns></returns> | ||
public override ISensor CreateSensor() | ||
{ | ||
return new PhysicsBodySensor(RootBody, Settings, sensorName); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override int[] GetObservationShape() | ||
{ | ||
if (RootBody == null) | ||
{ | ||
return new[] { 0 }; | ||
} | ||
|
||
// TODO static method in PhysicsBodySensor? | ||
// TODO only update PoseExtractor when body changes? | ||
var poseExtractor = new ArticulationBodyPoseExtractor(RootBody); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems a bit heavy handed, but it's not in a critical loop at the moment so maybe it's ok. looks like the TODOs would address this. |
||
var numTransformObservations = Settings.TransformSize(poseExtractor.NumPoses); | ||
return new[] { numTransformObservations }; | ||
} | ||
} | ||
|
||
} | ||
#endif // UNITY_2020_1_OR_NEWER |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using UnityEngine; | ||
using Unity.MLAgents.Sensors; | ||
|
||
namespace Unity.MLAgents.Extensions.Sensors | ||
{ | ||
/// <summary> | ||
/// ISensor implementation that generates observations for a group of Rigidbodies or ArticulationBodies. | ||
/// </summary> | ||
public class PhysicsBodySensor : ISensor | ||
{ | ||
int[] m_Shape; | ||
string m_SensorName; | ||
|
||
PoseExtractor m_PoseExtractor; | ||
PhysicsSensorSettings m_Settings; | ||
|
||
/// <summary> | ||
/// Construct a new PhysicsBodySensor | ||
/// </summary> | ||
/// <param name="rootBody"></param> | ||
/// <param name="settings"></param> | ||
/// <param name="sensorName"></param> | ||
public PhysicsBodySensor(Rigidbody rootBody, GameObject rootGameObject, PhysicsSensorSettings settings, string sensorName=null) | ||
{ | ||
m_PoseExtractor = new RigidBodyPoseExtractor(rootBody, rootGameObject); | ||
m_SensorName = string.IsNullOrEmpty(sensorName) ? $"PhysicsBodySensor:{rootBody?.name}" : sensorName; | ||
m_Settings = settings; | ||
|
||
var numTransformObservations = settings.TransformSize(m_PoseExtractor.NumPoses); | ||
m_Shape = new[] { numTransformObservations }; | ||
} | ||
|
||
#if UNITY_2020_1_OR_NEWER | ||
public PhysicsBodySensor(ArticulationBody rootBody, PhysicsSensorSettings settings, string sensorName=null) | ||
{ | ||
m_PoseExtractor = new ArticulationBodyPoseExtractor(rootBody); | ||
m_SensorName = string.IsNullOrEmpty(sensorName) ? $"ArticulationBodySensor:{rootBody?.name}" : sensorName; | ||
m_Settings = settings; | ||
|
||
var numTransformObservations = settings.TransformSize(m_PoseExtractor.NumPoses); | ||
m_Shape = new[] { numTransformObservations }; | ||
} | ||
#endif | ||
|
||
/// <inheritdoc/> | ||
public int[] GetObservationShape() | ||
{ | ||
return m_Shape; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public int Write(ObservationWriter writer) | ||
{ | ||
var numWritten = writer.WritePoses(m_Settings, m_PoseExtractor); | ||
return numWritten; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public byte[] GetCompressedObservation() | ||
{ | ||
return null; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public void Update() | ||
{ | ||
if (m_Settings.UseModelSpace) | ||
{ | ||
m_PoseExtractor.UpdateModelSpacePoses(); | ||
} | ||
|
||
if (m_Settings.UseLocalSpace) | ||
{ | ||
m_PoseExtractor.UpdateLocalSpacePoses(); | ||
} | ||
} | ||
|
||
/// <inheritdoc/> | ||
public void Reset() {} | ||
|
||
/// <inheritdoc/> | ||
public SensorCompressionType GetCompressionType() | ||
{ | ||
return SensorCompressionType.None; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public string GetName() | ||
{ | ||
return m_SensorName; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Package validation was complaining about this.