Skip to content

Commit 5406389

Browse files
Merge pull request #2463 from sensei-hacker/flight-axis-override-implementation
Flight axis override test + idiot-proof mag alignment handling for FCs without a mag
2 parents 7a4f83f + 17e7ce6 commit 5406389

File tree

3 files changed

+108
-41
lines changed

3 files changed

+108
-41
lines changed

js/transpiler/transpiler/tests/test_flight_axis_override.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
import { Transpiler } from '../index.js';
8+
import { Decompiler } from '../decompiler.js';
89

910
function testFlightAxisOverride() {
1011
console.log('Testing Flight Axis Override...\n');
@@ -45,7 +46,7 @@ if (flight.heading < 180) {
4546
code: `
4647
const { flight, override } = inav;
4748
48-
if (flight.armed) {
49+
if (flight.isArmed) {
4950
override.flightAxis.roll.angle = 30;
5051
override.flightAxis.pitch.angle = -15;
5152
override.flightAxis.yaw.rate = 50;
@@ -77,16 +78,17 @@ if (flight.armed) {
7778
});
7879

7980
// Test decompilation
80-
const decompiled = transpiler.decompile(logicConditions);
81+
const decompiler = new Decompiler();
82+
const decompiled = decompiler.decompile(logicConditions);
8183
if (decompiled.success) {
8284
console.log('✓ Decompilation successful');
8385
console.log('Decompiled code:');
8486
console.log(decompiled.code);
8587
} else {
86-
console.log('✗ Decompilation failed:', decompiled.errors);
88+
console.log('✗ Decompilation failed:', decompiled.error || decompiled.errors);
8789
}
8890
} else {
89-
console.log('✗ Compilation failed:', result.errors);
91+
console.log('✗ Compilation failed:', result.error || result.errors);
9092
}
9193
} catch (error) {
9294
console.log('✗ Error:', error.message);

js/transpiler/transpiler/tests/test_override_regressions.js

Lines changed: 84 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ let passed = 0;
3838
let failed = 0;
3939

4040
// Test 1: Basic throttle override still works
41-
passed += runTest('throttle override compiles', () => {
41+
if (runTest('throttle override compiles', () => {
4242
const code = `
4343
const { flight, override } = inav;
4444
if (flight.isArmed) {
@@ -51,10 +51,14 @@ if (flight.isArmed) {
5151
}
5252
assertEquals(result.success, true, 'Compilation should succeed');
5353
assertContains(result.commands.join(' '), '29', 'Should contain operation 29 (OVERRIDE_THROTTLE)');
54-
});
54+
})) {
55+
passed++;
56+
} else {
57+
failed++;
58+
}
5559

5660
// Test 2: VTX override with nested object still works
57-
passed += runTest('vtx.power override compiles', () => {
61+
if (runTest('vtx.power override compiles', () => {
5862
const code = `
5963
const { flight, override } = inav;
6064
if (flight.homeDistance > 100) {
@@ -67,10 +71,14 @@ if (flight.homeDistance > 100) {
6771
}
6872
assertEquals(result.success, true, 'Compilation should succeed');
6973
assertContains(result.commands.join(' '), '25', 'Should contain operation 25 (SET_VTX_POWER_LEVEL)');
70-
});
74+
})) {
75+
passed++;
76+
} else {
77+
failed++;
78+
}
7179

7280
// Test 3: VTX band override
73-
passed += runTest('vtx.band override compiles', () => {
81+
if (runTest('vtx.band override compiles', () => {
7482
const code = `
7583
const { override } = inav;
7684
override.vtx.band = 2;
@@ -81,10 +89,14 @@ override.vtx.band = 2;
8189
throw new Error(`Compilation failed: ${result.error}`);
8290
}
8391
assertContains(result.commands.join(' '), '30', 'Should contain operation 30 (SET_VTX_BAND)');
84-
});
92+
})) {
93+
passed++;
94+
} else {
95+
failed++;
96+
}
8597

