Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: CRS on January 04, 2017, 11:54:54 PM

Title: Lua
Post by: CRS on January 04, 2017, 11:54:54 PM
Hi guys, my name is Craig, a boatbuilder in the middle of converting a small Hercus mill to Mach4.

I know Lua is not a new topic, but being new to forums, I don't really know how to go about asking.

Anyway, days I have spent trying to get my head around Lua.  I've read every thread I can find about Lua, watched every video etc., and done as many online tutes as my tolerance will allow.  My rule is, don't ask questions until I've put some time in and my head hurts.

I must say, I am out of my depth and my head really hurts.  I have a lot to ask, but for now just a couple of things which are eluding me.

Can someone enlighten me about what the "mc" that appears in front of mcLua and many other script lines actually means?  And what does API stand for?  Just those two things will stop me chasing my tail quite so much.

Thanks,

Craig
Title: Re: Lua
Post by: joeaverage on January 05, 2017, 01:15:32 AM
Hi Craig,
I'm a newcomer to LUA myself and like you really struggled initially. While by no means fluent I'm getting there. Having got over
the hump I now find LUA to be superb.

API=Application Program Interface
mc.=Mach Core

LUA like all computer languages have variables which you can manipulate in any number of ways. For LUA to be useful to us we
need to be able to make it do things to our machine. Mach provides a whole bunch of functions which allow you to do that, collectively
called the API. On the 'Program Run' page with 'File Ops' in the lower notebook with Mach enabled there is a 'Help Docs?' button and in
there 'Mach4CoreAPI.chm' provides the complete list, aside from other manuals and lists. Its quite daunting to look at to start with.

Take a simple one to start with:
inst=mc.mcGetInstance()

The mc. means 'a function within Mach Core' and the function is mcGetInstance. Sometimes the mc. is referred to as a namespace
There are others and some I have a lot of trouble with for instance wx.  It refers to wxWidgets functions which are  graphical windows,
message boxes, dialogues and that sort of thing. Another one which crops up a lot is scr.  It addresses screen elements like buttons and LEDs.
While the Mach Core API is well documented some of the others are not, scr. for instance. wx. on the other hand is superbly documented
because it is so widely used by so many different languages, the downside is its so complete I can't usually make much sense of it, it takes
some serious 'nerdiness' to read.

I've found particularly with wx. I copy/edit examples of whats already been written, setting up frames, panels and event handlers from scratch
is not easy.

For basic LUA stuff I often have
https://www.lua.org/pil/contents.html (https://www.lua.org/pil/contents.html)
open on the desktop so I can dip into it at need.

The good news tho is that unless you have a particular need for something that you probably don't really need to write any code to convert
a machine, Mach4 is pretty complete for milling machines. Additionally if you do want something the chances are that someone has probably
got something pretty close you can copy/edit. There are some regular contributors, Daz-The-Gaz especially who is VERY well versed in LUA and
really goes out of his way to help newcomers.

Craig
Title: Re: Lua
Post by: CRS on January 05, 2017, 02:38:33 AM
Hi Craig, isn't that a coincident.  I will have a bit of trouble remembering that name...

I have to thank you so much for taking the time.  You have just answered something that has been bugging me.  I tend to go off on tangents until I get an answer to the silliest things, and now I have it, I can move on.

I look forward to getting over that hump you mentioned.  Currently I am bogged on the way up the pre hump side.  I have found many of the tutorials for dummies and beginners, start off well, nice and slow, and then they seem to jump a gear without a real explanation and leave me in the dust, it’s then I feel like I am back learning algebra.  Never did pass that.

To your credit, and maybe it comes from the fact that you have had the same brain numbing learning experience, your explanation has taught me more than I have gleaned from my scouring of the internet for a basic starting point.

Anyway, the main thing I want to achieve is to have my existing control panel (or a copy of) with all its buttons being able to send signals to Mach and on some of the illuminated buttons, like Cycle Start/Stop Feed Hold etc., receiving back the signal to light the bulb.  There are the jog buttons and the three MPGs.  The electronics, as it turns out, is the easy part.

I have done as you say and looked for scripts already written that I can copy.  DazTheGas is great isn’t he?  What a knowledge he seems to have.  I have managed to copy and implement those few line of the Cycle Start script, which is talked about in several places on the forum, and nearly wet myself from excitement when I got that to actually work with an external button onto my Pokeys 57E.

Hey this stuff is easy! (not really) And then I tried the same thing with Cycle Stop and Feed Hold.  NO JOY!  How the fatigue sets in fast when you get a negative response for about the tenth time.  I am sure you have never felt that….  So that is where I am at.  Totally confused and back to watching more videos.

Craig once again, thanks so much, I might go back and read your reply for about the eighteenth time now.
Craig.

