I want to use several formulas from which I have the C-code already, in a Flowcode project.
How do I do the following...
- Add variables in Flowcode (floating)
- Add a C-component.
- In this C-component put formulas that use the variables that were declared in Flowcode
- Use the result of the formula in the C-block in the rest of the Flowcode chart.
Example:
In FC I add 3 variables: X an Y and "result" (float)
In the C-block I add this formula: result = 0.39782 * sin(X) cosDec = cos(asin(Y))
The I need to print "result" to my LCD or use it later in the rest of the chart.
I've tried it, but my first test didn't work (I have the C simulation licence)...
Using C code in FC8: several questions
Moderator: Benj
-
- Valued Contributor
- Posts: 1189
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 439 times
- Contact:
Re: Using C code in FC8: several questions
Hi MJU,
Flowcode renames variables to either FCV_NAME or for locals FCL_NAME (where NAME is the name you gave the variable converted to upper case)
To define a float - declare the variable as type float
(An aside - note that due to a bug in FC - you must do this when initially declaring the variable - although a variable type can be changed - float becomes grayed out and can't be selected (might be possible to change the XML def though - which could save some heartache if you've used the variable in a lot of places ))
You can access these in a C block (you must use the FC modified names in C code) A useful tip is to view the code as 'C' - to see the names FC has used.
Here I created a new macro with x and y as (float) arguments and returning a float (return value is referred to as FCR_RETVAL)
If using C 'inline' you can also assign to FC variables (FCV_X = 1.234; for example)
Note that I've modified the formula you pasted - which is not correct C (or indeed math) - suspect just a typo.
Note also that C statements need a terminating ';' (semi colon)
Unfortunately sin cos etc don't seem to simulate but should compile and run (?)
It might be easier to just use a FC calculation block?
Martin
Flowcode renames variables to either FCV_NAME or for locals FCL_NAME (where NAME is the name you gave the variable converted to upper case)
To define a float - declare the variable as type float
(An aside - note that due to a bug in FC - you must do this when initially declaring the variable - although a variable type can be changed - float becomes grayed out and can't be selected (might be possible to change the XML def though - which could save some heartache if you've used the variable in a lot of places ))
You can access these in a C block (you must use the FC modified names in C code) A useful tip is to view the code as 'C' - to see the names FC has used.
Here I created a new macro with x and y as (float) arguments and returning a float (return value is referred to as FCR_RETVAL)
If using C 'inline' you can also assign to FC variables (FCV_X = 1.234; for example)
Note that I've modified the formula you pasted - which is not correct C (or indeed math) - suspect just a typo.
Note also that C statements need a terminating ';' (semi colon)
Unfortunately sin cos etc don't seem to simulate but should compile and run (?)
It might be easier to just use a FC calculation block?
Martin
-
- Posts: 502
- Joined: Wed Nov 07, 2007 6:51 pm
- Location: Antwerp Belgium
- Has thanked: 121 times
- Been thanked: 108 times
- Contact:
Re: Using C code in FC8: several questions
Wow, what a great reply..mnf wrote:Hi MJU,
Flowcode renames variables to either FCV_NAME or for locals FCL_NAME (where NAME is the name you gave the variable converted to upper case)
To define a float - declare the variable as type float
(An aside - note that due to a bug in FC - you must do this when initially declaring the variable - although a variable type can be changed - float becomes grayed out and can't be selected (might be possible to change the XML def though - which could save some heartache if you've used the variable in a lot of places ))
You can access these in a C block (you must use the FC modified names in C code) A useful tip is to view the code as 'C' - to see the names FC has used.
test.JPG
Formula.fcm
Here I created a new macro with x and y as (float) arguments and returning a float (return value is referred to as FCR_RETVAL)
If using C 'inline' you can also assign to FC variables (FCV_X = 1.234; for example)
Note that I've modified the formula you pasted - which is not correct C (or indeed math) - suspect just a typo.
Note also that C statements need a terminating ';' (semi colon)
Unfortunately sin cos etc don't seem to simulate but should compile and run (?)
It might be easier to just use a FC calculation block?
Martin
I've been trying to get this working, but the use of C is still a mystery to me.
When to use FCV_NAME or FCL_NAME (in this thing I want to get working) is still a problem for me.
I got so far (please check the attached file), all I want is to get the result from the calculation on the display..
Maybe I could use that little bit of help to get me going
-
- Valued Contributor
- Posts: 1189
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 439 times
- Contact:
Re: Using C code in FC8: several questions
Your code seems to simulate OK - remember to turn on C Sim - it sets return to 1060.1544189453125 (which is correct given the accuracy of floats -
see https://www.learncpp.com/cpp-tutorial/f ... t-numbers/ for some detail on this)
However - the result you should see on the LCD will be 1060 - because PrintNumber takes an integer argument - so the float result is converted to an integer before being printed. You could convert the value to a sting using FloatToString$ before using PrintString.
Note that you don't need to give the variables uppercase names - flowcode capitalizes them for it's 'internal' use - so if you want to access them in a C block you need to be aware of this.
The FCV_ prefix is used for global variables FCL_ for local variables.
(Modified to show these points)
Martin
see https://www.learncpp.com/cpp-tutorial/f ... t-numbers/ for some detail on this)
However - the result you should see on the LCD will be 1060 - because PrintNumber takes an integer argument - so the float result is converted to an integer before being printed. You could convert the value to a sting using FloatToString$ before using PrintString.
Note that you don't need to give the variables uppercase names - flowcode capitalizes them for it's 'internal' use - so if you want to access them in a C block you need to be aware of this.
The FCV_ prefix is used for global variables FCL_ for local variables.
(Modified to show these points)
Martin
-
- Valued Contributor
- Posts: 2045
- Joined: Wed Aug 27, 2008 10:31 pm
- Location: Netherlands
- Has thanked: 553 times
- Been thanked: 1081 times
- Contact:
Re: Using C code in FC8: several questions
How about using the C code to flowcode conversion option available in FC8?
“Integrity is doing the right thing, even when no one is watching.”
― C.S. Lewis
― C.S. Lewis
-
- Valued Contributor
- Posts: 1189
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 439 times
- Contact:
Re: Using C code in FC8: several questions
The majority of formula should convert to a FC calculation box easily - but the auto C->Flowcode converter doesn't seem to cope with sin, cos & tan etc...
Martin
Martin
-
- Posts: 502
- Joined: Wed Nov 07, 2007 6:51 pm
- Location: Antwerp Belgium
- Has thanked: 121 times
- Been thanked: 108 times
- Contact:
Re: Using C code in FC8: several questions
I've never heard about the C sim button, nor the C to flowchart option.kersing wrote:How about using the C code to flowcode conversion option available in FC8?
Maybe the Wiki should be updated or better be organised. Everytime I look for something I get 40 results that doesn't help me.
Thanks again for all the help, now the C code calculates well.
I will post several bugs from all I've read in your posts.
Thank you and I will be back later with more questions (if I can't find the answers myself).
-
- Posts: 502
- Joined: Wed Nov 07, 2007 6:51 pm
- Location: Antwerp Belgium
- Has thanked: 121 times
- Been thanked: 108 times
- Contact:
Re: Using C code in FC8: several questions
Thank you again, now it works (think I've replied this to Kersing accidental )mnf wrote:The majority of formula should convert to a FC calculation box easily - but the auto C->Flowcode converter doesn't seem to cope with sin, cos & tan etc...
Martin