From ab39b69ef198b56f0693a3a0e607419c67d367e4 Mon Sep 17 00:00:00 2001 From: Rafael Araujo Lehmkuhl Date: Wed, 6 Dec 2023 21:41:21 -0300 Subject: [PATCH] joystick-protocol: Adapt consumers to new structure --- src/App.vue | 16 +++++-- src/libs/vehicle/ardupilot/ardupilot.ts | 9 ++-- src/stores/controller.ts | 11 ++++- src/stores/mainVehicle.ts | 55 ++++++++++--------------- src/stores/widgetManager.ts | 16 +++++-- 5 files changed, 61 insertions(+), 46 deletions(-) diff --git a/src/App.vue b/src/App.vue index 04b9aaea8..bb3959cc0 100644 --- a/src/App.vue +++ b/src/App.vue @@ -148,7 +148,11 @@ import { useRoute } from 'vue-router' import ConfigurationMenu from '@/components/ConfigurationMenu.vue' import { coolMissionNames } from '@/libs/funny-name/words' -import { CockpitAction, registerActionCallback, unregisterActionCallback } from '@/libs/joystick/protocols' +import { + availableCockpitActions, + registerActionCallback, + unregisterActionCallback, +} from '@/libs/joystick/protocols/cockpit-actions' import { useMissionStore } from '@/stores/mission' import Dialog from './components/Dialog.vue' @@ -175,7 +179,10 @@ const routerSection = ref() const { isFullscreen, toggle: toggleFullscreen } = useFullscreen() const debouncedToggleFullScreen = useDebounceFn(() => toggleFullscreen(), 10) -const fullScreenCallbackId = registerActionCallback(CockpitAction.TOGGLE_FULL_SCREEN, debouncedToggleFullScreen) +const fullScreenCallbackId = registerActionCallback( + availableCockpitActions.toggle_full_screen, + debouncedToggleFullScreen +) onBeforeUnmount(() => unregisterActionCallback(fullScreenCallbackId)) const fullScreenToggleIcon = computed(() => (isFullscreen.value ? 'mdi-fullscreen-exit' : 'mdi-overscan')) @@ -209,7 +216,10 @@ watch([() => widgetStore.currentView, () => widgetStore.currentView.showBottomBa showBottomBarNow.value = widgetStore.currentView.showBottomBarOnBoot }) const debouncedToggleBottomBar = useDebounceFn(() => (showBottomBarNow.value = !showBottomBarNow.value), 25) -const bottomBarToggleCallbackId = registerActionCallback(CockpitAction.TOGGLE_BOTTOM_BAR, debouncedToggleBottomBar) +const bottomBarToggleCallbackId = registerActionCallback( + availableCockpitActions.toggle_bottom_bar, + debouncedToggleBottomBar +) onBeforeUnmount(() => unregisterActionCallback(bottomBarToggleCallbackId)) // Start datalogging diff --git a/src/libs/vehicle/ardupilot/ardupilot.ts b/src/libs/vehicle/ardupilot/ardupilot.ts index 7b75820dc..2915defab 100644 --- a/src/libs/vehicle/ardupilot/ardupilot.ts +++ b/src/libs/vehicle/ardupilot/ardupilot.ts @@ -20,7 +20,7 @@ import { } from '@/libs/connection/m2r/messages/mavlink2rest-enum' import { MavFrame } from '@/libs/connection/m2r/messages/mavlink2rest-enum' import { type Message } from '@/libs/connection/m2r/messages/mavlink2rest-message' -import { MavlinkControllerState } from '@/libs/joystick/protocols' +import { type MavlinkManualControlState } from '@/libs/joystick/protocols/mavlink-manual-control' import { SignalTyped } from '@/libs/signal' import { round } from '@/libs/utils' import { @@ -43,7 +43,6 @@ import { Velocity, } from '@/libs/vehicle/types' import type { MetadataFile } from '@/types/ardupilot-metadata' -import { ProtocolControllerState } from '@/types/joystick' import { type MissionLoadingCallback, type Waypoint, defaultLoadingCallback } from '@/types/mission' import * as Vehicle from '../vehicle' @@ -509,10 +508,10 @@ export abstract class ArduPilotVehicle extends Vehicle.AbstractVehicle void diff --git a/src/stores/mainVehicle.ts b/src/stores/mainVehicle.ts index 38fda83bb..3da88b297 100644 --- a/src/stores/mainVehicle.ts +++ b/src/stores/mainVehicle.ts @@ -1,6 +1,6 @@ import { useStorage, useTimestamp } from '@vueuse/core' import { defineStore } from 'pinia' -import { capitalize, computed, onBeforeUnmount, reactive, ref, watch } from 'vue' +import { computed, onBeforeUnmount, reactive, ref, watch } from 'vue' import { defaultGlobalAddress } from '@/assets/defaults' import * as Connection from '@/libs/connection/connection' @@ -9,13 +9,12 @@ import type { Package } from '@/libs/connection/m2r/messages/mavlink2rest' import { MavAutopilot, MAVLinkType, MavType } from '@/libs/connection/m2r/messages/mavlink2rest-enum' import type { Message } from '@/libs/connection/m2r/messages/mavlink2rest-message' import { - type InputWithPrettyName, - CockpitAction, - MavlinkControllerState, + availableCockpitActions, + CockpitActionsManager, registerActionCallback, - sendCockpitActions, unregisterActionCallback, -} from '@/libs/joystick/protocols' +} from '@/libs/joystick/protocols/cockpit-actions' +import { MavlinkManualControlManager } from '@/libs/joystick/protocols/mavlink-manual-control' import type { ArduPilot } from '@/libs/vehicle/ardupilot/ardupilot' import type { ArduPilotParameterSetData } from '@/libs/vehicle/ardupilot/types' import * as Protocol from '@/libs/vehicle/protocol/protocol' @@ -32,12 +31,6 @@ import type { } from '@/libs/vehicle/types' import * as Vehicle from '@/libs/vehicle/vehicle' import { VehicleFactory } from '@/libs/vehicle/vehicle-factory' -import { - type JoystickState, - type ProtocolControllerMapping, - JoystickProtocol, - ProtocolControllerState, -} from '@/types/joystick' import type { MissionLoadingCallback, Waypoint } from '@/types/mission' import { useControllerStore } from './controller' @@ -154,14 +147,6 @@ export const useMainVehicleStore = defineStore('main-vehicle', () => { } } - /** - * Send manual control message - * @param {ProtocolControllerState} controllerState Current state of the controller - */ - function sendManualControl(controllerState: ProtocolControllerState): void { - mainVehicle.value?.sendManualControl(controllerState) - } - /** * Send heartbeat from GCS */ @@ -308,8 +293,8 @@ export const useMainVehicleStore = defineStore('main-vehicle', () => { }, setFlightMode: setFlightMode, } - const mavlinkArmId = registerActionCallback(CockpitAction.MAVLINK_ARM, arm) - const mavlinkDisarmId = registerActionCallback(CockpitAction.MAVLINK_DISARM, disarm) + const mavlinkArmId = registerActionCallback(availableCockpitActions.mavlink_arm, arm) + const mavlinkDisarmId = registerActionCallback(availableCockpitActions.mavlink_disarm, disarm) onBeforeUnmount(() => { unregisterActionCallback(mavlinkArmId) unregisterActionCallback(mavlinkDisarmId) @@ -317,29 +302,31 @@ export const useMainVehicleStore = defineStore('main-vehicle', () => { }) const controllerStore = useControllerStore() - const currentControllerState = ref() - const currentProtocolMapping = ref() - const updateCurrentControllerState = (newState: JoystickState, newMapping: ProtocolControllerMapping): void => { - currentControllerState.value = newState - currentProtocolMapping.value = newMapping - } - controllerStore.registerControllerUpdateCallback(updateCurrentControllerState) + const mavlinkManualControlManager = new MavlinkManualControlManager() + const cockpitActionsManager = new CockpitActionsManager() + controllerStore.registerControllerUpdateCallback(mavlinkManualControlManager.updateControllerData) + controllerStore.registerControllerUpdateCallback(cockpitActionsManager.updateControllerData) // Loop to send MAVLink Manual Control messages setInterval(() => { - if (!currentControllerState.value || !currentProtocolMapping.value || controllerStore.joysticks.size === 0) return - const newControllerState = new MavlinkControllerState(currentControllerState.value, currentProtocolMapping.value) + if (!mainVehicle.value) return + + // Set the manager vehicle instance if yet undefined + if (mavlinkManualControlManager.vehicle === undefined) { + mavlinkManualControlManager.setVehicle(mainVehicle.value as ArduPilot) + } + + // Send MAVLink Manual Control message if (controllerStore.enableForwarding) { - sendManualControl(newControllerState) + mavlinkManualControlManager.sendManualControl() } }, 40) setInterval(() => sendGcsHeartbeat(), 1000) // Loop to send Cockpit Action messages setInterval(() => { - if (!currentControllerState.value || !currentProtocolMapping.value || controllerStore.joysticks.size === 0) return if (controllerStore.enableForwarding) { - sendCockpitActions(currentControllerState.value, currentProtocolMapping.value) + cockpitActionsManager.sendCockpitActions() } }, 10) diff --git a/src/stores/widgetManager.ts b/src/stores/widgetManager.ts index f38c43af6..7313b4186 100644 --- a/src/stores/widgetManager.ts +++ b/src/stores/widgetManager.ts @@ -11,7 +11,11 @@ import { widgetProfiles } from '@/assets/defaults' import { miniWidgetsProfile } from '@/assets/defaults' import { getKeyDataFromCockpitVehicleStorage, setKeyDataOnCockpitVehicleStorage } from '@/libs/blueos' import * as Words from '@/libs/funny-name/words' -import { CockpitAction, registerActionCallback, unregisterActionCallback } from '@/libs/joystick/protocols' +import { + availableCockpitActions, + registerActionCallback, + unregisterActionCallback, +} from '@/libs/joystick/protocols/cockpit-actions' import { isEqual } from '@/libs/utils' import type { Point2D, SizeRect2D } from '@/types/general' import type { MiniWidget, MiniWidgetContainer } from '@/types/miniWidgets' @@ -455,7 +459,10 @@ export const useWidgetManagerStore = defineStore('widget-manager', () => { selectView(currentProfile.value.views[newIndex]) } const debouncedSelectNextView = useDebounceFn(() => selectNextView(), 10) - const selectNextViewCallbackId = registerActionCallback(CockpitAction.GO_TO_NEXT_VIEW, debouncedSelectNextView) + const selectNextViewCallbackId = registerActionCallback( + availableCockpitActions.go_to_next_view, + debouncedSelectNextView + ) onBeforeUnmount(() => unregisterActionCallback(selectNextViewCallbackId)) const selectPreviousView = (): void => { @@ -463,7 +470,10 @@ export const useWidgetManagerStore = defineStore('widget-manager', () => { selectView(currentProfile.value.views[newIndex]) } const debouncedSelectPreviousView = useDebounceFn(() => selectPreviousView(), 10) - const selectPrevViewCBId = registerActionCallback(CockpitAction.GO_TO_PREVIOUS_VIEW, debouncedSelectPreviousView) + const selectPrevViewCBId = registerActionCallback( + availableCockpitActions.go_to_previous_view, + debouncedSelectPreviousView + ) onBeforeUnmount(() => unregisterActionCallback(selectPrevViewCBId)) // Profile migrations