浏览代码

copy sensor from robot branch, fix space

/PhysXArticulations20201Package
Chris Elion 5 年前
当前提交
dc4a001f
共有 7 个文件被更改,包括 279 次插入9 次删除
  1. 42
      Project/Assets/ML-Agents/Examples/Crawler/Prefabs/ArticulatedDynamicPlatform.prefab
  2. 2
      Project/Assets/ML-Agents/Examples/Crawler/Scripts/ArticulatedCrawlerAgent.cs
  3. 6
      Project/Assets/ML-Agents/Examples/Crawler/Scripts/CrawlerAgent.cs
  4. 211
      Project/Assets/ML-Agents/Examples/SharedAssets/ArticulationBodySensor.cs
  5. 3
      Project/Assets/ML-Agents/Examples/SharedAssets/ArticulationBodySensor.cs.meta
  6. 21
      Project/Assets/ML-Agents/Examples/SharedAssets/ArticulationBodySensorComponent.cs
  7. 3
      Project/Assets/ML-Agents/Examples/SharedAssets/ArticulationBodySensorComponent.cs.meta

42
Project/Assets/ML-Agents/Examples/Crawler/Prefabs/ArticulatedDynamicPlatform.prefab


m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RayTracingMode: 2
m_RayTraceProcedural: 0
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:

m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RayTracingMode: 2
m_RayTraceProcedural: 0
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:

m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RayTracingMode: 2
m_RayTraceProcedural: 0
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:

m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RayTracingMode: 2
m_RayTraceProcedural: 0
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:

m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RayTracingMode: 2
m_RayTraceProcedural: 0
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:

m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RayTracingMode: 2
m_RayTraceProcedural: 0
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:

m_Father: {fileID: 4595975114078986}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &6137723383829725008
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 49929167984663177}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 4ae1ad4b0c504a2ab52e4dc39d7070af, type: 3}
m_Name:
m_EditorClassIdentifier:
RootBody: {fileID: 6595311048349768081}
Settings:
transformFormat: 1
jointAngleFunction: 2
--- !u!1001 &51054157378214557
PrefabInstance:
m_ObjectHideFlags: 0

type: 3}
propertyPath: m_ParentAnchorPosition.y
value: -0.15444611
objectReference: {fileID: 0}
- target: {fileID: 3485593964014911945, guid: 1661ce2a8a3cdfd3480c3d24bddd72f2,
type: 3}
propertyPath: m_BrainParameters.VectorObservationSize
value: 102
objectReference: {fileID: 0}
- target: {fileID: 5023289971425501548, guid: 1661ce2a8a3cdfd3480c3d24bddd72f2,
type: 3}

type: 3}
propertyPath: brain
value:
objectReference: {fileID: 11400000, guid: 0e3b44d36c7a047c4addb92457b12be5,
type: 2}
objectReference: {fileID: 0}
- target: {fileID: 8326893706244448119, guid: 1661ce2a8a3cdfd3480c3d24bddd72f2,
type: 3}
propertyPath: m_ParentAnchorPosition.x

objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 1661ce2a8a3cdfd3480c3d24bddd72f2, type: 3}
--- !u!1 &49929167984663177 stripped
GameObject:
m_CorrespondingSourceObject: {fileID: 1129387478859796, guid: 1661ce2a8a3cdfd3480c3d24bddd72f2,
type: 3}
m_PrefabInstance: {fileID: 51054157378214557}
m_PrefabAsset: {fileID: 0}
--- !u!4 &52452562844929545 stripped
Transform:
m_CorrespondingSourceObject: {fileID: 4283901937423508, guid: 1661ce2a8a3cdfd3480c3d24bddd72f2,

--- !u!171741748 &6595311048349768081 stripped
ArticulationBody:
m_CorrespondingSourceObject: {fileID: 6571351125315617548, guid: 1661ce2a8a3cdfd3480c3d24bddd72f2,
type: 3}
m_PrefabInstance: {fileID: 51054157378214557}
m_PrefabAsset: {fileID: 0}

2
Project/Assets/ML-Agents/Examples/Crawler/Scripts/ArticulatedCrawlerAgent.cs


if (bp.arb.transform != body)
{
var localPosRelToBody = body.InverseTransformPoint(arb.transform.position); // Translate from world space to body local space, since all articulations are children of body in hiearchy
vectorSensor.AddObservation(localPosRelToBody);
//vectorSensor.AddObservation(localPosRelToBody);
vectorSensor.AddObservation(bp.currentXNormalizedRot); // Current x rot
vectorSensor.AddObservation(bp.currentYNormalizedRot); // Current y rot
vectorSensor.AddObservation(bp.currentZNormalizedRot); // Current z rot

6
Project/Assets/ML-Agents/Examples/Crawler/Scripts/CrawlerAgent.cs


public MeshRenderer foot3;
public Material groundedMaterial;
public Material unGroundedMaterial;
bool m_IsNewDecisionStep;
int m_CurrentDecisionStep;
Quaternion m_LookRotation;
Matrix4x4 m_TargetDirMatrix;

m_JdController = GetComponent<JointDriveController>();
m_CurrentDecisionStep = 1;
m_DirToTarget = target.position - body.position;

{
GetRandomTargetPos();
}
m_IsNewDecisionStep = true;
m_CurrentDecisionStep = 1;
}
}
}

