View Full Version : Sin and Cos in Spin

crgwbr

01-27-2007, 07:22 AM

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

asterick

01-27-2007, 07:34 AM

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 Schwabe (Parallax)

01-27-2007, 11:55 AM

crgwbr (http://forums.parallax.com/member.php?u=46269),

·

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[$F000 - Result]

else

Result := word[$E000 + Result]

if angle & $1000

-Result

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

Beau Schwabe (mailto:bschwabe@parallax.com)

IC Layout Engineer

Parallax, Inc.

crgwbr

01-28-2007, 06:16 AM

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 Allen

01-28-2007, 07:51 AM

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[$E000 + result]

1 : result := word[$F000 - result]

2 : result := -word[$E000 + result]

3 : result := -word[$F000 - result]

return

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

Tracy Allen

www.emesystems.com (http://www.emesystems.com)

crgwbr

01-28-2007, 08:51 AM

Thanks Tracy

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

NerdMaster

For

Life

Graham Stabler

01-29-2007, 07:42 PM

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 Stabler

01-29-2007, 07:44 PM

Oops, you said spin

crgwbr

01-29-2007, 07:49 PM

Sorry Graham, but I'm haveing enough trouble with learning spin. Not quite ready to learn assembly.

Thanks Anyway,

crgwbr

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

NerdMaster

For

Life