Canvas MFD Framework

From FlightGear wiki
Jump to: navigation, search
This article is a stub. You can help the wiki by expanding it.

Introduction

There is a simple implementation of the basic support for an MFD (or MPCD, or PFD), or any device that is basically a set of pages with buttons around the outside to select the page that is displayed.

This works with a SVG file that defines the menu label locations and has a group for each page. The framework manages the menu hierarchy and displays labels corresponding to the buttons.

This is based around the SpaceShuttle displays which are in turn based on the http://wiki.flightgear.org/McDonnell_Douglas_F-15_Eagle#MPCD

The code as below will allow multiple instances of a particular display to be created. Towards the end of the code an array is populated with one MFD_device instance per display. All use the same SVG and do the same job - but can be on different devices (left, right, etc) in the cockpit. The 3d model must have the frame, bezel and buttons for each display. The model_index selects which buttons to use. The buttons are named sim/model/f15/controls/PFD/button-pressed0 - where this property contains the index of the button, and there is one property per display.

MCPD Armament Top Level Menu

Implementation example

This uses a 3d rectangle called "MPCDImage". If there is likely to be more than one instance of an MFD then all of this logic should be wrapped up inside a class

# F-15 Canvas MPCD (Multi-Purpose-Colour-Display)
# ---------------------------
# MPCD has many pages; the classes here support multiple pages, menu
# operation and the update loop.
# 2016-05-17: Refactor to use Nasal/canvas/MFD_Generic.nas 
# ---------------------------
# Richard Harrison: 2015-01-23 : rjh@zaretto.com
# ---------------------------

#for debug: setprop ("/sim/startup/terminal-ansi-colors",0);

var MPCD_Station =
{
	new : func (svg, ident)
    {
		var obj = {parents : [MPCD_Station] };

        obj.status = svg.getElementById("PACS_L_"~ident);
        if (obj.status == nil)
            print("Failed to load PACS_L_"~ident);

        obj.label = svg.getElementById("PACS_V_"~ident);
        if (obj.label == nil)
            print("Failed to load PACS_V_"~ident);

        obj.selected = svg.getElementById("PACS_R_"~ident);
        if (obj.selected == nil)
            print("Failed to load PACS_R_"~ident);

        obj.selected1 = svg.getElementById("PACS_R1_"~ident);
        if (obj.selected1 == nil)
            print("Failed to load PACS_R1_"~ident);

        obj.prop = "payload/weight["~ident~"]";
        obj.ident = ident;

        setlistener(obj.prop~"/selected", func(v)
                    {
                        obj.update();
                    });
        setlistener("sim/model/f15/controls/armament/weapons-updated", func
                    {
                        obj.update();
                    });

        obj.update();
        return obj;
    },

    update: func
    {
        var weapon_mode = getprop("sim/model/f15/controls/armament/weapon-selector");
        var na = getprop(me.prop~"/selected");
        var sel = 0;
        var mode = "STBY";
        var sel_node = "sim/model/f15/systems/external-loads/station["~me.ident~"]/selected";
        var master_arm=getprop("sim/model/f15/controls/armament/master-arm-switch");

        if (na != nil and na != "none")
        {
            if (na == "AIM-9")
            {
                na = "9L";
                if (weapon_mode == 1)
                {
                    sel = getprop(sel_node);
                    if (sel and master_arm)
                        mode = "RDY";
                }
                else mode = "SRM";
            }
            elsif (na == "AIM-120") 
            {
                na = "120A";
                if (weapon_mode == 2)
                {
                    sel = getprop(sel_node);
                    if (sel and master_arm)
                        mode = "RDY";
                }
                else mode = "MRM";
            }
            elsif (na == "AIM-7") 
            {
                na = "7M";
                if (weapon_mode == 2)
                {
                    sel = getprop(sel_node);
                    if (sel and master_arm)
                        mode = "RDY";
                }
                else mode = "MRM";
            }
            me.status.setText(mode);
            me.label.setText(na);

            me.selected1.setVisible(sel);
            if (mode == "RDY")
            {
                me.selected.setVisible(sel);
                me.status.setColor(0,1,0);
            }
            else
            {
                me.selected.setVisible(0);
                me.status.setColor(1,1,1);
            }
        }
        else
        {
            me.status.setText("");
            me.label.setText("");
            me.selected.setVisible(0);
            me.selected1.setVisible(0);
        }
    },
};