Title: Re: Lua
Post by: joeaverage on January 05, 2017, 03:05:48 AM
Hi Craig,
how many buttons does your control panel have?

If its a reasonable number say 5 or less then the simplest way would be to assign 5 pins as input to Mach and
assign 5 pins as outputs from Mach to turn on the lights. I'm not familiar with your controller but would guess you
have enuf inputs/outputs

There are more complicated ways (MODBUS) to do it where you can have dozens of inputs/outputs if you need them.

Craig
Title: Re: Lua
Post by: CRS on January 05, 2017, 04:09:26 AM
Hi Craig

I didn't expect a reply so fast.  Thanks!  Hey which part of the world are you in?

Re the ins and outs.  I am using the ESS together with the MB2 break out board from CNCRoom.  The MB2, which is in my control cabinet has 22 Inputs and I think 14 outputs as well as a built in Analog speed controller.  For the front panel with all its buttons and other stuff I have the PoKeys 57E, which has 55 terminals which can be assigned as inputs or outputs.

So I have way enough to handle all the I/O.  I will attach a pdf just so you can see my intent with the front panel.  Probably getting a bit greedy, but I have been using the mill a certain way up till now, and really want to keep up what I am used to.  I have added a bit more, must admit.

The MB2 will handle of the safety stuff, E-Stop, Drive Fault as well as Limits and Spindle and the PocKeys will take care of the multitude of things on the control panel.  I have that hooked up in test mode and pretty much configured, and to my surprise, they all talk to each other.

So this is why the need to keep on learning Lua.  I did come across one thread where the guy was doing roughly the same.  He had the Cycle Start going and was onto the other buttons.  You know, for the life of me, I can’t find it again.  Really dumb of me not to book mark it.  I have found similar, but not that one.

Craig.
Title: Re: Lua
Post by: joeaverage on January 05, 2017, 04:44:43 AM
Hi Craig,
I live in New Zealand.

That's a pretty impressive control panel, without any multiplexing I got to 27 inputs without counting the MPGs or Overrides.

I would be having a close look at the number of input/output pairs you require. I to use the ESS and it has three ports. Lets assume
that the motors, limits and homes use one port, leaving two still to be assigned. Assume also that you configure pins 2-9 on both
ports as inputs for a total of 26 inputs (2-9+10-13+15 times 2) and 8 outputs (1+14+16-17 times 2). Without multiplexing not enuf.

The overrides as you have shown them are analogue so will have to be converted to digital signals and even as serial inputs will
require 1 pin per override without multiplexing. MPGs require two pins each.

I think you either have to rationalise your control panel or do some sort of MODBUS arrangement. Depends on what you want to do,
if your hobby is tinkering with electronics and software go MODBUS or similar, if you want to make things with your machine then
shagging around with MODBUS is going delay proceedings bigtime.

Craig
Title: Re: Lua
Post by: CRS on January 05, 2017, 05:25:05 AM
Ah ha, New Zealand, I have been there so many times.  Great country ah.  I’m not really sure how to spell ah...

Yes the question of inputs and outputs.  Having enough resources to handle it all.  But first to answer the other question about what I do with the machine.  The mill is actually a help for me to manufacture.  I cut moulds with it.  It functions well as is, although a huge pain.  I generate the tool path with Mastercam, then have to download it via the RS-232 port into the old ANCA control.

For one surfacing job, on one mould, on one side of the mould, I would normally do three to four separate downloads to get the job done.  About 12 to 14 thousand lines of code.  The old 1986 ANCA has a huge whopping 0.153mb (153kB) of memory free for me to use.  I know, I know, quite impressive hey...  So you can imagine why I am keen to get out of there.

Back to I/O.  The PoKeys connects to the PC via its own cat6 cable and as uneducated as I am with the whole Mach thing, it looked like, from Mach's point of view, when I go into the config Mach section, that there are plenty of inputs and outputs available.

I had the same question about the parallel port thing as well.  But from what I read on the Pokeys site and talking to my friend who is actually an electronics guys unlike me, and also has the ESS and PoKeys, it seems like Mach has the ability to accept all the inputs from the PoKeys as well as the ESS.  I think the MB2’s available I/O pretty much fills up what is available from the Parallel Ports, but when I look in Mach, there is something like sixty something available inputs.

Am I talking nonsense or is it possible?

The PoKeys also has I think 7 Analog inputs for use with Potentiometers or other analog devices.  Also heaps of encoder inputs to take care of the MPGs.

I am still trying to get a handle on all this, so now you have got me thinking about it, I best go off and re check.

Probably time for you to get to bed, being you are a couple of hours ahead of us here.

Craig.


