My primary goal of this example is to demonstrate something cool in LabVIEW. I hope to have accomplished a few things: Demonstrate cool LV capabilities such as scripting and XNodes in new and interesting ways and to share my vision for a new type of math tool in LabVIEW that is based on G, but leverages the power of other tools such as Mathematica, Math Type, LaTeX, and others. In short, I utilize the power of LaTeX and MathML to quickly and easily share equations between all of these programs (and even Wikipedia) and implement them in native G code in LabVIEW.
At the heart of the code is an expression parser which uses the Shunting Yard algorithm to convert math expressions to G code using scripting. On top of this, I have written parsers which convert both MathML and LaTeX to regular expressions which can then be converted to G. With this ability, it is very easy to transfer equations to LabVIEW. In addition, with the use of freely available software (MikTeX and Ghostscript) this tool will automatically place beautifully formatted equations as labels next to the G code. The generated code can be placed on the clipboard for pasting, put into a new VI, or saved as a Math Snippet. The Math Snippets contain the LaTeX or MathML used for the equation and label and they can be loaded and edited by the tool, a great timesaver.
As a bonus, I have taken things to the next level. While G code is beautiful for many things, complex equations can be difficult to follow and debug. Therefore, I have included an XNode which encapsulates the G code and presents only a Typeset equation and a set of terminals. Think formula node with LaTeX typesetting.
Finally, it may not be practical, but one of the flat-out coolest things I have done with LabVIEW is to use the Math Input Control of Windows 7 to write out an equation by hand (scribble really), and see that handwritten code turned automatically into G. Now you can do that too if you have Windows 7.
The code is Windows only since I do not have Mac access. Most of the Windows only functionality is in clipboard functions and system exec calls to execute the external tools, which should be fairly easy to port for those interested.
Three tools are included in this example.
Convert Math Expression to G: Takes code written in MathML, LaTeX, or normal text (y=a+b) and converts it to G with a nicely typeset label (if you install LaTeX and Ghostscript). You can also launch the Math Input Control and the result is automatically placed into the control, or go to a Wikipedia page and copy and equation and you can paste the code into the controls automatically. With Wikipedia, you can either use the web image as the label or tweak the LaTeX as you desire.
Math Node.xnode: When you drop this XNode onto a block diagram, a dialog box appears which is very similar in feel to the Convert Math Expression to G tool. You can load math snippets, or generate code in many of the same ways. When you are finished, you get a beautiful node powered by G under the hood. My knowledge of XNodes is primarily from reverse engineering the ones in vi.lib and this code should be considered a hack. It does however, demonstrate a Math Node I would like to see as a fully integrated tool in LV's arsenal. Plus, with scripting fully supported, maybe we can get some more light shed onto XNode technology, the logical next step. Note: the terminals are fully type adapting, need an array, or cluster, just wire it in. If you can wire it to the math primitives, you should be able to wire it up to the math node. Much of the underlying code is the same as the Convert Math Expression VI to G, I just keep them namespaced since having an XNode in memory reserves the SubVIs and prevents editing.
G shortcut for Quick Drop: Just a bonus tool I use to convert Greek symbols in control labels to Greek, so things like \alpha or \beta are converted to Symbol font. Some LV bugginess with copy and paste of formatting in terminal labels, but the front panel looks good. Shift-G undoes the greek-ification.
A math snippet with embedded information about the equations used for the G and the label. You can open this in either the ConvertMathExpressionToG.vi or the Math Node configuration dialog to edit the code.
Part 1 - Introduction : http://www.screencast.com/t/WgKDsNzI0
Part 2 - Math Input Control and Math Node : http://www.screencast.com/t/jnW69fj1P
Part 3 - Multiple Equations from Wikipedia and Quick Drop Action : http://www.screencast.com/t/YzG9ojDPig3
Removed a few vestigial passwords.
Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.
We're not worthy! We're not worthy!
I got to admit I feel I am worthy. I think you have an interesting idea here but I’m not a fan of the way you executed it. In my opinion I find your BD to be messy and not well documented. I think you over use local variables and do not have proper error handling in place.
>> I got to admit I feel I am worthy. I think you have an interesting idea here but I’m not a fan of the way you executed it. In my opinion I find your BD to be messy and not well documented. I think you over use local variables and do not have proper error handling in place.
Awesome! This would've been so useful in my research days where I was constantly switching between LabVIEW, Mathematica, and LaTeX!
If you can't beat 'em, cojoin 'em. Excellent bridge from formal math to LabVIEW.
Well, I suppose it's not bad... It is somewhat un-worthless and completely non-useless. If I close one eye and hold my hand up to partially cover the screen when viewing the block diagrams I can suppress the gagging reflex All fun aside, fantastic code – not as cool as my guitar tuner, but fantastic nonetheless, thanks!
This is a great concept that will help a lot of engineers make the transition from theory to practice when programming. Likewise, it looks like documentation in Block Diagrams with complex math will be greatly improved. I can't wait until I have a need to use this in an application.
herrlin wrote:
I got to admit I feel I am worthy. I think you have an interesting idea here but I’m not a fan of the way you executed it. In my opinion I find your BD to be messy and not well documented. I think you over use local variables and do not have proper error handling in place.
Negative comments without proof of contribution to the community is harmful, self-centered, egotistical. If you think you could do better, then why aren't your contributions being displayed? Why aren't you the one to submit this contribution? Please, next time just keep your comments to yourself, or be more helpful on how things could be optimized for the next version of it. This is a valid and useful tool no matter how you look at it. Maybe not needed by a great many of people but the people that do need it are grateful they don't have to reinvent the wheel.
TO: THE AUTHOR
Great work, please keep up the contributions. The community has been strengthened by your efforts.
topdevilx86 wrote:
Negative comments without proof of contribution to the community is ...
Everybody is entitled to his opinions, so let's not fall into the same trap and start name calling. Each reader can easily judge the "weight" of a comment based on other contributions of the same author.
I agree that Darin's code is absolutely stellar and very well organized. 99% of the users here in the forum could learn something about code structure! Considering that most of this was written under time pressure and in spare time makes it even more impressive.
Yes, there are locals, but they are confined to the toplevel UI loop where they belong. The locals only contain simple data structures (scalars, booleans). There are other ways to do this, but it is perfectly OK the way it is done here. It is also quite self-documenting.
Yes, there are very few diagram comments and they are really not needed! The program is well structured with simple modular subVIs that have descriptive names and have easy to understand "bite sized" code. The subVIs are even color coded by functional group. (There are none of these overzealous redundant comments we see often elsewhere, e.g. "Here we read the binary file" right above the binary file read icon which is also showing its label :o).)
What is obvious from looking at the code is that it is written by somebody with lots of experience. Miles of wires were strung before this code was written!!
I could not find any flaws in the program structure and fully agree with the assessment of Jeff K over here.
Congratulation..Its very great work.. I would like to know whether it will support the advanced Math functions like Bessel?.. Does it support complex datatypes?
topdevilx86
Please point me to your "contribution to the community."
Chillin
herrlin
Is it possible to create a version of the clipboard dll's that work with the 64bit version of LabVIEW?
Absolutely amazing! Wished I had this while doing my dissertation, no more retyping every single equation twice in LaTex and Matlab/Mathscript Node!
Its just amazing. Great work!!
Gd job bro...U rocks ..I really need this for development of an exoskeleton project..
Cool! But this is supposed to work with tek and ghostscript not installed, right? I tried the first video example (copy from Wikipedia) and can't seem to get anywhere (nothing appears in any of the panels when I try to copy from wikipedia or from any other source).
Then I typed \gamma = \frac{x}{y} as a Latex example (not expecting it to be rendered obviously) and tried to "Preview", which gave me the correct block diagram plus a bonus (see below).
I am using IE 8...
It should work in a limited fashion without TeX or GS, it looks like you are using a patched LV10 with IE8. Not sure I tried it with that combo (I never use IE, and rarely use LV10), I will attempt to this weekend. I should be ready to update one of these days as well.
One question: 32-bit or 64-bit? I don't have LV on any 64-bit OS.
I tried with Firefox, and that seems to solve the Wikipedia issue. It still returns the internal error though (it's actually just a warning, LV does not crash).
Interestingly, I could not find a way to "copy" only in Chrome. My only options are to copy the image or the URL...
I am using LV 2010f2 32 bits.
Copy Image should work in Chrome, it puts the proper HTML fragment onto the clipboard (at least for me).
How do you do that (Chrome)? I tried to select, Copy image then use either <--Clipboard or <--Wikipedia but nothing happens 😞
For me (LV9 + Chrome+XP) I select, right-click 'Copy Image' then use '<-- Wikipedia' . The LaTeX appears in the G Code string and the URL appears in the Label.
Yes, it seems to be a LV10 issue. I just tried all the above in LV9 SP1 and it works as described in IE8 and Chrome (I am using XP as well).
Thanks for the info, I'll try to look into the LV10 issue. I very rarely use it these days, LV9 seems quite rugged by comparison.
A colleague of mine suggested providing the opposite functionality too: starting from a mess of wires supposed to implement an equation, generate the corresponding LaTeX formatted equation (you don't need to simplify it algebraically!). Don't hate me, I am just the messenger! But I admit that it would be cool, just to make sure that a formula is implemented properly...
Kudos on this one!!!!
Absolutely awesome! I am looking forward to try this out tonight.
Very nice, but there is problem with localized decimal point. For example when set 2,34(localized ,), program hung-up. When LV is set to local decimal point and I set in your tools decimal point(.) LV cut decimal point and number.
This is pretty cool, Darin. I am thinking about how to use this in an application where the user would be entering a custom equation for data processing and the code to handle the equation would be created on the fly. Is this even possible? (I know my co-worker lives by the phrase "Anything is possible with LabVIEW", but even LV has it's limitations) 😉
I know you just thought, "duh, that's what I just did." I'm talking about integrating this into an app where the LaTex translated G would be created and called from within the app at run-time, not as part of the development of the app.
Also, where can I get that "Edit Connector Pane..." add-on? That's pretty sweet as well.
I made the same experience as X-). I'm also using Labview 10.0f2 and it doesn't work here either. Though I'm getting no error message, but Labview crashes completely.
Great tool! There does seem to be an issue in order of operations in the "IsOperator.vi" The default value of the array on the front panel has the subtraction as higher precedence than multiply and divide.
My version has +,-,/,*,^ in that order, precedence is given by the index so the last element has higher precedence.
In case you need to change it, you found the spot.
Pure wizardry. I am blown away. -drew
Superb addition to LabVIEW!! Awesome!
One question: Is it possible for us to add functionalities? For example I use the "Add array elements" very often, and I would like to incorporate a function named "sum()", which would be replaced by the aforementioned VI.
Thanks.
I can pretty reliably chrash Labview, by entering this formula in the 'Converter', and try to convert it.
\frac{(R * K) + (R * K)}{K - R}
(I know this also could be shortened, but this was not what I needed, its just what I could boil the example down to, and still get LV to chrash.)
From the chrash report:
DAbort 0x1A7102DF in fpsane.cpp
Version: 14.0.1f3 (32-bit)
Any plans to release a 64 bit version of the tool?
There is a couple of DLLs which break the whole thing in LV 64 bit:
ClipboardDataDLL.dll
GetClipboardUnicode.dll
I installed LV 2015 32 bit to have a go at a text to G conversion I was not looking forward to implement manually and I managed to crash the VI (and LabVIEW: DAbort 0x1A7102DF in fpsane.cpp error, line 547) with the Preview button.
Here is the plain text formula I pasted in the String for G code control:
(-r + (-1 + y^(-1))^(1/6))/(6*exp((-r + (-1 + y^(-1))^(1/6))^2/(2*s^2))*sqrt(2*Pi)*s^3*(-1 + y^(-1))^(5/6)*y^2)
[It is a Wolfram Alpha plaintext output - from Mathematica, with a few edits of mine, such as replacing e^ by exp].
The diagram creation seems to work fine until the final steps of cleanup when the crash occurs.
These subparts of the expression work fine:
(-r + (-1 + y^(-1))^(1/6))/(6*exp((-r + (-1 + y^(-1))^(1/6))^2/(2*s^2)))
Sqrt(2*Pi)*s^3*(-1 + y^(-1))^(5/6)*y^2
Trying to multiply or divide them and the crash occurs.
I know this is not how you demo your code, but I thought you might be interested in this failure of expression handling.
BTW, I added \sqrt in the array of LaTeX Commands (in ToLaTeXFunctions.vi). Not sure whether this is necessary, but since the expressions above go through that step I had to take a look.
EDIT: I verified that miktex and ghostscript where properly installed by checking that the LaTeX output of Mathematica was interpreted correctly in the "label" control:
\frac{\left(\sqrt[6]{\frac{1}{y}-1}-r\right)
e^{-\frac{\left(\sqrt[6]{\frac{1}{y}-1}-r\right)^2}{2 s^2}}}{6
\sqrt{2 \pi } s^3 \left(\frac{1}{y}-1\right)^{5/6} y^2}
gives:
Using this LaTeX version (after cleaning) in the "string for G code" control results in the same crash...
Just for info, the toolset crashes in CleanBlockDiagram.vi.
I modified the above expression as z = ... to check whether this was due to a lack of output terminal, but that appears not to be the case.
Right before the call to that VI, the VI being built looks like this:
Clearly, it has a lot of things wrong at this stage...
@Darin: Very cool stuff! Thanks for sharing!
However, I have encountered some problems with the operator precedence. When I enter this example:
F = 1 - A*x + B*x^2
I get this result:
which is obviously wrong.
I traced the origin of this behavior to the ShuntingYard.vi, where I figured I had to remove the wire from "FindFunction.vi" to "IsOperator.vi" in order to let the latter use its default operator precedence. This was just some guess, which yielded the correct result on the first example, but broke this example:
F = A/B*x
which now yields:
which is also not correct. However, if I leave the wire intact, the second example comes out right. So I think there is *something* wrong in the ShuntingYard.vi, but I cannot figure out the exact problem. Any idea? Any help would be greatly appreciated, because I have some monster of a formula to translate 😉
btw, I use LV2017 64bit
Edit: I could finally include some images...
I think I found the problem. Actually there were 2:
1) There is a second instance of "IsOperator.vi" in "ShuntingYard.vi", where the wire for the "Operators" input from the "FindFunctions.vi" is simply missing. -> attached that wire
2) The default value of the function list in "FindFunction.vi" offers the basic operators in a different order. -> made that consistent with the list in "IsOperator.vi".
These changes fixed all problems with operator precedence for me. Unfortunately I don't know how to attach the corrected VIs here, sorry...
This looks really cool, thanks for the post and update.
Reading jqu's recent posts, I revisited the problems I mentioned a couple of years ago. Implementing jqu's fixes does solve the layout issue reported in this post.
However, in this particular example, LabVIEW (2017 SP1 64 bits) still crashes.
I confirmed that the crash originates from the call to BD.CleanUp in MathToG.lvlib:CleanBlockDiagram.vi.
A simple disable structure fixes the problem:
Oh yeah and there still are a few hardcoded paths that don't work in 64 bits and will need to be corrected for LaTeX and GhostScript to be called properly (some of the VIs are duplicated for the XNode interface, and therefore two corrections are needed each time).
Look for latex.exe, gswin32c.exe and dvips (that last one is trickier as it requires building a path with dvips.exe).
As far as the DLLs involved in the capture of the Math Input Control data, I haven't bothered to look for a replacement as I am not interested.