Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaellehmkuhl committed Sep 4, 2023
1 parent 6a398a2 commit 7437c30
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 83 deletions.
2 changes: 1 addition & 1 deletion src/libs/joystick/protocols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export enum MAVLinkAxis {
R = 'r',
}
const mavlinkAvailableAxes = Object.values(MAVLinkAxis)
const mavlinkAvailableButtons = sequentialArray(16)
export const mavlinkAvailableButtons = sequentialArray(16)

const mavlinkAxesLimits = [-1000, 1000]
export const protocolAxesLimits = (protocol: JoystickProtocol): number[] => {
Expand Down
26 changes: 26 additions & 0 deletions src/libs/vehicle/ardupilot/ardupilot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
MavMissionResult,
MavMissionType,
MavModeFlag,
MavParamType,
MavState,
MavType,
} from '@/libs/connection/m2r/messages/mavlink2rest-enum'
Expand Down Expand Up @@ -490,6 +491,31 @@ export abstract class ArduPilotVehicle<Modes> extends Vehicle.AbstractVehicle<Mo
this.write(paramRequestMessage)
}

/**
* Request parameters list from vehicle
* @param id
* @param value
* @param type
*/
setParameter(id: string, value: number, type?: MavParamType): void {
const param_name = [...id]
while (param_name.length < 16) {
param_name.push('\0')
}
const paramSetMessage: Message.ParamSet = {
type: MAVLinkType.PARAM_SET,
target_system: 0,
target_component: 0,
param_id: param_name,
param_value: value,
param_type: {
type: type ?? MavParamType.MAV_PARAM_TYPE_UINT8,
},
}
console.log(paramSetMessage)
this.write(paramSetMessage)
}

/**
* Send number of mission items that will be uploaded next
* @param { number } itemsCount Number of mission items that will be sent
Expand Down
8 changes: 4 additions & 4 deletions src/stores/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export const useControllerStore = defineStore('controller', () => {
const updateCallbacks = ref<controllerUpdateCallback[]>([])
const protocolMapping = useStorage('cockpit-v0.0.7-protocol-mapping', cockpitStandardToProtocols)
const cockpitStdMappings = useStorage('cockpit-standard-mappings', availableGamepadToCockpitMaps)
const availableAxes = allAvailableAxes
const availableButtons = allAvailableButtons
const availableProtocolAxesFunctions = allAvailableAxes
const availableProtocolButtonFunctions = allAvailableButtons
const allPrettyButtonNames = ref<InputWithPrettyName[]>([])
const enableForwarding = ref(true)

Expand Down Expand Up @@ -100,8 +100,8 @@ export const useControllerStore = defineStore('controller', () => {
joysticks,
protocolMapping,
cockpitStdMappings,
availableAxes,
availableButtons,
availableProtocolAxesFunctions,
availableProtocolButtonFunctions,
allPrettyButtonNames,
downloadJoystickProfile,
loadJoystickProfile,
Expand Down
23 changes: 17 additions & 6 deletions src/stores/mainVehicle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ export const useMainVehicleStore = defineStore('main-vehicle', () => {
const powerSupply: PowerSupply = reactive({} as PowerSupply)
const velocity: Velocity = reactive({} as Velocity)
const parametersTable = reactive({})
// eslint-disable-next-line jsdoc/require-jsdoc
const buttonParameterTable = reactive<{ title: string; value: number }[]>([])
const currentParameters = reactive({})
const mainVehicle = ref<ArduPilot | undefined>(undefined)
const isArmed = ref<boolean | undefined>(undefined)
Expand Down Expand Up @@ -150,6 +152,13 @@ export const useMainVehicleStore = defineStore('main-vehicle', () => {
mainVehicle.value?.disarm()
}

/**
* Set parameter in the vehicle
*/
function setParameter(id: string, value: number, type?: string): void {
mainVehicle.value?.setParameter(id, value, type)
}

/**
* Send manual control message
* @param {ProtocolControllerState} controllerState Current state of the controller
Expand Down Expand Up @@ -408,22 +417,22 @@ export const useMainVehicleStore = defineStore('main-vehicle', () => {
const updateMavlinkButtonsPrettyNames = (): void => {
if (!currentParameters || !parametersTable) return
const newMavlinkButtonsNames: InputWithPrettyName[] = []
buttonParameterTable.splice(0)
// @ts-ignore: This type is huge. Needs refactoring typing here.
if (parametersTable['BTN0_FUNCTION'] && parametersTable['BTN0_FUNCTION']['Values']) {
const parameterValues: { title: string, value: number }[] = [] // eslint-disable-line
// @ts-ignore: This type is huge. Needs refactoring typing here.
Object.entries(parametersTable['BTN0_FUNCTION']['Values']).forEach((param) => {
const rawText = param[1] as string
const formatedText = (rawText.charAt(0).toUpperCase() + rawText.slice(1)).replace(new RegExp('_', 'g'), ' ')
parameterValues.push({ title: formatedText as string, value: Number(param[0]) })
// const formatedText = (rawText.charAt(0).toUpperCase() + rawText.slice(1)).replace(new RegExp('_', 'g'), ' ')
buttonParameterTable.push({ title: rawText as string, value: Number(param[0]) })
})
Object.entries(currentParameters).forEach((param) => {
if (!param[0].startsWith('BTN') || !param[0].endsWith('_FUNCTION')) return
const button = Number(param[0].replace('BTN', '').replace('_FUNCTION', ''))
const functionName = parameterValues.find((p) => p.value === param[1])?.title
const buttonId = Number(param[0].replace('BTN', '').replace('_FUNCTION', ''))
const functionName = buttonParameterTable.find((p) => p.value === param[1])?.title
if (functionName === undefined) return
newMavlinkButtonsNames.push({
input: { protocol: JoystickProtocol.MAVLink, value: button },
input: { protocol: JoystickProtocol.MAVLink, value: buttonId },
prettyName: functionName,
})
})
Expand All @@ -444,6 +453,7 @@ export const useMainVehicleStore = defineStore('main-vehicle', () => {
setFlightMode,
sendGcsHeartbeat,
requestParametersList,
setParameter,
fetchMission,
uploadMission,
clearMissions,
Expand All @@ -469,6 +479,7 @@ export const useMainVehicleStore = defineStore('main-vehicle', () => {
icon,
parametersTable,
currentParameters,
buttonParameterTable,
configurationPages,
rtcConfiguration,
genericVariables,
Expand Down
1 change: 1 addition & 0 deletions src/types/joystick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { JoystickModel } from '@/libs/joystick/manager'
export enum JoystickProtocol {
MAVLink = 'mavlink',
CockpitAction = 'cockpit-action',
Other = 'other',
}

/**
Expand Down
Loading

0 comments on commit 7437c30

Please sign in to comment.