-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
410 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { SimConnectPeriod, MSFS_API } from "../msfs-api.js"; | ||
import { watch } from "./util/reload-watcher.js"; | ||
|
||
let updateElevator, updateAileron; | ||
await watch(import.meta.dirname, "util/pid-code.js", (lib) => { | ||
updateElevator = lib.updateElevator; | ||
updateAileron = lib.updateAileron; | ||
}); | ||
|
||
let stop = () => {}; | ||
const api = new MSFS_API(); | ||
|
||
(async function tryConnect() { | ||
console.log(`Testing call prevention prior to connection`); | ||
console.log(`Awaiting connection`); | ||
api.connect({ | ||
autoReconnect: true, | ||
retries: Infinity, | ||
retryInterval: 5, | ||
onConnect: connect, | ||
onRetry: (_retries, interval) => | ||
console.log(`Connection failed: retrying in ${interval} seconds.`), | ||
}); | ||
})(); | ||
|
||
async function connect() { | ||
console.log(`MSFS connected`); | ||
|
||
let time = Date.now(); | ||
|
||
// Ask simconnect to update us on the state of | ||
// a small set of vars every frame. | ||
stop = api.periodic( | ||
(data) => { | ||
// But let's only run code (at most) 30 times a second. | ||
const now = Date.now(); | ||
if (now - time < 30) return; | ||
time = now; | ||
|
||
console.log(`.`); | ||
|
||
// Also, don't run if the AP is on. | ||
if (data.AUTOPILOT_MASTER === 1) return; | ||
|
||
// VS0 mode | ||
{ | ||
const elevator = data.AILERON_POSITION; | ||
const VS = data.VERTICAL_SPEED * 60; // fpm; | ||
updateElevator(api, elevator, VS); | ||
} | ||
|
||
// LVL mode | ||
{ | ||
const aileron = data.AILERON_POSITION; | ||
const bank = (data.PLANE_BANK_DEGREES * 180) / Math.PI; | ||
const turnRate = (data.TURN_INDICATOR_RATE * 180) / Math.PI; | ||
const heading = (data.PLANE_HEADING_DEGREES_MAGNETIC * 180) / Math.PI; | ||
updateAileron(api, aileron, bank, turnRate, heading); | ||
} | ||
}, | ||
SimConnectPeriod.SIM_FRAME, | ||
`AUTOPILOT_MASTER`, | ||
`AILERON_POSITION`, | ||
`ELEVATOR_POSITION`, | ||
`PLANE_BANK_DEGREES`, | ||
`TURN_INDICATOR_RATE`, | ||
`PLANE_HEADING_DEGREES_MAGNETIC`, | ||
`VERTICAL_SPEED` | ||
); | ||
} | ||
|
||
process.on("SIGINT", function () { | ||
console.log(`running stop first...`); | ||
stop(); | ||
setInterval(() => { | ||
process.exit(); | ||
}, 1000); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { PIDController } from "./pid-controller.js"; | ||
|
||
let elevatorPID = new PIDController(1, 0.5, 0); | ||
elevatorPID.setTarget(0); | ||
|
||
let aileronPID = new PIDController(1, 0, 1); | ||
aileronPID.setTarget(0); | ||
|
||
export function updateElevator(api, elevator, VS) { | ||
return; | ||
const recommendation = elevatorPID.update(VS); | ||
const correction = constrainMap(recommendation, -1000, 1000, -1, 1); | ||
api.set(`ELEVATOR_POSITION`, constrain(elevator + correction, -1, 1)); | ||
} | ||
|
||
export function updateAileron(api, aileron, bank, turnRate, heading) { | ||
const hdiff = heading - 340; | ||
console.log(bank.toFixed(4), turnRate.toFixed(4), heading.toFixed(4), hdiff.toFixed(4)); | ||
if ((hdiff < 0 && turnRate >= 0) || (hdiff > 0 && turnRate <= 0)) { | ||
const recommendation = aileronPID.update(hdiff); | ||
const correction = constrainMap(recommendation, -3, 3, -0.01, 0.01); | ||
api.set(`AILERON_POSITION`, constrain(aileron + correction, -1, 1)); | ||
} | ||
} | ||
|
||
function map(v, ds, de, ts, te) { | ||
const d = de - ds; | ||
if (d === 0) return ts; | ||
return ts + ((v - ds) * (te - ts)) / d; | ||
} | ||
|
||
function constrain(v, m, M) { | ||
if (m > M) return constrain(v, M, m); | ||
return v > M ? M : v < m ? m : v; | ||
} | ||
|
||
function constrainMap(v, ds, de, ts, te) { | ||
return constrain(map(v, ds, de, ts, te), ts, te); | ||
} |
Oops, something went wrong.