LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Tips and best practices for translating C into LabVIEW? SERIOUS newbie...

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:

  1. Most of the LabVIEW examples that I've seen (at least at the beginner level) seem to heavily rely on using the front panel controls  to provide inputs to functions.  How do I build a VI where the input arguments(input1, input2, etc) come as numbers, and aren't tied to dials or buttons on the front panel?
  2. The structure of the C function seems to rely heavily on the use of stack variables like metric1 and metric2 in order to perform calculations.  It seems like creating temporary "stack" variables in LabVIEW is possible, but frowned upon.  Is it possible to keep this general structure in the LabVIEW VI without making the code a mess?

 

Thanks guys!

 

0 Kudos
Message 1 of 34
(4,355 Views)

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

 

BeginnersQs_BD.png

 

Felix

Message Edited by F. Schubert on 04-20-2010 02:29 PM
0 Kudos
Message 2 of 34
(4,344 Views)

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:

 

Example_VI_BD.png

 

There are functions for If-Else statements.  They are called Case Structures.

 

Message Edited by tbob on 04-20-2010 12:26 PM
- tbob

Inventor of the WORM Global
0 Kudos
Message 3 of 34
(4,338 Views)
Hello.  Your question was very general, and the general answer is the you can convert most things from C++ to LabVIEW although the methods of doing so will be lsightly different.  Since you are coming from a C++ background, I wouild suggest using Object-Oriented programming from LabVIEW.  This will keep things organized in LabVIEW similarly to how you might organize objects in C++.
0 Kudos
Message 4 of 34
(4,337 Views)

And using for-loops:

 

BeginnersQs2_BD.png

 

Felix

0 Kudos
Message 5 of 34
(4,315 Views)

 

There's already a couple of good answers, but to add to #1:


You're clearly looking for a typical C-function. Any VI that doesn't require front panel opening (user interaction) can be such a function.
If the front panel is never opened the controls are merely used to send data to the VI, much like (identical to) the declaration of a C-function. The indicators can/will be return values.

Which controls and indicators are used to sending data in and out of a VI is almost too easy; Click the icon of the front panel (top right) and show connector, click which control/indicator goes where. Done. That's your functions declaration.

Basically one function is one VI, although you might want to split it even further, dont create 3k*3k pixel diagrams.
Depending on the amount of calculations done in your If-Thens they might be sub vi's of their own.

/Y
G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 6 of 34
(4,308 Views)

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

Message 7 of 34
(4,307 Views)

I will definitely explore that further... Might save quite a bit of hassle.

0 Kudos
Message 8 of 34
(4,275 Views)

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!

 

 

 

 

0 Kudos
Message 9 of 34
(4,273 Views)

Labview has references just like C has pointers.  They hold the address of the variable where the data is stored.  See below:

 

Example_VI_BD.png

 

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.

 

- tbob

Inventor of the WORM Global
Message 10 of 34
(4,269 Views)