adderbyte / GYM_XPLANE_ML

GYM Environment for XPlane. Reinforcement Learning and Autonomous Piloting.
GNU General Public License v3.0
66 stars 21 forks source link

Resetting the simulation #10

Closed sr71684 closed 4 years ago

sr71684 commented 4 years ago

How should I reset an X-plane session back to its initial state?

So far I have tried sending a set of DataRefs (position, elevation, heading, etc) to X-Plane, like so:

        self.client.sendDREF("sim/flightmodel/position/local_vx", 0)
        self.client.sendDREF("sim/flightmodel/position/local_vy", 0)
        self.client.sendDREF("sim/flightmodel/position/local_vz", 0)

        self.client.sendDREF("sim/flightmodel/position/theta", 0)
        self.client.sendDREF("sim/flightmodel/position/phi", 0)
        self.client.sendDREF("sim/flightmodel/position/psi", 0)

        self.client.sendDREF("sim/flightmodel/position/local_ax", 0)
        self.client.sendDREF("sim/flightmodel/position/local_ay", 0)
        self.client.sendDREF("sim/flightmodel/position/local_az", 0)

        self.client.sendDREF("sim/flightmodel/position/P", 0)
        self.client.sendDREF("sim/flightmodel/position/Q", 0)
        self.client.sendDREF("sim/flightmodel/position/R", 0)

        self.client.sendDREF("sim/flightmodel/position/true_theta", 0)
        self.client.sendDREF("sim/flightmodel/position/true_phi", 0)
        self.client.sendDREF("sim/flightmodel/position/true_psi", 0) 

But this does not always result in a perfect reset, occasionally there are subtle changes between resets.

Is it possible to reset by reloading an X-Plane situation file?

Additionally, could someone clarify how the reloadScript.lua is called? This does not appear to be documented beyond simply placing it in the X-Plane scripts folder.

adderbyte commented 4 years ago

Hi, the line 87 of the reloadScript.lua is the beginning of the definition of the function let_XPLM_reload_the_scenery() . This function consist of 3 if conditions that are checked often while the simulation is running - this is embodied in line 184 do_often("let_XPLM_reload_the_scenery()") of the script. Note that do_often comes as a predefined function with the flightwithua package (the package itself ensures seamless interaction with XPlane).

Now let's go through the the 3 if conditions defined in the let_XPLM_reload_the_scenery() function:

  1. At line 75 we set the variable ground DataRef("ground", "sim/flightmodel2/gear/on_ground") . The first if condition ( line 96) says if this variable is greater than 1 (which translate to the aircraft being on the ground ) then reload the situation (line 96 load_situation(SYSTEM_DIRECTORY .. "/Output/situations/keepHeading.sit" ) )

  2. The second if condition says if the crasher variable defined in line 77 is less then 1 (which means the aircraft has crashed) then reload the simulation (line 134).

  3. The last if condition (line 139) works this way: there are 2 variables count and counter_check defined in the dictionary controlVariables (line 90), where count is incremented each time using the function get_count (line 49) - note that this increment happens if the previous 2 if conditions have not been violated. Now, this if condition tells the simulation to reload (line 153) if the count (which is incremented) is greater than counter_check (counter_check was set at 90, but you can change it). Somehow we have to tell the simulation to stop at some point and the last if condition help achieves this.

    In particular, it should be noted that since the aim is to train an agent over multiple iterations, the let_XPLM_reload_the_scenery() makes it possible to reload the situation at each iteration. For example if at iteration one the agent crashes the aircraft, then the second if statement says reload the simulation. If the agent makes faulty command that results in the aircraft being on the ground while it should be in the air, then condition one says reload the simulation. However, if the agent is doing well, then it should be at the if-condition 3 where it pilots the aircraft up to the actual target point without any crash.

The reloadScript script as explained above actually reloads the situation file load_situation(SYSTEM_DIRECTORY .. "/Output/situations/keepHeading.sit" ) and you can change this to another situation file. So, to reload any situation, define the situation and replace keepHeading.sit with your favorite situation. Then use the reloadScript.lua to help reload the situation. You can also play with when you want the situation to be reloaded using the the discussion above.

I guess this summarizes it and should help define your own custom logic. I am sorry this was very long.

Best regards.

JDatPNW commented 3 years ago

Hello @conting3ncy and @adderbyte! I have read this issue and I am trying something similar to what @conting3ncy is doing, with the sendDREFs. Did you ever figure out how to reset the plane perfectly, maybe even with a starting velocity? I am not really familiar with lua, so my first instinct was to try running it in pure python.