Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: Tony Bullard on April 18, 2019, 01:39:05 PM

Title: Mach4 with Pokeys57CNC and FRO
Post by: Tony Bullard on April 18, 2019, 01:39:05 PM
I’m trying to set up an analog input in Mach4hobby by following this well written tutorial from Poscope.
https://blog.poscope.com/mach4-tutorial-fro-using-analog-input/   
The registers are setup and showing the analog value as 0 to 1 for 0 to 3.3 volts.
The device name = PokeysCNC_41744. Analog pin = 41.
I have this code in the screen load script.
Code: [Select]
--Function to read value from analog register
function ReadRegister(device, analogPin)
    local inst = mc.mcGetInstance()
    local hreg = mc.mcRegGetHandle(inst, string.format("%s/Analog input %s", device, analogPin))
    return mc.mcRegGetValueString(hreg)
end

--Function to set FRO value
function SetFRO(analog)
    local percent = analog/1*250 --calculate percentage from 0% to 250%
    local inst = mc.mcGetInstance()
    mc.mcCntlSetFRO(inst, percent)
end

--Main
local device = "PokeysCNC_41744" --Change this to the name of your PoKeys device
local analogPin = "41" --Analog input pin number

analogVal = ReadRegister(device, analogPin) --Save analog register value in variable
SetFRO(analogVal) -- Set FRO value in %

This is the error I get when saving the screen edit. See picture

It looks like it’s trying to do arithmetic on a string. How can I correct this?

This post seems to use the same code with no problem.
https://www.machsupport.com/forum/index.php?topic=34051.msg238477#msg238477

Thanks for any help.

Title: Re: Mach4 with Pokeys57CNC and FRO
Post by: Bill_O on April 18, 2019, 01:48:45 PM
local analog2 = tonumber(analog)
Title: Re: Mach4 with Pokeys57CNC and FRO
Post by: Tony Bullard on April 18, 2019, 02:09:48 PM
Thank you.
Would I do that here?
Code: [Select]
--Function to set FRO value
function SetFRO(analog)
local analog2 = tonumber(analog)
    local percent = analog2/1*250 --calculate percentage from 0% to 250%
--local percent = analog/1*250 --calculate percentage from 0% to 250%
    local inst = mc.mcGetInstance()
    mc.mcCntlSetFRO(inst, percent)
end
Title: Re: Mach4 with Pokeys57CNC and FRO
Post by: Tony Bullard on April 18, 2019, 02:37:43 PM
I tried  local analog2 = tonumber(analog) as above with the same error message.
Title: Re: Mach4 with Pokeys57CNC and FRO
Post by: joeaverage on April 18, 2019, 07:27:22 PM
Hi,
where is this code to be placed....the PLC script....where?

I understand that you are following an example but I think you have seriously over complicated what should be very
simple code. You have a 'main' and two supporting functions with input arguments for both functions and a return code
for one function returning a critical value. Why the complexity?

All that is required is the a register be read, converted to a numerical value and the FRO be updated.

Craig
Title: Re: Mach4 with Pokeys57CNC and FRO
Post by: joeaverage on April 18, 2019, 07:45:53 PM
Hi,

Code: [Select]
local inst=mc.mcGetInstance()
local analogRegHandle=mc.mcRegGetHandle(inst,'Analog input/PokeysCNC_41744/41')
if analogRegHandle==0 then
wx.wxMessageBox('Failed to find Register Handle')
else
local analogString=mc.mcRegGetValueString(analogRegHandle)
local percent= tonumber(analogString)*250
mc.mcCntlSetFRO(inst, percent)
end

Would this work?
Note I am not sure how the Pokeys registers are named, I am familiar with the ESS. I put a test in there so that if
a valid register handle is not found then the remainder of the code will not execute and crash Mach with an invalid handle.

Craig
Title: Re: Mach4 with Pokeys57CNC and FRO
Post by: KatzYaakov on April 18, 2019, 09:10:55 PM
the analog script should be place in plc not in screen load
i also use pokeys analog in more then 10 machines
if you follow there instruction its very easy to use
Title: Re: Mach4 with Pokeys57CNC and FRO
Post by: Tony Bullard on April 19, 2019, 11:17:26 AM
Hi,

