Board index FlightGear Development Nasal

FG HIL simulation with Nasal help

Nasal is the scripting language of FlightGear.

FG HIL simulation with Nasal help

Postby EliasTarasov » Mon Nov 28, 2016 9:47 am

Hello!
I have FG JSBSim model for my aircraft and i want to use FG as part of HIL simulation. So i send some control data from external control system, and JSBSim model from FG sends back some accelerations, velocities, positions, so on.
It works ok except one detail: My aircraft is a tiltrotor, so to control it i have to send pitch angles for each engine.
For JSBSim, it's not a problem, since it has property /propulsion/engine[N]/pitch-angle-rad.
But FG doesn't have such property. Therefore, i cannot setup this property in protocol configuration file, like this:

<input>
<line_separator>newline</line_separator>
<var_separator>tab</var_separator>

<!-- Controls -->
<chunk>
<name>aileron</name>
<type>float</type>
<node>/controls/flight/aileron</node>
</chunk>
....
</input>

FG has property /controls/flight/aileron which is mapped to /fcs/aileron-cmd-norm for JSBSim.
But FG doesn't have property, say, /controls/flight/engine[0]/pitch-angle-rad which would be mapped to /propulsion/engine[0]/pitch-angle-rad.

The workaround i suppose is:
1. Create nasal script that would have property, say, /controls/engines/engine[0]/pitch-angle-rad.
2. Expose this property to the protocol, say:
<input>
<line_separator>newline</line_separator>
<var_separator>tab</var_separator>

<!-- Controls -->
<chunk>
<name>engine_0_pitch</name>
<type>float</type>
<node>/controls/engines/engine[0]/pitch-angle-rad</node>
</chunk>
....
</input>
3. Write Nasal script that reads from FG's /controls/engines/engine[0]/pitch-angle-rad property and writes to JSBSim's /propulsion/engine[0]/pitch-angle-rad property.

The problem is that i don't have any experience with Nasal, and was unable to find any example, similar to the described above.
I suppose the situations when FG and JSBSim don't have directly mapped properties should be pretty common, and some examples must exist.
However, i didn't find any.

Could you please provide some examples or other hints, how to solve this task?
EliasTarasov
 
Posts: 16
Joined: Sun Mar 16, 2014 8:16 am

Re: FG HIL simulation with Nasal help

Postby Thorsten » Mon Nov 28, 2016 11:11 am

Is there an actual restriction that says you have to go through the FG control/ interface and prevents you from setting the JSBSim properties directly? I'm dealing with things like body flap or engine gimbal angles routinely without ever having gone through the FG interface.

If there is a restriction, I guess you may want to use a trivial autopilot property rule

http://wiki.flightgear.org/Autopilot_co ... _reference

(a gain with a factor 1) that just passes the property to another one rather than a Nasal script.
Thorsten
 
Posts: 12490
Joined: Mon Nov 02, 2009 9:33 am

Re: FG HIL simulation with Nasal help

Postby EliasTarasov » Mon Nov 28, 2016 12:10 pm

Thorsten, thanks for the reply!
So you say that i can directly setup the needed property in protocol communication file?

<input>
<line_separator>newline</line_separator>
<var_separator>tab</var_separator>

<!-- Controls -->
<chunk>
<name>engine_0_pitch</name>
<type>float</type>
<node>fdm/jsbsim/propulsion/engine[0]/pitch-angle-rad</node>
</chunk>
....
</input>

And FG automatically recognizes it and no more things are needs to be done?
EliasTarasov
 
Posts: 16
Joined: Sun Mar 16, 2014 8:16 am

Re: FG HIL simulation with Nasal help

Postby Thorsten » Mon Nov 28, 2016 2:17 pm

I don't know whether you can do it directly, but it'd be worth a try - JSBSim input properties are otherwise just normal properties from an FG perspective - you can set them from Nasal or manually for instance, so unless they're explicitly disabled in the communication protocol, you ought to be able to set them. There's no general requirement that commands and bindings have to go through /control in FG.
Thorsten
 
Posts: 12490
Joined: Mon Nov 02, 2009 9:33 am

Re: FG HIL simulation with Nasal help

Postby EliasTarasov » Wed Nov 30, 2016 1:15 pm

