Pomax / are-we-flying

Writing auto-pilots in JavaScript for MSFS
40 stars 2 forks source link

Error when Running API Server with MSFS: Illegal Offset RangeError #24

Closed mohammaddhaji closed 10 months ago

mohammaddhaji commented 10 months ago

Hi. I have problem running the code. Below is the API server log when i run it while MSFS is up:

No ALOS data folder specified, elevation service will not be available.
Server listening on http://localhost:8080
Connected to MSFS
Registering API server to PAUSE, UNPAUSED, CRASH, and SIM events.
new flight started, resetting autopilot
Server to server socket established
sending MSFS event
sending MSFS event
adding event listener for SIM
registering event listener with the simconnect wrapper
adding event listener for PAUSED
registering event listener with the simconnect wrapper
adding event listener for UNPAUSED
registering event listener with the simconnect wrapper
adding event listener for VIEW
registering event listener with the simconnect wrapper
RecvException { exception: 7, sendId: 21, index: 2 }
RecvException { exception: 7, sendId: 22, index: 2 }
RecvException { exception: 7, sendId: 26, index: 2 }
D:\Programming\Javascript\are-we-flying\node_modules\bytebuffer\dist\bytebuffer-node.js:1403
                throw RangeError("Illegal offset: 0 <= "+offset+" (+"+8+") <= "+this.buffer.length);
                ^