Code: [Select]
local inst=mc.mcGetInstance()
local analogRegHandle=mc.mcRegGetHandle(inst,'Analog input/PokeysCNC_41744/41')
if analogRegHandle==0 then
wx.wxMessageBox('Failed to find Register Handle')
else
local analogString=mc.mcRegGetValueString(analogRegHandle)
local percent= tonumber(analogString)*250
mc.mcCntlSetFRO(inst, percent)
end

Would this work?
Note I am not sure how the Pokeys registers are named, I am familiar with the ESS. I put a test in there so that if
a valid register handle is not found then the remainder of the code will not execute and crash Mach with an invalid handle.

Craig

Thank you Craig. This looks promising. I tried it and the handle was not found and and now I'm stuck in an endless loop with the MessageBox. I think Daz showed a way to edit the PLC script to get out of this. I'll have to find it.

Before this I tried to follow the tutorial exactly by putting the "PoKeys_analog_FRO.lua" file in the Mach4 directory and calling it from the PLC script with "dofile(“C:\\Mach4Hobby\\PoKeys_analog_FRO.lua”)".
Here's the code

Code: [Select]
--This is a lua file called from the PLC script. PoKeys_analog_FRO.lua
--Function to read value from analog register
function ReadRegister(device, analogPin)
    local inst = mc.mcGetInstance()
    local hreg = mc.mcRegGetHandle(inst, string.format("%s/Analog input %s", device, analogPin))
    return mc.mcRegGetValueString(hreg)
end

--Function to set FRO value
function SetFRO(analog)
    local percent = analog/1*250 --calculate percentage from 0% to 250%
    local inst = mc.mcGetInstance()
    mc.mcCntlSetFRO(inst, percent)
end

--Main
local device = "PokeysCNC_41744" --Change this to the name of your PoKeys device
local analogPin = "41" --Analog input pin number

analogVal = ReadRegister(device, analogPin) --Save analog register value in variable
--analogVal = tonumber(analogVal)

SetFRO(analogVal) -- Set FRO value in %

-- add ths line almost at the bottom of your PLC script:  tb
--dofile(“C:\\Mach4Hobby\\PoKeys_analog_FRO.lua”)
I don't think the registers are being found. Now it's trying to arithmetic on a null value.
Title: Re: Mach4 with Pokeys57CNC and FRO
Post by: joeaverage on April 19, 2019, 03:39:57 PM
Hi Tony,
yes the PLC can be a trap like that. Here is the procedure:

https://www.machsupport.com/forum/index.php?topic=39965.msg267488#msg267488 (https://www.machsupport.com/forum/index.php?topic=39965.msg267488#msg267488)

The reason that the script is failing is because it cant find the register handle and that is because we hav not
correctly specified the path.  The pics you have attached however detail it exactly.

It should be:
Code: [Select]
local analogRegHandle=mc.mcRegGetHandle(inst,'PoKeysCNC_41744/Analog input 41')
Note this has to be exact, including uppercase/lowercase and whitespace.

Because of the issues that you can get into with the PLC I like to write and debug scripts by storing
and working from within the macros folder. Once its right THEN copy it to the PLC.
None the less having the wxMessageBox statement in the PLC script is a mistake. Remove it.

Code: [Select]
local inst=mc.mcGetInstance()
local analogRegHandle=mc.mcRegGetHandle(inst,'PoKeysCNC_41744/Analog input 41')
if analogRegHandle==0 then
        mc.mcCntlGetLastError(inst,'Failed to find register handle')
else
local analogString=mc.mcRegGetValueString(analogRegHandle)
local percent= tonumber(analogString)*250
mc.mcCntlSetFRO(inst, percent)
end

Craig
Title: Re: Mach4 with Pokeys57CNC and FRO
Post by: Tony Bullard on April 20, 2019, 08:09:03 AM
Thanks again Craig for all you do here on the forums. By starting Mach4 in edit mode I was able to remove the bad code in the PLC script. Running the code you provided in a macro with the MDI proved the code was correct. I then put the code in near the end of the PLC script and it runs smooth as silk.
Thank you!