Application Notes
First program: programming motion
Home > Resources > Application Notes > First program: programming motion
Description
This Application Note covers a small program for a motion sequence. It is developed in a fashion that no I/O is used. The only thing needed to be connected is power and communications. There is no difference in function between Liberty* MDrive and MDrive motion products.
Applicable
- Liberty* MDrive
- MDrive Linear Actuator
- MDrive Plus
- MForce
Requirements
- PC with SEM Terminal installed
- An MDrive or MForce Motion Control connected to power and RS-422/485 communication. If using an MForce a motor needs to be connected as well.
*Previously Lexium
Details
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.
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.
PR “Enter direction (0) negative (1) positive”
IV k2
LB x2
BR x2, If=1
H 50
BR y2, k2=1
PR “Invalid Entry”
BR X4
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
VA k5
k5=P/k3
PR “Axis position is “, k5, ” absolute from home”
H 3000
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
Note that it also includes an error checking branch
PR “Program Ended”
E
PG
Copy/Paste the below into the Motion Control Programmer application
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | ‘******System Config***** Ms=256 Vi=200000 ‘Initial velocity = 200000 steps /sec Vm=2500000 ‘Max velocity = 2500000 steps /sec A=1000000 D=A Hc=2 Rc=75 ‘****global user variables to contain input data**** VA k1=0 ‘k1 will contain the # of revolutions to turn VA k2=0 ‘k2 contains the desired axis dierection VA k3=51200 ‘k3 defines 51200 steps as revolution VA k4=0 ‘k4 exit or repeat variable ‘****Program**** 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 ‘****block to input number of revs to move**** LB z1 PR “Enter the number of revolutions in whole numbers” IV k1 LB x1 BR x1, If=1 H 50 ‘****block to input the direction of rotation**** LB X4 PR “Enter rotation direction (0) negative (1) positive” IV k2 LB x2 BR x2, If=1 H 50 ‘**** run subroutine based on desired axis direction**** BR y1,k2=0 BR y2, k2=1 PR “Invalid Entry” BR X4 ‘**** display the position in revolutions*** LB X6 VA k5 k5=P/k3 PR “Axis position is “, k5, ” absolute from home” H 3000 ‘****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 ‘****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 ‘****end of program **** LB Z2 PR “Program Ended” E PG |
*Previously Lexium
When it comes to your form, fit and function requirements, don’t settle. Get precisely what you need working with us. We know motion.
Contact us with any questions about how we can help you with your motion application or for assistance with your IMS products.