Shop OBEX P1 Docs P2 Docs Learn Events
arctan — Parallax Forums

arctan

Jonathan HolleJonathan Holle Posts: 48
edited 2010-07-19 00:51 in BASIC Stamp
Hi all !

I'am doing an hexapod controlled by a basic stamp 2 and two PSC. So, in order to do inverse kinematic I have to use inverse trig functions (arcsin, arccos and arctan).
The only way I found is this :

' {$STAMP BS2}
' {$PBASIC 2.5}

X VAR Byte ' input value 0 to 99 (0.00 to 0.99)
Y VAR Byte
arccos VAR Byte ' angle in brads, 0<=A<=64.
arcsin VAR Byte
arctan VAR Byte
Z VAR Byte ' helper variable

X=


Z = X */ 983 / 3 ' renormalize to 127
arccos=63-(Z/2) ' first approx, to minimize iterations
boucle:
IF COS arccos <= Z THEN done
Arccos = Arccos+1
GOTO boucle
done:
Arccos = Arccos */ 360 ' convert brads to degrees using */ operator

arcsin = 90 - arccos

'For a negative X :
'Arccos = 180 - Arccos
'arcsin = -arsin

DEBUG "arccos 0.", DEC2 X," = ",DEC Arccos, CR
DEBUG ?arcsin, CR


But how could I get the arctan function, is there any relation with arcsin or arccos.
I know that a better way is to use a table ... But being completely beginner in BS2 programming, I don't know how to do it ...
Could you please help me :-D

many thanks !

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-07-18 23:13
    There is an arctangent function (operator) in PBasic (ATN). Look in the help files for the Stamp Editor or in the Stamp Syntax and Reference Manual if you have that.
  • Jonathan HolleJonathan Holle Posts: 48
    edited 2010-07-18 23:27
    But this function just returns the angle to a vector specified by X and Y coordinate values ; Am I wrong ?
  • Martin_HMartin_H Posts: 4,051
    edited 2010-07-18 23:55
    At one time I was awesome with trig functions, but I'm really rusty now. But when I get to the programming phase of my robot arm I'll need to work all this out in PBASIC. But it looks like arcsin and arccos can be defined in terms of arctan and the use of the half angle formula. Have a look here: http://en.wikipedia.org/wiki/Inverse_trigonometric_functions
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2010-07-19 00:51
    Jonathan, the Stamp ATN operator (x ATN y) does take the coordinates x and y, but the answer depends only on the ratio y/x (within quadrants 1 and 4). So (1000,1000) and (7,7) should give the same angle, 45°, or 32 brads in Stampese. If you are given a ratio, you can translate that into coordinates to plug into x and y. The related function, (x HYP y), returns the length of the vector, and that of course is different for those two points.

    I have a computation of the ARCTAN (using CORDIC math) posted at
    emesystems.com/programs/cordic-atn.bpe
    emesystems.com/BS2mathC.htm
    It is more accurate than the Stamp ATN operator, but slower.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
Sign In or Register to comment.