RangeError: Illegal offset: 0 <= 616 (+8) <= 620
    at module.exports.ByteBufferPrototype.readFloat64 (D:\Programming\Javascript\are-we-flying\node_modules\bytebuffer\dist\bytebuffer-node.js:1403:23)
    at RawBuffer.readFloat64 (D:\Programming\Javascript\are-we-flying\node_modules\node-simconnect\dist\RawBuffer.js:75:28)
    at Object.read (file:///D:/Programming/Javascript/are-we-flying/node_modules/msfs-simconnect-api-wrapper/simvars/simvar-utils.js:8:44)
    at file:///D:/Programming/Javascript/are-we-flying/node_modules/msfs-simconnect-api-wrapper/msfs-api.js:257:52
    at Array.forEach (<anonymous>)
    at SimConnectConnection.handleDataRequest (file:///D:/Programming/Javascript/are-we-flying/node_modules/msfs-simconnect-api-wrapper/msfs-api.js:256:21)
    at SimConnectConnection.emit (node:events:524:35)
    at SimConnectConnection.emit (D:\Programming\Javascript\are-we-flying\node_modules\node-simconnect\dist\SimConnectConnection.js:89:22)
    at SimConnectConnection._handleMessage (D:\Programming\Javascript\are-we-flying\node_modules\node-simconnect\dist\SimConnectConnection.js:1042:22)
    at SimConnectSocket.emit (node:events:512:28)

Node.js v19.5.0

And the web server log:

Server listening on http://localhost:3000
no API server (yet), retrying in 5 seconds
Client socket established.
API Server socket established
going back into wait mode
no API server (yet), retrying in 5 seconds

I don't get this error when I run API server before MSFS. But in that case nothing works. (when i click on take-off nothing happens and I can't put waypoints on map).

Node version: 19.5.0 .env file:

export DATA_FOLDER=
export ALOS_PORT=9000
export WEB_PORT=3000
export API_PORT=8080
export FLIGHT_OWNER_KEY=FOK-12345

Is there anything that I'm missing?

Pomax commented 10 months ago

The main branch is currently stale, I'm actively reworking the whole thing in the socketless branch instead (but note that while that work is ongoing, that branch may or may not be in a working state)

mohammaddhaji commented 10 months ago

That's correct. Is there a way I can test the main branch as you showed in the video you posted on YouTube?

Pomax commented 10 months ago

not really, that video was from back when things were Python + JS. I got rid of the Python part fairly quickly because it was terribly slow and python is just the worst to work with if you need async code behaviour, so the main branch was the first JS-only rewrite, and now (as in, literally right now, as I type this) I'm updating it to a much better client/server foundation, and updating the api dependencies. That work is nearing completion, and you can check out specific commits, such as https://github.com/Pomax/are-we-flying/commit/85359baaf6514277fd31a4c9ade828e863bb6365 - that one should work, but won't be as clean as things will be when I switch everything over.

And of course, the new branch code is rather different from the tutortial that's currently up. I'm obviously also updating that as I update the code, but it won't be done until after the code updates are.

mohammaddhaji commented 10 months ago

That's great. I look forward to your wonderful work. I tested the commit you mentioned. Apparently it needs some work to work properly. It would be great if I had your Python + JS version of the code, it could help me a lot in writing my own autopilot.

Pomax commented 10 months ago

you'll have to skip through the commit history then, but Python is absolutely not the right choice for this (my profiling showed it as running literally 3x as slow as Node, and when you're working against a real time simulation via a DLL, you can't afford to be that slow, especially not when your scheduler is not built for asynchronous execution. Not even "parallel", which neither Node nor Python are, but even just "asynchronous")

mohammaddhaji commented 10 months ago

Sorry I didn't understand what you mean by "skip through the commit history". I checked the first commit in main branch, but there wasn't any python code.

Apart from that, I have some questions:

1- In the last commit of the socketless branch, I still get that RangeError: Illegal offset error that I mentioned in my first comment when the aircraft is in the airport and ready to fly and I run the run.bat. But it's not the case when I run the run.bat first. I'm new to JavaScript and I can't figure out what this error is about. Do you have any idea how can I fix that?

2- Is it possible for the marker on the map to be updated in this current commit?

3- When I click on take off, the value of the model variable in run method of AutoTakeoff class is false, and that causes an error to occur in the next few lines in that function. What do you think is the reason?

Pomax commented 10 months ago

I meant going through the git log to find the commit that still has python, but it looks like I straight up deleted the python code and all associated commits when I did the initial JS rewrite (digging through my dev logs, I apparently did this because the git history had become several GB of data, and since I was doing a rewrite anyway, it was way easier to just delete the repo and then recreate it with an empty history than it was to try to force github to prune its stored .git data)

  1. how are you running things? And please include details: which OS, which version of MSFS, which version of Node, are you running any custom anti-virus or other file watchers, etc.
  2. I am constantly click-dragging them to move them around, clicking them to add/remove altitude values, and double-clicking them to remove them, so if that's what you mean: yes
  3. I'd have to see your api-server and web-server console logs. I only know of one plane that will do that (the x-cub) and I'm currently trying to find out why, myself.

In the browser, you open dev tools and then run TEST.state to get a full readout of what the client and browser know the game state to be, it would be helpful to see that in JSON form, too.

Pomax commented 10 months ago

Turns out the Xcub was not doing anything because apparently not every plane is well designed enough to report a value through the ELECTRICAL_TOTAL_LOAD_AMPS sim var, so I've also added a check on ELECTRICAL_AVIONICS_BUS_VOLTAGE to see if a plane's powered on. So that might have been the problem with whatever plane you were using, too.

mohammaddhaji commented 10 months ago

1- My OS is a fresh windows 11 without any anti-virus, the MSFS 2020 version is 1.7.12.0 and the node version is 19.5.0.

I select the c152 plane, hit the ready to fly button and get into game and run the run.bat. after that I will get that error. Note that if i run run.bat file before hitting ready to fly, I'll not get any error.

this is the API server console log the for the first case:

No ALOS data folder specified, elevation service will not be available.
resetting autopilot
Server listening on http://localhost:8080
Connected to MSFS.
resetting autopilot
authenticating client
RecvException { exception: 7, sendId: 26, index: 2 }
RecvException { exception: 7, sendId: 27, index: 2 }
RecvException { exception: 7, sendId: 31, index: 2 }
D:\Programming\Javascript\are-we-flying\node_modules\bytebuffer\dist\bytebuffer-node.js:1403
                throw RangeError("Illegal offset: 0 <= "+offset+" (+"+8+") <= "+this.buffer.length);
                ^

RangeError: Illegal offset: 0 <= 616 (+8) <= 620
    at module.exports.ByteBufferPrototype.readFloat64 (D:\Programming\Javascript\are-we-flying\node_modules\bytebuffer\dist\bytebuffer-node.js:1403:23)
    at RawBuffer.readFloat64 (D:\Programming\Javascript\are-we-flying\node_modules\node-simconnect\dist\RawBuffer.js:75:28)
    at Object.read (file:///D:/Programming/Javascript/are-we-flying/node_modules/msfs-simconnect-api-wrapper/simvars/simvar-utils.js:8:44)
    at file:///D:/Programming/Javascript/are-we-flying/node_modules/msfs-simconnect-api-wrapper/msfs-api.js:264:52
    at Array.forEach (<anonymous>)
    at SimConnectConnection.handleDataRequest (file:///D:/Programming/Javascript/are-we-flying/node_modules/msfs-simconnect-api-wrapper/msfs-api.js:263:21)
    at SimConnectConnection.emit (node:events:524:35)
    at SimConnectConnection.emit (D:\Programming\Javascript\are-we-flying\node_modules\node-simconnect\dist\SimConnectConnection.js:85:22)
    at SimConnectConnection._handleMessage (D:\Programming\Javascript\are-we-flying\node_modules\node-simconnect\dist\SimConnectConnection.js:988:22)
    at SimConnectSocket.emit (node:events:512:28)

Node.js v19.5.0

2- While the plane is flying, the plane marker on the map is not updating it's position. I am able to add/remove and drag the waypoints but it seems that the plane does not follow them.

3- API server log:

No ALOS data folder specified, elevation service will not be available.
resetting autopilot
Server listening on http://localhost:8080
Connected to MSFS.
authenticating client
received params { ATO: true }
changing ATO from false to true
file:///D:/Programming/Javascript/are-we-flying/src/autopilot/auto-takeoff.js:73
      let trimLimit = pitchTrimLimit[0];
                                    ^

TypeError: Cannot read properties of undefined (reading '0')
    at AutoTakeoff.run (file:///D:/Programming/Javascript/are-we-flying/src/autopilot/auto-takeoff.js:73:37)
    at AutoPilot.runAutopilot (file:///D:/Programming/Javascript/are-we-flying/src/autopilot/autopilot.js:301:24)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Node.js v19.5.0

Web server log:

Server listening on http://localhost:3000
client connected to server
reconnected
disconnected
trying to reconnect to the server...
trying to reconnect to the server...

I set the trimLimit to a const value of 10 and then take off was successful but after some time it gives another error in altitudeHold function for same variable.

TEST.state in browser dev tool after click on take off:

{
    "autopilot": {
        "MASTER": true,
        "waypoints": [
            {
                "id": 1,
                "number": 1,
                "lat": 48.751005650769144,
                "long": -123.70021820068361,
                "alt": false,
                "landing": false,
                "NM": 1,
                "active": false,
                "completed": false
            }
        ],
        "elevation": false,
        "LVL": false,
        "HDG": 133.1,
        "ALT": false,
        "ATT": false,
        "TER": false,
        "ATO": true,
        "ACR": false,
        "INV": false
    },
    "crashed": false,
    "flightData": false,
    "flightModel": false,
    "flying": false,
    "MSFS": true,
    "offline": false,
    "paused": true,
    "id": "18c2c6a13ac-3ad7d",
    "camera": {
        "main": 10,
        "sub": 0
    },
    "authenticated": true,
    "connected": true
}
Pomax commented 10 months ago

Cheers, I'll see if I can reproduce that. I'm on Win10, but that shouldn't really make a difference in this case.

I set the trimLimit to a const value of 10 and then take off was successful but after some time it gives another error in altitudeHold function for same variable.

Could I ask you to put that error here, as well? Might help figure out what's going on.

Pomax commented 10 months ago

Also, MSFS version question: the current version is 1.34.16.0, and MSFS won't start without running the updater... how are you still on the 1.7.12.0 launch version?

(and if piracy: that happens, but then (a) there's not much I can do for you, because I'm writing this code against the current version of MSFS and its associated SimConnect DLL, which is quite a number of versions newer than at launch, and (b) you did just miss the opportunity to actually buy the game for nice and cheap during the MSFS black friday sales)

Pomax commented 10 months ago

This might be a matter of being on too old a version of MSFS, as MSFS updates have also come with several SimConnect updates. If I run run.bat after setting up a Cessna 152 on a runway, I don't get any errors, and yes: the takeoff isn't the greatest, with a fair bit of VS oscillation going on that I've not seen in other planes:

image

But then I never fly the 152 so that'll make a decent test case from now on.

That said, it does eventually make it into the regular autopilot, and it properly tracks and targets waypoints the way it's supposed to.

image

mohammaddhaji commented 10 months ago

The problem was with the MSFS version. I had configured it to run in offline mode. I updated it now and the errors have been fixed. Thank you very much.

Pomax commented 10 months ago

Good to hear! I just pushed an update to the socketless branch that should significantly improve take-off for pretty much all previously problematic planes, so give that a shot and the 152 should take off pretty smoothly now.