diff --git a/app/src/modules/simulation/events/points/creator/pointsCreator.tsx b/app/src/modules/simulation/events/points/creator/pointsCreator.tsx index 24a8ac6..3acee3c 100644 --- a/app/src/modules/simulation/events/points/creator/pointsCreator.tsx +++ b/app/src/modules/simulation/events/points/creator/pointsCreator.tsx @@ -8,11 +8,13 @@ import { useSelectedEventSphere, useSelectedEventData, } from "../../../../../store/simulation/useSimulationStore"; -import PickDropPoints from "../../../../../components/ui/arm/PickDropPoints"; +import PickDropPoints from "../../../ui/arm/PickDropPoints"; import armPick from "../../../../../assets/gltf-glb/arm_ui_pick.glb"; import armDrop from "../../../../../assets/gltf-glb/arm_ui_drop.glb"; -import useDraggableGLTF from "../../../../../components/ui/arm/useDraggableGLTF"; +import useDraggableGLTF from "../../../ui/arm/useDraggableGLTF"; +import { useArmBotStore } from "../../../../../store/simulation/useArmBotStore"; +import { useThree } from "@react-three/fiber"; interface Process { startPoint: number[] | null; @@ -47,117 +49,53 @@ interface RoboticArmEvent { function PointsCreator() { const { events, updatePoint, getPointByUuid, getEventByModelUuid } = useEventsStore(); - - const [armBotStatusSample, setArmBotStatusSample] = useState< - RoboticArmEvent[] - >([ - { - modelUuid: "b3556818-9e46-48b0-a869-a75c92857125", - modelName: "robotic_arm", - position: [0, 0, 0], - rotation: [0, 0, 0], - state: "idle", - type: "roboticArm", - speed: 1.5, - point: { - uuid: "point-123", - position: [0, 1.5, 0], - rotation: [0, 0, 0], - actions: [ - { - actionUuid: "action-001", - actionName: "Pick Component", - actionType: "pickAndPlace", - process: { - startPoint: null, - endPoint: null, - }, - triggers: [], - }, - { - actionUuid: "action-002", - actionName: "Pick Component", - actionType: "pickAndPlace", - process: { - startPoint: null, - endPoint: null, - }, - triggers: [], - }, - ], - }, - }, - { - modelUuid: "16a394c7-0808-4bdf-a5d3-e5ca141ffb9f", - modelName: "arm without rig (1)", - position: [0, 0, 0], - rotation: [0, 0, 0], - state: "idle", - type: "roboticArm", - speed: 1.5, - point: { - uuid: "point-123", - position: [0, 1.5, 0], - rotation: [0, 0, 0], - actions: [ - { - actionUuid: "action-001", - actionName: "Pick Component", - actionType: "pickAndPlace", - process: { - startPoint: null, - endPoint: null, - }, - triggers: [], - }, - ], - }, - }, - ]); + const { armBots, updateArmBot, addArmBot, removeArmBot } = useArmBotStore() const armUiPick = useGLTF(armPick) as any; const armUiDrop = useGLTF(armDrop) as any; const updatePointToState = (obj: THREE.Object3D) => { const { modelUuid, pointUuid, actionType, actionUuid } = obj.userData; - const newPosition = obj.position.toArray(); + + const newPosition = new THREE.Vector3(); + obj.getWorldPosition(newPosition); + const worldPositionArray = newPosition.toArray(); - setArmBotStatusSample((prev) => - prev.map((event) => { - if (event.modelUuid === modelUuid) { - const updatedActions = event.point.actions.map((action) => { - if (action.actionUuid === actionUuid) { - const updatedProcess = { ...action.process }; - if (actionType === "pick") { - updatedProcess.startPoint = newPosition; - } else if (actionType === "drop") { - updatedProcess.endPoint = newPosition; - } - return { - ...action, - process: updatedProcess, - }; - } - return action; - }); + const armBot = armBots.find((a) => a.modelUuid === modelUuid); + if (!armBot) return; - return { - ...event, - point: { - ...event.point, - actions: updatedActions, - }, - }; + const updatedActions = armBot.point.actions.map((action: any) => { + if (action.actionUuid === actionUuid) { + const updatedProcess = { ...action.process }; + + if (actionType === "pick") { + updatedProcess.startPoint = getLocalPosition(modelUuid, worldPositionArray); } - return event; - }) - ); + if (actionType === "drop") { + updatedProcess.endPoint = getLocalPosition(modelUuid, worldPositionArray); + } + return { + ...action, + process: updatedProcess, + }; + } + return action; + }); + + updateArmBot(modelUuid, { + point: { + ...armBot.point, + actions: updatedActions, + }, + }); }; - const { handlePointerDown } = useDraggableGLTF(updatePointToState); + const { handlePointerDown } = useDraggableGLTF(updatePointToState); + const { scene } = useThree(); const { activeModule } = useModuleStore(); const transformRef = useRef(null); + const groupRef = useRef(null); const [transformMode, setTransformMode] = useState< "translate" | "rotate" | null >(null); @@ -206,12 +144,13 @@ function PointsCreator() { const getDefaultPositions = (modelUuid: string) => { const modelData = getModelByUuid(modelUuid); if (modelData) { + const baseX = modelData.position?.[0] || 0; - const baseY = modelData.position?.[1] + 2.8 || 1.5; + const baseY = 2.6; const baseZ = modelData.position?.[2] || 0; return { - pick: [baseX, baseY, baseZ - 2.5], - drop: [baseX, baseY, baseZ - 0.5], + pick: [baseX, baseY, baseZ + 0.4], + drop: [baseX, baseY, baseZ - 1.2], default: [baseX, baseY, baseZ - 1.5], }; } @@ -231,113 +170,61 @@ function PointsCreator() { } const storeModels = (useModuleStore.getState() as any).models || []; return storeModels.find((m: any) => m.modelUuid === modelUuid); - } catch (error) {} + } catch (error) { } return null; }; + function getLocalPosition(parentUuid: string, worldPosArray: [number, number, number] | null): [number, number, number] | null { + if (worldPosArray) { + const worldPos = new THREE.Vector3(...worldPosArray); + const localPos = worldPos.clone(); + + const parentObject = scene.getObjectByProperty('uuid', parentUuid); + if (parentObject) { + parentObject.worldToLocal(localPos); + + return [localPos.x, localPos.y, localPos.z]; + } else { + + } + } + return null; + } + useEffect(() => { - console.log("armBotStatusSample: ", armBotStatusSample); - }, [armBotStatusSample]); + + }, [armBots]); return ( <> {activeModule === "simulation" && ( <> - {events.map((event, i) => { - if (event.type === "transfer") { - return ( - - {event.points.map((point, j) => ( - (sphereRefs.current[point.uuid] = el!)} - onClick={(e) => { - e.stopPropagation(); - setSelectedEventSphere( - sphereRefs.current[point.uuid] - ); - }} - onPointerMissed={() => { - clearSelectedEventSphere(); - setTransformMode(null); - }} - key={`${i}-${j}`} - position={new THREE.Vector3(...point.position)} - userData={{ - modelUuid: event.modelUuid, - pointUuid: point.uuid, - }} - > - - - - ))} - - ); - } else if (event.type === "vehicle") { - return ( - - (sphereRefs.current[event.point.uuid] = el!)} - onClick={(e) => { - e.stopPropagation(); - setSelectedEventSphere( - sphereRefs.current[event.point.uuid] - ); - }} - onPointerMissed={() => { - clearSelectedEventSphere(); - setTransformMode(null); - }} - position={new THREE.Vector3(...event.point.position)} - userData={{ - modelUuid: event.modelUuid, - pointUuid: event.point.uuid, - }} - > - - - - - ); - } else if (event.type === "roboticArm") { + {armBots.map((event, i) => { + if (event.type === "roboticArm") { const defaultPositions = getDefaultPositions(event.modelUuid); const isSelected = selectedPoint?.userData?.modelUuid === event.modelUuid; - return ( (sphereRefs.current[event.point.uuid] = el!)} onClick={(e) => { e.stopPropagation(); - setSelectedEventSphere( - sphereRefs.current[event.point.uuid] - ); + setSelectedEventSphere(sphereRefs.current[event.point.uuid]); setSelectedPoint(e.object as THREE.Mesh); }} onPointerMissed={() => { clearSelectedEventSphere(); setTransformMode(null); }} - position={new THREE.Vector3(...defaultPositions.default)} - userData={{ - modelUuid: event.modelUuid, - pointUuid: event.point.uuid, - }} + position={new THREE.Vector3(...event.point.position)} + userData={{ modelUuid: event.modelUuid, pointUuid: event.point.uuid }} > {event.point.actions.map((action) => { if (action.actionType === "pickAndPlace") { + const pickPosition = action.process.startPoint || defaultPositions.pick; const dropPosition = @@ -385,37 +273,6 @@ function PointsCreator() { })} ); - } else if (event.type === "machine") { - return ( - - (sphereRefs.current[event.point.uuid] = el!)} - onClick={(e) => { - e.stopPropagation(); - setSelectedEventSphere( - sphereRefs.current[event.point.uuid] - ); - }} - onPointerMissed={() => { - clearSelectedEventSphere(); - setTransformMode(null); - }} - position={new THREE.Vector3(...event.point.position)} - userData={{ - modelUuid: event.modelUuid, - pointUuid: event.point.uuid, - }} - > - - - - - ); } else { return null; } diff --git a/app/src/modules/simulation/roboticArm/instances/armInstance/roboticArmInstance.tsx b/app/src/modules/simulation/roboticArm/instances/armInstance/roboticArmInstance.tsx index d934a2d..fe758c5 100644 --- a/app/src/modules/simulation/roboticArm/instances/armInstance/roboticArmInstance.tsx +++ b/app/src/modules/simulation/roboticArm/instances/armInstance/roboticArmInstance.tsx @@ -67,7 +67,7 @@ function RoboticArmInstance({ robot }: { robot: ArmBotStatus }) { } //Waiting for trigger. else if (robot && !robot.isActive && robot.state === "idle" && currentPhase === "rest" && !robot.currentAction) { - console.log("trigger"); + logStatus(robot.modelUuid, "Waiting to trigger CurrentAction") setTimeout(() => { addCurrentAction(robot.modelUuid, 'action-003'); }, 3000); diff --git a/app/src/modules/simulation/roboticArm/instances/ikInstance/ikInstance.tsx b/app/src/modules/simulation/roboticArm/instances/ikInstance/ikInstance.tsx index 29017ee..ca9edde 100644 --- a/app/src/modules/simulation/roboticArm/instances/ikInstance/ikInstance.tsx +++ b/app/src/modules/simulation/roboticArm/instances/ikInstance/ikInstance.tsx @@ -15,7 +15,7 @@ type IKInstanceProps = { setArmBotCurvePoints: any }; function IKInstance({ modelUrl, setIkSolver, ikSolver, robot, groupRef, processes, setArmBotCurvePoints }: IKInstanceProps) { - + const { scene } = useThree(); const gltf = useLoader(GLTFLoader, modelUrl, (loader) => { const draco = new DRACOLoader(); @@ -65,17 +65,8 @@ function IKInstance({ modelUrl, setIkSolver, ikSolver, robot, groupRef, processe const solver = new CCDIKSolver(OOI.Skinned_Mesh, iks); setIkSolver(solver); - const helper = new CCDIKHelper(OOI.Skinned_Mesh, iks, 0.05); - // if (solver) { - // const bone = solver.mesh.skeleton.bones.find( - // (b: any) => b.name === targetBoneName - // ) ?? ""; - // if (bone) { - // const position = new THREE.Vector3(); - // bone.getWorldPosition(position); - // console.log("world position", position.x, position.y, position.z); // this is the bone's world position - // } - // } + const helper = new CCDIKHelper(OOI.Skinned_Mesh, iks, 0.05) + scene.add(helper) diff --git a/app/src/modules/simulation/roboticArm/roboticArm.tsx b/app/src/modules/simulation/roboticArm/roboticArm.tsx index 66db204..69e6da0 100644 --- a/app/src/modules/simulation/roboticArm/roboticArm.tsx +++ b/app/src/modules/simulation/roboticArm/roboticArm.tsx @@ -6,12 +6,12 @@ import { useFloorItems } from "../../../store/store"; function RoboticArm() { const { armBots, addArmBot, removeArmBot } = useArmBotStore(); const { floorItems } = useFloorItems(); - + const armBotStatusSample: RoboticArmEventSchema[] = [ { state: "idle", - modelUuid: "armbot-xyz-001", + modelUuid: "3abf5d46-b59e-4e6b-9c02-a4634b64b82d", modelName: "ArmBot-X200", position: [0.20849215906958463, 0, 0.32079278127773675], rotation: [-1.3768690876192207e-15, 1.4883085074751308, 1.5407776675834467e-15], @@ -19,7 +19,7 @@ function RoboticArm() { speed: 1.5, point: { uuid: "point-123", - position: [0, 1.5, 0], + position: [0, 2.6, 0], rotation: [0, 0, 0], actions: [ { @@ -30,6 +30,18 @@ function RoboticArm() { startPoint: [-1, 2, 1], endPoint: [-2, 1, -1], }, + // process: { + // "startPoint": [ + // 0.37114476008711866, + // 1.9999999999999998, + // 1.8418816116721384 + // ], + // "endPoint": [ + // -0.42197069459490777, + // 1, + // -3.159515927851809 + // ] + // }, triggers: [ { triggerUuid: "trigger-001", @@ -154,7 +166,7 @@ function RoboticArm() { }, []); useEffect(() => { - // + }, [armBots]); return ( diff --git a/app/src/components/ui/arm/PickDropPoints.tsx b/app/src/modules/simulation/ui/arm/PickDropPoints.tsx similarity index 100% rename from app/src/components/ui/arm/PickDropPoints.tsx rename to app/src/modules/simulation/ui/arm/PickDropPoints.tsx diff --git a/app/src/components/ui/arm/useDraggableGLTF.ts b/app/src/modules/simulation/ui/arm/useDraggableGLTF.ts similarity index 100% rename from app/src/components/ui/arm/useDraggableGLTF.ts rename to app/src/modules/simulation/ui/arm/useDraggableGLTF.ts