Shop OBEX P1 Docs P2 Docs Learn Events
BS2 and Servo Question — Parallax Forums

BS2 and Servo Question

ArchiverArchiver Posts: 46,084
edited 2002-07-22 20:37 in General Discussion
I am trying to controll servos from a VB application, I can't seem to
get it figured out. Here is some snips, anyone have any ideas?

the Stamp Code
'{$STAMP BS2}
pinnumber var byte
servopos var byte
main:
Serin 16,16780,[noparse][[/noparse]WAIT(255),pinnumber,servopos]

pulsout pinnumber, (750 + servopos)

pause 15
goto main


The VB code
Private Sub cmdSend_Click()
Dim PinNumber As Long
Dim servopos As Long
servopos = Val(txtvalue.Text)

' Get Pin Number
PinNumber = cboPinNumber.ListIndex

' Send Out Data
MSComm1.Output = Chr$(255) & Chr$(PinNumber) & Chr$(servopos)
End Sub


servopos comes from a slider controll set at min 25 and max 254, not
sure if these are right but I have tried many values and nothing
works. Anyone have any ideas????

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2002-07-22 14:00
    Part of the problem is that to move the servo through its full swing, you
    need to send values between 500 and 1000 with PULSOUT. That means the value
    that is passed to your servo routine needs to be between -250 and +250 ... a
    byte variable can't hold anything but positive values (0 - 255).

    To keep things simple, I would have your VB slider send a value between 0 and
    500; 250 being the center point. Your servo update routine would change to:

    PULSOUT pinNumber, (500 + servoPos)

    Then, your servoPos value needs to be a Word (two bytes), so you'll have to
    modify your SERIN line.

    Or ... you could keep the servoPos variable a Byte and pass a servo position
    value between 0 and 250 (125 is the center now). Then change your servo
    update code to this:

    PULSOUT pinNumber, (500 + (servoPos * 2))


    I hope this helps.

    -- Jon Williams
    -- Applications Engineer, Parallax

    In a message dated 7/22/02 1:38:07 AM Central Daylight Time,
    joebiederman@h... writes:


    > I am trying to controll servos from a VB application, I can't seem to
    > get it figured out. Here is some snips, anyone have any ideas?
    >
    > the Stamp Code
    > '{$STAMP BS2}
    > pinnumber var byte
    > servopos var byte
    > main:
    > Serin 16,16780,[noparse][[/noparse]WAIT(255),pinnumber,servopos]
    >
    > pulsout pinnumber, (750 + servopos)
    >
    > pause 15
    > goto main
    >
    >
    > The VB code
    > Private Sub cmdSend_Click()
    > Dim PinNumber As Long
    > Dim servopos As Long
    > servopos = Val(txtvalue.Text)
    >
    > ' Get Pin Number
    > PinNumber = cboPinNumber.ListIndex
    >
    > ' Send Out Data
    > MSComm1.Output = Chr$(255) & Chr$(PinNumber) & Chr$(servopos)
    > End Sub
    >
    >
    > servopos comes from a slider controll set at min 25 and max 254, not
    > sure if these are right but I have tried many values and nothing
    >




    [noparse][[/noparse]Non-text portions of this message have been removed]
  • ArchiverArchiver Posts: 46,084
    edited 2002-07-22 17:22
    Thats helps tremendously, thanks a million!

    --- In basicstamps@y..., jonwms@a... wrote:
    > Part of the problem is that to move the servo through its full
    swing, you
    > need to send values between 500 and 1000 with PULSOUT. That means
    the value
    > that is passed to your servo routine needs to be between -250 and
    +250 ... a
    > byte variable can't hold anything but positive values (0 - 255).
    >
    > To keep things simple, I would have your VB slider send a value
    between 0 and
    > 500; 250 being the center point. Your servo update routine would
    change to:
    >
    > PULSOUT pinNumber, (500 + servoPos)
    >
    > Then, your servoPos value needs to be a Word (two bytes), so you'll
    have to
    > modify your SERIN line.
    >
    > Or ... you could keep the servoPos variable a Byte and pass a servo
    position
    > value between 0 and 250 (125 is the center now). Then change your
    servo
    > update code to this:
    >
    > PULSOUT pinNumber, (500 + (servoPos * 2))
    >
    >
    > I hope this helps.
    >
    > -- Jon Williams
    > -- Applications Engineer, Parallax
    >
    > In a message dated 7/22/02 1:38:07 AM Central Daylight Time,
    > joebiederman@h... writes:
    >
    >
    > > I am trying to controll servos from a VB application, I can't
    seem to
    > > get it figured out. Here is some snips, anyone have any ideas?
    > >
    > > the Stamp Code
    > > '{$STAMP BS2}
    > > pinnumber var byte
    > > servopos var byte
    > > main:
    > > Serin 16,16780,[noparse][[/noparse]WAIT(255),pinnumber,servopos]
    > >
    > > pulsout pinnumber, (750 + servopos)
    > >
    > > pause 15
    > > goto main
    > >
    > >
    > > The VB code
    > > Private Sub cmdSend_Click()
    > > Dim PinNumber As Long
    > > Dim servopos As Long
    > > servopos = Val(txtvalue.Text)
    > >
    > > ' Get Pin Number
    > > PinNumber = cboPinNumber.ListIndex
    > >
    > > ' Send Out Data
    > > MSComm1.Output = Chr$(255) & Chr$(PinNumber) & Chr$(servopos)
    > > End Sub
    > >
    > >
    > > servopos comes from a slider controll set at min 25 and max 254,
    not
    > > sure if these are right but I have tried many values and nothing
    > >
    >
    >
    >
    >
    > [noparse][[/noparse]Non-text portions of this message have been removed]
  • ArchiverArchiver Posts: 46,084
    edited 2002-07-22 18:23
    Seems to be working ok, I posted the code below. It won't let me
    update the servo position very fast. Anyone have any ideas?

    Stamp code
    '{$STAMP BS2}
    pinnumber var byte
    servopos var byte
    cont var byte
    b var byte
    main:
    Serin 16,16780,[noparse][[/noparse]WAIT(255),pinnumber,servopos]
    for b = 1 to 50
    pulsout pinnumber, (500 + (servopos * 2))
    pause 10
    next
    goto main


    VB Code

    Private Sub cmdcenter_Click()
    sldpos.Value = 125
    End Sub

    Private Sub Form_Load()
    Dim Pins As Long
    ' Add the pin numbers 0 to 15 to cboPinNumber
    For Pins = 0 To 15
    cboPinNumber.AddItem CStr(Pins)
    Next Pins

    ' Default to Pin 0 being selected
    cboPinNumber.ListIndex = 0

    ' Default to optState(0) being selected


    ' Use COM1
    MSComm1.CommPort = 1

    ' 2400 baud, no parity, 8 data bits, 1 stop bit
    MSComm1.Settings = "2400,N,8,1"

    ' Open the port
    MSComm1.PortOpen = True

    End Sub
    Private Sub cmdSend_Click()
    Dim PinNumber As Long
    Dim servopos As Long
    servopos = sldpos.Value


    ' Get Pin Number
    PinNumber = cboPinNumber.ListIndex

    ' Send Out Data
    MSComm1.Output = Chr$(255) & Chr$(PinNumber) & Chr$(servopos)

    End Sub
    Private Sub Form_Unload(Cancel As Integer)
    MSComm1.PortOpen = False
    End Sub

    Private Sub sldpos_change()
    cmdSend_Click
    End Sub






    --- In basicstamps@y..., "quasigeek2002" <joebiederman@h...> wrote:
    > Thats helps tremendously, thanks a million!
    >
    > --- In basicstamps@y..., jonwms@a... wrote:
    > > Part of the problem is that to move the servo through its full
    > swing, you
    > > need to send values between 500 and 1000 with PULSOUT. That
    means
    > the value
    > > that is passed to your servo routine needs to be between -250 and
    > +250 ... a
    > > byte variable can't hold anything but positive values (0 - 255).
    > >
    > > To keep things simple, I would have your VB slider send a value
    > between 0 and
    > > 500; 250 being the center point. Your servo update routine would
    > change to:
    > >
    > > PULSOUT pinNumber, (500 + servoPos)
    > >
    > > Then, your servoPos value needs to be a Word (two bytes), so
    you'll
    > have to
    > > modify your SERIN line.
    > >
    > > Or ... you could keep the servoPos variable a Byte and pass a
    servo
    > position
    > > value between 0 and 250 (125 is the center now). Then change
    your
    > servo
    > > update code to this:
    > >
    > > PULSOUT pinNumber, (500 + (servoPos * 2))
    > >
    > >
    > > I hope this helps.
    > >
    > > -- Jon Williams
    > > -- Applications Engineer, Parallax
    > >
    > > In a message dated 7/22/02 1:38:07 AM Central Daylight Time,
    > > joebiederman@h... writes:
    > >
    > >
    > > > I am trying to controll servos from a VB application, I can't
    > seem to
    > > > get it figured out. Here is some snips, anyone have any ideas?
    > > >
    > > > the Stamp Code
    > > > '{$STAMP BS2}
    > > > pinnumber var byte
    > > > servopos var byte
    > > > main:
    > > > Serin 16,16780,[noparse][[/noparse]WAIT(255),pinnumber,servopos]
    > > >
    > > > pulsout pinnumber, (750 + servopos)
    > > >
    > > > pause 15
    > > > goto main
    > > >
    > > >
    > > > The VB code
    > > > Private Sub cmdSend_Click()
    > > > Dim PinNumber As Long
    > > > Dim servopos As Long
    > > > servopos = Val(txtvalue.Text)
    > > >
    > > > ' Get Pin Number
    > > > PinNumber = cboPinNumber.ListIndex
    > > >
    > > > ' Send Out Data
    > > > MSComm1.Output = Chr$(255) & Chr$(PinNumber) &
    Chr$(servopos)
    > > > End Sub
    > > >
    > > >
    > > > servopos comes from a slider controll set at min 25 and max
    254,
    > not
    > > > sure if these are right but I have tried many values and
    nothing
    > > >
    > >
    > >
    > >
    > >
    > > [noparse][[/noparse]Non-text portions of this message have been removed]
  • ArchiverArchiver Posts: 46,084
    edited 2002-07-22 20:37
    You're waiting on the PC to send something. You could add a timeout to the
    SERIN so that the servos get updated if nothing new is sent from the PC. You
    could also use a timer on the PC side to continuously send the servo values
    (in case the Stamp is doing something else and misses and update from the
    PC). Lastly, you could use a "full" serial port with flow control.

    I'll be working on that last part (pc connection with flow control) myself in
    the next couple of weeks for an upcoming Nuts & Volts article.

    -- Jon Williams
    -- Applications Engineer, Parallax

    In a message dated 7/22/02 12:24:50 PM Central Daylight Time,
    joebiederman@h... writes:


    > Seems to be working ok, I posted the code below. It won't let me
    >




    [noparse][[/noparse]Non-text portions of this message have been removed]
Sign In or Register to comment.