| 1 |
- import{_ as t,f as i,g as s,s as e,r as o,h as n,e as a,a as h,w as c,i as l,o as d,d as r,j as x,t as g,b as m,c as f,k as y,F as u,l as p,q as P,m as w,p as T,u as b}from"./index-CGB6xs9J.js";import{j as S,_ as C}from"./js-yaml.B_5KFMWC.js";import{_ as I,a as v}from"./u11.3SWZsuMf.js";const L=t({data:()=>({IP:"192.168.40.62",isNav:1,yamlConfig:{resolution:.05,origin:[-1.22,-6.42,0]},sxTest:"行驶中",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:[],goArr:[],imgWidth:"",imgHeight:"",dx:"",dy:"",linePoints:[],points:[],yaw:"",goIndex:-1,goToLineIndex:0,isLoop:!1,isLoopIndex:1,isjl:!1,dqDistance:0,isdb:!1}),mounted(){this.loadPGMFile(),setTimeout((()=>{this.initCanvas()}),300)},onReady(){},onUnload(){this.disconnectFromRos(),this.stopAnimation()},onLoad(){this.IP=localStorage.getItem("ip");const t=localStorage.getItem("ysPoints");this.wzPoints=t?JSON.parse(t):[];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=!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){if(t.beginPath(),t.moveTo(this.linePoints[0].x+this.dx,this.linePoints[0].y+this.dy),this.linePoints.length>1)for(let i=1;i<=this.currentPointIndex;i++)i<this.linePoints.length&&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 o=this.worldToPixel(s,e);return o&&!(o.x<0||o.x>=this.canvasWidth||o.y<0||o.y>=this.canvasHeight)?[o.x,o.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://"+this.IP+":9001"),this.websocket.onopen=()=>{console.log("Connected to ROS2 Bridge"),this.isConnected=!0},this.websocket.onmessage=t=>{const i=JSON.parse(t.data);if(console.log(i),"1007"==i.cmd){const t=i.data,s=JSON.parse(t.replace(/'/g,'"'));s.length>0&&(s.forEach(((t,i)=>{const s=this.pixelToWorld(t.x,t.y);s&&this.points.push({x:s[0],y:s[1]})})),this.drawCanvas())}else if("1002"==i.cmd){const t=i.data;if(this.yaw=t.yaw,t.x){const i=this.pixelToWorld(t.x,t.y);i&&this.linePoints.push({x:i[0],y:i[1]});const s=this.addLine.length-1;if(this.goIndex<s){const i=this.goIndex+1,s=Math.sqrt(Math.pow(t.x-this.addLine[i].x,2)+Math.pow(t.y-this.addLine[i].y,2));if(1==this.isjl&&(this.dqDistance=s,this.isjl=!1,setTimeout((()=>{this.isdb=!0}),1e4)),1==this.isdb){this.dqDistance-s<=.1&&(this.goToLineIndex--,this.goTo()),this.isjl=!1,this.isdb=!1}s<=.5&&(this.goIndex++,this.goTo())}else this.isLoop?(this.linePoints=[],this.drawCanvas(),this.currentPointIndex=0,this.goToLineIndex=-1,this.goIndex=-1,this.goTo()):(this.isNav=2,this.sxTest="已到达目标点,完成本次行驶",this.stopAnimation())}}},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(){o({url:"http://"+this.IP+":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(){const t="http://"+this.IP+":9002/static/imgs/map.pgm";o({url:t,method:"GET",responseType:"ArrayBuffer",success:t=>{if(200===t.statusCode){this.getYaml();const i=t.data,s=this.parsePGMData(i),e=document.createElement("canvas"),o=e.getContext("2d");e.width=s.width,e.height=s.height,this.imgWidth=s.width,this.imgHeight=s.height;const n=o.createImageData(s.width,s.height);for(let t=0;t<s.data.length;t++){const i=s.data[t];n.data[4*t]=i,n.data[4*t+1]=i,n.data[4*t+2]=i,n.data[4*t+3]=255}o.putImageData(n,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,o=[],n="",a=0;for(let x=0;x<s.length;x++)if(10===s[x]){const t=n.trim();if(n="",t.startsWith("#")||""===t){e=x+1;continue}if(o.push(t),a++,e=x+1,a>=3)break}else n+=String.fromCharCode(s[x]);if(o.length<3)throw new Error("PGM文件头部信息不完整");const h=o[0],c=o[1].split(" ").map(Number),l=parseInt(o[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 d=c[0],r=c[1];if(isNaN(l)||l<=0)throw new Error("无效的PGM最大值");if("P5"===h){const i=new Uint8Array(t,e);return{width:d,height:r,data:this.normalizePixelData(i,l),maxVal:l,type:"P5 (二进制)"}}{const s=i.decode(new Uint8Array(t,e)).trim().split(/\s+/).map(Number);return{width:d,height:r,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(){if(1==this.isNav){const t=this;n({title:"提示",content:"是否确认退出本次导航?",success:function(i){i.confirm&&(t.stopAnimation(),a({url:"/pages/index/index"}))}})}else a({url:"/pages/index/index"})},goTo(){const t=this.goIndex+1;t!=this.goToLineIndex&&(this.goToLineIndex=t,this.websocket&&this.isConnected?this.addLine.length>0&&t<this.addLine.length&&(this.isjl=!0,setTimeout((()=>{const i={code:0,cmd:1003,msg:"",data:{target_x:this.addLine[t].x,target_y:this.addLine[t].y,target_yaw:0}};console.log(i),this.websocket.send(JSON.stringify(i))}),800)):e({title:"服务连接异常,请检查",icon:"none"}))},loop(t){if(2==t)this.isLoop=!1;else{if(this.addLine.length<=1)return void e({title:"请至少选择两个站点",icon:"none"});this.isLoop=!0}}}},[["render",function(t,i,s,e,o,n){const a=p,S=l,L=P,k=w,_=T;return d(),h(S,{class:"containerDh"},{default:c((()=>[r(S,{class:"contentDh"},{default:c((()=>[r(S,{class:"line"},{default:c((()=>[r(a,{class:"item-logo lleft",src:C}),r(S,{class:"item-font"},{default:c((()=>[x("位置点")])),_:1})])),_:1})])),_:1}),r(S,{class:"content"},{default:c((()=>[r(L,{"canvas-id":"myCanvas",id:"myCanvas",class:"canvas"})])),_:1}),r(S,{class:"footer"},{default:c((()=>[r(S,{class:"cxsm"},{default:c((()=>[r(S,{class:"cxsm-ts"},{default:c((()=>[x(" 行驶路线:"),r(k,{class:"xs-font"},{default:c((()=>[x(g(o.sxTest),1)])),_:1}),0==o.isLoop?(d(),h(_,{key:0,class:"bottom-button-xh",onClick:i[0]||(i[0]=t=>n.loop(1))},{default:c((()=>[x("循环行驶")])),_:1})):m("",!0),1==o.isLoop?(d(),h(_,{key:1,class:"bottom-button-xh",onClick:i[1]||(i[1]=t=>n.loop(2))},{default:c((()=>[x("停止循环")])),_:1})):m("",!0)])),_:1}),r(S,{class:"line"},{default:c((()=>[r(a,{class:"logo",src:I}),r(a,{class:"jt",src:v}),(d(!0),f(u,null,y(o.addLine,((t,i)=>(d(),f(u,null,[r(S,{class:b({"line-itemd":o.goIndex>=i,"line-item":o.goIndex<i})},{default:c((()=>[x(g(t.name),1)])),_:2},1032,["class"]),i<o.addLine.length-1?(d(),h(a,{key:0,class:"jt",src:v})):m("",!0)],64)))),256))])),_:1})])),_:1}),1==o.isNav?(d(),h(_,{key:0,onClick:i[2]||(i[2]=t=>n.goHome()),class:"bottom-buttonx"},{default:c((()=>[x("退出导航")])),_:1})):m("",!0),2==o.isNav?(d(),h(_,{key:1,onClick:i[3]||(i[3]=t=>n.goHome()),class:"bottom-button"},{default:c((()=>[x("完成")])),_:1})):m("",!0)])),_:1})])),_:1})}],["__scopeId","data-v-ff25ca44"]]);export{L as default};
|