also looking at this
feat: add shared-clock scoped vehicle simulation sessions
#98
42 files
+2764
-127
| @@ -0,0 +1,58 @@ | ||
| 1 | import type { SensorVector } from '../sensors/types' | |
| 2 | import type { VehicleMissionPlan } from '../vehicles/types' | |
| 3 | import type { CargoDeliveryDeclaration } from './coordinator' | |
| 4 | import { captureCargoDeliveryDeclaration } from './coordinator' | |
| 5 | import { validateVehicleMission } from '../missions/vehicle' | |
| 6 | ||
| 7 | export interface CargoDeliveryConfiguration { | |
| 8 | version: 1 | |
| 9 | id: string | |
| 10 | delivery: CargoDeliveryDeclaration | |
| 11 | load: { massKg: number, forwardM: number } | |
| 12 | alternateM: SensorVector | |
| 13 | power: VehicleMissionPlan['power'] | |
| 14 | } | |
| 15 | function record(value: unknown, fields: string[]) { | |
| 16 | if (!value || typeof value !== 'object' || Object.getPrototypeOf(value) !== Object.prototype || Reflect.ownKeys(value).length !== fields.length) throw new TypeError('cargo-delivery-configuration') | |
| 17 | const result: Record<string, unknown> = {} | |
| 18 | for (const field of fields) { | |
| 19 | const descriptor = Object.getOwnPropertyDescriptor(value, field) | |
| 20 | if (!descriptor?.enumerable || !Object.hasOwn(descriptor, 'value')) throw new TypeError('cargo-delivery-configuration') | |
| 21 | result[field] = descriptor.value | |
| 22 | } | |
| 23 | return result | |
| 24 | } | |
| 25 | ||
| 26 | /** Explicit modeled route admission, before allocating a plant or transport. */ | |
| 27 | export function planCargoDelivery(input: unknown) { | |
| 28 | const top = record(input, ['version', 'id', 'delivery', 'load', 'alternateM', 'power']) | |
| 29 | const delivery = captureCargoDeliveryDeclaration(top.delivery), load = record(top.load, ['massKg', 'forwardM']), power = record(top.power, ['availableWh', 'maxW', 'reservePercent']) | |
| 30 | if (top.version !== 1 || typeof top.id !== 'string' || !/^[a-z][a-z0-9-]{0,39}$/.test(top.id) | |
| 31 | || delivery.origin.latitudeDeg !== 0 || delivery.origin.longitudeDeg !== 0 || delivery.origin.ellipsoidHeightM !== 100 || delivery.site.positionM[2] !== 0 || delivery.site.containerId.length > 40 | |
| 32 | || typeof load.massKg !== 'number' || !Number.isFinite(load.massKg) || load.massKg <= 0 || load.massKg > .25 | |
| 33 | || typeof load.forwardM !== 'number' || !Number.isFinite(load.forwardM) || load.forwardM < -.1 || load.forwardM > .1 | |
| 34 | || !Object.values(power).every(n => typeof n === 'number' && Number.isFinite(n)) || (power.maxW as number) < 600 | |
| 35 | || !Array.isArray(top.alternateM) || top.alternateM.length !== 3 || Reflect.ownKeys(top.alternateM).length !== 4) throw new TypeError('cargo-delivery-configuration') | |
| 36 | const alternateM = [0, 1, 2].map(i => { | |
| 37 | const descriptor = Object.getOwnPropertyDescriptor(top.alternateM, String(i)) | |
| 38 | if (!descriptor?.enumerable || !Object.hasOwn(descriptor, 'value') || typeof descriptor.value !== 'number' || !Number.isFinite(descriptor.value)) throw new TypeError('cargo-delivery-configuration') | |
| 39 | return descriptor.value | |
| 40 | }) as SensorVector | |
| 41 | if (alternateM[2] !== 0) throw new TypeError('cargo-delivery-configuration') | |
| 42 | const configuration: CargoDeliveryConfiguration = { version: 1, id: top.id, delivery, load: { massKg: load.massKg, forwardM: load.forwardM }, alternateM, power: power as unknown as VehicleMissionPlan['power'] } | |
| 43 | const height = delivery.releaseHeightM, site = delivery.site.positionM | |
| 44 | const spatial = { acceptanceM: .18, maxArrivalSpeedMps: .15, settleMs: 300, timeoutMs: 20000 } | |
| 45 | const proposed: VehicleMissionPlan = { version: 2, vehicle: 'quadcopter', id: configuration.id, frame: 'LOCAL_NED', altitude: 'ORIGIN_RELATIVE_DOWN', terrain: 'DECLARED_MODEL_SURFACE', | |
| 46 | origin: delivery.origin, fence: { radiusM: 12, minDownM: -5, maxDownM: 0 }, rallyM: [0, 0, -height], power: configuration.power, start: 'start', tasks: [ | |
| 47 | { id: 'start', kind: 'start', next: 'depart' }, | |
| 48 | { id: 'depart', kind: 'takeoff', positionM: [0, 0, -height], ...spatial, next: 'site' }, | |
| 49 | { id: 'site', kind: 'waypoint', positionM: [site[0], site[1], -height], ...spatial, settleMs: 800, next: 'release' }, | |
| 50 | { id: 'release', kind: 'payload', operation: 'material-drop', containerId: delivery.site.containerId, timeoutMs: 1500, next: 'home', onFailure: 'alternate-approach' }, | |
| 51 | { id: 'home', kind: 'return', ...spatial, next: 'land' }, | |
| 52 | { id: 'land', kind: 'land', positionM: [0, 0, 0], ...spatial }, | |
| 53 | { id: 'alternate-approach', kind: 'waypoint', positionM: [alternateM[0], alternateM[1], -height], ...spatial, next: 'alternate-land' }, | |
| 54 | { id: 'alternate-land', kind: 'land', positionM: [...alternateM], ...spatial }, | |
| 55 | ] } | |
| 56 | const { plan, worstCaseMs, requiredWh } = validateVehicleMission(proposed) | |
| 57 | return structuredClone({ evidence: 'modeled-cargo-delivery-plan' as const, physicalFlightEnabled: false as const, identityAuthenticated: false as const, configuration, mission: plan, budget: { worstCaseMs, requiredWh } }) | |
| 58 | } | |