• AVR Freaks

Hot!Running out of memory when using math functions (Cosine or Power)

Author
marvinatorrr
New Member
  • Total Posts : 21
  • Reward points : 0
  • Joined: 2020/01/14 03:42:57
  • Location: 0
  • Status: offline
2020/02/24 10:48:06 (permalink)
0

Running out of memory when using math functions (Cosine or Power)

I cant seem to use the cosine or power function.
 
Using PIC18F1220, XC8 v1.34, MPLAB v5.30
 

void speedControl(int error)
{
    double speed;
    //speed = -0.4 * pow((double)error, 2.0) + 1.0;
    //speed = cos(0.001535 * (double)error);
    CCPR1L = speed * 199;
}

 
Whenever I try and use either math function and I get an error like this (this is for Cos):
 

:0: error: (1347) can't find 0x60 words (0x60 withtotal) for psect "cinit" in class "CODE" (largest unused contiguous range 0x48)
:0: error: (1347) can't find 0x5C words (0x5c withtotal) for psect "text27" in class "CODE" (largest unused contiguous range 0x48)
:0: error: (1347) can't find 0x50 words (0x50 withtotal) for psect "text9" in class "CODE" (largest unused contiguous range 0x48)
:0: error: (1347) can't find 0x3C words (0x3c withtotal) for psect "text19" in class "CODE" (largest unused contiguous range 0x2E)
:0: error: (1347) can't find 0x2A words (0x2a withtotal) for psect "text24" in class "CODE" (largest unused contiguous range 0x4)
:0: error: (1347) can't find 0x2A words (0x2a withtotal) for psect "text25" in class "CODE" (largest unused contiguous range 0x4)
:0: error: (1347) can't find 0x28 words (0x28 withtotal) for psect "text15" in class "CODE" (largest unused contiguous range 0x4)
:0: error: (1347) can't find 0x24 words (0x24 withtotal) for psect "text10" in class "CODE" (largest unused contiguous range 0x4)
:0: error: (1347) can't find 0x1A words (0x1a withtotal) for psect "text22" in class "CODE" (largest unused contiguous range 0x4)
:0: error: (1347) can't find 0x18 words (0x18 withtotal) for psect "text26" in class "CODE" (largest unused contiguous range 0x4)
:0: error: (1347) can't find 0x10 words (0x10 withtotal) for psect "text21" in class "CODE" (largest unused contiguous range 0x4)
:0: error: (1347) can't find 0xE words (0xe withtotal) for psect "text23" in class "CODE" (largest unused contiguous range 0x4)
:0: error: (1347) can't find 0x6 words (0x6 withtotal) for psect "idataCOMRAM" in class "CODE" (largest unused contiguous range 0x4)
(908) exit status = 1

#1

