From d1e4d64edefa3db42df710088d6da2155779adf6 Mon Sep 17 00:00:00 2001 From: tk Date: Sat, 12 Oct 2024 13:50:32 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E7=B1=BB=20=20=E5=9E=8B=E3=80=91?= =?UTF-8?q?=EF=BC=9Afactor=20=E7=94=B5=E9=87=8F=E6=98=BE=E7=A4=BA=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=20=E7=94=B5=E9=87=8F=E6=BB=A4=E6=B3=A2=20=E3=80=90?= =?UTF-8?q?=E5=8E=9F=20=20=E5=9B=A0=E3=80=91=EF=BC=9A=E8=BF=94=E8=88=AA?= =?UTF-8?q?=E7=82=B9ui=E5=9B=BE=E6=A0=87=E4=B9=B1=E8=B7=B3=E4=B8=8D?= =?UTF-8?q?=E7=A8=B3=E5=AE=9A=EF=BC=8C=E7=BB=99=E7=94=B5=E6=B5=81=E6=BB=A4?= =?UTF-8?q?=E6=B3=A2=20=E3=80=90=E8=BF=87=20=20=E7=A8=8B=E3=80=91=EF=BC=9A?= =?UTF-8?q?=E5=8F=96=E5=89=8D7=E4=B8=AA=E7=94=B5=E9=87=8F=E5=80=BC=20?= =?UTF-8?q?=E5=8E=BB=E6=8E=89=E4=B8=80=E4=B8=AA=E6=9C=80=E9=AB=98=20?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E6=9C=80=E4=BD=8E=20=E5=89=A9=E4=B8=8B?= =?UTF-8?q?=E5=B9=B3=E5=9D=87=E5=87=BA=E4=B8=80=E4=B8=AA=E5=80=BC=20?= =?UTF-8?q?=E3=80=90=E5=BD=B1=20=20=E5=93=8D=E3=80=91=EF=BC=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/BatteryStatus.vue | 38 ++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/src/components/BatteryStatus.vue b/src/components/BatteryStatus.vue index 8f86056..88d73d7 100644 --- a/src/components/BatteryStatus.vue +++ b/src/components/BatteryStatus.vue @@ -29,11 +29,19 @@ export default { name: 'BatteryStatus', data () { return { + batteryValues: [], // 存储最近的 7 个电量值 + filteredBattery: 0, // 滤波后的电量值 showBattery: true, // 用于控制显示电量或续航时间 interval: null // 用于存储定时器 ID } }, computed: { + batteryValue () { + if (this.plane && this.plane.planeState && this.plane.planeState.currentBattery) { + return this.plane.planeState.currentBattery + } + return 0 + }, statusColor () { // 获取剩余电量百分比和返航电量百分比 const remaining = this.batteryRemaining @@ -66,14 +74,14 @@ export default { }, // 剩余飞行时间 (分钟) endurance () { - if (this.plane && this.plane.planeState && this.plane.planeState.currentBattery > 2) { - return Math.floor(this.batteryRemainingPower / 1000 / this.plane.planeState.currentBattery * 60) + if (this.filteredBattery > 2) { + return Math.floor(this.batteryRemainingPower / 1000 / this.filteredBattery * 60) } return 0 }, // 控制 Tooltip 显示的条件 showTooltip () { - return Number(this.batteryRemainingPower) !== 0 || Number(this.endurance) !== 0 + return Number(this.batteryRemainingPower) > 0 || Number(this.endurance) > 0 }, // 剩余电量 (百分比) batteryRemaining () { @@ -103,7 +111,7 @@ export default { if (rtlTime <= 0) { return 0 } - const currentBattery = this.plane.planeState.currentBattery // 电流 (安培) + const currentBattery = this.filteredBattery // 滤波之后 电流 (安培) const batteryCapacity = this.plane.planeState.battCapacity // 电池总容量 (mAh) // 计算返航所需电量(mAh),假设电流与电量是线性关系 const rtlPowerRequired = rtlTime * currentBattery / 3.6 // 返航所需电量 (mAh) @@ -167,6 +175,28 @@ export default { } }, watch: { + // 电量更新时 进行滤波 + batteryValue (val) { + // 添加新值到数组 + this.batteryValues.push(Number(val)) + // 数组长度超出 7,删除最早的元素,保证数组不超过 7 元素 + if (this.batteryValues.length > 7) { + this.batteryValues.shift() + } + // 如果数组长度超过 3,执行滤波计算 + if (this.batteryValues.length >= 3) { + // 复制并排序数组 + const sortedValues = [...this.batteryValues].sort((a, b) => a - b) + // 去掉最高值和最低值 + sortedValues.pop() + sortedValues.shift() + // 计算平均值 + const average = sortedValues.reduce((acc, val) => acc + val, 0) / sortedValues.length + this.filteredBattery = average.toFixed(2) + } else { + this.filteredBattery = val + } + }, batteryRemainingPower () { this.checkDisplayConditions() },