Title: Re: Lua
Post by: joeaverage on January 05, 2017, 05:38:10 AM
Hi Craig,
to my knowledge Mach4 can have one motion controller active at a time. I'm a very recent convert to M4 so what I say
is far from gospel. If I'm correct then a simple in/out pin pair arrangement won't work with ESS.

I am unfamiliar with the PoKeys board but sounds like your best bet particularly if it has analogue inputs.

Mach4 has plenty of in/out but you have to squeeze it thru your motion controller, I think that's going to be your
bottleneck.

Craig
Title: Re: Lua
Post by: joeaverage on January 05, 2017, 06:06:32 AM
Hi,
just had a look at the PoKeys 57 manual, pretty impressive.

From what I can gather you can have a total of 55 inputs or outputs, less one for each analogue input and less two
for every encoder. Haven't quite worked out how the digitised data gets communicated yet but guess its via Modbus registers.

If you can have more than one motion controller active you'll be fine.

Craig
Title: Re: Lua
Post by: CRS on January 05, 2017, 07:00:36 AM
Hey Craig, you are a worse night owl than I am.

That is a good point you make about not having two motion controllers active at the same time.  Maybe becasue I am only using the PoKeys as an interface then that isn’t an issue.

About a week ago I opened a ticket with the Mach support and explained that to increase the I/O I wanted to use the PoKeys for all my front panel stuff and the ESS/MB2 for the other stuff and asked if they could see any issues.   Actually I did the same with the PoKeys support as well.  Both came back with positive responses.

Matevž from PoLabs said that he couldn't see any issues just to make sure that the PoKeys got a valid network configuration.  I had no idea what that meant and had to ask for clarification.

Rob Gaudette from Mach support said just to do all the mapping via the ESS and I would be good to go.  I still don't really understand what he meant, but I did my best to do that and have them both configured and running at the same time in Mach and they both seem to make stuff happen without getting in each other’s way.

But I am going to ask Mach support again I think, just to be sure.  Thanks so much for drawing my attention to this Craig.  You are a Champ

Bed time

Craig.
Title: Re: Lua
Post by: joeaverage on January 05, 2017, 07:41:20 AM
Hi Craig,
just checked and Daz-The-Gaz confirms that only one motion controller can be active at once, however more
than one device can be active.

Thinking that your motion controller would be ESS with usual motors, limits, homes and Estop.

Your control panel hooks to the PoKeys.

Both would require an Ethernet connection but pretty sure you can arrange it so the IP addresses don't clash but
your PC would require two Ethernet ports.

I would in the first instance get the motion controller working and not worry about the panel until you've got the machine
running. Trying to get both going at once if your new to it is going to give you grief.

Craig
Title: Re: Lua
Post by: DazTheGas on January 05, 2017, 07:52:39 AM
Sometimes you have to look into the future, ever wondered why you have an instance number for mach4 and suddenly appearing within in places within the GUI editor.. I could be wrong but I can see that in the future additional motion controllers could be attached to different instances, but then your talking big machines and definitely not the hobby version of mach4.

But thats just my views

DazTheGas
Title: Re: Lua
Post by: joeaverage on January 05, 2017, 08:11:24 AM
G'day DTG,
that sort of thinking does my head in! Imagine axes spread over two controllers and you want to probe....would the probe be attached to one controller
or both...

My guess was that there will come a time when you can have multiple copies of M4 running on the same platform but running different machines, or
rather multiple threads, one for each machine but only one copy of M4 running. I'm a long way from coming to terms with just one active thread!

Craig
Title: Re: Lua
Post by: CRS on January 05, 2017, 11:09:29 PM
Hi Craig,

Another day already, thank you for your time on this, I am sure you must have other more pressing things to do.  I really appreciate your help.

Well it seems the I/O shouldn't be an issue.  I did send the question back to Mach Support, but it looks like using the ESS as the motion control and the PoKeys purely as an interface card, shouldn't be an issue.

Actually, there is a YouTube video by a chap called Paul who happens to be just down the road from where I live.  He is asking for help with his MPG issue.  https://www.youtube.com/watch?v=qFqoS4VVnrw
He uses an ESS as the controller and also has the PoKeys in his custom control box for all the I/O.  STUPID ME!  That is where I got the idea!  Getting older.

So back to Lua.  I have once again been reading more.  The Help menu on the Mach screen with all the docs, was already one of my well frequented areas as was the other link you sent.  I am still trying to figure out how to get the Cycle Stop or Feed Hold to work.  Been looking how it is written further down in the script and comparing it to the Cycle Start.  That hump is pretty high.

Have you ever tried putting any of you buttons external?

