Hello Guest it is November 14, 2019, 01:57:13 AM

Author Topic: m3 gets special treatment?  (Read 1188 times)

0 Members and 1 Guest are viewing this topic.

m3 gets special treatment?
« on: November 19, 2017, 06:21:05 PM »
Here is the code for two macros, identical except one is the  m3.mcs file and the other is m300.mcs file code

If the 'm3 s500' is called from the mdi hParam == nil, if the 'm300 s500' is called from the mdi hParam ~= nil.
Does m3 get stripped of it's parameters?  If so why and how do you capture the requested s value?

function m3(hParam)
   if (hParam ~= nil) then
      wx.wxMessageBox('hParam~=nil')
   else
      wx.wxMessageBox('hParam==nil')
   end   

end

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

function m300(hParam)
   if (hParam ~= nil) then
      wx.wxMessageBox('hParam~=nil')
   else
      wx.wxMessageBox('hParam==nil')
   end   

end

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


tia

RT

Offline Chaoticone

*
  • *
  •  5,628 5,628
  • Precision Chaos
    • View Profile
Re: m3 gets special treatment?
« Reply #1 on: November 19, 2017, 09:32:19 PM »
Close Mach and delete any mcc or backup files in your C:\Mach4\Profiles\ProfileName\Macros folder and retest. This will force the lua chunk to be recompiled and your edits should take effect then.
;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!
Re: m3 gets special treatment?
« Reply #2 on: November 20, 2017, 02:00:50 PM »
deleted both the m3.mcc and the m3.mcs.bak

loaded Mach4 and in the mdi ran m3 s1000 and hParam still comes back nil: 'hParam==nil'



Offline smurph

*
  • *
  •  1,149 1,149
  • "That there... that's an RV."
    • View Profile
Re: m3 gets special treatment?
« Reply #3 on: November 20, 2017, 02:27:43 PM »
The "reserved" M codes will NOT take parameters.  Only user defined macros will.  M3, M4, M5, M6, etc...  are all defined to have a specific function in G code.  Thus, they don't do anything other than what they were meant to do.  Contrst that to say a M code that you create called M110.  The M110 macro scrip can then have parameters and hParam will not be nil. 

If you want to do something special with the S word, use spindlespeed.msc.  The parameter is the value of the S word.  So 1000 for your example. 

Here is an example:

Code: [Select]
function spindlespeed(rpm)
inst = mc.mcGetInstance();
CommandRPM = mc.mcSpindleGetCommandRPM(inst);
range = mc.mcSpindleGetCurrentRange(inst);
MaxRPM = mc.mcSpindleGetMaxRPM(inst, range);
mc.mcCntlSetLastError(inst, "SpindleSpeed! " .. tostring(CommandRPM) .. " RPM");

if MaxRPM == 0 then
mc.mcCntlSetLastError(inst, "MaxRPM is zero!  Please set up the spindle.");
else
--Get the hReg handle of the modbus analog register
hReg = mc.mcRegGetHandle(inst, "modbus0/mboreg1");
if hReg == 0 then
--mc.mcCntlSetLastError(inst, "Register not found!.");
            return;
else
--Set the analog voltage
analogval = (CommandRPM/MaxRPM) * 4096;
mc.mcRegSetValue(hReg, analogval);
end
end
end

if (mc.mcInEditor() == 1) then
    spindlespeed();
end

Steve
Re: m3 gets special treatment?
« Reply #4 on: November 20, 2017, 03:22:44 PM »
Thanks Steve,

I didn't get that impression from the scripting manual:

M codes in the column “User Scriptable And Internal Function” have internal functions in Mach4, but also allow user scripts. These codes are further divided into those that call the function internal to Mach4 OR a user script and those that run both. M3 to M5 and M7 to M9 are codes that control the spindle and coolant functions. If there is no user script for these codes they turn on/off their respective signals as defined inside Mach4. However, if a user script is present, the script will be run instead. This gives the user the power to create custom codes for custom spindle and coolant applications, but if the machine simply needs to turn on/off an output, no programming is required.

Might consider updating those to indicate that they will not allow parameters.

Thanks for the example but I need to know when the spindle speed has been requested to change or changed.  It appeared (with a cursory glance) that I could use the PGM RPM dro's onUpdateScript event to capture the value but after testing, the value returned is the one before the update.