var MPCD_Device =
{
#
# create new MFD device. This is the main interface (from our code) to the MFD device
# Each MFD device will contain the underlying PFD device object, the SVG, and the canvas
# Parameters
# - designation - Flightdeck Legend for this
# - model_element - name of the 3d model element that is to be used for drawing
# - model_index - index of the device
    new : func(designation, model_element, model_index=0)
    {
        var obj = {parents : [MPCD_Device] };
        obj.designation = designation;
        obj.model_element = model_element;
        var dev_canvas= canvas.new({
                "name": designation,
                           "size": [1024,1024], 
                           "view": [740,680],                       
                    "mipmapping": 1     
                    });                          

        dev_canvas.addPlacement({"node": model_element});
        dev_canvas.setColorBackground(0.003921,0.1764,0, 0);
# Create a group for the parsed elements
        obj.PFDsvg = dev_canvas.createGroup();
        var pres = canvas.parsesvg(obj.PFDsvg, "Nasal/MPCD/MPCD_0_0.svg");
# Parse an SVG file and add the parsed elements to the given group
        printf("MPCD : %s Load SVG %s",designation,pres);
        obj.PFDsvg.setTranslation (270.0, 197.0);
#
# create the object that will control all of this
        obj.num_menu_buttons = 20;
        obj.PFD = PFD_Device.new(obj.PFDsvg, obj.num_menu_buttons, "MI_", dev_canvas);
        obj.PFD._canvas = dev_canvas;
        obj.PFD.designation = designation;
        obj.mfd_device_status = 1;
        obj.model_index = model_index; # numeric index (1 to 9, left to right) used to connect the buttons in the cockpit to the display

        obj.addPages();
        return obj;
    },

    addPages : func
    {
        me.p1_1 = me.PFD.addPage("Aircraft Menu", "p1_1");

        me.p1_1.update = func
        {
            var sec = getprop("instrumentation/clock/indicated-sec");
            me.page1_1.time.setText(getprop("sim/time/gmt-string")~"Z");
            var cdt = getprop("sim/time/gmt");

            if (cdt != nil)
                me.page1_1.date.setText(substr(cdt,5,2)~"/"~substr(cdt,8,2)~"/"~substr(cdt,2,2)~"Z");
        };

        me.p1_1 = me.PFD.addPage("Aircraft Menu", "p1_1");
        me.p1_2 = me.PFD.addPage("Top Level PACS Menu", "p1_2");
        me.p1_3 = me.PFD.addPage("PACS Menu", "p1_3");
        me.p1_3.S0 = MPCD_Station.new(me.PFDsvg, 0);
        #1 droptank
        me.p1_3.S2 = MPCD_Station.new(me.PFDsvg, 2);
        me.p1_3.S3 = MPCD_Station.new(me.PFDsvg, 3);
        me.p1_3.S4 = MPCD_Station.new(me.PFDsvg, 4);
        #5 droptank
        me.p1_3.S6 = MPCD_Station.new(me.PFDsvg, 6);
        me.p1_3.S7 = MPCD_Station.new(me.PFDsvg, 7);
        me.p1_3.S8 = MPCD_Station.new(me.PFDsvg, 8);
        #9 droptank
        me.p1_3.S10 = MPCD_Station.new(me.PFDsvg, 10);

        me.pjitds_1 =  PFD_NavDisplay.new(me.PFD,"Situation", "mpcd-sit", "pjitds_1", "jtids_main");
        # use the radar range as the ND range.

        me.p_spin_recovery = me.PFD.addPage("Spin recovery", "p_spin_recovery");
        me.p_spin_recovery.cur_page = nil;

        me.p1_1.date = me.PFDsvg.getElementById("p1_1_date");
        me.p1_1.time = me.PFDsvg.getElementById("p1_1_time");

        me.p_spin_recovery.p_spin_cas = me.PFDsvg.getElementById("p_spin_cas");
        me.p_spin_recovery.p_spin_alt = me.PFDsvg.getElementById("p_spin_alt");
        me.p_spin_recovery.p_spin_alpha = me.PFDsvg.getElementById("p_spin_alpha");
        me.p_spin_recovery.p_spin_stick_left  = me.PFDsvg.getElementById("p_spin_stick_left");
        me.p_spin_recovery.p_spin_stick_right  = me.PFDsvg.getElementById("p_spin_stick_right");
        me.p_spin_recovery.update = func
        {
            me.p_spin_alpha.setText(sprintf("%d", getprop ("orientation/alpha-indicated-deg")));
            me.p_spin_alt.setText(sprintf("%5d", getprop ("instrumentation/altimeter/indicated-altitude-ft")));
            me.p_spin_cas.setText(sprintf("%3d", getprop ("instrumentation/airspeed-indicator/indicated-speed-kt")));

            if (math.abs(getprop("fdm/jsbsim/velocities/r-rad_sec")) > 0.52631578947368421052631578947368 
                or math.abs(getprop("fdm/jsbsim/velocities/p-rad_sec")) > 0.022)
            {
                me.p_spin_stick_left.setVisible(1);
                me.p_spin_stick_right.setVisible(0);
            }
            else
            {
                me.p_spin_stick_left.setVisible(0);
                me.p_spin_stick_right.setVisible(1);
            }
        };

        #
        # Page 1 is the time display
        me.p1_1.update = func
        {
            var sec = getprop("instrumentation/clock/indicated-sec");
            me.time.setText(getprop("sim/time/gmt-string")~"Z");
            var cdt = getprop("sim/time/gmt");

            if (cdt != nil)
                me.date.setText(substr(cdt,5,2)~"/"~substr(cdt,8,2)~"/"~substr(cdt,2,2)~"Z");
        };

        #
        # armament page gun rounds is implemented a little differently as the menu item (1) changes to show
        # the contents of the magazine.
        me.p1_3.gun_rounds = me.p1_3.addMenuItem(1, sprintf("HIGH\n%dM",getprop("sim/model/f15/systems/gun/rounds")), me.p1_3);

        setlistener("sim/model/f15/systems/gun/rounds", func(v)
                    {
                        if (v != nil) {
                            me.p1_3.gun_rounds.title = sprintf("HIGH\n%dM",v.getValue());
                            me.PFD.updateMenus();
                        }
                    }
            );
        me.PFD.selectPage(me.p1_1);
        me.mpcd_button_pushed = 0;
        # Connect the buttons - using the provided model index to get the right ones from the model binding
        setlistener("sim/model/f15/controls/MPCD/button-pressed", func(v)
                    {
                        if (v != nil) {
                            if (v.getValue())
                                me.mpcd_button_pushed = v.getValue();
                            else {
                                printf("%s: Button %d",me.designation, me.mpcd_button_pushed);
                                me.PFD.notifyButton(me.mpcd_button_pushed);
                                me.mpcd_button_pushed = 0;
                            }
                        }
                    }
            );

        # Set listener on the PFD mode button; this could be an on off switch or by convention
        # it will also act as brightness; so 0 is off and anything greater is brightness.
        # ranges are not pre-defined; it is probably sensible to use 0..10 as an brightness rather
        # than 0..1 as a floating value; but that's just my view.
        setlistener("sim/model/f15/controls/PFD/mode"~me.model_index, func(v)
                    {
                        if (v != nil) {
                            me.mfd_device_status = v.getValue();
                            print("MFD Mode ",me.designation," ",me.mfd_device_status);
                            if (!me.mfd_device_status)
                                me.PFDsvg.setVisible(0);
                            else
                                me.PFDsvg.setVisible(1);
                        }
                    }
            );

        me.mpcd_button_pushed = 0;
        me.setupMenus();
        me.PFD.selectPage(me.p1_1);
    },

    # Add the menus to each page. 
    setupMenus : func
    {
#
# Menu Id's
# 0           5            
# 1           6            
# 2           7            
# 3           8            
# 4           9            
#
# Top: 10 11 12 13 14 
# Bot: 15 16 17 18 19
        me.mpcd_spin_reset_time = 0;

        me.p1_1.addMenuItem(0, "ARMT", me.p1_2);
        me.p1_1.addMenuItem(1, "BIT", me.p1_2);
        me.p1_1.addMenuItem(2, "SIT", me.pjitds_1);
        me.p1_1.addMenuItem(3, "WPN", me.p1_2);
        me.p1_1.addMenuItem(4, "DTM", me.p1_2);

        me.p1_2.addMenuItem(1, "A/A", me.p1_3);
        me.p1_2.addMenuItem(2, "A/G", me.p1_3);
        me.p1_2.addMenuItem(3, "CBT JETT", me.p1_3);
        me.p1_2.addMenuItem(4, "WPN LOAD", me.p1_3);
        me.p1_2.addMenuItem(9, "M", me.p1_1);

        me.p1_3.addMenuItem(2, "SIT", me.pjitds_1);
        me.p1_3.addMenuItem(3, "A/G", me.p1_3);
        me.p1_3.addMenuItem(4, "2/2", me.p1_3);
        me.p1_3.addMenuItem(8, "TM\nPWR", me.p1_3);
        me.p1_3.addMenuItem(9, "M", me.p1_1);
        me.p1_3.addMenuItem(10, "PYLON", me.p1_3);
        me.p1_3.addMenuItem(12, "FUEL", me.p1_3);
        me.p1_3.addMenuItem(14, "PYLON", me.p1_3);
        me.p1_3.addMenuItem(15, "MODE S", me.p1_3);

        me.pjitds_1.addMenuItem(9, "M", me.p1_1);
    },

    update : func
    {
    # see if spin recovery page needs to be displayed.
    # it is displayed automatically and will remain for 5 seconds.
    # this page provides (sort of) guidance on how to recover from a spin
    # which is identified by the yar rate.
        if (!wow and math.abs(getprop("fdm/jsbsim/velocities/r-rad_sec")) > 0.52631578947368421052631578947368)
        {
            if (me.PFD.current_page != me.p_spin_recovery)
            {
                me.p_spin_recovery.cur_page = me.PFD.current_page;
                me.PFD.selectPage(me.p_spin_recovery);
            }
            me.mpcd_spin_reset_time = getprop("instrumentation/clock/indicated-sec") + 5;
        } 
        else
        {
            if (me.mpcd_spin_reset_time > 0 and getprop("instrumentation/clock/indicated-sec") > me.mpcd_spin_reset_time)
            {
                me.mpcd_spin_reset_time = 0;
                if (me.p_spin_recovery.cur_page != nil)
                {
                    me.PFD.selectPage(me.p_spin_recovery.cur_page);
                    me.p_spin_recovery.cur_page = nil;
                }
            }
        }

        if (me.mfd_device_status)
            me.PFD.update();
    },
};

