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>
public RoboidControl.DistanceSensor coreSensor => base.core as RoboidControl.DistanceSensor;
public float distance {
get {
if (coreSensor == null)
return float.PositiveInfinity;
return coreSensor.distance;
}
}
/// <summary>
/// Start the Unity representation
/// </summary>
@ -56,10 +64,10 @@ namespace RoboidControl.Unity {
Thing thing = hitInfo.transform.GetComponentInParent<Thing>();
if (thing == null) {
// Debug.Log($"collision {hitInfo.transform.name} {hitInfo.distance}");
coreSensor.distance = hitInfo.distance;
coreSensor.internalDistance = hitInfo.distance;
}
else
coreSensor.distance = 0;
coreSensor.internalDistance = float.PositiveInfinity;
}
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) {
switch (e.thing) {
case RoboidControl.DistanceSensor coreDistanceSensor:
coreDistanceSensor.component = DistanceSensor.Create(coreDistanceSensor);
break;
case RoboidControl.TouchSensor coreTouchSensor:
TouchSensor touchSensor = TouchSensor.Create(coreTouchSensor);
coreTouchSensor.component = touchSensor;
coreTouchSensor.component = TouchSensor.Create(coreTouchSensor);
break;
case RoboidControl.DifferentialDrive coreDrive:
DifferentialDrive differentialDrive = DifferentialDrive.Create(coreDrive);
coreDrive.component = differentialDrive;
coreDrive.component = DifferentialDrive.Create(coreDrive);
break;
case RoboidControl.Motor coreMotor:
if (coreMotor.component == null) {

View File

@ -493,6 +493,7 @@ namespace RoboidControl {
protected virtual Thing ProcessNewThing(Participant owner, ThingMsg msg, bool isRemote) {
// Console.WriteLine("--- New Thing");
Thing newThing = msg.thingType switch {
Thing.Type.DistanceSensor => new DistanceSensor(owner.root),
Thing.Type.TouchSensor => new TouchSensor(owner.root),
Thing.Type.DifferentialDrive => new DifferentialDrive(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.
/// </summary>
/// Currently this is used to create GameObjects in Unity
public virtual void CreateComponent() {
#if UNITY_5_3_OR_NEWER
this.component = Unity.Thing.Create(this);
this.component.core = this;
#endif
}
// public virtual void CreateComponent() {
// #if UNITY_5_3_OR_NEWER
// this.component = Unity.Thing.Create(this);
// this.component.core = this;
// #endif
// }
#endregion Init

View File

@ -1,48 +1,72 @@
using System;
namespace RoboidControl {
/// <summary>
/// A sensor measuring the distance in the forward direction
/// A sensor measuring distance
/// </summary>
public class DistanceSensor : Thing {
/// <summary>
/// The current measured distance
/// </summary>
public float distance = 0;
/*
/// <summary>
/// Constructor for a new distance sensor
/// Create a new distance sensor
/// </summary>
/// <param name="participant">The participant for which the sensor is needed</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) {}
*/
/// <param name="parent">The parent thing</param>
public DistanceSensor(Thing parent) : base(parent) {
this.type = Type.DistanceSensor;
this.name = "Distance sensor";
}
#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;
public float _internalDistance = float.PositiveInfinity;
public float internalDistance {
get { return _internalDistance; }
set {
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>
/// Function to extract the distance received in the binary message
/// </summary>
/// <param name="bytes">The byte array</param>
public override void ProcessBinary(byte[] bytes) {
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
/// </summary>
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>
/// Create a new child touch sensor
/// </summary>
@ -45,14 +31,14 @@ namespace RoboidControl {
}
private bool touchUpdated = false;
#if UNITY_5_3_OR_NEWER
/// @copydoc Passer::RoboidControl::Thing::CreateComponent
public override void CreateComponent() {
// System.Console.Write("Create touch sensor component");
this.component = Unity.TouchSensor.Create(this);
this.component.core = this;
}
#endif
// #if UNITY_5_3_OR_NEWER
// /// @copydoc Passer::RoboidControl::Thing::CreateComponent
// public override void CreateComponent() {
// // System.Console.Write("Create touch sensor component");
// this.component = Unity.TouchSensor.Create(this);
// this.component.core = this;
// }
// #endif
/// <summary>
/// Function used to generate binary data for this touch sensor
/// </summary>