Implemented updateQueues

This commit is contained in:
Pascal Serrarens 2025-05-01 17:39:50 +02:00
parent 19a6e22b16
commit 71adf127d9
5 changed files with 48 additions and 85 deletions

View File

@ -27,16 +27,12 @@ namespace RoboidControl.Unity {
private void HandleThingEvent(RoboidControl.Participant.UpdateEvent e) {
switch (e.thing) {
case RoboidControl.TouchSensor coreTouchSensor:
GameObject touchObj = new("Touch Sensor");
touchObj.transform.SetParent(this.transform);
TouchSensor touchSensor = touchObj.AddComponent<TouchSensor>();
touchSensor.coreSensor = coreTouchSensor;
TouchSensor touchSensor = TouchSensor.Create(coreTouchSensor);
coreTouchSensor.component = touchSensor;
break;
case RoboidControl.Thing coreThing:
GameObject thingObj = new("Thingg");
thingObj.transform.SetParent(this.transform);
Thing thing = thingObj.AddComponent<Thing>();
thing.core = coreThing;
Thing thing = Thing.Create(coreThing);
coreThing.component = thing;
break;
}
}

View File

@ -33,7 +33,6 @@ namespace RoboidControl.Unity {
return;
}
siteServer.site.Add(thing);
core.OnPoseChanged += PoseChanged;
}
public static Thing Create(RoboidControl.Thing core) {
@ -56,8 +55,6 @@ namespace RoboidControl.Unity {
this.transform.localPosition = core.position.ToVector3();
if (core.orientation != null)
this.transform.localRotation = core.orientation.ToQuaternion();
core.OnPoseChanged += this.PoseChanged;
}
/// <summary>
@ -71,7 +68,7 @@ namespace RoboidControl.Unity {
}
if (core.updateQueue.TryDequeue(out RoboidControl.Thing.UpdateEvent e))
HandleUpdateEvent(e);
HandleUpdateEvent(e);
if (core.linearVelocity != null && core.linearVelocity.distance != 0) {
Vector3 direction = Quaternion.AngleAxis(core.linearVelocity.direction.horizontal, Vector3.up) * Vector3.forward;
@ -82,37 +79,31 @@ namespace RoboidControl.Unity {
Vector3 axis = core.angularVelocity.direction.ToVector3();
this.transform.localRotation *= Quaternion.AngleAxis(core.angularVelocity.distance * Time.deltaTime, axis);
}
if (!string.IsNullOrEmpty(core.modelUrl) && this.modelUrl == null) {
string extension = core.modelUrl.Substring(core.modelUrl.LastIndexOf("."));
if (extension == ".jpg" || extension == ".png") {
StartCoroutine(LoadJPG());
}
this.modelUrl = core.modelUrl;
}
if (core.nameChanged) {
if (this.gameObject.name != core.name)
this.gameObject.name = core.name;
core.nameChanged = false;
}
if (core.hierarchyChanged) {
// Debug.Log("Parent changed");
if (core.parent == null)
this.transform.SetParent(null, true);
else
this.transform.SetParent(core.parent.component.transform, true);
core.hierarchyChanged = false;
}
}
private void HandleUpdateEvent(RoboidControl.Thing.UpdateEvent e) {
switch(e.messageId) {
case ThingMsg.id:
switch (e.messageId) {
case ThingMsg.id:
if (core.parent == null)
this.transform.SetParent(null, true);
else if (core.parent.component != null)
this.transform.SetParent(core.parent.component.transform, true);
break;
case NameMsg.Id:
this.gameObject.name = core.name;
break;
case ModelUrlMsg.Id:
string extension = core.modelUrl[core.modelUrl.LastIndexOf(".")..];
if (extension == ".jpg" || extension == ".png")
StartCoroutine(LoadJPG());
this.modelUrl = core.modelUrl;
break;
case PoseMsg.Id:
this.transform.localPosition = core.position.ToVector3();
this.transform.localRotation = core.orientation.ToQuaternion();
if (core.linearVelocity.distance == 0)
this.transform.localPosition = core.position.ToVector3();
if (core.angularVelocity.distance == 0)
this.transform.localRotation = core.orientation.ToQuaternion();
break;
}
}

View File

@ -17,7 +17,7 @@ namespace RoboidControl.Unity {
set => base.core = value;
}
SphereCollider collider = null;
SphereCollider touchCollider = null;
/// <summary>
/// Start the Unity represention
@ -27,7 +27,7 @@ namespace RoboidControl.Unity {
participant = FindAnyObjectByType<SiteServer>();
SetCoreThing(new RoboidControl.TouchSensor(participant.site));
}
collider = GetComponent<SphereCollider>();
touchCollider = GetComponent<SphereCollider>();
}
/// <summary>
@ -59,10 +59,10 @@ namespace RoboidControl.Unity {
protected override void Update() {
base.Update();
if (collider.radius == 0.01f &&
if (touchCollider.radius == 0.01f &&
this.transform.parent != null && this.transform.localPosition.magnitude > 0
) {
collider.radius = Vector3.Distance(this.transform.position, this.transform.parent.position) / 2;
touchCollider.radius = Vector3.Distance(this.transform.position, this.transform.parent.position) / 2;
this.transform.position = (this.transform.position + this.transform.parent.position) / 2;
}
}

View File

@ -111,7 +111,7 @@ namespace RoboidControl {
protected override void Process(Participant sender, NetworkIdMsg msg) { }
protected override void Process(Participant sender, ThingMsg msg) {
Console.WriteLine($"SiteServer: Process thing [{msg.networkId}/{msg.thingId}] {msg.thingType} {msg.parentId} ");
Console.WriteLine($"{this.name}: Process thing [{msg.networkId}/{msg.thingId}] {msg.thingType} {msg.parentId} ");
Thing thing = sender.Get(msg.thingId);
if (thing == null) {

View File

@ -58,7 +58,6 @@ namespace RoboidControl {
if (this.owner != null)
this.owner.Add(this);
if (invokeEvent) {
//InvokeNewThing(this);
Participant.UpdateEvent e = new() {
messageId = ThingMsg.id,
thing = this
@ -138,20 +137,25 @@ namespace RoboidControl {
if (_name != value) {
_name = value;
nameChanged = true;
OnNameChanged?.Invoke();
this.updateQueue.Enqueue(new UpdateEvent(NameMsg.Id));
}
}
}
/// <summary>
/// Event which is triggered when the name changes
/// </summary>
public event ChangeHandler OnNameChanged = delegate { };
public bool nameChanged = false;
private string _modelUrl = "";
/// <summary>
/// An URL pointing to the location where a model of the thing can be found
/// </summary>
public string modelUrl = "";
public string modelUrl {
get => _modelUrl;
set {
if (_modelUrl != value) {
_modelUrl = value;
this.updateQueue.Enqueue(new UpdateEvent(ModelUrlMsg.Id));
}
}
}
#if UNITY_5_3_OR_NEWER
/// <summary>
@ -183,6 +187,7 @@ namespace RoboidControl {
value.AddChild(this);
}
this.hierarchyChanged = true;
this.updateQueue.Enqueue(new UpdateEvent(ThingMsg.id));
}
}
@ -277,17 +282,11 @@ namespace RoboidControl {
if (_position != value) {
_position = value;
positionUpdated = true;
//OnPositionChanged?.Invoke();
UpdateEvent e = new() { messageId = PoseMsg.Id };
updateQueue.Enqueue(e);
updateQueue.Enqueue(new UpdateEvent(PoseMsg.Id));
}
}
}
/// <summary>
/// Event triggered when the pose has changed
/// </summary>
public event ChangeHandler OnPoseChanged = delegate { };
/// <summary>
/// Boolean indicating that the thing has an updated position
/// </summary>
public bool positionUpdated = false;
@ -321,15 +320,11 @@ namespace RoboidControl {
if (_linearVelocity != value) {
_linearVelocity = value;
linearVelocityUpdated = true;
OnLinearVelocityChanged?.Invoke(_linearVelocity);
updateQueue.Enqueue(new UpdateEvent(PoseMsg.Id));
}
}
}
/// <summary>
/// Event triggered when the linear velocity has changed
/// </summary>
public event SphericalHandler OnLinearVelocityChanged = delegate { };
/// <summary>
/// Boolean indicating the thing has an updated linear velocity
/// </summary>
public bool linearVelocityUpdated = false;
@ -344,15 +339,11 @@ namespace RoboidControl {
if (_angularVelocity != value) {
_angularVelocity = value;
angularVelocityUpdated = true;
OnAngularVelocityChanged?.Invoke(_angularVelocity);
updateQueue.Enqueue(new UpdateEvent(PoseMsg.Id));
}
}
}
/// <summary>
/// Event triggered when the angular velocity has changed
/// </summary>
public event SphericalHandler OnAngularVelocityChanged = delegate { };
/// <summary>
/// Boolean indicating the thing has an updated angular velocity
/// </summary>
public bool angularVelocityUpdated = false;
@ -387,8 +378,6 @@ namespace RoboidControl {
/// <param name="currentTime">he current clock time in milliseconds; if this is zero, the current time is retrieved automatically</param>
/// <param name="recurse">When true, this will Update the descendants recursively</param>
public virtual void Update(ulong currentTimeMs, bool recurse = false) {
if (this.positionUpdated || this.orientationUpdated)
OnPoseChanged?.Invoke();
this.positionUpdated = false;
this.orientationUpdated = false;
this.linearVelocityUpdated = false;
@ -406,23 +395,10 @@ namespace RoboidControl {
}
}
public delegate void ChangeHandler();
public delegate void SphericalHandler(Spherical v);
public delegate void ThingHandler(Thing t);
/// <summary>
/// Event triggered when a new thing has been created
/// </summary>
public static event ThingHandler OnNewThing = delegate { };
/// <summary>
/// Trigger the creation for the given thing
/// </summary>
/// <param name="thing">The created thing</param>
public static void InvokeNewThing(Thing thing) {
OnNewThing?.Invoke(thing);
}
public class UpdateEvent {
public UpdateEvent(int messageId) {
this.messageId = messageId;
}
public int messageId; // see the communication messages
};
public ConcurrentQueue<UpdateEvent> updateQueue = new();