Page 1 of 1

MATH problem

Posted: Sun Feb 11, 2007 2:23 pm
by gharryh
I want to perform this routine

distanceProd = distance * 36
timeProd = seconds * 10000
KPH = distanceProd / timeProd
sKPH = KPH * 32

where distance = 10000 (this is the maximum distance)
and seconds = 3600 (the minimum KPH of 0.01 at maximum distance)

This wouild mean a 32/24bit opperation. How can i do that in FlowCode

Regarsd

Harry

Posted: Mon Feb 12, 2007 11:11 am
by Benj
Hello Harry

You may be able to do the calculations using int variables but if the numbers are getting too large then you can define short variables and use a C code Block.

Code: Select all

short distanceProd, timeProd, KPH, sKPH; 

distanceProd = FCV_DISTANCE * 36;
timeProd = FCV_SECONDS * 10000; 
KPH = distanceProd / timeProd; 
sKPH = KPH * 32; 
However this calculation routine would be very inefficient and would probably not work correctly due to instructions running into the thousands if not higher. A simpler approach would be to scale down the equasions and use psudo numbers to represent the actual numbers.

Posted: Mon Feb 12, 2007 11:33 am
by Steve
32-bit maths can be done within C using the "long" datatype, so this may work:

Code: Select all

long distanceProd, timeProd, KPH; 

distanceProd = FCV_DISTANCE * 36; 
timeProd = FCV_SECONDS * 10000; 
KPH = distanceProd / timeProd; 
FCV_SKPH = KPH * 32; 
(where the 3 FCV_ variables are defined within Flowcode as 16-bit "integers").

Posted: Mon Feb 12, 2007 1:06 pm
by Mark
gharryh

I would personally consider this as a math problem rather than a Flowcode capability issue.

You generate a number *36 and divide it by a number *1000. It makes more sense to combine these constants into a single divide by 28. This should then sit nicely within the +/- 32000 that Flowcode provides.

As a general point, 32,000 effectively allows for 2^15 accuracy, which is 2^ 3 or 8 times better resolution than a good A/D input. Hence, I would personally put 32 bit math as a low priority for Flowcode.

Hope this helps,

Mark

Mark


ps the 28 is an approximation and could be *10 then /277

Posted: Mon Feb 12, 2007 10:03 pm
by gharryh
Hi Steve,
steve wrote:32-bit maths can be done within C using the "long" datatype, so this may work:

Code: Select all

long distanceProd, timeProd, KPH; 

distanceProd = FCV_DISTANCE * 36; 
timeProd = FCV_SECONDS * 10000; 
KPH = distanceProd / timeProd; 
FCV_SKPH = KPH * 32; 
(where the 3 FCV_ variables are defined within Flowcode as 16-bit "integers").
If i would use this inside flowcode how should i do that??

Posted: Tue Feb 13, 2007 10:13 am
by Steve
Create 3 integer variables, "Distance", "Seconds" and "sKPH", within Flowcode.

Put the code into a C icon within your flowchart.

Before this icon, populate "Distance" and "Seconds" with appropriate values.

After the C icon, the value in "sKPH" should be correct.

NOTE: this code will not simulate, so you would need to download it to a PICmicro to see it working.

Posted: Tue Feb 13, 2007 10:31 am
by gharryh
Can i attach my flowcode for a review??

Posted: Tue Feb 13, 2007 2:16 pm
by Steve
steve wrote:1. Create 3 integer variables, "Distance", "Seconds" and "sKPH", within Flowcode.

2. Put the code into a C icon within your flowchart.

3. Before this icon, populate "Distance" and "Seconds" with appropriate values.

4. After the C icon, the value in "sKPH" should be correct.

NOTE: this code will not simulate, so you would need to download it to a PICmicro to see it working.
The code you need to place into a C icon in step (2) is the code previously mentioned, ie:

Code: Select all

long distanceProd, timeProd, KPH; 

distanceProd = FCV_DISTANCE * 36; 
timeProd = FCV_SECONDS * 10000; 
KPH = distanceProd / timeProd; 
FCV_SKPH = KPH * 32; 
But also pay attention to Mark's post. He is quite right; with a few adjustments to your maths routines, you will not need to delve into C and use 32-bit maths.

We will be allowing users to post their code for review on this site, hopefully within the next month or two (we are waiting for an update to the BB software).

If you were a registered user of Flowcode, I would suggest that you send the code to us and we would have a quick look. But unfortunately, you have not bought Flowcode and so we cannot provide that extra level of support.