JSBSim-Team / jsbsim

An open source flight dynamics & control software library
GNU Lesser General Public License v2.1
1.33k stars 444 forks source link

Is there a way initialise the simulation with the aircraft in particular state (i.e.: aircraft is airborne at a particular state, with particular speed etc)? #298

Closed DheerendraTomar closed 4 years ago

DheerendraTomar commented 4 years ago

Hi everyone,

I am new to JSBSim and would like to know If it is possible to initialise a320 in JSBSim in a particular state? Also, Can we use joystick's input to control the aircraft directly? I came across this JSBSim newsletter in which they use sockets for changing the properties?

Please let me know , if you need more clarity of what I am trying to do?

kvnjonathan70 commented 4 years ago

well you can start from this link below https://jsbsim-team.github.io/jsbsim/classJSBSim_1_1FGOutput.html

seanmcleod commented 4 years ago

Take a look at the initialization file support.

For example here is aircraft\737\cruise_init.xml

<?xml version="1.0" encoding="utf-8"?>
<initialize name="cruise">
  <!--
    This file sets up the aircraft to start off
    at cruise conditions.
  -->
  <altitude unit="FT"> 30000.0 </altitude>
  <vt unit="FT/SEC">     750.0 </vt>
  <gamma unit="DEG">       0.0 </gamma>
  <latitude unit="DEG">   47.0 </latitude>
  <longitude unit="DEG"> 122.0 </longitude>
  <phi unit="DEG">         0.0 </phi>
  <psi unit="DEG">       225.0 </psi>
  <beta unit="DEG">        0.0 </beta>
</initialize>

Typically you'll want to trim the aircraft at these starting conditions. You can either initiate the trim from within a script, for example take a look at scripts\737_cruise.xml, here are a couple of relevant snippets from it.

  <use aircraft="737" initialize="cruise_init"/> 

    <!--
      For "do_simple_trim" (Classic trim):
      0: Longitudinal
      1: Full
      2: Ground
      3: Pullup
      4: Custom
      5: Turn
      6: None
    -->

    <event name="Trim">
      <condition> simulation/sim-time-sec gt 0.1 </condition>
      <set name="simulation/do_simple_trim" value="1"/>
      <delay>5.0</delay>
      <notify>
        <property>propulsion/engine[0]/n2</property>
        <property>propulsion/engine[1]/n2</property>
        <property>propulsion/engine[0]/thrust-lbs</property>
        <property>propulsion/engine[1]/thrust-lbs</property>
        <property>velocities/vc-kts</property>
        <property>velocities/vc-fps</property>
        <property>velocities/vt-fps</property>
        <property>attitude/phi-rad</property>
        <property>attitude/theta-rad</property>
        <property>attitude/psi-rad</property>
      </notify>
    </event>

Or you can also have the trim initiated when the initialisation file is read in by adding a <trim> element. If you do go with this approach and you're trimming an aircraft in the air as opposed to on the ground then you'll want to also add a <running> element to ensure that the engines are running, e.g.

 <running> -1 </running>
 <trim> 1 </trim>

The -1 means all engines.

Not sure what you're planning to do with JSBSim, but I presume you've looked at FlightGear?

DheerendraTomar commented 4 years ago

well you can start from this link below https://jsbsim-team.github.io/jsbsim/classJSBSim_1_1FGOutput.html

@kvnjonathan70 Thank you

DheerendraTomar commented 4 years ago

@seanmcleod Thank you so much. This script you provided helps a lot. Well, I want a RL environment. I built one using X-Plane before as I was very new to this field and didn't know much about simulator, FDMs etc. The main problem I face with my X-Plane RL environment is that every time when the episode terminates and things reset from start, X-Plane is highly unstable and upsets the aircraft. I don't need GUI for my work and I didn't find any way to use X-Plane's FDM. That's how I came across JSBSim and I have realised that it is the best thing for my project. I have also came across this RL environment and will be exploring it.

In the end, I am very thankful for the open source community for support.

seanmcleod commented 4 years ago

@DheerendraTomar okay so RL = Reinforcement Learning I assume? Based on the RL environment link you mentioned.

DheerendraTomar commented 4 years ago

@seanmcleod Yes. RL = Reinforcement Learning. If you have some inputs to give I would welcome your ideas.

seanmcleod commented 4 years ago

I just glanced at Gor-Ren's MSc thesis and it looks like it provides a pretty useful basis for RL using JSBSim.

DheerendraTomar commented 4 years ago

I just glanced at Gor-Ren's MSc thesis and it looks like it provides a pretty useful basis for RL using JSBSim.

