From 1612b15436ea7266772db57d7100a35c637a06ca Mon Sep 17 00:00:00 2001 From: Evan Summers Date: Mon, 19 Aug 2024 14:30:58 +0200 Subject: [PATCH] fix: Jogging websocket is now only open when mouse is inside panel --- .../jogging/JoggingCartesianTab.tsx | 30 ++++++++++++++++++- src/components/jogging/JoggingJointTab.tsx | 30 +++++++++++++++---- 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/src/components/jogging/JoggingCartesianTab.tsx b/src/components/jogging/JoggingCartesianTab.tsx index 92c653f..0c22207 100644 --- a/src/components/jogging/JoggingCartesianTab.tsx +++ b/src/components/jogging/JoggingCartesianTab.tsx @@ -18,6 +18,7 @@ import { JoggingVelocitySlider } from "./JoggingVelocitySlider" import { useReaction } from "../utils/hooks" import { JoggingCartesianValues } from "./JoggingCartesianValues" import { JoggingJointLimitDetector } from "./JoggingJointLimitDetector" +import { useEffect } from "react" type JoggingCartesianOpts = { axis: "x" | "y" | "z" @@ -49,6 +50,31 @@ export const JoggingCartesianTab = observer( { fireImmediately: true } as any, ) + useEffect(() => { + // Start in increment mode with no websockets open + store.jogger.setJoggingMode("increment") + + window.addEventListener("blur", disconnectJogger) + + return () => { + window.removeEventListener("blur", disconnectJogger) + } + }, []) + + async function connectJogger() { + store.jogger.setJoggingMode( + store.selectedDiscreteIncrement ? "increment" : "cartesian", + { + tcpId: store.selectedTcpId, + coordSystemId: store.selectedCoordSystemId, + }, + ) + } + + async function disconnectJogger() { + store.jogger.setJoggingMode("increment") + } + async function runIncrementalCartesianJog( opts: JoggingCartesianOpts, increment: DiscreteIncrementOption, @@ -85,6 +111,8 @@ export const JoggingCartesianTab = observer( async function startCartesianJogging(opts: JoggingCartesianOpts) { if (store.isLocked) return + connectJogger() + if (store.selectedDiscreteIncrement) { return runIncrementalCartesianJog(opts, store.selectedDiscreteIncrement) } @@ -143,7 +171,7 @@ export const JoggingCartesianTab = observer( } return ( - + {/* Show Wandelscript string for the current coords */} diff --git a/src/components/jogging/JoggingJointTab.tsx b/src/components/jogging/JoggingJointTab.tsx index 1903f9a..391681a 100644 --- a/src/components/jogging/JoggingJointTab.tsx +++ b/src/components/jogging/JoggingJointTab.tsx @@ -5,9 +5,29 @@ import type { JoggingStore } from "./JoggingStore" import { JoggingVelocitySlider } from "./JoggingVelocitySlider" import { JoggingJointRotationControl } from "./JoggingJointRotationControl" import { JoggingJointValues } from "./JoggingJointValues" +import { useEffect } from "react" export const JoggingJointTab = observer( - ({ store }: { store: JoggingStore; }) => { + ({ store }: { store: JoggingStore }) => { + useEffect(() => { + // Start in increment mode with no websockets open + store.jogger.setJoggingMode("increment") + + window.addEventListener("blur", disconnectJogger) + + return () => { + window.removeEventListener("blur", disconnectJogger) + } + }, []) + + async function connectJogger() { + store.jogger.setJoggingMode("joint") + } + + async function disconnectJogger() { + store.jogger.setJoggingMode("increment") + } + async function startJointJogging(opts: { joint: number direction: "-" | "+" @@ -22,16 +42,14 @@ export const JoggingJointTab = observer( async function stopJointJogging() { await store.jogger.stop() } - + return ( - + {store.jogger.motionStream.joints.map((joint) => { const jointLimits = - store.motionGroupSpec.mechanical_joint_limits?.[ - joint.index - ] + store.motionGroupSpec.mechanical_joint_limits?.[joint.index] const lowerLimitDegs = jointLimits?.lower_limit !== undefined ? radiansToDegrees(jointLimits.lower_limit)