Skip to content

Commit 15b84d2

Browse files
joystick: Auto load joystick profile based on vehicle type
1 parent 3d374a2 commit 15b84d2

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/stores/controller.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
defaultProtocolMappingVehicleCorrespondency,
1212
} from '@/assets/joystick-profiles'
1313
import { getKeyDataFromCockpitVehicleStorage, setKeyDataOnCockpitVehicleStorage } from '@/libs/blueos'
14+
import { MavType } from '@/libs/connection/m2r/messages/mavlink2rest-enum'
1415
import { type JoystickEvent, EventType, joystickManager, JoystickModel } from '@/libs/joystick/manager'
1516
import { allAvailableAxes, allAvailableButtons } from '@/libs/joystick/protocols'
1617
import { modifierKeyActions, otherAvailableActions } from '@/libs/joystick/protocols/other'
@@ -340,6 +341,17 @@ export const useControllerStore = defineStore('controller', () => {
340341
mapping.hash = correspondentDefault?.hash ?? uuid4()
341342
})
342343

344+
const loadDefaultProtocolMappingForVehicle = (vehicleType: MavType): void => {
345+
// @ts-ignore: We know that the value is a string
346+
const defaultMappingHash = vehicleTypeProtocolMappingCorrespondency.value[vehicleType]
347+
const defaultProtocolMapping = cockpitStandardToProtocols.find((mapping) => mapping.hash === defaultMappingHash)
348+
if (!defaultProtocolMapping) {
349+
throw new Error('Could not find default mapping for this vehicle.')
350+
}
351+
352+
loadProtocolMapping(defaultProtocolMapping)
353+
}
354+
343355
return {
344356
registerControllerUpdateCallback,
345357
enableForwarding,
@@ -360,5 +372,6 @@ export const useControllerStore = defineStore('controller', () => {
360372
importFunctionsMapping,
361373
exportFunctionsMappingToVehicle,
362374
importFunctionsMappingFromVehicle,
375+
loadDefaultProtocolMappingForVehicle,
363376
}
364377
})

src/stores/mainVehicle.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ class CustomizableParameter<T> {
8585
}
8686

8787
export const useMainVehicleStore = defineStore('main-vehicle', () => {
88+
const controllerStore = useControllerStore()
89+
8890
const cpuLoad = ref<number>()
8991
const globalAddress = useStorage('cockpit-vehicle-address', defaultGlobalAddress)
9092
const _mainConnectionURI = new CustomizableParameter<Connection.URI>(() => {
@@ -398,8 +400,19 @@ export const useMainVehicleStore = defineStore('main-vehicle', () => {
398400

399401
const heartbeat = pack.message as Message.Heartbeat
400402
firmwareType.value = heartbeat.autopilot.type
403+
const oldVehicleType = vehicleType.value
401404
vehicleType.value = heartbeat.mavtype.type
402405
lastHeartbeat.value = new Date()
406+
407+
if (oldVehicleType !== vehicleType.value && vehicleType.value !== undefined) {
408+
console.log('Vehicle type changed to', vehicleType.value)
409+
try {
410+
controllerStore.loadDefaultProtocolMappingForVehicle(vehicleType.value)
411+
console.info(`Loaded default joystick protocol mapping for vehicle type ${vehicleType.value}.`)
412+
} catch (error) {
413+
console.error(`Could not load default protocol mapping for vehicle type ${vehicleType.value}: ${error}`)
414+
}
415+
}
403416
})
404417
// eslint-disable-next-line @typescript-eslint/no-explicit-any
405418
getAutoPilot(vehicles).onMode.add((vehicleMode: any) => {
@@ -430,7 +443,6 @@ export const useMainVehicleStore = defineStore('main-vehicle', () => {
430443
registerActionCallback(availableCockpitActions.mavlink_disarm, disarm)
431444
})
432445

433-
const controllerStore = useControllerStore()
434446
const mavlinkManualControlManager = new MavlinkManualControlManager()
435447
const cockpitActionsManager = new CockpitActionsManager()
436448
controllerStore.registerControllerUpdateCallback(mavlinkManualControlManager.updateControllerData)

0 commit comments

Comments
 (0)