Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: DAAD on March 22, 2020, 03:41:57 PM

Title: Trying to write a simple function need help
Post by: DAAD on March 22, 2020, 03:41:57 PM
Still trying to figure out how to create a proper function...
Next one should be the M6 dual head... long way to go... :-(

Try to get a function where the gcode loaded depends on the axis / motor combination that is up at the moment.
What am i doing wrong?

Code: [Select]
local inst = mc.mcGetInstance()
function Zspindle()
local AxisZ = mc.mcAxisGetMotorId (0,2) --motor Id van Z axis (Motor 2 = Z, Motor 4 = A)
if AxisZ == "2" then mc.mcCntlGcodeExecute(inst, "G90 G53 X80 Y200 F10000")
else mc.mcCntlGcodeExecute(inst, "G90 G53 X72.1993 Y42.2352 F10000")
end
end




Title: Re: Trying to write a simple function need help
Post by: joeaverage on March 22, 2020, 03:59:14 PM
Hi,
couple of pointers:

1) Put the local inst = mc.mcGetInstance() INSIDE the function, thus the variable in in scope when the function executes.

local inst = mc.mcGetInstance()
function Zspindle()
local inst = mc.mcGetInstance()
........
.......

2) put parentheses around the conditional:
if (AxisZ == "2") then mc.mcCntlGcodeExecute(inst, "G90 G53 X80 Y200 F10000")

Craig
Title: Re: Trying to write a simple function need help
Post by: DAAD on March 22, 2020, 04:07:12 PM
Many thanks!

Will try this tomorrow!
Title: Re: Trying to write a simple function need help
Post by: SwiftyJ on March 22, 2020, 04:31:51 PM
Another few things,  the API mc.mcAxisGetMotorId requires the following..
Code: [Select]
motorId, rc = mc.mcAxisGetMotorId(
number mInst,
number axis,
number childId)

You are missing the child Id.
 
It also returns the motorId as a number so in your if statement you have
Code: [Select]
if AxisZ == "2"
this should be
Code: [Select]
if AxisZ == 2

Title: Re: Trying to write a simple function need help
Post by: DAAD on March 22, 2020, 05:27:39 PM
Thanks,

What does the child ID mean?
Do i need to put in something there? If yes what?
Title: Re: Trying to write a simple function need help
Post by: compewter_numerical on March 23, 2020, 01:03:06 AM
If you look at the Axis Mapping in the Mach4 control config you can get an idea of what child ID could be. I'm thinking it could be the position of master/slave boxes for each motor. So X axis master you would use 0 for the axis and 0 for child ID. To get the slave 1 motor id for the X axis you would use 0 for the axis and 1 for the child ID.

Are you trying to get the axis ID or the motor ID?

There is also this function from the API:
Code: [Select]
axisId, rc = mc.mcMotorGetAxis(number mInst, number motorId)

Title: Re: Trying to write a simple function need help
Post by: DAAD on March 23, 2020, 01:07:25 AM
Just need the motor ID.

Would the child id be the slave motor id? -> What if there is only one motor?
Title: Re: Trying to write a simple function need help
Post by: compewter_numerical on March 23, 2020, 01:13:22 AM
Quote
...X axis master you would use 0 for the axis and 0 for child ID.

Try zero for child ID it should be the master index.
Title: Re: Trying to write a simple function need help
Post by: DAAD on March 23, 2020, 01:16:54 AM
Will do, thanks!
Title: Re: Trying to write a simple function need help
Post by: DAAD on March 25, 2020, 01:44:19 AM
Did adjust the function, and did a compile succesfully but the function does not work.

I would like to use the axis id and the motor id info to determine which is active Spindle or router.

still not shure what child_id number would be..  Problem with filling in 0 is that there is a motor (x in my case) with number zero...

Any pointers? let me know!

Code: [Select]
local inst = mc.mcGetInstance()
--z axis = 2
-- A axis =4
--Motor 2 = Z
--Motor 4 = A


function Zspindle()
local AxisZ = mc.mcAxisGetMotorId (0,2,0) --get motor id from axis 2
if AxisZ == 2 then mc.mcCntlGcodeExecute(inst, "G90 G53 X80 Y200 F10000")
else mc.mcCntlGcodeExecute(inst, "G90 G53 X72.1993 Y42.2352 F10000")
end
end

ps: Gcode excecute only for testing purposes...
Title: Re: Trying to write a simple function need help
Post by: joeaverage on March 25, 2020, 03:01:37 AM
Hi,
to start with put
Code: [Select]
local inst = mc.mcGetInstance() inside the function otherwise 'inst' is out of scope.

Craig
Title: Re: Trying to write a simple function need help
Post by: DAAD on March 25, 2020, 03:38:37 AM
thanks for the basics tip!
Title: Re: Trying to write a simple function need help
Post by: DAAD on March 25, 2020, 04:31:07 PM
If you look at the Axis Mapping in the Mach4 control config you can get an idea of what child ID could be. I'm thinking it could be the position of master/slave boxes for each motor. So X axis master you would use 0 for the axis and 0 for child ID. To get the slave 1 motor id for the X axis you would use 0 for the axis and 1 for the child ID.

Are you trying to get the axis ID or the motor ID?

There is also this function from the API:
Code: [Select]
axisId, rc = mc.mcMotorGetAxis(number mInst, number motorId)


Searched inside the ini file and foud the following info:

Axis 3 = Child ID0 = Motor 4, Child ID1 = No motor found.  So your info seems correct. If i want the value of the motor in need 0 in the child ID.

still don't get the function working tho...
do i need to do something special for the number to return?