439 lines
15 KiB
JavaScript
439 lines
15 KiB
JavaScript
const { createApp } = Vue
|
|
const API = ''
|
|
|
|
createApp({
|
|
data() {
|
|
return {
|
|
tab: 'map',
|
|
// 任务配置
|
|
missionConfig: { rows: 3, cols: 3, grid: [], machines: [] },
|
|
selectedMachine: null,
|
|
// 地图
|
|
mapForm: { map_dir: '/home/elephant/agv_pro_ros2/src/agv_pro_navigation2/map/', map_file: 'map.yaml' },
|
|
mapMsg: '',
|
|
mapLoaded: false,
|
|
mapImageUrl: '',
|
|
mapMeta: null,
|
|
// 点位
|
|
points: [],
|
|
newPointName: '',
|
|
newPointMode: 'front',
|
|
newPointSequence: ['front', 'back'],
|
|
// 机型(姿态组)
|
|
models: [],
|
|
selectedModelId: null,
|
|
newModelName: '',
|
|
newModelSerial: '',
|
|
poseForm: {},
|
|
// 机械臂
|
|
armConnected: false,
|
|
currentAngles: [],
|
|
angleInputs: [],
|
|
previewUrl: API + '/api/camera/preview',
|
|
jogIntervals: {},
|
|
// AGV
|
|
cameraOpened: false,
|
|
agvConnected: false,
|
|
agvBattery: null,
|
|
agvPosition: null,
|
|
agvSpeed: 0.5,
|
|
agvMoveInterval: null,
|
|
agvCameraUrl: API + '/api/camera/refresh',
|
|
agvCameraTimer: null,
|
|
}
|
|
},
|
|
mounted() {
|
|
this.refresh()
|
|
this.refreshAngles()
|
|
},
|
|
watch: {
|
|
tab(val) {
|
|
if (val === 'agv') {
|
|
this.agvCameraTimer = setInterval(() => {
|
|
this.agvCameraUrl = API + '/api/camera/refresh?t=' + Date.now()
|
|
}, 1000)
|
|
} else {
|
|
if (this.agvCameraTimer) {
|
|
clearInterval(this.agvCameraTimer)
|
|
this.agvCameraTimer = null
|
|
}
|
|
}
|
|
}
|
|
},
|
|
beforeUnmount() {
|
|
Object.values(this.jogIntervals).forEach(i => clearInterval(i))
|
|
if (this.agvCameraTimer) clearInterval(this.agvCameraTimer)
|
|
},
|
|
methods: {
|
|
async refresh() {
|
|
try {
|
|
const res = await fetch(API + '/api/status')
|
|
const data = await res.json()
|
|
this.agvConnected = data.agv_connected
|
|
this.armConnected = data.arm_connected
|
|
this.cameraOpened = data.camera_opened
|
|
this.mapLoaded = data.map_loaded
|
|
// 如果地图已加载,自动获取地图图像和元数据
|
|
if (data.map_loaded) {
|
|
this.mapImageUrl = API + '/api/map/image?t=' + Date.now()
|
|
try {
|
|
const metaRes = await fetch(API + '/api/map/meta')
|
|
const meta = await metaRes.json()
|
|
if (meta.ok) this.mapMeta = meta
|
|
} catch (e) {}
|
|
}
|
|
} catch (e) {}
|
|
await this.loadAllPoints()
|
|
await this.loadAllModels()
|
|
},
|
|
// === 地图 ===
|
|
async loadMap() {
|
|
const res = await fetch(API + '/api/map/load', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(this.mapForm)
|
|
})
|
|
const data = await res.json()
|
|
this.mapMsg = data.ok ? '✅ 地图加载成功' : '❌ ' + (data.error || '加载失败')
|
|
this.mapLoaded = data.ok
|
|
if (data.ok) {
|
|
this.mapImageUrl = API + '/api/map/image?t=' + Date.now()
|
|
try {
|
|
const metaRes = await fetch(API + '/api/map/meta')
|
|
const meta = await metaRes.json()
|
|
if (meta.ok) this.mapMeta = meta
|
|
} catch (e) {}
|
|
}
|
|
},
|
|
onMapError() {
|
|
this.mapMsg = '❌ 地图图像加载失败'
|
|
},
|
|
getMapX(coords) {
|
|
if (!coords || !this.mapMeta) return 50
|
|
const [x, y, yaw] = coords
|
|
const { resolution, origin, width } = this.mapMeta
|
|
const px = (x - origin[0]) / (resolution * width) * 100
|
|
return Math.max(0, Math.min(100, px))
|
|
},
|
|
getMapY(coords) {
|
|
if (!coords || !this.mapMeta) return 50
|
|
const [x, y, yaw] = coords
|
|
const { resolution, origin, height } = this.mapMeta
|
|
const py = (y - origin[1]) / (resolution * height) * 100
|
|
return Math.max(0, Math.min(100, 100 - py))
|
|
},
|
|
async saveMap() {
|
|
await fetch(API + '/api/map/save', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(this.mapForm)
|
|
})
|
|
this.mapMsg = '✅ 地图配置已保存'
|
|
},
|
|
// === 点位 ===
|
|
async loadAllPoints() {
|
|
const res = await fetch(API + '/api/points/list')
|
|
const data = await res.json()
|
|
this.points = data.points || []
|
|
},
|
|
async addPoint() {
|
|
const res = await fetch(API + '/api/points/add', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
name: this.newPointName || 'point_' + (this.points.length + 1),
|
|
photo_mode: this.newPointMode,
|
|
sequence: this.newPointSequence
|
|
})
|
|
})
|
|
const data = await res.json()
|
|
if (data.ok) {
|
|
await this.loadAllPoints()
|
|
this.newPointName = ''
|
|
}
|
|
},
|
|
async deletePoint(id) {
|
|
if (!confirm('确定删除该点位?')) return
|
|
await fetch(API + '/api/points/delete/' + id, { method: 'DELETE' })
|
|
await this.loadAllPoints()
|
|
},
|
|
async saveAllPoints() {
|
|
await fetch(API + '/api/points/save', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ points: this.points })
|
|
})
|
|
alert('点位已保存')
|
|
},
|
|
getPoint(id) {
|
|
return this.points.find(p => p.id === id)
|
|
},
|
|
formatAngles(angles) {
|
|
if (!angles) return '—'
|
|
return angles.map(a => (a || 0).toFixed(1) + '°').join(' / ')
|
|
},
|
|
// === 机型管理 ===
|
|
async loadAllModels() {
|
|
const res = await fetch(API + '/api/models/list')
|
|
const data = await res.json()
|
|
this.models = data.models || []
|
|
// 初始化 poseForm
|
|
this.models.forEach(m => {
|
|
if (!this.poseForm[m.id]) {
|
|
this.poseForm[m.id] = { name: '', photo_type: 'front', description: '' }
|
|
}
|
|
})
|
|
},
|
|
async addModel() {
|
|
const res = await fetch(API + '/api/models/add', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
name: this.newModelName || 'model_' + (this.models.length + 1),
|
|
serial_prefix: this.newModelSerial,
|
|
description: ''
|
|
})
|
|
})
|
|
const data = await res.json()
|
|
if (data.ok) {
|
|
await this.loadAllModels()
|
|
this.newModelName = ''
|
|
this.newModelSerial = ''
|
|
}
|
|
},
|
|
async deleteModel(modelId) {
|
|
if (!confirm('确定删除该机型?其下所有姿态将被删除!')) return
|
|
await fetch(API + '/api/models/delete/' + modelId, { method: 'DELETE' })
|
|
await this.loadAllModels()
|
|
},
|
|
// === 姿态管理(属于机型)===
|
|
async addPose(modelId) {
|
|
const form = this.poseForm[modelId]
|
|
if (!form) return
|
|
await fetch(API + '/api/models/poses/add', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
model_id: modelId,
|
|
name: form.name || '姿态' + ((this.getModel(modelId)?.poses?.length || 0) + 1),
|
|
photo_type: form.photo_type,
|
|
arm_angles: this.currentAngles,
|
|
speed: 500,
|
|
description: form.description || ''
|
|
})
|
|
})
|
|
await this.loadAllModels()
|
|
form.name = ''
|
|
form.description = ''
|
|
},
|
|
async deletePose(modelId, poseId) {
|
|
if (!confirm('确定删除该姿态?')) return
|
|
await fetch(API + '/api/models/' + modelId + '/poses/' + poseId, { method: 'DELETE' })
|
|
await this.loadAllModels()
|
|
},
|
|
getModel(id) {
|
|
return this.models.find(m => m.id === id)
|
|
},
|
|
// === 机械臂 ===
|
|
|
|
clearSelection() { this.selectedMachine = null },
|
|
async saveMachineCoords() {
|
|
if (!this.selectedMachine) return
|
|
try {
|
|
const res = await fetch(API + '/api/mission/machines/' + this.selectedMachine.id, {
|
|
method: 'PATCH',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
front: this.selectedMachine.front,
|
|
back: this.selectedMachine.back
|
|
})
|
|
})
|
|
if (res.ok) {
|
|
this.mapMsg = '✅ 机器坐标已保存'
|
|
setTimeout(() => this.mapMsg = '', 2000)
|
|
} else {
|
|
alert('保存失败: ' + res.status)
|
|
}
|
|
} catch (e) { alert('保存失败: ' + e.message) }
|
|
},
|
|
selectMachine(machine) {
|
|
// 确保 front/back 永远有 coords 数组,避免 v-model 赋值失败
|
|
if (!machine.front) machine.front = { coords: [0, 0, 0], poses: [] }
|
|
else if (!Array.isArray(machine.front.coords)) machine.front.coords = [0, 0, 0]
|
|
if (!machine.back) machine.back = { coords: [0, 0, 0], poses: [] }
|
|
else if (!Array.isArray(machine.back.coords)) machine.back.coords = [0, 0, 0]
|
|
this.selectedMachine = machine
|
|
console.log('selectedMachine:', machine)
|
|
},
|
|
onCellClick(ri, ci) {
|
|
let m = this.getMachineAt(ri, ci)
|
|
if (!m) {
|
|
// 自动创建机器记录
|
|
this.createMachine(ri, ci).then(() => {
|
|
m = this.getMachineAt(ri, ci)
|
|
if (m) this.selectMachine(m)
|
|
})
|
|
} else {
|
|
this.selectMachine(m)
|
|
}
|
|
},
|
|
async createMachine(ri, ci) {
|
|
try {
|
|
const res = await fetch(API + '/api/mission/machines', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ row: ri, col: ci, front: { coords: [0, 0, 0], poses: [] }, back: { coords: [0, 0, 0], poses: [] } })
|
|
})
|
|
await this.loadAllMachines()
|
|
return res.ok
|
|
} catch (e) { alert('创建机器失败: ' + e.message); return false }
|
|
},
|
|
|
|
async loadAllMachines() {
|
|
try {
|
|
const res = await fetch(API + '/api/mission/machines')
|
|
const data = await res.json()
|
|
this.missionConfig.machines = data.machines || []
|
|
} catch (e) { console.error('加载机器列表失败', e) }
|
|
},
|
|
getMachineAt(ri, ci) {
|
|
if (!this.missionConfig.machines) return null
|
|
return this.missionConfig.machines.find(m => m.row === ri && m.col === ci) || null
|
|
},
|
|
getPositionAt(ri, ci) {
|
|
if (!this.missionConfig.machines) return null
|
|
const machine = this.getMachineAt(ri, ci)
|
|
if (!machine) return null
|
|
if (ri === 0) return machine.front
|
|
const prevMachine = this.getMachineAt(ri - 1, ci)
|
|
return prevMachine ? prevMachine.back : machine.front
|
|
},
|
|
async refreshAngles() {
|
|
if (!this.armConnected) return
|
|
try {
|
|
const res = await fetch(API + '/api/arm/get_angles')
|
|
const data = await res.json()
|
|
if (data.ok && data.angles) {
|
|
this.currentAngles = data.angles
|
|
this.angleInputs = [...data.angles]
|
|
}
|
|
} catch (e) {}
|
|
},
|
|
async setAngle(idx, val) {
|
|
await fetch(API + '/api/arm/set_angle', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ joint: 'J' + (idx + 1), angle: val })
|
|
})
|
|
},
|
|
async applyAngles() {
|
|
await fetch(API + '/api/arm/set_angles', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ angles: this.angleInputs, speed: 500 })
|
|
})
|
|
},
|
|
jogStart(idx, dir) {
|
|
const joint = 'J' + (idx + 1)
|
|
fetch(API + '/api/arm/jog', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ joint, direction: dir })
|
|
})
|
|
this.jogIntervals[idx] = setInterval(() => this.refreshAngles(), 200)
|
|
},
|
|
jogStop(idx) {
|
|
clearInterval(this.jogIntervals[idx])
|
|
const joint = 'J' + (idx + 1)
|
|
fetch(API + '/api/arm/jog', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ joint, direction: 0 })
|
|
})
|
|
setTimeout(() => this.refreshAngles(), 300)
|
|
},
|
|
onPreviewError(e) {
|
|
e.target.style.display = 'none'
|
|
},
|
|
// === AGV 控制 ===
|
|
async refreshAgvPosition() {
|
|
if (!this.agvConnected) return
|
|
try {
|
|
const res = await fetch(API + '/api/agv/position')
|
|
const data = await res.json()
|
|
if (data.ok) {
|
|
this.agvPosition = data.position
|
|
this.agvBattery = data.battery
|
|
}
|
|
} catch (e) {}
|
|
},
|
|
agvMoveStart(dir) {
|
|
if (!this.agvConnected) return
|
|
fetch(API + '/api/agv/move', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ direction: dir, speed: this.agvSpeed })
|
|
})
|
|
},
|
|
agvMoveStop() {
|
|
fetch(API + '/api/agv/move', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ direction: 'stop' })
|
|
})
|
|
},
|
|
async agvStop() {
|
|
await fetch(API + '/api/agv/stop', { method: 'POST' })
|
|
},
|
|
async agvResetCollision() {
|
|
if (!this.agvConnected) {
|
|
alert('AGV 未连接')
|
|
return
|
|
}
|
|
if (!confirm('确定执行撞物体后复位?')) return
|
|
try {
|
|
const res = await fetch(API + '/api/agv/reset', { method: 'POST' })
|
|
const data = await res.json()
|
|
if (data.ok) {
|
|
alert('✅ ' + data.message)
|
|
await this.refresh()
|
|
await this.refreshAgvPosition()
|
|
} else {
|
|
alert('❌ 复位失败: ' + (data.error || '未知错误'))
|
|
}
|
|
} catch (e) {
|
|
alert('❌ 复位请求失败: ' + e.message)
|
|
}
|
|
},
|
|
async capturePosition(ri, ci, side) {
|
|
if (!this.agvConnected) { alert('请先连接AGV'); return }
|
|
let machine = this.getMachineAt(ri, ci)
|
|
if (!machine) {
|
|
try {
|
|
const res = await fetch(API + '/api/mission/machines', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ row: ri, col: ci, front: { coords: [0, 0, 0], poses: [] }, back: { coords: [0, 0, 0], poses: [] } })
|
|
})
|
|
if (!res.ok) throw new Error('创建失败')
|
|
await this.loadAllMachines()
|
|
machine = this.getMachineAt(ri, ci)
|
|
} catch (e) { alert('创建机器失败: ' + e.message); return }
|
|
}
|
|
try {
|
|
const res = await fetch(API + '/api/agv/position')
|
|
const pos = await res.json()
|
|
let x = 0, y = 0, theta = 0
|
|
if (pos.position && Array.isArray(pos.position)) { x = pos.position[0]||0; y = pos.position[1]||0; theta = pos.position[2]||0 }
|
|
if (!machine) { machine = this.getMachineAt(ri, ci) }
|
|
if (!machine) { alert('机器记录不存在'); return }
|
|
if (side === 'front') { machine.front.coords = [x, y, theta] } else { machine.back.coords = [x, y, theta] }
|
|
await fetch(API + '/api/mission/machines/' + machine.id, {
|
|
method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(machine)
|
|
})
|
|
alert((side==='front'?'正面':'背面')+'点位已更新: ('+x.toFixed(2)+','+y.toFixed(2)+','+theta.toFixed(2)+')')
|
|
} catch (e) { alert('读取位置失败: '+e.message) }
|
|
},
|
|
}
|
|
}).mount('#app')
|