8698
// Test 4: VTX channel override
87-
passed += runTest('vtx.channel override compiles', () => {
99+
if (runTest('vtx.channel override compiles', () => {
88100
const code = `
89101
const { override } = inav;
90102
override.vtx.channel = 5;
@@ -95,10 +107,14 @@ override.vtx.channel = 5;
95107
throw new Error(`Compilation failed: ${result.error}`);
96108
}
97109
assertContains(result.commands.join(' '), '31', 'Should contain operation 31 (SET_VTX_CHANNEL)');
98-
});
110+
})) {
111+
passed++;
112+
} else {
113+
failed++;
114+
}
99115

100116
// Test 5: Throttle scale override
101-
passed += runTest('throttleScale override compiles', () => {
117+
if (runTest('throttleScale override compiles', () => {
102118
const code = `
103119
const { override } = inav;
104120
override.throttleScale = 75;
@@ -109,10 +125,14 @@ override.throttleScale = 75;
109125
throw new Error(`Compilation failed: ${result.error}`);
110126
}
111127
assertContains(result.commands.join(' '), '23', 'Should contain operation 23 (OVERRIDE_THROTTLE_SCALE)');
112-
});
128+
})) {
129+
passed++;
130+
} else {
131+
failed++;
132+
}
113133

114134
// Test 6: Arm safety override
115-
passed += runTest('armSafety override compiles', () => {
135+
if (runTest('armSafety override compiles', () => {
116136
const code = `
117137
const { override } = inav;
118138
override.armSafety = true;
@@ -123,10 +143,14 @@ override.armSafety = true;
123143
throw new Error(`Compilation failed: ${result.error}`);
124144
}
125145
assertContains(result.commands.join(' '), '22', 'Should contain operation 22 (OVERRIDE_ARMING_SAFETY)');
126-
});
146+
})) {
147+
passed++;
148+
} else {
149+
failed++;
150+
}
127151

128152
// Test 7: OSD layout override
129-
passed += runTest('osdLayout override compiles', () => {
153+
if (runTest('osdLayout override compiles', () => {
130154
const code = `
131155
const { override } = inav;
132156
override.osdLayout = 2;
@@ -137,10 +161,14 @@ override.osdLayout = 2;
137161
throw new Error(`Compilation failed: ${result.error}`);
138162
}
139163
assertContains(result.commands.join(' '), '32', 'Should contain operation 32 (SET_OSD_LAYOUT)');
140-
});
164+
})) {
165+
passed++;
166+
} else {
167+
failed++;
168+
}
141169

142170
// Test 8: Loiter radius override
143-
passed += runTest('loiterRadius override compiles', () => {
171+
if (runTest('loiterRadius override compiles', () => {
144172
const code = `
145173
const { override } = inav;
146174
override.loiterRadius = 5000;
@@ -151,10 +179,14 @@ override.loiterRadius = 5000;
151179
throw new Error(`Compilation failed: ${result.error}`);
152180
}
153181
assertContains(result.commands.join(' '), '41', 'Should contain operation 41 (LOITER_OVERRIDE)');
154-
});
182+
})) {
183+
passed++;
184+
} else {
185+
failed++;
186+
}
155187

156188
// Test 9: Min ground speed override
157-
passed += runTest('minGroundSpeed override compiles', () => {
189+
if (runTest('minGroundSpeed override compiles', () => {
158190
const code = `
159191
const { override } = inav;
160192
override.minGroundSpeed = 10;
@@ -165,10 +197,14 @@ override.minGroundSpeed = 10;
165197
throw new Error(`Compilation failed: ${result.error}`);
166198
}
167199
assertContains(result.commands.join(' '), '56', 'Should contain operation 56 (OVERRIDE_MIN_GROUND_SPEED)');
168-
});
200+
})) {
201+
passed++;
202+
} else {
203+
failed++;
204+
}
169205

