BS2 Serial to Standard Servo
Asim
Posts: 5
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:
VB.Net Code (for serial transfer):
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
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.
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?
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?
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
Execute the SEROUT command when the STAMP is ready for the next command after the loops have finished. What is your major?
Thanks