Board index FlightGear Development Canvas

kuifje09's FGPlot Development

Canvas is FlightGear's new fully scriptable 2D drawing system that will allow you to easily create new instruments, HUDs and even GUI dialogs and custom GUI widgets, without having to write C++ code and without having to rebuild FlightGear.

kuifje09's FGPlot Development

Postby Hooray » Thu May 23, 2013 7:23 pm

Continuing the original FGPlot thread: Subject: Flightgear plotter updated.
kuifje09 wrote:Thanks Hooray, I will have a look at howto plot in nasal. I already did some calculations, which where not difficult.
(Just had q quick look at the canvas-module. Its fairly new but I see a possibility for the fgplot ...)


To get you started, I have added a little tutorial and a screen shot, demonstrating that it's definitely possible to add a Nasal/Canvas-driven "fgplot" application to FlightGear, using just a simple XML dialog (because you're using FG 2.10, that doesn't yet have the more recent features):

http://wiki.flightgear.org/Howto:Adding ... log#FGPlot
Image

It's just proof of concept, and pretty simple for now - but should get you going pretty quickly.
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 11965
Joined: Tue Mar 25, 2008 8:40 am

Re: kuifje09's FGPlot Development

Postby kuifje09 » Mon May 27, 2013 3:15 pm

Hello to all developers in the canvas scene...
I am rather new to the canvas tool, but don't want to give up too soon. ( I have done some programming, build even a whole plane with all meters and screens. Even a moving map on a panel, but that was obviously in a wrong way and caused fg to slow down too much to be usable. was a nice experience )
Already have some example running, but its running too slow after a few seconds. I stripped it down and placed it here, please have a look at this : http://www.flightgear.org/forums/viewtopic.php?f=18&t=19976&p=184197&hilit=fgplot#p184197

Any advice is welcome. I am a little stuck now, I realy don't see what I do wrong. but is seems to be the problem in the method I use to draw some lines in a live-way. the more line-parts, the slower it gets. And it should possibly run for longer periods. Not just seconds. But after drawing about one cycle of o sine and a cosine, the framerate of flightgear went down to 1 or 2 frames a second. which means the end of the project.

The plot routine will be called later via a property reading coding, to read the values to be plot. the sine/cosine is just to get a working example.
kuifje09
 
Posts: 597
Joined: Tue May 17, 2011 8:51 pm

Re: kuifje09's FGPlot Development

Postby Hooray » Mon May 27, 2013 3:38 pm

Image
Like I said earlier, your algorithm itself is the problem here - you need to understand how the Canvas and OpenVG paths work to reduce the amount of work that the canvas is doing.

For example, you could start by looking at a conventional HTML5/Canvas tutorial doing the same thing (plotting a sine wave), and then just adapt the algorithmic side of the code accordingly: http://jsfiddle.net/veEyM/5/
http://stackoverflow.com/questions/6032 ... ith-canvas

Bottom line being: If your browser can handle it, FlightGear + Canvas can definitely handle it, too :lol:
Last edited by Hooray on Mon May 27, 2013 4:20 pm, edited 1 time in total.
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 11965
Joined: Tue Mar 25, 2008 8:40 am

Re: kuifje09's FGPlot Development

Postby kuifje09 » Mon May 27, 2013 4:10 pm

Allright, thats what I had on my mind. The graph should be written as a whole, time over time. I was trying to plot only a changed part of the graph.
But, if that javascript is as fast as the canvas in fg. then it is too slow. In firefox it runs till 80% of the 1 cpu.
I will rewrite the code and what is does.
kuifje09
 
Posts: 597
Joined: Tue May 17, 2011 8:51 pm

Re: kuifje09's FGPlot Development

Postby Hooray » Mon May 27, 2013 4:16 pm

I would only optimize the algo in general terms, not for your particular use-case - because here, you can easily precompute/cache all coordinates and reuse them, but for your intended purpose, you don't know all coordinates in advance - so there's no point in caching.

