Hello Guest it is March 29, 2024, 01:16:46 AM

Author Topic: Pokeys 56U PWM Output with LUA  (Read 1838 times)

0 Members and 1 Guest are viewing this topic.

Pokeys 56U PWM Output with LUA
« on: May 17, 2017, 05:35:48 PM »
Hello all.  I tried out the Pokeys today.  I used the FRO example in the manual today to test out an analog input and just my sanity.  After switching to the correct " after a copy and paste, I have this working great for the FRO slider on the screen.  I then wanted to try out using the same analog input to set the one PWM output.  So I tried out:

--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 Write value from analog register
function WriteRegister(device, outPin, newValue)
    local inst = mc.mcGetInstance()
    local hreg = mc.mcRegGetHandle(inst, string.format("%s/Analog output %s", device, analogPin))
    return mc.mcRegSetValueString(hreg, newValue)
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 = "PoKeys_30290" --Change this to the name of your PoKeys device
local analogPin = "41" --Analog input pin number
local outPin = "18" --Analog output pin number

analogVal = ReadRegister(device, analogPin) --Save analog register value in variable
outVal = analogVal
WriteRegister(device, outPin, outVal)  -- Set Output Value
SetFRO(analogVal) -- Set FRO value in %

In my quick and dirty additions, I thought I'd just maybe get this having an output duty cycle matching the analog signal, but alas it does not. 

Can someone help me with how you use similar code to above to set the duty cycle on the PWM Output?  I'll eventually be using some of the z positions for a custom solution to control the duty cycle but for now I just wanted to understand how to get the signals working.  I'm starting to understand the usage but I'm a little slow sometimes. 

Also - This in on MACH 4, I have the plugin installed and otherwise working well NOT using it as the pulse gen/motion control.  I just want to use this for some external accessories.  Thanks!

Jared
Re: Pokeys 56U PWM Output with LUA
« Reply #1 on: May 18, 2017, 08:34:04 AM »
So, I'm really green with the LUA.  I wasn't getting the string correct for the PWM register.  I fixed that up. 

function WriteRegister(device, outPin, newValue)
    local inst = mc.mcGetInstance()
    local hreg = mc.mcRegGetHandle(inst, string.format("%s/PWM %s duty", device, outPin))
    return mc.mcRegSetValueString(hreg, newValue)
end

Working now!!