Shop OBEX P1 Docs P2 Docs Learn Events
Programming with Trig Functions?? — Parallax Forums

Programming with Trig Functions??

PlusOn3PlusOn3 Posts: 15
edited 2012-03-03 13:01 in Propeller 1
I have some formulas that I would like to run in one of my programs that require the use of trig functions. I searched through the manual and was able to find an example of something (I can't figure out what it means). Does anyone know how to calculate the sin, cos, tan, arcsin, arccos, and arctan?? This is the code that was given in the manual if anyone can figure out what the hell is going on here and explain it to me that would work too.

Thanks guys
' Get sine/cosine
'
' quadrant: 1 2 3 4
' angle: $0000..$07FF $0800..$0FFF $1000..$17FF $1800..$1FFF
' table index: $0000..$07FF $0800..$0001 $0000..$07FF $0800..$0001
' mirror: +offset -offset +offset -offset
' flip: +sample +sample -sample -sample
'
' on entry: sin[12..0] holds angle (0° to just under 360°)
' on exit: sin holds signed value ranging from $0000FFFF ('1') to
' $FFFF0001 ('-1')
'
getcos add sin,sin_90 'for cosine, add 90°
getsin test sin,sin_90 wc 'get quadrant 2|4 into c
test sin_sin_180 wz 'get quadrant 3|4 into nz
negc sin,sin 'if quadrant 2|4, negate offset
or sin,sin_table 'or in sin table address >> 1
shl sin,#1 'shift left to get final word address
rdword sin,sin 'read word sample from $E000 to $F000
negnz sin,sin 'if quadrant 3|4, negate sample
getsin_ret
getcos_ret ret '39..54 clocks
'(variance due to HUB sync on RDWORD)
sin_90 long $0800
sin_180 long $1000
sin_table long $E000 >> 1 'sine table base shifted right
sin long 0

Comments

  • Heater.Heater. Posts: 21,230
    edited 2012-01-23 16:49
    Do you need the speed of PASM or can you get along in Spin?
    If Spins is OK I might suggest using floating point math. Make use of the F32 object in OBEX which has these functions.
  • AribaAriba Posts: 2,690
    edited 2012-01-23 19:55
    PlusOn3 wrote: »
    ...This is the code that was given in the manual if anyone can figure out what the hell is going on here and explain it to me that would work too.
    ' Get sine/cosine
    '
    ' quadrant: 1 2 3 4
    ' angle: $0000..$07FF $0800..$0FFF $1000..$17FF $1800..$1FFF
    ' table index: $0000..$07FF $0800..$0001 $0000..$07FF $0800..$0001
    ' mirror: +offset -offset +offset -offset
    ' flip: +sample +sample -sample -sample
    '
    ' on entry: sin[12..0] holds angle (0° to just under 360°)
    ' on exit: sin holds signed value ranging from $0000FFFF ('1') to
    ' $FFFF0001 ('-1')
    '
    getcos add sin,sin_90 'for cosine, add 90°
    getsin test sin,sin_90 wc 'get quadrant 2|4 into c
    test sin_sin_180 wz 'get quadrant 3|4 into nz
    negc sin,sin 'if quadrant 2|4, negate offset
    or sin,sin_table 'or in sin table address >> 1
    shl sin,#1 'shift left to get final word address
    rdword sin,sin 'read word sample from $E000 to $F000
    negnz sin,sin 'if quadrant 3|4, negate sample
    getsin_ret
    getcos_ret ret '39..54 clocks
    '(variance due to HUB sync on RDWORD)
    sin_90 long $0800
    sin_180 long $1000
    sin_table long $E000 >> 1 'sine table base shifted right
    sin long 0
    

    I hope it's not formated like that in the manual.
    The code uses the sine table in the Propeller ROM to generate sine and cosine values.The ROM has only the values for 0..90° so there is some calculation necessary.

    I think there is also a integer TRIG math object in the OBEX (written in Spin).

    Andy
  • SONIC the HedgehogSONIC the Hedgehog Posts: 321
    edited 2012-01-24 04:48
    If only they could have made it as simple as basic......
    Wouldn't you just be able to do sin[36..0] and be able to get the sine of value of 36 or is it the zero where the 36 should be?
  • ErlendErlend Posts: 612
    edited 2012-01-24 05:48
    There is a Sine lookup table inside Propeller - maybe you could use that?
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-01-24 09:13
    I agree with Heater, I'd start with F32 to do trig.

    You just need to make sure dont't try to mix foating point numbers with normal integers. You'll need to use the methods in F32 to convert them back and forth.
  • Kerno97Kerno97 Posts: 1
    edited 2012-03-03 10:14
    Would this program work in all quadrants for compass? I was planning on using a gps, compass module, and a microcontroller to orient a robot in the direction it started from and I wasn't sure if the resulting angle would be in compass directions.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-03-03 13:01
    Kerno97 wrote: »
    Would this program work in all quadrants for compass? I was planning on using a gps, compass module, and a microcontroller to orient a robot in the direction it started from and I wasn't sure if the resulting angle would be in compass directions.

    Kerno97,

    Yes, F32 will work with all quadrants. For instance a normal arctangent function will return an angle between -90 degrees and +90 degrees. With the Atan2 method you supply two number instead of just one (the y coordinate and the x coordinate) and the method will retun an angle between 0 and 360 degrees (atan2 is not unique to F32 or the Propeller). I used this (here comes a plug to my current project) to determine the angle my robot should travel by finding the angle made by the joystick from its center position.

    GPS stuff gets complicated fast but there are objects others have written that makes using GPS much easier.
Sign In or Register to comment.