Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: daniba73 on January 13, 2020, 04:33:35 AM

Title: Mechanical speed ranges in Mach4
Post by: daniba73 on January 13, 2020, 04:33:35 AM
hello
is it possible to insert a command in the gcode to change the mechanical speed range at the software level?
I'll explain:
having lathe with ranges, I would do the mechanical movement.
so I thought about this sequence:
M5
M0 (here I change mechanical range)
...... (code that changes my scale)
M3 G97 S ...
Title: Re: Mechanical speed ranges in Mach4
Post by: rhtuttle on January 13, 2020, 09:40:44 AM
If I understand you correctly here is a code snippet that I use in a macro:

  if mc.mcSpindleGetCurrentRange(inst)~=1 then
    wx.wxMessageBox('Set Spindle Gearing to High Speed')
    mc.mcSpindleSetRange(inst,1)
  end

HTH

RT
Title: Re: Mechanical speed ranges in Mach4
Post by: daniba73 on January 13, 2020, 10:07:36 AM
do I have to build every macro for every range?
example
M100 (equivalent to range 1)
M101 (equivalent to range 2)
did I get it right?
Title: Re: Mechanical speed ranges in Mach4
Post by: rhtuttle on January 13, 2020, 10:09:24 AM
You could pass the range you want as a parameter to the macro: m100 p2
Title: Re: Mechanical speed ranges in Mach4
Post by: daniba73 on January 13, 2020, 10:14:41 AM
ok I understand now.
I try as soon as possible.
I'll let you know!
thank you!
Title: Re: Mechanical speed ranges in Mach4
Post by: smurph on January 13, 2020, 04:14:08 PM
Typically, spindle speed ranges are in the M40-M46 range of M codes.  These M codes are just general as not all machines will have multiple speed spindles.  Some may have 2, 3, 4 or more.   The Machine Tool Builder (you) will have to implement them because of the differing methods of control and number of ranges implemented are too varied to have canned M codes for this purpose. 

Code: [Select]
function m40()
local inst = mc.mcGetInstance("Spindle range 0")
local rc
local currentRange
currentRange, rc = mc.mcSpindleGetCurrentRange(inst)
if (currentRange == 0) then
return -- nothing to do!
end
-- Do the machine dependent range change control.
--
-- End machine dependent range change control

-- Next, tell Mach what rage we are now in IF the above is successful. 
mc.mcSpindleSetRage(inst, 0)
end

if (mc.mcInEditor() == 1) then
    m40()
end

Rinse and repeat for all of your spindle speed ranges. 

Steve
Title: Re: Mechanical speed ranges in Mach4
Post by: daniba73 on January 13, 2020, 05:51:27 PM
I don't know where I'm wrong, but it doesn't work in either of the two examples you gave me.
Title: Re: Mechanical speed ranges in Mach4
Post by: rhtuttle on January 13, 2020, 06:02:52 PM
Can't help if you don't post the macro code you are using.
Title: Re: Mechanical speed ranges in Mach4
Post by: daniba73 on January 13, 2020, 06:19:41 PM
I just created a macro that I called "m50"
inside I copied what you wrote.
then I tried to write the m50 macro inside a gcode, but it was ignored.

later I tried to replace the internal macro m50 with the second example, but nothing changes.

I underline that I was testing it on the laptop in demo version, I didn't have time to go to the laboratory to try the lathe where I activated mach4 hobby with license.
Title: Re: Mechanical speed ranges in Mach4
Post by: smurph on January 13, 2020, 07:49:34 PM
1) Why are you calling the macro m50?  M40-M46 are the typical M codes used for spindle speed ranges. 
2) When you create an M code macro script, use lower case names for both the file AND the function inside it. 

"m50.mcs" is correct, while "M50.mcs" is not correct for a file name.

function m50()
...
end

is correct and

function M50()
...
end

is NOT correct. 

The FIRST thing you should do is take the example someone gives you and try it like it is.  And there is no guarantee that script people throw up on the forum will work because a lot of the time the people trying to help are putting "the general idea" up to help you on your way.  The rest is an exercise for the user (you).  :)  But anyway, try it like the example was and get things working.  THEN you can start renaming the macro script and function inside it.

When working with the script in the editor, it is always a good idea to try and compile it.  Because compiling it in the editor will show you is there are any syntax errors.  It could be as simple as a misspelled function call from a typo in the example. 

So in summary, if a script is "being ignored", it is most likely because of:

1. Not using lower case names for functions and file names. 
2. Not checking for syntax errors by compiling the macro script first. 

