Hello Guest it is April 19, 2024, 11:39:45 PM

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - rhtuttle

471
Mach4 General Discussion / Re: Lua Macro Parameters
« on: March 07, 2017, 07:43:31 PM »
Checked all profiles' macros folders and no load_modules.mcs in any of them.  There is a moduleload.mcs in the Mach4_Industrial_Mill macros folder.

When I added your package path code to the Load_modules.mcs that I created and then load Mach4 (lathe by the way) I see a quick flash of a windows command prompt window opening and closing.  Probably showing an error message.

My head hurts so I need to call it a night.  Thanks for trying.

RT

472
Mach4 General Discussion / Re: LUA Script error in PLC, PoKeys
« on: March 07, 2017, 07:04:28 PM »
I feel your pain.  Understanding/following code for the uninitiated is really confusing and Lua lets you do anything you want and assumes you know what you are doing so it is very hard to debug since it does very little type checking.

you are correct that "45" is analogPin, when it is passed to ReadRegister it is also named analogPin.  But ReadRegister could just as easily called it aPin.  All Read register knows is that it is supposed to get a string.  ReadRegister  looks up the value stored in a register named "45" and returns a valueString (return mc.mcRegGetValueString(hreg)) to analogVal.  It then sends that string of characters to setFRO:
analogVal = ReadRegister(device, analogPin) --Save analog register value in variable
SetFRO(analogVal) -- Set FRO value in % 

setFRO takes analogVal and calls it analog now.  Still a string, convert it to a number

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 = toNumber(analog)/1*250 --calculate percentage from 0% to 250%  *****changed here******
    local inst = mc.mcGetInstance()
    mc.mcCntlSetFRO(inst, percent)
end

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

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

HTH

RT

473
General Mach Discussion / Re: Inconsistant ATC Positioning
« on: March 07, 2017, 06:52:40 PM »
No, not necessary.  G90 is what they call modal.  It stays that way until a G91 is issued changing the mode to incremental

474
General Mach Discussion / Re: Inconsistant ATC Positioning
« on: March 07, 2017, 06:31:14 PM »
Anywhere before this:

If GetSelectedTool = 1 Then
Code("G0A0")
While IsMoving()
Wend
Code("G0A-358")
While IsMoving()
Wend
SetOemDRO(803,1)

End If  

I would change to:

Code("G90")
If NEWTOOL = 1 Then
  Code("G0A0")
  While IsMoving()
  Wend
  Code("G0A-358")
  While IsMoving()
  Wend
  SetOemDRO(803,1)
End If  


475
Mach4 General Discussion / Re: LUA Script error in PLC, PoKeys
« on: March 07, 2017, 06:16:34 PM »
Not the expert here but you passed the Analog variable which contained a string of two letters a '4' and a '5' representing "45".  When passed to the function where you do the calculations you are trying to divide a a string by some numbers.  You need to convert "45" to 45.

alog=toNumber(Analog)

alog will now contain the number 45 and you can do calculations with it.

HTH

RT

476
Mach4 General Discussion / Re: Lua Macro Parameters
« on: March 07, 2017, 06:12:50 PM »
Steve,

Thanks for the explanation that there are two instances of Lua running and controlling Mach4.  I couldn't tell whether Daz was referring to two instances of rt or Lua or whatever.

As I put in my previous post:


I created, since none existed, a load_modules.mcs file and put it in the same macro folder as my profile macros 'C:\Mach4Hobby\Profiles\My4Lathe\Macros'
...
package.loaded.rtModule = nil
rt = require "rtModule"
...


Is this not accomplishing the same thing as you and Daz suggested?  The only difference is .mcs versus .lua  Are they both not compiled into the one file?


477
Mach4 General Discussion / Re: LUA Script error in PLC, PoKeys
« on: March 07, 2017, 05:49:42 PM »
When I run it I get a debug message:

attempt to perform arithmetic on local 'analog' (a string value)

change it to a number and try it then

478
Mach4 General Discussion / Re: Lua Macro Parameters
« on: March 07, 2017, 05:40:23 PM »
Daz thanks for the reply,

Mach4 has 2 instances of what?

I followed Chaticione's example to a T, at least I think I did.  I'm missing some basic concept/information on how mach/lua deals differently with screen scripts versus macro scripts.  

when to use mcCntlMDIexecute versus mcCntlScriptExecute

wx, sc and mc are just libs that have been loaded at startup and now my rt is as well.  I get that the load screen scripts instantiates my rt variable and read that lua only loads a 'lib' once even if called more than once.  Your reference to creating a functions.lua in the macro folder seems to be what the load_modules.mcs is doing now and which just seems to be loading the same lib again.

Sorry for the terseness but I'm getting dizzy and frustrated after spending hours reading and researching something that no one seems to be able to explain to people that are new to lua, and yes I have read/studied a lot on lua in the last month.  Not real fond of the compiler or the debugger.


479
Mach4 General Discussion / Re: Lua Macro Parameters
« on: March 07, 2017, 04:45:17 PM »
Chasing my tail again. ???

I created a module 'rtModule.lua' in the modules folder
...
local rtModule = {}
  function rtModule.rtMessage(msg)
   wx.wxMessageBox(msg)
  end

return rtModule
...

Added this to the screen load script under the load modules section
...
package.loaded.rtModule = nil
rt = require "rtModule"
...



I created, since none existed, a load_modules.mcs file and put it in the same macro folder as my profile macros 'C:\Mach4Hobby\Profiles\My4Lathe\Macros'
...
package.loaded.rtModule = nil
rt = require "rtModule"
...



Created a macro 'm9002.mcs' under the profile macros folder
...
function m9002()
  rt.rtMessage('Another Hello')
end

if mc.mcInEditor()==1 then
  m9002()
end
...


Added 2 buttons to the screen

btn1 leftup script
...
rt.rtMessage('Hello from a Button')
...


btn2 leftup script
...
inst=mc.mcGetInstance()
mc.mcCntlMdiExecute(inst,'m9002')
...


btn1 gives me the expected 'Hello from a Button'
btn2 does nothing
put m9002 in the mdi and press cycle start, nothing.

So I know that the rtModule.lua is getting loaded and functiong correctly.
Do I have a syntax error in the macro?  Compiles.
Is there more to the load_modules.mcs that I am missing?

HELP!

480
General Mach Discussion / Re: Inconsistant ATC Positioning
« on: March 07, 2017, 02:36:22 PM »
George,

Glad to hear it is coming along.

I am confused as to why you want distance mode set to Inc.  This means every time you issue a G0 A45 it will advance the turret 45 degrees not go to home + 45 degrees.  Some of your other gcode may override either distance mode by issuing a G90(absolute-go to dro position X) or G91(Inc-move an additional X amount)

Your macro assumes that you want to be in Abs mode so start by adding the code "G90" to your macro.  You can get fancy later and add to your macro, read and save the current mode, execute your current macro and then reset the mode back to what it was.

Let me know if I misunderstood you.