#
# Mode switch is day/night/off. we just do on/off
setlistener("sim/model/f15/controls/MPCD/mode", func(v)
            {
                if (v != nil)
                {
                    MPCD.mpcd_mode = v.getValue();
#    if (!mpcd_mode)
#        MPCDcanvas.setVisible(0);
#    else
#        MPCDcanvas.setVisible(1);
                }
            });


#
# Connect the radar range to the nav display range. 
setprop("instrumentation/mpcd-sit/inputs/range-nm", getprop("instrumentation/radar/radar2-range"));
setlistener("instrumentation/radar/radar2-range", 
            func(v)
            {
                setprop("instrumentation/mpcd-sit/inputs/range-nm", v.getValue());
            });

var MFD_array = [];

#
# 
# Create and append all of the MFDs in the cockpit.
# - MFD_Device.new( Identity, Canvas3dSurface, model index)
#                 
append(MFD_array, MPCD_Device.new("F15-MPCD", "MPCDImage",0));

# update only one display per frame to reduce load. This can easily be changed
# to update all by looping around all of the displays in the MFD_array
var frame_device_update_id = 0;

var MFD_rtExec_update = func
{
    if (frame_device_update_id >= size(MDU_array))
        frame_device_update_id = 0;

    if (frame_device_update_id < size(MDU_array))
        MDU_array[frame_device_update_id].update();

    frame_device_update_id += 1;
    MFD_rtExec_timer.restart(0.02);
}

