pages-index-gj.DSMDhQqr.js 8.7 KB

1
  1. import{_ as t,c as i,a as s,s as e,r as n,n as o,e as a,w as h,i as c,o as l,g as r,h as d,d as x,j as m,F as g,k as f,p as y,l as u,m as p,t as P,f as w}from"./index-ClhdN-8z.js";import{j as S,_ as b}from"./u9.BlT4mEiu.js";import{_ as T,a as C}from"./u11.3SWZsuMf.js";const v=t({data:()=>({yamlConfig:{resolution:.05,origin:[-1.22,-6.42,0]},pgmData:[],websocket:null,isConnected:!1,imagePath:"",canvasWidth:"",canvasHeight:"",animationSpeed:1,currentPointIndex:0,animationTimer:null,znPosition:{x:85,y:200},znImage:"/static/dp_30_17.png",huzgPoints:{x:0,y:0},bjdPoints:[],jqPoints:[],wzPoints:[],addLine:[],imgWidth:"",imgHeight:"",dx:"",dy:"",linePoints:[],points:[]}),mounted(){this.loadPGMFile(),setTimeout((()=>{this.initCanvas()}),700)},onReady(){},onUnload(){this.disconnectFromRos(),this.stopAnimation()},onLoad(){const t=localStorage.getItem("ysPoints");this.wzPoints=t?JSON.parse(t):[],localStorage.getItem("wzPoints");const i=localStorage.getItem("addLine");i&&(this.addLine=JSON.parse(i)),this.connectToRos(),setTimeout((()=>{this.getLocation()}),300),setTimeout((()=>{this.getGj()}),500),setTimeout((()=>{this.startAnimation()}),800)},methods:{initCanvas(){i().select("#myCanvas").boundingClientRect((t=>{if(t){this.canvasWidth=t.width,this.canvasHeight=t.height;const i=s("myCanvas",this);this.ctx=i,this.drawCanvas()}})).exec()},drawCanvas(){this.ctx&&(this.dx=(this.canvasWidth-this.imgWidth)/2,this.dy=(this.canvasHeight-this.imgHeight)/2,this.ctx.drawImage(this.imagePath,this.dx,this.dy,this.imgWidth,this.imgHeight),this.bjdPoints.forEach(((t,i)=>{this.ctx.beginPath(),this.ctx.moveTo(t.x1,t.y1),this.ctx.lineTo(t.x2,t.y2),this.ctx.lineTo(t.x3,t.y3),this.ctx.lineTo(t.x4,t.y4),this.ctx.closePath(),this.ctx.fillStyle="#b0d7b0",this.ctx.fill(),this.ctx.strokeStyle="#b0d7b0",this.ctx.lineWidth=1,this.ctx.stroke()})),this.jqPoints.forEach(((t,i)=>{this.ctx.beginPath(),this.ctx.moveTo(t.x1,t.y1),this.ctx.lineTo(t.x2,t.y2),this.ctx.lineTo(t.x3,t.y3),this.ctx.lineTo(t.x4,t.y4),this.ctx.closePath(),this.ctx.fillStyle="#feb0b0",this.ctx.fill(),this.ctx.strokeStyle="#feb0b0",this.ctx.lineWidth=1,this.ctx.stroke()})),this.wzPoints.forEach(((t,i)=>{this.ctx.beginPath(),this.ctx.setFontSize(12),this.ctx.setFillStyle("#0099ff"),this.ctx.fillText(t.name,t.x+this.dx+6,t.y+this.dy+5),this.ctx.setFillStyle("#0066ff"),this.ctx.arc(t.x+this.dx,t.y+this.dy,4,0,2*Math.PI),this.ctx.fill(),this.ctx.closePath()})),this.ctx.setStrokeStyle("#01ff00"),this.ctx.setLineWidth(2),this.ctx.setLineCap("round"),this.points.length>0&&(this.ctx.beginPath(),this.ctx.moveTo(this.points[0].x+this.dx,this.points[0].y+this.dy),this.points.forEach(((t,i)=>{i>0&&this.ctx.lineTo(t.x+this.dx,t.y+this.dy)})),this.ctx.stroke(),this.ctx.setFillStyle("#d7d7d7"),this.ctx.beginPath(),this.ctx.arc(this.points[0].x+this.dx,this.points[0].y+this.dy,5,0,2*Math.PI),this.ctx.fill(),this.ctx.setFontSize(12),this.ctx.setFillStyle("#0099ff"),this.ctx.fillText("起点",this.points[0].x+this.dx+5,this.points[0].y+this.dy-5),this.ctx.setFillStyle("#ff0000"),this.ctx.beginPath(),this.ctx.arc(this.points[this.points.length-1].x+this.dx,this.points[this.points.length-1].y+this.dy,5,0,2*Math.PI),this.ctx.fill(),this.ctx.setFontSize(12),this.ctx.setFillStyle("#0099ff")),this.ctx.draw())},goAdd(){this.startAnimation()},startAnimation(){this.getLocation(),this.isAnimating||this.linePoints.length<=1||(this.isAnimating=!0,this.currentPointIndex=1,this.animationTimer=setInterval((()=>{this.getLocation(),this.drawTrajectorySegment(),this.currentPointIndex++}),2e3/this.animationSpeed))},getLocation(){if(!this.websocket||!this.isConnected)return void e({title:"服务连接异常,请检查",icon:"none"});this.websocket.send(JSON.stringify({code:0,cmd:1002,msg:"",data:""}))},drawTrajectorySegment(){const t=s("myCanvas",this);if(t.draw(!0),t.setStrokeStyle(this.trajectoryColor),t.setLineWidth(3),t.setLineCap("round"),t.setLineJoin("round"),t.setStrokeStyle("#0099ff"),this.linePoints.length>0){t.beginPath(),t.moveTo(this.linePoints[0].x+this.dx,this.linePoints[0].y+this.dy);for(let i=1;i<=this.currentPointIndex;i++)t.lineTo(this.linePoints[i].x+this.dx,this.linePoints[i].y+this.dy);t.stroke()}t.draw(!0)},stopAnimation(){this.isAnimating=!1,this.animationTimer&&(clearInterval(this.animationTimer),this.animationTimer=null)},pixelToWorld(t,i){const s=parseFloat(t),e=parseFloat(i);if(isNaN(s)||isNaN(e))return;const n=this.worldToPixel(s,e);return n&&!(n.x<0||n.x>=this.canvasWidth||n.y<0||n.y>=this.canvasHeight)?[n.x,n.y]:void 0},worldToPixel(t,i){if(!this.yamlConfig||!this.pgmData)return null;return{x:Math.round((t-this.yamlConfig.origin[0])/this.yamlConfig.resolution),y:Math.round((this.yamlConfig.origin[1]+this.imgHeight*this.yamlConfig.resolution-i)/this.yamlConfig.resolution)}},connectToRos(){try{this.websocket=new WebSocket("ws://192.168.2.8:9001"),this.websocket.onopen=()=>{console.log("Connected to ROS2 Bridge"),this.isConnected=!0},this.websocket.onmessage=t=>{const i=JSON.parse(t.data);if("1007"==i.cmd){const t=i.data,s=JSON.parse(t.replace(/'/g,'"'));s.length>0&&s.forEach(((t,i)=>{if(t.x){const i=this.pixelToWorld(t.x,t.y);i&&this.points.push({x:i[0],y:i[1]})}}))}else if("1002"==i.cmd){const t=i.data;if(t.x){const i=this.pixelToWorld(t.x,t.y);i&&this.linePoints.push({x:i[0],y:i[1]})}}},this.websocket.onerror=t=>{console.error("WebSocket error:",t),this.connectionStatus="连接错误: "+t,this.isConnected=!1},this.websocket.onclose=()=>{console.log("WebSocket connection closed"),this.isConnected=!1,this.connectionStatus="连接已关闭"}}catch(t){console.error("Failed to connect to ROS2:",t),this.connectionStatus="连接失败: "+t.message}},getYaml(){n({url:"http://192.168.2.8:9002/static/imgs/map.yaml",method:"GET",success:t=>{const i=S.load(t.data);this.yamlConfig=i},fail:t=>{console.error(t)}})},getGj(){if(!this.websocket||!this.isConnected)return void e({title:"服务连接异常,请检查",icon:"none"});this.websocket.send(JSON.stringify({code:0,cmd:1007,msg:"",data:""}))},loadPGMFile(){n({url:"http://192.168.2.8:9002/static/imgs/map.pgm",method:"GET",responseType:"ArrayBuffer",success:t=>{if(200===t.statusCode){this.getYaml();const i=t.data,s=this.parsePGMData(i),e=document.createElement("canvas"),n=e.getContext("2d");e.width=s.width,e.height=s.height,this.imgWidth=s.width,this.imgHeight=s.height;const o=n.createImageData(s.width,s.height);for(let t=0;t<s.data.length;t++){const i=s.data[t];o.data[4*t]=i,o.data[4*t+1]=i,o.data[4*t+2]=i,o.data[4*t+3]=255}n.putImageData(o,0,0),this.imagePath=e.toDataURL("image/png")}else console.error("请求PGM文件失败,状态码:",t.statusCode)},fail:t=>{console.error("请求PGM文件出错:",t)}})},parsePGMData(t){const i=new TextDecoder("ascii"),s=new Uint8Array(t);let e=0,n=[],o="",a=0;for(let x=0;x<s.length;x++)if(10===s[x]){const t=o.trim();if(o="",t.startsWith("#")||""===t){e=x+1;continue}if(n.push(t),a++,e=x+1,a>=3)break}else o+=String.fromCharCode(s[x]);if(n.length<3)throw new Error("PGM文件头部信息不完整");const h=n[0],c=n[1].split(" ").map(Number),l=parseInt(n[2]);if("P5"!==h&&"P2"!==h)throw new Error("不支持的PGM格式: "+h);if(c.length<2||isNaN(c[0])||isNaN(c[1]))throw new Error("无效的PGM维度信息");const r=c[0],d=c[1];if(isNaN(l)||l<=0)throw new Error("无效的PGM最大值");if("P5"===h){const i=new Uint8Array(t,e);return{width:r,height:d,data:this.normalizePixelData(i,l),maxVal:l,type:"P5 (二进制)"}}{const s=i.decode(new Uint8Array(t,e)).trim().split(/\s+/).map(Number);return{width:r,height:d,data:this.normalizePixelData(new Uint8Array(s),l),maxVal:l,type:"P2 (ASCII)"}}},normalizePixelData(t,i){if(255===i)return t;const s=new Uint8Array(t.length);for(let e=0;e<t.length;e++)s[e]=Math.round(255*t[e]/i);return s},goHome(){o({url:"/pages/index/index"})}}},[["render",function(t,i,s,e,n,o){const S=f,v=c,k=y,I=u,W=p;return l(),a(v,{class:"containerDh"},{default:h((()=>[r(v,{class:"contentDh"},{default:h((()=>[r(v,{class:"line"},{default:h((()=>[r(S,{class:"item-logo lleft",src:b}),r(v,{class:"item-font"},{default:h((()=>[d("位置点")])),_:1})])),_:1})])),_:1}),r(v,{class:"content"},{default:h((()=>[r(k,{"canvas-id":"myCanvas",id:"myCanvas",class:"canvas"})])),_:1}),r(v,{class:"footer"},{default:h((()=>[r(v,{class:"cxsm"},{default:h((()=>[r(v,{class:"cxsm-ts"},{default:h((()=>[d("行驶路线:"),r(I,{class:"xs-font"},{default:h((()=>[d("行驶中")])),_:1})])),_:1}),r(v,{class:"line"},{default:h((()=>[r(S,{class:"logo",src:T}),r(S,{class:"jt",src:C}),(l(!0),x(g,null,m(n.addLine,((t,i)=>(l(),x(g,null,[r(v,{class:"line-item"},{default:h((()=>[d(P(t.name),1)])),_:2},1024),i<n.addLine.length-1?(l(),a(S,{key:0,class:"jt",src:C})):w("",!0)],64)))),256))])),_:1})])),_:1}),r(W,{onClick:i[0]||(i[0]=t=>o.goHome()),class:"bottom-button"},{default:h((()=>[d("退出导航")])),_:1})])),_:1})])),_:1})}],["__scopeId","data-v-92b83e36"]]);export{v as default};