Add control library for distance sensor and display

This commit is contained in:
Johnathon Slightham
2026-03-04 07:55:35 -05:00
parent aad4b7654d
commit 6f63629119
4 changed files with 49 additions and 12 deletions

View File

@@ -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;