               QBasic Sample Code for the PIC-SERVO and PIC-IO

PSTEST.BAS is a collection of QBasic subroutines and functions which give 
you a simplified interface to the PIC-SERVO motor controller, the PIC-IO
multifunction I/O module, and in the future, other devices compatible with 
the PIC-SERVO communications interface (NMC communications protocol).  The 
main program in PSTEST.BAS is a short sample of initializing a network of 
motor controllers, setting motor control gains, and then performing a few 
short motions.  You can use this as a template for writing your own QBasic 
motor control programs.  You can also make calls to the  motor control 
functions and subroutines directly from the immediate execution window for 
experimenting with the various functions.  Look over the list of subroutines 
and functions to see what is available.  Descriptions preceeding the source 
code for each routine give you the details of how each is used.

LIMITATIONS

The routines in PSTEST.BAS are not complete in exercising the full 
potential of the PIC-SERVO, but they give you a good starting point for 
simple motion control tasks.  These routines can be easily modified to 
make your own subroutines for your particular needs.  The basic structure 
PSTEST.BAS, however, has a few fundamental limitiations which were made 
in order to make writing new subroutines easier:

1. The BAUD rate of the network of controllers is fixed at 19,200.  (This 
is the default power-up baud rate of the PIC-SERVO.)

2. All controllers are assigned addresses sequentially, starting with 
address 1 for the controller furthest from the host.  Group addresses are 
all set at 255.

3. Group commands are not supported.  This means that commands to different 
controllers must be sent sequentially.  

4. Each controller is set up to send back all possible status data.  This 
is less efficient, but it eliminates the bookkeeping of which controller 
is sending back what data with each command.

It is possible to modify PSTEST.BAS to expand beyond these limitations, 
but you will have to understand the operation of the PIC_SERVO 
communications protocol more completely.  Please refer to the PIC-SERVO 
datasheet and PSTEST.C for more information.

BASIC OPERATION

When using the routines in PSTEST.BAS, the subroutine NmcInit should be 
called first.  It opens up the serial port for communication, initializes 
the addresses of each control module found on the network, determines 
type of each controller found (other types of controllers besides the 
PIC-SERVO will be available soon), and then sets each controller to send 
back its complete set of status data after each command is issued.

The module type is stored stored in the global array ModuleType(addr), 
indexed by the module's address.  The current values of ModuleType 
supported are 0=PIC-SERVO controller and 2=PIC-IO controller (soon to 
be released).  You should make sure that module types listed on 
initialization correspond the chain of modules you have plugged in.

Whenever a command is executed, such as MoveTo or SetGain, the 
controller receiving the command will send back a packet of status data 
which is stored as a string in the array of strings ModuleData(addr), 
indexed by the controller's address.  Functions such as GetPosition 
or GetVelocity extract the pertinent data from the stored status string.  
Note that the data returned by these functions is not necessarily current -
 it only reflects the status from when the last command was issued.  To 
get current status data, simply call the routine NoOp(addr) to get the 
current status data string and then call GetPosition, etc, to extract the 
needed data.

Part of the status data returned is the status byte and aux. status byte, 
who's bits represent the current operating state.  Certain functions such 
as MoveDone or PowerOn will internally call NoOp and then examine the 
appropriate bit in the status bytes and return a boolean value.

After calling NmcInit but before moving a motor, you must first set its 
gain with SetGain and then enable the servo with EnableServo.  The gain 
values of kp=100, kd=1000, ki=0, el=2000 are typically a good starting 
place form most small motors.  Please refer to the PIC-SERVO datasheet 
for details on optimizing gain settings.  The EnableServo command enables 
the amplifier and then starts the motor servoing to its current position.

After the above intialization, can issue MoveTo commands to each 
initialized motor.  Note that the unit for position, velocity and 
acceleration are in encoder counts, counts/servo tick and counts/tick/tick.  
Also note that velocity and acceleration are multiplied by 65384.  A 
servo tick is equal to 512 microseconds.  For example, a velocity of 
100,000 is equal to a speed of 2980.23 counts/second.  Typical velocity 
and acceleration values are v=100000 and a=100.

Also note that if you specify a velocity or acceleration of 0, the motor 
will never get to its goal.  In particular, you must have set a non-zero 
acceleration value before using the "stop smoothly" command, or else the 
motor will never decelerate to a stop.  

