Hello Guest it is March 29, 2024, 02:28:06 AM

Author Topic: Using a button script to set jog increments etc  (Read 14588 times)

0 Members and 1 Guest are viewing this topic.

Using a button script to set jog increments etc
« on: May 19, 2018, 04:47:59 PM »
I've created buttons that I want to be able to click to automatically set the Jog mode to incremental and a specified increment. I originally did this without scripts but found there's a bug that occasionally moves 1" instead of 1mm!!!
I found a post about this and another function that forces the Jog to be metric so I'd like to combine all three calls in one script.

This is what I have, but it doesn't work.

   local inst = mc.mcGetInstance()
      --First force units to metric, it sometimes reverts to inches!
   mc.mcJogSetUnitsMode(inst, mc.X_AXIS, 21)      --I don't know why 21, but that's what the post said!
   mc.mcJogSetType(inst, mc.X_AXIS, mc.MC_JOG_TYPE_INC)
   mc.mcJogSetInc(inst, mc.X_AXIS, 1)   --1mm increment
   
I know the script is being executed when I click the button, I added a 'hello world' dialog box to the start of it to check.
I presume that the calls are inaccessible from where I'm trying to call them from?

I need to do this so I know for certain what's going to happen when I'm using an edge finder, or nudging up to an edge with a cutter. 25mm unexpected moves are not acceptable!

Any help would be much appreciated.
Re: Using a button script to set jog increments etc
« Reply #1 on: May 19, 2018, 05:07:45 PM »
Hi,

Quote
mc.mcJogSetUnitsMode(inst, mc.X_AXIS, 21)      --I don't know why 21, but that's what the post said!

This API call is not in the API.chm list. There are a few circumstances where an API call has been created but the documentation has not....yet.
smurph did explain in another post recently that his development PC had a melt down and all his development software went with it... including
the ability to write Windows Help files like our API.chm. He has only recently started putting it all back together....he explains also that he needs
a week of bad weather before the inclination to generate documentation overcomes him!

I'm going to guess that the '21' in the API call is somehow related to G21...which is the Gcode that cause all subsequent Gcode to be interpreted as metric.

Does your build of Mach have the new Zero Brane editor? If it does try typing out the API in question and the auto complete window will come up and then
with the opening '(' a list of required arguments and their types will come up. I suspect that the bare 21 in your code is not correct, it might have to be quoted
for instance.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Using a button script to set jog increments etc
« Reply #2 on: May 19, 2018, 05:16:25 PM »
Hi,
I've just gone and fiddled with the API and sure enough and unitsMode:number is just a plain Lua number, so 21 is acceptable, but is is the right number?

Going to take a help ticket to find out.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Using a button script to set jog increments etc
« Reply #3 on: May 19, 2018, 05:57:02 PM »
Here is the actual button down script I use. I have totally replaced the stock jog functions and this has proved reliable for a long time. This should give you some ideas from which to build, but note that I do not take any stock value for distance and jog type, as I created my own and saved them in the INI file. I call this from buttons and from keys mapped into the signal script.

Code: [Select]
--AW Jog button down initiates motion.
-- type is taken from INI if no parrameter is supplied
function Jogging.JogBtnDown(axis, direction, type)
    local inst = mc.mcGetInstance()
    if (type == nil) then
        type = mc.mcProfileGetInt(inst,'Preferences', 'JogType', mc.MC_JOG_TYPE_INC)
    end
    local dist, rc = mc.mcProfileGetString(inst,'Preferences', "JogDist", '0')
    dist = tonumber(dist)
    local defUnits = mc.mcCntlGetUnitsDefault(inst)
    rc = mc.mcJogSetUnitsMode(inst, axis, defUnits)
    if (type == mc.MC_JOG_TYPE_INC) then
        if (direction == mc.MC_JOG_NEG) then
            dist = -1 * dist
        end
        rc = mc.mcJogIncStart(inst, axis, dist)
    elseif (type ==mc.MC_JOG_TYPE_VEL) then
        rc = mc.mcJogVelocityStart(inst, axis, direction)
    end
end

My default units are set to metric, and I've never experienced a switch to Imperial units when using the ablove.

Allan

Offline smurph

*
  • *
  •  1,544 1,544
  • "That there... that's an RV."
    • View Profile
