mirror of
https://github.com/BotChain-Robots/ui.git
synced 2026-07-08 15:07:22 +02:00
Merge branch 'js/distance-sensor' into 'main'
Add control library for distance sensor and display See merge request capstone-group2/ui!13
This commit is contained in:
@@ -19,6 +19,12 @@ public class ControlLibrary : MonoBehaviour
|
||||
[DllImport("libc_control")]
|
||||
private static extern IntPtr get_configuration(out int module_id); // the data this points to will be invalidated when called again
|
||||
|
||||
[DllImport("libc_control")]
|
||||
public static extern int send_string_control(int module_id, string s);
|
||||
|
||||
[DllImport("libc_control")]
|
||||
public static extern double get_distance_control(int module_id);
|
||||
|
||||
[DllImport("libc_control")]
|
||||
public static extern bool control_sentry_init(
|
||||
string dsn,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using UnityEngine;
|
||||
using System;
|
||||
|
||||
public class DisplayModule : ModuleBase
|
||||
{
|
||||
@@ -17,5 +18,6 @@ public class DisplayModule : ModuleBase
|
||||
// Replace with actual hardware communication logic
|
||||
Debug.Log($"[DisplayModule] Sending display text: {text}");
|
||||
// Example: ControlLibrary.send_display_text(Int32.Parse(moduleID), text);
|
||||
ControlLibrary.send_string_control(Int32.Parse(moduleID), text);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +1,48 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public class DistanceSensorModule : ModuleBase
|
||||
{
|
||||
public bool objectDetected;
|
||||
public float distanceMeters;
|
||||
public string[] infoLines = new string[0];
|
||||
// Keep a fixed-size array (no GC alloc every frame)
|
||||
public string[] infoLines = new string[2];
|
||||
|
||||
public override string moduleType => "Distance";
|
||||
public override string moduleName => "Distance Sensor Module";
|
||||
|
||||
// Optional: slow down polling to avoid spamming hardware
|
||||
[SerializeField] float pollHz = 10f;
|
||||
float nextPollTime;
|
||||
|
||||
void Update()
|
||||
{
|
||||
// Replace with your ControlLibrary call pattern
|
||||
// var reading = ControlLibrary.get_distance(...)
|
||||
// Always show something immediately
|
||||
infoLines[0] = $"ModuleID raw: '{moduleID}'";
|
||||
|
||||
if (!objectDetected)
|
||||
infoLines = new[] {"Object: Not detected" };
|
||||
else
|
||||
infoLines = new[] { "Object: Detected", $"Distance: {distanceMeters:F2} m" };
|
||||
// Poll at a fixed rate (avoids frame spam)
|
||||
if (Time.unscaledTime < nextPollTime) return;
|
||||
nextPollTime = Time.unscaledTime + (1f / Mathf.Max(1f, pollHz));
|
||||
|
||||
if (!int.TryParse(moduleID, out var id))
|
||||
{
|
||||
infoLines[1] = "ERROR: moduleID is not an int";
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// Control library call
|
||||
double distance = ControlLibrary.get_distance_control(id);
|
||||
|
||||
infoLines[1] = $"Distance: {distance:F0} mm";
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
// SHOW THE PROBLEM ON SCREEN (works in release builds)
|
||||
infoLines[1] = $"EXCEPTION: {e.GetType().Name}: {e.Message}";
|
||||
|
||||
// Also log to Player.log (works in builds)
|
||||
Debug.LogException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public string[] GetInfoLines() => infoLines;
|
||||
|
||||
@@ -77,9 +77,10 @@ MonoBehaviour:
|
||||
m_Script: {fileID: 11500000, guid: e4735e9a5ae30490bba1dc35e4603bce, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier: '::'
|
||||
objectDetected: 0
|
||||
distanceMeters: 0
|
||||
infoLines: []
|
||||
infoLines:
|
||||
-
|
||||
-
|
||||
pollHz: 10
|
||||
--- !u!1 &5041994602013865196
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
|
||||
Reference in New Issue
Block a user