170206
// Test 10: Multiple overrides in one block
171-
passed += runTest('multiple overrides in same block', () => {
207+
if (runTest('multiple overrides in same block', () => {
172208
const code = `
173209
const { flight, override } = inav;
174210
if (flight.isArmed) {
@@ -182,10 +218,14 @@ if (flight.isArmed) {
182218
}
183219
assertContains(result.commands.join(' '), '29', 'Should contain OVERRIDE_THROTTLE');
184220
assertContains(result.commands.join(' '), '25', 'Should contain SET_VTX_POWER_LEVEL');
185-
});
221+
})) {
222+
passed++;
223+
} else {
224+
failed++;
225+
}
186226

187227
// Test 11: Flight axis override doesn't break other overrides
188-
passed += runTest('flight axis and regular overrides together', () => {
228+
if (runTest('flight axis and regular overrides together', () => {
189229
const code = `
190230
const { flight, override } = inav;
191231
if (flight.isArmed) {
@@ -201,21 +241,29 @@ if (flight.isArmed) {
201241
assertContains(result.commands.join(' '), '45', 'Should contain FLIGHT_AXIS_ANGLE_OVERRIDE');
202242
assertContains(result.commands.join(' '), '29', 'Should contain OVERRIDE_THROTTLE');
203243
assertContains(result.commands.join(' '), '25', 'Should contain SET_VTX_POWER_LEVEL');
204-
});
244+
})) {
245+
passed++;
246+
} else {
247+
failed++;
248+
}
205249

206250
// Test 12: Invalid override target should fail gracefully
207-
passed += runTest('invalid override target produces error', () => {
251+
if (runTest('invalid override target produces error', () => {
208252
const code = `
209253
const { override } = inav;
210254
override.invalidProperty = 100;
211255
`;
212256
const transpiler = new Transpiler();
213257
const result = transpiler.transpile(code);
214258
assertEquals(result.success, false, 'Compilation should fail');
215-
});
259+
})) {
260+
passed++;
261+
} else {
262+
failed++;
263+
}
216264

217265
// Test 13: Decompile existing overrides
218-
passed += runTest('decompile throttle override', () => {
266+
if (runTest('decompile throttle override', () => {
219267
const decompiler = new Decompiler();
220268
const lcs = [
221269
{
@@ -234,10 +282,14 @@ passed += runTest('decompile throttle override', () => {
234282
throw new Error(`Decompilation failed: ${result.error || JSON.stringify(result)}`);
235283
}
236284
assertContains(result.code, 'override.throttle', 'Should contain override.throttle');
237-
});
285+
})) {
286+
passed++;
287+
} else {
288+
failed++;
289+
}
238290

239291
// Test 14: Decompile VTX power
240-
passed += runTest('decompile vtx.power override', () => {
292+
if (runTest('decompile vtx.power override', () => {
241293
const decompiler = new Decompiler();
242294
const lcs = [
243295
{
@@ -256,7 +308,11 @@ passed += runTest('decompile vtx.power override', () => {
256308
throw new Error(`Decompilation failed: ${result.error || JSON.stringify(result)}`);
257309
}
258310
assertContains(result.code, 'override.vtx.power', 'Should contain override.vtx.power');
259-
});
311+
})) {
312+
passed++;
313+
} else {
314+
failed++;
315+
}
260316

261317
console.log('\n==================================================');
262318
console.log(`📊 Test Results:`);

tabs/magnetometer.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,29 +59,38 @@ TABS.magnetometer.initialize = function (callback) {
5959
function (callback) {
6060
mspHelper.getSetting("align_mag_roll").then(function (data) {
6161
if (data == null) {
62-
console.log("while settting align_mag_roll, data is null or undefined");
63-
return;
62+
console.warn("while setting align_mag_roll, data is null or undefined");
63+
return Promise.resolve();
6464
}
6565
self.alignmentConfig.roll = parseInt(data.value, 10) / 10;
66-
}).then(callback)
66+
}).then(callback).catch(err => {
67+
console.error('Failed to get align_mag_roll:', err);
68+
callback();
69+
});
6770
},
6871
function (callback) {
6972
mspHelper.getSetting("align_mag_pitch").then(function (data) {
7073
if (data == null) {
71-
console.log("while settting align_mag_pitch, data is null or undefined");
72-
return;
74+
console.warn("while setting align_mag_pitch, data is null or undefined");
75+
return Promise.resolve();
7376
}
7477
self.alignmentConfig.pitch = parseInt(data.value, 10) / 10;
75-
}).then(callback)
78+
}).then(callback).catch(err => {
79+
console.error('Failed to get align_mag_pitch:', err);
80+
callback();
81+
});
7682
},
7783
function (callback) {
7884
mspHelper.getSetting("align_mag_yaw").then(function (data) {
7985
if (data == null) {
80-
console.log("while settting align_mag_yaw, data is null or undefined");
81-
return;
86+
console.warn("while setting align_mag_yaw, data is null or undefined");
87+
return Promise.resolve();
8288
}
8389
self.alignmentConfig.yaw = parseInt(data.value, 10) / 10;
84-
}).then(callback)
90+
}).then(callback).catch(err => {
91+
console.error('Failed to get align_mag_yaw:', err);
92+
callback();
93+
});
8594
}
8695
];
8796

0 commit comments

Comments
 (0)