Skip to content

Commit 35bab58

Browse files
fix(RB-3031): RobotAnimator not updating on motion state prop change (#470)
When introduced into the current state of the RobotPad, the V2-Type-based RobotAnimator did not update the robot position on motion state change. It worked setting the position during initalization though. The file change fixes the bug, allowing the robot visualization to behave accordingly Before (.mov video) https://github.com/user-attachments/assets/8d56e3ac-0d8d-49b7-96ea-0d8c858c54dd After (.mov video) https://github.com/user-attachments/assets/1beeada9-6f7d-4cde-8cec-f1d6f7556b06
1 parent 848289b commit 35bab58

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/components/robots/RobotAnimator.tsx

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useFrame, useThree } from "@react-three/fiber"
22
import type { DHParameter, MotionGroupState } from "@wandelbots/nova-js/v2"
3-
import React, { useEffect, useRef } from "react"
3+
import React, { useEffect, useRef, useCallback } from "react"
44
import type { Group, Object3D } from "three"
55
import { useAutorun } from "../utils/hooks"
66
import { ValueInterpolator } from "../utils/interpolation"
@@ -64,11 +64,6 @@ export default function RobotAnimator({
6464
invalidate()
6565
}
6666

67-
function updateJoints(newJointValues: number[]) {
68-
jointValues.current = newJointValues
69-
interpolatorRef.current?.setTarget(newJointValues)
70-
}
71-
7267
function setRotation() {
7368
const updatedJointValues = interpolatorRef.current?.getCurrentValues() || []
7469

@@ -86,13 +81,24 @@ export default function RobotAnimator({
8681
}
8782
}
8883

89-
useAutorun(() => {
84+
const updateJoints = useCallback(() => {
9085
const newJointValues = rapidlyChangingMotionState.joint_position.filter(
9186
(item) => item !== undefined,
9287
)
9388

94-
requestAnimationFrame(() => updateJoints(newJointValues))
95-
})
89+
jointValues.current = newJointValues
90+
interpolatorRef.current?.setTarget(newJointValues)
91+
}, [rapidlyChangingMotionState])
92+
93+
/**
94+
* Fire an update joints call on every motion state change.
95+
* requestAnimationFrame used to avoid blocking main thread
96+
*/
97+
useEffect(() => {
98+
requestAnimationFrame(() => {
99+
updateJoints()
100+
})
101+
}, [rapidlyChangingMotionState, updateJoints])
96102

97103
return <group ref={setGroupRef}>{children}</group>
98104
}

0 commit comments

Comments
 (0)