ReviewOS

also looking at this

stacks/drone

feat: model VTOL electrical consumption and battery authority

#81
Merged chris wants to merge codex/owned-vtol-electrical-energy into main
15 files +605 -28
resources/functions/drone/vehicles/vtol/power.tsadded+37-0
Changes to resources/functions/drone/vehicles/vtol/power.ts
@@ -0,0 +1,37 @@
1import { numericPacket } from './packets'
2import type { VehiclePlantState } from '../types'
3
4/** Fictional electrical law, not identified motor, propeller, cell or thermal behavior. */
5export const vtolElectricalModel = Object.freeze({ auxiliaryW: 12, liftWPerN32: 4, pusherWPerN32: 5, surfaceWAtFullDeflection: 3, fullOpenCircuitV: 24, emptyOpenCircuitV: 20, resistanceOhm: 0.04 })
6
7export function vtolElectricalLoad(rotorInput: readonly number[], pusherN: number, surfaceInput: readonly number[]) {
8 const rotors = numericPacket(rotorInput, 4), surfaces = numericPacket(surfaceInput, 3)
9 if (rotors.some(n => n < 0 || n > 100) || !Number.isFinite(pusherN) || pusherN < 0 || pusherN > 100 || surfaces.some(n => Math.abs(n) > 0.4)) throw new RangeError('Invalid fictional VTOL electrical load')
10 return vtolElectricalModel.auxiliaryW + rotors.reduce((sum, n) => sum + vtolElectricalModel.liftWPerN32 * n ** 1.5, 0) + vtolElectricalModel.pusherWPerN32 * pusherN ** 1.5 + surfaces.reduce((sum, n) => sum + vtolElectricalModel.surfaceWAtFullDeflection * Math.abs(n) / 0.4, 0)
11}
12
13/** Stable low-current solution of P=VI with a declared series resistance. No regeneration. */
14export function vtolElectricalSupply(loadW: number, remainingFraction: number) {
15 if (!Number.isFinite(loadW) || loadW < 0 || !Number.isFinite(remainingFraction) || remainingFraction < 0 || remainingFraction > 1) throw new RangeError('Invalid fictional VTOL electrical supply')
16 const openCircuitV = vtolElectricalModel.emptyOpenCircuitV + (vtolElectricalModel.fullOpenCircuitV - vtolElectricalModel.emptyOpenCircuitV) * remainingFraction
17 const discriminant = openCircuitV ** 2 - 4 * vtolElectricalModel.resistanceOhm * loadW
18 if (discriminant < 0) throw new RangeError('Fictional VTOL electrical demand exceeds supply')
19 const currentA = 2 * loadW / (openCircuitV + Math.sqrt(discriminant))
20 return { currentA, terminalV: openCircuitV - currentA * vtolElectricalModel.resistanceOhm, sourceW: openCircuitV * currentA }
21}
22
23/** All lift rotors, pusher and surfaces together, at the lowest declared source voltage. */
24export function vtolElectricalMaximumW(maxRotorN: number, maxPusherN: number) {
25 if (!Number.isFinite(maxRotorN) || maxRotorN <= 0 || !Number.isFinite(maxPusherN) || maxPusherN <= 0) throw new RangeError('Invalid VTOL electrical envelope')
26 const loadW = vtolElectricalLoad([maxRotorN, maxRotorN, maxRotorN, maxRotorN], maxPusherN, [0.4, 0.4, 0.4])
27 return Math.ceil(vtolElectricalSupply(loadW, 0).sourceW)
28}
29
30/** Sensor synthesis only: energy rounds upward, so quantization cannot invent capacity. */
31export function renderVtolElectricalMeasurement(state: VehiclePlantState): Record<string, number | boolean> {
32 if (!Object.hasOwn(state.details, 'batteryCapacityWh')) return {}
33 const value = state.details
34 const round = (name: string) => Math.round((value[name] as number) * 1000) / 1000
35 return { batteryCapacityWh: value.batteryCapacityWh!, batteryEnergyUsedWh: Math.min(value.batteryCapacityWh as number, Math.ceil((value.batteryEnergyUsedWh as number) * 1000000) / 1000000), batterySourcePowerW: Math.ceil((value.batterySourcePowerW as number) * 1000) / 1000,
36 batteryLoadPowerW: round('batteryLoadPowerW'), batteryCurrentA: round('batteryCurrentA'), batteryTerminalV: round('batteryTerminalV'), batteryCutoff: value.batteryCutoff! }
37}