Skip to content

Commit

Permalink
joystick: Auto load joystick profile based on vehicle type
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaellehmkuhl committed Apr 9, 2024
1 parent 3d374a2 commit 15b84d2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/stores/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
defaultProtocolMappingVehicleCorrespondency,
} from '@/assets/joystick-profiles'
import { getKeyDataFromCockpitVehicleStorage, setKeyDataOnCockpitVehicleStorage } from '@/libs/blueos'
import { MavType } from '@/libs/connection/m2r/messages/mavlink2rest-enum'
import { type JoystickEvent, EventType, joystickManager, JoystickModel } from '@/libs/joystick/manager'
import { allAvailableAxes, allAvailableButtons } from '@/libs/joystick/protocols'
import { modifierKeyActions, otherAvailableActions } from '@/libs/joystick/protocols/other'
Expand Down Expand Up @@ -340,6 +341,17 @@ export const useControllerStore = defineStore('controller', () => {
mapping.hash = correspondentDefault?.hash ?? uuid4()
})

const loadDefaultProtocolMappingForVehicle = (vehicleType: MavType): void => {
// @ts-ignore: We know that the value is a string
const defaultMappingHash = vehicleTypeProtocolMappingCorrespondency.value[vehicleType]
const defaultProtocolMapping = cockpitStandardToProtocols.find((mapping) => mapping.hash === defaultMappingHash)
if (!defaultProtocolMapping) {
throw new Error('Could not find default mapping for this vehicle.')
}

loadProtocolMapping(defaultProtocolMapping)
}

return {
registerControllerUpdateCallback,
enableForwarding,
Expand All @@ -360,5 +372,6 @@ export const useControllerStore = defineStore('controller', () => {
importFunctionsMapping,
exportFunctionsMappingToVehicle,
importFunctionsMappingFromVehicle,
loadDefaultProtocolMappingForVehicle,
}
})
14 changes: 13 additions & 1 deletion src/stores/mainVehicle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ class CustomizableParameter<T> {
}

export const useMainVehicleStore = defineStore('main-vehicle', () => {
const controllerStore = useControllerStore()

const cpuLoad = ref<number>()
const globalAddress = useStorage('cockpit-vehicle-address', defaultGlobalAddress)
const _mainConnectionURI = new CustomizableParameter<Connection.URI>(() => {
Expand Down Expand Up @@ -398,8 +400,19 @@ export const useMainVehicleStore = defineStore('main-vehicle', () => {

const heartbeat = pack.message as Message.Heartbeat
firmwareType.value = heartbeat.autopilot.type
const oldVehicleType = vehicleType.value
vehicleType.value = heartbeat.mavtype.type
lastHeartbeat.value = new Date()

if (oldVehicleType !== vehicleType.value && vehicleType.value !== undefined) {
console.log('Vehicle type changed to', vehicleType.value)
try {
controllerStore.loadDefaultProtocolMappingForVehicle(vehicleType.value)
console.info(`Loaded default joystick protocol mapping for vehicle type ${vehicleType.value}.`)
} catch (error) {
console.error(`Could not load default protocol mapping for vehicle type ${vehicleType.value}: ${error}`)
}
}
})
// eslint-disable-next-line @typescript-eslint/no-explicit-any
getAutoPilot(vehicles).onMode.add((vehicleMode: any) => {
Expand Down Expand Up @@ -430,7 +443,6 @@ export const useMainVehicleStore = defineStore('main-vehicle', () => {
registerActionCallback(availableCockpitActions.mavlink_disarm, disarm)
})

const controllerStore = useControllerStore()
const mavlinkManualControlManager = new MavlinkManualControlManager()
const cockpitActionsManager = new CockpitActionsManager()
controllerStore.registerControllerUpdateCallback(mavlinkManualControlManager.updateControllerData)
Expand Down

0 comments on commit 15b84d2

Please sign in to comment.