Distance Sensor Support

This commit is contained in:
Pascal Serrarens 2025-06-19 12:00:47 +02:00
parent 66c494ad60
commit 534220a178
7 changed files with 95 additions and 60 deletions

View File

@ -14,6 +14,14 @@ namespace RoboidControl.Unity {
/// </summary> /// </summary>
public RoboidControl.DistanceSensor coreSensor => base.core as RoboidControl.DistanceSensor; public RoboidControl.DistanceSensor coreSensor => base.core as RoboidControl.DistanceSensor;
public float distance {
get {
if (coreSensor == null)
return float.PositiveInfinity;
return coreSensor.distance;
}
}
/// <summary> /// <summary>
/// Start the Unity representation /// Start the Unity representation
/// </summary> /// </summary>
@ -56,10 +64,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}");
coreSensor.distance = hitInfo.distance; coreSensor.internalDistance = hitInfo.distance;
} }
else else
coreSensor.distance = 0; coreSensor.internalDistance = float.PositiveInfinity;
} }
yield return new WaitForSeconds(0.1f); yield return new WaitForSeconds(0.1f);
} }

View File

@ -0,0 +1,15 @@
using UnityEditor;
namespace RoboidControl.Unity {
[CustomEditor(typeof(DistanceSensor))]
public class DistanceSensor_Editor : Editor {
public override void OnInspectorGUI() {
DistanceSensor distanceSensor = (DistanceSensor)target;
DrawDefaultInspector();
EditorGUILayout.LabelField("Distance", distanceSensor.distance.ToString());
}
}
}

View File