var MFD_rtExec_timer = maketimer(6, MFD_rtExec_update);
MFD_rtExec_timer.restart(6);

Background

Note  Please also see Howto:Coding a simple Nasal Framework
Cquote1.png I wish we could share more of our development on these things. Having lots of logic for route management and performance data and autopilot stuff re-implemented in every new "realistic" plane kinda sucks. It would be nice if we could build up the built-in system that covers 90% of the cases and any specialiation can be handled by refining the built-in system. Adding a new aircraft should be throw up a couple of displays and live with the built-in default ND and MFD in the first iteration, and then progressively refine it to match the real deal.
— alge (Feb 24th, 2016). Re: FMC.
(powered by Instant-Cquotes)
Cquote2.png
Cquote1.png If the coders here at FG spent their time (1000 hrs) making the apps / tools that take all the coding out of making stuff, then the modders those with little coding knowledge would spend the 3000 hrs doing the rest.
Cquote2.png
Cquote1.png as long as you keep coding with a generic mindset, i.e. to support use-cases like:


  • multiple independent instances per instrument/layer
  • different instruments/aircraft (i.e. with different AP/RM or NAV/COM dependencies)
  • the GUI use-case (see map-canvas.xml in $FG_ROOT/gui/dialogs)
  • styling (custom colors, fonts, symbols etc)
  • different position sources (aircraftpos.controller/NDSourceDriver)


