From 5c24d7ca71f82d474851c3b89e1c328e19f1f8ea Mon Sep 17 00:00:00 2001
From: Jerald-Golden-B <jerald@hexrfactory.com>
Date: Wed, 16 Apr 2025 10:47:12 +0530
Subject: [PATCH] feat: Improve trigger processing and static machine status
 updates in IKAnimationController

---
 .../armbot/IKAnimationController.tsx          | 57 +++++++++----------
 1 file changed, 28 insertions(+), 29 deletions(-)

diff --git a/app/src/modules/simulation/armbot/IKAnimationController.tsx b/app/src/modules/simulation/armbot/IKAnimationController.tsx
index 1044540..9f19797 100644
--- a/app/src/modules/simulation/armbot/IKAnimationController.tsx
+++ b/app/src/modules/simulation/armbot/IKAnimationController.tsx
@@ -114,10 +114,38 @@ const IKAnimationController = ({
 
         if (prev.needsInitialMovement !== needsInitialMovement && !needsInitialMovement) {
             logStatus(`[Arm ${uuid}] Reached rest position, ready for animation`);
+
         }
 
         if (prev.selectedTrigger !== selectedTrigger) {
             logStatus(`[Arm ${uuid}] Processing new trigger: ${selectedTrigger}`);
+
+
+            const currentProcess = process.find(p => p.triggerId === prev.selectedTrigger);
+            if (currentProcess) {
+                const triggerId = currentProcess.triggerId;
+
+                const endPoint = armBot.actions.processes.find((process) => process.triggerId === triggerId)?.endPoint;
+
+                // Search simulationStates for a StaticMachine that has a point matching this endPointId
+                const matchedStaticMachine = simulationStates.find(
+                    (state) =>
+                        state.type === "StaticMachine" &&
+                        state.points?.uuid === endPoint// check for static machine with matching point uuid
+                ) as any;
+
+                if (matchedStaticMachine) {
+                    setStaticMachines((machines) => {
+                        return machines.map((machine) => {
+                            if (machine.uuid === matchedStaticMachine.modeluuid) {
+                                return { ...machine, status: "running" };
+                            } else {
+                                return machine;
+                            }
+                        });
+                    });
+                }
+            }
         }
 
         // Update previous state
@@ -249,35 +277,6 @@ const IKAnimationController = ({
         } else if (progress >= startToEndRange[0] && progress < startToEndRange[1]) {
             currentSpeed = speed;
             currentStatus = "moving"; // Moving between points
-            if (1 - progress < 0.05) {
-                // Find the process that matches the current trigger
-                const currentProcess = process.find(p => p.triggerId === selectedTrigger);
-                if (currentProcess) {
-                    const triggerId = currentProcess.triggerId;
-
-                    const endPoint = armBot.actions.processes.find((process) => process.triggerId === triggerId)?.endPoint;
-
-                    // Search simulationStates for a StaticMachine that has a point matching this endPointId
-                    const matchedStaticMachine = simulationStates.find(
-                        (state) =>
-                            state.type === "StaticMachine" &&
-                            state.points?.uuid === endPoint// check for static machine with matching point uuid
-                    ) as any;
-
-                    if (matchedStaticMachine) {
-                        setStaticMachines((machines) => {
-                            return machines.map((machine) => {
-                                if (machine.uuid === matchedStaticMachine.modeluuid) {
-                                    return { ...machine, status: "running" };
-                                } else {
-                                    return machine;
-                                }
-                            });
-                        });
-                    }
-                }
-
-            }
         } else if (progress >= endToRestRange[0] && progress < 1) {
             currentSpeed = restSpeed;
             currentStatus = "moving"; // Returning to rest