 |
 |
|
 |
|
|
|
|
|
|
|
|
|
|
Programming Stepper-Bee in Visual Basic |
|
|
|
|
|
|
|
|
|
|
|
| |
Although Stepper-Bee comes with it's
own software (AutoStep) to allow the beginner to start using it in home
automation projects very quickly, it also comes with a DLL interface to
allow the intermediate and advanced user to write their own programs for
it. The DLL provides a general purpose interface that greatly simplifies
the task of writing programs for a USB device. It can be tricky
manipulating the USB comms into sending and receiving messages to and from
a device which can easily be plugged and unplugged at any time. The DLL
eliminates all of these headaches by simplifying the task into a few
library functions.
InitStp()
RunMotor1(steps, interval, direction, outputs)
RunMotor2(steps, interval, direction, outputs)
StopMotor1(outputs)
StopMotor2(outputs)
GetCurrentStatus(M1Active, M2Active, M1Steps, M2Steps, Inputs)
InitStp() Function Before
using any of the motor control functions you must first initialise the
StepperBee using the initialise function as follows…
InitStp()
There are no parameters required for this
function call. Once initialised you can then use the functions, listed
below, anywhere in your program.
RunMotor1() Function The
RunMotor1() function has 4 parameters and will cause Motor1 to run
according to your specified parameters. All of the following also
applies to RunMotor2().
All four parameters are type integer and would be declared
somewhere in your program as follows;
Dim steps, interval, direction, outputs As Integer
The corresponding function call would be
RunMotor1(steps, interval, direction, outputs)
The parameters are as follows..
steps - integer in the range 1 to 16000
corresponding to the number of steps to execute
interval – integer in the range1 to 16000
corresponding to the time interval in milli-seconds between each step.
direction – integer in the range 0 to 1. Zero
corresponds to forward and 1 to reverse.
outputs – integer in the range 0 to 7
corresponding to the bit pattern for on/off of the additional switching
outputs associated with motor 1. e.g. a value of 5 (which is 00000101 in
binary) would result in outputs 1 and 3 being on.
Example
To run Motor1 forward for 200 steps with 50ms between steps and all
additional switching outputs off would use the following function call…
RunMotor1(200, 50, 0, 0)
StopMotor1() Function
The following applies to both StopMotor1() and
StopMotor2() functions.
The StopMotor1() function has one integer parameter and simply terminates
the current task being performed by motor1 immediately while updating the
current state of the other switching outputs.
The parameter is of type integer and would be declared
somewhere in your program as follows:
Dim outputs As Integer
The corresponding function call would be
StopMotor1(outputs)
outputs – integer in the range 0 to 7 corresponding to the bit
pattern of on/off of the additional switching outputs associated with
motor1
Example
To stop motor 1 and set the switching outputs 1 and 2 to ON, use the
following function call
StopMotor1(3)
GetCurrentStatus() function
This function may be called at any time to determine the running status
of both motors and the status of the digital inputs. This function has 5
integer parameters passed by reference. The parameters would be declared
in your program somewhere as..
Dim M1Active, M2Active, M1Steps, M2Steps, Inputs As
Integer
The corresponding function call would be
GetCurrentStatus(M1Active, M2Active, M1Steps,
M2Steps, Inputs)
After calling this function the values returned in the parameters
correspond to the following…
M1Active – set to 1 for motor1 active, 0 for
motor1 stopped.
M2Active – set to 1 for motor2 active, 0 for
motor2 stopped.
M1Steps – integer in the range 0 – 16000
corresponding to the number of steps motor1 has left to complete.
M2Steps – integer in the range 0 – 16000
corresponding to the number of steps motor2 has left to complete.
Inputs – integer in the range 0 – 31
corresponding to the bit pattern of the current digital inputs. (bit 0
corresponds to input 1, bit 1 to input 2, etc…)
Example
If motor 1 is running with 123 steps left to complete, motor 2 is stopped
and only digital input 4 is on then the following call…
GetCurrentStatus(M1Active, M2Active, M1Steps,
M2Steps, Inputs)
… will results in the following values returned …
M1Active = 1, M2Active = 0, M1Steps = 123, M2Steps =
0, Inputs = 8
The only other thing that a VB program must do is to declare
the functions that it is going to use within the DLL and the name of the
DLL itself. This must be done at the start of your program or at least
before any references to the three functions are made. The following is a
program excerpt showing how this is done...
Declare Function InitStp Lib "stp.dll" () As Integer
Declare Function RunMotor1 Lib "stp.dll" ( ByVal steps As Integer,
ByVal interval As Integer,
ByVal direction As Integer,
ByVal outputs As Integer,
) As Boolean
Declare Function RunMotor2 Lib "stp.dll" ( ByVal steps As Integer,
ByVal interval As Integer,
ByVal direction As Integer,
ByVal outputs As Integer,
) As Boolean
Declare Function StopMotor1 Lib "stp.dll" ( ByVal outputs As Integer, ) As
Boolean
Declare Function StopMotor2 Lib "stp.dll" ( ByVal outputs As Integer, ) As
Boolean
Declare Function SetStepMode Lib "stp.dll" ( ByVal M1Mode As Integer,
ByVal M2Mode As Integer,
) As Boolean
Declare Function GetStatus Lib "stp.dll" ( ByRef M1Active As Integer,
ByRef M2Active As Integer,
ByRef M1Steps As Integer,
ByRef M2Steps As Integer,
ByRef Inputs As Integer,
) As Boolean
Normally each of the parameters of the functions would be on the same
line in your program. They are shown here on separate lines for clarity.
These provide sufficient information for your compiler to determine the
correct way to use the functions contained within the DLL. You should also
ensure that you copy the stp.dll file from the installation disk to your
c:\windows\system32 directory so that your compiler can find it.
It should be noted that the
Lib "stp.dll"
lets the program know where to find the stp.dll file. When written like
this it assumes, since there is no path information, that the stp.dll file
can be found in the windows system directory ( c:\windows\system32 ) If
you like you can copy the file stp.dll on the installation disk to the
system32 directory and the above statement will work perfectly.
Alternatively you can copy the file to some other location and give that
location in the declaration as in the example below...
Declare Function InitStp Lib "c:\mylibrary\stp.dll"
() As Boolean |
|
| |
|
|
|
|
| |
|
|
|
|
|
|
|
|
|
| |
Source Code
To speed up your development of
software for the Stepper-Bee a complete working example is available for
download below. It is called VBstepper and creates a very simple form
based program that has individual buttons for various functions such as
initialising the Stepper-Bee, setting motor steps, intervals, directions,
as well as setting the switching outputs and displaying the digital
inputs. The main (and only) screen is shown on the right. This has
been written using Microsoft Visual Studio .net and the download files
contain the full workspace (solution) details to allow you to
immediately open and start editing or running this application.
Even if you don't have Visual Studio, the source code is virtually
self explanatory with the most relevant sections being in "Form1.vb" which
can even be opened in a simple text editor such as notepad. The files are
zipped for convenience and may be downloaded by right clicking on the link
below and choosing "save target as" to begin the download. |
|
| |
|
|
|
|
|
|
|
|
|
| |
|
|
Download Files |
|
|
|
| |
|
|
|
|
|
|
|
|
|
| |
|
|
|
|
|
|
|
|
|
| |
.
. |
|
| |
©
Copyright pc-control.co.uk 2008 |
|
| |
|
|