diff --git a/Unity/Thing.cs b/Unity/Thing.cs index 575b72a..0e78e9c 100644 --- a/Unity/Thing.cs +++ b/Unity/Thing.cs @@ -55,7 +55,7 @@ namespace RoboidControl.Unity { this.transform.SetParent(core.parent.component.transform, false); this.transform.localPosition = Vector3.zero; } - else { + else if (core.owner.component != null) { this.transform.SetParent(core.owner.component.transform, false); } diff --git a/Unity/Wheel.cs b/Unity/Wheel.cs index 41d427f..ba0dd24 100644 --- a/Unity/Wheel.cs +++ b/Unity/Wheel.cs @@ -37,8 +37,11 @@ namespace RoboidControl.Unity { // Use resource prefab when available GameObject gameObj = Instantiate(prefab); Wheel component = gameObj.GetComponent(); - if (component != null) - component.core = new RoboidControl.Thing(RoboidControl.Thing.Type.UncontrolledMotor); + if (component != null) { + component.core = new RoboidControl.Thing { + type = RoboidControl.Thing.Type.UncontrolledMotor + }; + } return component; } else { @@ -48,9 +51,14 @@ namespace RoboidControl.Unity { Wheel component = gameObj.AddComponent(); SiteServer participant = FindAnyObjectByType(); RoboidControl.Thing core = participant.coreParticipant.Get(thingId); - if (core == null) + if (core == null) { //core = new(participant.coreParticipant, RoboidControl.Thing.Type.UncontrolledMotor, thingId, false); - core = RoboidControl.Thing.CreateRemote(participant.coreParticipant, RoboidControl.Thing.Type.UncontrolledMotor, thingId); + //core = RoboidControl.Thing.CreateRemote(participant.coreParticipant, thingId); + core = new RoboidControl.Thing(participant.coreParticipant.root) { + id = thingId, + type = RoboidControl.Thing.Type.UncontrolledMotor + }; + } else { ; } diff --git a/src/Participants/SiteServer.cs b/src/Participants/SiteServer.cs index 519f425..682d3eb 100644 --- a/src/Participants/SiteServer.cs +++ b/src/Participants/SiteServer.cs @@ -131,12 +131,14 @@ namespace RoboidControl { } protected virtual Thing ProcessNewThing(Participant sender, ThingMsg msg) { - return msg.thingType switch { - //Thing.Type.TouchSensor => new TouchSensor(sender, msg.thingId), - //Thing.Type.DifferentialDrive => new DifferentialDrive(sender, msg.thingId), - _ => Thing.CreateRemote(sender, msg.thingType, msg.thingId) + Thing newThing = msg.thingType switch { + Thing.Type.TouchSensor => new TouchSensor(sender.root), + Thing.Type.DifferentialDrive => new DifferentialDrive(sender.root), + _ => new Thing(sender.root) }; - + newThing.id = msg.thingId; + newThing.type = msg.thingType; + return newThing; } #endregion Receive diff --git a/src/Thing.cs b/src/Thing.cs index e642388..0aea683 100644 --- a/src/Thing.cs +++ b/src/Thing.cs @@ -105,8 +105,8 @@ namespace RoboidControl { this.parent = parent; } */ - public Thing(byte thingType = Type.Undetermined, Thing parent = default) { - this.type = thingType; + public Thing(Thing parent = default) { + this.type = Type.Undetermined; this.positionUpdated = true; this.orientationUpdated = true; @@ -127,12 +127,12 @@ namespace RoboidControl { this.owner.updateQueue.Enqueue(e); } - public static Thing CreateRemote(Participant owner, byte thingType, byte thingId) { - Thing remoteThing = new(thingType, owner.root) { - id = thingId - }; - return remoteThing; - } + // public static Thing CreateRemote(Participant owner, byte thingId) { + // Thing remoteThing = new(owner.root) { + // id = thingId + // }; + // return remoteThing; + // } /// /// Function which can be used to create components in external engines. diff --git a/src/Things/DifferentialDrive.cs b/src/Things/DifferentialDrive.cs index 039d26c..aa3b4f4 100644 --- a/src/Things/DifferentialDrive.cs +++ b/src/Things/DifferentialDrive.cs @@ -40,7 +40,9 @@ namespace RoboidControl { /// The parent thing /// 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 DifferentialDrive(Thing parent) : base(Type.DifferentialDrive, parent) { } + public DifferentialDrive(Thing parent) : base(parent) { + this.type = Type.DifferentialDrive; + } /// @brief Configures the dimensions of the drive /// @param wheelDiameter The diameter of the wheels in meters diff --git a/src/Things/DigitalSensor.cs b/src/Things/DigitalSensor.cs index 3d1a19c..9eb074d 100644 --- a/src/Things/DigitalSensor.cs +++ b/src/Things/DigitalSensor.cs @@ -26,7 +26,9 @@ namespace RoboidControl { /// The parent thing /// 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 DigitalSensor(Thing parent) : base(Type.Switch, parent) { } + public DigitalSensor(Thing parent) : base(parent) { + this.type = Type.Switch; + } /// /// Value which is true when the sensor is touching something, false otherwise diff --git a/src/Things/DistanceSensor.cs b/src/Things/DistanceSensor.cs index def58f9..04a479d 100644 --- a/src/Things/DistanceSensor.cs +++ b/src/Things/DistanceSensor.cs @@ -24,7 +24,9 @@ namespace RoboidControl { /// The ID of the thing public DistanceSensor(Participant owner, byte thingId) : base(owner, Type.TemperatureSensor, thingId) {} */ - public DistanceSensor(Thing parent): base(Type.DistanceSensor, parent) {} + public DistanceSensor(Thing parent) : base(parent) { + this.type = Type.DistanceSensor; + } #if UNITY_5_3_OR_NEWER diff --git a/src/Things/Motor.cs b/src/Things/Motor.cs index e4ecad8..9a9ac7b 100644 --- a/src/Things/Motor.cs +++ b/src/Things/Motor.cs @@ -4,7 +4,9 @@ namespace RoboidControl { public class Motor : Thing { //public Motor(bool invokeEvent = true) : base(Type.UncontrolledMotor, invokeEvent) { } - public Motor(Thing parent) : base(Type.UncontrolledMotor, parent) { } + public Motor(Thing parent) : base(parent) { + this.type = Type.UncontrolledMotor; + } /// @brief Motor turning direction public enum Direction { diff --git a/src/Things/RelativeEncoder.cs b/src/Things/RelativeEncoder.cs index aee9d7c..34901ca 100644 --- a/src/Things/RelativeEncoder.cs +++ b/src/Things/RelativeEncoder.cs @@ -12,8 +12,8 @@ namespace RoboidControl { /* public RelativeEncoder(bool invokeEvent = true) : base(Type.IncrementalEncoder, invokeEvent) { } */ - public RelativeEncoder(Thing parent = default) : base(Type.RelativeEncoder, parent) { - + public RelativeEncoder(Thing parent = default) : base(parent) { + this.type = Type.RelativeEncoder; } protected float _rotationSpeed = 0; diff --git a/src/Things/TemperatureSensor.cs b/src/Things/TemperatureSensor.cs index ecf548e..84dc5e2 100644 --- a/src/Things/TemperatureSensor.cs +++ b/src/Things/TemperatureSensor.cs @@ -26,7 +26,9 @@ namespace RoboidControl { /// The parent thing /// 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 TemperatureSensor(Thing parent) : base(Type.TemperatureSensor, parent) { } + public TemperatureSensor(Thing parent) : base(parent) { + this.type = Type.TemperatureSensor; + } /// /// The measured temperature diff --git a/src/Things/TouchSensor.cs b/src/Things/TouchSensor.cs index e2db699..4c1670e 100644 --- a/src/Things/TouchSensor.cs +++ b/src/Things/TouchSensor.cs @@ -24,11 +24,10 @@ namespace RoboidControl { /// Create a new child touch sensor /// /// The parent thing - /// 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(Thing parent) : base(Type.TouchSensor, parent) { + public TouchSensor(Thing parent) : base(parent) { + this.type = Type.TouchSensor; this.name = "TouchSensor"; - } + } /// /// Value which is true when the sensor is touching something, false otherwise