First step to ControlCore

This commit is contained in:
Pascal Serrarens 2025-02-11 16:02:28 +01:00
parent 0307a62ed8
commit 85a19faeb4
6 changed files with 133 additions and 2 deletions

20
Sensors/DistanceSensor.cs Normal file
View File

@ -0,0 +1,20 @@
using System;
namespace Passer.Control.Core {
public class DistanceSensor : Thing {
public float distance = 0;
public DistanceSensor() : base() { }
public DistanceSensor(byte networkId, byte thingId) : base(null, networkId, thingId, (byte)Type.TemperatureSensor) {
}
public override void ProcessBinary(byte[] bytes) {
byte ix = 0;
this.distance = LowLevelMessages.ReceiveFloat16(bytes, ref ix);
}
}
}

View File

@ -13,7 +13,7 @@ namespace Passer.Control.Core {
this.ipAddress = "0.0.0.0";
this.port = port;
this.endPoint = new IPEndPoint(IPAddress.Parse(ipAddress), port); // for sending
//this.endPoint = new IPEndPoint(IPAddress.Parse(ipAddress), port); // for sending
Console.Write($"Prepare receive on port {port}");
this.udpClient = new UdpClient(port); // for receiving
@ -22,6 +22,10 @@ namespace Passer.Control.Core {
new Tuple<UdpClient, IPEndPoint>(this.udpClient, new(IPAddress.Any, port)));
}
public void Close() {
this.udpClient.Close();
}
public override void Publish() {
}

25
Unity/DebugConsole.cs Normal file
View File

@ -0,0 +1,25 @@
#if UNITY_5_3_OR_NEWER
using System.IO;
using System.Text;
using UnityEngine;
namespace Passer.Control.Unity {
public class UnityLogWriter : TextWriter {
public override void Write(char value) {
Debug.Log(value);
}
public override void Write(string value) {
Debug.Log(value);
}
public override void WriteLine(string value) {
Debug.Log(value);
}
public override Encoding Encoding => Encoding.UTF8;
}
}
#endif

43
Unity/SiteServer.cs Normal file
View File

@ -0,0 +1,43 @@
#if UNITY_5_3_OR_NEWER
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Passer.Control.Unity {
public class SiteServer : MonoBehaviour {
public Core.SiteServer site;
public Queue<Core.Thing> thingQueue = new();
protected virtual void Awake() {
Console.SetOut(new UnityLogWriter());
site = new(7681);
Core.Thing.OnNewThing += HandleNewThing;
}
void OnApplicationQuit() {
site.Close();
}
public void HandleNewThing(Core.Thing thing) {
// if (thing.participant == null) {
// Console.WriteLine($"new thing without participant [{thing.networkId}/{thing.id}] {thing.name}");
// this.site.Add(thing, false);
// }
thingQueue.Enqueue(thing);
}
protected virtual void Update() {
site.Update((ulong)(Time.time * 1000));
if (thingQueue.TryDequeue(out Core.Thing thing)) {
thing.CreateComponent();
}
}
}
}
#endif

37
Unity/Thing.cs Normal file
View File

@ -0,0 +1,37 @@
#if UNITY_5_3_OR_NEWER
using UnityEngine;
namespace Passer.Control.Unity {
public class Thing : MonoBehaviour {
protected Core.Thing core;
protected void CreateThing(Core.Thing thing) {
SiteServer siteServer = FindAnyObjectByType<SiteServer>();
if (siteServer == null) {
Debug.LogWarning("No site server found");
return;
}
core = thing;
siteServer.site.Add(thing);
}
protected virtual void Update() {
if (core == null)
return;
if (core.linearVelocity != null) {
Vector3 direction = Quaternion.AngleAxis(core.linearVelocity.direction.horizontal, Vector3.up) * Vector3.forward;
this.transform.Translate(direction * core.linearVelocity.distance * Time.deltaTime);
}
if (core.angularVelocity != null) {
Vector3 axis = Quaternion.AngleAxis(core.angularVelocity.direction.horizontal, Vector3.up) * Vector3.forward;
this.transform.Rotate(axis, core.angularVelocity.distance * Time.deltaTime);
}
}
}
}
#endif

View File

@ -1,4 +1,5 @@
using System;
#if !UNITY_5_3_OR_NEWER
using System;
using System.Threading;
using NUnit.Framework;
@ -83,3 +84,4 @@ namespace ControlCore.test {
}
}
}
#endif