Using C code in FC8: several questions

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 8.

Moderator: Benj

Post Reply
MJU
Posts: 502
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 121 times
Been thanked: 108 times
Contact:

Using C code in FC8: several questions

Post by MJU »

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)...

mnf
Valued Contributor
Valued Contributor
Posts: 1188
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

Post by mnf »

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
test.JPG (9.98 KiB) Viewed 4133 times
Formula.fcm
(633 Bytes) Downloaded 206 times
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

MJU
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

Post by MJU »

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
Wow, what a great reply..

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 :-)
CTEST1.fcfx
(8.67 KiB) Downloaded 194 times

mnf
Valued Contributor
Valued Contributor
Posts: 1188
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

Post by mnf »

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.
CTEST1.fcfx
(9.11 KiB) Downloaded 184 times
(Modified to show these points)

Martin

kersing
Valued Contributor
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

Post by kersing »

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

mnf
Valued Contributor
Valued Contributor
Posts: 1188
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

Post by mnf »

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

MJU
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

Post by MJU »

kersing wrote:How about using the C code to flowcode conversion option available in FC8?
I've never heard about the C sim button, nor the C to flowchart option.
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).

MJU
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

Post by MJU »

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
Thank you again, now it works (think I've replied this to Kersing accidental )

Post Reply