Shop OBEX P1 Docs P2 Docs Learn Events
Sin and Cos in Spin — Parallax Forums

Sin and Cos in Spin

crgwbrcrgwbr Posts: 614
edited 2007-01-29 12:49 in Propeller 1
Are there any objects already released that do Sin and Cos calculations.· If not, I could just use a floating point co-processor; I would just rather do it with software.

Thanks,
crgwbr

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
NerdMaster
For
Life

Comments

  • asterickasterick Posts: 158
    edited 2007-01-27 00:34
    There is a SIN table in the rom, it's fixed point, so you can just convert it to floating point by sign extending it and then dividing it by 32768.0. I forget where it's located... check the manual
  • Beau SchwabeBeau Schwabe Posts: 6,547
    edited 2007-01-27 04:55
    crgwbr,
    ·
    Here are a few routines that can be incorporated into any object.· Keep in mind, that the Propeller contains a ROM table for 1/4 of SIN.· Based on your degree value you either flip and/or mirror the data to get the proper quadrant. (The routine below does this for you).· The·SIN·angle is represented as a 13-bit value, while the returned value is represented as a signed 16-bit value.
    ·

    PUB DEG2PROP(Deg)               'Convert Deg to 13-bit Propeller angle
        Result := (Deg * 1024)/45
     
    PUB Cos(angle)                  'Cos angle is 13-bit ; Returns a 16-bit signed value
        Result := sin(angle + $800)
     
    PUB Sin(angle)                  'Sin angle is 13-bit ; Returns a 16-bit signed value
        Result := angle << 1 & $FFE
        if angle & $800
           Result := word[noparse][[/noparse]$F000 - Result]
        else
           Result := word[noparse][[/noparse]$E000 + Result]
        if angle & $1000
           -Result
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • crgwbrcrgwbr Posts: 614
    edited 2007-01-27 23:16
    Thanks Beau,

    So to use those routines, I would use something like:

    PropAng := DEG2PROP(270)
    CosAng := Cos(PropAng)
    SinAng := Sin(PropAng)

    Would this work?

    Thanks Again,
    crgwbr

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    NerdMaster
    For
    Life
  • Tracy AllenTracy Allen Posts: 6,657
    edited 2007-01-28 00:51
    That's right, or you can encapulate it as:

    CosAng := Cos(DEG2PROP(270))
    SinAng := Sin(DEG2PROP(270))



    Here is an alternaive method to code the Sin routine that resolves the 4 quadrants using a case statement:

    PRI Sin(pangle) | q                           ' prop angle is 0 to 2^13 (0 to 8191) for 0 to 360 degrees
      q := pangle >> 11                            ' quadrant is 2 highest bits
      result := (pangle & $7ff) << 1             ' 0 to 90- degrees, 11 bits, times two for word offset into sine table
      case q                                           ' result by quadrant, lookup in HUB SINE table
        0 : result := word[noparse][[/noparse]$E000 + result]                     
        1 : result := word[noparse][[/noparse]$F000 - result]
        2 : result := -word[noparse][[/noparse]$E000 + result]
        3 : result := -word[noparse][[/noparse]$F000 - result]
      return
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • crgwbrcrgwbr Posts: 614
    edited 2007-01-28 01:51
    Thanks Tracy

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    NerdMaster
    For
    Life
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-01-29 12:42
    If you are interested in assembly or just high speed then I wrote a cordic algorithm that will compute R.Sin(theta) and R.Cos(theta) simultaneously in no time at all, it uses Cordic:

    http://en.wikipedia.org/wiki/Cordic

    My intention was to releas the code in two forms, assembly to be inserted in to other assembly programs and as a high speed cordic object. The cordic method can be used for other things like sqrt and 1/x so it could sit in a cog and just churn out what was asked of it. I just need time to sit down and put it together but after my move I am still no quite unpacked.

    I did release a basic demo program here though:

    http://forums.parallax.com/forums/default.aspx?f=25&m=150119

    Graham
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-01-29 12:44
    Oops, you said spin
  • crgwbrcrgwbr Posts: 614
    edited 2007-01-29 12:49
    Sorry Graham, but I'm haveing enough trouble with learning spin. Not quite ready to learn assembly.

    Thanks Anyway,
    crgwbr

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    NerdMaster
    For
    Life
Sign In or Register to comment.