-
Notifications
You must be signed in to change notification settings - Fork 4.4k
[MLA-1138] joint observations #4224
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2c4a9d7
joint extractors, articulation impl
5e6dcac
rb joint extractor WIP
dd5e686
rb joint extractor WIP
f65f7f7
cleanup and tests
3044754
rename and docstrings
82eee97
Merge remote-tracking branch 'origin/master' into MLA-1138-articJoints
15dfcc7
fix rename
fb2089a
undo pack job change
2b5bac7
comments
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
147 changes: 147 additions & 0 deletions
147
com.unity.ml-agents.extensions/Runtime/Sensors/ArticulationBodyJointExtractor.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| #if UNITY_2020_1_OR_NEWER | ||
|
|
||
| using System.Collections.Generic; | ||
| using UnityEngine; | ||
| using Unity.MLAgents.Sensors; | ||
|
|
||
| namespace Unity.MLAgents.Extensions.Sensors | ||
| { | ||
| public class ArticulationBodyJointExtractor : IJointExtractor | ||
| { | ||
| ArticulationBody m_Body; | ||
|
|
||
| public ArticulationBodyJointExtractor(ArticulationBody body) | ||
| { | ||
| m_Body = body; | ||
| } | ||
|
|
||
| public int NumObservations(PhysicsSensorSettings settings) | ||
| { | ||
| return NumObservations(m_Body, settings); | ||
| } | ||
|
|
||
| public static int NumObservations(ArticulationBody body, PhysicsSensorSettings settings) | ||
| { | ||
| if (body == null || body.isRoot) | ||
| { | ||
| return 0; | ||
| } | ||
|
|
||
| var totalCount = 0; | ||
| if (settings.UseJointPositionsAndAngles) | ||
| { | ||
| switch (body.jointType) | ||
| { | ||
| case ArticulationJointType.RevoluteJoint: | ||
surfnerd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| case ArticulationJointType.SphericalJoint: | ||
| // Both RevoluteJoint and SphericalJoint have all angular components. | ||
| // We use sine and cosine of the angles for the observations. | ||
| totalCount += 2 * body.dofCount; | ||
| break; | ||
| case ArticulationJointType.FixedJoint: | ||
| // Since FixedJoint can't moved, there aren't any interesting observations for it. | ||
| break; | ||
| case ArticulationJointType.PrismaticJoint: | ||
| // One linear component | ||
| totalCount += body.dofCount; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (settings.UseJointForces) | ||
| { | ||
| totalCount += body.dofCount; | ||
| } | ||
|
|
||
| return totalCount; | ||
| } | ||
|
|
||
| public int Write(PhysicsSensorSettings settings, ObservationWriter writer, int offset) | ||
| { | ||
| if (m_Body == null || m_Body.isRoot) | ||
| { | ||
| return 0; | ||
| } | ||
|
|
||
| var currentOffset = offset; | ||
|
|
||
| // Write joint positions | ||
| if (settings.UseJointPositionsAndAngles) | ||
| { | ||
| switch (m_Body.jointType) | ||
| { | ||
| case ArticulationJointType.RevoluteJoint: | ||
| case ArticulationJointType.SphericalJoint: | ||
| // All joint positions are angular | ||
| for (var dofIndex = 0; dofIndex < m_Body.dofCount; dofIndex++) | ||
| { | ||
| var jointRotationRads = m_Body.jointPosition[dofIndex]; | ||
| writer[currentOffset++] = Mathf.Sin(jointRotationRads); | ||
| writer[currentOffset++] = Mathf.Cos(jointRotationRads); | ||
| } | ||
| break; | ||
| case ArticulationJointType.FixedJoint: | ||
| // No observations | ||
| break; | ||
| case ArticulationJointType.PrismaticJoint: | ||
| writer[currentOffset++] = GetPrismaticValue(); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (settings.UseJointForces) | ||
| { | ||
| for (var dofIndex = 0; dofIndex < m_Body.dofCount; dofIndex++) | ||
| { | ||
| // take tanh to keep in [-1, 1] | ||
| writer[currentOffset++] = (float) System.Math.Tanh(m_Body.jointForce[dofIndex]); | ||
| } | ||
| } | ||
|
|
||
| return currentOffset - offset; | ||
| } | ||
|
|
||
| float GetPrismaticValue() | ||
| { | ||
| // Prismatic joints should have at most one free axis. | ||
| bool limited = false; | ||
| var drive = m_Body.xDrive; | ||
| if (m_Body.linearLockX == ArticulationDofLock.LimitedMotion) | ||
| { | ||
| drive = m_Body.xDrive; | ||
| limited = true; | ||
| } | ||
| else if (m_Body.linearLockY == ArticulationDofLock.LimitedMotion) | ||
| { | ||
| drive = m_Body.yDrive; | ||
| limited = true; | ||
| } | ||
| else if (m_Body.linearLockZ == ArticulationDofLock.LimitedMotion) | ||
| { | ||
| drive = m_Body.zDrive; | ||
| limited = true; | ||
| } | ||
|
|
||
| var jointPos = m_Body.jointPosition[0]; | ||
| if (limited) | ||
| { | ||
| // If locked, interpolate between the limits. | ||
| var upperLimit = drive.upperLimit; | ||
| var lowerLimit = drive.lowerLimit; | ||
| if (upperLimit <= lowerLimit) | ||
| { | ||
| // Invalid limits (probably equal), so don't try to lerp | ||
| return 0; | ||
| } | ||
| var invLerped = Mathf.InverseLerp(lowerLimit, upperLimit, jointPos); | ||
|
|
||
| // Convert [0, 1] -> [-1, 1] | ||
| var normalized = 2.0f * invLerped - 1.0f; | ||
| return normalized; | ||
| } | ||
| // take tanh() to keep in [-1, 1] | ||
| return (float) System.Math.Tanh(jointPos); | ||
| } | ||
| } | ||
| } | ||
| #endif | ||
11 changes: 11 additions & 0 deletions
11
com.unity.ml-agents.extensions/Runtime/Sensors/ArticulationBodyJointExtractor.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
com.unity.ml-agents.extensions/Runtime/Sensors/IJointExtractor.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| using Unity.MLAgents.Sensors; | ||
|
|
||
| namespace Unity.MLAgents.Extensions.Sensors | ||
| { | ||
| /// <summary> | ||
| /// Interface for generating observations from a physical joint or constraint. | ||
| /// </summary> | ||
| public interface IJointExtractor | ||
surfnerd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| /// <summary> | ||
| /// Determine the number of observations that would be generated for the particular joint | ||
| /// using the provided PhysicsSensorSettings. | ||
| /// </summary> | ||
| /// <param name="settings"></param> | ||
| /// <returns>Number of floats that will be written.</returns> | ||
| int NumObservations(PhysicsSensorSettings settings); | ||
|
|
||
| /// <summary> | ||
| /// Write the observations to the ObservationWriter, starting at the specified offset. | ||
| /// </summary> | ||
| /// <param name="settings"></param> | ||
| /// <param name="writer"></param> | ||
| /// <param name="offset"></param> | ||
| /// <returns>Number of floats that were written.</returns> | ||
| int Write(PhysicsSensorSettings settings, ObservationWriter writer, int offset); | ||
| } | ||
| } | ||
11 changes: 11 additions & 0 deletions
11
com.unity.ml-agents.extensions/Runtime/Sensors/IJointExtractor.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What happens in this case? Is this TODO later?
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.
That should "fall through" to
totalCount += 2 * body.dofCountright? Or is my understanding of switch statements obsolete?I believe (should verify with the physics folks) that:
RevoluteJoint: 1 angular dof
SphericalJoint: 3 angular dof
FixedJoint: 0 dof
PrismaticJoint: 1 linear dof
so both RevoluteJoint and SphericalJoint are basically the same (just different values for body.dofCount)
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.
fall through is what will happen, maybe a comment to display intent could help