Do I write a function that loops every 200 milliseconds and checks to see if that value changes?  Is there a better way?

TIA

RT

Offline smurph

*
  • *
  •  1,149 1,149
  • "That there... that's an RV."
    • View Profile
Re: m3 gets special treatment?
« Reply #5 on: November 20, 2017, 05:31:40 PM »
That spindle speed script gets called whenever there is a S word in G code or when the spindle is changed via a screen or panel control.  I believe it will do what you want.  It was originally conceived to allow external spindle controls (not on the motion device) like MODBUS work well. 

Steve
Re: m3 gets special treatment?
« Reply #6 on: November 21, 2017, 09:02:26 AM »
Many thanks for that, it works both with gcodes and mdi input.  I have been able to develop a hack like mach3's linearity.dat and can now get my spindle speed to within a couple of revs from what it is supposed to be.  If I requested s500 previously it would spin at 450 and s2000 would spin at 2170.  Unfortunately the new commandRPM displays not 500 but the 553 needed to get 500.

I can see how the modbus would be an excellent route but my  little board does not support it.

So it begs the question, who sets the voltage level? Does mach set a register value that vendors read or do the motion control vendors read the spindle gear table values and derive the amount of current?

Offline smurph

*
  • *
  •  1,149 1,149
  • "That there... that's an RV."
    • View Profile
Re: m3 gets special treatment?
« Reply #7 on: November 21, 2017, 12:17:24 PM »
The voltage would be from the device that drives the spindle, most likely a plugin.  All Mach does is drive a number (whatever S or screen controls say).  The RPM of the spindle is what Mach is concerned with.  It really doesn't care how that is driven.  Could be a voltage, or air pressure, or hydraulic, etc... 

In the case of a motor driven spindle, there may (or may not) be some gear ratio involved.  This is defined on the spindles tab in the configurations dialog.  Hence we have the notion of the commanded spindle RPM and the notion of the motor RPM that is required to produce the commanded spindle RPM.  Ultimately, the motor RPM is what is needed to be controlled.  Devices that control spindles (typically from a plugin) have access to all of this information and can use it as it needs to.  Typically, it is a pretty simple calculation.

Whenever the commanded RPM is changed (by S word or screen/control panel), the plugin will receive a message telling it that the spindle has changed.  The spindlespeed.mcs script is also called at this time.  Anyway, the commanded RPM is given to both the spindlespeed.msc script and the plugin.

As an example, say the spindle motor is controlled by an analog voltage from 0 to 10v and that there are two spindle ranges defined.  Range 1 is 0 to 2000 RPM and range 2 is 0 to 5000 RPM.  To start with, say range 1 is selected and a spindle RPM of 1000 is commanded.  The plugin would then get a spindle changed message with 1000 as the rpm parameter and do:

1. RPM 1000 is specified
2. Determine which range is selected (1)
2. Determine the maximum RPM of the selected range (2000)

In this case, an output voltage of 0v would be 0 RPM and an output of 10v would be 2000 RPM as defined by the spindle range 1.

3. Determine the linear ratio of volts to RPM (1000/2000 == .5)
4. Apply the ratio to the maximum voltage of 10 (10 * .5 == 5v)
5. Output 5v to the VFD.

Notice that step three applies a LINEAR ratio.  And that handles 99% of the VFDs out there.  But what if 5v doesn't actually run the spindle at 1000 RPM?  Well, that is when you need to provide some sort of spindle RPM feedback.  And then the plugin also needs to be able to apply that feedback.  Not all plugins will operate the same.  For instance, the Galil plugin offers two types of spindle control.  1) Spindle without feedback and 2) spindle with encoder feedback.  If using the first method, it uses that linear scaling of voltage that I described above.  Basically, you get whatever RPM 5v produced no matter what.  If using the second method with feedback, the Galil plugin will offset the voltage by whatever amount if required to achieve the desired commanded RPM. 

To summarize, and to answer your question directly, the plugin that controls the spindle "reads" RPM and range information from Mach and its' job is to make that happen. 

Steve


 
Re: m3 gets special treatment?
« Reply #8 on: November 21, 2017, 02:33:17 PM »
Many thanks, very well written.