Skip to content

Commit 59a94df

Browse files
libs: vehicle: {ardupilot, mavlink}: Apply necessary changes to create generic mavlink vehicle
Signed-off-by: Patrick José Pereira <patrickelectric@gmail.com>
1 parent 32d9b1a commit 59a94df

File tree

2 files changed

+60
-41
lines changed

2 files changed

+60
-41
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import type { MetadataFile } from '@/types/ardupilot-metadata'
2+
3+
import * as MAVLinkVehicle from '../mavlink/vehicle'
4+
import * as Vehicle from '../vehicle'
5+
6+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
7+
export type ArduPilot = ArduPilotVehicle<any>
8+
9+
/**
10+
* Generic ArduPilot vehicle
11+
*/
12+
export abstract class ArduPilotVehicle<Modes> extends MAVLinkVehicle.MAVLinkVehicle<Modes> {
13+
_metadata: MetadataFile
14+
15+
/**
16+
* Construct a new generic ArduPilot type
17+
* @param {Vehicle.Type} type
18+
* @param {number} systemId
19+
*/
20+
constructor(type: Vehicle.Type, systemId: number) {
21+
super(Vehicle.Firmware.ArduPilot, type, systemId)
22+
}
23+
24+
/**
25+
* Return metadata from the vehicle
26+
* @returns {MetadataFile}
27+
*/
28+
metadata(): MetadataFile {
29+
return this._metadata
30+
}
31+
}

src/libs/vehicle/mavlink/vehicle.ts

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { differenceInMilliseconds } from 'date-fns'
2+
import { defaultMessageIntervalsOptions } from 'defaults'
23
import { unit } from 'mathjs'
34

45
import {
@@ -48,28 +49,22 @@ import {
4849
StatusText,
4950
Velocity,
5051
} from '@/libs/vehicle/types'
51-
import type { MetadataFile } from '@/types/ardupilot-metadata'
5252
import { type MissionLoadingCallback, type Waypoint, defaultLoadingCallback } from '@/types/mission'
5353

5454
import { flattenData } from '../common/data-flattener'
55-
import { defaultMessageIntervalsOptions } from '../mavlink/defaults'
56-
import * as MAVLinkVehicle from '../mavlink/vehicle'
5755
import * as Vehicle from '../vehicle'
5856

5957
export const MAVLINK_MESSAGE_INTERVALS_STORAGE_KEY = 'cockpit-mavlink-message-intervals'
6058

61-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
62-
export type ArduPilot = ArduPilotVehicle<any>
63-
6459
const preDefinedDataLakeVariables = {
6560
cameraTilt: { id: 'cameraTiltDeg', name: 'Camera Tilt Degrees', type: 'number' },
66-
ardupilotSystemId: { id: 'ardupilotSystemId', name: 'ArduPilot System ID', type: 'number' },
61+
autopilotSystemId: { id: 'autopilotSystemId', name: 'Autopilot System ID', type: 'number' },
6762
}
6863

6964
/**
70-
* Generic ArduPilot vehicle
65+
* Generic MAVLink vehicle
7166
*/
72-
export abstract class ArduPilotVehicle<Modes> extends MAVLinkVehicle.MAVLinkVehicle<Modes> {
67+
export abstract class MAVLinkVehicle<Modes> extends Vehicle.AbstractVehicle<Modes> {
7368
_altitude = new Altitude({ msl: unit(0, 'm'), rel: 0 })
7469
_attitude = new Attitude({ roll: 0, pitch: 0, yaw: 0 })
7570
_communicationDropRate = 0
@@ -90,7 +85,7 @@ export abstract class ArduPilotVehicle<Modes> extends MAVLinkVehicle.MAVLinkVehi
9085
_statusText = new StatusText()
9186
_statusGPS = new StatusGPS()
9287
_vehicleSpecificErrors = [0, 0, 0, 0]
93-
_metadata: MetadataFile
88+
9489
_messages: MAVLinkMessageDictionary = new Map()
9590

9691
onIncomingMAVLinkMessage = new SignalTyped()
@@ -100,31 +95,13 @@ export abstract class ArduPilotVehicle<Modes> extends MAVLinkVehicle.MAVLinkVehi
10095
protected currentSystemId = 1
10196

10297
/**
103-
* Returns the current system ID
104-
* @returns {number}
105-
*/
106-
get systemId(): number {
107-
return this.currentSystemId
108-
}
109-
110-
/**
111-
* Function for subclass inheritance
112-
* Helps to deal with specialized vehicles that has particular or custom behaviour
113-
* @param {Package} mavlink message
114-
*/
115-
protected onMAVLinkPackage(mavlink: Package): void {
116-
// Nothing here, typescript does not not have clean optional abstract methods
117-
// without abstract class
118-
mavlink
119-
}
120-
121-
/**
122-
* Construct a new generic ArduPilot type
98+
* Create MAVLink vehicle
99+
* @param {Vehicle.Firmware} firmware
123100
* @param {Vehicle.Type} type
124101
* @param {number} systemId
125102
*/
126-
constructor(type: Vehicle.Type, systemId: number) {
127-
super(Vehicle.Firmware.ArduPilot, type)
103+
constructor(firmware: Vehicle.Firmware, type: Vehicle.Type, systemId: number) {
104+
super(firmware, type)
128105
this.currentSystemId = systemId
129106

130107
// Request vehicle to stream a pre-defined list of messages so the GCS can receive them
@@ -139,7 +116,26 @@ export abstract class ArduPilotVehicle<Modes> extends MAVLinkVehicle.MAVLinkVehi
139116
this.createPredefinedDataLakeVariables()
140117

141118
// Set the system ID in the data-lake
142-
setDataLakeVariableData(preDefinedDataLakeVariables.ardupilotSystemId.id, systemId)
119+
setDataLakeVariableData(preDefinedDataLakeVariables.autopilotSystemId.id, systemId)
120+
}
121+
122+
/**
123+
* Returns the current system ID
124+
* @returns {number}
125+
*/
126+
get systemId(): number {
127+
return this.currentSystemId
128+
}
129+
130+
/**
131+
* Function for subclass inheritance
132+
* Helps to deal with specialized vehicles that has particular or custom behaviour
133+
* @param {Package} mavlink message
134+
*/
135+
protected onMAVLinkPackage(mavlink: Package): void {
136+
// Nothing here, typescript does not not have clean optional abstract methods
137+
// without abstract class
138+
mavlink
143139
}
144140

145141
/**
@@ -694,14 +690,6 @@ export abstract class ArduPilotVehicle<Modes> extends MAVLinkVehicle.MAVLinkVehi
694690
return this._flying
695691
}
696692

697-
/**
698-
* Return metadata from the vehicle
699-
* @returns {MetadataFile}
700-
*/
701-
metadata(): MetadataFile {
702-
return this._metadata
703-
}
704-
705693
/**
706694
* Return vehicle position information
707695
* @returns {Coordinates}

0 commit comments

Comments
 (0)