Hello Guest it is March 28, 2024, 09:15:11 AM

Author Topic: Macros for lathe spindle range change  (Read 1958 times)

0 Members and 1 Guest are viewing this topic.

Macros for lathe spindle range change
« on: July 24, 2017, 08:56:33 AM »
Hello All
Is there a macro that has been written that would allow me to enter a m41- m46 to change my lathe spindle range in a g code program for mach4
Thanks

Offline GerdS

*
  •  42 42
    • View Profile
Re: Macros for lathe spindle range change
« Reply #1 on: August 28, 2017, 04:53:24 PM »
Just read your post.

The macro below does this job for me.
I have defined the spindle range (6 gears) in solidcam and the post pocessor call the macro on tool change.

M101 P <Range>

Hope that helps.

function M101(hVars)
   local inst = mc.mcGetInstance()
   local CurrentSpindleRange = mc.mcSpindleGetCurrentRange(inst)
   local NextSpindleRange = mc.mcCntlGetLocalVar(inst, hVars, mc.SV_P)
   
   mc.mcCntlSetLastError(inst, "Sets spindle range to " ..NextSpindleRange)
   if CurrentSpindleRange ~= NextSpindleRange then
      wx.wxMessageBox("Change Gear to " ..NextSpindleRange, "Gear box")
      rc = mc.mcSpindleSetRange(inst, NextSpindleRange)
   end
end

if (mc.mcInEditor() == 1) then
   M101()
end
Re: Macros for lathe spindle range change
« Reply #2 on: September 12, 2018, 03:33:13 AM »
Hi,
Can you explain me better please?
I have to do something similar too.
I do an example:
range 1 (from 0 to 500 rpm)
range 2 (from 0 to 1000 rpm)
range 3 (from 0 to 1500 rpm)

how should i do?
thank you!

Offline smurph

*
  • *
  •  1,544 1,544
  • "That there... that's an RV."
    • View Profile
Re: Macros for lathe spindle range change
« Reply #3 on: September 15, 2018, 02:22:03 AM »
A lot of machines use M40 and up to set the range.  For example, M40, M41, and M42 for a three range spindle.  All of those M code macro script would be implemented like GerdS example with the exception of no input parameter.  The range would be hard coded. 

Steve
Re: Macros for lathe spindle range change
« Reply #4 on: September 15, 2018, 11:32:23 PM »
I'm curious how the "P" Value is read.  I didn't know you could add a "P" value to a macro line.  

The code that GerdS posted doesn't do anything when I test it.  I'm just running the sim on my laptop.
Chad Byrd
Re: Macros for lathe spindle range change
« Reply #5 on: September 15, 2018, 11:36:14 PM »
Okay,
I noticed that GerdS had the "M" capitalized in the macro.  I made it lower cased and it works now.  

Still, What does the P Value do in macros, it can be read by the (hVars) on a macro line?

What other things do people use this for?
« Last Edit: September 15, 2018, 11:41:43 PM by Cbyrdtopper »
Chad Byrd

Offline smurph

*
  • *
  •  1,544 1,544
  • "That there... that's an RV."
    • View Profile
Re: Macros for lathe spindle range change
« Reply #6 on: September 16, 2018, 12:27:02 AM »
local NextSpindleRange = mc.mcCntlGetLocalVar(inst, hVars, mc.SV_P)

That line pulls the P word from the G code line.  It will be hard to debug in the editor since you can't really execute a G code line.  There are also these related functions available:

flag, rc = mc.mcCntlGetLocalVarFlag(number inst, number hVars, number varNumber)  -- This one tels you if the G code word was set on the G code line.  e.g.  "M101 P1"

Code: [Select]
local flag
flag, rc = mc.mcCntlGetLocalVarFlag(inst, hVars, mc.SV_P)  -- flag would equal 1 because P1 was specified on the G code line.
flag, rc = mc.mcCntlGetLocalVarFlag(inst, hVars, mc.SV_Q)  -- flag would equal 0 because Q was specified on the G code line.

comment, rc = mc.mcCntlGetLocalComment(number inst, number hVars) -- retrieve the comment on the G code line that contains the M code. 

As I said earlier, it is hard to debug these things because no G code line has been executed when debugging in the script editor.  So I put in a little helper API function that will simulate it.  :)

hVars, rc = mc.mcCntlCreateLocalVars(number inst, string gCodeLine)

It would be used in the function call stub like this:

Code: [Select]
if (mc.mcInEditor() == 1) then
   -- The function call stub is only ever executed when in the editor debugging, so it is safe to leave this in for testing.
   local inst = mc.mcGetInstance()
   local hVars, rc
   hVars, rc = mc.mcCntlCreateLocalVars(number inst, string gCodeLine)
   M101(hVars)
end

There are caveats to passung G code words to M codes.  One is you can't use the M word as a variable.  You can use a word more than once.  And finally, the M code that takes variables has to be the only thing on the line. 

Steve