Shop OBEX P1 Docs P2 Docs Learn Events
BS2 Serial to Standard Servo — Parallax Forums

BS2 Serial to Standard Servo

AsimAsim Posts: 5
edited 2011-03-08 19:17 in BASIC Stamp
Hello,

My name is Asim at University of Waterloo. Me and my friend are working on our final design project (only one more month until I graduate!).

The background is our device is a pill dispenser being controlled by a VB.net interface to a Basic Stamp 2 via the USB board to a Standard Servo.

Goal:
Our goal is to have the interface send a signal to the BS2 to drive the servo. For example if the interface sends a "1" it should rotate the servo 180 and back. Otherwise it should do nothing but wait for a "1".

Current state:
The Standard Servo is connected via Vdd, Vss ground, and uses Pin 15 for the control signal.

Based on our current code we are able to successfully send and read a "1" on the serial line which then drives the motor.

Problem:
There are two problems, which are likely jointly related.

We used a loop so that the BS2 will keep listening for the next "1" to arrive. Sometimes we send this "1" but it seems like the BS2 was already listening or active using up the port, and then we will get a "no port" message error catch from our VB code.

Furthermore to this, is whenever the BS2 serial data lights up,and the servo is still in its motion, it will stop out of place (not completing its full range).

The intervals where the BS2 serial data seems to activate we timed it to be about 16s apart, and active for something like 8s. This occurs even if the BS2 is powered down, leading me to believe it is due to the OS?

Whenever this occurs and we are trying to send our "1" or the servo is still moving that is when the problems occur.

I read the FAQ stating that there is a lot of timing associated with the serial transfer... can anyone point me in the right direction?

I appreciate all help and direction.

I have attached the code we are using in case that helps!

Basic Stamp 2 Code:
' {$STAMP BS2}
' {$PBASIC 2.5}

counter VAR     Word
command VAR     Byte


main:

SERIN 16, 16468,[STR command\1]

IF command = "0" THEN

elseIF command="1" THEN

  GOSUB Zero2180:
  PAUSE 500
  GOSUB One8020
  PAUSE 500

ENDIF

GOTO main

Zero2180:
  FOR counter = 190 TO 1180 STEP 8
    PULSOUT 15, counter
    PAUSE 20
  NEXT
RETURN

One8020:
FOR counter = 1180 TO 190 STEP 8
  PULSOUT 15, counter
  PAUSE 20
NEXT
RETURN

VB.Net Code (for serial transfer):
Dim port As SerialPort
  port = New SerialPort("COM7", 9600, Parity.None, 8, StopBits.One)
            port.Open()
            port.RtsEnable = True
            port.Write("1")
            port.Close()

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2011-03-07 20:09
    Your two loops have about 125 steps and each step takes about 21 to 22ms. That's about 2.5 seconds each. The Stamp cannot receive anything while it's executing Zero2180 or One8020. It only looks at the serial line when it's executing the SERIN. Anything sent another time is ignored.

    If the 5 second delay is ok and the only problem is that the Stamp misses the next command, have the Stamp send a request to the PC before doing the SERIN. Any character or sequence of characters will do. When the PC receives the request characters, it will immediately send the next command.
  • AsimAsim Posts: 5
    edited 2011-03-08 17:18
    A 5 second delay is no problem for us. Does the SERIN execute as per the flashing pattern we noticed? Or is that the OS doing something and stealing the port?

    So we should have some sort of:

    SEROUT *send signal to PC*
    PC always reading SEROUT, if interface is ready to send 1 and serout is recieved
    send "1"
    then SERIN will catch the "1" on time?
  • Mike GMike G Posts: 2,702
    edited 2011-03-08 17:36
    Send the PC a message when the STAMP is ready for the next command.

    A port error message can have something to do with your vb.net logic. Are you opening and closing the serial port? Are you using a windows form to display results from the serial port?
  • AsimAsim Posts: 5
    edited 2011-03-08 18:24
    Mike G wrote: »
    Send the PC a message when the STAMP is ready for the next command.

    A port error message can have something to do with your vb.net logic. Are you opening and closing the serial port? Are you using a windows form to display results from the serial port?

    Our port error message is just an error catch we made if the port is in use. Port ends up in use because of that periodic port usage which we don't know what that is. I am going to try looking at a port monitor to see whats causing that.

    Do i need SEROUT in the IF statement or inside the looping main function to send this message?
  • Mike GMike G Posts: 2,702
    edited 2011-03-08 18:36
    Our port error message is just an error catch we made if the port is in use. Port ends up in use because of that periodic port usage which we don't know what that is. I am going to try looking at a port monitor to see whats causing that.

    That's why I asked if you were use a windows form and are opening and closing the port. Which you did not answer. The SerialPort object in .NET and the win forms are operating on two different threads. If you're closing and opening the port and are trying to display data in a win form you can easily attempt to read from the Rx buffer on one thread while the main thread has already closed the port. This can also happen depending on object scope in your application.
  • AsimAsim Posts: 5
    edited 2011-03-08 18:53
    Mike G wrote: »
    That's why I asked if you were use a windows form and are opening and closing the port. Which you did not answer. The SerialPort object in .NET and the win forms are operating on two different threads. If you're closing and opening the port and are trying to display data in a win form you can easily attempt to read from the Rx buffer on one thread while the main thread has already closed the port. This can also happen depending on object scope in your application.

    Not sure what a windows form is,

    In our code we do open the port before we use it, then its closed. We had lines of code to close the port if it was already open for some reason, but that didn't seem to make much difference
  • Mike GMike G Posts: 2,702
    edited 2011-03-08 19:05
    Let's forget about the .NET stuff...
    Do i need SEROUT in the IF statement or inside the looping main function to send this message?

    Execute the SEROUT command when the STAMP is ready for the next command after the loops have finished. What is your major?
  • AsimAsim Posts: 5
    edited 2011-03-08 19:17
    We are both in Systems Design Engineering,
    Thanks
Sign In or Register to comment.