Hello Guest it is March 29, 2024, 03:48:45 AM

Author Topic: Lua  (Read 15291 times)

0 Members and 1 Guest are viewing this topic.

Re: Lua
« Reply #20 on: January 07, 2017, 01:42:24 AM »
Hi dude1,
just having a look at your code. Note that you have numbered the entries in the signal table. Is it necessary?

This is from the default screen load script:
Code: [Select]
SigLib = {
[mc.OSIG_MACHINE_ENABLED] = function (state)
    machEnabled = state;
    ButtonEnable()
end,

[mc.ISIG_INPUT0] = function (state)
   
end,

[mc.ISIG_INPUT1] = function (state)
   -- if (state == 1) then   
--        CycleStart()
--    --else
--        --mc.mcCntlFeedHold (0)
--    end

end,

They are not numbered. The SigLib table can be searched by key and the key can be alphanumeric, a handy and powerful property in LUA.

The signal script can therefore be simplified, this is the default signal script:
Code: [Select]
if SigLib[sig] ~= nil then
    SigLib[sig](state);
end

This means that you don't have to use multiple IF statements, should sig be mentioned in your SigLib, ie a signal of interest, it returns non-nil and therefore
executes the function associated with that key with state as argument.

In fact LUA is a bit more crafty than that; if sig exists as a key in SigLib it returns the function entire, I think that property is called 'functions as first class values'.
Another extremely powerful and flexible property of LUA. In this instance of course we don't require the function but just that it exists, ie non-nil.
If you could be sure that EVERY possible sig is in the table then the script could be simplified even further:
Code: [Select]
SigLib(sig)(state);
This idea of bandying about a function, with all its statements, as simply as a single number is a real eye opener to me.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'

Offline dude1

*
  •  1,253 1,253
    • View Profile
Re: Lua
« Reply #21 on: January 07, 2017, 03:31:39 AM »
What i posted is old and may be slightly wrong and it's just one way to do it, it's from the manual, useing a table or library different ways to do the same thing.

I haven't use lua in years I am learning gear monkey script what is simpler to lua but can call to it.
Re: Lua
« Reply #22 on: January 07, 2017, 04:37:28 AM »
Hi dude1,
sorry, didn't mean to imply that it wouldn't work, what I see would work fine. With all the signals in Mach, not just the I/O but all
the other stuff going on the signal script gets hammered. Any reduction in code in it the better.

Functions as first class values, function closures and that sort of thing are really powerful. Just started reading up on the C API...wow!
You got to feel sorry for the 'poor sick little puppies' how dreamed all this s...t up! I think even a toothache would be less painful than
thinking like they do, and the LUA interpreter is only 500 lines.....

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'

Offline dude1

*
  •  1,253 1,253
    • View Profile
Re: Lua
« Reply #23 on: January 07, 2017, 04:27:51 PM »
I am implying it may not work as it is too, Reading through what Art has written about what he has done in Gear monkey script, And the math behind it, Lua and M4 is the same, there are math words I have never heared of.
Setting up all the calls and the way they interact with said application and what does what iT's mind boggling what is sitting in the backed, I have not read up on Lua just the cross references stuff  C type code It shows the limit of the code is the person writing it, it can do a lot more than just running a machine.
Where is NZ are you
Re: Lua
« Reply #24 on: January 07, 2017, 10:08:04 PM »
Hi dude1,
I live in Akaroa, about 100k from Christchurch. Didn't realise I was talking to another Kiwi.. Where abouts are you?

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'

Offline dude1

*
  •  1,253 1,253
    • View Profile
Re: Lua
« Reply #25 on: January 07, 2017, 10:12:52 PM »
Blenheim shaky town, I had to hide all my info I was getting trolled by a member They tried to hide who it was but forgot to remove there IP address, I am just weighting for them to do it again then they will get arrested.
Re: Lua
« Reply #26 on: January 07, 2017, 10:28:31 PM »
Hi dude1,
you lucky bugger!...dream of being trolled (not quite sure what that means but sounds devious).

I work for a company in Christchurch fixing welders. About a year or so ago I happened to meet a guy, a friend of
a customer who used Mach3 for making boards. Really nice guy and clued up but unfortunately lost contact with him,
believe he came from Blenheim, despite looking in the phone book and so on. You wouldn't happen to know whom I
mean would you? Don't want any personal details or anything but would be nice to make contact, he sure knew his
stuff.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'

Offline dude1

*
  •  1,253 1,253
    • View Profile
Re: Lua
« Reply #27 on: January 08, 2017, 12:06:54 AM »
nup, being trolled is when you get nasty massages from A hole

Offline CRS

*
  •  61 61
    • View Profile
Re: Lua
« Reply #28 on: January 18, 2017, 10:08:39 PM »
Hi Dude1 and Joeaverage, I am feeling a bit dumb as I couldn’t find this thread and so had to start another to ask this question.  Then today I realised I could just go to my own profile and find it from there.

Thanks for your help guys, I have gone off and had good success using your suggestions and bits and pieces that you posted as well as some other scripts that had been uploaded in the past.
I still have very little to no idea with Lua, but have learned quite successfully how to copy and paste bits from here and there.  I have most exterior buttons talking to Mach4 now and even a couple of potentiometers for Feed Rate Override and Spindle Speed Override working just as they should.  Almost.....

Would either of you know how to express a percentage range in this line below?

local percent = analog/1*250 ‐‐Calculate percentage from 0% to 250%

It comes from a tutorial that Matevž from PoLabs did about how to connect up the pot to the Pockeys board and then map it to Mach4 as an analog input for the FRO.

For the FRO, 0 to 250% is good, but for the Spindle OVR I need it to be 50% to 150%.  So there is a percentage range from 50 to 150 and I can’t get it happening.  The 150 is easy, currently when the pot is full CW it hits 3.3 Volts and the slider in Mach hits the top, but it’s the other end of travel that is the challenge.

For some reason with the Spindle speed it must be expressed as a decimal percentage, e.g. 0.5 to 1.5.  But how to write it in the correct way in the above line?

The full script is in the attached pdf. Or this is the link to it just in case you have any interest.
http://blog.poscope.com/mach4-tutorial-fro-using-analog-input/

Any suggestions are welcome.  Thanks again guys

Regards,

Craig.

Offline dude1

*
  •  1,253 1,253
    • View Profile
Re: Lua
« Reply #29 on: January 18, 2017, 10:17:38 PM »
That I can't help with, I am trying to do the same thing in a different program with the same problem top speed is fine the low end nup, It's with a pokeys 57cnc.

I would say DAZ would know send him a pm