Skip to content

Commit 945c25b

Browse files
committed
joystick: add reversing support by default
1 parent cbac8d6 commit 945c25b

1 file changed

Lines changed: 43 additions & 11 deletions

File tree

src/libs/joystick/protocols/predefined-resources.ts

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ export let mavlinkCameraZoomActionId: string | undefined = undefined
2222
export let mavlinkCameraFocusActionId: string | undefined = undefined
2323

2424
const joystickAxisConfig = [
25-
{ key: 'axis_x' },
26-
{ key: 'axis_y' },
27-
{ key: 'axis_z' },
28-
{ key: 'axis_r' },
29-
{ key: 'axis_s' },
30-
{ key: 'axis_t' },
25+
{ key: 'axis_x', reverseVehicleTypes: ['copter', 'sub'] },
26+
{ key: 'axis_y', reverseVehicleTypes: ['copter', 'sub'] },
27+
{ key: 'axis_z', reverseVehicleTypes: ['rover'] },
28+
{ key: 'axis_r', reverseVehicleTypes: [] as string[] },
29+
{ key: 'axis_s', reverseVehicleTypes: ['sub'] },
30+
{ key: 'axis_t', reverseVehicleTypes: ['sub'] },
3131
] as const
3232

3333
const axisInputId = (key: string): string => `joystick/inputs/${key.replace('_', '-')}`
@@ -37,6 +37,22 @@ const axisName = (key: string): string =>
3737
.map((w) => w.charAt(0).toUpperCase() + w.slice(1))
3838
.join(' ')
3939

40+
/**
41+
* Generates a JavaScript expression that evaluates to -1 when the vehicle type matches
42+
* one of the given types and reverse is active, or 1 otherwise
43+
* @param {string[]} vehicleTypes - Vehicle types that should trigger reversal
44+
* @returns {string} A data-lake expression string
45+
*/
46+
const getReverseExpression = (vehicleTypes: readonly string[]): string => {
47+
if (vehicleTypes.length === 0) return '1'
48+
if (vehicleTypes.length === 1) {
49+
return `('{{cockpit/vehicle/type}}' === '${vehicleTypes[0]}' && {{joystick/inputs/reverse}}) ? -1 : 1`
50+
}
51+
return `([${vehicleTypes
52+
.map((t) => `'${t}'`)
53+
.join(', ')}].includes('{{cockpit/vehicle/type}}') && {{joystick/inputs/reverse}}) ? -1 : 1`
54+
}
55+
4056
/**
4157
* Pre-built data lake variable actions for joystick axis inputs, used in joystick profile mappings
4258
*/
@@ -172,22 +188,38 @@ export const setupJoystickAxesResources = (): void => {
172188
const id = axisInputId(axis.key)
173189
const name = axisName(axis.key)
174190
const outputId = id.replace('/inputs/', '/outputs/')
191+
const reverseId = `${outputId}-reverse`
175192

176193
createDataLakeVariable({ id, name, ...commonVariableConfig }, 0)
177194

178195
try {
179-
const existing = getAllTransformingFunctions().find((f) => f.id === outputId)
180-
if (!existing) {
196+
const existingReverse = getAllTransformingFunctions().find((f) => f.id === reverseId)
197+
if (!existingReverse) {
198+
createTransformingFunction(
199+
reverseId,
200+
`${name} Reverse`,
201+
'number',
202+
getReverseExpression(axis.reverseVehicleTypes),
203+
`Reverse multiplier for ${name} based on vehicle type and reverse toggle.`
204+
)
205+
}
206+
} catch (error) {
207+
console.error(`Error creating reverse transforming function for ${name}:`, error)
208+
}
209+
210+
try {
211+
const existingOutput = getAllTransformingFunctions().find((f) => f.id === outputId)
212+
if (!existingOutput) {
181213
createTransformingFunction(
182214
outputId,
183215
`${name} Output`,
184216
'number',
185-
`{{${id}}}`,
217+
`{{${reverseId}}} * {{${id}}}`,
186218
`Output value for MANUAL_CONTROL ${name}.`
187219
)
188220
}
189221
} catch (error) {
190-
console.error(`Error creating transforming function for ${name}:`, error)
222+
console.error(`Error creating output transforming function for ${name}:`, error)
191223
}
192224
}
193225
}
@@ -219,6 +251,6 @@ export const setupReverseResources = (): void => {
219251

220252
export const setupPredefinedLakeAndActionResources = (): void => {
221253
setupMavlinkCameraResources()
222-
setupJoystickAxesResources()
223254
setupReverseResources()
255+
setupJoystickAxesResources()
224256
}

0 commit comments

Comments
 (0)