实现固定航线切换至手柄控制

main
许宏杰 4 weeks ago
parent 8d4b2d416f
commit 36bbef1ca6

@ -5,7 +5,8 @@
<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 } from "vue";
const { proxy } = getCurrentInstance();
let options = { let options = {
scene: { scene: {
center: { center: {
@ -59,36 +60,19 @@ let options = {
const params = reactive({ const params = reactive({
lat: 31.162232, lat: 31.162232,
lng: 120.734077, lng: 120.734077,
altitude: 3000, altitude: 100,
heading: 0, // heading: 0, //
pitch: 0, // pitch: 0, //
roll: 0, // roll: 0, //
correction: 1, correction: 1,
speed: 800, speed: 500,
}); });
const EARTH_RADIUS = 6371000;
let isUav = true
const DIRECTION = {
UP: "w",
DOWN: "s",
LEFT: "a",
RIGHT: "d",
SPEED_UP: "q",
SPEED_DOWN: "e",
};
const keyboardMap = { let isGamepad = false;
[DIRECTION.UP]: false,
[DIRECTION.DOWN]: false,
[DIRECTION.LEFT]: false,
[DIRECTION.RIGHT]: false,
[DIRECTION.SPEED_UP]: false,
[DIRECTION.SPEED_DOWN]: false,
};
let map = null; let map = null;
let list = []; let list = [];
let uav = null let uav = null;
const mapLayer = {}; const mapLayer = {};
const mapLoad = (mapInstance) => { const mapLoad = (mapInstance) => {
map = mapInstance; map = mapInstance;
@ -97,64 +81,101 @@ const mapLoad = (mapInstance) => {
allowDrillPick: true, // clickgraphics allowDrillPick: true, // clickgraphics
}); });
map.addLayer(mapLayer.markerLayer); map.addLayer(mapLayer.markerLayer);
// //
// initUav() mapLayer.uav = new mars3d.layer.GraphicLayer()
// // map.addLayer(mapLayer.uav);
// initRectangle();
// initMarker(); boostrapUav();
boostrapUav()
}; };
const boostrapUav = () => { onMounted(()=>{
initUav() onAddGamepadboardListener();
onAddKeyboardListener() })
const renderer = () => {
onAdjustParams(); /**
onAdjustAttitude(); * 监听游戏手柄
requestAnimationFrame(renderer); */
}; const onAddGamepadboardListener = () => {
renderer(); window.addEventListener("gamepadconnected", function (e) {
} var gp = navigator.getGamepads()[e.gamepad.index];
proxy.$modal.msgSuccess("手柄控制已启动");
//
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 renderer = () => {
startgamepad();
onAdjustAttitude();
requestAnimationFrame(renderer);
};
renderer();
//
const onAddKeyboardListener = () => {
document.addEventListener("keydown", (e) => {
if (Object.keys(keyboardMap).includes(e.key)) {
keyboardMap[e.key] = true;
}
}); });
document.addEventListener("keyup", (e) => { //
if (Object.keys(keyboardMap).includes(e.key)) { window.addEventListener("gamepaddisconnected", function (e) {
keyboardMap[e.key] = false; clearInterval(interval);
}
}); });
}; };
// const boostrapUav = () => {
const onAdjustParams = () => { // //
if (keyboardMap[DIRECTION.SPEED_UP]) { // initRectangle();
params.speed += 100; // //
// initMarker();
//
initUav();
};
const startgamepad = () => {
var gamepad = navigator.getGamepads()[0];
if (gamepad) {
//
pressKey(gamepad.buttons);
//
rocker(gamepad.axes);
} }
if (keyboardMap[DIRECTION.SPEED_DOWN]) { };
if (params.speed >= 500) { //
params.speed -= 100; const rocker = (axes) => {
const deadZone = 0.5;
const x = axes[0];
const y = axes[1];
// //
if (x < -deadZone) {
//
params.heading -= 0.115;
if (params.roll > -10) {
params.roll -= 0.115;
}
} else if (x > deadZone) {
//
params.heading += 0.115;
if (params.roll < 10) {
params.roll += 0.115;
} }
} }
// //
if (keyboardMap[DIRECTION.UP] && params.pitch <= 0.3) { if (y < -deadZone && params.pitch <= 0.3) {
params.pitch += 0.005; params.pitch += 0.015;
if (params.pitch > 0) { if (params.pitch > 0) {
const { speed, pitch } = params; const { speed, pitch } = params;
const temp = (params.speed / 60 / 60 / 60) * 110; const temp = (params.speed / 60 / 60 / 60) * 110;
//1110km //1110km
params.altitude += temp * Math.sin(pitch); params.altitude += temp * Math.sin(pitch);
} }
} } else if (y > deadZone && params.pitch >= -0.3) {
// //
if (keyboardMap[DIRECTION.DOWN] && params.pitch >= -0.3) { params.pitch -= 0.016;
params.pitch -= 0.006;
if (params.pitch < 0) { if (params.pitch < 0) {
const { speed, pitch } = params; const { speed, pitch } = params;
//1110km //1110km
@ -162,21 +183,24 @@ const onAdjustParams = () => {
params.altitude += temp * Math.sin(pitch); params.altitude += temp * Math.sin(pitch);
} }
} }
// reset();
if (keyboardMap[DIRECTION.LEFT]) { };
params.heading -= 0.115; //
if (params.roll > -10) { const pressKey = (buttons) => {
params.roll -= 0.115; //
} if (buttons[12] && buttons[12].pressed) {
params.speed += 100;
} }
// //
if (keyboardMap[DIRECTION.RIGHT]) { if (buttons[13] && buttons[13].pressed) {
params.heading += 0.115; if (params.speed >= 500) {
if (params.roll < 10) { params.speed -= 100;
params.roll += 0.115;
} }
} }
reset()
};
const reset = () => {
const { heading, pitch, roll } = params; const { heading, pitch, roll } = params;
const { abs, cos } = Math; const { abs, cos } = Math;
params.correction = abs(cos(heading) * cos(pitch)); params.correction = abs(cos(heading) * cos(pitch));
@ -200,26 +224,24 @@ const onAdjustAttitude = () => {
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,
roll roll,
}); });
uav.addTimePosition(position); uav.addTimePosition(position);
}; };
const initUav = () => { const initUav = () => {
if (list.length == 0) { if (list.length == 0 && !isGamepad ) {
PointJson.map((item) => { PointJson.map((item) => {
list.push(item.wz.split(",").map(Number)); const position = item.wz.split(",").map(Number)
position[2] = 100
list.push(position);
}); });
} }
uav = new mars3d.graphic.Route({
uav = new mars3d.graphic.Route({ id: "uav1",
id: "uav",
name: "无人机模型", name: "无人机模型",
position: { position: {
type: "time", // type: "time", //
@ -232,14 +254,13 @@ const initUav = () => {
minimumPixelSize: 0.1, minimumPixelSize: 0.1,
runAnimations: true, runAnimations: true,
// mergeOrientation: true, // mergeOrientation: true,
}, },
camera: { camera: {
type: "gs", type: "gs",
}, },
}); });
mapLayer.markerLayer.addGraphic(uav); mapLayer.uav.addGraphic(uav);
}; };
const initRectangle = () => { const initRectangle = () => {

@ -1,94 +0,0 @@
<template>
<div class="panel-box panel-left">
<view-title height="50%" title="无人机信息">
<!-- <model-collada
:backgroundAlpha="0"
:rotation="{
x: - Math.PI / 2,
y: 0,
z: 0,
}"
src="../../../assets/images/uav-model.dae"
/> -->
</view-title>
<view-title height="50%" title="无人机实时状态">
<!-- 内容区 -->
<div class="contain-grid">
<div v-for="item in gridItems" :key="item.id" class="grid-item">
<div class="grid-item-content">{{ item.content }}</div>
<div class="grid-item-footer">{{ item.footer }}</div>
</div>
</div>
</view-title>
<!-- 底部 -->
<div class="contain-foot">
<div class="itemone">
<div><span>经度:</span>: <span>31</span></div>
</div>
<div class="itemone">
<div><span>纬度:</span> <span>31</span></div>
</div>
<div class="itemtwo">
<div><span>经度</span>: <span>31</span></div>
</div>
</div>
</div>
</template>
<script setup>
import { ViewTitle } from "@/views/components/map";
// import { ModelCollada } from 'vue-3d-model';
//
const gridItems = [
{ id: 1, content: '-20', footer: '经度(°)' },
{ id: 2, content: '20', footer: '纬度(°)' },
{ id: 3, content: '20', footer: '高度(m)' },
{ id: 4, content: '20', footer: '偏航角(°)' },
{ id: 5, content: '20', footer: '水平速度(°)' },
{ id: 6, content: '20', footer: '垂直速度(m/s)' },
{ id: 7, content: '20', footer: '电量(%)' },
{ id: 8, content: '20', footer: '电池温度(°c)' },
];
</script>
<style scoped>
.panel-left {
display: flex;
flex-direction: column;
}
.contain-grid {
height: 100%;
overflow: auto;
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 10px;
}
.grid-item {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
/* background-color: #fff; */
border:1px solid #ccc;
}
.contain-foot{
padding: .3rem 1rem;
position: absolute;
width: 79%;
margin-left: -4.4%;
background-color: #05192A;
bottom: 2.4%;
display: flex;
justify-content: space-between;
}
.itemone{
display: flex;
}
.itemone span:first-child{
color:#BCDEFC
}
</style>

@ -1,7 +1,7 @@
<template> <template>
<div class="panel-box panel-left"> <div class="panel-box panel-left">
<view-title height="50%" title="无人机信息"> <view-title height="50%" title="无人机信息">
<model-collada <!-- <model-collada
@load="onLoad" @load="onLoad"
src="src/assets/images/uav-model.dae" src="src/assets/images/uav-model.dae"
:rotation="rotation" :rotation="rotation"
@ -14,7 +14,7 @@
enableZoom: false, // enableZoom: false, //
enablePan: false // enablePan: false //
}" }"
/> /> -->
</view-title> </view-title>
<view-title height="50%" title="无人机实时状态"> <view-title height="50%" title="无人机实时状态">
<!-- 内容区 --> <!-- 内容区 -->

@ -151,7 +151,7 @@ const startgamepad = () => {
var gamepad = navigator.getGamepads()[0] var gamepad = navigator.getGamepads()[0]
if (gamepad) { if (gamepad) {
// //
// pressKey(gamepad!.buttons) pressKey(gamepad.buttons)
// //
rocker(gamepad.axes) rocker(gamepad.axes)
} }

@ -2,7 +2,7 @@
<div> <div>
<map-container> <map-container>
<template #map> <template #map>
<!-- <mars-map></mars-map> --> <mars-map></mars-map>
</template> </template>
<template #left> <template #left>
<panel-left></panel-left> <panel-left></panel-left>

Loading…
Cancel
Save