VBA Example using Microsoft® Excel

Sample project for developing code using VBA to programming to control motion direction and velocity using an Excel spreadheet (*.xls in a *zip file).

Embedded Project using Microsoft® Excel

Sample project for developing code using Visual Basic programming language to communicate with MDrive  motion control products.

Embedded VBA Project
Version: 1.0.0
Download 0    297

Os – Output setup

The output setup (Os) command is used to set the functionality of the outputs. The parameters define the output point to be configured, the desired functionality, and the active control state high or low. When applied to outputs 1 – 2 the output setup command is utilized thus:

Os=<output #>, <function>, <active>

Example:

Output functions

All digital input parameters are listed in the table below.

Outputs
Output Notes
1 May be programmed to any function except trip and Encoder Channel B. Not available in NEMA 17 Sizes
2 May be programmed to any function except trip and Encoder Channel A. Not available in NEMA 17 Sizes
3 May be programmed to any function except as an Encoder output. External circuitry may be required if using to drive inductive loads.
Functions
Function Description
16 USER: General purpose user output. (Default)
17 MOVING: output will be active while the axis is in motion.
18 ERROR: will be active when a software error occurs.
19 STALL: Indicates a stall has been detected [Closed loop models only]
20 VCHANGE: velocity changing indicates the axis is accelerating or decelerating.
21 LOCKED ROTOR: indicates the rotor is in a locked state [Closed loop models only].
23 MOVINGPOS: indicates the axis is moving to a specified position.
24 HMT ACTIVE: indicates when HMT is active [Closed loop models only].
25 MAKE UP ACTIVE: indicates when HMT is making up steps [Closed loop models only].
26 ENC A: Encoder channel A output applies to output 1 only. Os=1,26,<0/1> [Closed loop models only].
27 ENC B: Encoder channel B output applies to output 2 only. Os=2,27,<0/1> [Closed loop models only].
28 TRIP OUT, applies to output 3 only, active low only. Os=3,28,0
29 ATTENTION indicates a status or statuses as configured by the AO variable.
Active
State Description
0 Active when LOW (default)
1 Active when HIGH

General output usage commands

An I/O instruction results in the change of parameters or the state of an input or output. The syntax for these commands are as follows: type the command then an equal sign, then the data. Example:

Mnemonic Function Usage example Description
Individual I/O point control commands
O<output> Set the logic state of an individual output O3=1 Set output 3 HIGH (1)
Binary coded decimal (BCD) commands
OT=<0-7> Set the logic state of all outputs OT=7 Set outputs 1 – 3 HIGH
Note On NEMA17 models this command in not applicable

Is – Input setup command

The input setup (Is) command is used to set the functionality of all inputs, digital and analog. The parameters define the input point to be configured, the desired functionality, and the active response state high or low.

Digital inputs

Usage

When applied to the digital inputs 1 – 4 the input setup command is utilized thus:

Is=<input #>, <function>, <active>

Example:

Digital input functions

All digital input parameters are listed in the table below.

Inputs
Input Notes
1 May be programmed to any function, not available in NEMA 17 sizes
2 Maybe programmed to any function except capture
3 Maybe programmed to any function except capture
4 Maybe programmed to any function except capture
Functions
Function Description
0 General purpose input (default)
1 Homing function will function as specified by the homing command (HM).
2 Limit + will function as specified by the limit command (LM).
3 Limit —, will function as specified by the limit command (LM).
4 G0 input, will run program at address 1 upon activation.
5 Soft stop, stops motion with deceleration and halts program execution. If program is paused (PS), input is ignored.
6 Pause, pause/resume program with motion
7 Jog +, Will Jog motor in the positive direction at Max. Velocity (VM). The Jog Enable (JE) Flag must be set for this to function.
8 Jog —, Will Jog motor in thenegative direction at max. velocity (VM). The jog enable (JE) flag must be set for this to function.
11 Reset, When set as RESET input, then the action is equivalent to a ^C entered into a terminal. Note: If setting the input to sourcing, active true, ground the input first or a reset will occur.
12 Capture input witll operate with the Trip Capture (TC) trip to run a subroutine when active NOTE: THIS FUNCTION APPLIES TO INPUT 1 ONLY!
Active
State Description
0 Active when LOW
1 Active when HIGH (default)

Analog Input

Usage

When used to setup the analog input, the Is command is used in this manner:

Is=5, <mode>, <level>

Example:

Analog input details

