Update docs/cleanup
This commit is contained in:
parent
ae1e43a332
commit
f611a0755c
@ -2,54 +2,77 @@
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace RoboidControl.Unity {
|
namespace RoboidControl.Unity {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The Unity representation of a Roboid Control differential drive
|
||||||
|
/// </summary>
|
||||||
public class DifferentialDrive : Thing {
|
public class DifferentialDrive : Thing {
|
||||||
|
|
||||||
public Wheel leftWheel;
|
/// <summary>
|
||||||
public Wheel rightWheel;
|
/// The core differential drive
|
||||||
public SphereCollider casterWheel;
|
/// </summary>
|
||||||
|
public RoboidControl.DifferentialDrive coreDrive => core as RoboidControl.DifferentialDrive;
|
||||||
protected RoboidControl.DifferentialDrive coreDrive => core as RoboidControl.DifferentialDrive;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create the Unity representation
|
/// Create the Unity representation
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="core">The core touch sensor</param>
|
/// <param name="coreDrive">The core touch sensor</param>
|
||||||
/// <returns>The Unity representation of the touch sensor</returns>
|
/// <returns>The Unity representation of the touch sensor</returns>
|
||||||
public static DifferentialDrive Create(RoboidControl.DifferentialDrive core) {
|
/// This uses a 'DifferentialDrive' resource when available for the Unity representation.
|
||||||
DifferentialDrive component = null;
|
/// If this is not available, a default representation is created.
|
||||||
Rigidbody rb = null;
|
public static DifferentialDrive Create(RoboidControl.DifferentialDrive coreDrive) {
|
||||||
|
DifferentialDrive differentialDrive;
|
||||||
GameObject prefab = (GameObject)Resources.Load("DifferentialDrive");
|
GameObject prefab = (GameObject)Resources.Load("DifferentialDrive");
|
||||||
if (prefab != null) {
|
if (prefab != null) {
|
||||||
// Use resource prefab when available
|
// Use resource prefab when available
|
||||||
GameObject gameObj = Instantiate(prefab);
|
GameObject gameObj = Instantiate(prefab);
|
||||||
component = gameObj.GetComponent<DifferentialDrive>();
|
differentialDrive = gameObj.GetComponent<DifferentialDrive>();
|
||||||
if (component != null)
|
differentialDrive.Init(coreDrive);
|
||||||
component.core = core;
|
|
||||||
|
|
||||||
rb = component.GetComponent<Rigidbody>();
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Fallback implementation
|
// Default implementation
|
||||||
GameObject gameObj = new(core.name);
|
GameObject gameObj = new(coreDrive.name);
|
||||||
component = gameObj.AddComponent<DifferentialDrive>();
|
differentialDrive = gameObj.AddComponent<DifferentialDrive>();
|
||||||
component.Init(core);
|
differentialDrive.Init(coreDrive);
|
||||||
|
|
||||||
rb = gameObj.AddComponent<Rigidbody>();
|
Rigidbody rb = gameObj.AddComponent<Rigidbody>();
|
||||||
rb.isKinematic = false;
|
rb.isKinematic = false;
|
||||||
rb.mass = 0.1f;
|
rb.mass = 0.1f;
|
||||||
}
|
}
|
||||||
return component;
|
return differentialDrive;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The left wheel of the differential drive
|
||||||
|
/// </summary>
|
||||||
|
public Wheel leftWheel;
|
||||||
|
/// <summary>
|
||||||
|
/// The right wheel of the differential drive
|
||||||
|
/// </summary>
|
||||||
|
public Wheel rightWheel;
|
||||||
|
/// <summary>
|
||||||
|
/// The caster wheel to keep the differential drive horizontal
|
||||||
|
/// </summary>
|
||||||
|
public SphereCollider casterWheel;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The rigidbody of the differential drive
|
||||||
|
/// </summary>
|
||||||
private Rigidbody rb = null;
|
private Rigidbody rb = null;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Start the Unity representation
|
||||||
|
/// </summary>
|
||||||
protected virtual void Start() {
|
protected virtual void Start() {
|
||||||
rb = GetComponent<Rigidbody>();
|
rb = GetComponent<Rigidbody>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Handle the binary event indicating a configuration change
|
||||||
|
/// </summary>
|
||||||
protected override void HandleBinary() {
|
protected override void HandleBinary() {
|
||||||
Debug.Log("Diff drive handle Binary");
|
// Ignore it when the wheel radius is not set
|
||||||
if (coreDrive.wheelRadius <= 0) // || coreDrive.wheelSeparation <= 0)
|
if (coreDrive.wheelRadius <= 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Destroy any (generic) thing with the same id
|
// Destroy any (generic) thing with the same id
|
||||||
@ -66,7 +89,6 @@ namespace RoboidControl.Unity {
|
|||||||
leftWheel.core ??= coreDrive.leftWheel;
|
leftWheel.core ??= coreDrive.leftWheel;
|
||||||
SphereCollider leftWheelCollider = leftWheel.GetComponent<SphereCollider>();
|
SphereCollider leftWheelCollider = leftWheel.GetComponent<SphereCollider>();
|
||||||
leftWheelCollider.radius = coreDrive.wheelRadius;
|
leftWheelCollider.radius = coreDrive.wheelRadius;
|
||||||
// leftWheelCollider.center = new Vector3(-coreDrive.wheelSeparation / 2, 0, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Destroy any (generic) thing with the same id
|
// Destroy any (generic) thing with the same id
|
||||||
@ -83,7 +105,6 @@ namespace RoboidControl.Unity {
|
|||||||
rightWheel.core ??= coreDrive.rightWheel;
|
rightWheel.core ??= coreDrive.rightWheel;
|
||||||
SphereCollider rightWheelCollider = rightWheel.GetComponent<SphereCollider>();
|
SphereCollider rightWheelCollider = rightWheel.GetComponent<SphereCollider>();
|
||||||
rightWheelCollider.radius = coreDrive.wheelRadius;
|
rightWheelCollider.radius = coreDrive.wheelRadius;
|
||||||
// rightWheelCollider.center = new Vector3(coreDrive.wheelSeparation / 2, 0, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (casterWheel == null) {
|
if (casterWheel == null) {
|
||||||
@ -99,6 +120,9 @@ namespace RoboidControl.Unity {
|
|||||||
casterWheel.center = new Vector3(0, 0, -wheelSeparation);
|
casterWheel.center = new Vector3(0, 0, -wheelSeparation);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Update the Unity representation state
|
||||||
|
/// </summary>
|
||||||
protected override void FixedUpdate() {
|
protected override void FixedUpdate() {
|
||||||
base.FixedUpdate();
|
base.FixedUpdate();
|
||||||
|
|
||||||
|
@ -5,42 +5,45 @@ using UnityEngine;
|
|||||||
namespace RoboidControl.Unity {
|
namespace RoboidControl.Unity {
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The Unity representation of a distance sensor
|
/// The Unity representation of a Roboid Control distance sensor
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class DistanceSensor : Thing {
|
public class DistanceSensor : Thing {
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The core distance sensor
|
/// The core distance sensor
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public new RoboidControl.DistanceSensor core {
|
public RoboidControl.DistanceSensor coreSensor => base.core as RoboidControl.DistanceSensor;
|
||||||
get => (RoboidControl.DistanceSensor)base.core;
|
|
||||||
set => base.core = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Start the Unity representation
|
/// Start the Unity representation
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected virtual void Start() {
|
protected virtual void Start() {
|
||||||
if (core == null) {
|
|
||||||
SiteServer siteServer = FindAnyObjectByType<SiteServer>();
|
|
||||||
SetCoreThing(new RoboidControl.DistanceSensor(siteServer.site));
|
|
||||||
}
|
|
||||||
|
|
||||||
StartCoroutine(MeasureDistance());
|
StartCoroutine(MeasureDistance());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create the Unity representation of the distance sensor
|
/// Create the Unity representation of the distance sensor
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="parent">The parent of the core distance sensor</param>
|
/// <param name="core">The core distance sensor</param>
|
||||||
/// <returns>The Unity representation of the distance sensor</returns>
|
/// <returns>The Unity representation of the distance sensor</returns>
|
||||||
public static DistanceSensor Create(RoboidControl.DistanceSensor core) {
|
/// This uses a 'DistanceSensor' resource when available for the Unity representation.
|
||||||
GameObject distanceObj = new("Distance sensor");
|
/// If this is not available, a default representation is created.
|
||||||
DistanceSensor component = distanceObj.AddComponent<DistanceSensor>();
|
public static DistanceSensor Create(RoboidControl.DistanceSensor coreSensor) {
|
||||||
if (core.parent != null && core.parent.component != null)
|
DistanceSensor distanceSensor;
|
||||||
distanceObj.transform.SetParent(core.parent.component.transform, false);
|
GameObject prefab = (GameObject)Resources.Load("DistanceSensor");
|
||||||
|
if (prefab != null) {
|
||||||
return component;
|
// Use resource prefab when available
|
||||||
|
GameObject gameObj = Instantiate(prefab);
|
||||||
|
distanceSensor = gameObj.GetComponent<DistanceSensor>();
|
||||||
|
distanceSensor.Init(coreSensor);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Default implementation
|
||||||
|
GameObject distanceObj = new(coreSensor.name);
|
||||||
|
distanceSensor = distanceObj.AddComponent<DistanceSensor>();
|
||||||
|
distanceSensor.Init(coreSensor);
|
||||||
|
}
|
||||||
|
return distanceSensor;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -53,10 +56,10 @@ namespace RoboidControl.Unity {
|
|||||||
Thing thing = hitInfo.transform.GetComponentInParent<Thing>();
|
Thing thing = hitInfo.transform.GetComponentInParent<Thing>();
|
||||||
if (thing == null) {
|
if (thing == null) {
|
||||||
// Debug.Log($"collision {hitInfo.transform.name} {hitInfo.distance}");
|
// Debug.Log($"collision {hitInfo.transform.name} {hitInfo.distance}");
|
||||||
core.distance = hitInfo.distance;
|
coreSensor.distance = hitInfo.distance;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
core.distance = 0;
|
coreSensor.distance = 0;
|
||||||
}
|
}
|
||||||
yield return new WaitForSeconds(0.1f);
|
yield return new WaitForSeconds(0.1f);
|
||||||
}
|
}
|
||||||
|
@ -2,50 +2,73 @@
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace RoboidControl.Unity {
|
namespace RoboidControl.Unity {
|
||||||
|
/// <summary>
|
||||||
|
/// The Unity representation of a Roboid Control motor
|
||||||
|
/// </summary>
|
||||||
public class Motor : Thing {
|
public class Motor : Thing {
|
||||||
|
|
||||||
public float maxSpeed = 5;
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create the Unity representation
|
/// The core motor
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="core">The core motor</param>
|
public RoboidControl.Motor coreMotor => base.core as RoboidControl.Motor;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create the Unity representation of the motor
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="coreMotor">The core motor</param>
|
||||||
/// <returns>The Unity representation of a motor</returns>
|
/// <returns>The Unity representation of a motor</returns>
|
||||||
public static Motor Create(RoboidControl.Motor core) {
|
/// This uses a 'Motor' resource when available for the Unity representation.
|
||||||
|
/// If this is not available, a default representation is created.
|
||||||
|
public static Motor Create(RoboidControl.Motor coreMotor) {
|
||||||
|
Motor motor;
|
||||||
GameObject prefab = (GameObject)Resources.Load("Motor");
|
GameObject prefab = (GameObject)Resources.Load("Motor");
|
||||||
if (prefab != null) {
|
if (prefab != null) {
|
||||||
// Use resource prefab when available
|
// Use resource prefab when available
|
||||||
GameObject gameObj = Instantiate(prefab);
|
GameObject gameObj = Instantiate(prefab);
|
||||||
Motor component = gameObj.GetComponent<Motor>();
|
motor = gameObj.GetComponent<Motor>();
|
||||||
if (component != null)
|
motor.Init(coreMotor);
|
||||||
component.core = core;
|
|
||||||
return component;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Fallback implementation
|
// Default implementation
|
||||||
GameObject gameObj = new(core.name);
|
GameObject gameObj = new(coreMotor.name);
|
||||||
Motor component = gameObj.AddComponent<Motor>();
|
motor = gameObj.AddComponent<Motor>();
|
||||||
component.Init(core);
|
motor.Init(coreMotor);
|
||||||
|
|
||||||
Rigidbody rb = gameObj.AddComponent<Rigidbody>();
|
Rigidbody rb = gameObj.AddComponent<Rigidbody>();
|
||||||
rb.isKinematic = true;
|
rb.isKinematic = true;
|
||||||
return component;
|
|
||||||
}
|
}
|
||||||
|
return motor;
|
||||||
}
|
}
|
||||||
|
|
||||||
public float rotationSpeed = 0.0f;
|
/// <summary>
|
||||||
|
/// The maximum rotation speed of the motor in rotations per second
|
||||||
|
/// </summary>
|
||||||
|
public float maxSpeed = 5;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The actual rotation speed in rotations per second
|
||||||
|
/// </summary>
|
||||||
|
public float rotationSpeed { get; protected set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Update the Unity state
|
||||||
|
/// </summary>
|
||||||
|
protected override void FixedUpdate() {
|
||||||
|
base.FixedUpdate();
|
||||||
|
// We rotate the first child of the motor, which should be the axle.
|
||||||
|
float rotation = 360 * this.rotationSpeed * Time.fixedDeltaTime;
|
||||||
|
if (this.transform.childCount > 0)
|
||||||
|
this.transform.GetChild(0).Rotate(rotation, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Handle the binary event containing the rotation speed
|
||||||
|
/// </summary>
|
||||||
protected override void HandleBinary() {
|
protected override void HandleBinary() {
|
||||||
RoboidControl.Motor coreMotor = core as RoboidControl.Motor;
|
RoboidControl.Motor coreMotor = core as RoboidControl.Motor;
|
||||||
this.rotationSpeed = coreMotor.targetSpeed * maxSpeed;
|
this.rotationSpeed = coreMotor.targetSpeed * maxSpeed;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void Update() {
|
|
||||||
base.Update();
|
|
||||||
// We rotate the first child of the motor, which should be the axle.
|
|
||||||
float rotation = 360 * this.rotationSpeed * Time.deltaTime;
|
|
||||||
if (this.transform.childCount > 0)
|
|
||||||
this.transform.GetChild(0).Rotate(rotation, 0, 0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
@ -6,35 +6,25 @@ using UnityEngine.Networking;
|
|||||||
namespace RoboidControl.Unity {
|
namespace RoboidControl.Unity {
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The representation of a Thing in Unity
|
/// The Unity representation fo a Roboid Control Thing
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class Thing : MonoBehaviour {
|
public class Thing : MonoBehaviour {
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The core C# thing
|
/// The core C# thing
|
||||||
/// </summary>
|
/// </summary>
|
||||||
//[field: SerializeField]
|
|
||||||
public RoboidControl.Thing core { get; set; }
|
public RoboidControl.Thing core { get; set; }
|
||||||
|
|
||||||
public SiteServer participant;
|
/// <summary>
|
||||||
|
/// The owner of this thing
|
||||||
private string modelUrl = null;
|
/// </summary>
|
||||||
|
public Participant owner;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Set the core C# thing
|
/// Create a Unity representation of a Thing
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected void SetCoreThing(RoboidControl.Thing thing) {
|
/// <param name="core">The core of the thing</param>
|
||||||
core = thing;
|
/// <returns>The created thing</returns>
|
||||||
core.component = this;
|
|
||||||
|
|
||||||
SiteServer siteServer = FindAnyObjectByType<SiteServer>();
|
|
||||||
if (siteServer == null || siteServer.site == null) {
|
|
||||||
Debug.LogWarning("No site server found");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
siteServer.site.Add(thing);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Thing Create(RoboidControl.Thing core) {
|
public static Thing Create(RoboidControl.Thing core) {
|
||||||
// Debug.Log("Creating new Unity thing");
|
// Debug.Log("Creating new Unity thing");
|
||||||
GameObject gameObj = string.IsNullOrEmpty(core.name) ?
|
GameObject gameObj = string.IsNullOrEmpty(core.name) ?
|
||||||
@ -45,11 +35,17 @@ namespace RoboidControl.Unity {
|
|||||||
return component;
|
return component;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initialize the Thing
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="core">The core of the thing</param>
|
||||||
|
/// This affects the parent and pose of the thing
|
||||||
protected void Init(RoboidControl.Thing core) {
|
protected void Init(RoboidControl.Thing core) {
|
||||||
this.core = core;
|
this.core = core;
|
||||||
this.core.component = this;
|
this.core.component = this;
|
||||||
this.participant = FindAnyObjectByType<SiteServer>();
|
this.owner = FindAnyObjectByType<SiteServer>();
|
||||||
core.owner = this.participant.coreParticipant;
|
core.owner = this.owner.coreParticipant;
|
||||||
|
|
||||||
if (core.parent != null && core.parent.component != null) {
|
if (core.parent != null && core.parent.component != null) {
|
||||||
this.transform.SetParent(core.parent.component.transform, false);
|
this.transform.SetParent(core.parent.component.transform, false);
|
||||||
this.transform.localPosition = Vector3.zero;
|
this.transform.localPosition = Vector3.zero;
|
||||||
@ -62,7 +58,7 @@ namespace RoboidControl.Unity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Update the Unity representation
|
/// Update the Unity rendering
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected virtual void Update() {
|
protected virtual void Update() {
|
||||||
if (core == null)
|
if (core == null)
|
||||||
@ -79,10 +75,16 @@ namespace RoboidControl.Unity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Update the Unity state (just calls UpdateThing)
|
||||||
|
/// </summary>
|
||||||
protected virtual void FixedUpdate() {
|
protected virtual void FixedUpdate() {
|
||||||
UpdateThing();
|
UpdateThing();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Update the Unity state
|
||||||
|
/// </summary>
|
||||||
public void UpdateThing() {
|
public void UpdateThing() {
|
||||||
if (core == null) {
|
if (core == null) {
|
||||||
Debug.Log($"{this} core thing is gone, self destruct in 0 seconds...");
|
Debug.Log($"{this} core thing is gone, self destruct in 0 seconds...");
|
||||||
@ -90,12 +92,16 @@ namespace RoboidControl.Unity {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (core.updateQueue.TryDequeue(out RoboidControl.Thing.UpdateEvent e))
|
while (core.updateQueue.TryDequeue(out RoboidControl.Thing.CoreEvent e))
|
||||||
HandleUpdateEvent(e);
|
HandleCoreEvent(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void HandleUpdateEvent(RoboidControl.Thing.UpdateEvent e) {
|
/// <summary>
|
||||||
switch (e.messageId) {
|
/// Handle events from the core thing
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="coreEvent">The core event to handle</param>
|
||||||
|
private void HandleCoreEvent(RoboidControl.Thing.CoreEvent coreEvent) {
|
||||||
|
switch (coreEvent.messageId) {
|
||||||
case ThingMsg.id:
|
case ThingMsg.id:
|
||||||
Debug.Log($"{this.core.id} Handle Thing");
|
Debug.Log($"{this.core.id} Handle Thing");
|
||||||
if (core.parent == null)
|
if (core.parent == null)
|
||||||
@ -113,7 +119,6 @@ namespace RoboidControl.Unity {
|
|||||||
if (extension == ".jpg" || extension == ".png")
|
if (extension == ".jpg" || extension == ".png")
|
||||||
StartCoroutine(LoadJPG());
|
StartCoroutine(LoadJPG());
|
||||||
|
|
||||||
this.modelUrl = core.modelUrl;
|
|
||||||
break;
|
break;
|
||||||
case PoseMsg.Id:
|
case PoseMsg.Id:
|
||||||
Debug.Log($"{this.core.id} Handle Pose");
|
Debug.Log($"{this.core.id} Handle Pose");
|
||||||
@ -126,6 +131,10 @@ namespace RoboidControl.Unity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Load and attach a JPG sprite visualization of the thing
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
private IEnumerator LoadJPG() {
|
private IEnumerator LoadJPG() {
|
||||||
UnityWebRequest request = UnityWebRequestTexture.GetTexture(core.modelUrl);
|
UnityWebRequest request = UnityWebRequestTexture.GetTexture(core.modelUrl);
|
||||||
yield return request.SendWebRequest();
|
yield return request.SendWebRequest();
|
||||||
@ -153,6 +162,11 @@ namespace RoboidControl.Unity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Handle a Pose event
|
||||||
|
/// </summary>
|
||||||
|
/// This can update the position and/or orientation when the velocity of the thing is zero.
|
||||||
|
/// If a velocity is not zero, the position and/or orientation update will be ignored
|
||||||
protected virtual void HandlePose() {
|
protected virtual void HandlePose() {
|
||||||
if (core.linearVelocity.distance == 0)
|
if (core.linearVelocity.distance == 0)
|
||||||
this.transform.localPosition = core.position.ToVector3();
|
this.transform.localPosition = core.position.ToVector3();
|
||||||
@ -160,6 +174,10 @@ namespace RoboidControl.Unity {
|
|||||||
this.transform.localRotation = core.orientation.ToQuaternion();
|
this.transform.localRotation = core.orientation.ToQuaternion();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Handle a Binary event
|
||||||
|
/// </summary>
|
||||||
protected virtual void HandleBinary() { }
|
protected virtual void HandleBinary() { }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,52 +4,36 @@ using UnityEngine;
|
|||||||
namespace RoboidControl.Unity {
|
namespace RoboidControl.Unity {
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The Unity representation of the TouchSensor
|
/// The Unity representation of a Roboid Control touch sensor
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class TouchSensor : Thing {
|
public class TouchSensor : Thing {
|
||||||
|
|
||||||
// public SiteServer participant;
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The core touch sensor
|
/// The core touch sensor
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public RoboidControl.TouchSensor coreSensor {
|
public RoboidControl.TouchSensor coreSensor => base.core as RoboidControl.TouchSensor;
|
||||||
get => base.core as RoboidControl.TouchSensor;
|
|
||||||
set => base.core = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
SphereCollider touchCollider = null;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Start the Unity represention
|
/// Create the Unity representation of the touch sensor
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected virtual void Start() {
|
/// <param name="coreSensor">The core touch sensor</param>
|
||||||
if (core == null) {
|
|
||||||
participant = FindAnyObjectByType<SiteServer>();
|
|
||||||
SetCoreThing(new RoboidControl.TouchSensor(participant.site));
|
|
||||||
}
|
|
||||||
touchCollider = GetComponent<SphereCollider>();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Create the Unity representation
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="core">The core touch sensor</param>
|
|
||||||
/// <returns>The Unity representation of the touch sensor</returns>
|
/// <returns>The Unity representation of the touch sensor</returns>
|
||||||
public static TouchSensor Create(RoboidControl.TouchSensor core) {
|
/// This uses a 'TouchSensor' resource when available for the Unity representation.
|
||||||
|
/// If this is not available, a default representation is created.
|
||||||
|
public static TouchSensor Create(RoboidControl.TouchSensor coreSensor) {
|
||||||
|
TouchSensor touchSensor;
|
||||||
GameObject prefab = (GameObject)Resources.Load("TouchSensor");
|
GameObject prefab = (GameObject)Resources.Load("TouchSensor");
|
||||||
if (prefab != null) {
|
if (prefab != null) {
|
||||||
// Use resource prefab when available
|
// Use resource prefab when available
|
||||||
GameObject gameObj = Instantiate(prefab);
|
GameObject gameObj = Instantiate(prefab);
|
||||||
TouchSensor component = gameObj.GetComponent<TouchSensor>();
|
touchSensor = gameObj.GetComponent<TouchSensor>();
|
||||||
if (component != null)
|
touchSensor.Init(coreSensor);
|
||||||
component.core = core;
|
|
||||||
return component;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Fallback implementation
|
// Default implementation
|
||||||
GameObject gameObj = new(core.name);
|
GameObject gameObj = new(coreSensor.name);
|
||||||
TouchSensor component = gameObj.AddComponent<TouchSensor>();
|
touchSensor = gameObj.AddComponent<TouchSensor>();
|
||||||
component.Init(core);
|
touchSensor.Init(coreSensor);
|
||||||
|
|
||||||
Rigidbody rb = gameObj.AddComponent<Rigidbody>();
|
Rigidbody rb = gameObj.AddComponent<Rigidbody>();
|
||||||
rb.isKinematic = true;
|
rb.isKinematic = true;
|
||||||
@ -57,30 +41,33 @@ namespace RoboidControl.Unity {
|
|||||||
SphereCollider collider = gameObj.AddComponent<SphereCollider>();
|
SphereCollider collider = gameObj.AddComponent<SphereCollider>();
|
||||||
collider.radius = 0.01f;
|
collider.radius = 0.01f;
|
||||||
collider.isTrigger = true;
|
collider.isTrigger = true;
|
||||||
return component;
|
|
||||||
}
|
}
|
||||||
|
return touchSensor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Handle touch trigger collider enter event
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="other">The collider which entered our trigger collider</param>
|
||||||
private void OnTriggerEnter(Collider other) {
|
private void OnTriggerEnter(Collider other) {
|
||||||
// Debug.Log("Touch?");
|
// Don't detect trigger colliders
|
||||||
if (other.isTrigger) {
|
if (other.isTrigger)
|
||||||
// Debug.Log($" was trigger {other.name}");
|
return;
|
||||||
return;
|
// Don't touch yourself
|
||||||
}
|
if (this.transform.root == other.transform.root)
|
||||||
if (this.transform.root == other.transform.root) {
|
return;
|
||||||
Debug.Log($" was myself {other.name}");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Debug.Log($"*** {this} Touch");
|
this.coreSensor.touchedSomething = true;
|
||||||
this.coreSensor.touchedSomething = true;
|
this.core.updateQueue.Enqueue(new RoboidControl.Thing.CoreEvent(BinaryMsg.Id));
|
||||||
this.core.updateQueue.Enqueue(new RoboidControl.Thing.UpdateEvent(BinaryMsg.Id));
|
|
||||||
}
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Handl touch trigger collider exit event
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="other">The collider which exited our trigger collider </param>
|
||||||
private void OnTriggerExit(Collider other) {
|
private void OnTriggerExit(Collider other) {
|
||||||
if (other.isTrigger)
|
if (other.isTrigger)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
Debug.Log($"*** {this} Touch end");
|
|
||||||
this.coreSensor.touchedSomething = false;
|
this.coreSensor.touchedSomething = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,10 @@
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace RoboidControl.Unity {
|
namespace RoboidControl.Unity {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The Unity representation of a Roboid Control wheel
|
||||||
|
/// </summary>
|
||||||
public class Wheel : Motor {
|
public class Wheel : Motor {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create the Unity representation
|
/// Create the Unity representation
|
||||||
|
18
src/Thing.cs
18
src/Thing.cs
@ -138,7 +138,7 @@ namespace RoboidControl {
|
|||||||
if (_name != value) {
|
if (_name != value) {
|
||||||
_name = value;
|
_name = value;
|
||||||
nameChanged = true;
|
nameChanged = true;
|
||||||
this.updateQueue.Enqueue(new UpdateEvent(NameMsg.Id));
|
this.updateQueue.Enqueue(new CoreEvent(NameMsg.Id));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -153,7 +153,7 @@ namespace RoboidControl {
|
|||||||
set {
|
set {
|
||||||
if (_modelUrl != value) {
|
if (_modelUrl != value) {
|
||||||
_modelUrl = value;
|
_modelUrl = value;
|
||||||
this.updateQueue.Enqueue(new UpdateEvent(ModelUrlMsg.Id));
|
this.updateQueue.Enqueue(new CoreEvent(ModelUrlMsg.Id));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -188,7 +188,7 @@ namespace RoboidControl {
|
|||||||
value.AddChild(this);
|
value.AddChild(this);
|
||||||
}
|
}
|
||||||
this.hierarchyChanged = true;
|
this.hierarchyChanged = true;
|
||||||
this.updateQueue.Enqueue(new UpdateEvent(ThingMsg.id));
|
this.updateQueue.Enqueue(new CoreEvent(ThingMsg.id));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -283,7 +283,7 @@ namespace RoboidControl {
|
|||||||
if (_position != value) {
|
if (_position != value) {
|
||||||
_position = value;
|
_position = value;
|
||||||
positionUpdated = true;
|
positionUpdated = true;
|
||||||
updateQueue.Enqueue(new UpdateEvent(PoseMsg.Id));
|
updateQueue.Enqueue(new CoreEvent(PoseMsg.Id));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -321,7 +321,7 @@ namespace RoboidControl {
|
|||||||
if (_linearVelocity != value) {
|
if (_linearVelocity != value) {
|
||||||
_linearVelocity = value;
|
_linearVelocity = value;
|
||||||
linearVelocityUpdated = true;
|
linearVelocityUpdated = true;
|
||||||
updateQueue.Enqueue(new UpdateEvent(PoseMsg.Id));
|
updateQueue.Enqueue(new CoreEvent(PoseMsg.Id));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -340,7 +340,7 @@ namespace RoboidControl {
|
|||||||
if (_angularVelocity != value) {
|
if (_angularVelocity != value) {
|
||||||
_angularVelocity = value;
|
_angularVelocity = value;
|
||||||
angularVelocityUpdated = true;
|
angularVelocityUpdated = true;
|
||||||
updateQueue.Enqueue(new UpdateEvent(PoseMsg.Id));
|
updateQueue.Enqueue(new CoreEvent(PoseMsg.Id));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -396,13 +396,13 @@ namespace RoboidControl {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class UpdateEvent {
|
public class CoreEvent {
|
||||||
public UpdateEvent(int messageId) {
|
public CoreEvent(int messageId) {
|
||||||
this.messageId = messageId;
|
this.messageId = messageId;
|
||||||
}
|
}
|
||||||
public int messageId; // see the communication messages
|
public int messageId; // see the communication messages
|
||||||
};
|
};
|
||||||
public ConcurrentQueue<UpdateEvent> updateQueue = new();
|
public ConcurrentQueue<CoreEvent> updateQueue = new();
|
||||||
|
|
||||||
#endregion Update
|
#endregion Update
|
||||||
|
|
||||||
|
@ -76,7 +76,7 @@ namespace RoboidControl {
|
|||||||
// this.rightWheel.position = new Spherical(distance, Direction.right);
|
// this.rightWheel.position = new Spherical(distance, Direction.right);
|
||||||
|
|
||||||
owner.Send(new BinaryMsg(owner.networkId, this));
|
owner.Send(new BinaryMsg(owner.networkId, this));
|
||||||
this.updateQueue.Enqueue(new UpdateEvent(BinaryMsg.Id));
|
this.updateQueue.Enqueue(new CoreEvent(BinaryMsg.Id));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief Directly specify the speeds of the motors
|
/// @brief Directly specify the speeds of the motors
|
||||||
@ -165,7 +165,7 @@ namespace RoboidControl {
|
|||||||
this.rightWheel = this.owner.Get(rightWheelId) as Motor;
|
this.rightWheel = this.owner.Get(rightWheelId) as Motor;
|
||||||
this._wheelRadius = LowLevelMessages.ReceiveFloat16(data, ref ix);
|
this._wheelRadius = LowLevelMessages.ReceiveFloat16(data, ref ix);
|
||||||
//this._wheelSeparation = LowLevelMessages.ReceiveFloat16(data, ref ix);
|
//this._wheelSeparation = LowLevelMessages.ReceiveFloat16(data, ref ix);
|
||||||
this.updateQueue.Enqueue(new UpdateEvent(BinaryMsg.Id));
|
this.updateQueue.Enqueue(new CoreEvent(BinaryMsg.Id));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@ namespace RoboidControl {
|
|||||||
set {
|
set {
|
||||||
if (value != this._targetAngularSpeed) {
|
if (value != this._targetAngularSpeed) {
|
||||||
this._targetAngularSpeed = value;
|
this._targetAngularSpeed = value;
|
||||||
updateQueue.Enqueue(new UpdateEvent(BinaryMsg.Id));
|
updateQueue.Enqueue(new CoreEvent(BinaryMsg.Id));
|
||||||
owner.Send(new BinaryMsg(this));
|
owner.Send(new BinaryMsg(this));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,7 @@ namespace RoboidControl {
|
|||||||
set {
|
set {
|
||||||
if (value != _targetSpeed) {
|
if (value != _targetSpeed) {
|
||||||
_targetSpeed = Float.Clamp(value, -1, 1);
|
_targetSpeed = Float.Clamp(value, -1, 1);
|
||||||
updateQueue.Enqueue(new UpdateEvent(BinaryMsg.Id));
|
updateQueue.Enqueue(new CoreEvent(BinaryMsg.Id));
|
||||||
owner.Send(new BinaryMsg(this));
|
owner.Send(new BinaryMsg(this));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user