Shop OBEX P1 Docs P2 Docs Learn Events
P2 SPIN python to spin — Parallax Forums

P2 SPIN python to spin

pic18f2550pic18f2550 Posts: 392
edited 2021-07-02 15:21 in Propeller 2

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.

  • Cluso99Cluso99 Posts: 18,069

    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.

  • AribaAriba Posts: 2,682

    @pic18f2550 said:
    ...
    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:

    XYPOL(x, y) : length, angle32bit               Convert (x,y) to (length,angle32bit)
    
  • pub atan2(x, y) : a
      a := xypol(x, y) sca ( 360 * 1_000)
    

    This should fit.

  • evanhevanh Posts: 15,192
    edited 2021-07-04 10:06

    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:

    float  c_asin( float sine )  // sohcahtoa
    {
        uint32_t  opposite, adjacent, angle;
        float  scale = (float)0x4000_0000;
    
        adjacent = sqrtf( 1.0 - sine * sine ) * scale;
        opposite = sine * scale;
        __asm {
            qvector adjacent, opposite
            getqy   angle
        }
    
        return  angle / (float)0x8000_0000 * pi;
    }
    
    
    
    float  c_acos( float cosine )  // sohcahtoa
    {
        uint32_t  opposite, adjacent, angle;
        float  scale = (float)0x4000_0000;
    
        adjacent = cosine * scale;
        opposite = sqrtf( 1.0 - cosine * cosine ) * scale;
        __asm {
            qvector adjacent, opposite
            getqy   angle
        }
    
        return  angle / (float)0x8000_0000 * pi;
    }
    
    
    
    float  c_atan( float tangent )  // sohcahtoa
    {
        uint32_t  opposite, adjacent, angle;
        float  scale = (float)0x4000_0000;
    
        adjacent = scale / sqrtf( 1.0 + tangent * tangent );
        opposite = tangent * adjacent;
        __asm {
            qvector adjacent, opposite
            getqy   angle
        }
    
        return  angle / (float)0x8000_0000 * pi;
    }
    
  • pic18f2550pic18f2550 Posts: 392
    edited 2021-07-05 09:38

    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

  • evanhevanh Posts: 15,192
    edited 2021-07-05 09:42

    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)

  • evanhevanh Posts: 15,192

    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, maybe x,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:

        x, _ := xypol(x, y)
    
Sign In or Register to comment.