Unfortunately, this suggestion doesn't work.
For instance, if i put:

<!-- Controls -->
<chunk>
<name>throttle</name>
<type>float</type>
<node>/fdm/jsbsim/fcs/throttle-cmd-norm[0]</node>
</chunk>

and then send 0.9 for the 0 engine throttle, it doesn't work.

However, if i put:

<!-- Controls -->
<chunk>
<name>throttle</name>
<type>float</type>
<node>/controls/engines/engine[0]/throttle</node>
</chunk>

and then send 0.9 for the 0 engine throttle, it works as it is expected.

So, it means if there is no binding between FG and JSBSim properties, then it is impossible to address data to JSBSim through FG.
For the throttles is ok, but, for instance, for the pitch angle of the engine it is not.
EliasTarasov
 
Posts: 16
Joined: Sun Mar 16, 2014 8:16 am

Re: FG HIL simulation with Nasal help

Postby AndersG » Wed Nov 30, 2016 3:24 pm

Have you tried using the JSBSim property for engine pitch angle directly?
It might work. Unlike with /fdm/jsbsim/fcs/throttle-cmd-norm[0] where you will have, as you discovered, interference from /controls/engines/engine[0]/throttle since FG copies the value of the latter to the former for each frame (or even each FDM iteration).
Callsign: SE-AG
Aircraft (uhm...): Submarine Scout, Zeppelin NT, ZF Navy free balloon, Nordstern, Hindenburg, Short Empire flying-boat, ZNP-K, North Sea class, MTB T21 class, U.S.S. Monitor, MFI-9B, Type UB I submarine, Gokstad ship, Renault FT.
AndersG
 
Posts: 2524
Joined: Wed Nov 29, 2006 10:20 am
Location: Göteborg, Sweden
Callsign: SE-AG
OS: Debian GNU Linux

Re: FG HIL simulation with Nasal help

Postby EliasTarasov » Wed Nov 30, 2016 4:00 pm

Direct using of JSBSim properties was exactly what i tried.
The results:

1. Does not work.
<!-- Controls -->
<chunk>
<name>engine_0_pitch</name>
<type>float</type>
<node>fdm/jsbsim/propulsion/engine[0]/pitch-angle-rad</node>
</chunk>

2. Does not work.
<!-- Controls -->
<chunk>
<name>engine_0_throttle</name>
<type>float</type>
<node>/fdm/jsbsim/fcs/throttle-cmd-norm[0]</node>
</chunk>

3. Works.
<!-- Controls -->
<chunk>
<name>engine_0_throttle</name>
<type>float</type>
<node>/controls/engines/engine[0]/throttle</node>
</chunk>

4. Here should be FG property binded to JSBSim /fdm/jsbsim/propulsion/engine[0]/pitch-angle-rad
<!-- Controls -->
<chunk>
<name>engine_0_throttle</name>
<type>float</type>
<node>/controls/engines/engine[0]/pitch-angle-rad</node>
</chunk>

But there is no such property.

It seems, that it should be created, according to Thorsten's advice:
1. Create FG Autopilot file and attach it to FG/JSBSim model.
2. Within this AP file create component, say, filter, which would map /controls/engines/engine[0]/pitch-angle-rad to the /fdm/jsbsim/propulsion/engine[0]/pitch-angle-rad
<filter>
<type>gain</type>
<gain>1.0</gain>
<input> /controls/engines/engine[0]/pitch-angle-rad</input>
<output>/fdm/jsbsim/propulsion/engine[0]/pitch-angle-rad</output>
</filter>
3. Enable AP.

I hadn't try this approach yet.
EliasTarasov
 
Posts: 16
Joined: Sun Mar 16, 2014 8:16 am

Re: FG HIL simulation with Nasal help

Postby EliasTarasov » Thu Dec 01, 2016 10:52 am

It's almost finished.
I created autopilot.xml file and attached it to FG model.
Now i can see created property /controls/engines/engine[0]/pitch-angle-rad and when i send value 0.1 it is displayed, so this property receives the value correctly.
However, the property /fdm/jsbsim/propulsion/engine[0]/pitch-angle-rad is still zero.
I suppose that autopilot is just not enabled.
AP code is:

<PropertyList>