Steve
Title: Re: Mechanical speed ranges in Mach4
Post by: daniba73 on January 14, 2020, 02:22:02 AM
yes, I forgot to write that I tried both ways.
I put the m40.mcs macros in the folder:
C: \ Mach4Hobby \ Profiles \ Mach4Lathe \ Macros

then I wrote this little gcode test.

%
M5
M40
T101
M30
;

M40 is ignored, does not change the scale.
Title: Re: Mechanical speed ranges in Mach4
Post by: daniba73 on January 14, 2020, 03:51:54 PM
I forgot something?
or did I make a mistake?
Title: Re: Mechanical speed ranges in Mach4
Post by: daniba73 on January 15, 2020, 03:29:53 AM
not to misunderstand, I would like to change this.
look at the picture.
Title: Re: Mechanical speed ranges in Mach4
Post by: rhtuttle on January 15, 2020, 11:05:20 AM
daniba73,  you're not going to get much help if you don't follow the advice as given.  You say that you tried both suggestions and they didn't work but did not provide what you 'actually' did.  Attach the macro file that you tried and the gcode that you tried as well.

RT
Title: Re: Mechanical speed ranges in Mach4
Post by: daniba73 on January 15, 2020, 06:18:49 PM
ok, this is the gcode:
%
M5
M40
G97 S1000 M3
T101
G0 X100 Z10
G1 Z-20 G98 F100
G0 X150 Z10
M5
M42
G97 S1000 M3
T202
G0 X98 Z10
G1 Z-20 G98 F100
G0 X150 Z10
M5
M30
;
Title: Re: Mechanical speed ranges in Mach4
Post by: joeaverage on January 15, 2020, 06:51:53 PM
Hi,
you've seen Smurph's recommendation about using lowercase in Gcode......is there any reason you ignore his advice?

Craig
Title: Re: Mechanical speed ranges in Mach4
Post by: rhtuttle on January 15, 2020, 07:15:49 PM
function m42()
   local inst = mc.mcGetInstance("Spindle range 2")
   local rc
   local currentRange
   currentRange, rc = mc.mcSpindleGetCurrentRange(inst)
   if (currentRange == 2) then
      return -- nothing to do!
   end
   -- Do the machine dependent range change control.
   --
   -- End machine dependent range change control

   -- Next, tell Mach what rage we are now in IF the above is successful.
   mc.mcSpindleSetRage(inst, 2) <----Should be: mc.mcSpindleSetRange(inst, 2)
end

if (mc.mcInEditor() == 2) then (to debug it should be: if (mc.mcInEditor() == 1) then
    m42()
end
Title: Re: Mechanical speed ranges in Mach4
Post by: smurph on January 15, 2020, 07:31:21 PM
Hi,
you've seen Smurph's recommendation about using lowercase in Gcode......is there any reason you ignore his advice?

Craig

I think there is a misunderstanding.  You don't have to call the m40.mcs macro script with lower case in G code.  But you do have to make the macro script filename lower case.  And the function name in the script should be lower case as well.

The first thing the interpreter does is lower case everything.  So if you had a line wit "M40" on it, when the interpreter processed that line it would do:

1. M40 -> m40  (lower case the M40 to m40)
2. Look for m40.mcs (notice the lower case filename).
3. Look for function m40() in the m40.mcs file (notice the lower case function name).

Steve
Title: Re: Mechanical speed ranges in Mach4
Post by: daniba73 on January 16, 2020, 04:38:31 AM
I found and solved the problem, it was a writing error in the m40 macro that you had loaded.
this is correct:

function m40()
   local inst = mc.mcGetInstance("Spindle range 0")
   local rc
   local currentRange
   currentRange, rc = mc.mcSpindleGetCurrentRange(inst)
   if (currentRange == 0) then
      return -- nothing to do!
   end
   -- Do the machine dependent range change control.
   --
   -- End machine dependent range change control

   -- Next, tell Mach what rage we are now in IF the above is successful.
   mc.mcSpindleSetRange(inst, 0)
end

if (mc.mcInEditor() == 1) then
    m40()
end




P.S in gdode and MDI it is recognized and applied whether you use uppercase or lowercase letters.
thank you so much!! you are great!!
Title: Re: Mechanical speed ranges in Mach4
Post by: daniba73 on January 16, 2020, 11:51:02 AM
Is it possible to associate an output to the macro to activate a light bulb?
Title: Re: Mechanical speed ranges in Mach4
Post by: daniba73 on January 17, 2020, 08:20:12 AM
if it were possible, I would do something similar in the image