Hello Guest it is January 29, 2020, 04:36:31 AM

Author Topic: Lua  (Read 8480 times)

0 Members and 1 Guest are viewing this topic.

Offline CRS

*
  •  61 61
    • View Profile
Re: Lua
« Reply #40 on: January 22, 2017, 02:49:45 PM »
And this one

Offline Chaoticone

*
  • *
  •  5,634 5,634
  • Precision Chaos
    • View Profile
Re: Lua
« Reply #41 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).
;D If you could see the things I have in my head, you would be laughing too. ;D

My guard dog is not what you need to worry about!

Offline Chaoticone

*
  • *
  •  5,634 5,634
  • Precision Chaos
    • View Profile
Re: Lua
« Reply #42 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.
;D If you could see the things I have in my head, you would be laughing too. ;D

My guard dog is not what you need to worry about!

Offline CRS

*
  •  61 61
    • View Profile
Re: Lua
« Reply #43 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?
Re: Lua
« Reply #44 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 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
My wife left with my best friend...
     and I miss him!

Offline smurph

*
  • *
  •  1,187 1,187
  • "That there... that's an RV."
    • View Profile
Re: Lua
« Reply #45 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

Offline Chaoticone

*
  • *
  •  5,634 5,634
  • Precision Chaos
    • View Profile
Re: Lua
« Reply #46 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.  
;D If you could see the things I have in my head, you would be laughing too. ;D

My guard dog is not what you need to worry about!

Offline CRS

*
  •  61 61
    • View Profile
Re: Lua
« Reply #47 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.

Offline DazTheGas

*
  •  752 752
  • DazTheGas
    • View Profile
Re: Lua
« Reply #48 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
« Last Edit: January 24, 2017, 04:21:09 PM by DazTheGas »
Theres always time for a choccy biccy

Offline Chaoticone

*
  • *
  •  5,634 5,634
  • Precision Chaos
    • View Profile
Re: Lua
« Reply #49 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.
;D If you could see the things I have in my head, you would be laughing too. ;D

My guard dog is not what you need to worry about!