Shop OBEX P1 Docs P2 Docs Learn Events
xtreme serial control code for servo? — Parallax Forums

xtreme serial control code for servo?

tmacktmack Posts: 8
edited 2006-08-22 18:24 in BASIC Stamp
' {$STAMP BS2}
' {$PBASIC 2.5}
' Using the BSII with X-Treme Serial Control
' Visual Basic Software to Control 4 Relays.

relay· VAR B3··· 'relay number storage variable
stat·· VAR B4··· 'relay status ON/OFF variable
baud·· CON 16468 '9600,N,8,1
serpin CON 0···· 'serial I/O pin = 0
DO
· SERIN serpin,baud,[noparse][[/noparse]WAIT(254),relay,stat] 'serial data in on pin0
· IF relay = 1 THEN outr1 ' if request is for relay#1 then goto relay#1 routine
LOOP
outr1:
· IF stat = 1 THEN
·DO
·PULSOUT 15, 500
·LOOP
ENDIF
· IF stat = 0 THEN
· DEBUG " pin low"
ENDIF
RETURN

Can anyone explain to me how to keep my servo on in this code while outr1 status is 1 and turn it off when the status is 0?? ( I am hacking a serial control relay project to control servos for a robot)
Thanks for any help.

Comments

  • stamptrolstamptrol Posts: 1,731
    edited 2006-08-16 16:51
    The servos need constant refreshing to stay on position. While your SERIN is waiting for the next update to come along, the servo can forget what you told it to do.

    You may need to use a servo control board which can provide the updating while the Stamp does other things. There are a couple on the Parallax pages.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tom Sisk

    http://www.siskconsult.com
    ·
  • Beau SchwabeBeau Schwabe Posts: 6,559
    edited 2006-08-16 17:09
    You are going to at least need a pause in here somewhere between 15mS and 25mS

     DO
     PULSOUT 15, 500
     [color=red]PAUSE 20[/color]
     LOOP
    
    

    ·

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • tmacktmack Posts: 8
    edited 2006-08-17 12:09
    ·I forgot to mention that the servos are continuous rotation.
  • tmacktmack Posts: 8
    edited 2006-08-17 13:26
    I have an idea on how to do it in a round about way for now. All I need is to insert a "pulsout 15,500, pause 20 into the program so that it will continue to run as it did before (without the previous pulsout) but it also just pulses pin 15 indefinately. I cant figure out where to put the code in . It seems wherever I put it in, it either pulses the pin or controls the relays not both.Any help? Thanks
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-08-17 13:58
    tmack -
    The answer to your question is simple, but there are MANY problems that you haven't even seen yet in this program. Just to answer your question first, here's what you need to do. Where you have:

    DO
    PULSOUT 15, 500
    LOOP

    You need to add the PAUSE in there so it looks like this:

    DO
    PULSOUT 15, 500
    PAUSE 20
    LOOP

    HOWEVER, if you use DO...LOOP you need to have a conditional within the DO..LOOP, or a conditional argument in the DO part, or LOOP part, in order to exit, otherwise it will continue forever.

    Moreover the RETURN at the end of that program is eventuall ygoing to cause you all sorts of grief, since there is no GOSUB anywhere in the program. GOSUB...RETURN always act in pairs.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <!--StartFragment -->
  • TechnoRobboTechnoRobbo Posts: 323
    edited 2006-08-21 13:32
    Save yourself some headaches and buy the Parallax Servo Controller.
    http://www.parallax.com/detail.asp?product_id=28023

    Why waste code an clock cycles on spinning motors. Save your code space for the important stuff.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Have Fun


    TR
  • tmacktmack Posts: 8
    edited 2006-08-22 11:53
    ·Its Really about the learning how and the doing. I very well may get the PSC next·. Once I get started on a project though I dont like to let it go until I get it to do what I want. This is what I have so far:

    '{$STAMP BS2}
    '{$PBASIC 2.5}
    ' Using the BSII with X-Treme Serial Control
    ' Visual Basic Software to Control 4 Relays.




    relay· VAR B3··· 'relay number storage variable
    stat·· VAR B4··· 'relay status ON/OFF variable
    baud·· CON 16468 '9600,N,8,1
    serpin CON 0···· 'serial I/O pin = 0

    key:

    · SERIN serpin,baud,[noparse][[/noparse]WAIT(254),relay,stat] 'serial data in on pin0
    · IF relay = 1 THEN outr1
    · IF relay = 2 THEN outr2
    · IF relay = 3 THEN outr3
    · IF relay = 4 THEN outr4








    outr1:
    ·DEBUG ?IN5

    · IF stat = 1 THEN GOTO work ' If status request is I/O pin#1 logic 1 [noparse][[/noparse]high]
    · LOW 1: GOTO key······ ' then make I/O pin#1 high, else make it [noparse][[/noparse]low]

    high1:

    HIGH 1: GOTO key

    outr2:
    DEBUG ? IN5

    · IF stat = 1 THEN high2
    · LOW 2: GOTO key

    high2:
    DEBUG ? IN5
    · HIGH 2: GOTO key

    outr3:
    · IF stat = 1 THEN high3
    · LOW 3: GOTO key

    high3:
    · HIGH 3: GOTO key

    outr4:
    · IF stat = 1 THEN high4
    · LOW 4: GOTO key

    high4:
    · HIGH 4: GOTO key

    work:


    IF (IN5 = 0) THEN
    DO
    PULSOUT 15,500
    PAUSE 20
    IF (IN5)=1 THEN
    GOTO key
    ·ENDIF
    LOOP UNTIL IN5=1
    ENDIF

    It is sort of working but I cant get it to interupt when the servo is pulsing to check the condition of IN5 so that when in5 is changed to 1 the servo stops.
  • TechnoRobboTechnoRobbo Posts: 323
    edited 2006-08-22 18:24
    'Let's Have a Look

    '{$STAMP BS2}
    '{$PBASIC 2.5}
    ' Using the BSII with X-Treme Serial Control
    ' Visual Basic Software to Control 4 Relays.

    relay· VAR B3··· 'relay number storage variable
    stat·· VAR B4··· 'relay status ON/OFF variable
    baud·· CON 16468 '9600,N,8,1
    serpin CON 0···· 'serial I/O pin = 0

    key:

    · SERIN serpin,baud,[noparse][[/noparse]WAIT(254),relay,stat] 'serial data in on pin0
    · IF relay = 1 THEN outr1
    · IF relay = 2 THEN outr2
    · IF relay = 3 THEN outr3
    · IF relay = 4 THEN outr4

    'ON relay GOSUB... may work better here - easier to follow.·


    outr1:
    ·DEBUG ?IN5

    · IF stat = 1 THEN GOTO work ' If status request is I/O pin#1 logic 1 [noparse][[/noparse]high]
    · LOW 1: GOTO key······ ' then make I/O pin#1 high, else make it [noparse][[/noparse]low]

    'Code doesn't do what comment says - when does it make pin 1 high? goto prohibits it.

    high1: 'This label is completely unreachable in the code

    HIGH 1: GOTO key

    outr2:

    'This works - maybe substituting an IF THEN ELSE statement can help you with program flow.
    DEBUG ? IN5



    · IF stat = 1 THEN high2
    · LOW 2: GOTO key

    high2:
    DEBUG ? IN5
    · HIGH 2: GOTO key

    outr3:
    'This works - maybe substituting an IF THEN ELSE statement can help you with program flow.

    · IF stat = 1 THEN high3
    · LOW 3: GOTO key

    high3:
    · HIGH 3: GOTO key

    outr4:

    'This works - maybe substituting an IF THEN ELSE statement can help you with program flow.
    · IF stat = 1 THEN high4
    · LOW 4: GOTO key

    high4:
    · HIGH 4: GOTO key

    work:

    ' a DO WHILE (IN5 = 0)...· may work better for flow control, clarity and memory usage


    IF (IN5 = 0) THEN
    DO
    PULSOUT 15,500
    PAUSE 20
    IF (IN5)=1 THEN
    GOTO key
    ·ENDIF
    LOOP UNTIL IN5=1
    ENDIF

    'program terminates here was that your intention? try adding an·END to save battery.





    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Have Fun


    TR
Sign In or Register to comment.