None of this is rocket science or even very difficult - these are fairly basic things, but most people fail to keep all of them in mind, so that they end up coming up with instruments that only support a single use-case unfortunately.

But as long as people keep following a handful of guidelines, we can even easily support new use-cases, including even a standalone FGCanvas mode, without it involving a ton of porting work. Likewise, GUI dialogs for configuring weather or showing a GUI console, would then automatically be able to also support holding patterns, vertical profiles (VSD) and weather overlays.

And you really only need to know a subset of Nasal to apply these guidelines, and it will get you pretty far.


— Hooray (Mon Aug 11). Re: A330-200 with Canvas and other features.
(powered by Instant-Cquotes)
Cquote2.png

Hooray: Basically, my idea was to generalize the ND code some more and come up with helper classes for 1) SGSubsystems, 2) MFD instruments and 3) image generators, 4) displays and display switches/selectors. Introducing a handful of helper classes would allow us to support all important use-cases that are common in modern glass cockpits, including switching between different FMCs or PFDs/NDs. I would really prefer to closely coordinate things here - we really don't want to have people come up with hugely different approaches for different types of MFDs.

A common framework in the background (with some common elements like "rotating" numbers, HSI logic, trend vector calculations, etc.) is definitely a good idea.

Canvas MFD displays are just canvas textures, which are just represented as "properties", or rather, "property branches" - in the form of /canvas/by-index/texture[x] - you can use the property tree browser to check how these work - but basically, each canvas texture can have multiple "placements", such as "aircraft", "scenery" or GUI (dialog/window) - these placements maintain a reference count internally.

Toggling between different image sources is also accomplished by supporting recursion through "nested canvases" - i.e. a canvas can "include" (reference) another canvas and use it as the image source (raster image).

Thus, technically, the only thing involved would be coming up with two classes for "displays" and "image generators" - where each would be internally mapped to a canvas texture, the image generator would be "black box" where rendering takes place - while the "display" would be just a simple canvas that references the proper canvas texture, based on the currently selected switch/mode. Meanwhile, I would prefer coming up with a real MFD framework that manages different displays/screens and image sources.

That should then also help with PFD/ND/CDU and EICAS/EFIS stuff.

Philosopher's MapStructure framework has been specifically designed to support the notion of controllers for these things, so need to add any heavy hacks to the code - we should better work together and ensure that MapStructure ends up in fgdata soon enough ... Gijs already started working on a MFD creation framework for the 744, and as previously mentioned, certain features are going to be identical - regardless of aircraft, i.e. bizjet, boeing, airbus etc - most MFDs will have knobs to adjust brightness or change video sources - so I'd rather keep the general design in mind here, and not implement such things specifically for a certain aircraft. Ultimately, it really just boils down to mapping a few properties to the corresponding canvas properties.


