The code for the servo-pod is very simple, and modifications to suit your specific needs should not be much of a problem. My motto is to keep it simple if at all possible, and always leave plenty of room for expansion.
A Look Under The Hood:
The first line include “modedefs.bas” simply includes the mode definitions file allowing us to use the pre-defined mode N2400 to specify the baud rate. This isn’t 100% necessary, but it makes our code much more readable. The alternate way to indicate the baud rate would be to simply indicate the mode by using the mode number. In this case N9600 would be mode 6. See the PBP manuals serin command page for detailed information on the different serial modes.
The ID number hard-coded into each servo-pod forces each PIC to respond only when it receives its assigned ID number. Once the correct ID number is received, position data is then accepted, and stored in the byte-variable pos.
The serial input line uses the built-in timeout and label capabilities of PicBasic Pro to allow program execution to continue if no serial data is received within a pre-set time frame. In this case, the timeout has been set to 15mS as shown below.
This forces program execution to wait for 15 milliseconds. If no serial data is received within 15mS, then goto label nxt. The nxt label holds a goto instruction that directs program flow back to the main label which resumes sending control pulses to the servo motor, and looking for more incoming serial data.
Using the timeout & label method we can use the timeout for setting up the time between each control pulse, and to make sure we aren’t locked in the serial receiving routine waiting for incoming serial data. We have pretty much killed two birds with one stone by using this method.
Wait For The Servo ID #:
The next thing we need is to force the servo-pod to respond only to positional data that is specifically meant for a certain address by using the data qualifier capabilities of PBP. The qualifier is the actual ID value we have assigned to each servo-pod as shown below.
ID var byte ‘ID number storage variable
ID = 5 ‘Assign unique ID to servo-pod
Line 1 assigns the byte-variable storage space to hold the physical value or number for the servo-pod ID. Line 2 writes the physical value of #5 into this RAM byte-variable storage location. The last part of our serial input line will now wait until the ID number 5 arrives before accepting servo positional data and placing it into the pre-defined RAM storage location pos.
Here’s a complete breakdown of how the serial input line functions.
serin serpin,N9600,15,nxt,[ID],pos ‘serial in on gpio.4
Get serial input on serpin (GPIO.4).
Receive this data at 9600 baud — inverted.
Wait 15mS for incoming data.
If no data is received within 15mS, jump to label nxt.
Wait for only 15mS to receive the ID.
ID received, store servo position data in byte-variable pos.
Return to “Main” and send the pulse to the servo.
PicBasic Pro allows us to create some very powerful command lines with the unique capability of waiting for specific data, and the timeout, jump to label features. This single line of code handles many various requirements that are necessary to make the servo-pod successful.
Visit Here for more.