Modes
Description
9 Voltage mode (default)
10 Current mode
Levels
Description
0 0 to +5V (voltage mode), 0 to 20 mA (current mode) (default)
1 0 to 10V (voltage mode), 4 to 20 mA (current mode)

Encoder inputs

Usage and details

The encoder index mark only has one settable parameter, which is the active response state.

Is=6,0,<active>

This can be active when LOW (0, default) or active when HIGH (1).

General input usage commands

Mnemonic Function Usage example Description
Individual input point control commands
I<input> Read an individual input state as 1 or 0 PR I4 Read the state of input 4
CL k3, I4=1 Call subroutine k3 when I/O 4 is ACTIVE (1)
Binary coded decimal (BCD) commands
IN Read all logic inputs as a group. PR IN Read inputs1 – 4 as a group (response will be 0 – 15 based on I1 being LSb and I4 being MSb)
Note on NEMA 17 models the response range will be 0-7.
Input filtering commands
D<input>=<0-255> Set the input filtering to time in milliseconds D1=150 Set the filter for input 1 to 150 milliseconds. Input must be stable for 150 msec before the change will be detected.
Input trip
TI=<input>, <subroutine> Sets the operation of an input trip TI=2, K3 Execute subroutine labeled K3 when input 2 is active.

Global or Local variables

Like All MDrive variables, the I/O point configuration can be declared two ways:

  1. Globally
  2. Locally

Global:  Global I/O configurations, are declared outside the main body of the program. These are more typical and are used when you want the I/O point to have the same function at all times.

Local:  Local configurations are declared inside the body of the program and are stored in RAM, thus may be changed on the fly. An example would be where you may want an input to output to perform one function in part of the program and another in a different part of the program.

Event triggering

The most common use of the I/O is to trigger events within a system:

  • Inputs:  to trigger events internal to the MDrive program.
  • Outputs:  to trigger events within a machine or system.

Input example:

The following program example shows an input used to trigger an internal event:

The following program will move the axis to an absolute position based upon the state of I/O 1 and will hold that position until the state changes.

In this example, the program will use a conditional branch (BR) to activate the event. Input-driven events may also use the Call Subroutine (CL) command in a similar fashion.

Output event

The following program example shows an output used to trigger an external event. A real-world example may be that the MDrive performs and completes a task, when complete the program will activate an output to trigger an external process, such as executing a program in a second system MDrive.

Using a Bank of I/O

The MDrive I/O can read inputs and write outputs as a group in Binary Coded Decimal (BCD).

The I/O may be connected as shown below. For purposes of example, we have used switches for inputs and LED’s for outputs. All are configured as general purpose, active when low, sinking.

For this example we will use two basic programs:

  • Inputs: A program that will perform a specific move based on the input decimal value.
  • Outputs: A program that will cause the outputs to operate as a binary up counter.

Move on BCD Input Example

The following application will move the motor to a variety of positions based upon a binary input to I/O 1-4. One of the things this application shows is one of the new Microstep Resolution settings that give the ability to set motion to occur in 0.001˚ increments. The program does a calculation after each move to show motor position in degrees. To make the web view shorter, the example below omits many repetitive branch routines for the various moves. See the full program to view.

Binary up counter – BCD output example

The following program example shows the use of the lower output bank as a binary up-counter. IO1 – IO4 are setup as general purpose outputs, active when HIGH, sinking. The main program will set the outputs, store the decimal equivalent of the outputs, and index the motor a multiple of the output state. Once complete it will increment the register and repeat until the value of the register =15 (binary 1111). It will then clear the register, set the outputs to 0, and restart the process.

 

PNP Input

In this example case we will assume that we need a PNP Sensor connected to an input to trigger events within an MDrive program. Thus we will configure I/O point 1 as a General Input which will activate when HIGH.

The command:

will set I/O1 to function as we desire:

  • S1=: setup I/O 1
  • 0: General purpose input function
  • 1: Active when HIGH
  • 1: Sourcing input type

Switch Input

In this example case we need a Start switch to execute an MDrive program.

The commands:

will set I/O 1 to function as we desire:

  • S1=: setup I/O 1
  • 4: G0 input function
  • 1: Active when HIGH
  • 1: Sourcing input type

Inductive Load Output

In this example case we will configure a sourcing output. When active, the output will energize a solenoid which in turn will activate another system process based upon an event inside an MDrive program.

The command:

will set I/O 12 to function as we desire:

  • S12=: setup I/O 12
  • 16: FGeneral purpose output function
  • 1: Active when HIGH
  • 0: Sourcing output type

NPN input

