Page 1 of 1

Using C code in FC8: several questions

Posted: Sat Apr 27, 2019 5:53 pm
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)...

Re: Using C code in FC8: several questions

Posted: Sat Apr 27, 2019 6:43 pm
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 4212 times
Formula.fcm
(633 Bytes) Downloaded 210 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

Re: Using C code in FC8: several questions

Posted: Sat Apr 27, 2019 11:04 pm
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 195 times

Re: Using C code in FC8: several questions

Posted: Sun Apr 28, 2019 9:16 am
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 187 times
(Modified to show these points)

Martin

Re: Using C code in FC8: several questions

Posted: Sun Apr 28, 2019 10:35 am
by kersing
How about using the C code to flowcode conversion option available in FC8?

Re: Using C code in FC8: several questions

Posted: Sun Apr 28, 2019 2:07 pm
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

Re: Using C code in FC8: several questions

Posted: Mon Apr 29, 2019 5:39 pm
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).

Re: Using C code in FC8: several questions

Posted: Mon Apr 29, 2019 5:40 pm
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 )