forked from iNavFlight/inav-configurator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathltmDecoder.js
260 lines (218 loc) · 8.56 KB
/
ltmDecoder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
'use strict';
const ltmDecoder = (function () {
let TELEMETRY = {
//A frame
pitch: null,
roll: null,
heading: null,
//S frame
voltage: null,
currectDrawn: null,
rssi: null,
airspeed: null,
flightmode: null,
flightmodeName: null,
armed: null,
failsafe: null,
//G frame
latitude: null,
longitude: null,
altitude: null,
groundSpeed: null,
gpsFix: null,
gpsSats: null,
//X frame
hdop: null,
sensorStatus: null,
frameCounter: null,
disarmReason: null,
disarmReasonName: null
};
let publicScope = {},
privateScope = {};
const LTM_TIMEOUT_MS = 5000;
const LTM_FRAME_TIMEOUT_MS = 700;
const LTM_HEADER_START_1 = '$';
const LTM_HEADER_START_2 = 'T';
const LTM_FRAMELENGTH = {
'G': 18,
'A': 10,
'S': 11,
'O': 18,
'N': 10,
'X': 10
};
const LTM_FLIGHT_MODE_NAMES = [
"MANUAL",
"RATE",
"ANGLE",
"HORIZON",
"ACRO",
"STABALIZED1",
"STABALIZED2",
"STABILIZED3",
"ALTHOLD",
"GPSHOLD",
"WAYPOINTS",
"HEADHOLD",
"CIRCLE",
"RTH",
"FOLLOWME",
"LAND",
"FLYBYWIRE1",
"FLYBYWIRE2",
"CRUISE",
"UNKNOWN",
"LAUNCH",
"AUTOTUNE"
];
const LTM_DISARM_REASON_NAMES = [
"NONE",
"TIMEOUT",
"STICKS",
"SWITCH_3D",
"SWITCH",
"KILLSWITCH",
"FAILSAFE",
"NAVIGATION",
"LANDING"
];
const LTM_STATE_IDLE = 0;
const LTM_STATE_HEADER_START_1 = 1;
const LTM_STATE_HEADER_START_2 = 2;
const LTM_STATE_MSGTYPE = 3;
privateScope.protocolState = LTM_STATE_IDLE;
privateScope.lastFrameReceivedMs = null;
privateScope.frameType = null;
privateScope.frameLength = null;
privateScope.receiverIndex = 0;
privateScope.serialBuffer = [];
privateScope.frameProcessingStartedAtMs = 0;
privateScope.readByte = function (offset) {
return privateScope.serialBuffer[offset];
};
privateScope.readInt = function (offset) {
return privateScope.serialBuffer[offset] + (privateScope.serialBuffer[offset + 1] << 8);
}
privateScope.readInt32 = function (offset) {
return privateScope.serialBuffer[offset] + (privateScope.serialBuffer[offset + 1] << 8) + (privateScope.serialBuffer[offset + 2] << 16) + (privateScope.serialBuffer[offset + 3] << 24);
}
privateScope.push = function (data) {
let charCode = String.fromCharCode(data);
//If frame is processed for too long, reset protocol state
if (privateScope.protocolState != LTM_STATE_IDLE && new Date().getTime() - privateScope.frameProcessingStartedAtMs > LTM_FRAME_TIMEOUT_MS) {
privateScope.protocolState = LTM_STATE_IDLE;
privateScope.frameProcessingStartedAtMs = new Date().getTime();
console.log('LTM privateScope.protocolState: TIMEOUT, forcing into IDLE, processed frame: ' + privateScope.frameType);
}
if (privateScope.protocolState == LTM_STATE_IDLE) {
if (charCode == LTM_HEADER_START_1) {
privateScope.protocolState = LTM_STATE_HEADER_START_1;
privateScope.frameProcessingStartedAtMs = new Date().getTime();
}
return;
} else if (privateScope.protocolState == LTM_STATE_HEADER_START_1) {
if (charCode == LTM_HEADER_START_2) {
privateScope.protocolState = LTM_STATE_HEADER_START_2;
} else {
privateScope.protocolState = LTM_STATE_IDLE;
}
return;
} else if (privateScope.protocolState == LTM_STATE_HEADER_START_2) {
//Check if incoming frame type is a known one
if (LTM_FRAMELENGTH[charCode] == undefined) {
//Unknown frame type, reset protocol state
privateScope.protocolState = LTM_STATE_IDLE;
console.log('Unknown frame type, reset protocol state');
} else {
//Known frame type, store it and move to next state
privateScope.frameType = charCode;
privateScope.frameLength = LTM_FRAMELENGTH[charCode];
privateScope.receiverIndex = 0;
privateScope.serialBuffer = [];
privateScope.protocolState = LTM_STATE_MSGTYPE;
console.log('protocolState: LTM_STATE_MSGTYPE', 'will expext frame ' + privateScope.frameType, 'expected length: ' + privateScope.frameLength);
}
return;
} else if (privateScope.protocolState == LTM_STATE_MSGTYPE) {
/*
* Check if last payload byte has been received.
*/
if (privateScope.receiverIndex == privateScope.frameLength - 4) {
/*
* If YES, check checksum and execute data processing
*/
let checksum = 0;
for (let i = 0; i < privateScope.serialBuffer.length; i++) {
checksum ^= privateScope.serialBuffer[i];
}
if (checksum != data) {
console.log('LTM checksum error, frame type: ' + privateScope.frameType + ' rejected');
privateScope.protocolState = LTM_STATE_IDLE;
privateScope.serialBuffer = [];
privateScope.receiverIndex = 0;
return;
}
if (privateScope.frameType == 'A') {
TELEMETRY.pitch = privateScope.readInt(0);
TELEMETRY.roll = privateScope.readInt(2);
TELEMETRY.heading = privateScope.readInt(4);
}
if (privateScope.frameType == 'S') {
TELEMETRY.voltage = privateScope.readInt(0);
TELEMETRY.currectDrawn = privateScope.readInt(2);
TELEMETRY.rssi = privateScope.readByte(4);
TELEMETRY.airspeed = privateScope.readByte(5);
let fm = privateScope.readByte(6);
TELEMETRY.flightmode = fm >> 2;
TELEMETRY.flightmodeName = LTM_FLIGHT_MODE_NAMES[TELEMETRY.flightmode];
TELEMETRY.armed = (fm & 0x02) >> 1;
TELEMETRY.failsafe = fm & 0x01;
}
if (privateScope.frameType == 'G') {
TELEMETRY.latitude = privateScope.readInt32(0);
TELEMETRY.longitude = privateScope.readInt32(4);
TELEMETRY.groundSpeed = privateScope.readByte(8);
TELEMETRY.altitude = privateScope.readInt32(9);
let raw = privateScope.readByte(13);
TELEMETRY.gpsSats = raw >> 2;
TELEMETRY.gpsFix = raw & 0x03;
}
if (privateScope.frameType == 'X') {
TELEMETRY.hdop = privateScope.readInt(0);
TELEMETRY.sensorStatus = privateScope.readByte(2);
TELEMETRY.frameCounter = privateScope.readByte(3);
TELEMETRY.disarmReason = privateScope.readByte(4);
TELEMETRY.disarmReasonName = LTM_DISARM_REASON_NAMES[TELEMETRY.disarmReason];
}
privateScope.protocolState = LTM_STATE_IDLE;
privateScope.serialBuffer = [];
privateScope.lastFrameReceivedMs = new Date().getTime();
privateScope.receiverIndex = 0;
} else {
/*
* If no, put data into buffer
*/
privateScope.serialBuffer.push(data);
privateScope.receiverIndex++;
}
}
}
publicScope.read = function (readInfo) {
var data = new Uint8Array(readInfo.data);
for (var i = 0; i < data.length; i++) {
privateScope.push(data[i]);
}
};
publicScope.isReceiving = function () {
return privateScope.lastFrameReceivedMs !== null && (new Date().getTime() - privateScope.lastFrameReceivedMs) < LTM_TIMEOUT_MS;
};
publicScope.wasEverReceiving = function () {
return privateScope.lastFrameReceivedMs !== null;
};
publicScope.get = function () {
return TELEMETRY;
};
return publicScope;
})();
module.exports = ltmDecoder;