Using C code in FC8: several questions

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

Moderator: Benj

MJU
Posts: 429
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 105 times
Been thanked: 94 times
Contact:

Using C code in FC8: several questions

Postby MJU » Sat Apr 27, 2019 5:53 pm

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: 562
Joined: Wed May 31, 2017 11:57 am
Has thanked: 54 times
Been thanked: 316 times
Contact:

Re: Using C code in FC8: several questions

Postby mnf » Sat Apr 27, 2019 6:43 pm

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 871 times

Formula.fcm
(633 Bytes) Downloaded 26 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: 429
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 105 times
Been thanked: 94 times
Contact:

Re: Using C code in FC8: several questions

Postby MJU » Sat Apr 27, 2019 11:04 pm

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 26 times

mnf
Valued Contributor
Valued Contributor
Posts: 562
Joined: Wed May 31, 2017 11:57 am
Has thanked: 54 times
Been thanked: 316 times
Contact:

Re: Using C code in FC8: several questions

Postby mnf » Sun Apr 28, 2019 9:16 am

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 21 times

(Modified to show these points)

Martin

kersing
Valued Contributor
Valued Contributor
Posts: 1873
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 530 times
Been thanked: 1040 times
Contact:

Re: Using C code in FC8: several questions

Postby kersing » Sun Apr 28, 2019 10:35 am

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: 562
Joined: Wed May 31, 2017 11:57 am
Has thanked: 54 times
Been thanked: 316 times
Contact:

Re: Using C code in FC8: several questions

Postby mnf » Sun Apr 28, 2019 2:07 pm

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
These users thanked the author mnf for the post:
MJU (Mon Apr 29, 2019 5:40 pm)
Rating: 5.26%
 

MJU
Posts: 429
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 105 times
Been thanked: 94 times
Contact:

Re: Using C code in FC8: several questions

Postby MJU » Mon Apr 29, 2019 5:39 pm

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: 429
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 105 times
Been thanked: 94 times
Contact:

Re: Using C code in FC8: several questions

Postby MJU » Mon Apr 29, 2019 5:40 pm

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 )