GitHub
4 年前
当前提交
4f4f2445
共有 15 个文件被更改,包括 424 次插入 和 31 次删除
-
2com.unity.ml-agents.extensions/Runtime/Sensors/ArticulationBodyPoseExtractor.cs
-
12com.unity.ml-agents.extensions/Runtime/Sensors/ArticulationBodySensorComponent.cs
-
53com.unity.ml-agents.extensions/Runtime/Sensors/PhysicsBodySensor.cs
-
30com.unity.ml-agents.extensions/Runtime/Sensors/PhysicsSensorSettings.cs
-
18com.unity.ml-agents.extensions/Runtime/Sensors/PoseExtractor.cs
-
2com.unity.ml-agents.extensions/Runtime/Sensors/RigidBodyPoseExtractor.cs
-
13com.unity.ml-agents.extensions/Runtime/Sensors/RigidBodySensorComponent.cs
-
34com.unity.ml-agents.extensions/Tests/Editor/Sensors/ArticulationBodySensorTests.cs
-
22com.unity.ml-agents.extensions/Tests/Editor/Sensors/RigidBodySensorTests.cs
-
147com.unity.ml-agents.extensions/Runtime/Sensors/ArticulationBodyJointExtractor.cs
-
11com.unity.ml-agents.extensions/Runtime/Sensors/ArticulationBodyJointExtractor.cs.meta
-
27com.unity.ml-agents.extensions/Runtime/Sensors/IJointExtractor.cs
-
11com.unity.ml-agents.extensions/Runtime/Sensors/IJointExtractor.cs.meta
-
62com.unity.ml-agents.extensions/Runtime/Sensors/RigidBodyJointExtractor.cs
-
11com.unity.ml-agents.extensions/Runtime/Sensors/RigidBodyJointExtractor.cs.meta
|
|||
#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: |
|||
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
|
|
|||
fileFormatVersion: 2 |
|||
guid: 238d15f867b9c4ced9cef331b7420b27 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using Unity.MLAgents.Sensors; |
|||
|
|||
namespace Unity.MLAgents.Extensions.Sensors |
|||
{ |
|||
/// <summary>
|
|||
/// Interface for generating observations from a physical joint or constraint.
|
|||
/// </summary>
|
|||
public interface IJointExtractor |
|||
{ |
|||
/// <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); |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 2d2a01ea194334a4682d5c8cad4a956b |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System.Collections.Generic; |
|||
using UnityEngine; |
|||
using Unity.MLAgents.Sensors; |
|||
|
|||
namespace Unity.MLAgents.Extensions.Sensors |
|||
{ |
|||
public class RigidBodyJointExtractor : IJointExtractor |
|||
{ |
|||
Rigidbody m_Body; |
|||
Joint m_Joint; |
|||
|
|||
public RigidBodyJointExtractor(Rigidbody body) |
|||
{ |
|||
m_Body = body; |
|||
m_Joint = m_Body?.GetComponent<Joint>(); |
|||
} |
|||
|
|||
public int NumObservations(PhysicsSensorSettings settings) |
|||
{ |
|||
return NumObservations(m_Body, m_Joint, settings); |
|||
} |
|||
|
|||
public static int NumObservations(Rigidbody body, Joint joint, PhysicsSensorSettings settings) |
|||
{ |
|||
if(body == null || joint == null) |
|||
{ |
|||
return 0; |
|||
} |
|||
|
|||
var numObservations = 0; |
|||
if (settings.UseJointForces) |
|||
{ |
|||
// 3 force and 3 torque values
|
|||
numObservations += 6; |
|||
} |
|||
|
|||
return numObservations; |
|||
} |
|||
|
|||
public int Write(PhysicsSensorSettings settings, ObservationWriter writer, int offset) |
|||
{ |
|||
if (m_Body == null || m_Joint == null) |
|||
{ |
|||
return 0; |
|||
} |
|||
|
|||
var currentOffset = offset; |
|||
if (settings.UseJointForces) |
|||
{ |
|||
// Take tanh of the forces and torques to ensure they're in [-1, 1]
|
|||
writer[currentOffset++] = (float)System.Math.Tanh(m_Joint.currentForce.x); |
|||
writer[currentOffset++] = (float)System.Math.Tanh(m_Joint.currentForce.y); |
|||
writer[currentOffset++] = (float)System.Math.Tanh(m_Joint.currentForce.z); |
|||
|
|||
writer[currentOffset++] = (float)System.Math.Tanh(m_Joint.currentTorque.x); |
|||
writer[currentOffset++] = (float)System.Math.Tanh(m_Joint.currentTorque.y); |
|||
writer[currentOffset++] = (float)System.Math.Tanh(m_Joint.currentTorque.z); |
|||
} |
|||
return currentOffset - offset; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 5014d7ab95c6a44469f447b8a7019746 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue