从原J6交错优化版V8复制新方案,原方案及历史任务保留。 每组RX先按原方向仅J6负90度完整扫描,再恢复本组J6和原RPY复扫原XYZ点集。 扫描速度保持长边80、短边100毫米每秒;扫描加速度默认80可调,非采集入场150毫米每秒。 保留200毫米退让、连续停稳、真实关节限位和原位跳过检查。 新增独立维护生成器、规划源码和中文发布说明;独立方案V1已发布,平台无需重启。 离线图、引用、原点集与边界检查通过;未下发现场设备动作。
164 lines
13 KiB
JavaScript
164 lines
13 KiB
JavaScript
import fs from 'node:fs'
|
||
import crypto from 'node:crypto'
|
||
import assert from 'node:assert/strict'
|
||
|
||
// Offline derivative. Never edits, publishes or executes its source flow.
|
||
const [sourcePath, outputPath, summaryPath] = process.argv.slice(2)
|
||
if (!sourcePath || !outputPath) throw new Error('Usage: node build_j6_first.mjs <source-snapshot.json> <flow.json> [summary.json]')
|
||
const snapshot = JSON.parse(fs.readFileSync(sourcePath, 'utf8').replace(/^\uFEFF/, ''))
|
||
const original = JSON.parse(snapshot.published?.flowData || snapshot.flowData || JSON.stringify(snapshot))
|
||
const graph = structuredClone(original), byId = new Map(graph.nodes.map(node => [node.id, node]))
|
||
const 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 source layout before generating another revision')
|
||
assert.equal(order.length, 374)
|
||
const uid = () => crypto.randomUUID().replaceAll('-', '')
|
||
const params = node => node.properties.nodeParams || []
|
||
const mode = node => params(node).find(field => field.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 || node, 'output', ...path], quoteType: 'output', required: true, disabled: true })
|
||
const field = (name, type, children = []) => ({ name, type, children, required: 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 add = node => { graph.nodes.push(node); byId.set(node.id, node); return node }
|
||
const newEdges = []
|
||
function closure(roots) {
|
||
const nodes = new Set()
|
||
function visit(node) { if (nodes.has(node)) return; nodes.add(node); for (const id of node.children || []) visit(byId.get(id)) }
|
||
roots.forEach(visit); return [...nodes]
|
||
}
|
||
function cloneBlock(roots) {
|
||
const nodes = closure(roots), ids = new Map(nodes.map(node => [node.id, uid()]))
|
||
const replace = value => {
|
||
let text = JSON.stringify(value)
|
||
for (const [before, after] of ids) text = text.replaceAll(before, after)
|
||
return JSON.parse(text)
|
||
}
|
||
for (const node of nodes) add(replace(node))
|
||
for (const edge of graph.edges.filter(edge => ids.has(edge.sourceNodeId) && ids.has(edge.targetNodeId))) newEdges.push({ ...replace(edge), id: uid() })
|
||
return roots.map(node => byId.get(ids.get(node.id)))
|
||
}
|
||
function settle(preparation, label) {
|
||
assert.equal(preparation.length, 5)
|
||
assert.deepEqual(preparation.map(node => node.type), ['code', 'loop', 'serviceNode', 'sleep', 'serviceNode'])
|
||
const copy = cloneBlock(preparation)
|
||
copy.forEach((node, index) => { node.properties.name = label + [' 开始连续停稳', ' 连续停稳门(120秒)', ' 首帧', ' 回读间隔(250ms)', ' 第二帧'][index] })
|
||
return { nodes: copy, first: copy[2], last: copy[4] }
|
||
}
|
||
function codeCopy(template, name, operation, parameters) {
|
||
const node = cloneBlock([template])[0]
|
||
node.properties.name = name
|
||
node.properties.nodeParams = [input('mode', operation), ...parameters, params(template).find(field => field.name === 'code')]
|
||
return node
|
||
}
|
||
const helper = fs.readFileSync(new URL('./j6-first-planner.js', import.meta.url), 'utf8')
|
||
const plannerTemplate = graph.nodes.find(node => mode(node) === 'offset_after_restore')
|
||
assert(plannerTemplate)
|
||
const poseFields = ['x', 'y', 'z', 'rx', 'ry', 'rz', 'velocity'].map(name => field(name, 'number'))
|
||
const options = add({ id: uid(), type: 'code', x: 0, y: 0, properties: {
|
||
name: '扫描加速度(默认80mm/s²;扫描速度仍80/100)', action: 'CODE', nodeParams: [
|
||
{ name: 'scan_acceleration_mm', type: 'quote', input: '', quote: [order[0].id, 'input', 'scan_acceleration_mm'], quoteType: 'input', required: true, disabled: true },
|
||
input('code', 'function handler(params) { var value=params.scan_acceleration_mm; if(typeof value!=="number"||!isFinite(value)||value<=0)throw new Error("扫描加速度必须为正数"); return {scanAcceleration:value/1000}; }')
|
||
], outputType: 'json', outputParams: [field('result', 'object', [field('scanAcceleration', 'number')])]
|
||
} })
|
||
// A new run reads the controller's real limits; no hard-coded ranges or angle wrapping.
|
||
const info = add({ id: uid(), type: 'serviceNode', x: 0, y: 0, properties: {
|
||
name: '读取本次六轴真实关节限位', action: 'ARM_GET_ARM_INFO', nodeType: 'EDGE',
|
||
executionTarget: structuredClone(order.find(node => node.properties.action === 'ARM_MOVE_TO_J').properties.executionTarget),
|
||
nodeParams: [input('timeoutMs', 120000)], outputType: 'json', outputParams: [field('jointLimits', 'array<object>', [field('jointName', 'string'), field('positionLimitsAvailable', 'boolean'), field('lower', 'number'), field('upper', 'number')])]
|
||
} })
|
||
const groups = [], replacements = new Map()
|
||
for (const [groupIndex, rotation] of order.filter(node => mode(node) === 'rx_target').entries()) {
|
||
const start = order.indexOf(rotation), baseIndex = order.findIndex((node, index) => index > start && mode(node) === 'rx_scan')
|
||
const turnIndex = order.findIndex((node, index) => index > start && mode(node) === 'j6_target')
|
||
const turnedIndex = order.findIndex((node, index) => index > start && mode(node) === 'j6_scan')
|
||
const restoreIndex = order.findIndex((node, index) => index > start && mode(node) === 'j6_restore')
|
||
const verifyIndex = order.findIndex((node, index) => index > restoreIndex && mode(node) === 'verify_restore')
|
||
const nextRotation = order.findIndex((node, index) => index > start && mode(node) === 'rx_target')
|
||
const last = groupIndex % 2 === 0 ? nextRotation - 1 : verifyIndex
|
||
const label = rotation.properties.name.split(' 在退让处')[0]
|
||
const base = order[baseIndex], turn = order[turnIndex], turned = order[turnedIndex], restore = order[restoreIndex], verify = order[verifyIndex]
|
||
const firstPreparation = order.slice(baseIndex - 5, baseIndex)
|
||
const baseCapture = order.slice(baseIndex, baseIndex + 6), turnWithdrawal = order.slice(baseIndex + 6, turnIndex)
|
||
const turnedPreparation = order.slice(turnedIndex - 5, turnedIndex), turnedCapture = order.slice(turnedIndex, turnedIndex + 6)
|
||
const restoreWithdrawal = order.slice(turnedIndex + 6, restoreIndex)
|
||
assert.equal(turnWithdrawal.length, 4); assert.equal(restoreWithdrawal.length, 4)
|
||
assert.equal(baseCapture.at(-1).properties.action, 'SCANNER_STOP'); assert.equal(turnedCapture.at(-1).properties.action, 'SCANNER_STOP')
|
||
const first = settle(firstPreparation, label + ' 首遍J6换向前')
|
||
set(turn, 'mode', input('mode', 'j6_first'))
|
||
turn.properties.nodeParams = params(turn).filter(parameter => parameter.name !== 'offset')
|
||
set(turn, 'state', quote('state', first.last, 'state')); set(turn, 'previousState', quote('previousState', first.first, 'state'))
|
||
set(turn, 'rotation', quote('rotation', rotation, 'result')); set(turn, 'jointLimits', quote('jointLimits', info, 'jointLimits'))
|
||
turn.properties.name = label + ' 首遍仅J6-90°(沿用原方向)'
|
||
turnWithdrawal.forEach(node => { node.properties.name = node.properties.name.replace('J6换向', '原姿态复扫结束') })
|
||
set(restore, 'jointLimits', quote('jointLimits', info, 'jointLimits'))
|
||
const afterRestore = settle(firstPreparation, label + ' J6恢复后')
|
||
set(verify, 'state', quote('state', afterRestore.last, 'state'))
|
||
const correction = codeCopy(plannerTemplate, label + ' 复用既有退让Y,不重复退让', 'offset_after_restore', [quote('state', afterRestore.last, 'state'), quote('previousState', afterRestore.first, 'state'), quote('restore', restore, 'result'), quote('offset', restoreWithdrawal[1], 'result')])
|
||
const originalCorrectionLoop = order.find(node => node.type === 'loop' && params(node).some(parameter => parameter.name === 'loopNum' && parameter.quote?.[0] === plannerTemplate.id))
|
||
assert(originalCorrectionLoop)
|
||
const correctionLoop = cloneBlock([originalCorrectionLoop])[0]
|
||
correctionLoop.properties.name = label + ' 已到退让Y则跳过'
|
||
set(correctionLoop, 'loopNum', quote('loopNum', correction, 'result', 'retreatMoves'))
|
||
const correctionRead = cloneBlock(firstPreparation.slice(2))
|
||
correctionRead[0].properties.name = label + ' 原姿态校正前首帧'
|
||
correctionRead[2].properties.name = label + ' 原姿态校正前第二帧'
|
||
const orientation = codeCopy(plannerTemplate, label + ' 在退让平面恢复本组原RPY', 'base_orientation', [quote('state', correctionRead[2], 'state'), quote('previousState', correctionRead[0], 'state'), quote('offset', correction, 'result'), quote('rotation', rotation, 'result')])
|
||
const result = orientation.properties.outputParams.find(parameter => parameter.name === 'result')
|
||
result.children.push(field('orientationMoves', 'array', poseFields))
|
||
const orientationLoop = cloneBlock([originalCorrectionLoop])[0]
|
||
orientationLoop.properties.name = label + ' 原RPY已恢复则跳过校正'
|
||
set(orientationLoop, 'loopNum', quote('loopNum', orientation, 'result', 'orientationMoves'))
|
||
set(base, 'rotation', quote('rotation', orientation, 'result'))
|
||
base.properties.name = label + ' 恢复原姿态后完整复扫路径'
|
||
for (const node of closure([...firstPreparation, ...baseCapture])) node.properties.name = node.properties.name.replaceAll('未转J6', '恢复原姿态')
|
||
// Preserve the original route, scope, capture count and J6 restoration. Only swap passes.
|
||
const nodes = [rotation, order[start + 1], ...first.nodes, turn, order[turnIndex + 1], ...turnedPreparation, ...turnedCapture,
|
||
...restoreWithdrawal, restore, order[restoreIndex + 1], ...afterRestore.nodes, verify, correction, correctionLoop,
|
||
...correctionRead, orientation, orientationLoop, ...firstPreparation, ...baseCapture, ...turnWithdrawal]
|
||
replacements.set(start, { last, nodes })
|
||
// The following RX starts at this newly completed base pass, never a future/old result.
|
||
if (groupIndex % 2 === 0) {
|
||
set(order[nextRotation], 'state', quote('state', turnWithdrawal[3], 'state'))
|
||
set(order[nextRotation], 'offset', quote('offset', turnWithdrawal[1], 'result'))
|
||
}
|
||
groups.push({ label, rx: params(rotation).find(parameter => parameter.name === 'rx').input, first: turned.id, second: base.id, turn: turn.id, restore: restore.id, originalRotation: rotation.id, finalWithdrawal: turnWithdrawal[1].id })
|
||
}
|
||
assert.equal(groups.length, 8)
|
||
const main = []
|
||
for (let index = 0; index < order.length;) {
|
||
if (replacements.has(index)) { const block = replacements.get(index); main.push(...block.nodes); index = block.last + 1 }
|
||
else { main.push(order[index]); if (index === 0) main.push(options); if (index === 11) main.push(info); index++ }
|
||
}
|
||
const start = main[0]
|
||
start.properties.inputParams.push({ name: 'scan_acceleration_mm', type: 'number', children: [], desc: '扫描加速度(mm/s²),默认80;原方案40,可按现场质量与稳定性调整为正数。', input: 80, required: true })
|
||
start.properties.inputParams.find(parameter => parameter.name === 'scan_layers').desc = '每组RX完整扫两遍:先仅J6-90°扫描,再恢复原J6及原RPY完整复扫;不拆奇偶层。'
|
||
order[1].properties.name = '两组RX各完整两遍(先J6-90°,恢复后原姿态复扫)'
|
||
order[1].properties.desc = '保持原两组RX、XYZ范围与完整分层;每组先J6-90°扫一遍,再恢复本组原J6及原RPY完整复扫。仅调整两遍顺序,不拆分奇偶层。'
|
||
const retained = new Set(closure(main).map(node => node.id)), mainIds = new Set(main.map(node => node.id))
|
||
graph.nodes = graph.nodes.filter(node => retained.has(node.id))
|
||
graph.edges = [...graph.edges, ...newEdges].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: uid(), type: 'bezier', sourceNodeId: node.id, targetNodeId: main[index + 1].id, sourceAnchorId: node.id + '_1', targetAnchorId: main[index + 1].id + '_3' })))
|
||
for (const node of graph.nodes) {
|
||
if (mode(node)) 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('完整扫描') && !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, 16)
|
||
assert.equal(graph.nodes.filter(node => node.properties.action === 'SCANNER_STOP').length, 16)
|
||
assert.equal(JSON.stringify(original), JSON.stringify(JSON.parse(snapshot.published?.flowData || snapshot.flowData || JSON.stringify(snapshot))))
|
||
fs.writeFileSync(outputPath, JSON.stringify(graph, null, 2))
|
||
const summary = { nodes: graph.nodes.length, edges: graph.edges.length, groups, scanAccelerationMm: 80, travelEntrySpeedMm: 150, scanSpeedsUnchanged: true, sourceUntouched: true, deviceCommandsSent: 0 }
|
||
if (summaryPath) fs.writeFileSync(summaryPath, JSON.stringify(summary, null, 2))
|
||
console.log(JSON.stringify({ ...summary, groups: groups.length }))
|