Hello Guest it is March 28, 2024, 02:27:32 PM

Author Topic: change motor counts with lua  (Read 3445 times)

0 Members and 1 Guest are viewing this topic.

change motor counts with lua
« on: June 08, 2016, 02:35:11 PM »
I'm trying to change a motor count value with a lua scripted M code.

This code works to change the value but it doesn't seem to make a difference in how the motor runs until I disable and actually open the Mach settings (Configure menu > Mach). I've tested and just disabling and enabling the control does not do anything. To get it to work I have to disable, open the settings, hit cancel, and then enable.

Code: [Select]
function m108()
   inst = mc.mcGetInstance()
   local val = tostring(1000)
   mc.mcProfileWriteString(inst, "Motor0", "CountsPerUnit", val)
end

Any ideas or solutions?
Re: change motor counts with lua
« Reply #1 on: June 08, 2016, 09:48:35 PM »
First, why not use mc.mcProfileWriteInt() instead of converting to string and then writing the string?

Try (maybe) mc.mcProfileSave(inst) and (definitely) mc.mcProfileReload(inst) after you write to the INI file.  This function is only available on Mach4 build 2914 and higher.  See the Mach4CoreAPI.chm file in the Mach4Hobby\Doc directory.

I'm not sure if you need the mcProfileSave().  In older Mach4 builds calling any of the mcProfileWrite*********() functions automatically updated the file to disk.  But in Mach4 build 2914 I don't remember if that is still the case.  This changed in build 2872/2914 when the reload call was added.  So the sequence would be:
Code: [Select]
function m108()
   inst = mc.mcGetInstance()
   -- local val = tostring(1000)
   -- mc.mcProfileWriteString(inst, "Motor0", "CountsPerUnit", val)
   mc.mcProfileWriteInt(inst, "Motor0", "CountsPerUnit", 1000 )

   -- mc.mcProfileSave(inst)  -- Uncomment this if just the "reload" by itself doesn't work
   mc.mcProfileReload(inst)
end

Bob
Re: change motor counts with lua
« Reply #2 on: June 09, 2016, 09:29:57 AM »
First, why not use mc.mcProfileWriteInt() instead of converting to string and then writing the string?

Yes that is the right way to do that. I'm very new to lua and still learning.

I'll try the mc.mcProfileSave(inst) and mc.mcProfileReload(inst) and see if that works. I'll report back with results.

Thanks for your help!
-Joel
Re: change motor counts with lua
« Reply #3 on: June 09, 2016, 10:32:19 AM »
No Luck.

For testing, here is the code I have:
Code: [Select]
fuction m108()
   inst=mc.mcGetInstance()
   mc.mcProfileWriteInt(inst, "Motor0", "CountsPerUnit", 800)
   mc.mcProfileSave(inst)
   mc.mcProfileReload(inst)
end

fuction m110()
   inst=mc.mcGetInstance()
   mc.mcProfileWriteInt(inst, "Motor0", "CountsPerUnit", 1600)
   mc.mcProfileSave(inst)
   mc.mcProfileReload(inst)
end

then I run:

Code: [Select]
M108
G91G1X1F30
M0
M110
X1F30
M30

My motors are currently set up at 800 counts per rev so I would expect that code to do 1 revolution after the M108 and then 2 revolutions after the M110. The code seems to update the value but I don't see any actual change in the motor revolutions until I open the settings.
Re: change motor counts with lua
« Reply #4 on: June 09, 2016, 11:48:29 AM »
First - did you "copy and paste" your code into your previous post, or did you type it in by hand?  I ask because the word "function" is misspelled in both places.

Ahhh... I missed a small note in the mcProfileReload description - that function can only be called when Mach4 is in the "idle" state so it may be that Mach4 ust be disabled in order for that function call to work.

I tried the m108 script here real quick and I was unable to see any change from it.  The INI file never changed and when I looked in the Configure->Mach dialog, then motor tuning params did not change.  I'm afraid I can't be of much more help. Let's see if anyone else can chime in.

Though I have to ask...  Why are  you trying to change the motor tuning parameters on the fly?

Bob
Re: change motor counts with lua
« Reply #5 on: June 09, 2016, 12:00:54 PM »
Yes I retyped it. I checked and it is correct on the PC running Mach4.

We are trying to do this because I am making a tube plasma and I must be able to do arcs in the Z and C axis and Mach doesn't support cylindrical interpolation. Therefore, I have the machine set up as a XYZ machine and I'm just changing the motor counts based on the tube diameter. I can make it work by just closing and reopening Mach and selecting a different profile, but I was hoping to make it work with less user interaction (just an M code).
Re: change motor counts with lua
« Reply #6 on: June 09, 2016, 12:59:57 PM »
Looking through the Mach4 Core API I found mcMotorGetInfoStruct and mcMotorSetInfoStruct, but they only have C/C++ syntax listed and no LUA syntax. Can I accomplish this with C/C++ instead?

Offline DazTheGas

*
  •  778 778
  • DazTheGas
    • View Profile
Re: change motor counts with lua
« Reply #7 on: June 09, 2016, 02:21:53 PM »
The series of events need to be in a certain order and can only be done in an idle state so you will prob need to run mc.mcCntlEnable(inst, 0) before writing to the ini, then re-enable with mc.mcCntlEnable(inst, 1)

Code: [Select]
local inst = mc.mcGetInstance()
mc.mcCntlEnable(inst, 0) -- disable controller
mc.mcProfileWriteInt(inst, "Motor0", "CountsPerUnit", 1000 ) -- what we require in our ini
mc.mcProfileFlush(inst) -- force the changes to be written
mc.mcProfileReload(inst) -- reload the changes
mc.mcCntlEnable(inst, 1) -- re - enable controller

Although the above code works fine, there are other factors to take into account like the motion controller will need to know that your settings have changed  ( take a look at mcCntlConfigStart and mcCntlConfigStop ) about this.

DazTheGas
New For 2022 - Instagram: dazthegas
Re: change motor counts with lua
« Reply #8 on: June 11, 2016, 08:32:02 AM »
Yes I retyped it. I checked and it is correct on the PC running Mach4.

We are trying to do this because I am making a tube plasma and I must be able to do arcs in the Z and C axis and Mach doesn't support cylindrical interpolation. Therefore, I have the machine set up as a XYZ machine and I'm just changing the motor counts based on the tube diameter. I can make it work by just closing and reopening Mach and selecting a different profile, but I was hoping to make it work with less user interaction (just an M code).

Why do you need to do arcs?  Why wouldn't a segmented line arc do the same think.  We tried it both ways in M3 and cutting the tube with the rotary axis set up as linear was a pain.  Too much for the operator to do to make it work correctly.  If you set up the rotary axis as angular and post angular code, you don't have to change anything in Mach.

Also, when you set the rotary axis as angular, the tool path display shows a rotating tube during the cut.  I've had a number of people ask which controller software we use because "Mach3 is not capable of that".  If you treat the tube like a flat sheet in Mach it will depict a flat sheet in the TP display.