Skip to content

Commit

Permalink
working (missing auto-adapt to number of vehicle buttons and m2r vers…
Browse files Browse the repository at this point in the history
…ion)
  • Loading branch information
rafaellehmkuhl committed Nov 10, 2023
1 parent 137e89c commit 5063bfe
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/libs/joystick/protocols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class MavlinkControllerState extends ProtocolControllerState {
z: number
r: number
buttons: number
buttons2: number
target: number
public static readonly BUTTONS_PER_BITFIELD = 16

Expand All @@ -58,6 +59,17 @@ export class MavlinkControllerState extends ProtocolControllerState {
buttons_int += buttonState * 2 ** i
}

let buttons2_int = 0
for (let i = 16; i < 2 * MavlinkControllerState.BUTTONS_PER_BITFIELD; i++) {
let buttonState = 0
mapping.buttonsCorrespondencies.forEach((b, idx) => {
if (isMavlinkInput(b) && b.value === i && joystickState.buttons[idx]) {
buttonState = 1
}
})
buttons2_int += buttonState * 2 ** (i - 16)
}

const xIndex = mapping.axesCorrespondencies.findIndex((v) => isMavlinkInput(v) && v.value === MAVLinkAxis.X)
const yIndex = mapping.axesCorrespondencies.findIndex((v) => isMavlinkInput(v) && v.value === MAVLinkAxis.Y)
const zIndex = mapping.axesCorrespondencies.findIndex((v) => isMavlinkInput(v) && v.value === MAVLinkAxis.Z)
Expand All @@ -76,6 +88,7 @@ export class MavlinkControllerState extends ProtocolControllerState {
this.r = rIndex === undefined ? 0 : round(scale(joystickState.axes[rIndex] ?? 0, -1, 1, rLimits[0], rLimits[1]), 0)

this.buttons = buttons_int
this.buttons2 = buttons2_int
this.target = round(target, 0)
}
}
Expand Down Expand Up @@ -152,7 +165,7 @@ export enum MAVLinkAxis {
R = 'r',
}
const mavlinkAvailableAxes = Object.values(MAVLinkAxis)
export const mavlinkAvailableButtons = sequentialArray(16)
export const mavlinkAvailableButtons = sequentialArray(32)

const mavlinkAxesLimits = [-1000, 1000]
export const protocolAxesLimits = (protocol: JoystickProtocol): number[] => {
Expand Down
1 change: 1 addition & 0 deletions src/libs/vehicle/ardupilot/ardupilot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ export abstract class ArduPilotVehicle<Modes> extends Vehicle.AbstractVehicle<Mo
z: state.z,
r: state.r,
buttons: state.buttons,
buttons2: state.buttons2,
target: 1,
}
this.write(manualControlMessage)
Expand Down

0 comments on commit 5063bfe

Please sign in to comment.