And actually, Tom's canvas system can reduce the load a little, because it can handle transformation and even shift the image. I don't know if the C++ code is currently smart enough to update only bounding boxes that have changed, but HTML5/Canvas has some pretty serious restrictions here - which FG's canvas doesn't need to have (no W3C committee behind it to see changes) - so we can get lots of changes because FG/Canvas is more flexible, especially with some added sweet-talking to Tom :lol:

So depending on the style of graph, it would be more efficient not to clear/shift/redraw the graph, but to use a nested group or canvas, shift the previous graph there, and use the other "layer" for drawing the new stuff, that would reduce your load significantly, even if you didn't change anything else:

    loop:
  • get previously drawn coordinates
  • update the group, shift the group as required
  • then draw the new coordinates
  • loop()
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 11965
Joined: Tue Mar 25, 2008 8:40 am

Re: kuifje09's FGPlot Development

Postby TheTom » Mon May 27, 2013 4:37 pm

Try the following code inside your dialog.xml:
Code: Select all
<load> <!-- when the fgplot is loaded == pressed equipment->fgplot -->
<![CDATA[

# you can add your canvas-specific code here
var my_canvas = canvas.get( cmdarg() ); # this will get a handle to the parent canvas:

var root = my_canvas.createGroup();

var graph = root.createChild("group");

var x_axis = graph.createChild("path", "x-axis")
.moveTo(0, 151)
.lineTo(850, 151)
.setColor(0.8,0.8,0.8)
.setStrokeLineWidth(1);

var y_axis = graph.createChild("path", "y-axis")
.moveTo(1, 0)
.lineTo(1, 301)
.setColor(0.8,0.8,0.8)
.setStrokeLineWidth(1);

var startover = func (s) {
   settimer(dotPlot(s),1);
};

var p = (2*math.pi)/360;
var y0 = 151;
var y1 = 151;
var x0 = 0;
var i = 0;
var plot1 =
  graph.createChild("path", "data")
       .setColor(1,0.5,0.8);
var plot2 =
  graph.createChild("path", "data")
       .setColor(0,0.5,1.0);

var dotPlot = func () {
# For debugging
#   print("i=",i," y0=",y0," y1=",y1);
   if( i == 0 )
   {
     plot1.reset().moveTo(i,y0);
     plot2.reset().moveTo(i,y1);
   }
   # this is|should be an external var
   # but its value is last after stop/start
   y0 = math.sin(i*p)*100+151;
   y1 = math.cos(i*p)*100+151;

   plot1.lineTo(i,y0);
   plot2.lineTo(i,y1);

   i += 6;
   if( i >= 850 )
     i=0;

   # This routine should be looping while running is active.
   # But it may not becoming active in itself.
   # It must be restarted from "outside"
   if(getprop("/gui/fgplot/running") == 1)settimer(func{dotPlot()},0.05);
};

var setProps = func () {
   setprop("/gui/fgplot/transparent",0);
   setprop("/gui/fgplot/P-value",1);
   setprop("/gui/fgplot/I-value",1);
   setprop("/gui/fgplot/D-value",1);
   setprop("/gui/fgplot/In-value",1);
   setprop("/gui/fgplot/Ref-value",1);
   setprop("/gui/fgplot/Out-value",1);
   setprop("/gui/fgplot/running",0);
   setprop("/gui/fgplot/plot",0);
   print("Properties set");
   gui.popupTip("Properties set",3);
};


]]>
</load>


Basically you should:
  • not create a separate path for each line segment, but instead reuse and extend the previous path.
  • chose arbitrary sizes for your path segments. There isn't any obvious visual difference in using 1px vs 6px segments but a huge differences in the workload for the canvas.
  • not call any unneeded commands - for example if drawing a line, you do not need to draw a line back to the beginning of the path (with close())
  • keep the update frequency as low as possible. 10 or 20 updates per second will probably enough (It is though always limited by the actual framerate).

Btw. FlightGear Canvas and HTML5 Canvas are quite different things. In contrary to HTML5 the FlightGear Canvas has a scenegraph which allows parts of the canvas to be replaced/changed without manually redrawing everything else (which is the case with HTML5).
TheTom
 