Cquote1.png I also need a better way to switch pages on the lower EICAS. Right now I delete/re-create the Canvas with this code. It doesn't work well though; at times no page is loaded at all. Of course I cannot delete a Canvas when I have it displayed in a dialog, so this method is probably doomed... [1]
— Gijs
Cquote2.png


Cquote1.png I'm also doing some work on my C-130J cockpit and therefore have got nearly the same problems^^ There are currently five screens with a lot of pages which can be freely placed on any of the screens. I'm not yet sure on how to setup this system in detail. If displays/windows/etc. show exactly the same thing they should also use the same canvas. One approach would be to use a canvas for each page and add one ore more placements to it depending on where it should be displayed.

Another approach would be to use a canvas for each screen and either reload each page on switching or after loading once hide the according group. A completely different approach (which probably also will require some core changes) is to allow moving groups between different canvasses and also just to a storage location to move pages around as needed.

[2]
— TheTom
Cquote2.png


Cquote1.png It would probably be a good idea to look at existing airliners in FG, such as the 744, 777 and then come up with a simple Nasal-space framework to manage image sources and screens, so that a screen selector would ideally only manage placements, while supporting different MFDs for each pilot - analogous to how A661 has the concept of an image generator (IG) and a cockpit display system (CDS).

For most modern jets it would make sense to introduce some intermediate layer that wraps the main canvas system, so that different displays (PFD, ND, EICAS, M/CDU etc) can be conveniently managed.

Basically, we only need to add a handful of Nasal wrapper classes that provide the building blocks for any kind of EFIS, i.e. generic components such as:

cockpit developers would then ideally use existing components or add new ones as required, for different types of EFIS (777, 747, A320, A380).

PFD/ND and EICAS/ECAM or MCDUs would be built on top of these.[3]
— Hooray
Cquote2.png
Cquote1.png Canvas & Nasal are still fairly low-level for most aircraft developers, to come up with good -and fast displays (code)- people still need to be experienced coders, and familiar with FlightGear scripting and Canvas technologies/elements and the way performance is affected through certain constructs. So far, we now have the means to create the corresponding visuals, but there's still quite some work ahead to re-implement existing hard-coded displays - but to implement a compelling jet fighter, including a credible cockpit, you would need more than "just" the visuals, i.e. lots of handbooks/manuals, building blocks for creating systems and components, and scripting-space frameworks to help with the latter. The best option to pave the way for this is to keep on generalizing existing code, so that instruments support multiple instances, multiple aircraft, and multiple "sensors". Here, galvedro's work is really promising. But for the "visual" side of this, we really need to generalize our NavDisplay code much more - so that we can factor out MFD related functionality, and reuse it on fighters like the m2000-5.
Cquote2.png
  1. Gijs (Sun Oct 13, 2013 9:04 am). The making of the Queen.
  2. TheTom (Mon Oct 14, 2013 5:02 am). The making of the Queen.
  3. Hooray (Mon Oct 14, 2013 5:02 am). The making of the Queen.

Design

This article or section contains out-of-date information

Please help improve this article by updating it. There may be additional information on the talk page.

  • Screen
  • Image Source
  • Switch/Selector
  • Placement Manager

From am design point of view, I would probably introduce a handful of helpers to help with all these tasks:

  • SGSubsystem wrapper for Nasal-based subystems
  • MFDScreen (wrapper for canvases referenced as raster images)
  • MFDImageGenerator (wrapper for a canvas rendering context)
  • CockpitButton (Switch, Button, Selector)
  • MFDSourceSelector (wrapper for assigning different image generators to a single canvas screen)
  • NavDisplay (wip)
  • PrimaryFlightDisplay
# wrapper for a cockpit placement
var Screen = {
};

# wrapper for any Nasal class managing a canvas
var ImageSource = {
};

var SourceSelector = {
};

var MFDisplay = {}; # top-level MFD helper

var MFDMode = {}; # implement different modes
var MFDPage = {}; # implement pages for each mode

var MFDSwitch = {}; # handle switches & buttons