P2 SPIN python to spin
pic18f2550
Posts: 400
Hello,
does anyone know python and how to implement this in Spin?
Thanks.
a = math.atan2(y, x) if ( a < 0): a += 2 * math.pi a = math.degrees(a) + 0.78
Comments
Spin does not support floating point operations natively, so you'll have to download a floating point object. For Spin2 there's a BinFloat object.
I think CORDIC may be useful here but no idea how it works.
I found out what the code section is supposed to be for.
It is a kind of sin-cos encoder with an offset value.
I am actually only looking for the CORTIC part, where an angle value is output from the x and y values.
From the Spin2 documentation:
This should fit.
You've got me trying to nut out how to do this ... Looking at what Eric has done for C support of same functions ... I've figured out for each case of arcsine, arccosine and arctangent each requires a single square-root calculation, amongst a few multiplies and divides, preceding the Cordic's QVECTOR command.
Here's three routines that I've tested/compared, correct for unsigned numbers first qradrant, against Eric's built-ins:
XYPOL(x, y) : length, angle32bit Convert (x,y) to (length,angle32bit)
Pretty thin description
What is the value range of X & Y ? 32bit with sign?
My values are in the range $F800..$07FF(-2048..2047 ) so no scaling should be necessary.
Two return values ? length, angle32bit
length should be sqr( X² + Y²)
angle32bit should contain the angle but what are the values e.g. at 45°, 90°, 180 and 360° without scaling?
In PASM:
QVECTOR x, y
GETX länge
GETY Winkel
SCA will require a single input. It can't be tacked in that way. I imagine most operators will have that issue.
even without SCA the problem is present.
xy := xypol(x, y)'' sca ( 360 * 1_000)
I'm not sure how spin syntax goes. The
xy
will be no good as well, it'll need to be a pair of variables, maybex,y := xypol(x,y)
that's new to me, I didn't know that was possible.
Thanks
If you don't need all of the values from a method that returns multiple values, you can use an underscore to ignore one (or more) of them. For example: