From ee319c28e440ccc8c10df9596e57eb9874d203f9 Mon Sep 17 00:00:00 2001 From: Jerald-Golden-B Date: Wed, 16 Apr 2025 16:33:48 +0530 Subject: [PATCH] refactor: Remove unnecessary console logs and improve connection limit checks in simulation components --- .../mechanics/ArmBotMechanics.tsx | 1 - .../simulation/armbot/ArmBotInstances.tsx | 1 - .../armbot/IKAnimationController.tsx | 63 ++++++- .../simulation/armbot/MaterialInstances.tsx | 21 +-- .../modules/simulation/path/pathConnector.tsx | 166 ++++++++++++++++-- .../simulation/process/processAnimator.tsx | 6 +- 6 files changed, 219 insertions(+), 39 deletions(-) diff --git a/app/src/components/layout/sidebarRight/mechanics/ArmBotMechanics.tsx b/app/src/components/layout/sidebarRight/mechanics/ArmBotMechanics.tsx index d3e5301..70e297b 100644 --- a/app/src/components/layout/sidebarRight/mechanics/ArmBotMechanics.tsx +++ b/app/src/components/layout/sidebarRight/mechanics/ArmBotMechanics.tsx @@ -150,7 +150,6 @@ const ArmBotMechanics: React.FC = () => { modeluuid: updatedPath.modeluuid, eventData: { type: "ArmBot", points: updatedPath.points } } - console.log('data: ', data); socket.emit('v2:model-asset:updateEventData', data); } diff --git a/app/src/modules/simulation/armbot/ArmBotInstances.tsx b/app/src/modules/simulation/armbot/ArmBotInstances.tsx index 8d20a87..a0657db 100644 --- a/app/src/modules/simulation/armbot/ArmBotInstances.tsx +++ b/app/src/modules/simulation/armbot/ArmBotInstances.tsx @@ -46,7 +46,6 @@ export const ArmbotInstances: React.FC = ({ index, armBot, const [processes, setProcesses] = useState([]); useEffect(() => { - if (armBot.actions.processes.length > 0) { const mappedProcesses = armBot.actions.processes.map((process) => { return { diff --git a/app/src/modules/simulation/armbot/IKAnimationController.tsx b/app/src/modules/simulation/armbot/IKAnimationController.tsx index efe88d4..6b77bac 100644 --- a/app/src/modules/simulation/armbot/IKAnimationController.tsx +++ b/app/src/modules/simulation/armbot/IKAnimationController.tsx @@ -1,7 +1,7 @@ import { useEffect, useMemo, useState, useRef } from "react"; import { useFrame } from "@react-three/fiber"; import * as THREE from "three"; -import { usePlayButtonStore } from "../../../store/usePlayButtonStore"; +import { usePlayButtonStore, useResetButtonStore } from "../../../store/usePlayButtonStore"; import { useSimulationStates } from "../../../store/store"; import MaterialInstances from "./MaterialInstances"; @@ -70,6 +70,7 @@ const IKAnimationController = ({ const { isPlaying } = usePlayButtonStore();; const statusRef = useRef("idle"); const { simulationStates } = useSimulationStates(); + const { isReset } = useResetButtonStore(); const initialCurveRef = useRef(null); const initialStartPositionRef = useRef(null); @@ -78,6 +79,13 @@ const IKAnimationController = ({ setProgress(0); }, [selectedTrigger]); + useEffect(() => { + setProgress(0); + setNeedsInitialMovement(true); + setInitialProgress(0); + setIsInitializing(true); + }, [isReset]); + useEffect(() => { if (ikSolver) { const targetBone = ikSolver.mesh.skeleton.bones.find( @@ -229,6 +237,9 @@ const IKAnimationController = ({ currentStatus = "processing"; const segmentProgress = (newProgress - restToStartEnd) / (processEnd - restToStartEnd); currentPosition = processCurve.getPoint(segmentProgress); + if (statusRef.current !== "processing") { + updateConveyorOrStaticMachineStatusOnStart(selectedTrigger); + } } else { // Returning to rest position currentStatus = "returning to rest"; @@ -245,7 +256,7 @@ const IKAnimationController = ({ // Only trigger when the entire animation is complete (newProgress === 1) if (newProgress === 1 && currentStatus === "returning to rest") { - updateConveyorOrStaticMachineStatus(selectedTrigger); + updateConveyorOrStaticMachineStatusOnEnd(selectedTrigger); } bone.position.copy(currentPosition); @@ -254,7 +265,45 @@ const IKAnimationController = ({ }); }); - const updateConveyorOrStaticMachineStatus = (selectedTrigger: string) => { + const updateConveyorOrStaticMachineStatusOnStart = (selectedTrigger: string) => { + const currentProcess = processes.find(p => p.triggerId === selectedTrigger); + if (currentProcess) { + const triggerId = currentProcess.triggerId; + + const startPoint = armBot.actions.processes.find((process) => process.triggerId === triggerId)?.startPoint; + + const matchedMachine = simulationStates.find((state) => { + if (state.type === "Conveyor") { + return (state).points.some( + (point) => point.uuid === startPoint + ); + } else if (state.type === "StaticMachine") { + return state.points.uuid === startPoint; + } + return false; + }); + + if (matchedMachine) { + if (matchedMachine.type === "Conveyor") { + logStatus(`[Arm ${uuid}] start point which is a conveyor (${matchedMachine.modelName})`); + } else { + logStatus(`[Arm ${uuid}] started form start point which is a static machine (${matchedMachine.modelName})`); + } + + setTimeout(() => { + if (matchedMachine.type === "StaticMachine") { + updateArmBotStatus('dropping'); + } + + if (matchedMachine.type === "Conveyor") { + updateArmBotStatus('picking'); + } + }, 0); + } + } + } + + const updateConveyorOrStaticMachineStatusOnEnd = (selectedTrigger: string) => { const currentProcess = processes.find(p => p.triggerId === selectedTrigger); if (currentProcess) { const triggerId = currentProcess.triggerId; @@ -314,7 +363,13 @@ const IKAnimationController = ({ } } - return null; + return ( + + ); }; export default IKAnimationController; \ No newline at end of file diff --git a/app/src/modules/simulation/armbot/MaterialInstances.tsx b/app/src/modules/simulation/armbot/MaterialInstances.tsx index 778ae30..c83cd51 100644 --- a/app/src/modules/simulation/armbot/MaterialInstances.tsx +++ b/app/src/modules/simulation/armbot/MaterialInstances.tsx @@ -3,37 +3,26 @@ import * as THREE from 'three'; import { Box } from '@react-three/drei'; type MaterialInstancesProps = { - groupRef: React.RefObject; - activeCurve: any; - progress: number; + statusRef: React.RefObject; ikSolver: any; targetBoneName: string; }; function MaterialInstances({ - groupRef, - activeCurve, - progress, + statusRef, ikSolver, targetBoneName }: MaterialInstancesProps) { - const { endToRestRange, startToEndRange } = activeCurve; + if (!ikSolver) return null; - // Show the box from when we reach the start point until we leave the end point - const shouldShow = (progress >= startToEndRange[0] && progress < startToEndRange[1] && progress >= endToRestRange[0]); - - if (!shouldShow || !ikSolver) return null; - - const targetBone = ikSolver.mesh.skeleton.bones.find( - (b: any) => b.name === targetBoneName - ); + const targetBone = ikSolver.mesh.skeleton.bones.find((b: any) => b.name === targetBoneName); if (!targetBone) return null; const worldPos = new THREE.Vector3(); targetBone.getWorldPosition(worldPos); return ( - + ); diff --git a/app/src/modules/simulation/path/pathConnector.tsx b/app/src/modules/simulation/path/pathConnector.tsx index 3e35925..3db399e 100644 --- a/app/src/modules/simulation/path/pathConnector.tsx +++ b/app/src/modules/simulation/path/pathConnector.tsx @@ -40,6 +40,37 @@ function PathConnector({ pathsGroupRef, }: { pathsGroupRef: React.MutableRefObje }; const existingTargets = point.connections.targets || []; + // Check connection limits + const toPath = simulationStates.find(p => p.modeluuid === toModelUUID); + if (toPath) { + // Check if we already have this type of connection + const hasConveyor = existingTargets.some(t => { + const targetPath = simulationStates.find(p => p.modeluuid === t.modelUUID); + return targetPath?.type === "Conveyor"; + }); + const hasArmBot = existingTargets.some(t => { + const targetPath = simulationStates.find(p => p.modeluuid === t.modelUUID); + return targetPath?.type === "ArmBot"; + }); + const hasVehicle = existingTargets.some(t => { + const targetPath = simulationStates.find(p => p.modeluuid === t.modelUUID); + return targetPath?.type === "Vehicle"; + }); + + if (toPath.type === "Conveyor" && hasConveyor) { + console.log("Conveyor can only connect to one other conveyor"); + return point; + } + if (toPath.type === "ArmBot" && hasArmBot) { + console.log("Conveyor can only connect to one ArmBot"); + return point; + } + if (toPath.type === "Vehicle" && hasVehicle) { + console.log("Conveyor can only connect to one Vehicle"); + return point; + } + } + if (!existingTargets.some((target) => target.modelUUID === newTarget.modelUUID && target.pointUUID === newTarget.pointUUID)) { return { ...point, @@ -66,6 +97,36 @@ function PathConnector({ pathsGroupRef, }: { pathsGroupRef: React.MutableRefObje }; const existingTargets = point.connections.targets || []; + // Check connection limits + const fromPath = simulationStates.find(p => p.modeluuid === fromModelUUID); + if (fromPath) { + const hasConveyor = existingTargets.some(t => { + const targetPath = simulationStates.find(p => p.modeluuid === t.modelUUID); + return targetPath?.type === "Conveyor"; + }); + const hasArmBot = existingTargets.some(t => { + const targetPath = simulationStates.find(p => p.modeluuid === t.modelUUID); + return targetPath?.type === "ArmBot"; + }); + const hasVehicle = existingTargets.some(t => { + const targetPath = simulationStates.find(p => p.modeluuid === t.modelUUID); + return targetPath?.type === "Vehicle"; + }); + + if (fromPath.type === "Conveyor" && hasConveyor) { + console.log("Conveyor can only connect to one other conveyor"); + return point; + } + if (fromPath.type === "ArmBot" && hasArmBot) { + console.log("Conveyor can only connect to one ArmBot"); + return point; + } + if (fromPath.type === "Vehicle" && hasVehicle) { + console.log("Conveyor can only connect to one Vehicle"); + return point; + } + } + if (!existingTargets.some((target) => target.modelUUID === reverseTarget.modelUUID && target.pointUUID === reverseTarget.pointUUID)) { return { ...point, @@ -494,21 +555,59 @@ function PathConnector({ pathsGroupRef, }: { pathsGroupRef: React.MutableRefObje } } - // For non-Vehicle paths, check if already connected - if (intersected.userData.path.type !== "Vehicle") { - const isAlreadyConnected = simulationStates.some((path) => { - if (path.type === "Conveyor") { - return path.points.some( - (point) => - point.uuid === sphereUUID && - point.connections.targets.length > 0 - ); + // For Conveyors, check connection limits in BOTH DIRECTIONS + if (firstSelected && (firstPath?.type === "Conveyor" || secondPath?.type === "Conveyor")) { + const checkConveyorLimits = (path: any, pointUUID: string) => { + if (path?.type === "Conveyor") { + const point = path.points.find((p: { uuid: string }) => p.uuid === pointUUID); + if (point) { + return { + hasConveyor: point.connections.targets.some((t: { modelUUID: string }) => { + const targetPath = simulationStates.find((p: { modeluuid: string }) => p.modeluuid === t.modelUUID); + return targetPath?.type === "Conveyor"; + }), + hasArmBot: point.connections.targets.some((t: { modelUUID: string }) => { + const targetPath = simulationStates.find((p: { modeluuid: string }) => p.modeluuid === t.modelUUID); + return targetPath?.type === "ArmBot"; + }), + hasVehicle: point.connections.targets.some((t: { modelUUID: string }) => { + const targetPath = simulationStates.find(p => p.modeluuid === t.modelUUID); + return targetPath?.type === "Vehicle"; + }) + }; + } } - return false; - }); + return { hasConveyor: false, hasArmBot: false, hasVehicle: false }; + }; - if (isAlreadyConnected) { - console.log("Conveyor point is already connected. Ignoring."); + const firstConveyorLimits = checkConveyorLimits(firstPath, firstSelected?.sphereUUID); + const secondConveyorLimits = checkConveyorLimits(secondPath, sphereUUID); + + // Check if trying to connect two conveyors + if (firstPath?.type === "Conveyor" && secondPath?.type === "Conveyor") { + if (firstConveyorLimits.hasConveyor || secondConveyorLimits.hasConveyor) { + console.log("Conveyor can only connect to one other conveyor"); + return; + } + } + + // Check if trying to connect to an ArmBot when already connected to one + if (secondPath?.type === "ArmBot" && firstConveyorLimits.hasArmBot) { + console.log("Conveyor can only connect to one ArmBot"); + return; + } + if (firstPath?.type === "ArmBot" && secondConveyorLimits.hasArmBot) { + console.log("Conveyor can only connect to one ArmBot"); + return; + } + + // Check if trying to connect to a Vehicle when already connected to one + if (secondPath?.type === "Vehicle" && firstConveyorLimits.hasVehicle) { + console.log("Conveyor can only connect to one Vehicle"); + return; + } + if (firstPath?.type === "Vehicle" && secondConveyorLimits.hasVehicle) { + console.log("Conveyor can only connect to one Vehicle"); return; } } @@ -762,6 +861,45 @@ function PathConnector({ pathsGroupRef, }: { pathsGroupRef: React.MutableRefObje !(firstPath?.type === 'Conveyor' || firstPath?.type === 'StaticMachine' || secondPath?.type === 'Conveyor' || secondPath?.type === 'StaticMachine'); + // NEW: Check conveyor connection limits + let isConveyorAtMaxConnections = false; + if (firstPath?.type === 'Conveyor' || secondPath?.type === 'Conveyor') { + const conveyorPath = firstPath?.type === 'Conveyor' ? firstPath : secondPath; + const otherPath = firstPath?.type === 'Conveyor' ? secondPath : firstPath; + + if (conveyorPath) { + const conveyorPoint = Array.isArray(conveyorPath.points) + ? conveyorPath.points.find((p: { uuid: string }) => p.uuid === + (firstPath?.type === 'Conveyor' ? firstSelected.sphereUUID : sphereUUID)) + : undefined; + + if (conveyorPoint) { + const hasConveyor = conveyorPoint.connections.targets.some((t: { modelUUID: string }) => { + const targetPath = simulationStates.find((p: { modeluuid: string }) => p.modeluuid === t.modelUUID); + return targetPath?.type === 'Conveyor'; + }); + const hasArmBot = conveyorPoint.connections.targets.some((t: { modelUUID: string }) => { + const targetPath = simulationStates.find((p: { modeluuid: string }) => p.modeluuid === t.modelUUID); + return targetPath?.type === 'ArmBot'; + }); + const hasVehicle = conveyorPoint.connections.targets.some((t: { modelUUID: string }) => { + const targetPath = simulationStates.find(p => p.modeluuid === t.modelUUID); + return targetPath?.type === 'Vehicle'; + }); + + if (otherPath?.type === 'Conveyor' && hasConveyor) { + isConveyorAtMaxConnections = true; + } + if (otherPath?.type === 'ArmBot' && hasArmBot) { + isConveyorAtMaxConnections = true; + } + if (otherPath?.type === 'Vehicle' && hasVehicle) { + isConveyorAtMaxConnections = true; + } + } + } + } + if ( !isDuplicateConnection && !isVehicleToVehicle && @@ -773,6 +911,7 @@ function PathConnector({ pathsGroupRef, }: { pathsGroupRef: React.MutableRefObje !isArmBotToArmBot && !isArmBotToInvalidType && !isArmBotAlreadyConnectedToStatic && + !isConveyorAtMaxConnections && // NEW: Check conveyor limits firstSelected.sphereUUID !== sphereUUID && firstSelected.modelUUID !== modelUUID && (firstSelected.isCorner || isConnectable) && @@ -957,7 +1096,6 @@ function PathConnector({ pathsGroupRef, }: { pathsGroupRef: React.MutableRefObje state.modeluuid === connection2.model ); - console.log("updatedPaths: ", updatedPaths); updateBackend(updatedPaths); setSimulationStates(updatedStates); diff --git a/app/src/modules/simulation/process/processAnimator.tsx b/app/src/modules/simulation/process/processAnimator.tsx index 981846b..ddf21be 100644 --- a/app/src/modules/simulation/process/processAnimator.tsx +++ b/app/src/modules/simulation/process/processAnimator.tsx @@ -564,9 +564,9 @@ const ProcessAnimator: React.FC = ({ // Log if no animation is occurring when it should if (!animationOccurring && !isConnected) { - console.log( - `Warning: No animation occurring for process ${process.id} despite not being connected` - ); + // console.log( + // `Warning: No animation occurring for process ${process.id} despite not being connected` + // ); } newStates[process.id] = {