Programming with Trig Functions??
PlusOn3
Posts: 15
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
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
If Spins is OK I might suggest using floating point math. Make use of the F32 object in OBEX which has these functions.
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
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?
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.
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.