This setup example uses the hardware configuration shown in the illustration labeled A on the right. In this example case, we will assume that we need a PLC output to stop the motion of an axis due to an event elsewhere in the system. Thus we will configure I/O point 1 as a Soft Stop Input which will activate when LOW.

The command:

will set I/O1 to function as we desire:

  • S1=: setup I/O 1
  • 5: Soft Stop input function
  • 0: Active when LOW
  • 0: Sinking input type

Switch input

In this example case we will assume a linear actuator is needing positive and negative limit switches. We will use I/O3 as the positive limit and I/O4 as the negative limit.

The commands:

will set I/O 3 and 4 to function as we desire:

  • S3=: setup I/O 3
  • 2: Positive Limit input function
  • 0: Active when LOW
  • 0: Sinking input type
  • S4=: setup I/O 4
  • 3: Negative Limit input function
  • 0: Active when LOW
  • 0: Sinking input type

Inductive load output

In this example case we will configure a sinking output. When active, the output will open a relay which will de-energize another part of the system on an error or fault.

The command:

will set I/O 2 to function as we desire:

  • S2=: setup I/O 2
  • 18: Fault/Error output function
  • 1: Active when HIGH
  • 0: Sinking output type

LED output

In this example case we will configure an output to illuminate a LED indicator when the axis is in motion.

The command:

will set I/O 1 to function as we desire:

  • S1=Moving output function
  • 0: Active when LOW
  • 0: Sinking output type

S command (I/O setup)

The I/O setup command establishes how the I/O point will function in the system, note that each I/O point MUST be setup individually if changed from the factory default:

  • Will it be an input or an output?
  • Will it be general purpose, or set to a dedicated function?
  • Will it be a sinking or sourcing type?
  • Will it be ACTIVE when logic HIGH or LOW?

Setup parameters

The I/O setup command has three parameters that define its operational characteristics, these are:

  1. Function: Assigns the point to an input or output function.
  2. The active state defines the point as being ACTIVE when HIGH or LOW.
  3. Sink/Source: Sets the point as a sinking or sourcing type. NOTE: sourcing outputs only available on Plus2 models.

I/O Functions

The following table lists the function codes relevant to the first parameter: Input or Output functions.

Input Functions Output Functions
Param # Function Param # Function
0 General purpose 16 General purpose
1 Homing 17 Moving
2 Positive limit 18 Fault/Error
3 Negative limit 19 Stall
4 G0 20 Velocity changing
5 Soft stop
6 Pause running program
7 Jog positive
8 Jog negative
11 Reset

Active state

The Active State parameter defines the level at which the I/O point will be active and may be set to:

0 – I/O is Active when logic LOW (0 volts, ground)
1 – I/O is Active when logic HIGH (+5 to +24 VDC)

With outputs, the Active setting becomes of particular importance. The impact of the Active state setting will impact functionality based upon the hardware configuration of the output point and whether it will be sinking or sourcing. The following table details the output circuit conditions for each state and sink/source configuration.

Sink/Source Active State Output State Circuit Condition
Sinking Active HIGH Output = 1 Sink OFF, High impedance
Output = 0 Sink ON
Active LOW Output = 1 Sink ON
Output = 0 Sink OFF, High impedance
Sourcing
(Plus2 Only)
Active HIGH Output = 1 Source ON
Output = 0 Source OFF, High impedance
Active LOW Output = 1 Source OFF, High impedance
Output = 0 Source ON

 

Sink/Source

The Sink/Source parameter identifies the I/O point as sinking or sourcing. If the hardware is connected using any of the configurations shown in the Connecting Power and I/O tutorial, this should be set to match the hardware connection.

0 – Sinking – Point will change states when pulled to ground.
1 – Sourcing – Point will change states when sourced (+5 to +24 VDC).

General I/O usage commands

An I/O instruction results in the change of parameters or the state of an input or output. The syntax for these commands are as follows: type the command then an equal sign, then the data. Example: O2=0 will set output 2 to 0.

This tutorial only covers the General purpose I/O points with the following set of commands to be exampled in the programming part of this tutorial. The Clock I/O and Capture/Trip I/O are only slightly different in their configuration. Please refer to the MCode manual for this instructions.

