Flowcode handling formulas

Any general or miscellaneous queries that do not fit into the other forum catagories

Moderators: Benj, Mods

Post Reply
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:

Flowcode handling formulas

Post by medelec35 »

I am using the following calculation:
fet_on_time = mark_time / 100 * 52 + 60 * - 68 / 100
Calculation is correct eg if I enter 128, result should be 26 (result calulated on calculator and excel)
However, when ran in a different simulator, result was not 26 but was much higher.
Should above calculation be in C rather then calculation box?

Does calculation box use BIDMAS like calculators and excel?

Would I need to use brackets to make result correct?
I have read something about handling negative numbers, could this have a bearing on result?
Or is my simulator wrong, and result is correct?

Please answer this one, as this is the only thing holding me up now.
Last edited by medelec35 on Mon Apr 27, 2009 2:46 pm, edited 1 time in total.
Martin

User avatar
Steve
Matrix Staff
Posts: 3421
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: Flowcode handling formulas

Post by Steve »

I'd always suggest you use brackets in situations like this. This has the added benefit of making it clear what you are trying to do.

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: Flowcode handling formulas

Post by medelec35 »

Hi Steve.
I have change calculation to 4 + 2 * 8 and left out brackets.
Correct answer of 0x14 was displayed in register.
This would mean brackets are not required?
In my original formula, I tried the following: fet_on_time = ( mark_time / 100 * 52 ) + 60 * ( - 68 / 100 )
but wrong value is still in register 0x29. with 245 (0xFE) at add 0x23, 92 (0x5C) should appear at add 0x29
This is how I would like the program to function:
While GP0 is high, now action required.
When GP0 goes low, timer1 will start counting.
timer1 wil stop counting if either GP0 goes back high, or timer1> 0x254
now program is waiting for GP1 to be within a range of 0.12V and 0.97V. If it is (around zero crossing)
then mark_time = timer1 (note mark is = 0, and space = 1 i.e. inversion of PWM.
using formula: fet_on_time = ( mark_time / 100 * 52 ) + 60 * ( - 68 / 100 )
The fet_on_time will be a value from 2.6mS to 9.2ms. Since calculations don't use d.p, the calculated fet_on_time will be a value from 26 to 92 so delay will be from 100us(26) to 100us(92)
(80 = 26 and 254 = 92)
GP5 will be on for this timed duration.
Then process is repeated.
GP2 is used for over current. if GP2 INT has detected a large rise in current, then GP5 goes low until GP0 and GP1 have gone low as in description above.

Would you be able to state why calculation for fet_on_time is correct by calculator but not in practice on Flowcode please?

It could be me just overlooking the obvious.
Attachments
PWM to AC7.fcf
(12.5 KiB) Downloaded 268 times
Martin

User avatar
Steve
Matrix Staff
Posts: 3421
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: Flowcode handling formulas

Post by Steve »

There are lots of things I don't understand about your formula. For example, 60 * ( - 68 / 100 ) will equal -60 or 0 because the divide is done first and you are using integer-based mathematics [-68/100 = -0.68 which will be rounded to either 0 or -1].

When doing integer calculations, it is really important that you rearrange the formula so that divides are essentially done at the end. Either that, or use floating-point calculations.

Also, by "( mark_time / 100 * 52 )" are you intending "( mark_time / 5200 )" or "( 52*mark_time / 100)"? This result will also be affected by doing the divide first.

You might find it better to rearrange the formula so it look like this (if I have got your intentions right):
fet_on_time = ( ( mark_time * 52 ) - 4080) / 100

I also note that in your program you are using C code to set the value of mark-time. C code is not simulated within Flowcode, so this value will not be filled how you intend.

I hope the above helps.

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: Flowcode handling formulas

Post by medelec35 »

Solved Calculation problem. Last night
I simplified fet_on_time = ( mark_time / 100 * 52 ) + 60 * ( - 68 / 100 ) (which is correct formula derived from simultaneous equations by putting high and low values before conversion and after conversion into an excel sheet.
However after further simplifying by getting HCF etc and using result : fet_on_time = mark_time * 13 /25 - 205/5

Delay_time was 100% spot on in simulation.

My maths is not as good as yours : :cry:
It looks like you have done a similar thing but rearranging in a better order, so I’m sure that will work too
So thank you.

I will change the excel sheet to further simply equations, since I will need to use it in the future.
Martin

User avatar
Steve
Matrix Staff
Posts: 3421
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: Flowcode handling formulas

Post by Steve »

Glad you've got it working.

Just a quick note to say that your equation should be "fet_on_time = mark_time * 13 /25 - 204/5" (204, not 205).

And my original equation can be further simplified to the following (I forgot to divide and bottom by 4, as you have done):

fet_on_time = ( ( mark_time * 13 ) - 1020) / 25

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: Flowcode handling formulas

Post by medelec35 »

steve wrote:Glad you've got it working.

Just a quick note to say that your equation should be "fet_on_time = mark_time * 13 /25 - 204/5" (204, not 205).
fet_on_time = ( ( mark_time * 13 ) - 1020) / 25
Yes you are correct, I have got 204, 205 was just a typing error.
Martin

Post Reply