Shop OBEX P1 Docs P2 Docs Learn Events
SL32_INTEngine.spin HELP! — Parallax Forums

SL32_INTEngine.spin HELP!

mhamen3mhamen3 Posts: 69
edited 2011-07-14 13:07 in Propeller 1
Hi guys,

I'm pretty new to the forums, very new to SPIN. I'm trying to use the SL32_INTEngine.spin object but I don't seem to be using it right. What I'm trying to do is run two servos through a 140 degree (servo travel) sinusoidal sweep, 90 degrees out of phase of each other. It's probably my code, which I'll post below, but I also don't totally understand the operations of the object.

Does it only work from 0 to 90deg? Do I have to mirror or reverse it myself?

If it does work through 360deg will it also work past that? e.g. can I put 390deg in and get the same results as 30deg?

Anyway, here is the code. I'm at work so I can't just post the file but it's short anyway.
CON
 _CLKMODE=XTAL1 + PLL16X
 _CLKFREQ=5_000_000

 ServoMin=700               'Used for servo control, won't allow PWM signal to go below 700
 ServoCh1 = 1
 ServoCh2 = 2

OBJ
 SERVO : "Servo32v7.spin"
 TRIG     : "SL32_INTEngine.spin"

VAR

  long Angle
  long PosA
  long PosB

PUB main
  SERVO.start
  SERVO.ramp
  SERVO.set(ServoCh1,1500)
  SERVO.set(ServoCh2,1500)
  Angle := 1                                                          ' Set initial condition (necessary?)

 repeat
   PosA := TRIG.sin(Angle,1)*1400+ServoMin         'Scales sweep of 1400 by sin value and adds it to servo minimum
   PosB := TRIG.cos(Angle,1)*1400+ServoMin
   SERVO.set(ServoCh1, PosA) 
   SERVO.set(ServoCh2, PosB)
   waitcnt(1_400_000 + cnt)                          'waits about 1/60th of a sec to allow movement
   Angle := Angle + 1                                     ' adds 1 degree for next iteration
   IF Angle==361
     Angle := 1                                                 'Reset
If anyone could help me out with this I would much appreciate it:lol:

Comments

  • KyeKye Posts: 2,200
    edited 2011-07-14 12:21
    Change the "1" in the function calls to a large number like 100 or 300. etc. (Note: The large number will then be the ouput range multiplied by the sin/cos of the angle). E.g. sin(90, 100) = 100... sin (45, 100) = 70... sin(30, 100) = 50.

    Let's go back to Trig. So remember the unit circle? And how all sin and cos calculations were done using the unit circle? Well... since we are using integers and not floats we can't use the unit circle due to having numbers less than one on it. But, if we were to scale the unit circle up then we could then use it.

    That's what the second argument of the function call is. Just think about trig a bit when using the library and everything will make sense. All the functions work using triagles and then calculating the COS and SIN argument from them. Just pull the wikipedia page up and you should be good.
  • mhamen3mhamen3 Posts: 69
    edited 2011-07-14 13:07
    Aahh ha. I get you. I forgot that only integers were used. I understand the trig I just didn't realize I was screwing myself by limiting the result to either a 0 or a 1.

    Thanks a lot, Kye.
Sign In or Register to comment.