lua Encoder in Degrees with Zero
This will show you how to set up an encoder that will display your position in degrees with the ability to zero the position.
Step 1
Make a copy of your "C:\Mach4Hobby\" folder in case you mess something up. You can always revert to it if you need to.
Alternatively, make a copy of your screen set file, which you can find in "C:\Mach4Hobby\Screens\", since this is what we will be modifying. You may find the name of your current screen set in the lower right hand corner of the Mach screen set.
I would recommend using a custom name different than the default screen set names
Step 2
In your ESS config, hook up an encoder as shown (you may use a different encoder input signal, but your code will change from mine.
Step 3
Start editing the screen.
Step 4
We now need to edit some scripts.
Step 5
Open the Screen Load Script, by clicking in the area just to the right of where it says "Screen Load Script". Then click on the "..." that appears.
The Screen Load script is only ran once, and it is the perfect place to add global variables, so they can retain the value you set them too. Now add this singe variable definition to the screen load script:
EncoderPositionZero = 0
When you click on the button to zero out the encoder position, it will be set this global variable to the current Encoder value.
Go to Menu -> Project -> Compile and make sure that no errors are reported.
Save and close this script.
Step 6
Open the PLC Script, by clicking in the area just to the right of where it says "PLC Script". Then click on the "..." that appears.
The PLC script will run at your PLC Interval (frequency ) which defaults to 50 Hz. You don't want to declare a global variables in here, but local variables (that will be overwritten every time) are fine. This is where you will add this script functionality, to calculate the current encoder position in degrees:
-------------------------------------------------------
--Encoder Position Update Code
-------------------------------------------------------
PPR = 100
hEncoderPosition, rc = mc.mcRegGetHandle(inst,"ESS/Encoders/Encoder_Aux_0")
EncoderPositionVal, rc = mc.mcRegGetValue(hEncoderPosition)
local modulo = (EncoderPositionVal-EncoderPositionZero) % PPR
local degrees = (360 * modulo ) / PPR
scr.SetProperty('droEncoderAngle', 'Value', tostring(degrees))
Change the PPR value to the number of Pulses Per Rev that your encoder has.
Go to Menu -> Project -> Compile and make sure that no errors are reported.
Save and close this script.
Step 7
Add a button, and change the label to say "Zero" or zero the encoder position or whatever you would like. Click on the white icon to the left of the lighting bolt to see the properties.
Now click on the lighting bolt, the "Left Up Script" area, and then the "..."
Add this code to the script area:
local inst = mc.mcGetInstance('Encoder Zero Button') -- Pass in the script number, so we can see the commands called by this script in the log
hEncoder, rc = mc.mcRegGetHandle(inst,"ESS/Encoders/Encoder_Aux_0")
EncoderPositionZero, rc = mc.mcRegGetValue(hEncoder)
Go to Menu -> Project -> Compile and make sure that no errors are reported.
Save and close this script.
Step 8
Add a DRO and name it "droEncoderAngle"
Step 9
Close out of the Screen editor by clicking:
Click "Yes" to save your changes. If you click "No", they will all be lost.
Step 10
Your DRO should show the current position in degrees and you can zero it out!