Craig
Title: Re: Lua
Post by: dude1 on January 06, 2017, 01:22:19 AM
Have a look up https://www.youtube.com/user/hossmachine/videos Hoss use's a C6 to run the motors and other stuff and a pokeys for IO, on his control panel.
With the pokeys you set it to a IO devices, Mach can use one motion control and one or more IO at once.

your ESS will be the motion devices on that board and the pokeys set IO, It's in the pokeys manual how to do it.

The Pokeys 57cnc is a monster of a devices it's a mile a head of the ESS the ESS may catch up one day.

Have a look on the pokeys web sight there is instructions on there for doing just what you wont http://blog.poscope.com/mach4-tutorial-custom-signal-mapping/
Title: Re: Lua
Post by: joeaverage on January 06, 2017, 01:25:58 AM
Hi Craig,
no, I don't have any external buttons. I do use a pendant which has an MPG wheel and has all the functions of your panel
and some extras. It plugs in to the USB port, real breeze. Perhaps not as convenient as your panel but it works well.

A couple of ways to do it:
1) in the PLC script have a bunch of code that reads all of the inputs in turn and performs an action if it detects
    a button. The PLC script runs every 50ms or so.
2) use a signal script. In the 'help docs' look at 'Mach4 CNC Controller Lua Scripting Guide', section 3.2.4 Signal Script
    The method of building a table (in the screen load script) and having a series of functions in the signal script for
    each of the different buttons is probably the way to go.

Option 2 is by far the more computationally efficient method.

Have you got your machine running yet? No matter how fancy your control panel is you must have a debugged motion
controller and machine for it to control. At need you can run ALL your programs without a control panel, in fact the vast
majority of Mach4 installations run this way. With a touch screen you don't even have to have a physical panel.
1) Get your machine running with Mach4/ESS/MB2
2) Shag around with a panel to your hearts content, at least you'll still be able to get your moulds done in the mean time.

Craig
Title: Re: Lua
Post by: CRS on January 06, 2017, 04:25:22 AM
Hi Dude 1 and Craig aka joaverage,

Firstly Dude.  Thanks for chiming in, yes I have watched quite a few of Hoss’ videos.  And once again silly me for not remembering his set up.

I don’t really have any problems with the whole ESS/PoKeys getting along thing.  I have both working together with Mach and can run a G Code file and have the ESS, via the MB2 BOB sending out motor commands which was all initiated by using my Cycle Start button which is hooked up to the PoKeys 57E.  Also I have configured pins on the PoKeys to receive signals back from Mach which I set up with the default choices in the Mach configuration dialog box.  And that all works just fine.

If I was starting again, maybe I would have gone with the PoKeys CNC, but when I started, all I knew about was the Smooth Stepper.  I will be making another after this, so I will probably go with it for that one.

Thanks again to Craig for the info.  I couldn’t agree more about getting the mill up and running first.  I can get a bit wordy with my explanations, so on my wife’s direction I have learnt not to include every bit of boring info straight up.  Let people warm to me first…. And then bore them to death.

So I left out the whole explanation of where I am at with the building of the control and the fact that it is chugging along very nicely in the back ground.  The whole put-together of the electrics, the electronics and associated parts is fun and is now quite easy for me.  Didn't state out that way though.

I decided to dedicate myself to learning about Lua, as I knew it was coming up and I wanted to be ready.  Yes I will run the machine and get it all working first and then spend the time getting the buttons working.  The touch screen is a real option.  But the darn things seem to be quite expensive, so I shyed away.

Back to Dude1, I did actually use the PoKeys script example which you steered me towards.  It also appears in several other places in the Mach forum and by following that example, it was quite easy to get my Cycle Start button working with the PoKeys.  But I get lost with the next step where he talks of the Signal Library, handlers and tables.  My wife is Croatian and I have been trying to learn that language for about thirteen years, Lua seems about as complex at that.

Craig, I have read your last email a few times and now off to study more on how to do what you have suggested with option 2.  Once again those strange terms like Signal scripts etc..  I read about it all, but still not making sense.  Wise words though my friend about majoring on the major things.  Couldn't agree more.

Guys thanks, I will stop bugging you now.  I will go suffer in silence.  Have a great weekend and hopefully the next time I post on the forum, I will be free-wheeling down the other side of that proverbial Hump.
Craig.
Title: Re: Lua
Post by: Chaoticone on January 06, 2017, 09:07:58 AM
This is an example that is in the default screens. Most of it is commented out and it doesn't have as many comments but what it does is sets up input one to do two things. Cycle start and feed hold. It acts like a dead man if used with a momentary switch.

You push the switch in, input one changes states from 0 (off) to 1 (on), the function runs which checks the state of the switch and if == 1 it runs the CycleStart function (found further down in the screen load script).