Posts: 322
Joined: Sun Oct 09, 2011 10:20 am

Re: kuifje09's FGPlot Development

Postby kuifje09 » Wed May 29, 2013 2:55 pm

Ah, thanks Tom. It can be indeed a bit faster as I did. Unfortunaltly I already did some work and readings.
Got almost a same solution as yours, but I would have a continuous curve. ( flowing backwards ) So a rewrite of the whole line is needed after a reset of the vector.
That slows down a lot. The close was indeed a mistake and a misunderstanding.
I am still busy to get a nice solution. I think indeed for best performance it will be a roundrobbin graph.
I just, before reading this, I had a flowing graph, but it slowed down the framerate to 7 FPS.
But if you want to see all the spikes in the PID calculations you can have 300 values a second on a fast system.
My system wil get only 17 frames a second I have seen. wich will make 102 values a second.
Then you will see about 5 seconds in a graph to get a good feeling of the behaviour of the AP-PID, so you will have ~500 values per line.
On a fast system you would see less "seconds" because of the faster points per second.

Can you agree of have a better idea?

I mean this, when you do not see the spikes and or noise in the calculation you have in fact a bad performing PID-controler.
Or would it be of no importance?

( b.t.w. I have set the multythreading to CullDrawThreadPerContext, which set both cores to full ~ 100% )

Sorry if my english is sometimes somewhat weird.
kuifje09
 
Posts: 597
Joined: Tue May 17, 2011 8:51 pm

Re: kuifje09's FGPlot Development

Postby Hooray » Wed May 29, 2013 3:43 pm

tell us exactly what kind of graph you'd like to have, so that we can check how to come up with an optimized implementation that reduces the workload for the canvas. As was mentioned previously, the canvas system can do quite a bit more than HTML5 Canvas, it uses OpenGL and a scene graph, so you should be able to even have a moving graph,just by adusting the position of the previously drawn graph and then using an offset to continue drawing. Basically, you can transform the whole texture without having to redraw everything, which should save quite a bit of work.

Before we look into profiling the canvas system, you should really be using an optimized algorithm for drawing - the taxiway experience at KNUQ has shown that Tom comes usually up with various optimization ideas before he has to look into the code.

Also, keep in mind that the canvas is updated at frame rate, while the FDM/AP are updated at FDM rate
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 11965
Joined: Tue Mar 25, 2008 8:40 am

Re: kuifje09's FGPlot Development

Postby kuifje09 » Thu May 30, 2013 1:02 pm

Hi Hooray and TheTom and other fellows.

I made a newer version, get more familiar to nasal and this a basic version with low workload.
I added a test feature in it so you can see the difference in performance. depending the way you program the "loops" you get good or worse results.

Have a look and shoot again... Plaese some advise to toggle transparency, I don't get that quite right.

I will try to fix it up now to make a real PID-grapher. ( reading from the props-table. )

SEE update below
Last edited by kuifje09 on Sat Jun 15, 2013 2:19 pm, edited 1 time in total.
kuifje09
 
Posts: 597
Joined: Tue May 17, 2011 8:51 pm

Re: kuifje09's FGPlot Development

Postby Hooray » Thu May 30, 2013 5:28 pm

it's working - but when I open the debug/property browser and go to /canvas/texture[2], I am seeing ~50 groups with each having more than 500 paths - for 5 different graphs after just a couple of seconds, why all these groups and paths ? what exactly are you trying to do there ?

PS: If loop-unrolling had any significant effect, that would suggest that there's some Nasal-space overhead, too.
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 11965
Joined: Tue Mar 25, 2008 8:40 am

Re: kuifje09's FGPlot Development

Postby kuifje09 » Thu May 30, 2013 8:17 pm

Strange. My goal was to have 6 lines over 8 path, so each part of a line can be reset and plot again. Else you would have graphs over graphs and that does not look nice. This would make 48 vectors.
So I adopted a piece of code TheTom showed " plot1.reset().moveTo(i,y0); " so a line is 8 vectors being reused over and over.
About the groups ??? I only see one <group> </group> in de code .

