根据传回来的角速度和线速度 优化显示倒车影像,未测试
This commit is contained in:
@@ -3,6 +3,10 @@ import React, { useEffect, useRef } from 'react';
|
|||||||
type GuideLineProps = {
|
type GuideLineProps = {
|
||||||
direction: 'forward' | 'reverse';
|
direction: 'forward' | 'reverse';
|
||||||
steer: number;
|
steer: number;
|
||||||
|
/** 实时线速度(m/s):前进为正,后退为负。缺省时回退到 steer */
|
||||||
|
linearSpeed?: number;
|
||||||
|
/** 实时角速度(rad/s):左转为正,右转为负。缺省时回退到 steer */
|
||||||
|
angularSpeed?: number;
|
||||||
bottomGap?: number;
|
bottomGap?: number;
|
||||||
topGap?: number;
|
topGap?: number;
|
||||||
visible?: boolean;
|
visible?: boolean;
|
||||||
@@ -19,6 +23,8 @@ type Point = {
|
|||||||
const CarReverseGuideLine: React.FC<GuideLineProps> = ({
|
const CarReverseGuideLine: React.FC<GuideLineProps> = ({
|
||||||
direction,
|
direction,
|
||||||
steer,
|
steer,
|
||||||
|
linearSpeed,
|
||||||
|
angularSpeed,
|
||||||
bottomGap,
|
bottomGap,
|
||||||
topGap,
|
topGap,
|
||||||
visible = true,
|
visible = true,
|
||||||
@@ -28,6 +34,10 @@ const CarReverseGuideLine: React.FC<GuideLineProps> = ({
|
|||||||
const containerRef = useRef<HTMLDivElement>(null);
|
const containerRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
const steerRef = useRef(steer);
|
const steerRef = useRef(steer);
|
||||||
|
const linearSpeedRef = useRef(linearSpeed);
|
||||||
|
const angularSpeedRef = useRef(angularSpeed);
|
||||||
|
// 平滑后的转向量:> 0 右移,< 0 左移,0 为直行
|
||||||
|
const turnRef = useRef(0);
|
||||||
const directionRef = useRef(direction);
|
const directionRef = useRef(direction);
|
||||||
const flashRef = useRef(chevronFlash);
|
const flashRef = useRef(chevronFlash);
|
||||||
const gapRef = useRef({ bottomGap, topGap });
|
const gapRef = useRef({ bottomGap, topGap });
|
||||||
@@ -36,12 +46,16 @@ const CarReverseGuideLine: React.FC<GuideLineProps> = ({
|
|||||||
// 更新参数,不触发 React 组件重新渲染
|
// 更新参数,不触发 React 组件重新渲染
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
steerRef.current = steer;
|
steerRef.current = steer;
|
||||||
|
linearSpeedRef.current = linearSpeed;
|
||||||
|
angularSpeedRef.current = angularSpeed;
|
||||||
directionRef.current = direction;
|
directionRef.current = direction;
|
||||||
flashRef.current = chevronFlash;
|
flashRef.current = chevronFlash;
|
||||||
gapRef.current = { bottomGap, topGap };
|
gapRef.current = { bottomGap, topGap };
|
||||||
visibleRef.current = visible;
|
visibleRef.current = visible;
|
||||||
}, [
|
}, [
|
||||||
steer,
|
steer,
|
||||||
|
linearSpeed,
|
||||||
|
angularSpeed,
|
||||||
direction,
|
direction,
|
||||||
chevronFlash,
|
chevronFlash,
|
||||||
bottomGap,
|
bottomGap,
|
||||||
@@ -87,30 +101,67 @@ const CarReverseGuideLine: React.FC<GuideLineProps> = ({
|
|||||||
resizeObserver.observe(container);
|
resizeObserver.observe(container);
|
||||||
resize();
|
resize();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 计算车道中心线。
|
* 计算当前转向量(-1 左 … 0 直行 … +1 右)。
|
||||||
* steer < 0 左转,steer > 0 右转。
|
*
|
||||||
|
* 优先使用实时状态:曲率 κ = ω / v
|
||||||
|
* linear_speed : 前进为正,后退为负(只取大小参与曲率,方向由视角决定)
|
||||||
|
* angular_speed : 左转为正,右转为负
|
||||||
|
* 曲率 > 0(左转)对应画面左侧,因此转向量取负号;
|
||||||
|
* 静止 / 极低速时不产生行驶轨迹,按直行处理。
|
||||||
|
* 无实时数据时回退到方向盘转角 steer。
|
||||||
*/
|
*/
|
||||||
|
const computeTurn = () => {
|
||||||
|
const v = linearSpeedRef.current;
|
||||||
|
const w = angularSpeedRef.current;
|
||||||
|
|
||||||
|
const hasRealtime =
|
||||||
|
typeof v === 'number' &&
|
||||||
|
Number.isFinite(v) &&
|
||||||
|
typeof w === 'number' &&
|
||||||
|
Number.isFinite(w);
|
||||||
|
|
||||||
|
if (!hasRealtime) {
|
||||||
|
const angle = Math.max(
|
||||||
|
-45,
|
||||||
|
Math.min(45, steerRef.current)
|
||||||
|
);
|
||||||
|
|
||||||
|
// 方向盘转角转换为曲率响应
|
||||||
|
return Math.tan((angle * Math.PI) / 180);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 静止 / 极低速:没有行驶轨迹,保持直线
|
||||||
|
if (Math.abs(v) < 0.03) return 0;
|
||||||
|
|
||||||
|
// 低速时限制分母,避免曲率被放大到满量程
|
||||||
|
const speed = Math.max(Math.abs(v), 0.15);
|
||||||
|
|
||||||
|
// 曲率:ω / v,正值左转
|
||||||
|
const kappa = w / speed;
|
||||||
|
|
||||||
|
// 满量程曲率(约对应 1m 转弯半径)
|
||||||
|
const KAPPA_FULL_SCALE = 1.0;
|
||||||
|
|
||||||
|
const normalized = Math.max(
|
||||||
|
-1,
|
||||||
|
Math.min(1, kappa / KAPPA_FULL_SCALE)
|
||||||
|
);
|
||||||
|
|
||||||
|
// 左转为正 → 画面左侧为负偏移
|
||||||
|
return -normalized;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 计算转向后的车道中心
|
* 计算转向后的车道中心。
|
||||||
*
|
*
|
||||||
* forward:
|
* forward:
|
||||||
* 下方近端相对稳定,上方远端逐渐弯曲
|
* 下方近端相对稳定,上方远端逐渐弯曲
|
||||||
*
|
*
|
||||||
* reverse:
|
* reverse:
|
||||||
* 上方远端相对稳定,下方近端逐渐弯曲
|
* 上方远端相对稳定,下方近端逐渐弯曲
|
||||||
*/
|
*/
|
||||||
const getCenterX = (progress: number) => {
|
const getCenterX = (progress: number) => {
|
||||||
const angle = Math.max(
|
|
||||||
-45,
|
|
||||||
Math.min(45, steerRef.current)
|
|
||||||
);
|
|
||||||
|
|
||||||
// 方向盘转角转换为曲率响应
|
|
||||||
const turn = Math.tan(
|
|
||||||
(angle * Math.PI) / 180
|
|
||||||
);
|
|
||||||
|
|
||||||
// progress: 0 = 顶部远端,1 = 底部近端
|
// progress: 0 = 顶部远端,1 = 底部近端
|
||||||
let curveProgress: number;
|
let curveProgress: number;
|
||||||
|
|
||||||
@@ -123,14 +174,13 @@ const CarReverseGuideLine: React.FC<GuideLineProps> = ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 平滑曲率,避免轨迹弯曲突兀
|
// 平滑曲率,避免轨迹弯曲突兀
|
||||||
const curveFactor =
|
const curveFactor = curveProgress * curveProgress;
|
||||||
curveProgress * curveProgress;
|
|
||||||
|
|
||||||
// 转向最大横向偏移
|
// 转向最大横向偏移
|
||||||
const maxOffset = width * 0.40;
|
const maxOffset = width * 0.40;
|
||||||
|
|
||||||
const offset =
|
const offset =
|
||||||
turn *
|
turnRef.current *
|
||||||
maxOffset *
|
maxOffset *
|
||||||
curveFactor;
|
curveFactor;
|
||||||
|
|
||||||
@@ -388,6 +438,12 @@ const CarReverseGuideLine: React.FC<GuideLineProps> = ({
|
|||||||
lastTime = time;
|
lastTime = time;
|
||||||
animationTime += delta;
|
animationTime += delta;
|
||||||
|
|
||||||
|
// 实时数据抖动较大,按帧平滑转向量,避免辅助线抖动
|
||||||
|
const targetTurn = computeTurn();
|
||||||
|
turnRef.current +=
|
||||||
|
(targetTurn - turnRef.current) *
|
||||||
|
Math.min(1, delta * 8);
|
||||||
|
|
||||||
ctx.clearRect(0, 0, width, height);
|
ctx.clearRect(0, 0, width, height);
|
||||||
|
|
||||||
// 底层车道线
|
// 底层车道线
|
||||||
|
|||||||
@@ -2797,7 +2797,13 @@ export default function DeviceStatusPage() {
|
|||||||
|
|
||||||
<AgrtcRoomRobot CHANNEL={"41-thream03"} />
|
<AgrtcRoomRobot CHANNEL={"41-thream03"} />
|
||||||
|
|
||||||
<CarReverseGuideLine direction="forward" steer={steerXY.x * 45} visible={isVideoPlaying} />
|
<CarReverseGuideLine
|
||||||
|
direction="forward"
|
||||||
|
steer={steerXY.x * 45}
|
||||||
|
linearSpeed={Number(runningStatus?.linear_speed ?? runningStatus?.linearSpeed)}
|
||||||
|
angularSpeed={Number(runningStatus?.angular_speed ?? runningStatus?.angularSpeed)}
|
||||||
|
visible={isVideoPlaying}
|
||||||
|
/>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -2821,7 +2827,15 @@ export default function DeviceStatusPage() {
|
|||||||
|
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{mowerView === 'back' && (<CarReverseGuideLine direction="reverse" steer={steerXY.x * 45} visible={isVideoPlaying} />)}
|
{mowerView === 'back' && (
|
||||||
|
<CarReverseGuideLine
|
||||||
|
direction="reverse"
|
||||||
|
steer={steerXY.x * 45}
|
||||||
|
linearSpeed={Number(runningStatus?.linear_speed ?? runningStatus?.linearSpeed)}
|
||||||
|
angularSpeed={Number(runningStatus?.angular_speed ?? runningStatus?.angularSpeed)}
|
||||||
|
visible={isVideoPlaying}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user