When you release the button the state of input one changes again and it now == 0 (off). So, since the state changed from 1 to 0 the function runs again which checks the state and if anything other than 1 run the  mc.mcCntlFeedHold API call.

Code: [Select]
[mc.ISIG_INPUT1] = function (state) --Add input one to the signal table so this function runs each time input one changes states.
    if (state == 1) then --If the state of input one equals 1 (on) run the CycleStart function.
        CycleStart()
    else --The state of input one does not equal 1
        mc.mcCntlFeedHold (0) --Feed hold instance 0 of Mach4
    end

end,
Title: Re: Lua
Post by: CRS on January 06, 2017, 11:28:06 PM
Hi Chaoticone, thanks for your input.  I have seen a lot of your posts over the time that I have been looking though the forums.  It is a pretty amazing thing, that a community of like minds that can share this knowledge world-wide, without having to actually meet.  That still amazes me.

I think my wife is becoming a CNC widow..

I had seen that example that you showed on your post, and I think pretty much the same as on the Pokeys site and a few other threads.  Thanks for explaining it as you did.  Every bit helps me to become more comfortable with it all.  I did get the Cycle Start working as it should by using this example.  But when I try the same thing for Feed Hold and Cycle stop, I just get errors.  More to learn.

Got to get my head around how the bits in the Screen Load script, the PLC, Signal Library and the Signal Script all relate to each other.  And what all the weird names mean.  I have done many tutorials and the penny will drop I am sure.  It is just a mind shift I guess.  Thank you though for taking the time to explain a bit more.  I look forward to being able to help some other confused enthusiast down the track.

Craig.
Title: Re: Lua
Post by: dude1 on January 07, 2017, 01:11:57 AM
You need something like this I am not sure if this is the correct way to do it now
Code: [Select]
SignalTable = {001,
    [mc.ISIG_INPUT0] = function (on_off)
        if (on_off == 1) then
            mc.mcCntlCycleStart(inst)
        end
    end
}
SignalTable = {002,
    [mc.ISIG_INPUT1] = function (on_off)
        if (on_off == 1) then
            mc.mcCntlCycleStop(inst)
        end
    end
}
SignalTable = {003,
    [mc.ISIG_INPUT2] = function (on_off)
        if (on_off == 1) then
            mc.mcCntlReset(inst)
        end
    end
}
SignalTable = {004,
    [mc.ISIG_INPUT3] = function (on_off)
        if (on_off == 1) then
            mc.mcCntlFeedHold(inst)
        end
    end
}   
 ------------------------------------------------------------------------   

if (SignalTable[001] ~= nil) then
SignalTable[sig](state)
end

if (SignalTable[002] ~= nil) then
SignalTable[sig](state)
end

if (SignalTable[003] ~= nil) then
SignalTable[sig](state)
end

if (SignalTable[004] ~= nil) then
    SignalTable[sig](state)
end
Title: Re: Lua
Post by: joeaverage 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
Title: Re: Lua
Post by: dude1 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.
Title: Re: Lua
Post by: joeaverage 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
Title: Re: Lua
Post by: dude1 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
Title: Re: Lua
Post by: joeaverage 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
Title: Re: Lua
Post by: dude1 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.
Title: Re: Lua
Post by: joeaverage 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
Title: Re: Lua
Post by: dude1 on January 08, 2017, 12:06:54 AM
nup, being trolled is when you get nasty massages from A hole
Title: Re: Lua
Post by: CRS 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.
Title: Re: Lua
Post by: dude1 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
Title: Re: Lua
Post by: Chaoticone on January 18, 2017, 11:16:18 PM
Something like this?

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

if (percent < 50) then
    (percent = 50)
end

That wont take care of offset so you can take advantage of the the full sweep of the pot though.
Title: Re: Lua
Post by: DazTheGas on January 19, 2017, 03:14:20 PM
This should get you in the right direction, lua file attached.

https://youtu.be/3TeqRoT1pD4

One other thing though, I took a look at the link you sent me and looked at the code, I would be totally against putting "dofile(“C:\\Mach4Hobby\\PoKeys_analog_FRO.lua”)" in the PLC. dofile is just as it says (it runs the file) so this will be done at the speed of the PLC, so continuously reading the harddisk or cache and redeclareing the functions. I would seriously consider moving the functions to the screen load script and calling them from the PLC.

DazTheGas
Title: Re: Lua
Post by: CRS on January 19, 2017, 03:38:07 PM
Daz, how did you do that?  In the time it took me to go to the workshop and get a few things ready for today’s work, you manage to write all that stuff in Lua, set up the bread board and make a video!

I was still back on the question about the resolution of the pot....  You are a man of action.

Can you tell me how you know about Lua.  Did you learn on the go, or do you have a back ground in programming.

