import fs from 'node:fs' import crypto from 'node:crypto' import assert from 'node:assert/strict' // Offline derivative only. Source methods, frozen versions and devices are untouched. const [sourcePath, outputPath, summaryPath] = process.argv.slice(2) if (!sourcePath || !outputPath) throw new Error('Usage: node build_rz_sweep.mjs [summary.json]') const snapshot = JSON.parse(fs.readFileSync(sourcePath, 'utf8').replace(/^\uFEFF/, '')) const sourceText = snapshot.published?.flowData || snapshot.flowData || JSON.stringify(snapshot) const original = JSON.parse(sourceText), graph = structuredClone(original) const byId = new Map(graph.nodes.map(node => [node.id, node])), order = [] for (let node = graph.nodes.find(node => node.type === 'start'); node;) { assert(!order.includes(node), 'Source main chain has a cycle'); order.push(node) const edges = graph.edges.filter(edge => edge.sourceNodeId === node.id) assert(edges.length <= 1, 'Unexpected main branch') node = edges.length ? byId.get(edges[0].targetNodeId) : null } assert.equal(graph.nodes.length, 698, 'Review another source revision before generating this derivative') assert.equal(order.length, 374) const params = node => node.properties.nodeParams || [] const mode = node => params(node).find(parameter => parameter.name === 'mode')?.input const input = (name, value) => ({ name, type: 'input', input: value, required: true, disabled: true }) const quote = (name, node, ...path) => ({ name, type: 'quote', input: '', quote: [node.id, 'output', ...path], quoteType: 'output', required: true, disabled: true }) const set = (node, name, value) => { const index = params(node).findIndex(parameter => parameter.name === name) if (index < 0) node.properties.nodeParams.push(value); else node.properties.nodeParams[index] = value } const helper = fs.readFileSync(new URL('./rz-sweep-planner.js', import.meta.url), 'utf8') const start = order[0], pathGenerator = order[1] const options = { id: crypto.randomUUID().replaceAll('-', ''), type: 'code', x: 0, y: 0, properties: { name: '扫描加速度(默认80mm/s²)', action: 'CODE', nodeParams: [ { name: 'scan_acceleration_mm', type: 'quote', input: '', quote: [start.id, 'input', 'scan_acceleration_mm'], quoteType: 'input', required: true, disabled: true }, input('code', 'function handler(params){var v=params.scan_acceleration_mm;if(typeof v!=="number"||!isFinite(v)||v<=0)throw new Error("扫描加速度必须为正数");return {scanAcceleration:v/1000};}') ], outputType: 'json', outputParams: [{ name: 'result', type: 'object', required: true, children: [{ name: 'scanAcceleration', type: 'number', required: true, children: [] }] }] } } graph.nodes.push(options); byId.set(options.id, options) const stationIndices = order.flatMap((node, index) => node.properties.action === 'AGV_MOVE_TO_STATION' ? [index] : []) assert.equal(stationIndices.length, 4) const main = [start, options, ...order.slice(1, stationIndices[0])], groups = [] for (let stationIndex = 0; stationIndex < 4; stationIndex++) { const begin = stationIndices[stationIndex], station = order[begin].properties.name const end = stationIndex < 3 ? stationIndices[stationIndex + 1] : order.findIndex((node, index) => index > begin && node.properties.action === 'SCANNER_MESH') const section = order.slice(begin, end), rotations = section.filter(node => mode(node) === 'rx_target') assert.equal(rotations.length, 2) const firstRotationIndex = order.indexOf(rotations[0]), prefix = order.slice(begin, firstRotationIndex) const initialOffset = prefix.find(node => mode(node) === 'offset_initial') const safety = section.at(-1) assert(initialOffset && safety.properties.action === 'ARM_MOVE_TO_J') prefix.forEach(node => { node.properties.name = node.properties.name.replace('准备RX=-1.1', '准备RX=-2') }) main.push(...prefix) let previousOffset = initialOffset, previousRead = prefix.at(-1) for (const rx of [-2, -1.1]) { const rotation = rotations.find(node => params(node).find(parameter => parameter.name === 'rx')?.input === rx) assert(rotation) const rotationIndex = order.indexOf(rotation) const scanIndex = order.findIndex((node, index) => index > rotationIndex && mode(node) === 'rx_scan') const turnIndex = order.findIndex((node, index) => index > rotationIndex && mode(node) === 'j6_target') assert(scanIndex > rotationIndex && turnIndex > scanIndex) const preparation = order.slice(scanIndex - 5, scanIndex), scanBlock = order.slice(scanIndex, scanIndex + 6) const withdrawal = order.slice(scanIndex + 6, turnIndex), scan = order[scanIndex] assert.equal(preparation.length, 5); assert.equal(withdrawal.length, 4) assert.equal(preparation[2].properties.action, 'ARM_GET_ROBOT_STATE') assert.equal(preparation[4].properties.action, 'ARM_GET_ROBOT_STATE') assert.equal(scanBlock.at(-1).properties.action, 'SCANNER_STOP') rotation.properties.nodeParams = [input('mode', 'rx_target'), quote('state', previousRead, 'state'), quote('offset', previousOffset, 'result'), quote('reference', initialOffset, 'result'), input('rx', rx), input('code', helper)] rotation.properties.name = station + ' RX=' + rx + ' 在退让处设置本组原姿态' scan.properties.nodeParams = params(scan).filter(parameter => !['settleRequired', 'settleDeadlineMs'].includes(parameter.name)) set(scan, 'mode', input('mode', 'scan')) const label = station + ' RX=' + rx + ' X/Z运动同步RZ往复120°单遍' for (const node of [...preparation, ...scanBlock]) node.properties.name = node.properties.name.replace('未转J6完整路径', 'RZ摆扫路径').replace('未转J6完整扫描', 'RZ摆扫单遍') scan.properties.name = label withdrawal.forEach(node => { node.properties.name = node.properties.name.replace('J6换向', 'RZ摆扫结束') }) main.push(rotation, order[rotationIndex + 1], ...preparation, ...scanBlock, ...withdrawal) previousOffset = withdrawal[1]; previousRead = withdrawal[3] groups.push({ station, rx, rotation: rotation.id, scan: scan.id, originalReference: initialOffset.id, withdrawal: previousOffset.id }) } main.push(safety) } main.push(...order.slice(order.findIndex(node => node.properties.action === 'SCANNER_MESH'))) const retained = new Set() function visit(node) { if (retained.has(node.id)) return; retained.add(node.id); for (const id of node.children || []) visit(byId.get(id)) } main.forEach(visit) graph.nodes = graph.nodes.filter(node => retained.has(node.id)) const mainIds = new Set(main.map(node => node.id)) graph.edges = graph.edges.filter(edge => retained.has(edge.sourceNodeId) && retained.has(edge.targetNodeId) && !mainIds.has(edge.sourceNodeId) && !mainIds.has(edge.targetNodeId)) graph.edges.push(...main.slice(0, -1).map((node, index) => ({ id: crypto.randomUUID().replaceAll('-', ''), type: 'bezier', sourceNodeId: node.id, targetNodeId: main[index + 1].id, sourceAnchorId: node.id + '_1', targetAnchorId: main[index + 1].id + '_3' }))) start.properties.inputParams.push({ name: 'scan_acceleration_mm', type: 'number', children: [], input: 80, required: true, desc: '扫描加速度(mm/s²),默认80,可输入正数;扫描速度仍长边80、短边100。' }) start.properties.inputParams.find(parameter => parameter.name === 'scan_layers').desc = '每站先RX=-2再RX=-1.1,每个RX完整扫描一遍;X/Z运动中RZ初始→+120°→初始,整片一次往复,不专门转J6或重复扫描。' pathGenerator.properties.name = '保留原两组RX的完整XYZ点集(各一遍,RX=-2优先)' pathGenerator.properties.desc = '原点生成代码不改;保留X/Y/Z范围与分层,近端选起点后沿原直线段同步分配一次RZ往复。' for (const node of graph.nodes) { if (mode(node)) { assert(['offset_initial', 'rx_target', 'scan', 'offset'].includes(mode(node)), 'Dedicated J6 logic must not survive') set(node, 'code', input('code', helper)) } if (node.properties.action === 'ARM_MOVE_TO_POINT' && node.properties.parentId) { const parent = byId.get(node.properties.parentId) if (parent.properties.name.includes('RZ摆扫单遍') && !parent.properties.name.includes('衔接')) set(node, 'acceleration', quote('acceleration', options, 'result', 'scanAcceleration')) } } main.forEach((node, index) => { node.x = index * 320; node.y = 220 }) assert.equal(graph.nodes.filter(node => node.properties.action === 'SCANNER_START').length, 8) assert.equal(graph.nodes.filter(node => node.properties.action === 'SCANNER_STOP').length, 8) assert.equal(graph.nodes.filter(node => node.properties.action === 'ARM_MOVE_TO_J').length, 8) assert.equal(JSON.stringify(original), JSON.stringify(JSON.parse(sourceText))) fs.writeFileSync(outputPath, JSON.stringify(graph, null, 2)) const summary = { nodes: graph.nodes.length, edges: graph.edges.length, groups, captures: 8, rzOffsetsDeg: [0, 120, 0], dedicatedJ6Turns: 0, sourceUntouched: true, deviceCommandsSent: 0 } if (summaryPath) fs.writeFileSync(summaryPath, JSON.stringify(summary, null, 2)) console.log(JSON.stringify({ ...summary, groups: groups.length }))