Skip to content

Commit

Permalink
only map when all parameters are available
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaellehmkuhl committed Dec 6, 2023
1 parent f752031 commit ce6f9cd
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
12 changes: 10 additions & 2 deletions src/libs/joystick/protocols/mavlink-manual-control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ export class MavlinkManualControlManager {
parametersTable: { title: string; value: number }[] = []
vehicleButtonParameterTable: { title: string; value: number }[] = []
currentVehicleParameters: { [key in string]: number } = {}
vehicleTotalParametersCount: number | undefined = undefined
public vehicle: ArduPilot | undefined

constructor() {
Expand All @@ -308,9 +309,10 @@ export class MavlinkManualControlManager {
this.vehicle = vehicle

// Update interval parameters when they are available
this.vehicle.onParameter.add((newParameter: Parameter) => {
this.vehicle.onParameter.add(([newParameter, parametersCount]) => {
const newVehicleParameters = { ...this.currentVehicleParameters, ...{ [newParameter.name]: newParameter.value } }
this.currentVehicleParameters = newVehicleParameters
this.vehicleTotalParametersCount = parametersCount
})

this.vehicle.requestParametersList()
Expand Down Expand Up @@ -423,7 +425,13 @@ export class MavlinkManualControlManager {
}

remapActionsToVehicleButtonParameters = (): void => {
if (!this.vehicle || !this.currentActionsMapping || !this.currentVehicleParameters || !this.vehicleButtonParameterTable) return
if (!this.vehicle || !this.currentActionsMapping || !this.currentVehicleParameters || !this.vehicleButtonParameterTable || !this.vehicleTotalParametersCount) return
// Do not proceed with remapping unless we already have all parameters downloaded
// This prevents us from thinking some function is not mapped in the vehicle when in fact we just didn't download it yet
const allParametersDownloaded = Object.entries(this.currentVehicleParameters).length >= this.vehicleTotalParametersCount
if (!allParametersDownloaded) {
return
}

const buttonParametersNamedObject: { [key in number]: string } = {}
this.vehicleButtonParameterTable.forEach((entry) => (buttonParametersNamedObject[entry.value] = entry.title))
Expand Down
12 changes: 11 additions & 1 deletion src/libs/vehicle/ardupilot/ardupilot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export abstract class ArduPilotVehicle<Modes> extends Vehicle.AbstractVehicle<Mo
_isArmed = false // Defines if the vehicle is armed
_powerSupply = new PowerSupply()
_lastParameter = new Parameter()
_totalParametersCount: number | undefined = undefined
_currentCockpitMissionItemsOnPlanning: Waypoint[] = []
_currentMavlinkMissionItemsOnVehicle: Message.MissionItemInt[] = []
_statusText = new StatusText()
Expand Down Expand Up @@ -321,11 +322,12 @@ export abstract class ArduPilotVehicle<Modes> extends Vehicle.AbstractVehicle<Mo
case MAVLinkType.PARAM_VALUE: {
const receivedMessage = mavlink_message.message as Message.ParamValue
const param_name = receivedMessage.param_id.join('').replace(/\0/g, '')
const { param_value } = receivedMessage
const { param_value, param_count } = receivedMessage
// We need this due to mismatches between js 64-bit floats and REAL32 in MAVLink
const trimmed_value = Math.round(param_value * 10000) / 10000

this._lastParameter = { name: param_name, value: trimmed_value }
this._totalParametersCount = Number(param_count)
this.onParameter.emit()
break
}
Expand Down Expand Up @@ -480,6 +482,14 @@ export abstract class ArduPilotVehicle<Modes> extends Vehicle.AbstractVehicle<Mo
return this._lastParameter
}

/**
* Return total amount of parameters in the vehicle
* @returns { Number }
*/
totalParametersCount(): number | undefined {
return this._totalParametersCount
}

/**
* Return status text information
* @returns {StatusText}
Expand Down
5 changes: 3 additions & 2 deletions src/libs/vehicle/vehicle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export abstract class AbstractVehicle<Modes> {
onMode = new Signal<Modes>()
onPosition = new Signal<Coordinates>()
onPowerSupply = new Signal<PowerSupply>()
onParameter = new Signal<Parameter>()
onParameter = new Signal<[Parameter, number | undefined]>()
onStatusGPS = new Signal<StatusGPS>()
onStatusText = new Signal<StatusText>()
onVelocity = new Signal<Velocity>()
Expand Down Expand Up @@ -114,7 +114,7 @@ export abstract class AbstractVehicle<Modes> {
this.onMode.register_caller(() => this.mode())
this.onPosition.register_caller(() => this.position())
this.onPowerSupply.register_caller(() => this.powerSupply())
this.onParameter.register_caller(() => this.lastParameter())
this.onParameter.register_caller(() => [this.lastParameter(), this.totalParametersCount()])
this.onStatusText.register_caller(() => this.statusText())
this.onStatusGPS.register_caller(() => this.statusGPS())
this.onVelocity.register_caller(() => this.velocity())
Expand Down Expand Up @@ -180,6 +180,7 @@ export abstract class AbstractVehicle<Modes> {
abstract position(): Coordinates
abstract velocity(): Velocity
abstract powerSupply(): PowerSupply
abstract totalParametersCount(): number | undefined
abstract lastParameter(): Parameter
abstract statusText(): StatusText
abstract statusGPS(): StatusGPS
Expand Down
2 changes: 1 addition & 1 deletion src/stores/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type controllerUpdateCallback = (
export const useControllerStore = defineStore('controller', () => {
const joysticks = ref<Map<number, Joystick>>(new Map())
const updateCallbacks = ref<controllerUpdateCallback[]>([])
const protocolMapping = useStorage('cockpit-protocol-mapping-v5.2', cockpitStandardToProtocols)
const protocolMapping = useStorage('cockpit-protocol-mapping-v5.3', cockpitStandardToProtocols)
const cockpitStdMappings = useStorage('cockpit-standard-mappings', availableGamepadToCockpitMaps)
const availableAxesActions = allAvailableAxes
const availableButtonActions = allAvailableButtons
Expand Down

0 comments on commit ce6f9cd

Please sign in to comment.