<filter>
<name>Simple Engine 0 Pitch Mapper</name>
<input>/controls/engines/engine[0]/pitch-angle-rad</input>
<output>/fdm/jsbsim/propulsion/engine[0]/pitch-angle-rad</output>
</filter>

</PropertyList>

How can i switch it on, please?
EliasTarasov
 
Posts: 16
Joined: Sun Mar 16, 2014 8:16 am

Re: FG HIL simulation with Nasal help

Postby EliasTarasov » Thu Dec 01, 2016 12:18 pm

I tried to use Nasal, but also failed

1. Added reference to Nasal script in <aircraft_name>-set.xml

<nasal>
<aircraft_name>
<file>Aircraft/aircraft_name/Systems/main.nas</file>
</aircraft_name>
</nasal>

2. Added the main.nas script itself into subfolder Systems

var main_loop = func {

engine_0_pitch_angle_rad = getprop("/controls/engines/engine[0]/pitch-angle-rad");
setprop("/fdm/jsbsim/propulsion/engine[0]/pitch-angle-rad", engine_0_pitch_angle_rad);
print ("engine_0_pitch_angle_rad = ", engine_0_pitch_angle_rad);

engine_1_pitch_angle_rad = getprop("/controls/engines/engine[1]/pitch-angle-rad");
setprop("/fdm/jsbsim/propulsion/engine[1]/pitch-angle-rad", engine_1_pitch_angle_rad);
print ("engine_1_pitch_angle_rad = ", engine_1_pitch_angle_rad);

engine_2_pitch_angle_rad = getprop("/controls/engines/engine[2]/pitch-angle-rad");
setprop("/fdm/jsbsim/propulsion/engine[2]/pitch-angle-rad", engine_2_pitch_angle_rad);
print ("engine_2_pitch_angle_rad = ", engine_2_pitch_angle_rad);

engine_3_pitch_angle_rad = getprop("/controls/engines/engine[3]/pitch-angle-rad");
setprop("/fdm/jsbsim/propulsion/engine[3]/pitch-angle-rad", engine_3_pitch_angle_rad);
print ("engine_3_pitch_angle_rad = ", engine_3_pitch_angle_rad);

settimer(main_loop, 0.1);
}


setlistener("/sim/signals/fdm-initialized",
func {
main_loop();
});

But it seems script doesn't work since the nasal console is empty and the values are not changed.
Now i really stuck.
EliasTarasov
 
Posts: 16
Joined: Sun Mar 16, 2014 8:16 am

Re: FG HIL simulation with Nasal help

Postby AndersG » Thu Dec 01, 2016 3:12 pm

Can you successfully write to /fdm/jsbsim/propulsion/engine[0]/pitch-angle-rad with the property browser?

The property is listed as read/write by JSBSim/standalone so it ought to be writeable, but if it is actually not then all ways of routing values there will fail.

However, it was possible to use these properties when I made the Zeppelin NT (ZLT-NT) and last time I tried the aircraft it was still working. In this case I have JSBSim FCS components writing to propulsion/engine[{0,1,2}]/pitch-angle-rad rather than FG side tools, though.
Callsign: SE-AG
Aircraft (uhm...): Submarine Scout, Zeppelin NT, ZF Navy free balloon, Nordstern, Hindenburg, Short Empire flying-boat, ZNP-K, North Sea class, MTB T21 class, U.S.S. Monitor, MFI-9B, Type UB I submarine, Gokstad ship, Renault FT.
AndersG
 
Posts: 2524
Joined: Wed Nov 29, 2006 10:20 am
Location: Göteborg, Sweden
Callsign: SE-AG
OS: Debian GNU Linux

Re: FG HIL simulation with Nasal help

Postby EliasTarasov » Thu Dec 01, 2016 4:57 pm

Solved. The problem was in setlistener() function call.
Since it waits for fdm to be initialized, then it starts to read user-defined property getprop("/controls/engines/engine[0]/pitch-angle-rad") which doesn't have any meaningful value yet.
If i bind main_loop(), say, to the menu item and initialize it after first data is sent to FG, then getprop("/controls/engines/engine[0]/pitch-angle-rad") works correctly.
EliasTarasov
 
Posts: 16
Joined: Sun Mar 16, 2014 8:16 am


Return to Nasal

Who is online

Users browsing this forum: No registered users and 4 guests