I have NO idea about it.  Can you tell me a good REALLY beginners way to learn it?  All the tutes start off assuming i know something and then hit over drive.

Thanks so so much for your time and explanation.  Hopefully I can return the favour one day.

Craig Smith
Title: Re: Lua
Post by: CRS on January 19, 2017, 03:41:19 PM
Hi Dude, hey check out Daz's response on this thread.  What a guy.  Already made a video and uploaded the Lua script to take care of it.  How did he do that?!

I have to go out and all I want to do it try it.  Let me know how it goes for you.

Craig.
Title: Re: Lua
Post by: DazTheGas on January 19, 2017, 03:45:07 PM
Arduino already setup for another project I am working on,

A real good way is to look at other code snippets and scripts, learn a few commands from the api and when they make sense the rest of the api will too.

DazTheGas

Title: Re: Lua
Post by: dude1 on January 19, 2017, 04:42:26 PM
That's why I said PM Daz he is very good,

What he has done is in the wrong langue for me to use My machine that will be run on M4 has all the codes done from Daz's help, years ago.
Title: Re: Lua
Post by: DazTheGas on January 19, 2017, 05:01:30 PM
Duuuuuude, nice to see you back here :-)

Daz
Title: Re: Lua
Post by: CRS on January 21, 2017, 07:24:52 PM
Hi DazTheGas,

I have to say I was pretty excited to get the Spin OVR happening, thanks again for you very impressive help.

All grown up and I still get that sinking feeling when things don’t quite go as planned.  Two days now of…. Hmmm what if I try this or that or this..etc..

I think I must be filling in one of the options incorrectly as the Spin OVR slider in Mach 4 is stuck hard to the bottom.  There are no errors when I run the Screen Load or PLC scrips.  Actually there is a nagging little error in the PLC, but that has been there since day one.

Firstly, the name of my controller, I know this should be obvious to me, but do you mean the PoKeys, the ESS or the screen set?  I have tried all three with no success.

I noticed that on your video, the name of the Screen Set is “machduino” which matched what you have in the table, however there are capitols in that line of code. “MachDuino”.  So perhaps your Arduino is called machduino. Confussion.  I am using screen set wx4.

The other thing is the name of the register. I assume that’s the name of the input when you look in the Register Diagnostics in Mach, which in my case is “Analog pin 42”.  So I think I got that right.

Like I said, no errors show up, but that slider sucker won’t move.  Any thoughts DTG? Sorry to harass on a Sunday.