Mnemonic Function Usage example Description
Individual I/O point control commands
I<io point> Read an individual input state as 1 or 0 PR I4 Read the state of input 4
CL k3,I4=1 Call subroutine k3 when I/O 4 is ACTIVE (1)
O<io point> Set the logic state of an individual output O3=1 Set output 3 HIGH (1)
Binary coded decimal (BCD) commands
OL=<0-15> Set the logic state of the lower output bank OL=15 Set outputs 1 – 4 HIGH (All MDrives) See Details
OH=<0-15> Set the logic state of the upper output bank OH=15 Set outputs 9 – 12 HIGH (Plus2 MDrives) See Details
OT=<0-255> Set the logic state of both output banks OH=255 Set outputs 1 – 4 and 9 – 12 HIGH (if standard I/O this will only set the available points, 1 – 4) See Details
IL Read lower input bank as BCD PR IL Read inputs 1 – 4 as group (response will be 0 – 15 based on I1 being LSb and I4 being MSb) See Details
IH Read upper input bank as BCD PR IH Read inputs 9 – 12 as group (response will be 0 – 15 based on I9 being LSb and I12 being MSb – Plus2MDrives only) See Details
IN Read both input banks as BCD PR IN Read inputs1 – 4 and 9 – 12 as group (response will be 0 – 255 based on I1 being LSb and I12 being MSb) See Details
Input filtering commands
D<input>=<0-255> Set the input filtering to time in milliseconds D1=150 Set the filter for input 1 to 150 milliseconds. Input must be stable for 150 msec before a change will be detected.
Input trip
TI=<input>, <subroutine> Sets the operation of an input trip TI=2, K3 Execute subroutine labeled K3 when input 2 is active. See Details
  1. MODBUS/TCP (Connect to port 502)
    MODBUS/TCP is a standard protocol designed for multidrop networks based on a client-server architecture communicating over TCP/IP.
  2. MCode/TCP (Connect to port 503)
    MCode/TCP is a protocol based upon the MDrive programming language using 1 and two character mnemonics.

Depending on the port selected (ip.address:502 or ip.address:503) the device operation will differ significantly. When connected to port 502 the device will respond to standard MODBUS/TCP function code formats. Commands will map to register addresses and follow the MODBUS/TCP message format. Most importantly, MODBUS/TCP follows a client-server networking model. The device is able to receive commands in immediate mode only.

When connected to port 503, the device will operate like the RS-422/485 products with the exception that party mode commands and features are disabled, as multi-drop communications are addressed via IP.

Comparison

Function MODBUS/TCP MCode/TCP
Networking model Server (Client-Server network) Programmable node
Port 502 503
Command format 6 standard function codes, 2 mfg specific function codes Standard MCode mnemonics
Programmable No Yes – Standard MCode program format
Communication modes Immediate mode functions only Immediate and program mode
Complexity Complex Simple 1 and 2 character mnemonics
Compatibility with programs from RS-422/485 MDrive products None Fully compatible with non-party mode programs.
Recommended use Use in systems unified by the MODBUS protocol. Use in systems where multiple device types communicating using the same protocol isn’t required.

MCode/TCP (Port 503)

MCode is the programming and control language utilized by MDrive Motion Control products developed to communicate over RS-422/485. MCode/TCP is MCode communicating over TCP/IP. The language consists of an extensive command set made up of 1 and 2 character mnemonics.

When connected to ip.address:503 the MDrive will automatically be in MCode/TCP mode. In this mode the device is fully programmable and will operate identical to the RS-422/485 products with the exception that multidrop communication is accomplished using individual IP addresses instead of party-mode names. Global commands are sent using the UDP protocol to address 255.255.255.255.

The key advantages of MCode/TCP:

  • Ease of connection using commonly available RJ-45 connectors and CAT 5 cabling.
  • Elimination of complicated party mode communication wiring and configuration.
  • Higher speed, less error prone than RS-422/485.

MODBUS/TCP (Port 502)

MODBUS is a communications interface developed in 1979 by PLC manufacturer Modicon, Inc. (now a brand of Schneider Electric). MODBUS is designed for multidrop networks based on a master-client architecture.

The availability of devices using MODBUS has made it a de facto standard for industrial communications network. Modbus was originally developed for use with serial communications interfaces such as RS-232 and RS-485, MODBUS communications over TCP/IP has become a standard because of the ease of interface and simpler message format

When connected to ip.address:502 the MDrive will automatically respond to MODBUS/TCP protocol function codes only. Note that when operating the MDrive using MODBUS/TCP it is not programmable and can only use immediate command functions.

MODBUS/TCP message format

Protocol Data Unit (PDU)

The MDrive command information is contained in the PDU and consists of the function code. The data block will contain the address of the register which relates to an MDrive command, and the data of the command in hexadecimal format. MODBUS/TCP communications are in Big Endian form (Most Significant Byte first (MSB))

