diff --git a/Unity/DistanceSensor.cs b/Unity/DistanceSensor.cs
index 4b81f9b..7774919 100644
--- a/Unity/DistanceSensor.cs
+++ b/Unity/DistanceSensor.cs
@@ -14,6 +14,14 @@ namespace RoboidControl.Unity {
///
public RoboidControl.DistanceSensor coreSensor => base.core as RoboidControl.DistanceSensor;
+ public float distance {
+ get {
+ if (coreSensor == null)
+ return float.PositiveInfinity;
+ return coreSensor.distance;
+ }
+ }
+
///
/// Start the Unity representation
///
@@ -56,10 +64,10 @@ namespace RoboidControl.Unity {
Thing thing = hitInfo.transform.GetComponentInParent();
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);
}
diff --git a/Unity/Editor/DistanceSensor_Editor.cs b/Unity/Editor/DistanceSensor_Editor.cs
new file mode 100644
index 0000000..33d6e2a
--- /dev/null
+++ b/Unity/Editor/DistanceSensor_Editor.cs
@@ -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());
+ }
+ }
+}
diff --git a/Unity/Participant.cs b/Unity/Participant.cs
index cdd163d..f48219e 100644
--- a/Unity/Participant.cs
+++ b/Unity/Participant.cs
@@ -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) {
diff --git a/src/Participants/ParticipantUDP.cs b/src/Participants/ParticipantUDP.cs
index 091ac30..a9f6a4a 100644
--- a/src/Participants/ParticipantUDP.cs
+++ b/src/Participants/ParticipantUDP.cs
@@ -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)
diff --git a/src/Thing.cs b/src/Thing.cs
index 0d5fd4d..78fefd5 100644
--- a/src/Thing.cs
+++ b/src/Thing.cs
@@ -109,12 +109,12 @@ namespace RoboidControl {
/// Function which can be used to create components in external engines.
///
/// 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
diff --git a/src/Things/DistanceSensor.cs b/src/Things/DistanceSensor.cs
index 04a479d..c723527 100644
--- a/src/Things/DistanceSensor.cs
+++ b/src/Things/DistanceSensor.cs
@@ -1,48 +1,72 @@
+using System;
+
namespace RoboidControl {
///
- /// A sensor measuring the distance in the forward direction
+ /// A sensor measuring distance
///
public class DistanceSensor : Thing {
- ///
- /// The current measured distance
- ///
- public float distance = 0;
- /*
///
- /// Constructor for a new distance sensor
+ /// Create a new distance sensor
///
- /// The participant for which the sensor is needed
- public DistanceSensor(Participant participant) : base(participant, Type.Undetermined) { }
-
- ///
- /// Create a distance sensor with the given ID
- ///
- /// The participant for with the sensor is needed
- /// The network ID of the sensor
- /// The ID of the thing
- public DistanceSensor(Participant owner, byte thingId) : base(owner, Type.TemperatureSensor, thingId) {}
- */
+ /// The parent thing
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;
+ ///
+ /// The current distance
+ ///
+ 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[] bytes = new byte[2];
+ byte ix = 0;
+ LowLevelMessages.SendFloat16(bytes, ref ix, this.internalDistance);
+ distanceUpdated = false;
+ return bytes;
+ }
+
///
/// Function to extract the distance received in the binary message
///
/// The byte array
public override void ProcessBinary(byte[] bytes) {
byte ix = 0;
- this.distance = LowLevelMessages.ReceiveFloat16(bytes, ref ix);
+ this.externalDistance = LowLevelMessages.ReceiveFloat16(bytes, ref ix);
}
}
diff --git a/src/Things/TouchSensor.cs b/src/Things/TouchSensor.cs
index cdeb0dc..bdb23ab 100644
--- a/src/Things/TouchSensor.cs
+++ b/src/Things/TouchSensor.cs
@@ -6,20 +6,6 @@ namespace RoboidControl {
/// A sensor which can detect touches
///
public class TouchSensor : Thing {
- /*
- ///
- /// Create a touch sensor without communication abilities
- ///
- /// Invoke a OnNewThing event when the thing has been created
- public TouchSensor(bool invokeEvent = true) : base(Type.TouchSensor, invokeEvent) { }
- ///
- /// Create a touch sensor for a participant
- ///
- /// The owning participant
- /// The ID of the thing, leave out or set to zero to generate an ID
- /// Invoke a OnNewThing event when the thing has been created
- public TouchSensor(Participant owner, byte thingId = 0, bool invokeEvent = true) : base(owner, Type.TouchSensor, thingId, invokeEvent) { }
- */
///
/// Create a new child touch sensor
///
@@ -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
///
/// Function used to generate binary data for this touch sensor
///