ptz control updates

This commit is contained in:
legop3
2026-07-17 23:06:12 -04:00
parent 4e6e9e3021
commit afe8ffc62e
14 changed files with 522 additions and 328 deletions
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -78,7 +78,7 @@
<script defer src="https://analytics.otter.land/script.js" data-website-id="82dd56a5-db44-4279-bd1e-a4d9fee39af7" data-domains="rover.otter.land"></script> <script defer src="https://analytics.otter.land/script.js" data-website-id="82dd56a5-db44-4279-bd1e-a4d9fee39af7" data-domains="rover.otter.land"></script>
<script defer src="https://analytics.otter.land/recorder.js" data-website-id="82dd56a5-db44-4279-bd1e-a4d9fee39af7" data-domains="rover.otter.land" data-sample-rate="0.15" data-mask-level="moderate" data-max-duration="300000"></script> <script defer src="https://analytics.otter.land/recorder.js" data-website-id="82dd56a5-db44-4279-bd1e-a4d9fee39af7" data-domains="rover.otter.land" data-sample-rate="0.15" data-mask-level="moderate" data-max-duration="300000"></script>
<title>Roomba Rover</title> <title>Roomba Rover</title>
<script type="module" crossorigin src="/assets/index-DC2T_UQE.js"></script> <script type="module" crossorigin src="/assets/index-CVzYrQ9e.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-BHMHSpML.css"> <link rel="stylesheet" crossorigin href="/assets/index-BHMHSpML.css">
</head> </head>
<body> <body>
+144 -30
View File
@@ -29,6 +29,13 @@ const PTZ_STREAM_PATH = 'ptz-camera';
const DEFAULT_ONVIF_PORT = 8000; const DEFAULT_ONVIF_PORT = 8000;
const DEFAULT_PROFILE_TOKEN = '003'; const DEFAULT_PROFILE_TOKEN = '003';
const DEFAULT_TURN_DURATION_MS = 5 * 60 * 1000; const DEFAULT_TURN_DURATION_MS = 5 * 60 * 1000;
// The TrackMix reports PT1S as its minimum supported continuous-move timeout.
// Browser heartbeats arrive every 250 ms, so 650 ms allows ordinary LAN jitter
// while still issuing an explicit stop well before the camera's own one-second
// timeout becomes the final safety backstop.
const MOTION_WATCHDOG_MS = 650;
const ONVIF_MOTION_TIMEOUT_MS = 1000;
const STOP_MOTION = Object.freeze({ pan: 0, tilt: 0, zoom: 0 });
// PTZ is a normal replay source now, so capture should be on unless the feature // PTZ is a normal replay source now, so capture should be on unless the feature
// explicitly disables replay for the camera. // explicitly disables replay for the camera.
const DEFAULT_REPLAY_ENABLED = true; const DEFAULT_REPLAY_ENABLED = true;
@@ -92,6 +99,11 @@ let publisherStderrSyncTimer = null;
let snapshotTimer = null; let snapshotTimer = null;
let spotlightVerifyTimer = null; let spotlightVerifyTimer = null;
let vendorStatePromise = Promise.resolve(); let vendorStatePromise = Promise.resolve();
let motionWatchdogTimer = null;
let desiredMotion = STOP_MOTION;
let desiredMotionVersion = 0;
let appliedMotionVersion = 0;
let motionCommandPromise = null;
let lastSnapshotState = null; let lastSnapshotState = null;
const snapshotSubscribers = new Map(); const snapshotSubscribers = new Map();
const socketSnapshotSubscriptions = new Map(); const socketSnapshotSubscriptions = new Map();
@@ -924,7 +936,10 @@ function revokeOperator(reason = 'release') {
state.deadline = null; state.deadline = null;
clearTurnTimer(); clearTurnTimer();
videoSessions.revokeWhere((info) => info.socketId === previous && info.sourceType === 'ptz'); videoSessions.revokeWhere((info) => info.socketId === previous && info.sourceType === 'ptz');
callOnvif('stop', { profileToken: state.profileToken, panTilt: true, zoom: true }).catch(() => {}); // Operator handoff/disconnect must enter the same serialized stream as
// movement. A raw concurrent Stop could otherwise finish before an older
// ContinuousMove and allow that stale move to restart the camera afterward.
forceMotionStop(`operator-${reason}`);
events.emit('operator', { socketId: previous, action: 'release', reason }); events.emit('operator', { socketId: previous, action: 'release', reason });
} }
@@ -1089,26 +1104,126 @@ function normalizePresetCreateName(rawName) {
return name; return name;
} }
async function move(socket, payload = {}) { function normalizeMotionIntent(payload = {}) {
requireOperator(socket); return {
await initialize(); pan: clampUnit(payload.pan ?? payload.x),
const x = clampUnit(payload.pan ?? payload.x); tilt: clampUnit(payload.tilt ?? payload.y),
const y = clampUnit(payload.tilt ?? payload.y); zoom: clampUnit(payload.zoom),
const zoom = clampUnit(payload.zoom); };
await callOnvif('continuousMove', {
profileToken: state.profileToken,
x,
y,
zoom,
timeout: 1000,
});
return { ok: true };
} }
async function stop(socket) { function isMotionIdle(motion = STOP_MOTION) {
return !motion.pan && !motion.tilt && !motion.zoom;
}
function clearMotionWatchdog() {
if (!motionWatchdogTimer) return;
clearTimeout(motionWatchdogTimer);
motionWatchdogTimer = null;
}
function runMotionCommandPump() {
if (motionCommandPromise) return motionCommandPromise;
/*
ONVIF requests are asynchronous HTTP/SOAP operations. Starting one request
per Socket.IO event allowed a quick move/stop/move sequence to overlap at
the camera, where response order is not a safe proxy for execution order.
This single pump permits exactly one camera operation at a time. If browser
heartbeats arrive while it is busy, only the newest complete desired state
survives the loop, so intermediate input noise is coalesced rather than
replayed after the operator has already released the controls.
*/
motionCommandPromise = (async () => {
while (appliedMotionVersion < desiredMotionVersion) {
const commandVersion = desiredMotionVersion;
const commandMotion = desiredMotion;
try {
await initialize();
if (isMotionIdle(commandMotion)) {
await callOnvif('stop', {
profileToken: state.profileToken,
panTilt: true,
zoom: true,
});
} else {
/*
Send the complete vector even when only one axis changed. The live
TrackMix accepts combined pan/tilt/zoom despite failing to advertise
its continuous zoom space, and zero on an axis is how the newest
intent releases that axis without disturbing a non-zero sibling.
*/
await callOnvif('continuousMove', {
profileToken: state.profileToken,
x: commandMotion.pan,
y: commandMotion.tilt,
zoom: commandMotion.zoom,
timeout: ONVIF_MOTION_TIMEOUT_MS,
});
}
} catch (err) {
/*
Mark this version consumed below instead of spinning on a failing
camera. A held control supplies another heartbeat and therefore a
bounded retry; a failed stop still has the camera's one-second ONVIF
timeout as its independent final safety mechanism.
*/
logger.warn('PTZ motion command failed', {
error: getErrorMessage(err),
idle: isMotionIdle(commandMotion),
version: commandVersion,
});
}
appliedMotionVersion = commandVersion;
}
})().finally(() => {
motionCommandPromise = null;
// An intent can arrive after the loop condition but before this promise's
// finally callback. Recheck the versions so that narrow timing window does
// not strand the newest state without a command pump.
if (appliedMotionVersion < desiredMotionVersion) runMotionCommandPump();
});
return motionCommandPromise;
}
function queueMotionIntent(motion, reason = 'input') {
desiredMotion = normalizeMotionIntent(motion);
desiredMotionVersion += 1;
clearMotionWatchdog();
if (!isMotionIdle(desiredMotion)) {
/*
Socket disconnect normally arrives quickly, but it is not a suitable motor
safety boundary. Every non-zero browser heartbeat replaces this timer; if
releases or subsequent heartbeats disappear, the server injects a zero
intent into the same serialized stream as ordinary control changes.
*/
motionWatchdogTimer = setTimeout(() => {
motionWatchdogTimer = null;
queueMotionIntent(STOP_MOTION, 'watchdog');
}, MOTION_WATCHDOG_MS);
}
const pending = runMotionCommandPump();
pending.catch(() => {});
return { ok: true, motion: desiredMotion, reason };
}
function acceptMotionIntent(socket, payload = {}) {
requireOperator(socket); requireOperator(socket);
await callOnvif('stop', { profileToken: state.profileToken, panTilt: true, zoom: true }); return queueMotionIntent(payload, 'operator-input');
return { ok: true }; }
function forceMotionStop(reason = 'safety-stop') {
/*
Lifecycle stops intentionally increment the version even when local state is
already zero. The browser may have lost its final packet, or the camera may
have accepted a command whose response has not returned, so deduplicating a
safety stop would trust precisely the state we are trying to recover from.
*/
queueMotionIntent(STOP_MOTION, reason);
return motionCommandPromise || Promise.resolve();
} }
async function getStatus(socket) { async function getStatus(socket) {
@@ -1133,9 +1248,10 @@ async function gotoPreset(socket, payload = {}) {
Stop any continuous move before jumping to a preset. Without this, a held Stop any continuous move before jumping to a preset. Without this, a held
key or touch control can keep sending pan/tilt velocity while the camera is key or touch control can keep sending pan/tilt velocity while the camera is
trying to execute the absolute preset move, which makes the final position trying to execute the absolute preset move, which makes the final position
feel inconsistent. feel inconsistent. Await the serialized safety stop instead of issuing a
raw concurrent ONVIF request that could itself race an older movement.
*/ */
await callOnvif('stop', { profileToken: state.profileToken, panTilt: true, zoom: true }).catch(() => {}); await forceMotionStop('preset').catch(() => {});
await callOnvif('gotoPreset', { await callOnvif('gotoPreset', {
profileToken: state.profileToken, profileToken: state.profileToken,
/* /*
@@ -1475,18 +1591,16 @@ function registerSocketHandlers() {
cb({ error: err.message }); cb({ error: err.message });
} }
}); });
socket.on('ptzCamera:move', async (firstArg, secondArg) => { socket.on('ptzCamera:motion', (firstArg, secondArg) => {
const { payload, cb } = normalizeSocketArgs(firstArg, secondArg); const { payload, cb } = normalizeSocketArgs(firstArg, secondArg);
try { try {
cb(await move(socket, payload)); /*
} catch (err) { Acknowledge acceptance of the newest desired state immediately. The
cb({ error: err.message }); serialized ONVIF pump deliberately runs independently of Socket.IO
} request latency so browser heartbeats cannot accumulate while waiting
}); for a camera SOAP response.
socket.on('ptzCamera:stop', async (firstArg, secondArg) => { */
const { cb } = normalizeSocketArgs(firstArg, secondArg); cb(acceptMotionIntent(socket, payload));
try {
cb(await stop(socket));
} catch (err) { } catch (err) {
cb({ error: err.message }); cb({ error: err.message });
} }
@@ -148,13 +148,25 @@ export default function ControlPadPanel({ compact = false, disabled = false }) {
return () => { return () => {
clearRepeatTimer(); clearRepeatTimer();
/* /*
Mobile controls can unmount when layouts change or the driver leaves the Mobile controls can unmount during an orientation/layout change while a
control surface. Clear the shared flag so a stale mobile precision choice pointer is still captured by the disappearing element. Publish a neutral
cannot leave desktop/keyboard camera tilt in fine-step mode. vector directly during cleanup so neither rover drive nor PTZ pan/tilt
can retain the last cell merely because pointerup had nowhere to land.
*/ */
activeCellRef.current = null;
setDriveVector({ x: 0, y: 0, boost: false }, { source: SOURCE });
registerInputState(SOURCE, {
keys: [],
vector: { x: 0, y: 0, boost: false },
activeCell: 'stop',
speedMode: speedModeRef.current,
lastEvent: 'unmount',
});
// Clear the shared flag too, so a stale mobile precision choice cannot
// leave desktop/keyboard camera tilt in fine-step mode.
setCameraPrecisionMode(false); setCameraPrecisionMode(false);
}; };
}, [clearRepeatTimer, setCameraPrecisionMode]); }, [clearRepeatTimer, registerInputState, setCameraPrecisionMode, setDriveVector]);
useEffect(() => { useEffect(() => {
if (!disabled) return; if (!disabled) return;
+18 -38
View File
@@ -188,46 +188,30 @@ function PtzLightingControls({ ptz, disabled = false }) {
} }
function PtzMobileZoomButtons({ disabled = false }) { function PtzMobileZoomButtons({ disabled = false }) {
const { nudgeServo } = useControlActions(); const { setCameraAxisIntent } = useControlActions();
const repeatTimerRef = useRef(null);
const stopZoom = useCallback(() => { const stopZoom = useCallback(() => {
/* /*
Mobile zoom is intentionally routed through the normal camera-up/down Zero only releases the zoom axis. The PTZ adapter combines it with any
control action instead of emitting PTZ socket commands directly. That pan/tilt direction still held on the movement pad, so lifting one finger
keeps the zoom buttons on the same path as keyboard/gamepad camera tilt, cannot erase the other finger's intent.
and the PTZ adapter remains the one place that translates "camera nudge"
into Reolink zoom pulses.
*/ */
if (repeatTimerRef.current) { setCameraAxisIntent(0);
clearInterval(repeatTimerRef.current); }, [setCameraAxisIntent]);
repeatTimerRef.current = null;
}
/*
Zero is a zoom-only release signal in the PTZ adapter. Using the global
stop action here previously erased a simultaneously held pan/tilt vector,
making mixed touch controls unexpectedly stop the camera.
*/
nudgeServo(0);
}, [nudgeServo]);
const startZoom = useCallback( const startZoom = useCallback(
(direction) => (event) => { (direction) => (event) => {
/* /*
Send an immediate nudge and then repeat while held. The adapter turns Publish held state once. The adapter owns the single motion heartbeat,
each nudge into a short zoom pulse, so repeating the standard action is so this button no longer creates a second interval whose queued callback
the simplest way to get continuous hold-to-zoom without adding another could run after pointerup and restart zoom.
PTZ-specific command loop.
*/ */
event.preventDefault(); event.preventDefault();
if (disabled) return; if (disabled) return;
stopZoom(); event.currentTarget.setPointerCapture?.(event.pointerId);
nudgeServo(direction); setCameraAxisIntent(direction);
repeatTimerRef.current = setInterval(() => {
nudgeServo(direction);
}, 120);
}, },
[disabled, nudgeServo, stopZoom], [disabled, setCameraAxisIntent],
); );
const stopFromPointer = useCallback( const stopFromPointer = useCallback(
(event) => { (event) => {
@@ -242,16 +226,12 @@ function PtzMobileZoomButtons({ disabled = false }) {
() => () => { () => () => {
/* /*
A touch surface can unmount during orientation changes or fullscreen A touch surface can unmount during orientation changes or fullscreen
close while a pointer is still down. Clear the repeat timer here so a close while a pointer is still down. Explicitly clear zoom here because
held zoom button cannot keep firing camera-up/down actions after the an unmounted DOM node cannot deliver its pointerup/pointercancel event.
mobile controls have disappeared.
*/ */
if (repeatTimerRef.current) { setCameraAxisIntent(0);
clearInterval(repeatTimerRef.current);
repeatTimerRef.current = null;
}
}, },
[], [setCameraAxisIntent],
); );
return ( return (
@@ -263,7 +243,7 @@ function PtzMobileZoomButtons({ disabled = false }) {
onPointerDown={startZoom(-1)} onPointerDown={startZoom(-1)}
onPointerUp={stopFromPointer} onPointerUp={stopFromPointer}
onPointerCancel={stopFromPointer} onPointerCancel={stopFromPointer}
onPointerLeave={stopFromPointer} onLostPointerCapture={stopFromPointer}
onContextMenu={(event) => event.preventDefault()} onContextMenu={(event) => event.preventDefault()}
> >
Zoom out Zoom out
@@ -275,7 +255,7 @@ function PtzMobileZoomButtons({ disabled = false }) {
onPointerDown={startZoom(1)} onPointerDown={startZoom(1)}
onPointerUp={stopFromPointer} onPointerUp={stopFromPointer}
onPointerCancel={stopFromPointer} onPointerCancel={stopFromPointer}
onPointerLeave={stopFromPointer} onLostPointerCapture={stopFromPointer}
onContextMenu={(event) => event.preventDefault()} onContextMenu={(event) => event.preventDefault()}
> >
Zoom in Zoom in
+24 -13
View File
@@ -1,7 +1,7 @@
// Vip PTZ Camera Card // Vip PTZ Camera Card
// Purpose: Provides the verified-user entry point and fullscreen controller for the single Reolink PTZ camera. // Purpose: Provides the verified-user entry point and fullscreen controller for the single Reolink PTZ camera.
// Scope: Owns PTZ UI state only; server-side PTZ ownership, rover handoff, and command authorization remain authoritative. // Scope: Owns PTZ UI state only; server-side PTZ ownership, rover handoff, and command authorization remain authoritative.
import { useCallback, useMemo, useState } from 'react'; import { useCallback, useEffect, useMemo, useState } from 'react';
import { createPortal } from 'react-dom'; import { createPortal } from 'react-dom';
import ChatPanel from '../ChatPanel/index.jsx'; import ChatPanel from '../ChatPanel/index.jsx';
import CardFrame from '../CardFrame/index.jsx'; import CardFrame from '../CardFrame/index.jsx';
@@ -11,13 +11,12 @@ import PtzLiveVideo from '../PtzLiveVideo/index.jsx';
import ReplaySourcesPanel from '../ReplaySourcesPanel/index.jsx'; import ReplaySourcesPanel from '../ReplaySourcesPanel/index.jsx';
import KeyPill from './VipAudioUploadCard/KeyPill.jsx'; import KeyPill from './VipAudioUploadCard/KeyPill.jsx';
import { useSessionActions, useSessionSelector } from '../../context/SessionContext.jsx'; import { useSessionActions, useSessionSelector } from '../../context/SessionContext.jsx';
import { useControlSelector } from '../../controls/index.js'; import { useControlActions, useControlSelector } from '../../controls/index.js';
import { formatKeyLabel } from '../../controls/keymapUtils.js'; import { formatKeyLabel } from '../../controls/keymapUtils.js';
import { usePtzCameraSnapshots } from '../../hooks/usePtzCameraSnapshot.js'; import { usePtzCameraSnapshots } from '../../hooks/usePtzCameraSnapshot.js';
import { isFeatureEnabled } from '../../lib/features.js'; import { isFeatureEnabled } from '../../lib/features.js';
const PTZ_CAMERA_ID = 'ptz-camera'; const PTZ_CAMERA_ID = 'ptz-camera';
const PTZ_ZOOM_SPEED = 0.55;
function formatRemaining(deadline) { function formatRemaining(deadline) {
const remaining = Math.max(0, Math.ceil((Number(deadline || 0) - Date.now()) / 1000)); const remaining = Math.max(0, Math.ceil((Number(deadline || 0) - Date.now()) / 1000));
@@ -218,22 +217,25 @@ function PtzLightingControls({ ptz, disabled = false }) {
} }
function PtzMobileZoomButtons({ disabled = false }) { function PtzMobileZoomButtons({ disabled = false }) {
const { ptzMove, ptzStop } = useSessionActions(); const { setCameraAxisIntent } = useControlActions();
const stopZoom = useCallback(() => { const stopZoom = useCallback(() => {
ptzStop().catch(() => {}); // This is a zoom-only release; the shared adapter retains any simultaneous
}, [ptzStop]); // pan/tilt intent from the movement pad in its next combined motion state.
setCameraAxisIntent(0);
}, [setCameraAxisIntent]);
const startZoom = useCallback( const startZoom = useCallback(
(direction) => (event) => { (direction) => (event) => {
/* /*
Mobile needs explicit zoom targets because the regular mobile drive pad The adapter owns renewal for held PTZ state. Publishing the direction
is already used for pan/tilt. Desktop does not render these buttons; it once avoids a component-local repeat timer and keeps this legacy card on
uses the mapped camera up/down controls shown in the reference panel. the exact same motion path as the dedicated PTZ route.
*/ */
event.preventDefault(); event.preventDefault();
if (disabled) return; if (disabled) return;
ptzMove({ pan: 0, tilt: 0, zoom: direction * PTZ_ZOOM_SPEED }).catch(() => {}); event.currentTarget.setPointerCapture?.(event.pointerId);
setCameraAxisIntent(direction);
}, },
[disabled, ptzMove], [disabled, setCameraAxisIntent],
); );
const stopFromPointer = useCallback( const stopFromPointer = useCallback(
(event) => { (event) => {
@@ -244,6 +246,15 @@ function PtzMobileZoomButtons({ disabled = false }) {
[disabled, stopZoom], [disabled, stopZoom],
); );
useEffect(
() => () => {
// Orientation changes can unmount the button before pointerup. Clear the
// held zoom state explicitly instead of waiting for the server watchdog.
setCameraAxisIntent(0);
},
[setCameraAxisIntent],
);
return ( return (
<div className="mobile-touch-control grid grid-cols-2 gap-0.5 text-sm"> <div className="mobile-touch-control grid grid-cols-2 gap-0.5 text-sm">
<button <button
@@ -253,7 +264,7 @@ function PtzMobileZoomButtons({ disabled = false }) {
onPointerDown={startZoom(-1)} onPointerDown={startZoom(-1)}
onPointerUp={stopFromPointer} onPointerUp={stopFromPointer}
onPointerCancel={stopFromPointer} onPointerCancel={stopFromPointer}
onPointerLeave={stopFromPointer} onLostPointerCapture={stopFromPointer}
onContextMenu={(event) => event.preventDefault()} onContextMenu={(event) => event.preventDefault()}
> >
Zoom out Zoom out
@@ -265,7 +276,7 @@ function PtzMobileZoomButtons({ disabled = false }) {
onPointerDown={startZoom(1)} onPointerDown={startZoom(1)}
onPointerUp={stopFromPointer} onPointerUp={stopFromPointer}
onPointerCancel={stopFromPointer} onPointerCancel={stopFromPointer}
onPointerLeave={stopFromPointer} onLostPointerCapture={stopFromPointer}
onContextMenu={(event) => event.preventDefault()} onContextMenu={(event) => event.preventDefault()}
> >
Zoom in Zoom in
-2
View File
@@ -426,8 +426,6 @@ export function SessionProvider({ children }) {
emitWithAck('session:privateSafety:set', { roverId, safety }), emitWithAck('session:privateSafety:set', { roverId, safety }),
ptzClaim: () => emitWithAck('ptzCamera:claim'), ptzClaim: () => emitWithAck('ptzCamera:claim'),
ptzRelease: () => emitWithAck('ptzCamera:release'), ptzRelease: () => emitWithAck('ptzCamera:release'),
ptzMove: (payload = {}) => emitWithAck('ptzCamera:move', payload),
ptzStop: () => emitWithAck('ptzCamera:stop'),
ptzSpotlight: (payload = {}) => emitWithAck('ptzCamera:spotlight', payload), ptzSpotlight: (payload = {}) => emitWithAck('ptzCamera:spotlight', payload),
ptzIr: (payload = {}) => emitWithAck('ptzCamera:ir', payload), ptzIr: (payload = {}) => emitWithAck('ptzCamera:ir', payload),
ptzListPresets: () => emitWithAck('ptzCamera:presets:list'), ptzListPresets: () => emitWithAck('ptzCamera:presets:list'),
+42 -16
View File
@@ -45,6 +45,7 @@ const CONTROL_ACTION_NAMES = [
'setAuxMotors', 'setAuxMotors',
'setServoAngle', 'setServoAngle',
'nudgeServo', 'nudgeServo',
'setCameraAxisIntent',
'goServoHome', 'goServoHome',
'setCameraPrecisionMode', 'setCameraPrecisionMode',
'runMacro', 'runMacro',
@@ -352,21 +353,12 @@ export function ControlSystemProvider({ children }) {
const setServoAngle = useCallback( const setServoAngle = useCallback(
(value, options = {}) => { (value, options = {}) => {
if (ptzControls.isActive) {
/* /*
Servo-capable rover controls converge here from keyboard, gamepad, Absolute servo positions belong only to rover hardware. PTZ zoom now
desktop, and mobile. When the active control target is the PTZ camera, enters through setCameraAxisIntent as a signed held velocity, so this
route the intent through the PTZ adapter instead of making the rover function must not infer zoom direction by comparing unrelated absolute
command pipeline understand camera zoom semantics. angle values from gamepad/manual-dock callers.
*/ */
const baseline = typeof servoAngleRef.current === 'number' ? servoAngleRef.current : 0;
const numeric = Number(value);
if (!Number.isFinite(numeric)) return;
ptzControls.pulseZoom(numeric - baseline);
servoAngleRef.current = numeric;
recordControlIntent();
return;
}
if (!pipeline.servoConfig) return; if (!pipeline.servoConfig) return;
const force = Boolean(options?.force); const force = Boolean(options?.force);
if (state.manualDockAssist?.active && !force) return; if (state.manualDockAssist?.active && !force) return;
@@ -376,13 +368,24 @@ export function ControlSystemProvider({ children }) {
servoAngleRef.current = clamped; servoAngleRef.current = clamped;
recordControlIntent(); recordControlIntent();
}, },
[pipeline, ptzControls, recordControlIntent, state.manualDockAssist?.active], [pipeline, recordControlIntent, state.manualDockAssist?.active],
); );
const nudgeServo = useCallback( const nudgeServo = useCallback(
(delta = 0) => { (delta = 0) => {
if (ptzControls.isActive) {
/*
A PTZ camera has no absolute browser-side servo angle. Treat a nudge
as held zoom direction and, importantly, preserve zero as an explicit
release. The previous fallback converted nudgeServo(0) into a positive
default step, so releasing the mobile zoom button could zoom in again.
*/
ptzControls.setZoomIntent(delta);
recordControlIntent();
return;
}
const config = pipeline.servoConfig; const config = pipeline.servoConfig;
if (!config && !ptzControls.isActive) return; if (!config) return;
const step = typeof delta === 'number' && delta !== 0 ? delta : config?.nudgeDegrees || 1; const step = typeof delta === 'number' && delta !== 0 ? delta : config?.nudgeDegrees || 1;
const baseline = const baseline =
typeof servoAngleRef.current === 'number' typeof servoAngleRef.current === 'number'
@@ -392,7 +395,28 @@ export function ControlSystemProvider({ children }) {
: 0; : 0;
setServoAngle(baseline + step); setServoAngle(baseline + step);
}, },
[pipeline.servoConfig, ptzControls.isActive, setServoAngle], [pipeline.servoConfig, ptzControls, recordControlIntent, setServoAngle],
);
const setCameraAxisIntent = useCallback(
(direction = 0) => {
/*
Keyboard, touch, and gamepad all need an explicit way to say that a
camera axis returned to neutral. Rover servos remain position/nudge
based, so returning false tells those callers to continue through their
existing rover implementation without introducing PTZ rules there.
*/
if (!ptzControls.isActive) return false;
ptzControls.setZoomIntent(direction);
/*
Do not dispatch recordControlIntent here. Gamepads publish their neutral
and held axes every animation frame; the PTZ adapter deduplicates state
and owns its 250 ms heartbeat, so a React reducer update per frame would
add churn without representing a new user action.
*/
return true;
},
[ptzControls],
); );
const goServoHome = useCallback(() => { const goServoHome = useCallback(() => {
@@ -691,6 +715,7 @@ export function ControlSystemProvider({ children }) {
setAuxMotors, setAuxMotors,
setServoAngle, setServoAngle,
nudgeServo, nudgeServo,
setCameraAxisIntent,
goServoHome, goServoHome,
setCameraPrecisionMode, setCameraPrecisionMode,
runMacro, runMacro,
@@ -718,6 +743,7 @@ export function ControlSystemProvider({ children }) {
setAuxMotors, setAuxMotors,
setServoAngle, setServoAngle,
nudgeServo, nudgeServo,
setCameraAxisIntent,
goServoHome, goServoHome,
setCameraPrecisionMode, setCameraPrecisionMode,
runMacro, runMacro,
@@ -53,6 +53,7 @@ export default function GamepadInputManager() {
setDriveVector, setDriveVector,
setAuxMotors, setAuxMotors,
setServoAngle, setServoAngle,
setCameraAxisIntent,
runMacro, runMacro,
toggleHeadlight, toggleHeadlight,
toggleLaser, toggleLaser,
@@ -173,6 +174,7 @@ export default function GamepadInputManager() {
runMacro, runMacro,
saveGamepadSettings, saveGamepadSettings,
setAuxMotors, setAuxMotors,
setCameraAxisIntent,
setDriveVector, setDriveVector,
setMode, setMode,
setServoAngle, setServoAngle,
@@ -187,6 +189,9 @@ export default function GamepadInputManager() {
if (!latest) return; if (!latest) return;
const activePad = pickActivePad(hubState.pads, latest.activeSignature); const activePad = pickActivePad(hubState.pads, latest.activeSignature);
if (!activePad) { if (!activePad) {
// A disconnected controller cannot deliver a final neutral axis sample.
// Publish it here so PTZ zoom never depends on the browser doing so.
latest.setCameraAxisIntent(0);
if (!areVectorsEqual(lastVectorRef.current, ZERO_VECTOR)) { if (!areVectorsEqual(lastVectorRef.current, ZERO_VECTOR)) {
lastVectorRef.current = ZERO_VECTOR; lastVectorRef.current = ZERO_VECTOR;
latest.setDriveVector(ZERO_VECTOR, { source: SOURCE }); latest.setDriveVector(ZERO_VECTOR, { source: SOURCE });
@@ -204,6 +209,9 @@ export default function GamepadInputManager() {
} }
if (isTextEntryActive()) { if (isTextEntryActive()) {
// Entering text blocks gamepad control immediately, including a held
// camera axis that otherwise would keep its last PTZ zoom direction.
latest.setCameraAxisIntent(0);
if (!areVectorsEqual(lastVectorRef.current, ZERO_VECTOR)) { if (!areVectorsEqual(lastVectorRef.current, ZERO_VECTOR)) {
lastVectorRef.current = ZERO_VECTOR; lastVectorRef.current = ZERO_VECTOR;
latest.setDriveVector(ZERO_VECTOR, { source: SOURCE }); latest.setDriveVector(ZERO_VECTOR, { source: SOURCE });
@@ -302,7 +310,14 @@ export default function GamepadInputManager() {
handleButtonEdge('laserToggle', false); handleButtonEdge('laserToggle', false);
} }
if (Math.abs(outputs.cameraAxis) > 0.001) { /*
PTZ zoom consumes the live signed gamepad axis, including its zero
position, so releasing the stick is an explicit stop instead of merely
ending calls to the old servo updater. Rover camera servos return false
here and continue through their established absolute/velocity mapping.
*/
const handledAsPtzZoom = latest.setCameraAxisIntent(outputs.cameraAxis);
if (!handledAsPtzZoom && Math.abs(outputs.cameraAxis) > 0.001) {
handleCameraAxis(outputs.cameraAxis, profile.calibration); handleCameraAxis(outputs.cameraAxis, profile.calibration);
} }
@@ -90,6 +90,7 @@ export default function KeyboardInputManager() {
setDriveVector, setDriveVector,
setAuxMotors, setAuxMotors,
nudgeServo, nudgeServo,
setCameraAxisIntent,
runMacro, runMacro,
stopAllMotion, stopAllMotion,
registerInputState, registerInputState,
@@ -215,6 +216,13 @@ export default function KeyboardInputManager() {
const ensureServoLoop = useCallback(() => { const ensureServoLoop = useCallback(() => {
const direction = computeServoDirection(); const direction = computeServoDirection();
if (direction === 0) { if (direction === 0) {
/*
Keyup must publish a real neutral PTZ zoom intent before the repeat loop
disappears. Rover servos ignore this generic axis release and retain
their existing nudge behavior because setCameraAxisIntent returns false
whenever PTZ is not the active target.
*/
latestRef.current?.setCameraAxisIntent(0);
stopServoLoop(); stopServoLoop();
return; return;
} }
@@ -235,7 +243,9 @@ export default function KeyboardInputManager() {
const tokensSnapshot = new Set(activeTokensRef.current); const tokensSnapshot = new Set(activeTokensRef.current);
const precisionActive = isPrecisionDriveActive(tokensSnapshot, latest.keymap); const precisionActive = isPrecisionDriveActive(tokensSnapshot, latest.keymap);
const servoStep = precisionActive ? PRECISION_SERVO_NUDGE_DEGREES : latest.servoStep; const servoStep = precisionActive ? PRECISION_SERVO_NUDGE_DEGREES : latest.servoStep;
if (!latest.setCameraAxisIntent(nextDirection)) {
latest.nudgeServo(nextDirection * servoStep); latest.nudgeServo(nextDirection * servoStep);
}
servoIntervalRef.current = setTimeout(tick, latest.servoRepeatMs); servoIntervalRef.current = setTimeout(tick, latest.servoRepeatMs);
}; };
servoIntervalRef.current = setTimeout(tick, 0); servoIntervalRef.current = setTimeout(tick, 0);
@@ -394,6 +404,7 @@ export default function KeyboardInputManager() {
servoRepeatMs, servoRepeatMs,
servoStep, servoStep,
setAuxMotors, setAuxMotors,
setCameraAxisIntent,
setCameraPrecisionMode, setCameraPrecisionMode,
setDriveVector, setDriveVector,
setMicPttActive, setMicPttActive,
+98 -71
View File
@@ -12,7 +12,11 @@ const PTZ_SPEEDS = {
medium: 0.5, medium: 0.5,
fast: 1, fast: 1,
}; };
const ZOOM_PULSE_MS = 220; // The TrackMix advertises a minimum ONVIF movement timeout of one second. A
// quarter-second browser heartbeat gives the server several opportunities to
// renew a genuinely held intent while still letting its watchdog distinguish a
// live control from a browser that disappeared without delivering a release.
const MOTION_HEARTBEAT_MS = 250;
function clampUnit(value) { function clampUnit(value) {
const number = Number(value) || 0; const number = Number(value) || 0;
@@ -102,9 +106,10 @@ export function usePtzControlAdapter() {
const ptz = useSessionSelector((state) => state.session?.ptzCamera || null); const ptz = useSessionSelector((state) => state.session?.ptzCamera || null);
const isActive = Boolean(ptz?.isOperator); const isActive = Boolean(ptz?.isOperator);
const lastMotionSignatureRef = useRef(payloadSignature(PTZ_STOP)); const lastMotionSignatureRef = useRef(payloadSignature(PTZ_STOP));
const desiredMotionRef = useRef(PTZ_STOP);
const panTiltIntentRef = useRef({ pan: 0, tilt: 0 }); const panTiltIntentRef = useRef({ pan: 0, tilt: 0 });
const zoomIntentRef = useRef(0); const zoomIntentRef = useRef(0);
const zoomStopTimerRef = useRef(null); const heartbeatTimerRef = useRef(null);
const emitPtz = useCallback( const emitPtz = useCallback(
(eventName, payload = {}) => { (eventName, payload = {}) => {
@@ -114,22 +119,13 @@ export function usePtzControlAdapter() {
[isActive, socket], [isActive, socket],
); );
const stopMotion = useCallback(() => { const clearHeartbeat = useCallback(() => {
if (zoomStopTimerRef.current) { if (!heartbeatTimerRef.current) return;
clearTimeout(zoomStopTimerRef.current); clearInterval(heartbeatTimerRef.current);
zoomStopTimerRef.current = null; heartbeatTimerRef.current = null;
} }, []);
// A true global stop is used for blur, route close, and control release, so
// it deliberately clears every independently tracked PTZ axis intent.
panTiltIntentRef.current = { pan: 0, tilt: 0 };
zoomIntentRef.current = 0;
const stopSignature = payloadSignature(PTZ_STOP);
if (lastMotionSignatureRef.current === stopSignature) return;
lastMotionSignatureRef.current = stopSignature;
emitPtz('ptzCamera:stop');
}, [emitPtz]);
const sendMotion = useCallback( const publishMotion = useCallback(
(payload, options = {}) => { (payload, options = {}) => {
if (!isActive) return false; if (!isActive) return false;
const nextPayload = { const nextPayload = {
@@ -138,16 +134,49 @@ export function usePtzControlAdapter() {
zoom: clampUnit(payload?.zoom), zoom: clampUnit(payload?.zoom),
}; };
const nextSignature = payloadSignature(nextPayload); const nextSignature = payloadSignature(nextPayload);
desiredMotionRef.current = nextPayload;
if (isIdlePayload(nextPayload)) {
clearHeartbeat();
} else if (!heartbeatTimerRef.current) {
/*
Movement is renewed from the complete desired vector, not from the
individual input event that happened to start it. This is what makes
pan/tilt and zoom independent: every heartbeat describes all axes as
they should be now, and no delayed zoom pulse can resurrect an older
direction after a release.
*/
heartbeatTimerRef.current = setInterval(() => {
const current = desiredMotionRef.current;
if (isIdlePayload(current)) {
clearHeartbeat();
return;
}
emitPtz('ptzCamera:motion', current);
}, MOTION_HEARTBEAT_MS);
}
if (!options.force && lastMotionSignatureRef.current === nextSignature) return true; if (!options.force && lastMotionSignatureRef.current === nextSignature) return true;
lastMotionSignatureRef.current = nextSignature; lastMotionSignatureRef.current = nextSignature;
if (isIdlePayload(nextPayload)) { // Zero is a first-class desired state. The server translates the complete
emitPtz('ptzCamera:stop'); // idle vector into ONVIF Stop inside the same serialized command stream as
} else { // movement, which prevents separate move/stop handlers from racing.
emitPtz('ptzCamera:move', nextPayload); emitPtz('ptzCamera:motion', nextPayload);
}
return true; return true;
}, },
[emitPtz, isActive], [clearHeartbeat, emitPtz, isActive],
);
const stopMotion = useCallback(
(options = {}) => {
// A global stop deliberately clears every axis. Safety/lifecycle callers
// use force so the server receives a fresh stop even when the browser's
// local signature already says it is idle after a dropped connection.
panTiltIntentRef.current = { pan: 0, tilt: 0 };
zoomIntentRef.current = 0;
return publishMotion(PTZ_STOP, { force: Boolean(options.force) });
},
[publishMotion],
); );
const applyDriveVector = useCallback( const applyDriveVector = useCallback(
@@ -163,60 +192,36 @@ export function usePtzControlAdapter() {
Preserve the current zoom intent when a direction update arrives so a Preserve the current zoom intent when a direction update arrives so a
keyboard or touch event on one axis cannot erase another held axis. keyboard or touch event on one axis cannot erase another held axis.
*/ */
sendMotion({ publishMotion({
...panTiltIntentRef.current, ...panTiltIntentRef.current,
zoom: zoomIntentRef.current, zoom: zoomIntentRef.current,
}); });
return true; return true;
}, },
[isActive, sendMotion], [isActive, publishMotion],
); );
const pulseZoom = useCallback( const setZoomIntent = useCallback(
(direction) => { (direction) => {
if (!isActive) return false; if (!isActive) return false;
const sign = axisSign(direction); const numeric = clampUnit(direction);
if (!sign) { const sign = axisSign(numeric);
if (zoomStopTimerRef.current) {
clearTimeout(zoomStopTimerRef.current);
zoomStopTimerRef.current = null;
}
zoomIntentRef.current = 0;
/* /*
Releasing zoom must not call the global PTZ stop. Re-emit the retained PTZ zoom is a held velocity, not a rover servo nudge. Convert the input
pan/tilt intent with zoom cleared so a held direction continues magnitude to the same precision/normal tiers used for pan and tilt, then
immediately instead of waiting for another directional key event. retain it until the input surface explicitly publishes zero. The shared
heartbeat renews that state; there are no per-button repeat or delayed
stop timers left to race with pointer/key release.
*/ */
sendMotion({ ...panTiltIntentRef.current, zoom: 0 }, { force: true }); const speed = !sign ? 0 : Math.abs(numeric) <= 0.45 ? PTZ_SPEEDS.slow : PTZ_SPEEDS.medium;
return true; zoomIntentRef.current = sign * speed;
} publishMotion({
/*
Zoom is different from pan/tilt because it is driven by repeated nudge
events from existing camera controls. Force each pulse through even when
the payload is identical, otherwise holding "camera up" only sends the
first zoom command and every later nudge is de-duped away.
*/
zoomIntentRef.current = sign * PTZ_SPEEDS.medium;
sendMotion({
...panTiltIntentRef.current, ...panTiltIntentRef.current,
zoom: zoomIntentRef.current, zoom: zoomIntentRef.current,
}, { force: true }); });
if (zoomStopTimerRef.current) clearTimeout(zoomStopTimerRef.current);
/*
Existing rover camera controls are nudge/slider based, not hold-based.
Treat each nudge as a short PTZ zoom pulse, then stop from the adapter
so delayed stop behavior is owned by the camera layer only.
*/
zoomStopTimerRef.current = setTimeout(() => {
zoomStopTimerRef.current = null;
zoomIntentRef.current = 0;
// A zoom pulse ending restores, rather than stops, any direction that
// is still held in the independent pan/tilt intent.
sendMotion({ ...panTiltIntentRef.current, zoom: 0 }, { force: true });
}, ZOOM_PULSE_MS);
return true; return true;
}, },
[isActive, sendMotion], [isActive, publishMotion],
); );
const setSpotlight = useCallback( const setSpotlight = useCallback(
@@ -253,20 +258,42 @@ export function usePtzControlAdapter() {
useEffect(() => { useEffect(() => {
if (isActive) return undefined; if (isActive) return undefined;
lastMotionSignatureRef.current = payloadSignature(PTZ_STOP); lastMotionSignatureRef.current = payloadSignature(PTZ_STOP);
desiredMotionRef.current = PTZ_STOP;
panTiltIntentRef.current = { pan: 0, tilt: 0 }; panTiltIntentRef.current = { pan: 0, tilt: 0 };
zoomIntentRef.current = 0; zoomIntentRef.current = 0;
if (zoomStopTimerRef.current) { clearHeartbeat();
clearTimeout(zoomStopTimerRef.current);
zoomStopTimerRef.current = null;
}
return undefined; return undefined;
}, [isActive]); }, [clearHeartbeat, isActive]);
useEffect(() => {
if (!isActive) return undefined;
const forceSafetyStop = () => stopMotion({ force: true });
const handleVisibility = () => {
if (document.visibilityState === 'hidden') forceSafetyStop();
};
/*
Input components handle ordinary pointer/key releases, but the adapter is
the only layer guaranteed to see every PTZ control surface. Centralizing
browser lifecycle stops here covers touch, keyboard, and gamepad equally
when a tab hides, a window blurs, or mobile navigation fires pagehide.
*/
window.addEventListener('blur', forceSafetyStop);
window.addEventListener('pagehide', forceSafetyStop);
document.addEventListener('visibilitychange', handleVisibility);
return () => {
window.removeEventListener('blur', forceSafetyStop);
window.removeEventListener('pagehide', forceSafetyStop);
document.removeEventListener('visibilitychange', handleVisibility);
};
}, [isActive, stopMotion]);
useEffect( useEffect(
() => () => { () => () => {
if (zoomStopTimerRef.current) clearTimeout(zoomStopTimerRef.current); clearHeartbeat();
}, },
[], [clearHeartbeat],
); );
return useMemo( return useMemo(
@@ -274,11 +301,11 @@ export function usePtzControlAdapter() {
isActive, isActive,
state: ptz, state: ptz,
applyDriveVector, applyDriveVector,
pulseZoom, setZoomIntent,
setSpotlight, setSpotlight,
setIr, setIr,
stopMotion, stopMotion,
}), }),
[applyDriveVector, isActive, ptz, pulseZoom, setIr, setSpotlight, stopMotion], [applyDriveVector, isActive, ptz, setIr, setSpotlight, setZoomIntent, stopMotion],
); );
} }