Craig. (CRS)
Title: Re: Lua
Post by: DazTheGas on January 22, 2017, 07:29:02 AM
How very odd, ive just read the pokey`s manual and the analog is a value between 0v and 3.3v so I will have to redo the table to reflect this as the value its picking up is 0 so thinking you want only 50%

Take no notice of the screenset name thats just purely a name.

Quote
which in my case is “Analog pin 42”
that is the name of the register, if you look above this then you will have the controller name too, (take a look at page 32 of the pokeys manual at 2 pics at top)

Code: [Select]
local hreg = mc.mcRegGetHandle(inst, string.format("NAME OF CONTROLLER", "Analog pin 42"))
I will redo the table and repost.

PS what nag do you have in the PLC??

DazTheGas

Title: Re: Lua
Post by: CRS on January 22, 2017, 02:46:05 PM
DTG, when do you sleep?  Thanks again, I don't know what else to say, I'm starting to feel quite guilty.

I tried to look in the PoKeys manual, of which I seem to have a few versions, but couldn't see anything on any of the page 32s. (nice grammar)  Not to worry as I got what you are saying.  My controller is "PoKeys_25172" and Register name is "Analog input 42". (Not actually Analog pin 42)

I changed that in my SpindleOVR.lua file, but slider still stuck hard.  I thought it may come up a bit based on what you wrote about it thinking I only waned 50%, thought it may slide up a bit.  But no.

I should have mentioned about the 0 to 3.3 volts, for once I knew something and neglected to share.
I will wait for the modified table and see if that does it.  Can’t thank you enough for doing this.

The nag in the PLC.  This is the message I get when I run it:

Compilation successful!
Output: "C:\Users\Craig\AppData\Local\Temp\leEAAF.mcc"
Lua: Error while running chunk

It didn’t look life threatening, so I just continued on undeterred.  I will attach the PLC file. 2 infact as not sure which is the one.

Thanks again Daz.  Need any help with boatbuilding?  Epoxy Resin?  Fibre reinforcement? Just ask.

Craig.
Title: Re: Lua
Post by: CRS on January 22, 2017, 02:49:45 PM
And this one
Title: Re: Lua
Post by: Chaoticone on January 22, 2017, 06:00:09 PM
I think this should work.

local MinFRO = 50
local hReg = mc.mcRegGetHandle(inst, "NAME OF CONTROLLER/Analog pin 42")
local AnalogIn = mc.mcRegGetValue(hReg)

local FRO = ((AnalogIn * 30.303030) + MinFRO)
mc.mcSpindleSetOverride(inst, FRO)

This should give you a min of 50% and a max of 150% and use the whole sweep of the pot. It doesn't have any rounding in it so it won't jump but that could be a problem if Pokeys doesn't have any filtering (but I think they do).
Title: Re: Lua
Post by: Chaoticone on January 22, 2017, 06:31:00 PM
Ignore my last post until we know for sure what value that analog register will be at min and max.
Title: Re: Lua
Post by: CRS on January 22, 2017, 07:15:05 PM
Daz and Jo are having that discussion on another thread currently, but you may already know that, and hence your last post.

Chaoticone, just as a side topic.  You guys all seem to have quite a grasp of how to read, interpret and write the Lua code.  Of course I am a bit bias, but I don't think I am all that slow, but after having done a few hours of sitting and plodding through online Lua tutorials as well as reading the Mach scripting stuff etc., until my eyes go fuzzy, most of it still eludes me.

Did you start off that way, or did you have some background in programming which helped?
Title: Re: Lua
Post by: joeaverage on January 22, 2017, 08:02:15 PM
Hi Craig,
I'm a newbie at LUA coding. I have programmed before in FORTRAN as a student and in more recent years VB and C,
also like to think I'm pretty fair in assembler. LUA has been a bit of a challenge, while it is deceptively simple its power
and flexibility is right up there.

When coding I have
https://www.lua.org/pil/contents.html (https://www.lua.org/pil/contents.html) permanently on the taskbar.

Much of the code that we are trying to produce involves manipulating Mach4 and consequently the Mach 4 API.chm is also
on the taskbar.

The other resource are the existing LUA scripts within Mach4 and LUA Examples in the Mach4Hobby directory, often you can cut
and paste or otherwise adapt examples to your needs.

Craig
Title: Re: Lua
Post by: smurph on January 23, 2017, 02:11:22 AM
The max value of the analog input is determined by the ADC used on the hardware. 

10 bit == 0 to 1023 for a 1024 count range
12 bit == 0 to 4095 for a 4096 count range
14 bit == 0 to 16383 (16384 range)
16 bit == 0 to 65535 (65536 range)

This is called the ADC resolution.  To get the analog input value as a percentage, just divide the value by the total range. 

percent = value / range

For example, if the ADC is 10 bit and the value is 512, then:

512/1024 == .5 (or 50%).

Now you can play with some numbers to arrive at solutions to common problems.  For example (and just for kicks), say you want to know the voltage present at the ADC given its' integer value.  For the above value of 512 that yields 50% and a VCC voltage of 3.3v:

3.3 * .5 == 1.65v. 

So going back to main problem at hand, we now want to know what 50% of a range is.  Say 50 to 150.  The formula for that is:

val = (percent * (max - min)) + min

Where percent is a decimal representation of a percentage.  e.g. .5 for 50%

(.5 * (150 - 50)) + 50 == 100.

Steve
Title: Re: Lua
Post by: Chaoticone on January 23, 2017, 09:11:18 AM
Daz and Jo are having that discussion on another thread currently, but you may already know that, and hence your last post.

Chaoticone, just as a side topic.  You guys all seem to have quite a grasp of how to read, interpret and write the Lua code.  Of course I am a bit bias, but I don't think I am all that slow, but after having done a few hours of sitting and plodding through online Lua tutorials as well as reading the Mach scripting stuff etc., until my eyes go fuzzy, most of it still eludes me.

Did you start off that way, or did you have some background in programming which helped?


Hello Craig,

Yup, I started off that way and still am at times.  ;D I had no background in coding or scripting other than doing some in cypress enable in Mach3. It took me a while to get my head around the few things I think I understand now. For me it boils down to shear determination when it comes to coding. I also have some great mentors that give me a bump when I get stuck. Most all of them are here on the forum. A few other things I consider advantages though are I worked on CNC's for 25 years and all sorts of mechanical, wiring, electronics, and processor based controls for many more years. That helps a lot in knowing how I want things to work. I just have to throw myself into the script until I get it to do that. So yes, lua has been a steep learning curve to me but it really is powerful and many times more powerful than the scripting we had in Mach3. This means we can do things in 4 not possible in 3.  :)

The tips I would give anyone diving in would be to simply quote Craig. He is spot on.

Quote
Hi Craig,
I'm a newbie at LUA coding. I have programmed before in FORTRAN as a student and in more recent years VB and C,
also like to think I'm pretty fair in assembler. LUA has been a bit of a challenge, while it is deceptively simple its power
and flexibility is right up there.

When coding I have
https://www.lua.org/pil/contents.html permanently on the taskbar.

Much of the code that we are trying to produce involves manipulating Mach4 and consequently the Mach 4 API.chm is also
on the taskbar.

The other resource are the existing LUA scripts within Mach4 and LUA Examples in the Mach4Hobby directory, often you can cut
and paste or otherwise adapt examples to your needs.

Craig

I do have a few additional links to share though.

http://www.lua.org/manual/5.2/
http://www.lua.org/pil/contents.html#contents
http://wxlua.sourceforge.net/docs/wxluaref.html
http://docs.wxwidgets.org/3.0.1/
https://www.youtube.com/watch?v=iMacxZQMPXs&index=5&list=PLFJceKphr7ePmkYA1FmagKRPafFiEsnQU&t=1648s

And I cannot overstress how helpful the Mach4API help file is and the existing scripts. Craig said it, I just want to emphasise it.  
Title: Re: Lua
Post by: CRS on January 24, 2017, 04:11:17 PM
Hi Guys, I want to thank, and in no order,

dude1, joeaverage, DazTheGas, Chaoticone & smurf.  You guys have all taken time to try and help with this perplexing task of expressing a percentage range for the Spindle Override.  I was starting to get somewhat overwhelmed by all the different options, most of which I don't understand, but thanks for taking the time and for all the tips and links to learning resources.

I contacted Matevž at PoLabs, which I had been reluctant to do, as I had harassed him quite a lot already about the PoKeys57E.  Anyway, as always he graciously came back with this.

Hi, Craig

Although the analog inputs are 12-bit, the analog input registers contain a value between 0 and 1.
To get the 50-150% range, simply use
local percent = 50 + 100 * analog


I don't think he was aware that for "mcSpindleSetOverride" the percentage must be expressed as a decimal.  So this works perfectly.

local percent = 0.5 + 1.0 * analog.  Now the slider obeys my every command.

This question goes out to DazTheGas.  Even though both override sliders are now working well, there is a slight amount of shuddering or dithering happening with both sliders.  They are not completely still.  Only small, but it's there.
You mentioned that you weren't keen on calling the lua file from the PLC script, do you think this could be causing the small fluctuations?  How would you take the same info that Matevž has given for the FRO and Spin OVR and perhaps follow the same method that you first applied but without using the table?

Thanks again guys, you have all been great and I hope that others can use this as well.

Craig.
Title: Re: Lua
Post by: DazTheGas on January 24, 2017, 04:18:32 PM
Yay glad your sorted. ;-)

The reason I rounded the number and used a table is it stops unwanted interferance or noise from changeing the spindle speed.

DazTheGas
Title: Re: Lua
Post by: Chaoticone on January 24, 2017, 04:28:40 PM
Yup, the dither is going to be there without filtering of some sort. You only want it to change the feedrate if the analog input changes by a certain amount (which you can do in code as Daz did with the table) or there could be filtering in the pokeys for anolog inputs (which I think there is). Filtering the input would be much cleaner and what you have already will work just fine with it.
Title: Re: Lua
Post by: DazTheGas on January 24, 2017, 04:38:08 PM
The way it works with the plugin ive been writing for what seems an age is it does the filtering before it sends, in the video i posted you will notice that the tx light only goes on when I move the pot. this is done in a simple form of

Code: [Select]
  pot = analogRead(potPin);

  if (potb > pot + 2 || potb < pot - 2)
  {
    Serial.Send(pot)
    potb = pot;
    delay(50);
  }

so any fluctuations from the pot etc get ignored.

This can be done with lua also in an IF statement in the plc to compare the new value with the last value and if its greater than or less than a certain percentage then set spindle to new value.

DazTheGas
Title: Re: Lua
Post by: CRS on January 25, 2017, 04:26:47 AM
Hi DazTheGas, the PoKeys does have a filtering facility.  The down side is the reaction time of the override slider becomes a bit sluggish.  But the fluctuations are a lot better.  Even when I run the 180VDC spindle, it's not bad.  I think good enough to leave for now.

But having said that, I just had to try your table idea again.

Redid the numbers so the left column went from 0 to 1 in 0.01 increments.  The right column stayed the same 0.5 to 1.5. All looked good and no errors.  But I really am just playing Tattslotto and hoping for the best.  Wasn't sure how to reword the rounding factor.  So I just replaced 10 with 0.01.  Hmm that didn't do it.  I will keep playing with that, but for now, slider still glued to the bottom.

What you said about filtering before it sends makes sense when I read it, but remember you are talking to a Lua illiterate, so I think time to move on to the next thing.  I'm sure you'll be hearing from me.

Thanks Daz, have another biccy on me.

Craig