Shop OBEX P1 Docs P2 Docs Learn Events
UPDATED: Need some help with alittle servo not working/jitter! — Parallax Forums

UPDATED: Need some help with alittle servo not working/jitter!

DiablodeMorteDiablodeMorte Posts: 238
edited 2007-10-11 16:46 in BASIC Stamp
Hello everbody. I need some help with some servo jitter. I have two servos hooked up to a basicSX OEM board(the servos work fine when i run a routine that isn't computer based) and I'm getting two words of data from a serial line at 9600 baud from a VB program i built myself(yay me). Anyway. The problem is, one of the servo's jitter. I did alittle debuging and it seems that which ever servo i address first in the code to pulsout jitters. The other does not(I switched up the servos just to maintain a control). Any helpful ideas as to remedy my problem?


Here's my code:

' PULSIN.bs2
' {$STAMP BS2sx}
' {$PBASIC 2.5}


'Pin Declarations
Pulse PIN 0 ' pulse input pin


'Variables
pwmInput1 VAR Word     ''PWM Input Variable
pwmInput2 VAR Word


'Constants
#SELECT $STAMP
#CASE BS2, BS2E, BS2PE
Scale CON $200 ' 2.0 us per unit
#CASE BS2SX, BS2P
Scale CON $0CC ' 0.8 us per unit
#CASE BS2PX
Scale CON $0CF ' 0.81 us per unit
#ENDSELECT


#SELECT $STAMP
#CASE BS2, BS2E, BS2PE
T1200 CON 813
T2400 CON 396
T9600 CON 84
T19K2 CON 32
T38K4 CON 6
#CASE BS2SX, BS2P
T1200 CON 2063
T2400 CON 1021
T9600 CON 240
T19K2 CON 110
T38K4 CON 45
#CASE BS2PX
T1200 CON 3313
T2400 CON 1646
T9600 CON 396
T19K2 CON 188
T38K4 CON 84
#ENDSELECT
Inverted CON $4000
Open CON $8000
Baud CON T9600 + Inverted

pwmInput1 = 1830
pwmInput2 = 1830

goPwm:
     'Attempt to handle more than one servo:
     PULSOUT 6, pwmInput1
     PULSOUT 5, pwmInput2

GOTO Main


'Main Program
Main:
  DO
    noData = 0
    'PULSIN Pulse, 1, pwmInput ' measure positive pulse
    SERIN 16, Baud, 20, goPwm, [noparse][[/noparse]DEC4 pwmInput1, DEC4 pwmInput2] ' receive one byte
    GOTO goPwm

  LOOP 


Post Edited (DiablodeMorte) : 10/11/2007 3:43:10 AM GMT

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-10-10 03:43
    I suspect your problem is that you're exceeding the 20ms maximum time between servo pulses. You have a 20ms timeout specified on the SERIN, but that's a per character timeout. You could have a delay of 15ms before the first character comes in, then 4 characters come in at full speed (about 1 per ms), then another 15ms pause, then the 2nd 4 at 1 per ms. That's a total of 38ms and the timeout wouldn't ever be activated.

    Try shortening the timeout and speeding up the PC end of the control loop. Additionally, you could shorten the number of characters needed by sending 16 bits as two 8-bit characters for each value. That would halve the number of characters needed.
  • DiablodeMorteDiablodeMorte Posts: 238
    edited 2007-10-10 13:57
    Mike Green said...
    Additionally, you could shorten the number of characters needed by sending 16 bits as two 8-bit characters for each value. That would halve the number of characters needed.
    Could you elaborate(maybe with some sample psuedo/real code) on what you mean by this? I don't really understand how to optimize code when it comes to variables and serial input.

    Thanks
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-10-10 16:10
    Things may work a little smoother if you add a short string to synchronize the data. Let me just re-arrange the code a little

    'Main·Program
    Main:
    ··DO
    ····noData·=·0
    ····'PULSIN·Pulse,·1,·pwmInput·'·measure·positive·pulse
    ····SERIN·16,·Baud,·20,time_out,·[noparse][[/noparse]WAIT("PWM"),DEC4·pwmInput1,·DEC4·pwmInput2]·'·receive·one·byte
    ··· time_out:
    ····GOSUB·goPwm

    ··LOOP
    goPwm:
    ·····'Attempt·to·handle·more·than·one·servo:
    ·····PULSOUT·6,·pwmInput1
    ·····PULSOUT·5,·pwmInput2
    RETURN

    Adjust your VB code to send like this.........SerialPort.WriteLine("PWM" & vbCR & pwminput1 & vbCR & pwminput2)
    If you still get a little jitter try a slower baud.

    Jeff T.
  • DiablodeMorteDiablodeMorte Posts: 238
    edited 2007-10-11 03:40
    I first want to thank unsoundcode and Mike Green for their very helpful replies. They helped alot.

    Sadly, I have a new interesting problem:
    The servo's work now w/o much jitter(the wait command helped, i hit my head on the table when i read your post unsoundcode, I already knew the wait comand but i forgot!) but the new problem is this:

    When all three servos are running now every so often they just stop working at all. I have to put my joystick back in the center position and then after about 4 seconds they start to work again. If i try to move too much on the joustick the servo's stop responding again. Any help?

    Here's an updated code snippit.
    goPwm:
         'Attempt to handle more than one servo:
         PULSOUT 6, pwmInput1*100
         'PAUSE 5
         PULSOUT 5, pwmInput2*100
         PULSOUT 7, pwmInput3*100
    
    
    GOTO Main
    
    'Main Program
    Main:
      DO
        SERIN 16, Baud, 50, goPwm, [noparse][[/noparse]WAIT("PWM"),DEC2 pwmInput1, DEC2 pwmInput2, DEC2 pwmInput3] ' receive one byte
        noData = 1
        GOTO goPwm
    
      LOOP
    



    and the VB code i'm using:
    MSComm1.Output = "PWM" & vbCr & Str(CurrentJoyR) & Str(CurrentJoyZ) & Str(CurrentJoyX)
    
  • Mike GreenMike Green Posts: 23,101
    edited 2007-10-11 04:03
    What is being transmitted by the PC when the servos stop responding?

    Can you hook up an LED to a spare pin (don't forget the current limiting resistors) to use to tell where the Stamp hangs up ... turn it on just before the SERIN and turn it off just after goPWM so it's on during the SERIN.
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-10-11 05:54
    I am not sure what values the joystick is giving you but looking at your Stamp code they have to be between 5 and 10. That means inputs 1 2 and 3 can be either 1 byte or 2 bytes. Your SERIN at the Stamp is configured for 2 bytes for each input (DEC2·pwmInput1) so for example if the joystick values were 6 7 8 respectively then input1 would recieve the 2 bytes 67 input2 would recieve the remaining byte 8 and input3 would·recieve nothing causing the SERIN· to keep timing out with the inputs containing bad data. You can overcome this by letting the Stamp know when each of the joystick axis has been transmitted and its time to read the next.
    Alter your VB code like this

    MSComm1.Output·=·"PWM"·&·vbCr·&·Str(CurrentJoyR)· &·vbCr & Str(CurrentJoyZ)·&·vbCr & Str(CurrentJoyX) & vbCr

    the carriage return will tell the Stamp to read into the next field.

    Jeff T.
  • DiablodeMorteDiablodeMorte Posts: 238
    edited 2007-10-11 13:26
    UNsoundcode: I'm at school currently so I can't test this... and I'm in the library so i can't bang my head on the table unless i want to bring on the wrath of the librarians but if I could I would. I can't belive I forgot about that......
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-10-11 15:24
    Actually I screwed up with the numbers, I see you have a BS2sx which means the values from the joystick·should in fact be integers somewhere between 12 and 25 so you should be sending a 2 byte string·every time. I would still keep the vbCr after each axis and perhaps put a temporary textbox in your VB application to verify your joystick strings are what you expect them to be.

    Jeff T.
  • DiablodeMorteDiablodeMorte Posts: 238
    edited 2007-10-11 16:46
    I think your point is still valid. I'll check what i'm sending when I get home.
Sign In or Register to comment.