Supported function codes

dec hex function
Device ID
43/14 0x2B/0x0E Read device identification
Public Function Codes
01 0x01 Read coils (digital outputs)
02 0x02 Read digital inputs
05 0x05 Write single coil (digital output)
03 0x03 Read holding register
16 0x10 Write multiple registers
Manufacturer Specific Function Codes
65 0x41 Read I/O and trip configuration and parameters
66 0x42 Write I/O and trip configuration and parameters, enter upgrade mode.

MODBUS/TCP resources

Program flow

The flowchart below gives a functional overview of what processes the program will employ. The short overview is that the program will ask for some rotations, ask an axis direction, and then perform the move. Once motion is complete, the axis position will be displayed, and the program will ask to repeat or quit.

What functions of the MCode language is learned in this tutorial?

  • Use of user variables and math functions to perform a move in familiar units of measure (in this case revolutions of the motor shaft).
  • Use of program branches (BR) to control program processes and error checking.
  • Use of labels to provide easy access to the various parts of the program, without having to track memory addresses.
  • Use of the print (PR) command and the input variable (IV)command to allow user interaction with the program.

Detailed explanationFull Program

The following represents the complete program separated out into functional blocks. The full program may be downloaded from the link above and programmed into your drive.

System Configuration Details
Ms=256
Vi=200000 ‘Initial velocity = 200000 steps /sec
Vm=2500000 ‘Max velocity = 2500000 steps /sec
A=1000000
D=A
Hc=2
Rc=75
This block, located outside the program envelope, specifies the basic system parameter under which the program will operate
Global user variables
VA k1=0 ‘# of revs to turn
VA k2=0 ‘direction
VA k3=51200 ‘steps/rev
VA k4=0 ‘repeat
The four global user variables contain the data the program will use to calculate and perform the motion based upon the user input into the program.
Initialization block
PG 1
LB su ‘the label “su” designates this ‘program to run on startup
P=-0 ‘clear position counter on startup
PR “At Home Position”
H 2000
This first set of program commands will initialize the position counter to 0, and inform the user that the axis is at home position.
First interaction block – How many revolutions?
LB z1
PR “Enter the number of revolutions in whole numbers”
IV k1
LB x1
BR x1, If=1
H 50
The first interaction block will use the input variable (IV) command to request the desired number of revolutions. This number will be stored in the global user variable k1.

Once the IV command becomes active, the Input Varable Pending flag, IF, will equal 1. We create a lable, x1, and conditionally branch back to the label for as long IF=1.

Second interaction block – which direction?
LB X4
PR “Enter direction (0) negative (1) positive”
IV k2
LB x2
BR x2, If=1
H 50
The second iteration block, again uses the IV command to request information from a user, branching back to a label while IF=1.
Access the subroutine based upon user entry
BR y1,k2=0
BR y2, k2=1
PR “Invalid Entry”
BR X4
This block will decide which subroutine to run based on the desired direction of rotation entered into user variable k2. Introduced here is error checking. If an invalid number (other than 0 or 1) was entered into k2, the program will branch back to the X4 location and await a valid entry.
Motion subroutines
‘****subroutine to move k1 revs in the negative ‘direction****
LB y1
MR -k3*k1
H
BR X6

‘****subroutine to move k1 revs in the positive ‘direction****
LB y2
MR k3*k1
H
BR X6

Thise block will calculate and command the motion based upon the entries into k1 and k2 using the value of k3, which was set to the number of microsteps (51200) in a revolution for an MS setting of 256.
Block to display the axis position
LB X6
VA k5
k5=P/k3
PR “Axis position is “, k5, ” absolute from home”
H 3000
This block creates a local user variable, k5 to store the position in revolutions by dividing the position counter, which is in motor steps, by k3, one motor revolution in steps. The result is displayed in absolute revolutions from home.
Third interaction block – repeat or end?
LB X5
PR “Repeat program (1) or quit (0)”
IV k4
LB x3
BR x3, If=1
BR z1, k4=1
BR z2, k4=0
PR “Invalid Entry”
BR X5
This block will determine whether or not the program will repeat or end. Again, it stores a response in a user variable, and based upon that response will exit the program or return to the first interaction block.

Note that it also includes an error checking branch

End of program block
LB Z2
PR “Program Ended”
E
PG
This block designates the end of the program, and the return to immediate mode.

Copy/Paste the below into the Motion Control Programmer application