| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | using System; |
| | using System.Collections.Generic; |
| | using System.Linq; |
| | using System.Xml; |
| | using UnityEngine; |
| |
|
| | namespace Mujoco { |
| | |
| | public class MjActuator : MjComponent { |
| |
|
| | public enum ActuatorType { |
| | General, |
| | Motor, |
| | Position, |
| | Velocity, |
| | Cylinder, |
| | Muscle |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | [Serializable] |
| | public class CommonParameters { |
| |
|
| | |
| | |
| | public bool CtrlLimited; |
| |
|
| | |
| | |
| | public bool ForceLimited; |
| |
|
| | |
| | public Vector2 CtrlRange; |
| |
|
| | |
| | public Vector2 ForceRange; |
| |
|
| | |
| | public Vector2 LengthRange; |
| |
|
| | |
| | |
| | |
| | |
| | public List<float> Gear = new List<float>() { 1.0f }; |
| |
|
| | public void ToMjcf(XmlElement mjcf) { |
| | mjcf.SetAttribute("ctrllimited", $"{CtrlLimited}".ToLowerInvariant()); |
| | mjcf.SetAttribute("forcelimited", $"{ForceLimited}".ToLowerInvariant()); |
| | mjcf.SetAttribute( |
| | "ctrlrange", |
| | MjEngineTool.MakeLocaleInvariant($"{MjEngineTool.GetSorted(CtrlRange).x} {MjEngineTool.GetSorted(CtrlRange).y}")); |
| | mjcf.SetAttribute( |
| | "forcerange", |
| | MjEngineTool.MakeLocaleInvariant($"{MjEngineTool.GetSorted(ForceRange).x} {MjEngineTool.GetSorted(ForceRange).y}")); |
| | mjcf.SetAttribute( |
| | "lengthrange", |
| | MjEngineTool.MakeLocaleInvariant($"{MjEngineTool.GetSorted(LengthRange).x} {MjEngineTool.GetSorted(LengthRange).y}")); |
| | mjcf.SetAttribute("gear", MjEngineTool.ListToMjcf(Gear)); |
| | } |
| |
|
| | public void FromMjcf(XmlElement mjcf) { |
| | CtrlRange = mjcf.GetVector2Attribute("ctrlrange", defaultValue: Vector2.zero); |
| | ForceRange = mjcf.GetVector2Attribute("forcerange", defaultValue: Vector2.zero); |
| | LengthRange = mjcf.GetVector2Attribute("lengthrange", defaultValue: Vector2.zero); |
| | Gear = mjcf.GetFloatArrayAttribute("gear", defaultValue: new float[] { 1.0f }).ToList(); |
| |
|
| | CtrlLimited = mjcf.GetLimitedAttribute("ctrllimited", |
| | rangeDefined: mjcf.HasAttribute("ctrlrange")); |
| | ForceLimited = mjcf.GetLimitedAttribute("forcelimited", |
| | rangeDefined: mjcf.HasAttribute("forcerange")); |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | [Serializable] |
| | public class CustomParameters { |
| |
|
| | |
| |
|
| | |
| | public MujocoLib.mjtDyn DynType; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | public MujocoLib.mjtGain GainType; |
| |
|
| | |
| | public MujocoLib.mjtBias BiasType; |
| |
|
| | |
| | |
| | |
| | |
| | public List<float> DynPrm = new List<float>() { 1.0f, 0.0f, 0.0f }; |
| |
|
| | |
| | |
| | |
| | |
| | public List<float> GainPrm = new List<float>() { 1.0f, 0.0f, 0.0f }; |
| |
|
| | |
| | |
| | public List<float> BiasPrm = new List<float>() { 0.0f, 0.0f, 0.0f }; |
| |
|
| | public void GeneralToMjcf(XmlElement mjcf) { |
| | mjcf.SetAttribute("dyntype", $"{DynType}".Substring(6).ToLowerInvariant()); |
| | mjcf.SetAttribute("gaintype", $"{GainType}".Substring(7).ToLowerInvariant()); |
| | mjcf.SetAttribute("biastype", $"{BiasType}".Substring(7).ToLowerInvariant()); |
| | mjcf.SetAttribute("dynprm", MjEngineTool.ListToMjcf(DynPrm)); |
| | mjcf.SetAttribute("gainprm", MjEngineTool.ListToMjcf(GainPrm)); |
| | mjcf.SetAttribute("biasprm", MjEngineTool.ListToMjcf(BiasPrm)); |
| | } |
| |
|
| | public void GeneralFromMjcf(XmlElement mjcf) { |
| | var dynTypeStr = mjcf.GetStringAttribute("dyntype", defaultValue: "none"); |
| | var gainTypeStr = mjcf.GetStringAttribute("gaintype", defaultValue: "fixed"); |
| | var biasTypeStr = mjcf.GetStringAttribute("biastype", defaultValue: "none"); |
| | var ignoreCase = true; |
| | Enum.TryParse<MujocoLib.mjtDyn>($"mjdyn_{dynTypeStr}", ignoreCase, out DynType); |
| | Enum.TryParse<MujocoLib.mjtGain>($"mjgain_{gainTypeStr}", ignoreCase, out GainType); |
| | Enum.TryParse<MujocoLib.mjtBias>($"mjbias_{biasTypeStr}", ignoreCase, out BiasType); |
| |
|
| | DynPrm = mjcf.GetFloatArrayAttribute( |
| | "dynprm", defaultValue: new float[] { 1.0f, 0.0f, 0.0f }).ToList(); |
| | GainPrm = mjcf.GetFloatArrayAttribute( |
| | "gainprm", defaultValue: new float[] { 1.0f, 0.0f, 0.0f }).ToList(); |
| | BiasPrm = mjcf.GetFloatArrayAttribute( |
| | "biasprm", defaultValue: new float[] { 0.0f, 0.0f, 0.0f }).ToList(); |
| | } |
| |
|
| | |
| |
|
| | |
| | [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); |
| | } |
| |
|
| | |
| |
|
| | |
| | [AbsoluteValue] |
| | public float Kv = 1.0f; |
| |
|
| | public void VelocityToMjcf(XmlElement mjcf) { |
| | mjcf.SetAttribute("kv", MjEngineTool.MakeLocaleInvariant($"{Math.Abs(Kv)}")); |
| | } |
| | public void VelocityFromMjcf(XmlElement mjcf) { |
| | Kv = mjcf.GetFloatAttribute("kv", defaultValue: 1.0f); |
| | } |
| |
|
| | |
| |
|
| | |
| | public float CylinderTimeConst = 1.0f; |
| |
|
| | |
| | [AbsoluteValue] |
| | public float Area = 1.0f; |
| |
|
| | |
| | |
| | [AbsoluteValue] |
| | public float Diameter = 0.0f; |
| |
|
| | |
| | public float[] Bias = new float[] { 0.0f, 0.0f, 0.0f }; |
| |
|
| | public void CylinderToMjcf(XmlElement mjcf) { |
| | mjcf.SetAttribute("timeconst", MjEngineTool.MakeLocaleInvariant($"{CylinderTimeConst}")); |
| | mjcf.SetAttribute("area", MjEngineTool.MakeLocaleInvariant($"{Math.Abs(Area)}")); |
| | mjcf.SetAttribute("diameter", MjEngineTool.MakeLocaleInvariant($"{Math.Abs(Diameter)}")); |
| | mjcf.SetAttribute("bias", MjEngineTool.ArrayToMjcf(Bias)); |
| | } |
| |
|
| | public void CylinderFromMjcf(XmlElement mjcf) { |
| | CylinderTimeConst = mjcf.GetFloatAttribute("timeconst", defaultValue: 1.0f); |
| | Area = mjcf.GetFloatAttribute("area", defaultValue: 1.0f); |
| | Diameter = mjcf.GetFloatAttribute("diameter", defaultValue: 0.0f); |
| | Bias = mjcf.GetFloatArrayAttribute("bias", defaultValue: new float[] { 0.0f, 0.0f, 0.0f }); |
| | } |
| |
|
| | |
| |
|
| | |
| | public Vector2 MuscleTimeConst = new Vector2(0.01f, 0.04f); |
| |
|
| | |
| | public Vector2 Range = new Vector2(0.75f, 1.05f); |
| |
|
| | |
| | |
| | public float Force = -1.0f; |
| |
|
| | |
| | |
| | |
| | |
| | public float Scale = 200.0f; |
| |
|
| | |
| | public float LMin = 0.5f; |
| |
|
| | |
| | public float LMax = 1.6f; |
| |
|
| | |
| | public float VMax = 1.5f; |
| |
|
| | |
| | public float FpMax = 1.3f; |
| |
|
| | |
| | public float FvMax = 1.2f; |
| |
|
| | public void MuscleToMjcf(XmlElement mjcf) { |
| | mjcf.SetAttribute("timeconst", MjEngineTool.MakeLocaleInvariant($"{MuscleTimeConst[0]} {MuscleTimeConst[1]}")); |
| | mjcf.SetAttribute( |
| | "range", MjEngineTool.MakeLocaleInvariant($"{MjEngineTool.GetSorted(Range).x} {MjEngineTool.GetSorted(Range).y}")); |
| | mjcf.SetAttribute("force", MjEngineTool.MakeLocaleInvariant($"{Force}")); |
| | mjcf.SetAttribute("scale", MjEngineTool.MakeLocaleInvariant($"{Scale}")); |
| | mjcf.SetAttribute("lmin", MjEngineTool.MakeLocaleInvariant($"{LMin}")); |
| | mjcf.SetAttribute("lmax", MjEngineTool.MakeLocaleInvariant($"{LMax}")); |
| | mjcf.SetAttribute("vmax", MjEngineTool.MakeLocaleInvariant($"{VMax}")); |
| | mjcf.SetAttribute("fpmax", MjEngineTool.MakeLocaleInvariant($"{FpMax}")); |
| | mjcf.SetAttribute("fvmax", MjEngineTool.MakeLocaleInvariant($"{FvMax}")); |
| | } |
| |
|
| | public void MuscleFromMjcf(XmlElement mjcf) { |
| | MuscleTimeConst = mjcf.GetVector2Attribute( |
| | "timeconst", defaultValue: new Vector2(0.01f, 0.04f)); |
| | Range = mjcf.GetVector2Attribute("range", defaultValue: new Vector2(0.75f, 1.05f)); |
| | Force = mjcf.GetFloatAttribute("force", defaultValue: -1.0f); |
| | Scale = mjcf.GetFloatAttribute("scale", defaultValue: 200.0f); |
| | LMin = mjcf.GetFloatAttribute("lmin", defaultValue: 0.5f); |
| | LMax = mjcf.GetFloatAttribute("lmax", defaultValue: 1.6f); |
| | VMax = mjcf.GetFloatAttribute("vmax", defaultValue: 1.5f); |
| | FpMax = mjcf.GetFloatAttribute("fpmax", defaultValue: 1.3f); |
| | FvMax = mjcf.GetFloatAttribute("fvmax", defaultValue: 1.2f); |
| | } |
| | } |
| |
|
| | public ActuatorType Type; |
| |
|
| | [Tooltip("Joint actuation target. Mutually exclusive with tendon target.")] |
| | public MjBaseJoint Joint; |
| |
|
| | [Tooltip("Tendon actuation target. Mutually exclusive with joint target.")] |
| | public MjBaseTendon Tendon; |
| |
|
| | [Tooltip("Parameters specific to each actuator type.")] |
| | [HideInInspector] |
| | public CustomParameters CustomParams = new CustomParameters(); |
| |
|
| | [Tooltip("Parameters shared by all types of actuators.")] |
| | public CommonParameters CommonParams = new CommonParameters(); |
| |
|
| | [Tooltip("Actuator control.")] |
| | public float Control; |
| |
|
| | |
| | public float Length { get; private set; } |
| |
|
| | |
| | public float Velocity { get; private set; } |
| |
|
| | |
| | public float Force { get; private set; } |
| |
|
| | public override MujocoLib.mjtObj ObjectType => MujocoLib.mjtObj.mjOBJ_ACTUATOR; |
| |
|
| | |
| | protected override void OnParseMjcf(XmlElement mjcf) { |
| | if (!Enum.TryParse(mjcf.Name, ignoreCase: true, result: out Type)) { |
| | throw new ArgumentException($"Unknown actuator type {mjcf.Name}."); |
| | } |
| | CommonParams.FromMjcf(mjcf); |
| | switch (Type) { |
| | case MjActuator.ActuatorType.General: { |
| | CustomParams.GeneralFromMjcf(mjcf); |
| | break; |
| | } |
| | case MjActuator.ActuatorType.Position: { |
| | CustomParams.PositionFromMjcf(mjcf); |
| | break; |
| | } |
| | case MjActuator.ActuatorType.Velocity: { |
| | CustomParams.VelocityFromMjcf(mjcf); |
| | break; |
| | } |
| | case MjActuator.ActuatorType.Cylinder: { |
| | CustomParams.CylinderFromMjcf(mjcf); |
| | break; |
| | } |
| | case MjActuator.ActuatorType.Muscle: { |
| | CustomParams.MuscleFromMjcf(mjcf); |
| | break; |
| | } |
| | } |
| | Joint = mjcf.GetObjectReferenceAttribute<MjBaseJoint>("joint"); |
| | Tendon = mjcf.GetObjectReferenceAttribute<MjBaseTendon>("tendon"); |
| | } |
| |
|
| | |
| | protected override XmlElement OnGenerateMjcf(XmlDocument doc) { |
| | if (Joint == null && Tendon == null) { |
| | throw new InvalidOperationException($"Actuator {name} is not assigned a joint nor tendon."); |
| | } |
| | if (Joint != null && Tendon != null) { |
| | throw new InvalidOperationException( |
| | $"Actuator {name} can't have both a tendon and a joint target."); |
| | } |
| |
|
| | var mjcf = doc.CreateElement(Type.ToString().ToLowerInvariant()); |
| | if (Joint != null) { |
| | mjcf.SetAttribute("joint", Joint.MujocoName); |
| | } else { |
| | mjcf.SetAttribute("tendon", Tendon.MujocoName); |
| | } |
| | CommonParams.ToMjcf(mjcf); |
| |
|
| | switch (Type) { |
| | case MjActuator.ActuatorType.General: { |
| | CustomParams.GeneralToMjcf(mjcf); |
| | break; |
| | } |
| | case MjActuator.ActuatorType.Position: { |
| | CustomParams.PositionToMjcf(mjcf); |
| | break; |
| | } |
| | case MjActuator.ActuatorType.Velocity: { |
| | CustomParams.VelocityToMjcf(mjcf); |
| | break; |
| | } |
| | case MjActuator.ActuatorType.Cylinder: { |
| | CustomParams.CylinderToMjcf(mjcf); |
| | break; |
| | } |
| | case MjActuator.ActuatorType.Muscle: { |
| | CustomParams.MuscleToMjcf(mjcf); |
| | break; |
| | } |
| | } |
| | return mjcf; |
| | } |
| |
|
| | |
| | public override unsafe void OnSyncState(MujocoLib.mjData_* data) { |
| | data->ctrl[MujocoId] = Control; |
| | Length = (float)data->actuator_length[MujocoId]; |
| | Velocity = (float)data->actuator_velocity[MujocoId]; |
| | Force = (float)data->actuator_force[MujocoId]; |
| | } |
| |
|
| | public void OnValidate() { |
| | if (Joint != null && Tendon != null) { |
| | Debug.LogError( |
| | $"Actuator {name} can't have both a tendon and a joint target.", this); |
| | } |
| | } |
| | } |
| | } |
| |
|