211
Project/Assets/ML-Agents/Examples/SharedAssets/ArticulationBodySensor.cs


using System;
using System.Collections.Generic;
using Unity.MLAgents.Sensors;
using UnityEngine;
[Serializable]
public class ArticulationBodySensorSettings
{
public enum TransformFormat
{
PositionForwardRight,
PositionQuaternion,
};
public enum JointAngleFunction
{
Ignore,
Fmod,
SinCos,
}
public static readonly Dictionary<TransformFormat, int> s_NumObservationsByTransformFormat =
new Dictionary<TransformFormat, int>
{
{ TransformFormat.PositionForwardRight, 9 }, // transform pos, fwd, right
{ TransformFormat.PositionQuaternion, 7 } // transform pos, quaternion
};
public static readonly Dictionary<JointAngleFunction, int> s_NumObservationsByJointAngleFunction =
new Dictionary<JointAngleFunction, int>
{
{ JointAngleFunction.Ignore, 0 },
{ JointAngleFunction.Fmod, 1 },
{ JointAngleFunction.SinCos, 2 }
};
public TransformFormat transformFormat = TransformFormat.PositionForwardRight;
public JointAngleFunction jointAngleFunction = JointAngleFunction.Fmod;
public int NumTransformObservations
{
get { return s_NumObservationsByTransformFormat[transformFormat]; }
}
public int NumJointAngleObservations
{
get { return s_NumObservationsByJointAngleFunction[jointAngleFunction]; }
}
}
public class ArticulationBodySensor : ISensor
{
string m_SensorName;
int[] m_Shape;
ArticulationBody[] m_Bodies;
ArticulationBodySensorSettings m_Settings;
public ArticulationBodySensor(ArticulationBody rootBody, ArticulationBodySensorSettings settings, string name = null)
{
m_SensorName = string.IsNullOrEmpty(name) ? $"ArticulationBodySensor:{rootBody.name}" : name;
m_Settings = settings;
// Note that m_Bodies[0] will always be rootBody
m_Bodies = rootBody.GetComponentsInChildren<ArticulationBody>();
var sensorSize = GetArticulationSensorSize(rootBody, settings);
m_Shape = new[] { sensorSize };
}
/// <inheritdoc/>
public int[] GetObservationShape()
{
return m_Shape;
}
/// <inheritdoc/>
public int Write(ObservationWriter writer)
{
int obsIndex = 0;
foreach (var body in m_Bodies)
{
obsIndex = WriteBody(writer, body, obsIndex);
}
return obsIndex;
}
/// <inheritdoc/>
public byte[] GetCompressedObservation()
{
throw new NotImplementedException();
}
/// <inheritdoc/>
public void Update()
{
}
/// <inheritdoc/>
public void Reset() { }
/// <inheritdoc/>
public SensorCompressionType GetCompressionType()
{
return SensorCompressionType.None;
}
/// <inheritdoc/>
public string GetName()
{
return m_SensorName;
}
public static int GetArticulationSensorSize(ArticulationBody rootBody, ArticulationBodySensorSettings settings)
{
if (rootBody == null)
{
return 0;
}
int numObs = 0;
var bodies = rootBody.GetComponentsInChildren<ArticulationBody>();
foreach (var childBody in bodies)
{
numObs += GetArticulationObservationSize(childBody, settings);
}
return numObs;
}
static int GetArticulationObservationSize(ArticulationBody body, ArticulationBodySensorSettings settings)
{
if (body == null)
{
return 0;
}
var transformObsSize = settings.NumTransformObservations;
// TODO more observations for dof depending on type
var obsPerDof = settings.NumJointAngleObservations;
var dof = body.dofCount;
return transformObsSize + dof * obsPerDof;
}
int WriteBody(ObservationWriter writer, ArticulationBody body, int observationIndex)
{
if (body == null)
{
// TODO - getting this error
// MissingReferenceException: The object of type 'ArticulationBody' has been destroyed but you are still trying to access it.
// Your script should either check if it is null or you should not destroy the object.
// Handle later.
return observationIndex;
}
var rootWorldTransform = m_Bodies[0].transform;
var modelSpacePos = rootWorldTransform.InverseTransformPoint(body.transform.position);
writer[observationIndex++] = modelSpacePos.x;
writer[observationIndex++] = modelSpacePos.y;
writer[observationIndex++] = modelSpacePos.z;
if (m_Settings.transformFormat == ArticulationBodySensorSettings.TransformFormat.PositionForwardRight)
{
var modelFwd = rootWorldTransform.InverseTransformDirection(body.transform.forward);
writer[observationIndex++] = modelFwd.x;
writer[observationIndex++] = modelFwd.y;
writer[observationIndex++] = modelFwd.z;
var modelRight = rootWorldTransform.InverseTransformDirection(body.transform.right);
writer[observationIndex++] = modelRight.x;
writer[observationIndex++] = modelRight.y;
writer[observationIndex++] = modelRight.z;
}
else
{
// TODO not 100% sure this is right
var modelSpaceRot = Quaternion.Inverse(rootWorldTransform.rotation) * body.transform.rotation;
writer[observationIndex++] = modelSpaceRot.x;
writer[observationIndex++] = modelSpaceRot.y;
writer[observationIndex++] = modelSpaceRot.z;
writer[observationIndex++] = modelSpaceRot.w;
}
// Write degree-of-freedom info. For now, assume all angular.
for (var dofIndex = 0; dofIndex < body.dofCount; dofIndex++)
{
var jointRotationRads = body.jointPosition[dofIndex];
if (m_Settings.jointAngleFunction == ArticulationBodySensorSettings.JointAngleFunction.Ignore)
{
// Nothing
}
else if (m_Settings.jointAngleFunction == ArticulationBodySensorSettings.JointAngleFunction.Fmod)
{
var jointRotationDegs = jointRotationRads * Mathf.Rad2Deg;
var rotationFmod = (jointRotationDegs / 360.0f) % 1f;
writer[observationIndex++] = rotationFmod;
}
else if (m_Settings.jointAngleFunction == ArticulationBodySensorSettings.JointAngleFunction.SinCos)
{
writer[observationIndex++] = Mathf.Sin(jointRotationRads);
writer[observationIndex++] = Mathf.Cos(jointRotationRads);
}
}
return observationIndex;
}
}

3
Project/Assets/ML-Agents/Examples/SharedAssets/ArticulationBodySensor.cs.meta


fileFormatVersion: 2
guid: e14bb25a95194b69987ea4f57e249169
timeCreated: 1589824795

21
Project/Assets/ML-Agents/Examples/SharedAssets/ArticulationBodySensorComponent.cs


using Unity.MLAgents.Sensors;
using UnityEngine;
public class ArticulationBodySensorComponent : SensorComponent
{
public ArticulationBody RootBody;
[SerializeField]
public ArticulationBodySensorSettings Settings = new ArticulationBodySensorSettings();
/// <inheritdoc/>
public override ISensor CreateSensor()
{
return new ArticulationBodySensor(RootBody, Settings);
}
/// <inheritdoc/>
public override int[] GetObservationShape()
{
return new[] { ArticulationBodySensor.GetArticulationSensorSize(RootBody, Settings) };
}
}

3
Project/Assets/ML-Agents/Examples/SharedAssets/ArticulationBodySensorComponent.cs.meta


fileFormatVersion: 2
guid: 4ae1ad4b0c504a2ab52e4dc39d7070af
timeCreated: 1589824814
正在加载...
取消
保存