" some Nasal-space overhead, too" obvious every way of programming has its pros and cons. Smaller code does not have to be faster.
Calling a sub is slower then inline functions... I just added it to show. The number 1 or the full-out written is the fastest on my system.
Can fly as if no fgplot is activ.
Last edited by kuifje09 on Thu May 30, 2013 8:22 pm, edited 1 time in total.
kuifje09
 
Posts: 597
Joined: Tue May 17, 2011 8:51 pm

Re: kuifje09's FGPlot Development

Postby Hooray » Thu May 30, 2013 8:20 pm

will check again, maybe I was wrong.
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 11965
Joined: Tue Mar 25, 2008 8:40 am

Re: kuifje09's FGPlot Development

Postby kuifje09 » Thu May 30, 2013 8:33 pm

I have : /canvas/by-index/texture then group/group then 48 path for the vectors.
Nothing strange in the other sub-tree of canvas.
kuifje09
 
Posts: 597
Joined: Tue May 17, 2011 8:51 pm

Re: kuifje09's FGPlot Development

Postby Hooray » Thu May 30, 2013 8:49 pm

Okay, I still think we can do some more algorithmic optimizations of your code before we really look at Nasal and the Canvas system - and your loop-unrolling suggest that you are familiar with compiler optimizations, Nasal doesn't do _ANY_ optimizations at all (except for tail calls) - so you will want to move unnecessary stuff out of loops and avoid computations that aren't necessary - I added a new article on memoization to the wiki today. You will probably want to use systime() or debug.benchmark() to check the overhead of certain constructs.

Nasal not doing any optimizations, also means that you can easily optimize things by avoiding repeated nested hash/vector lookups, and using a local reference instead:

Code: Select all
var local = deeply.nested.object[4].foo[4].bar[0];

That way, the look-up overhead is reduced because everything is cached in the local symbol, that you could then directly use.


Regarding space-time tradeoffs, you could also use a func generator to pre-cache the loops, without having to manually unroll things - Nasal metaprogramming is definitely possible.

Finally, keep in mind that some of your optimizations are pretty much pointless once you stop plotting a well-defined function and use "live" properties from the tree instead. Optimizing the plotting routine for a sin/cos graph isn't overly difficult - the challenge is coming up with something generic that will also work for "unknown" data. Which is exactly where using the right OpenVG primitives pays off, as we could see with the KNUQ taxiways.

Once the algorithmic optimization potential in your Nasal code and drawing instructions is exhaused, I'd suggest to use the [url=wiki.flightgear.org/Built-in_Profiler]built-in profiler[/B] to benchmark the whole thing with real FDM/AP properties, so that we can check if there are any low-hanging fruits - which would be right in time for optimizations - i.e. prior to the "frozen" period of the upcoming release.
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 11965
Joined: Tue Mar 25, 2008 8:40 am

Re: kuifje09's FGPlot Development

Postby kuifje09 » Thu May 30, 2013 9:51 pm

Hello Hooray, don't forget Iam just building from no knowlege about the canvas and plotting on it.
So I try to figure out step by step what it does and so on.
And then, after my X11/motif program you siad it might be nice to have such thing in flightgear.
I Realy like It if I can fix that for the flightgear scene.
The toughest part is getting values from the prop-tree and then get them in a reasonable value for the graph.
In fact only possible if flightgear has a simple way to get them into.
I don't want to do much in that. I don't think it has much weight what I suggest.

I would love to know how to make the canvas transparent. I see other use it, but I realy don't get it how to do.
The surrounding of the canvas was not difficult.

This moment I try to make a little program to get the debug output back in the proptree via telnet. Thats also a chalenge .
Never done network related programming. ( but its only for my test !!! , Just someting to fiddel with. Talking "telnet" already working at a low level. Its dedicated so not much testing on input is needed. No idea if it is fast enough in the end. )
kuifje09
 
Posts: 597
Joined: Tue May 17, 2011 8:51 pm

Next

Return to Canvas

Who is online

Users browsing this forum: No registered users and 1 guest