gamepad rate lmiit

This commit is contained in:
legop3
2026-01-28 17:00:19 -05:00
parent c5ae78dcab
commit bf05d07092
3 changed files with 39 additions and 14 deletions
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -11,7 +11,7 @@
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="apple-mobile-web-app-title" content="Multi Roomba Rover" />
<title>Multi Roomba Rover</title>
<script type="module" crossorigin src="/assets/index-cDTlxmMF.js"></script>
<script type="module" crossorigin src="/assets/index-CVxRGxYn.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-BhKFQa9T.css">
</head>
<body>
@@ -13,6 +13,8 @@ import { isTextEntryActive } from './inputFocusUtils.js';
const SOURCE = 'gamepad';
const ZERO_VECTOR = { x: 0, y: 0, boost: false };
const ZERO_AUX = { main: 0, side: 0, vacuum: 0 };
const DRIVE_RATE_MS = 100;
const AUX_RATE_MS = 100;
function areVectorsEqual(a, b) {
return a && b && a.x === b.x && a.y === b.y && a.boost === b.boost;
@@ -22,6 +24,16 @@ function areAuxEqual(a, b) {
return a && b && a.main === b.main && a.side === b.side && a.vacuum === b.vacuum;
}
function vectorMagnitude(vector) {
if (!vector) return 0;
return Math.hypot(vector.x || 0, vector.y || 0);
}
function isAuxIdle(aux) {
if (!aux) return true;
return !aux.main && !aux.side && !aux.vacuum;
}
function pickActivePad(pads, activeSignature) {
if (!pads || pads.length === 0) return null;
if (activeSignature) {
@@ -53,6 +65,8 @@ export default function GamepadInputManager() {
const lastAuxRef = useRef(ZERO_AUX);
const reverseStateRef = useRef({ main: false, side: false });
const buttonStateRef = useRef(new Map());
const lastDriveSentAtRef = useRef(0);
const lastAuxSentAtRef = useRef(0);
const lastServoAtRef = useRef(0);
const lastServoAngleRef = useRef(null);
@@ -151,6 +165,8 @@ export default function GamepadInputManager() {
}
buttonStateRef.current = new Map();
reverseStateRef.current = { main: false, side: false };
lastDriveSentAtRef.current = 0;
lastAuxSentAtRef.current = 0;
registerInputState(SOURCE, { connected: false });
return;
}
@@ -179,8 +195,13 @@ export default function GamepadInputManager() {
const outputs = computeGamepadOutputs(activePad, profile);
if (!areVectorsEqual(outputs.driveVector, lastVectorRef.current)) {
lastVectorRef.current = outputs.driveVector;
setDriveVector(outputs.driveVector, { source: SOURCE });
const now = typeof performance !== 'undefined' ? performance.now() : Date.now();
const idle = vectorMagnitude(outputs.driveVector) < 0.02;
if (idle || now - lastDriveSentAtRef.current >= DRIVE_RATE_MS) {
lastVectorRef.current = outputs.driveVector;
lastDriveSentAtRef.current = now;
setDriveVector(outputs.driveVector, { source: SOURCE });
}
}
const auxSideScale = profile.calibration?.auxSideScale ?? 0.55;
@@ -199,8 +220,12 @@ export default function GamepadInputManager() {
aux = { main: 127, side: 127, vacuum: 127 };
}
if (!areAuxEqual(aux, lastAuxRef.current)) {
lastAuxRef.current = aux;
setAuxMotors(aux);
const now = typeof performance !== 'undefined' ? performance.now() : Date.now();
if (isAuxIdle(aux) || now - lastAuxSentAtRef.current >= AUX_RATE_MS) {
lastAuxRef.current = aux;
lastAuxSentAtRef.current = now;
setAuxMotors(aux);
}
}
if (outputs.buttons.mainReverse && handleButtonEdge('mainReverse', true)) {