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

 

Motion variables and commands covered

Variables

  • A – Acceleration
  • D – Deceleration
  • MS – Set microstep resolution
  • VI – Initial velocity
  • VM – Maximum velocity

Commands

  • MA – Move to absolute position
  • MR – Move to relative position
  • SL – Slew at constant velocity

MCode motion variables

  1. A – Acceleration
  2. D – Deceleration
  3. MS – Set microstep resolution
  4. VI – Initial velocity
  5. VM – Maximum velocity

The relationship between VI, VM, A and D is shown in the motion profile diagram on the right. The following details the sequence of events when a motion command is given.

  1. The motor will accelerate from VI to VM based upon the setting of A in steps per second^2.
  2. Upon reaching VM, the motor will index at the velocity specified by VM in steps per second.
  3. Before reaching the commanded position, the axis will decelerate at the rate in steps per second^2.Note: Setting VI to a high value could cause a motor stall.

The relationship of MS (Microstep Resolution select) and the motion variables

The motion variables are all based on microsteps as the unit of measure. The ratio between the microstep resolution setting and the variables is an important factor in how the motor will perform. Using the factory default settings as a baseline, for example, we can see that when we change the step resolution of the device, the motion variable may also need to be changed.

For example, the default settings of the Lexium MDrive Motion Control:

MS (steps/rev) VI VM A D
Default 51200 1000 768000 1000000 1000000
New 200 4 3000 3906 3906
Ratio MS:<var> 1:0.20 1:15 1:19.5 1:19.5

As shown in the table, the settings for a particular velocity profile can change dramatically based on the microstep resolution setting.

Move to a relative position (MR)

A moving relative (MR) moves an offset distance (motor steps) from the current position. In the case of the profile shown above the end position of the first move will be 1 + 2, or 0 + 51200. The position will be 51200 steps from 0.

The second move of +51200 steps will end at position 3 or 51200 steps from position 2. The third MR will index the axis 153600 steps in the negative direction from position 3.

Note that if the position counter is read using PR P, the counter will read in steps from 0 or -51200.

Move to an Absolute Position (MA)

A move absolute (MA) moves a distance (motor steps) from the 0. In the case of the profile shown above the end position of the first move will be 1 + 2, or 0 + 51200. The position will be 51200 steps from 0.

The second move of +51200 steps will end at position 3 or 102400 steps from position 2. The third MA will index the axis 153600 steps in the negative direction from position 3, with a final position of -51200 absolute from 0.

Slew at velocity (SL)

Slew (SL) is used to index the axis at a constant velocity. The motor will continue to slew at commanded velocity until an SL 0, or escape is commanded.

Note that SL will function independent of the VM setting. If a velocity greater than VM is commanded the motor will accelerate to the commanded velocity, ignoring the setting of VM.

Using an encoder

If the encoder is enabled the move commands use different values. The encoder has 1000 lines and yields 4000 counts or counts per revolution, as shown in the diagram at right.

Therefore, the MR and MA command values are programmed in encoder counts. One full revolution would be programmed as MR or MA 4000.

When the encoder is enabled, the MS value defaults to 256.

Knowing these factors, you can program a multitude of different movements, speeds, and time intervals.

The default values for the motion variables is now used in encoder counts with the following values:

MS VI VM A D
51200 78 60000 78125 78125

Encoder related variables and commands

C2 – Encoder counter I6 – Read encoder index mark
DB – Encoder deadband PM – Position maintenance enable flag
EE – Encoder enable flag SF – Stall factor
EL – Encoder line count SM – Stall detect mode
HI – Home to index mark ST – Stall flag

Note that when the Lexium MDrive is in an hMTechnology mode (AS=1, 2 or 3) the encoder functions become irrelevant.

Lexium MCodeMDrive MCode