mirror of
https://github.com/legop3/MultiRoombaRover.git
synced 2026-09-16 01:21:20 -04:00
gamepad rate lmiit
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -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,9 +195,14 @@ export default function GamepadInputManager() {
|
||||
const outputs = computeGamepadOutputs(activePad, profile);
|
||||
|
||||
if (!areVectorsEqual(outputs.driveVector, lastVectorRef.current)) {
|
||||
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;
|
||||
const mainMagnitude = Math.round(Math.min(Math.abs(outputs.auxAxis.main), 1) * 127);
|
||||
@@ -199,9 +220,13 @@ export default function GamepadInputManager() {
|
||||
aux = { main: 127, side: 127, vacuum: 127 };
|
||||
}
|
||||
if (!areAuxEqual(aux, lastAuxRef.current)) {
|
||||
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)) {
|
||||
reverseStateRef.current.main = !reverseStateRef.current.main;
|
||||
|
||||
Reference in New Issue
Block a user