无人机模块功能完善完毕

main
许宏杰 4 weeks ago
parent 4a76d2bebe
commit 420a11cda0

@ -29,6 +29,7 @@
"js-beautify": "1.14.11", "js-beautify": "1.14.11",
"js-cookie": "3.0.5", "js-cookie": "3.0.5",
"jsencrypt": "3.3.2", "jsencrypt": "3.3.2",
"moment": "^2.30.1",
"nprogress": "0.2.0", "nprogress": "0.2.0",
"pinia": "2.1.7", "pinia": "2.1.7",
"splitpanes": "3.1.5", "splitpanes": "3.1.5",

@ -5,8 +5,11 @@
<script setup> <script setup>
import marsMap from "@/components/marsMap"; import marsMap from "@/components/marsMap";
import PointJson from "./point.json"; import PointJson from "./point.json";
import { onMounted,nextTick } from "vue"; import { onMounted, ref, reactive, watch, onUnmounted } from "vue";
import moment from "moment";
const { proxy } = getCurrentInstance(); const { proxy } = getCurrentInstance();
let options = { let options = {
scene: { scene: {
center: { center: {
@ -68,17 +71,44 @@ const params = reactive({
speed: 500, speed: 500,
}); });
// 使 import.meta.glob let currentTimestampInSeconds = ref(0);
const imageModules = import.meta.glob('@/assets/images/*.png', { eager: true }); let isGamepad = ref(false);
let animationFrameId;
let gamepad = reactive({});
// 使 import.meta.glob
const imageModules = import.meta.glob("@/assets/images/*.png", { eager: true });
// //
const imageUrls = Object.values(imageModules).map((module) => module.default); const imageUrls = Object.values(imageModules).map((module) => module.default);
let isGamepad = false;
let map = null; let map = null;
let list = []; let list = [];
let uav = null; let uav = null;
const mapLayer = {}; const mapLayer = {};
// 5
const intervalId = setInterval(() => {
const currentTimestamp = moment().unix();
// moment
const moment1 = moment.unix(currentTimestamp);
const moment2 = moment.unix(currentTimestampInSeconds.value);
//
const diffInMinutes = Math.abs(moment1.diff(moment2, "minutes"));
if (diffInMinutes === 1 && isGamepad.value) {
isGamepad.value = false;
currentTimestampInSeconds.value = 0
params.altitude = 100
proxy.$modal.msgSuccess("巡航模式已启动");
map.flyToPoint(new mars3d.LngLatPoint(120.657967, 31.048193));
uav = null;
mapLayer.uav.clear(); //
setTimeout(() => {
initUav(); //线
}, 3000);
}
}, 5000);
const mapLoad = (mapInstance) => { const mapLoad = (mapInstance) => {
map = mapInstance; map = mapInstance;
//marker //marker
@ -87,53 +117,70 @@ const mapLoad = (mapInstance) => {
}); });
map.addLayer(mapLayer.markerLayer); map.addLayer(mapLayer.markerLayer);
// //
mapLayer.uav = new mars3d.layer.GraphicLayer() mapLayer.uav = new mars3d.layer.GraphicLayer();
map.addLayer(mapLayer.uav); map.addLayer(mapLayer.uav);
map.on(mars3d.EventType.load, function (event) { map.on(mars3d.EventType.load, function (event) {
onAddGamepadboardListener();
boostrapUav(); boostrapUav();
}) });
};
};
onMounted(()=>{ //
onAddGamepadboardListener(); onUnmounted(() => {
}) clearInterval(intervalId);
if(animationFrameId){
cancelAnimationFrame(animationFrameId);
}
window.removeEventListener("gamepadconnected", handleGamepadConnected);
window.removeEventListener("gamepaddisconnected", handleGamepadDisconnected);
});
/** /**
* 监听游戏手柄 * 监听游戏手柄
*/ */
const onAddGamepadboardListener = () => { const onAddGamepadboardListener = () => {
window.addEventListener("gamepadconnected", function (e) { window.addEventListener("gamepadconnected", handleGamepadConnected);
// var gp = navigator.getGamepads()[e.gamepad.index]; //
proxy.$modal.msgSuccess("手柄控制已启动"); window.addEventListener("gamepaddisconnected", handleGamepadDisconnected);
// 线 };
const uavItem = mapLayer.uav.getGraphicById('uav1'); const handleGamepadDisconnected = () => {
const {heading,pitch,roll,position } = uavItem console.log("断开");
const cartographic = mars3d.LngLatPoint.fromCartesian(position) };
params.lat =cartographic._lat
params.lng=cartographic._lng const handleGamepadConnected = (e) => {
params.heading=heading
params.pitch=pitch
params.roll=roll
handleUavDataLast();
// //
const renderer = () => { const renderer = () => {
startgamepad(); startgamepad();
if(isGamepad.value){
onAdjustAttitude(); onAdjustAttitude();
requestAnimationFrame(renderer); }
animationFrameId = requestAnimationFrame(renderer);
}; };
renderer(); renderer();
}); };
//
window.addEventListener("gamepaddisconnected", function (e) { const handleUavDataLast = () => {
clearInterval(interval); proxy.$modal.msgSuccess("手柄控制已启动");
}); isGamepad.value = true;
// 线
const uavItem = mapLayer.uav.getGraphicById("uav1");
const { heading, pitch, roll, position } = uavItem;
const cartographic = mars3d.LngLatPoint.fromCartesian(position);
params.lat = cartographic._lat;
params.lng = cartographic._lng;
params.heading = heading;
params.pitch = pitch;
params.roll = roll;
}; };
const boostrapUav = () => { const boostrapUav = () => {
@ -146,27 +193,36 @@ const boostrapUav = () => {
}; };
const startgamepad = () => { const startgamepad = () => {
var gamepad = navigator.getGamepads()[0]; gamepad = navigator.getGamepads()[0];
if (gamepad) { if (gamepad) {
// //
pressKey(gamepad.buttons); // pressKey(gamepad.buttons);
// //
rocker(gamepad.axes); rocker();
} }
}; };
// //
const rocker = (axes) => { const rocker = () => {
const axes = gamepad.axes;
const deadZone = 0.5; const deadZone = 0.5;
const x = axes[0]; const x = axes[0];
const y = axes[1]; const y = axes[1];
// //
//
if (x < -deadZone) { if (x < -deadZone) {
if(!isGamepad.value){
console.log('左')
}
handleUavTime();
// //
params.heading -= 0.115; params.heading -= 0.115;
if (params.roll > -10) { if (params.roll > -10) {
params.roll -= 0.115; params.roll -= 0.115;
} }
} else if (x > deadZone) { } else if (x > deadZone) {
handleUavTime();
// //
params.heading += 0.115; params.heading += 0.115;
if (params.roll < 10) { if (params.roll < 10) {
@ -175,6 +231,7 @@ const rocker = (axes) => {
} }
// //
if (y < -deadZone && params.pitch <= 0.3) { if (y < -deadZone && params.pitch <= 0.3) {
handleUavTime();
params.pitch += 0.015; params.pitch += 0.015;
if (params.pitch > 0) { if (params.pitch > 0) {
const { speed, pitch } = params; const { speed, pitch } = params;
@ -183,6 +240,7 @@ const rocker = (axes) => {
params.altitude += temp * Math.sin(pitch); params.altitude += temp * Math.sin(pitch);
} }
} else if (y > deadZone && params.pitch >= -0.3) { } else if (y > deadZone && params.pitch >= -0.3) {
handleUavTime();
// //
params.pitch -= 0.016; params.pitch -= 0.016;
if (params.pitch < 0) { if (params.pitch < 0) {
@ -198,15 +256,29 @@ const rocker = (axes) => {
const pressKey = (buttons) => { const pressKey = (buttons) => {
// //
if (buttons[12] && buttons[12].pressed) { if (buttons[12] && buttons[12].pressed) {
handleUavTime();
params.speed += 100; params.speed += 100;
} }
// //
if (buttons[13] && buttons[13].pressed) { if (buttons[13] && buttons[13].pressed) {
handleUavTime();
if (params.speed >= 500) { if (params.speed >= 500) {
params.speed -= 100; params.speed -= 100;
} }
} }
reset() reset();
};
//
const handleUavTime = () => {
if(!isGamepad.value){
handleUavDataLast()
return
}
currentTimestampInSeconds.value = moment().unix();
}; };
const reset = () => { const reset = () => {
@ -227,12 +299,14 @@ const reset = () => {
// 姿/ // 姿/
const onAdjustAttitude = () => { const onAdjustAttitude = () => {
if (!uav) return;
const temp = params.speed / 60 / 60 / 60 / 110; const temp = params.speed / 60 / 60 / 60 / 110;
params.lng += temp * Math.cos(Cesium.Math.toRadians(params.heading)); params.lng += temp * Math.cos(Cesium.Math.toRadians(params.heading));
params.lat -= temp * Math.sin(Cesium.Math.toRadians(params.heading)); params.lat -= temp * Math.sin(Cesium.Math.toRadians(params.heading));
const { lng, lat, altitude, heading, pitch, roll } = params; const { lng, lat, altitude, heading, pitch, roll } = params;
params.altitude += temp * Math.sin(pitch) * 110 * 1000 * 10; params.altitude += temp * Math.sin(pitch) * 110 * 1000 * 10;
const position = Cesium.Cartesian3.fromDegrees(lng, lat, altitude); const position = Cesium.Cartesian3.fromDegrees(lng, lat, altitude);
uav.model.setStyle({ uav.model.setStyle({
heading, heading,
pitch, pitch,
@ -242,9 +316,7 @@ const onAdjustAttitude = () => {
}; };
const initUav = () => { const initUav = () => {
if (list.length == 0 && !isGamepad ) {
list = PointJson.map((item) => item.wz); list = PointJson.map((item) => item.wz);
}
uav = new mars3d.graphic.FixedRoute({ uav = new mars3d.graphic.FixedRoute({
id: "uav1", id: "uav1",
name: "无人机模型", name: "无人机模型",
@ -264,16 +336,16 @@ const initUav = () => {
polyline: { polyline: {
materialType: mars3d.MaterialType.PolylineDash, //线 materialType: mars3d.MaterialType.PolylineDash, //线
materialOptions: { materialOptions: {
color: 'rgba(34, 232, 174, 1)', color: "rgba(34, 232, 174, 1)",
dashLength: 8.0 dashLength: 8.0,
} },
}, },
camera: { camera: {
type: "gs", type: "gs",
}, },
}); });
mapLayer.uav.addGraphic(uav); mapLayer.uav.addGraphic(uav);
uav.start() uav.start();
}; };
const initRectangle = () => { const initRectangle = () => {
@ -298,9 +370,8 @@ const initRectangle = () => {
* 渲染所有点位 * 渲染所有点位
*/ */
const initMarker = () => { const initMarker = () => {
console.log(imageModules) console.log(imageModules);
PointJson.map((item, index) => { PointJson.map((item, index) => {
// //
lablePoint(item, index); lablePoint(item, index);
// //
@ -319,8 +390,20 @@ const lablePoint = (item, index) => {
style: { style: {
html: ` html: `
<div class="div-marker"> <div class="div-marker">
<div class="marker-name ${item.type == 'school' ? 'color-school' : item.type == 'park' ? 'color-park' :'color-uav' } ">${item.name}</div> <div class="marker-name ${
<img class="marker-icon" src="${item.type == 'school' ? imageUrls[5] : item.type == 'park' ? imageUrls[4] :imageUrls[6] }" /> item.type == "school"
? "color-school"
: item.type == "park"
? "color-park"
: "color-uav"
} ">${item.name}</div>
<img class="marker-icon" src="${
item.type == "school"
? imageUrls[5]
: item.type == "park"
? imageUrls[4]
: imageUrls[6]
}" />
</div> </div>
`, `,
offsetY: -73, offsetY: -73,
@ -345,7 +428,12 @@ const circlePoint = (item, index) => {
height: 0, height: 0,
materialType: mars3d.MaterialType.CircleWave, materialType: mars3d.MaterialType.CircleWave,
materialOptions: { materialOptions: {
color: item.type == 'school' ? '#59F9FF' : item.type == 'park' ? '#33E064' : '#22E8AE', color:
item.type == "school"
? "#59F9FF"
: item.type == "park"
? "#33E064"
: "#22E8AE",
count: 2, count: 2,
speed: 6, speed: 6,
}, },

Loading…
Cancel
Save