@ -41,13 +41,14 @@ namespace RoboidControl.Unity {
protected virtual void HandleThingEvent(RoboidControl.Participant.UpdateEvent e) { protected virtual void HandleThingEvent(RoboidControl.Participant.UpdateEvent e) {
switch (e.thing) { switch (e.thing) {
case RoboidControl.DistanceSensor coreDistanceSensor:
coreDistanceSensor.component = DistanceSensor.Create(coreDistanceSensor);
break;
case RoboidControl.TouchSensor coreTouchSensor: case RoboidControl.TouchSensor coreTouchSensor:
TouchSensor touchSensor = TouchSensor.Create(coreTouchSensor); coreTouchSensor.component = TouchSensor.Create(coreTouchSensor);
coreTouchSensor.component = touchSensor;
break; break;
case RoboidControl.DifferentialDrive coreDrive: case RoboidControl.DifferentialDrive coreDrive:
DifferentialDrive differentialDrive = DifferentialDrive.Create(coreDrive); coreDrive.component = DifferentialDrive.Create(coreDrive);
coreDrive.component = differentialDrive;
break; break;
case RoboidControl.Motor coreMotor: case RoboidControl.Motor coreMotor:
if (coreMotor.component == null) { if (coreMotor.component == null) {

View File

@ -493,6 +493,7 @@ namespace RoboidControl {
protected virtual Thing ProcessNewThing(Participant owner, ThingMsg msg, bool isRemote) { protected virtual Thing ProcessNewThing(Participant owner, ThingMsg msg, bool isRemote) {
// Console.WriteLine("--- New Thing"); // Console.WriteLine("--- New Thing");
Thing newThing = msg.thingType switch { Thing newThing = msg.thingType switch {
Thing.Type.DistanceSensor => new DistanceSensor(owner.root),
Thing.Type.TouchSensor => new TouchSensor(owner.root), Thing.Type.TouchSensor => new TouchSensor(owner.root),
Thing.Type.DifferentialDrive => new DifferentialDrive(owner.root), Thing.Type.DifferentialDrive => new DifferentialDrive(owner.root),
_ => new Thing(owner.root) _ => new Thing(owner.root)

View File

@ -109,12 +109,12 @@ namespace RoboidControl {
/// Function which can be used to create components in external engines. /// Function which can be used to create components in external engines.
/// </summary> /// </summary>
/// Currently this is used to create GameObjects in Unity /// Currently this is used to create GameObjects in Unity
public virtual void CreateComponent() { // public virtual void CreateComponent() {
#if UNITY_5_3_OR_NEWER // #if UNITY_5_3_OR_NEWER
this.component = Unity.Thing.Create(this); // this.component = Unity.Thing.Create(this);
this.component.core = this; // this.component.core = this;
#endif // #endif
} // }
#endregion Init #endregion Init

View File

@ -1,48 +1,72 @@
using System;
namespace RoboidControl { namespace RoboidControl {
/// <summary> /// <summary>
/// A sensor measuring the distance in the forward direction /// A sensor measuring distance
/// </summary> /// </summary>
public class DistanceSensor : Thing { public class DistanceSensor : Thing {
/// <summary>
/// The current measured distance
/// </summary>
public float distance = 0;
/*
/// <summary> /// <summary>
/// Constructor for a new distance sensor /// Create a new distance sensor
/// </summary> /// </summary>
/// <param name="participant">The participant for which the sensor is needed</param> /// <param name="parent">The parent thing</param>
public DistanceSensor(Participant participant) : base(participant, Type.Undetermined) { }
/// <summary>
/// Create a distance sensor with the given ID
/// </summary>
/// <param name="owner">The participant for with the sensor is needed</param>
/// <param name="networkId">The network ID of the sensor</param>
/// <param name="thingId">The ID of the thing</param>
public DistanceSensor(Participant owner, byte thingId) : base(owner, Type.TemperatureSensor, thingId) {}
*/
public DistanceSensor(Thing parent) : base(parent) { public DistanceSensor(Thing parent) : base(parent) {
this.type = Type.DistanceSensor; this.type = Type.DistanceSensor;
this.name = "Distance sensor";
} }
#if UNITY_5_3_OR_NEWER public float _internalDistance = float.PositiveInfinity;
/// @copydoc Passer::RoboidControl::Thing::CreateComponent public float internalDistance {
public override void CreateComponent() { get { return _internalDistance; }
this.component = Unity.DistanceSensor.Create(this); set {
this.component.core = this; if (value != _internalDistance) {
_internalDistance = value;
distanceUpdated = true;
owner.Send(new BinaryMsg(this));
}
}
} }
#endif protected float externalDistance = float.PositiveInfinity;
/// <summary>
/// The current distance
/// </summary>
public float distance {
get {
if (this.externalDistance < this.internalDistance)
return this.externalDistance;
else
return this.internalDistance;
}
}
protected bool distanceUpdated;
// #if UNITY_5_3_OR_NEWER
// /// @copydoc Passer::RoboidControl::Thing::CreateComponent
// public override void CreateComponent() {
// this.component = Unity.DistanceSensor.Create(this);
// this.component.core = this;
// }
// #endif
public override byte[] GenerateBinary() {
if (!distanceUpdated)
return Array.Empty<byte>();
byte[] bytes = new byte[2];
byte ix = 0;
LowLevelMessages.SendFloat16(bytes, ref ix, this.internalDistance);
distanceUpdated = false;
return bytes;
}
/// <summary> /// <summary>
/// Function to extract the distance received in the binary message /// Function to extract the distance received in the binary message
/// </summary> /// </summary>
/// <param name="bytes">The byte array</param> /// <param name="bytes">The byte array</param>
public override void ProcessBinary(byte[] bytes) { public override void ProcessBinary(byte[] bytes) {
byte ix = 0; byte ix = 0;
this.distance = LowLevelMessages.ReceiveFloat16(bytes, ref ix); this.externalDistance = LowLevelMessages.ReceiveFloat16(bytes, ref ix);
} }
} }

View File

@ -6,20 +6,6 @@ namespace RoboidControl {
/// A sensor which can detect touches /// A sensor which can detect touches
/// </summary> /// </summary>
public class TouchSensor : Thing { public class TouchSensor : Thing {
/*
/// <summary>
/// Create a touch sensor without communication abilities
/// </summary>
/// <param name="invokeEvent">Invoke a OnNewThing event when the thing has been created</param>
public TouchSensor(bool invokeEvent = true) : base(Type.TouchSensor, invokeEvent) { }
/// <summary>
/// Create a touch sensor for a participant
/// </summary>
/// <param name="owner">The owning participant</param>
/// <param name="thingId">The ID of the thing, leave out or set to zero to generate an ID</param>
/// <param name="invokeEvent">Invoke a OnNewThing event when the thing has been created</param>
public TouchSensor(Participant owner, byte thingId = 0, bool invokeEvent = true) : base(owner, Type.TouchSensor, thingId, invokeEvent) { }
*/
/// <summary> /// <summary>
/// Create a new child touch sensor /// Create a new child touch sensor
/// </summary> /// </summary>
@ -45,14 +31,14 @@ namespace RoboidControl {
} }
private bool touchUpdated = false; private bool touchUpdated = false;
#if UNITY_5_3_OR_NEWER // #if UNITY_5_3_OR_NEWER
/// @copydoc Passer::RoboidControl::Thing::CreateComponent // /// @copydoc Passer::RoboidControl::Thing::CreateComponent
public override void CreateComponent() { // public override void CreateComponent() {
// System.Console.Write("Create touch sensor component"); // // System.Console.Write("Create touch sensor component");
this.component = Unity.TouchSensor.Create(this); // this.component = Unity.TouchSensor.Create(this);
this.component.core = this; // this.component.core = this;
} // }
#endif // #endif
/// <summary> /// <summary>
/// Function used to generate binary data for this touch sensor /// Function used to generate binary data for this touch sensor
/// </summary> /// </summary>