Thank you. I am reading the same.

DheerendraTomar commented 4 years ago

Can someone help me with this?

`

import jsbsim
import time

fdm = jsbsim.FGFDMExec('C:\JSBSim', None)
fdm.load_model('A320')
fdm.load_ic('basic_ic.xml', True)
fdm.run_ic()
fdm.set_output_directive('data_output/flightgear.xml' )
#fdm.load_script('scripts/737_cruise.xml')

fdm.print_simulation_configuration()
frame_duration = fdm.get_delta_t()
sleep_nseconds = (frame_duration if True else sleep_period) * 1E9
current_seconds = initial_seconds = time.time()
result = fdm.run()

print(result)

while result and fdm.get_sim_time() <= 100.0:
    current_seconds = time.time()
    actual_elapsed_time = current_seconds - initial_seconds
    sim_lag_time = actual_elapsed_time - fdm.get_sim_time()

    for _ in range(int(sim_lag_time / frame_duration)):
        result = fdm.run()
        current_seconds = time.time()

`

The initialization file for the aircraft is at pastebin basic_ic

The set ouput directive script(flightgear.xml) goes like this...

` <?xml version="1.0" encoding="utf-8"?>

<output name="localhost" type="FLIGHTGEAR" protocol="UDP" port="5550" rate="60">
</output>

`

Flight Gear is started as mentioned in flightgear.xml file.

The console outputs are show in the the snapshots in this drive link

I need a little guidance to set this up and How JSBSim runs the simulation when I run fdm.run(). Does it run whole simulation at once or it runs one time step and wait for next instruction.

Note: I don't have any script file. I want the stick controls and throttle to provided by python and then run a single time step in the simulation and observe the changes in Flight Gear (Once everything works fine, will deactivate flightgear part). I will be later taking outputs like aircraft state from JSBSim as well.

seanmcleod commented 4 years ago

fdm.run() executes a single time-step.

Here is a small snippet from one of the python tests, found in tests\TestActuator.py.

So it processes the initial conditions and then sits in a loop executing a single time-step until a specified amount of time has elapsed.

Within the loop it checks the position of an aileron. In your case during each time-step you could set the inceptor (stick, rudder, throttle etc.) command.

        fdm.run_ic()

        while fdm.run() and fdm.get_sim_time() < time_limit:
            aileron_pos = fdm['fcs/left-aileron-pos-rad']
            self.assertEqual(aileron_pos, 0.0,
                            msg="Failed running the script %s at time step %f\nProperty fcs/left-aileron-pos-rad is non-zero (%f)" % (self.script_path, fdm.get_sim_time(), aileron_pos))
DheerendraTomar commented 4 years ago

@seanmcleod Thank you so much for the explanation. The test files explains a lot, will explore them and update.

DheerendraTomar commented 4 years ago

I am using A320. The aircraft is airborne at 5000 feet. But, I am facing this strange problem. Thust from one of engine is decreasing, I can't figure it out.

The basic code for this is: pastebin_code

The console output for the above code is: engine_thrust_problem

seanmcleod commented 4 years ago

Instead of

fdm['fcs/throttle-cmd-norm'] = 0.8

try

fdm['fcs/throttle-cmd-norm[-1]'] = 0.8

Take a look at the implementation of SetThrottleCmd, if you pass -1 then the command will be applied to all engines. Without specifying an index in fdm['fcs/throttle-cmd-norm'] it defaults to index 0.

https://github.com/JSBSim-Team/jsbsim/blob/15fe952905f9b153d05f5879634717bcac57c063/src/models/FGFCS.cpp#L315-L329

DheerendraTomar commented 4 years ago

Alright. Thank you.

DheerendraTomar commented 4 years ago

@seanmcleod It works like charm, like this

fdm['fcs/throttle-cmd-norm'[-1]] = 0.8

seanmcleod commented 4 years ago

@DheerendraTomar do you have enough information now to initialise the simulation with a particular starting state for us to close this issue?

DheerendraTomar commented 4 years ago

@seanmcleod Yes. I moved ahead with my simulation. Thank you for your help. I will close this issue now and would reopen when I need to ask something else. Apologies for the delay in closing the issue. Thanks

bcoconni commented 4 years ago

@DheerendraTomar Thanks for your feedback.

Would you need some further support, please open a new issue rather than reopening this one. There are no limits to the number of issues that can be opened in a GitHub project so let's take advantage of it. It is easier afterwards to reference an issue when it addresses only one topic (see issue #xyz) rather than to specify which part of an issue is relevant for reference (see message abc in issue #xyz).

Thanks in advance.