|
Programming the ApplicationI have included three programs in this application to demonstrate how servo control is accomplished with and without the RSB509. Of course, the RSB509 serial buffer is useful in circuits beyond those that use RC servos. Any continuous or simultaneous control requirement needs to have a serial buffer of some sort if it is to receive asynchronous serial data. The demo programs are:
To use the programming code directly from this on-line application note, highlight the code you want to use. Press <ctrl-C> to copy it into the clipboard. Open up the stamp editor and position the cursor in the programming area. Press <ctrl-V> to paste the clipboard contents into the programming area. Repeat this process as necessary to get all elements of the program into the Stamp editor program. Then run the program with the appropriate stamp type selected. |
Program 1: No RSB509. User input required for every cycle. Use the programming cable plugged into the unbuffered plug.'{$STAMP BS2}
x var byte 'This is just a loop counter for positioning' inbyte var byte 'this is the value received from the serial port' highval var word ' this is the value used to position the server to its max'
around: serin 0,16468,[inbyte]
highval=1000 if inbyte<>"1" then next1 highval=625
next1: if inbyte<>"2" then next2 highval=750
next2: if inbyte<>"3" then next3 highval=875
next3: if inbyte<>"4" then next4 highval=1000
next4: for x=1 to 40 pulsout 8, 500 pause 9 next
for x=1 to 40 pulsout 8, highval pause 9 next
goto around The first program works well as long as you wait for the entire servo positioning pulse train to be sent before you enter your positioning command. This program is completely synchronous. The servo action is synchronized to the users input. In fact the servo does nothing until the user inputs a value. Often this situation is unacceptable. Program 2: No RSB509. User input attempted for any cycle. Use the programming cable plugged into the unbuffered plug.Lets take a case where a servo is agitating a container. In this situation you want the agitation to continue until it is stopped, but you would like to be able to change the severity of the agitation on command from your PC. Program 2 attempts this without a serial buffer. In program 2 you have a 5 millisecond window to enter your command before the next servo cycle begins. |
'{$STAMP BS2} ' The servo is acting as an agitator now, running continuously' ' The user has only 5ms window to input serial data' ' All data input must by synchronous'
x var byte 'This is just a loop counter for positioning' inbyte var byte 'this is the value received from the serial port' highval var word ' this is the value used to position the server to its max'
around: serin 0,16468,5, no_input, [inbyte]
no_input: highval=1000 if inbyte<>"0" then next0 highval=500
next0: if inbyte<>"1" then next1 highval=625
next1: if inbyte<>"2" then next2 highval=750
next2: if inbyte<>"3" then next3 highval=875
next3: if inbyte<>"4" then next4 highval=1000
next4: for x=1 to 40 pulsout 8, 500 pause 9 next
for x=1 to 40 pulsout 8, highval pause 9 next
goto around Program 2 works terribly. It is nearly impossible for a user to enter data at exactly the right point in time. In this program we require serial input to be synchronized with servo actions. A real world situation might offer other strategies for synchronizing input, but it is still a complication that is nice to avoid. |
Program 3: RSB509 used. User input allowed any time. Use the programming cable plugged into the buffered plug.In the third program, an RSB509 serial buffer is used to capture serial input and store it until the Stamp is ready to look at it. In this case, the Stamp is polling the RSB509 for data and gives the RSB509 only 1 millisecond to send data. '{$STAMP BS2} ' The servo is acting as an agitator now, running continuously' ' The user has only 5ms window to input serial data' ' All data input must by synchronous'
x var byte 'This is just a loop counter for positioning' inbyte var byte 'this is the value received from the serial port' highval var word ' this is the value used to position the server to its max'
' This first code just initializes the RSB509C-0' ' Signal Initialization' high 7 pause 2 input 7
' program the RSB509C-0 for 9600 buad inverted 8N1 format serout 7,49236, [%00000100]
' wait for recover from init (really not required on slow stamp) pause 1
around: ' signal RSB509 to send data if it has any high 7 serin 7,16468,1, no_input, [inbyte]
no_input: highval=1000 if inbyte<>"0" then next0 highval=500
next0: if inbyte<>"1" then next1 highval=625
next1: if inbyte<>"2" then next2 highval=750
next2: if inbyte<>"3" then next3 highval=875
next3: if inbyte<>"4" then next4 highval=1000
next4: for x=1 to 40 pulsout 8, 500 pause 9 next
for x=1 to 40 pulsout 8, highval pause 9 next
goto around |
Program 3 works perfectly. The user can stop the agitation servo by entering a "0". Likewise the user can enter a severity of agitation from 1 to 4 and the servo will continue the agitation until it is stopped. The user can also type several settings ahead which are buffered. Each setting is retrieved from the buffer after a servo cycle. Up to 32 cycles can be "programmed" ahead of time. The actual utility of this example is questionable in the real world, but the concept of asynchronous serial reception is required very often. The RSB509 provides that capability with ease. Application ConclusionThe RSB509 serial buffer gives single thread processors like the Basic Stamp a whole new bag of tricks. The Stamp can even perform complex time intensive functions and still have the ability to receive asynchronous serial input. The time required to receive the input is reduced because all communication with the RSB509 takes place at 9600 baud. It takes 1/4 of the processing time to receive 1200 baud signals through the RSB509 as compared to the Stamp receiving the signal directly. Also, no additional I/O is required beyond the single data pin. This opens up applications for stamp use with modems, NEMA devices like GPS receivers, Terminal node controllers, etc. |