Re: Using a button script to set jog increments etc
« Reply #4 on: May 19, 2018, 09:16:33 PM »
Striplar,

One thing to do is check the return code from any and all API calls.  These return codes will do a lot of work for you in diagnosing why something doesn't work.  Anything other than zero (mc.MERROR_NOERROR) is an error.

Allan's code is getting the return codes in the variable rc.  That is a step in the right direction, but it is still not actually checking the return code. 

Code: [Select]
local inst = mc.mcGetInstance()

rc = mc.mcJogIncStart(inst, axis, dist)
if (rc ~= mc.MERROR_NOERROR) then
    -- Error condition!  Maybe use SetLastErrror
    mc.mcCntlSetLastError(inst, "jog Start Failed")
    -- And return immediately so that things don't go all pear shaped.
    return;
end
...

It is a lot more coding, but I can promise you that it will pay off.  Things won't sneak up and bite you.  And it covers all of the bases, so to speak.  If you get into the habit of checking the return codes, it just becomes at some point.  It is especially important on API calls that are to perform an action.  Because you want to handle the case if the action didn't or couldn't happen. 

Steve
Re: Using a button script to set jog increments etc
« Reply #5 on: May 20, 2018, 05:04:46 AM »
Thanks to you both, that's all very useful. I'll have to digest the code, I'm still taking baby steps in this.

The switch back to Imperial units might not happen when I'm not messing about with the setups, so that call to force it to metric might not be necessary

However, I still don't understand why the following call has no effect...

 mc.mcJogSetType(inst, mc.X_AXIS, mc.MC_JOG_TYPE_INC)

I've checked return code which is 0. Can anyone tell me if this function actually works, or if I'm wasting my time?

The same 0 return code comes from the other functions I called in the example, but I don't know what that means.
Re: Using a button script to set jog increments etc
« Reply #6 on: May 20, 2018, 05:45:42 AM »
smurph

Point taken. I tend to use the return codes during debugging, but for simple calls like the above most returns are caused by parameter failures which should not normally occur once the code is stable. But I agree this is a short cut.

The probing module, which I have completely re-vamped, is another matter. Due to the likelyhood of issues, I check each and every return code associated with motion and control how errors propagate through the call chain.

Allan
Re: Using a button script to set jog increments etc
« Reply #7 on: May 20, 2018, 05:57:20 AM »
Hi Allan,
Where did you find the information on the following function that you used?

      type = mc.mcProfileGetInt(inst,'Preferences', 'JogType', mc.MC_JOG_TYPE_INC)

I can see the details of the call in the help, but where is the list of the 'Preferences' and 'JogType'?

I get the impression that there's another large technical document that I'm missing that shows the system variables, where they're stored and what they do.
Re: Using a button script to set jog increments etc
« Reply #8 on: May 20, 2018, 06:54:25 AM »

JofType is just the entry under the section [Preferences] in the machine.ini file. I can't remember for sure as it was some time since I did that, but if this entry is not in your machine.ini it must be one that I have added.

I have replaced the standard jog tab with one that uses a set of buttons that directly set the jog increment and type, and these are saved in machine.ini. The buttons cover increments from 0.01 to 10mm in a .01, .03, .1, .3 .. 3, 10 sequence, plus a continuous option. The values are taken from the list in Mach4's Configuration/Control. 

The jog buttons call the function listed above, plus a related button up function which stops motion. These same functions are accessed by the keyboard via signal table entries in the screen load script.

Allan
Re: Using a button script to set jog increments etc
« Reply #9 on: May 20, 2018, 07:26:10 AM »
Ah, I see that now. I'm going to have to take a long hard look at that example you gave.

Fortunately I've managed to cheat somewhat by calling "Jog Mode Step" action when the button is down which sets the jog mode to incremental and then another one "Jog Inc.4" that explicitly sets the increment value.

Maybe it's possible to call those from a script instead, but I don't know how to do that.

I can see the various functions that have been defined in the screen script, but I can't see how you are supposed to add your own ones. The Zero Brane editor seems to be what's used, but ScreenScript.lua appears to be common to all profiles. Somehow I thought there would be one for each profile?

So presumably to create your own new functions, you have to edit the common ScreenScript.lua file and then your function appears in the list of Actions in the properties?