reset function updated
This commit is contained in:
parent
f888c2798c
commit
be8f937759
|
@ -38,9 +38,17 @@ const SimulationPlayer: React.FC = () => {
|
||||||
const { isReset, setReset } = useResetButtonStore();
|
const { isReset, setReset } = useResetButtonStore();
|
||||||
const { subModule } = useSubModuleStore();
|
const { subModule } = useSubModuleStore();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (isReset) {
|
||||||
|
setTimeout(()=>{
|
||||||
|
setReset(false);
|
||||||
|
},0)
|
||||||
|
}
|
||||||
|
}, [isReset])
|
||||||
|
|
||||||
// Button functions
|
// Button functions
|
||||||
const handleReset = () => {
|
const handleReset = () => {
|
||||||
setReset(!isReset);
|
setReset(true);
|
||||||
setSpeed(1);
|
setSpeed(1);
|
||||||
};
|
};
|
||||||
const handlePlayStop = () => {
|
const handlePlayStop = () => {
|
||||||
|
@ -271,8 +279,7 @@ const SimulationPlayer: React.FC = () => {
|
||||||
</div>
|
</div>
|
||||||
{index < intervals.length - 1 && (
|
{index < intervals.length - 1 && (
|
||||||
<div
|
<div
|
||||||
className={`line ${
|
className={`line ${progress >= ((index + 1) / totalSegments) * 100
|
||||||
progress >= ((index + 1) / totalSegments) * 100
|
|
||||||
? "filled"
|
? "filled"
|
||||||
: ""
|
: ""
|
||||||
}`}
|
}`}
|
||||||
|
@ -314,8 +321,7 @@ const SimulationPlayer: React.FC = () => {
|
||||||
<div className="custom-slider-wrapper">
|
<div className="custom-slider-wrapper">
|
||||||
<div className="custom-slider">
|
<div className="custom-slider">
|
||||||
<button
|
<button
|
||||||
className={`slider-handle ${
|
className={`slider-handle ${isDragging ? "dragging" : ""
|
||||||
isDragging ? "dragging" : ""
|
|
||||||
}`}
|
}`}
|
||||||
style={{ left: `${calculateHandlePosition()}%` }}
|
style={{ left: `${calculateHandlePosition()}%` }}
|
||||||
onMouseDown={handleMouseDown}
|
onMouseDown={handleMouseDown}
|
||||||
|
|
|
@ -44,8 +44,9 @@ function RoboticArmAnimator({
|
||||||
setCirclePoints(points);
|
setCirclePoints(points);
|
||||||
}, [armBot.position]);
|
}, [armBot.position]);
|
||||||
|
|
||||||
|
//Handle Reset Animation
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isReset || !isPlaying) {
|
if (isReset ) {
|
||||||
progressRef.current = 0;
|
progressRef.current = 0;
|
||||||
curveRef.current = null;
|
curveRef.current = null;
|
||||||
setCurrentPath([]);
|
setCurrentPath([]);
|
||||||
|
@ -53,11 +54,15 @@ function RoboticArmAnimator({
|
||||||
totalDistanceRef.current = 0;
|
totalDistanceRef.current = 0;
|
||||||
startTimeRef.current = null;
|
startTimeRef.current = null;
|
||||||
segmentDistancesRef.current = [];
|
segmentDistancesRef.current = [];
|
||||||
setReset(false);
|
if (!ikSolver) return
|
||||||
|
const bone = ikSolver.mesh.skeleton.bones.find((b: any) => b.name === targetBone);
|
||||||
|
if (!bone) return;
|
||||||
|
bone.position.copy(restPosition)
|
||||||
|
ikSolver.update();
|
||||||
}
|
}
|
||||||
}, [isReset, isPlaying])
|
}, [isReset, isPlaying])
|
||||||
|
|
||||||
|
//Generate Circle Points
|
||||||
function generateRingPoints(radius: any, segments: any) {
|
function generateRingPoints(radius: any, segments: any) {
|
||||||
const points: [number, number, number][] = [];
|
const points: [number, number, number][] = [];
|
||||||
for (let i = 0; i < segments; i++) {
|
for (let i = 0; i < segments; i++) {
|
||||||
|
@ -70,7 +75,7 @@ function RoboticArmAnimator({
|
||||||
}
|
}
|
||||||
return points;
|
return points;
|
||||||
}
|
}
|
||||||
|
// Function for find nearest Circlepoints Index
|
||||||
const findNearestIndex = (nearestPoint: [number, number, number], points: [number, number, number][], epsilon = 1e-6) => {
|
const findNearestIndex = (nearestPoint: [number, number, number], points: [number, number, number][], epsilon = 1e-6) => {
|
||||||
for (let i = 0; i < points.length; i++) {
|
for (let i = 0; i < points.length; i++) {
|
||||||
const [x, y, z] = points[i];
|
const [x, y, z] = points[i];
|
||||||
|
@ -85,6 +90,15 @@ function RoboticArmAnimator({
|
||||||
return -1; // Not found
|
return -1; // Not found
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//function to find nearest Circlepoints
|
||||||
|
const findNearest = (target: [number, number, number]) => {
|
||||||
|
return circlePoints.reduce((nearest, point) => {
|
||||||
|
const distance = Math.hypot(target[0] - point[0], target[1] - point[1], target[2] - point[2]);
|
||||||
|
const nearestDistance = Math.hypot(target[0] - nearest[0], target[1] - nearest[1], target[2] - nearest[2]);
|
||||||
|
return distance < nearestDistance ? point : nearest;
|
||||||
|
}, circlePoints[0]);
|
||||||
|
};
|
||||||
|
|
||||||
// Handle nearest points and final path (including arc points)
|
// Handle nearest points and final path (including arc points)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (circlePoints.length > 0 && currentPath.length > 0) {
|
if (circlePoints.length > 0 && currentPath.length > 0) {
|
||||||
|
@ -95,13 +109,13 @@ function RoboticArmAnimator({
|
||||||
const raisedStart = [start[0], start[1] + 0.5, start[2]] as [number, number, number];
|
const raisedStart = [start[0], start[1] + 0.5, start[2]] as [number, number, number];
|
||||||
const raisedEnd = [end[0], end[1] + 0.5, end[2]] as [number, number, number];
|
const raisedEnd = [end[0], end[1] + 0.5, end[2]] as [number, number, number];
|
||||||
|
|
||||||
const findNearest = (target: [number, number, number]) => {
|
// const findNearest = (target: [number, number, number]) => {
|
||||||
return circlePoints.reduce((nearest, point) => {
|
// return circlePoints.reduce((nearest, point) => {
|
||||||
const distance = Math.hypot(target[0] - point[0], target[1] - point[1], target[2] - point[2]);
|
// const distance = Math.hypot(target[0] - point[0], target[1] - point[1], target[2] - point[2]);
|
||||||
const nearestDistance = Math.hypot(target[0] - nearest[0], target[1] - nearest[1], target[2] - nearest[2]);
|
// const nearestDistance = Math.hypot(target[0] - nearest[0], target[1] - nearest[1], target[2] - nearest[2]);
|
||||||
return distance < nearestDistance ? point : nearest;
|
// return distance < nearestDistance ? point : nearest;
|
||||||
}, circlePoints[0]);
|
// }, circlePoints[0]);
|
||||||
};
|
// };
|
||||||
|
|
||||||
const nearestToStart = findNearest(raisedStart);
|
const nearestToStart = findNearest(raisedStart);
|
||||||
const nearestToEnd = findNearest(raisedEnd);
|
const nearestToEnd = findNearest(raisedEnd);
|
||||||
|
@ -176,8 +190,13 @@ function RoboticArmAnimator({
|
||||||
if (!bone) return;
|
if (!bone) return;
|
||||||
|
|
||||||
if (isPlaying) {
|
if (isPlaying) {
|
||||||
|
if (isReset) {
|
||||||
|
bone.position.copy(restPosition);
|
||||||
|
setCustomCurvePoints([]);
|
||||||
|
ikSolver.update();
|
||||||
|
}
|
||||||
if (!isPaused && customCurvePoints && customCurvePoints.length > 0) {
|
if (!isPaused && customCurvePoints && customCurvePoints.length > 0) {
|
||||||
const distances = segmentDistancesRef.current; // distances between each pair of points
|
const distances = segmentDistancesRef.current;
|
||||||
const totalDistance = totalDistanceRef.current;
|
const totalDistance = totalDistanceRef.current;
|
||||||
|
|
||||||
progressRef.current += delta * (speed * armBot.speed);
|
progressRef.current += delta * (speed * armBot.speed);
|
||||||
|
@ -186,11 +205,11 @@ function RoboticArmAnimator({
|
||||||
let index = 0;
|
let index = 0;
|
||||||
let accumulatedDistance = 0;
|
let accumulatedDistance = 0;
|
||||||
|
|
||||||
// Find which segment we are currently in
|
|
||||||
while (index < distances.length && coveredDistance > accumulatedDistance + distances[index]) {
|
while (index < distances.length && coveredDistance > accumulatedDistance + distances[index]) {
|
||||||
accumulatedDistance += distances[index];
|
accumulatedDistance += distances[index];
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (index < distances.length) {
|
if (index < distances.length) {
|
||||||
const startPoint = customCurvePoints[index];
|
const startPoint = customCurvePoints[index];
|
||||||
const endPoint = customCurvePoints[index + 1];
|
const endPoint = customCurvePoints[index + 1];
|
||||||
|
@ -201,6 +220,7 @@ function RoboticArmAnimator({
|
||||||
bone.position.copy(position);
|
bone.position.copy(position);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (progressRef.current >= totalDistance) {
|
if (progressRef.current >= totalDistance) {
|
||||||
HandleCallback();
|
HandleCallback();
|
||||||
setCurrentPath([]);
|
setCurrentPath([]);
|
||||||
|
@ -212,13 +232,13 @@ function RoboticArmAnimator({
|
||||||
|
|
||||||
ikSolver.update();
|
ikSolver.update();
|
||||||
}
|
}
|
||||||
} else if ((!isPlaying && currentPath.length === 0) || isReset) {
|
} else if (!isPlaying && currentPath.length === 0) {
|
||||||
bone.position.copy(restPosition);
|
bone.position.copy(restPosition);
|
||||||
ikSolver.update();
|
|
||||||
}
|
}
|
||||||
|
ikSolver.update();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{customCurvePoints && customCurvePoints?.length >= 2 && currentPath && isPlaying && (
|
{customCurvePoints && customCurvePoints?.length >= 2 && currentPath && isPlaying && (
|
||||||
|
|
|
@ -97,25 +97,25 @@ function RoboticArmInstance({ armBot }: { armBot: ArmBotStatus }) {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isReset || !isPlaying) {
|
if (isReset || !isPlaying) {
|
||||||
logStatus(armBot.modelUuid, "Simulation Play Reset Successfully")
|
logStatus(armBot.modelUuid, "Simulation Play Reset Successfully")
|
||||||
removeCurrentAction(armBot.modelUuid)
|
|
||||||
setArmBotActive(armBot.modelUuid, false)
|
setArmBotActive(armBot.modelUuid, false)
|
||||||
setArmBotState(armBot.modelUuid, "idle")
|
setArmBotState(armBot.modelUuid, "idle")
|
||||||
setCurrentPhase("init");
|
setCurrentPhase("init");
|
||||||
|
setPath([])
|
||||||
|
removeCurrentAction(armBot.modelUuid)
|
||||||
isPausedRef.current = false
|
isPausedRef.current = false
|
||||||
pauseTimeRef.current = null
|
pauseTimeRef.current = null
|
||||||
startTime = 0
|
startTime = 0
|
||||||
const targetBones = ikSolver?.mesh.skeleton.bones.find(
|
const targetBones = ikSolver?.mesh.skeleton.bones.find((b: any) => b.name === targetBone
|
||||||
(b: any) => b.name === targetBone
|
|
||||||
);
|
);
|
||||||
if (targetBones) {
|
if (targetBones && isPlaying) {
|
||||||
let curve = createCurveBetweenTwoPoints(targetBones.position, restPosition)
|
let curve = createCurveBetweenTwoPoints(targetBones.position, restPosition)
|
||||||
if (curve) {
|
if (curve) {
|
||||||
setPath(curve.points.map(point => [point.x, point.y, point.z]));
|
setPath(curve.points.map(point => [point.x, point.y, point.z]));
|
||||||
}
|
|
||||||
}
|
|
||||||
setReset(false);
|
|
||||||
logStatus(armBot.modelUuid, "Moving armBot from initial point to rest position.")
|
logStatus(armBot.modelUuid, "Moving armBot from initial point to rest position.")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
// setReset(false);
|
||||||
|
}
|
||||||
}, [isReset, isPlaying])
|
}, [isReset, isPlaying])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
@ -127,7 +127,6 @@ function RoboticArmInstance({ armBot }: { armBot: ArmBotStatus }) {
|
||||||
(b: any) => b.name === targetBone
|
(b: any) => b.name === targetBone
|
||||||
);
|
);
|
||||||
if (isPlaying) {
|
if (isPlaying) {
|
||||||
|
|
||||||
//Moving armBot from initial point to rest position.
|
//Moving armBot from initial point to rest position.
|
||||||
if (!armBot?.isActive && armBot?.state == "idle" && currentPhase == "init") {
|
if (!armBot?.isActive && armBot?.state == "idle" && currentPhase == "init") {
|
||||||
|
|
||||||
|
@ -204,16 +203,16 @@ function RoboticArmInstance({ armBot }: { armBot: ArmBotStatus }) {
|
||||||
// logStatus(armBot.modelUuid, "Moving armBot from end point to rest position.")
|
// logStatus(armBot.modelUuid, "Moving armBot from end point to rest position.")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
logStatus(armBot.modelUuid, "Simulation Play Exited")
|
// logStatus(armBot.modelUuid, "Simulation Play Exited")
|
||||||
setArmBotActive(armBot.modelUuid, false)
|
// setArmBotActive(armBot.modelUuid, false)
|
||||||
setArmBotState(armBot.modelUuid, "idle")
|
// setArmBotState(armBot.modelUuid, "idle")
|
||||||
setCurrentPhase("init");
|
// setCurrentPhase("init");
|
||||||
setPath([])
|
// setPath([])
|
||||||
isPausedRef.current = false
|
// isPausedRef.current = false
|
||||||
pauseTimeRef.current = null
|
// pauseTimeRef.current = null
|
||||||
isPausedRef.current = false
|
// isPausedRef.current = false
|
||||||
startTime = 0
|
// startTime = 0
|
||||||
removeCurrentAction(armBot.modelUuid)
|
// removeCurrentAction(armBot.modelUuid)
|
||||||
}
|
}
|
||||||
|
|
||||||
}, [currentPhase, armBot, isPlaying, ikSolver])
|
}, [currentPhase, armBot, isPlaying, ikSolver])
|
||||||
|
@ -260,6 +259,7 @@ function RoboticArmInstance({ armBot }: { armBot: ArmBotStatus }) {
|
||||||
}
|
}
|
||||||
const logStatus = (id: string, status: string) => {
|
const logStatus = (id: string, status: string) => {
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ function Simulator() {
|
||||||
const executionOrder = determineExecutionOrder(products);
|
const executionOrder = determineExecutionOrder(products);
|
||||||
|
|
||||||
executionOrder.forEach(point => {
|
executionOrder.forEach(point => {
|
||||||
useActionHandler(point);
|
// useActionHandler(point);
|
||||||
});
|
});
|
||||||
|
|
||||||
function determineExecutionSequences(products: productsSchema): PointsScheme[][] {
|
function determineExecutionSequences(products: productsSchema): PointsScheme[][] {
|
||||||
|
|
Loading…
Reference in New Issue