导航修复

This commit is contained in:
ywb
2026-05-18 23:40:14 +08:00
parent deb97a67c0
commit 50f6d4c295
3 changed files with 322 additions and 78 deletions
+214 -1
View File
@@ -22,6 +22,8 @@ createApp({
nav2Available: false,
// 点位
points: [],
editingPoint: null, // 当前编辑的点位 {pointRow, col}
pointEditor: { x: 0, y: 0, yaw: 0 },
newPointName: '',
newPointMode: 'front',
newPointSequence: ['front', 'back'],
@@ -722,6 +724,217 @@ createApp({
return machineBelow.front
}
return null
}
},
// 点位归属标签
getPointOwnerLabel(pointRow, col) {
var rows = this.missionConfig.rows
var labels = []
if (pointRow === 0) {
var m = this.getMachineAt(0, col)
if (m) labels.push('机器' + (m.row + 1) + '正面')
} else if (pointRow === rows) {
var m2 = this.getMachineAt(rows - 1, col)
if (m2) labels.push('机器' + (m2.row + 1) + '背面')
} else {
var mAbove = this.getMachineAt(pointRow - 1, col)
var mBelow = this.getMachineAt(pointRow, col)
if (mAbove) labels.push('机器' + (mAbove.row + 1) + '背面')
if (mBelow) labels.push('机器' + (mBelow.row + 1) + '正面')
}
if (labels.length === 0) return '第' + (col + 1) + '列 · 无归属'
return '第' + (col + 1) + '列 · ' + labels.join('/')
},
canClearPoint(pointRow, col) {
var rows = this.missionConfig.rows
if (pointRow > 0 && pointRow <= rows) {
var mAbove = this.getMachineAt(pointRow - 1, col)
if (mAbove) return false
}
if (pointRow >= 0 && pointRow < rows) {
var mBelow = this.getMachineAt(pointRow, col)
if (mBelow) return false
}
return true
},
openPointEdit(pointRow, col) {
var existing = this.getPointAt(pointRow, col)
if (existing && existing.coords) {
this.pointEditor.x = existing.coords[0] !== undefined ? existing.coords[0] : 0
this.pointEditor.y = existing.coords[1] !== undefined ? existing.coords[1] : 0
this.pointEditor.yaw = existing.coords[2] !== undefined ? existing.coords[2] : 0
} else {
this.pointEditor.x = 0
this.pointEditor.y = 0
this.pointEditor.yaw = 0
}
this.editingPoint = { pointRow: pointRow, col: col }
},
closePointEdit() {
this.editingPoint = null
},
async loadPointFromAgv() {
if (!this.agvConnected) { alert('请先连接AGV'); return }
try {
const res = await fetch(API + '/api/agv/position')
const pos = await res.json()
if (pos.ok && pos.position && Array.isArray(pos.position)) {
this.pointEditor.x = pos.position[0] ?? 0
this.pointEditor.y = pos.position[1] ?? 0
this.pointEditor.yaw = pos.position[2] ?? 0
alert('✅ 已读取AGV位置: (' + this.pointEditor.x.toFixed(2) + ', ' + this.pointEditor.y.toFixed(2) + ', ' + this.pointEditor.yaw.toFixed(2) + ')')
} else if (pos.ok && (!pos.position || !Array.isArray(pos.position))) {
alert('⚠️ AGV 未发布位置数据,请检查 AGV 传感器是否正常')
} else {
alert('读取AGV位置失败: ' + (pos.error || '未知错误'))
}
} catch (e) { alert('读取AGV位置失败: ' + e.message) }
},
async savePoint() {
if (!this.editingPoint) return
var pointRow = this.editingPoint.pointRow
var col = this.editingPoint.col
var coords = [this.pointEditor.x, this.pointEditor.y, this.pointEditor.yaw]
try {
var saveRes = await fetch(API + '/api/mission/positions', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
row: pointRow, col: col, side: 'shoot', coords: coords, poses: []
})
})
var saveData = await saveRes.json()
if (!saveData.ok) { alert('保存点位失败: ' + (saveData.error || '未知错误')); return }
this.syncPointToMachines(pointRow, col, coords)
await this.loadMissionConfig()
alert('✅ 点位坐标已保存: (' + coords[0].toFixed(2) + ', ' + coords[1].toFixed(2) + ', ' + coords[2].toFixed(2) + ')')
this.closePointEdit()
} catch (e) { alert('保存点位失败: ' + e.message) }
},
syncPointToMachines(pointRow, col, coords) {
var rows = this.missionConfig.rows
if (pointRow === 0) {
var m0 = this.getMachineAt(0, col)
if (m0) this.updateMachineSide(m0, 'front', coords)
return
}
if (pointRow === rows) {
var mLast = this.getMachineAt(rows - 1, col)
if (mLast) this.updateMachineSide(mLast, 'back', coords)
return
}
var mAbove = this.getMachineAt(pointRow - 1, col)
var mBelow = this.getMachineAt(pointRow, col)
if (mAbove) this.updateMachineSide(mAbove, 'back', coords)
if (mBelow) this.updateMachineSide(mBelow, 'front', coords)
},
async updateMachineSide(machine, side, coords) {
try {
var update = {}
update[side] = { coords: coords, poses: (machine[side] && machine[side].poses) ? machine[side].poses : [] }
await fetch(API + '/api/mission/machines/' + machine.id, {
method: 'PUT', headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(update)
})
} catch (e) { console.error('同步机器坐标失败', e) }
},
async clearPoint() {
if (!this.editingPoint) return
var pointRow = this.editingPoint.pointRow
var col = this.editingPoint.col
if (!this.canClearPoint(pointRow, col)) {
var ownerLabel = this.getPointOwnerLabel(pointRow, col)
alert('⚠️ 无法清空!此点位服务于: ' + ownerLabel + '\n必须先移除相关机器才能清空此点位。')
return
}
if (!confirm('确定清空此点位坐标?')) return
try {
await fetch(API + '/api/mission/positions/' + pointRow + '/' + col, { method: 'DELETE' })
await this.loadMissionConfig()
alert('✅ 点位已清空')
this.closePointEdit()
} catch (e) { alert('清空点位失败: ' + e.message) }
},
async navigateToPoint() {
if (!this.editingPoint || !this.pointEditor) return
var coords = [this.pointEditor.x, this.pointEditor.y, this.pointEditor.yaw]
if (!confirm('确定导航到点位 (' + coords[0].toFixed(2) + ', ' + coords[1].toFixed(2) + ') 吗?')) return
try {
var res = await fetch(API + '/api/navigate/to', {
method: 'POST', headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ x: coords[0], y: coords[1] })
})
var data = await res.json()
if (data.ok) {
alert('✅ 导航已启动')
} else {
alert('❌ ' + (data.error || '导航启动失败'))
}
} catch (e) { alert('导航请求失败: ' + e.message) }
},
async refreshPoseAngles(modelId, poseId) {
if (!this.armConnected) { alert('机械臂未连接'); return }
try {
var res = await fetch(API + '/api/arm/get_angles')
var data = await res.json()
if (data.ok && data.angles) {
var model = this.models.find(m => m.id === modelId)
if (model) {
var pose = model.poses.find(p => p.id === poseId)
if (pose) {
pose.arm_angles = [...data.angles]
await fetch(API + '/api/models/' + modelId + '/poses/' + poseId, {
method: 'PUT', headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ arm_angles: pose.arm_angles })
})
}
}
}
} catch (e) { console.error('refreshPoseAngles error:', e) }
},
async applyPoseAngles(modelId, poseId) {
var model = this.models.find(m => m.id === modelId)
if (model) {
var pose = model.poses.find(p => p.id === poseId)
if (pose && pose.arm_angles) {
try {
await fetch(API + '/api/arm/set_angles', {
method: 'POST', headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ angles: pose.arm_angles, speed: 500 })
})
} catch (e) { console.error('applyPoseAngles error:', e) }
}
}
},
async adjustPoseAngle(modelId, poseId, jointIndex, delta) {
var model = this.models.find(m => m.id === modelId)
if (model) {
var pose = model.poses.find(p => p.id === poseId)
if (pose) {
if (!pose.arm_angles) pose.arm_angles = [0, 0, 0, 0, 0, 0]
pose.arm_angles[jointIndex] = (pose.arm_angles[jointIndex] || 0) + delta
await fetch(API + '/api/models/' + modelId + '/poses/' + poseId, {
method: 'PUT', headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ arm_angles: pose.arm_angles })
})
await fetch(API + '/api/arm/set_angle', {
method: 'POST', headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ joint: 'J' + (jointIndex + 1), angle: pose.arm_angles[jointIndex] })
})
}
}
},
}
}).mount('#app')