急停 发送ws 遥感控制 避免 raf循环闭包
This commit is contained in:
@@ -191,7 +191,7 @@ class WebSocketManager {
|
|||||||
console.log('WebSocket not connected, cannot send message');
|
console.log('WebSocket not connected, cannot send message');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
console.log('WebSocket send message');
|
console.log('WebSocket send message', JSON.stringify(data));
|
||||||
this.ws.send(JSON.stringify(data));
|
this.ws.send(JSON.stringify(data));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ import envConfig from '../../../env';
|
|||||||
import utils from '@/lib/utils.ts';
|
import utils from '@/lib/utils.ts';
|
||||||
import { useUnitFormatter } from '@/lib/units';
|
import { useUnitFormatter } from '@/lib/units';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
|
import { sendEmergencyStop } from './emergencyStopControl';
|
||||||
|
|
||||||
const { Content } = Layout;
|
const { Content } = Layout;
|
||||||
const { Text, Title } = Typography;
|
const { Text, Title } = Typography;
|
||||||
@@ -543,7 +544,11 @@ export default function DeviceOverviewPage() {
|
|||||||
const handlePause = () => message.info(t('devices.taskPaused'));
|
const handlePause = () => message.info(t('devices.taskPaused'));
|
||||||
const handleReturn = () => message.info(t('devices.fixed2ReturnHomeFixed2CommandIssuedFixed2'));
|
const handleReturn = () => message.info(t('devices.fixed2ReturnHomeFixed2CommandIssuedFixed2'));
|
||||||
const handleRecharge = () => message.info(t('devices.commandIssuedFixed2'));
|
const handleRecharge = () => message.info(t('devices.commandIssuedFixed2'));
|
||||||
const handleEmergencyStop = () => message.error(t('devices.s73'));
|
// 急停:点击时直接下发 button7(绕过控制权限校验),并提示已触发
|
||||||
|
const handleEmergencyStop = () => {
|
||||||
|
sendEmergencyStop(userInfo);
|
||||||
|
message.error(t('devices.s73'));
|
||||||
|
};
|
||||||
|
|
||||||
const fix2 = (val) => {
|
const fix2 = (val) => {
|
||||||
if (val === null || val === undefined || isNaN(val)) return '-';
|
if (val === null || val === undefined || isNaN(val)) return '-';
|
||||||
|
|||||||
@@ -671,6 +671,7 @@ export default function DeviceStatusPage() {
|
|||||||
}, [device, uavState, currentDroneData]);
|
}, [device, uavState, currentDroneData]);
|
||||||
|
|
||||||
const handleGamepadUpdate = useCallback((state) => {
|
const handleGamepadUpdate = useCallback((state) => {
|
||||||
|
console.log('handleGamepadUpdate called with state:', isControlled);
|
||||||
// 无控制权限时:不发送消息,且只发起一次权限申请(避免手柄/方向盘高频更新刷屏)
|
// 无控制权限时:不发送消息,且只发起一次权限申请(避免手柄/方向盘高频更新刷屏)
|
||||||
if (!isControlled) {
|
if (!isControlled) {
|
||||||
if (!hasRequestedControlRef.current && !connecting) {
|
if (!hasRequestedControlRef.current && !connecting) {
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
import React, { useState } from 'react';
|
import React, { useState } from 'react';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
|
import { useSelector } from 'react-redux';
|
||||||
|
import { RootState } from '@/store';
|
||||||
|
import { sendEmergencyStop } from './emergencyStopControl';
|
||||||
import {
|
import {
|
||||||
ShieldAlert,
|
ShieldAlert,
|
||||||
Zap,
|
Zap,
|
||||||
@@ -48,6 +51,7 @@ const RobotFeatureButtons: React.FC<RobotFeatureButtonsProps> = ({
|
|||||||
className = '',
|
className = '',
|
||||||
}) => {
|
}) => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
const { userInfo } = useSelector((state: RootState) => state.user);
|
||||||
const [active, setActive] = useState<Set<RobotFeature>>(new Set(activeFeatures));
|
const [active, setActive] = useState<Set<RobotFeature>>(new Set(activeFeatures));
|
||||||
|
|
||||||
const handleClick = (feature: RobotFeature) => {
|
const handleClick = (feature: RobotFeature) => {
|
||||||
@@ -56,6 +60,8 @@ const RobotFeatureButtons: React.FC<RobotFeatureButtonsProps> = ({
|
|||||||
if (next) nextSet.add(feature);
|
if (next) nextSet.add(feature);
|
||||||
else nextSet.delete(feature);
|
else nextSet.delete(feature);
|
||||||
setActive(nextSet);
|
setActive(nextSet);
|
||||||
|
// 急停:点击时直接下发 button7(绕过控制权限校验)
|
||||||
|
if (feature === 'emergencyStop') sendEmergencyStop(userInfo);
|
||||||
onToggle?.(feature, next);
|
onToggle?.(feature, next);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,9 @@ const XboxController = ({ onGamepadUpdate }) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const requestRef = useRef<number>(null);
|
const requestRef = useRef<number>(null);
|
||||||
|
// 用 ref 保存最新的回调,避免 RAF 循环闭包捕获旧 props 导致权限判断失效
|
||||||
|
const onGamepadUpdateRef = useRef(onGamepadUpdate);
|
||||||
|
onGamepadUpdateRef.current = onGamepadUpdate;
|
||||||
const DEAD_ZONE = 0.08;
|
const DEAD_ZONE = 0.08;
|
||||||
|
|
||||||
const normalizeAxis = (v: number) =>
|
const normalizeAxis = (v: number) =>
|
||||||
@@ -46,7 +49,7 @@ const XboxController = ({ onGamepadUpdate }) => {
|
|||||||
now - lastSendRef.current > SEND_INTERVAL
|
now - lastSendRef.current > SEND_INTERVAL
|
||||||
// payload !== lastPayloadRef.current
|
// payload !== lastPayloadRef.current
|
||||||
) {
|
) {
|
||||||
onGamepadUpdate(state); // ✅ 只有这里才发
|
onGamepadUpdateRef.current(state); // ✅ 只有这里才发
|
||||||
lastSendRef.current = now;
|
lastSendRef.current = now;
|
||||||
lastPayloadRef.current = payload;
|
lastPayloadRef.current = payload;
|
||||||
}
|
}
|
||||||
|
|||||||
28
src/components/devices/emergencyStopControl.ts
Normal file
28
src/components/devices/emergencyStopControl.ts
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import { wsManager } from '../WebSocketManager';
|
||||||
|
|
||||||
|
// 急停对应手柄 button7(buttons 数组下标 7)
|
||||||
|
const EMERGENCY_STOP_BUTTON_INDEX = 7;
|
||||||
|
|
||||||
|
// 构建单帧遥控数据:仅急停位按下/松开,其余按键与摇杆轴保持空闲
|
||||||
|
const buildFrame = (pressed: boolean) => {
|
||||||
|
const buttons = new Array(17).fill(false);
|
||||||
|
buttons[EMERGENCY_STOP_BUTTON_INDEX] = pressed;
|
||||||
|
return { buttons, axes: [0, 0, 0, 0], connected: true, ts: Date.now() };
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 单击急停:绕过控制权限校验,直接经 WebSocket 下发 button7;
|
||||||
|
* 约 300ms 后自动补发一帧复位数据,模拟按下再松开。
|
||||||
|
*/
|
||||||
|
export const sendEmergencyStop = (userInfo?: any) => {
|
||||||
|
const send = (pressed: boolean) =>
|
||||||
|
wsManager.send({
|
||||||
|
type: 'remoteControl',
|
||||||
|
remoteData: buildFrame(pressed),
|
||||||
|
token: userInfo?.token,
|
||||||
|
userName: userInfo?.username,
|
||||||
|
});
|
||||||
|
|
||||||
|
send(true);
|
||||||
|
setTimeout(() => send(false), 300);
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user