Skip to content

Commit

Permalink
joystick: Allow associating vehicle type to specific mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaellehmkuhl committed Apr 9, 2024
1 parent 9dd7c1d commit 3d374a2
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 11 deletions.
7 changes: 7 additions & 0 deletions src/assets/joystick-profiles.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { MavType } from '@/libs/connection/m2r/messages/mavlink2rest-enum'
import { JoystickModel } from '@/libs/joystick/manager'
import { availableCockpitActions } from '@/libs/joystick/protocols/cockpit-actions'
import {
Expand All @@ -17,6 +18,12 @@ export const defaultRovMappingHash = '10b0075a-27a7-4800-ba95-f35fd722d1df'
export const defaultBoatMappingHash = 'd3427f20-ba28-4cf7-ae24-ec740dd6dce0'
export const defaultMavMappingHash = 'dd654387-18fc-4674-89a6-4dc4d0bc8240'

export const defaultProtocolMappingVehicleCorrespondency = {
[MavType.MAV_TYPE_SUBMARINE]: defaultRovMappingHash,
[MavType.MAV_TYPE_SURFACE_BOAT]: defaultBoatMappingHash,
[MavType.MAV_TYPE_QUADROTOR]: defaultMavMappingHash,
}

// TODO: Adjust mapping for PS5 controller
export const cockpitStandardToProtocols: JoystickProtocolActionsMapping[] = [
{
Expand Down
11 changes: 10 additions & 1 deletion src/stores/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import Swal from 'sweetalert2'
import { v4 as uuid4 } from 'uuid'
import { computed, ref, toRaw, watch } from 'vue'

import { availableGamepadToCockpitMaps, cockpitStandardToProtocols } from '@/assets/joystick-profiles'
import {
availableGamepadToCockpitMaps,
cockpitStandardToProtocols,
defaultProtocolMappingVehicleCorrespondency,
} from '@/assets/joystick-profiles'
import { getKeyDataFromCockpitVehicleStorage, setKeyDataOnCockpitVehicleStorage } from '@/libs/blueos'
import { type JoystickEvent, EventType, joystickManager, JoystickModel } from '@/libs/joystick/manager'
import { allAvailableAxes, allAvailableButtons } from '@/libs/joystick/protocols'
Expand Down Expand Up @@ -42,6 +46,10 @@ export const useControllerStore = defineStore('controller', () => {
const availableButtonActions = allAvailableButtons
const enableForwarding = ref(true)
const holdLastInputWhenWindowHidden = useStorage('cockpit-hold-last-joystick-input-when-window-hidden', false)
const vehicleTypeProtocolMappingCorrespondency = useStorage<typeof defaultProtocolMappingVehicleCorrespondency>(
'cockpit-default-vehicle-type-protocol-mappings',
defaultProtocolMappingVehicleCorrespondency
)

const protocolMapping = computed<JoystickProtocolActionsMapping>({
get() {
Expand Down Expand Up @@ -342,6 +350,7 @@ export const useControllerStore = defineStore('controller', () => {
cockpitStdMappings,
availableAxesActions,
availableButtonActions,
vehicleTypeProtocolMappingCorrespondency,
loadProtocolMapping,
exportJoystickMapping,
importJoystickMapping,
Expand Down
58 changes: 48 additions & 10 deletions src/views/ConfigurationJoystickView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,29 @@
<p>Could not stablish communication with the vehicle.</p>
<p>Button functions will appear as numbers. If connection is restablished, function names will appear.</p>
</div>
<div v-if="availableModifierKeys" class="flex items-center px-5 py-3 m-5 font-bold border rounded-md">
<Button
v-for="functionMapping in controllerStore.protocolMappings"
:key="functionMapping.name"
class="m-2"
:class="{ 'bg-slate-700': controllerStore.protocolMapping.name === functionMapping.name }"
@click="controllerStore.loadProtocolMapping(functionMapping)"
>
{{ functionMapping.name }}
</Button>
<div v-if="availableModifierKeys" class="flex flex-col items-center px-5 py-3 m-5 font-bold border rounded-md">
<div class="flex">
<Button
v-for="functionMapping in controllerStore.protocolMappings"
:key="functionMapping.name"
class="m-2"
:class="{ 'bg-slate-700': controllerStore.protocolMapping.name === functionMapping.name }"
@click="controllerStore.loadProtocolMapping(functionMapping)"
>
{{ functionMapping.name }}
</Button>
</div>
<div class="flex flex-col items-center w-full my-2">
<v-combobox
v-model="vehicleTypesAssignedToCurrentProfile"
:items="availableVehicleTypes"
label="Vehicle types that use this profile by default:"
chips
multiple
variant="outlined"
class="w-10/12 m-4"
/>
</div>
</div>
<div class="flex items-center px-5 py-3 m-5 font-bold border rounded-md">
<Button
Expand Down Expand Up @@ -316,6 +329,7 @@ import { type Ref, computed, nextTick, onMounted, onUnmounted, ref, watch } from
import Button from '@/components/Button.vue'
import JoystickPS from '@/components/joysticks/JoystickPS.vue'
import { getArdupilotVersion, getMavlink2RestVersion } from '@/libs/blueos'
import { MavType } from '@/libs/connection/m2r/messages/mavlink2rest-enum'
import { modifierKeyActions } from '@/libs/joystick/protocols/other'
import { useControllerStore } from '@/stores/controller'
import { useMainVehicleStore } from '@/stores/mainVehicle'
Expand Down Expand Up @@ -588,4 +602,28 @@ const axisRemappingText = computed(() => {
const buttonActionsToShow = computed(() =>
controllerStore.availableButtonActions.filter((a) => JSON.stringify(a) !== JSON.stringify(modifierKeyActions.regular))
)
const availableVehicleTypes = computed(() => Object.keys(MavType))
const vehicleTypesAssignedToCurrentProfile = computed({
get() {
return Object.keys(controllerStore.vehicleTypeProtocolMappingCorrespondency).filter((vType) => {
// @ts-ignore: Enums in TS such
return controllerStore.vehicleTypeProtocolMappingCorrespondency[vType] === controllerStore.protocolMapping.hash
})
},
set(selectedVehicleTypes: string[]) {
availableVehicleTypes.value.forEach((vType) => {
// @ts-ignore: Enums in TS such
if (controllerStore.vehicleTypeProtocolMappingCorrespondency[vType] === controllerStore.protocolMapping.hash) {
// @ts-ignore: Enums in TS such
controllerStore.vehicleTypeProtocolMappingCorrespondency[vType] = undefined
}
if (selectedVehicleTypes.includes(vType)) {
// @ts-ignore: Enums in TS such
controllerStore.vehicleTypeProtocolMappingCorrespondency[vType] = controllerStore.protocolMapping.hash
}
})
},
})
</script>

0 comments on commit 3d374a2

Please sign in to comment.