04-20-2010 02:11 PM
I need to translate a C function into LabVIEW. This will be my *first* LabVIEW project. I've been reading some tutorials, and I'm still struggling to get my brain out of "C/C++ mode" and learn the LabVIEW paradigms.
Structurally, the function that I need to translate gets called from a while-loop and performs a bunch of mathematical calculations.
The basic layout is something like this (this obviously isn't the actual code, it just illustrates the general flow control and techniques that it uses).
struct Params { // About 20 int and float parameters } int CalculateMetrics(Params *pParams, float input1, float input2 [etc]) { int errorCode = 0; float metric1; float metric2; float metric3; // Do some math like: metric1 = input1 * (pParams->someParam - 5); metric2 = metric1 + (input2 / pParams->someOtherParam); // Tons more simple math // A couple for-loops if (metric1 < metric2) { // manipulate metric1 somehow } else { // set some kind of error code errorCode = ...; } if (!errorCode) { metric3 = metric1 + pow(metric2, 3); } // More math... // etc...
// update some external global metrics variables
return errorCode; }
I'm still too green to understand whether or not a function like this can translate cleanly from C to LabVIEW, or whether the LabVIEW version will have significant structural differences.
Are there any general tips or "best practices" for this kind of task?
Here are some more specific questions:
Thanks guys!
04-20-2010 02:21 PM - edited 04-20-2010 02:29 PM
Short hints, I'll code up something...
1. The inputs will be always front panel controls. They don't cost you anything, but help you debugging.
2. The wire is the variable.
EDIT: Here is some of your code translated to LV. No you can try around and ask more specific Q's
Felix
04-20-2010 02:25 PM - edited 04-20-2010 02:26 PM
In Labview, the wires are the variables. You don't need a specific variable for each intermediate step. For example:
A = 1 + 2
B = A - 3
C = A + 1
You don't need to declare a variable named A. Wire in controls or constants to the Add function. The output of the Add can be wired directly to the subtraction function.
Like this:
There are functions for If-Else statements. They are called Case Structures.
04-20-2010 02:25 PM
04-20-2010 02:36 PM
And using for-loops:
Felix
04-20-2010 02:44 PM
There's already a couple of good answers, but to add to #1:
04-20-2010 02:45 PM
Promba,
If you have some c-functions that you would like to use in LabVIEW, you could also paste the code (some modification may be required) into a Formula Node. That way, you may be able to convert tested c-code over without a lot of re-coding in LV.
-cb
04-20-2010 03:11 PM
I will definitely explore that further... Might save quite a bit of hassle.
04-20-2010 03:13 PM
OK, here's another example:
Input1 = (Input2 * (Input1 + Input3));
Is there a way to assign input1 and reference input1 in LabVIEW? Or do I need some other temporary variable? When I try to wire the output of the "*" operator into Input1, I get an error.
Thanks!
04-20-2010 03:21 PM
Labview has references just like C has pointers. They hold the address of the variable where the data is stored. See below:
In the top row, the wire color is orange to indicate a floating point type.
In the next row, the first object is a reference (pointer) to Input1. Notice that the wire color is blue-green. It contains an address, not a value. So it cannot be wired directly into a numerical function. Use the Value property to obtain the actual data at that address.