Macro Library - 8-bit Float Maths

A forum where Flowcode v6 user created components can be shared, discussed and reviewed.

Moderator: Benj

Post Reply
User avatar
SteveM
Posts: 55
Joined: Tue Mar 25, 2014 2:09 pm
Has thanked: 28 times
Been thanked: 65 times
Contact:

Macro Library - 8-bit Float Maths

Post by SteveM »

Hi All,
The new component creator in v6 enables us to make all sorts of fantastic new 'add-ons' for Flowcode in a way that was much easier than before - they're just a bundle of flowcharts; no more messing with C-code and VB required!
However, when we think of "components", it's very easy to fall into the assumption that we only mean things like LEDs, switches, motors etc. - items which represent "real hardware" that we want to interface with. However, the flowcharts that get bundled into an exported component can do absolutely anything we care to program.

Sharing useful code between Flowcharts is a really cool thing to do - why write the same code twice! We've been able to do this for a long time using the Import/Export macro functions from the main menu. But there are a few things you have to be really careful of when doing this...
- You have to be very careful that any global variables and constants referenced in the macros get duplicated in your 'target' flowchart, otherwise the imported macros might not work - a real pain if you happen to have already used the same variable names!
- You might even have already used the same macro names in your flowchart - which will get Flowcode very confused!
- It gets very laborious if there are many macros that you want to re-use. They have to be exported/imported one at a time, and they clutter up the windows in Flowcode with stuff that you want to use but don't necessarily ever want to edit.

So I though I'd put together a quick example of how we can use a component to produce a 'macro library' - bundling up several related macros into a package that we can just drag-and-drop into a flowchart whenever we need it. The example also shows how the 'macro library' can have its own set of 'global' variables, contants and even editable properties, that are encapsulated inside the component so that they won't "leak out" into the rest of your project.

I chose a real problem that has come up a couple of times recently on the forum to use as an example. The compiler that we use for most 8-bit PIC devices has only limited support for float maths. The basics like addition and multiplication are supported, but there are no trigonometry functions or logarithms, for example. This has caught out a couple of users recently who required logarithms, but needed to do it on our little ECIO modules. The new component adds support for ln(x), exp(x), log10(x), sin(x), cos(x) and tan(x).
Don't worry about how the maths works - the whole point of such code libraries is that the person using them doesn't need to know, it can be a 'black box' that just gets the job done! Note also, this is not just an abstract example - if you do find yourself needing these maths functions on an 8-bit PIC, this component is a fully working solution!

First, here's the final exported component...
float_maths_8bit.fcpx
(2.98 KiB) Downloaded 566 times
To use it, just copy the file into the components folder of your Flowcode installation (e.g. C:\Program Files (x86)\Flowcode 6\components). The next time you open Flowcode, you will see a "Float Maths 8-bit" component in the "Tools" section of the component browser. Drag the component onto your System Panel or Dashboard Panel - you'll see a small icon with one of those weird symbols that mathematicians seem to like - just to remind you that the code library is now present in your flowchart.
To call the maths functions, remember that you'll need a Component Macro icon - you can't use them directly in a Calculation icon like you would the native maths functions; but that's only a very minor inconvenience. If you right-click the panel icon and choose 'Properties', you'll notice that there are a couple of 'global' settings you can make...
- Precision - This is just a positive integer number. A bigger number makes the internal maths more accurate, but the calculation will take longer. Values between around 4...8 are recommended.
- Angle Units - For the trignometry functions you can decide whether you'd like to use Degrees or Radians.

NB) If you just want to use the component, but don't care for writing your own, you can safely ignore the rest of this post!

And here's the 'source code' for the component.
Float Maths 8-bit.fcfx
(23.01 KiB) Downloaded 541 times
As this is just a forum post, there's not space to really go into this step-by-step here. The Flowcode documentation Wiki has several example of component building, so I'll just note a few of the features that make for a useful 'macro library' as opposed to the more typical case of a 'hardware' component...

- If you look at the variables and constants, you'll see a whole range of maths constants defined globally for use by all of the functions. When exported as a component, however, these will become local to the component and are safely wrapped up inside. The macro's can't break because you didn't define the right global variables - nor will they create a 'name clash' if you already happened to have some variables with the same names in your main flowchart.

- If you open the properties panel, you can see how the properties have been defined. These are a very flexible way to give the 'end user' some control over how the macros in the 'library' will work.

- You will notice maybe a few macros in here that you couldn't access when using the 'library' as a component. That's because they are just little 'helper' macros that set up some common code - converting between Degrees and Radians, for example, which all of the trig' functions need, but don't make much sense to use on their own. Which macros become visible is set in the 'Interface Manager' section of the component properties.

- Flowcode compiles macros to your hardware target very intelligently. If, for example, our final design only ever calls the 'sin()' macro from the component, then the code for all the others would not be compiled - and no memory gets allocated for variables inside the component that never end up getting used. So you can bundle as many macros as you like into a single 'library' without worrying that you'll be wasting valuable program memory on your chip.

- Components can include other components! So, for example, if we made a more complex component that required this sort of maths, we could include our 'macro library' within the other component. Again, Flowcode is really intelligent about this - if the same component is loaded in many different places, the code will still only be compiled to your chip once so that there's no wasted memory.

- Using components this way makes it really easy to share code macros with our fellow forum users - no more need to copy individual macros around, or tear apart our flowcharts and 'reverse engineer' variables names etc. just to use a bit of code that a buddy is helping us with. And if you are working professionally, and your code might include some source code that you don't want to reveal, you can use this method to share a set of macros without giving away the secrets of how they work.

Hopefully this post has given a little insight into just how versatile the new component creator can be. It's not just all about making funky 3D animated simulations (through that is rather cool!) - it can be used to emulate the kind of source code libraries that coders in more traditional computer languages rely on all the time, letting you develop your project quicker because you don't have to 're-invent' the wheel with every new project.

Best Regards,
Steve.

(NB - for those that are interested, the maths functions in the examples are all programmed using Taylor Polynomial expansions. The 'Precision' property decides how many terms of the expansion are used for the approximation. The accuracy of the results is comparable to the 'native' functions used in the bigger target chips when the precision is set to around 6 terms.)

hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times
Contact:

Re: Macro Library - 8-bit Float Maths

Post by hyperion007 »

May I ask if it is possible for you to implement a macro for rounding a float to a configurable number of decimal places?

Since the calculation function: fround$(float,int) doesn't work for PIC with the compiler supplied. I have posted on the forum over at the website of the company that makes the compiler but it's been over a month and they have not bothered replying so I'm stuck. It's a bit silly to display temperature readings on a display using 6 decimal places. I need the 6 decimal precision for the rest of the calculations but not for the final result.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Macro Library - 8-bit Float Maths

Post by medelec35 »

Hi Daniel,
Have you seen This post?
It may or may not be useful?

Martin.
Martin

hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times
Contact:

Re: Macro Library - 8-bit Float Maths

Post by hyperion007 »

Yes I have seen that but I don't want to cut off the number, I need to round it.

hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times
Contact:

Re: Macro Library - 8-bit Float Maths

Post by hyperion007 »

Any chance of adding a working macro for pow(x,y) ?

By working I mean where y can be more than whole numbers (decimals, 4.1234 etc)


Thanks

/Daniel S

Post Reply