Skip to content

Commit

Permalink
joystick-protocol: Adapt consumers to new structure
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaellehmkuhl committed Dec 7, 2023
1 parent 5b0840c commit ab39b69
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 46 deletions.
16 changes: 13 additions & 3 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'))
Expand Down Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions src/libs/vehicle/ardupilot/ardupilot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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'
Expand Down Expand Up @@ -509,10 +508,10 @@ export abstract class ArduPilotVehicle<Modes> extends Vehicle.AbstractVehicle<Mo

/**
* Send manual control
* @param {'ProtocolControllerState'} controllerState Current state of the controller
* @param {'MavlinkManualControlState'} controllerState Current state of the controller
*/
sendManualControl(controllerState: ProtocolControllerState): void {
const state = controllerState as MavlinkControllerState
sendManualControl(controllerState: MavlinkManualControlState): void {
const state = controllerState as MavlinkManualControlState
const manualControlMessage: Message.ManualControl = {
type: MAVLinkType.MANUAL_CONTROL,
x: state.x,
Expand Down
11 changes: 10 additions & 1 deletion src/stores/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@ import { ref } from 'vue'
import { availableGamepadToCockpitMaps, cockpitStandardToProtocols } from '@/assets/joystick-profiles'
import { type JoystickEvent, EventType, joystickManager, JoystickModel } from '@/libs/joystick/manager'
import { allAvailableAxes, allAvailableButtons } from '@/libs/joystick/protocols'
import { type JoystickState, type ProtocolControllerMapping, Joystick } from '@/types/joystick'
import { modifierKeyActions, otherAvailableActions } from '@/libs/joystick/protocols/other'
import {
type JoystickProtocolActionsMapping,
type JoystickState,
type ProtocolAction,
CockpitModifierKeyOption,
Joystick,
JoystickButton,
JoystickProtocol,
} from '@/types/joystick'

export type controllerUpdateCallback = (state: JoystickState, protocolMapping: ProtocolControllerMapping) => void

Expand Down
55 changes: 21 additions & 34 deletions src/stores/mainVehicle.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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'
Expand All @@ -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'
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -308,38 +293,40 @@ 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)
})
})

const controllerStore = useControllerStore()
const currentControllerState = ref<JoystickState>()
const currentProtocolMapping = ref<ProtocolControllerMapping>()
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)

Expand Down
16 changes: 13 additions & 3 deletions src/stores/widgetManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -455,15 +459,21 @@ 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 => {
const newIndex = currentViewIndex.value === 0 ? currentProfile.value.views.length - 1 : currentViewIndex.value - 1
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
Expand Down

0 comments on commit ab39b69

Please sign in to comment.