12 Replies Related Threads

    mbrowning
    USNA79
    • Total Posts : 1746
    • Reward points : 0
    • Joined: 2005/03/16 14:32:56
    • Location: Melbourne, FL
    • Status: offline
    Re: Running out of memory when using math functions (Cosine or Power) 2020/02/24 11:10:23 (permalink)
    +3 (3)
    Using floating point functions on the smallest part in the PIC18 line is bound to fail with insufficient code space. You could try compiling for an 18F1330 to see if the code will fit. Or you might try a newer version of XC8 with higher optimization to see if it might possibly fit. The best code space on recent versions I've used is 2.05 in C90 mode and optimization level 2 (levels up to 2 are free. Above that costs, but you can use the free trial to see if it makes a difference).
    #2
    crosland
    Super Member
    • Total Posts : 1940
    • Reward points : 0
    • Joined: 2005/05/10 10:55:05
    • Location: Warks, UK
    • Status: offline
    Re: Running out of memory when using math functions (Cosine or Power) 2020/02/24 11:38:08 (permalink)
    +2 (2)
    Why on earth are you trying to use floating point on such a small device?
    #3
    1and0
    Access is Denied
    • Total Posts : 10783
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Running out of memory when using math functions (Cosine or Power) 2020/02/24 11:46:05 (permalink)
    +1 (1)
    Furthermore, 32-bit float will take more code space than 24-bit float.  Why are you casting an int to a double only to raise it to a power of 2?  Even so, raising a floating point to a power of 2 can be optimized, much better than pow().
    #4
    1and0
    Access is Denied
    • Total Posts : 10783
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Running out of memory when using math functions (Cosine or Power) 2020/02/24 11:54:15 (permalink)
    +1 (1)
    marvinatorrr
    I cant seem to use the cosine or power function.
    Using PIC18F1220, XC8 v1.34, MPLAB v5.30

    void speedControl(int error)
    {
        speed = -0.4 * pow((double)error, 2.0) + 1.0;
        CCPR1L = speed * 199;


    That can be done with integer math, but why are you setting CCPR1L to a negative value?
     
    Edit: You do know CCPR1L has a size of only 8 bits?  That is, a value of "error" greater than 2 or less than -2 will overflow it!
    post edited by 1and0 - 2020/02/24 12:20:31
    #5
    Chris A
    Super Member
    • Total Posts : 855
    • Reward points : 0
    • Joined: 2010/07/20 04:37:07
    • Location: 0
    • Status: offline
    Re: Running out of memory when using math functions (Cosine or Power) 2020/02/24 12:01:40 (permalink)
    0
    Think of it this way. What range of values can error take.  Can you pre-calculate a small lookup table to do the job.
    #6
    1and0
    Access is Denied
    • Total Posts : 10783
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Running out of memory when using math functions (Cosine or Power) 2020/02/24 12:08:42 (permalink)
    +1 (1)
    Chris A
    Think of it this way. What range of values can error take.  Can you pre-calculate a small lookup table to do the job.

    If the calculation in my previous post is what OP really wanted, then a table of ~3 entries will work. LOL!!!
     
    post edited by 1and0 - 2020/02/24 12:21:03
    #7
    NKurzman
    A Guy on the Net
    • Total Posts : 18678
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: Running out of memory when using math functions (Cosine or Power) 2020/02/24 12:37:05 (permalink)
    +2 (2)
    Floating point libraries are big.  Your 4K is not enough to the Libraries and the rest of you code.
    You need a Bigger chip, or smarter code.
    #8
    marvinatorrr
    New Member
    • Total Posts : 21
    • Reward points : 0
    • Joined: 2020/01/14 03:42:57
    • Location: 0
    • Status: offline
    Re: Running out of memory when using math functions (Cosine or Power) 2020/02/24 18:47:11 (permalink)
    0
    @1and0 oops that was meant to be positive

    @Chris A error can be a range from -1023 to +1023. After reading the replies I will probably be going with a look up table. Kinda like a low resolution equation.
    #9
    1and0
    Access is Denied
    • Total Posts : 10783
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Running out of memory when using math functions (Cosine or Power) 2020/02/24 23:31:11 (permalink)
    +2 (2)
    marvinatorrr
    @1and0 oops that was meant to be positive

    Still, most of those results cannot fit in the 8-bit CCPR1L register.


    @Chris A error can be a range from -1023 to +1023. After reading the replies I will probably be going with a look up table. Kinda like a low resolution equation.

    Since cos(-x) = cos(x) you'll need only half the table.
    #10
    Aussie Susan
    Super Member
    • Total Posts : 3709
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: Running out of memory when using math functions (Cosine or Power) 2020/02/25 18:27:28 (permalink)
    +3 (3)
    Also cos(x+90) = -cos(90-x) so a quarter table can be used (with appropriate sign correction)
    Also look at the accuracy of the domain (to give it the correct term - range is for the output of the function) values - perhaps every 5 degrees is sufficient.
    Susan
    #11
    1and0
    Access is Denied
    • Total Posts : 10783
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Running out of memory when using math functions (Cosine or Power) 2020/02/25 22:07:02 (permalink)
    0
       cos(0.001535 * error)        , where error = -1023 to +1023
    = cos(-1.570305) to cos(1.570305)
    = cos(-pi/2) to cos(pi/2)
     
    So a table of 0 to pi/2 radian (0° to 90°) is sufficient.
    #12
    oliverb
    Super Member
    • Total Posts : 282
    • Reward points : 0
    • Joined: 2009/02/16 13:12:38
    • Location: 0
    • Status: offline
    Re: Running out of memory when using math functions (Cosine or Power) 2020/02/26 01:53:06 (permalink)
    +1 (1)
    Alternatively you could use the power series approach, but rewrite it to use integer math. Raising to a power of two is just multiplying by itself.
     
    The trick is to think of the values as fractions so if you have a value w that you want to replace with an integer you could substitute w=(x/divisor) where x is an integer type and the divisor is chosen to keep the integer within range. The divisor is also usually a power of 2 so divisions can be performed using shift operations.
     
    Then you multiply through by the divisor to get an all-integer version of the formula.
    #13
    Jump to:
    © 2020 APG vNext Commercial Version 4.5