Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: Cal892 on December 21, 2016, 04:11:13 PM

Title: re-mapping motors and home switch inputs through lua script
Post by: Cal892 on December 21, 2016, 04:11:13 PM
I'm running a CNC hotwire and require all axes to be homed independently (essentially gantry squaring). The PMDX Smart BOB unfortunately does not support homing slaved motors so we currently have to run two separate profiles of Mach4, one just for homing of all the axes every day. I read in the forums that someone had tried to create a script to decouple the motors and re-map them but haven't found anything past the rumors.

I looked through the API and found mcMotorGetAxis(), however I don't see any method of setting or changing the axis label. Is it possible to edit the axis that motors are assigned to through lua scripting? Possibly through some other sort of IO commands?

I haven't been able to do testing on modifying the home switch labels, however I believe I can do it using mcIoSetState() to modify the "Home 2 Motor" input. Please correct me if I am wrong, I'm new to lua but understand scripting and coding.

Any input is appreciated.
Title: Re: re-mapping motors and home switch inputs through lua script
Post by: joeaverage on December 22, 2016, 04:36:48 AM
Hi Cal892,
if I understand what you want to do is when you wish to reference your machine you wish to 'uncouple' two
motors, home them individually and then slave one back to the master when the gantry is square?

Would  mcAxisMapMotor and mcAxisUnmapMotor be of any use?

I was thinking the process might be:

1) home Xaxis with both motors coupled to one home switch on your nominal master rail
2) unmap the slave motor and jiggle it back and forward until the gantry is square with a secondary home switch
3) remap the slave to the master
4) issue mcAxisHomeComplete.

In the API all of those calls have LUA syntax and I imagine therefore could be used in a script.

Craig
Title: Re: re-mapping motors and home switch inputs through lua script
Post by: Cal892 on December 22, 2016, 10:15:08 AM
Thanks Craig, I wasn't aware of the mcAxisMotor. It's not listed in the motor commands in the API.
I'll give it a shot and update after.
Title: Re: re-mapping motors and home switch inputs through lua script
Post by: joeaverage on December 22, 2016, 12:42:23 PM
Hi Cal892,
I started experimenting after posting and have been thinking about it overnight. I'm now unsure that what looked like promising
instructions will be of use.

Even if you can couple/uncouple motors in Machs Core using them will the controller follow suit? If memory serves, when I set up the ESS
plugin it registered and mapped the axes which suggests that temporary rearrangements would have to be made there.

Craig
Title: Re: re-mapping motors and home switch inputs through lua script
Post by: Cal892 on December 22, 2016, 01:23:28 PM
Thanks Craig,
I've been experimenting as well, but without much luck.
I haven't been able to successfully un-map or re-map the axis yet. Also running into difficulties with modifying the home switch input values.

I'm going to open a support ticket and see if Mach can offer any help. I'll update the thread with Mach's suggestions.

Cal
Title: Re: re-mapping motors and home switch inputs through lua script
Post by: joeaverage on December 22, 2016, 11:33:51 PM
Hi Cal,
I've been thinking about this all day. I think what we are considering here is the heart of how Mach4 works with a motion
controller.

I think axis slaving is done by the controller, at least I understand this is the case for the SmoothStepper. The SmoothStepper configuration
plugin registers and maps the axes and motors and informs Machs core with API calls like those above. I don't believe Warp9 the plugin developer
has left functions exposed that may be manipulated programmatically other than through the configuration dialogue.

One possibility that occurs to me is that it might be possible to configure Mach to run the master/slave setup. The Mach configurator Axis Mapping
tab seems to suggest it might be possible. Under that circumstance I imagine that Mach would produce two nominally identical position/velocity over
time commands which would be in the controller mapped to two different and independent motors that happen to be on each end of the gantry.
As we can communicate to Machs core via API commands it would be possible to temporarily break the 'indentical' streams so each motor could act
independently to square the gantry.

As it stands Mach issues one position/velocity command which the controller applies identically to two motors.

The only other solution would be to have two axes, say X and A configured indentically and modify your G code:

G1 X1.5 Y5  say would become
G1 X1.5 A1.5 Y5

Doable but time consuming. How it would work with arc moves or canned cycles makes my head hurt.

Discounting the G code manipulation I think the question to be posed to those who know Mach and perhaps controller plugins
best is 'Can Mach control its own assignment of master/slave or MUST it be done by the controller plugin?'

I've noted in the API that there are a number of commands that have C/C++ syntax but no LUA syntax, ie those commands can only be
used by plugins or modules in C/C++. The commands that we're thinking about which do have LUA syntax does not
necessarily mean that they could be used in a script to achieve anything useful.

Craig
Title: Re: re-mapping motors and home switch inputs through lua script
Post by: joeaverage on December 23, 2016, 06:36:34 AM
Hi,
I have been experimenting with M4 in Sim and discovered a few useful things.

First is that if you go to the Configure/Mach/Axis Mapping tab and even if you change nothing as you click OK then those
settings are made. I have tried:

mcAxisEnable(inst,0,true); it as expected enables/disables axis 0 in this example,

mc.mcAxisUnmapMotors(inst,0); it unmaps all motors from axis 0 in this example,

mc.mcAxisMapMotor(inst,0,0); it maps motor0 to axis0 in this example;

mc.mcAxisMapMotor(inst,0,4); it maps motor4 to the axis0, and this is the kicker now two motors are mapped to axis 0 exactly as Cal requires
and can be temporarily undone by:
mc.mcAxisUnmapMotor(inst,0,4); there again as Cal requires. Under this circumstance motor 0 is still mapped and could be jogged/MDI'ed into position
and then motor4 could again be slaved back to motor0. Don't bother to go to the Configure/Mach/Axis Mapping tab it won't reflect your hand coded
mappings/unmappings and worse will overwrite them just to screw with your head!.....Go on...tell me you didn't go there to try for yourself and now
all your mappings are up the duff right, I bloody told you!

I have been observing the results of various experiments with the sim diagnostic pinned to be always on top, really useful. The reason I chose motor4
rather than motor3, the next spare one, is that the velocity trace which should be solid black doesn't show up whereas all the others do. The string of
position data in the pane above suggests that motor3 is moving as anticipated just that the velocity trace doesn't show, a bug maybe.

What I propose is that if Cal were to write a macro it would in the first instance read ALL the existing enable states and mappings. Then home all axes,
uncouple the master/slave pair, jiggle the remaining motor until square and recouple. Then as an important last before exiting the macro restore
ALL the enable states and mappings to avoid having some hapless operator go to the Configure/Mach/Axis Mappings tab and inadvertently change
everything.

I am still of the opinion when you set up your controller that the two motors would have to be assigned independent 'channels'. The experimenting
I've done suggests that it is possible to have Machs core run the master/slave assignments and they can programmatically be manipulated. This would
obviate the need for the motion controller plugin to handle squaring. A good result I think!

Craig
Title: Re: re-mapping motors and home switch inputs through lua script
Post by: Ya-Nvr-No on December 23, 2016, 09:37:31 AM
I showed, how to do axis squaring using LUA scripting Sept 2015
admit I have not tried it in over a year, as things might have changed.

but the principle steps I am sure would be the same.
Note: you might have to add some wait states for motors to finish.
as I never tested on a master slave router
I just seen in the sim it runs very fast.

http://www.machsupport.com/forum/index.php/topic,27162.msg214343.html#msg214343
Title: Re: re-mapping motors and home switch inputs through lua script
Post by: joeaverage on December 23, 2016, 02:26:16 PM
Hi Ya-Nvr-No,
kool, very kool indeed!

When you ran the script did your reassignments show up on Axis Mapping page?. They don't when using sim on my laptop.

I can map motor4 to the X axis as a slave to motor 0 but it doesn't show on the table and if I inspect the table the table
takes precedence an overwrites the assignment made in my script. How I tumbled to this was that I disabled all
axes and unmapped all motors on the table, and then no axis movement would occur when I issued any MDI commands.
Then I would run my script which enabled axes 0,1,3 and mapped motors 0 and 4 to axis 0, motor1 to axis 1, motor2 to axis2
and the sim diagnostic would show motor movements when I issued MDI moves. When I went back to the Configure/Mach/Axis Mapping
page none of my assignments were there and when I clicked OK to get out of there I was back to a totally disabled machine.

Once I understood this behaviour the rest of my experimentation went a lot smoother. It forms the basis of my suggestion that
the existing enable/mapping states should be read prior to any manipulation for squaring and once done the enable/mapping
states should be restored so the assignment table reflects the machines actual state.

I haven't fiddled yet with any to the SetHome/IsHomed/Home Complete type API commands yet but from your post I'm sure they
will prove to be equally valuable.

Have to say that M4 is VERY crafty! Light years ahead of M3.

I think Cal892 has everything he needs to script his squaring procedure, very kool indeed.

Craig
Title: Re: re-mapping motors and home switch inputs through lua script
Post by: joeaverage on December 25, 2016, 11:52:59 AM
Hi,
small thing I learnt last night...

I have been confused that the Configure/Mach/Axis Mapping page does not reflect actual enable/mapping states
when you start manipulating them programmatically.

I chanced to view the status when I was working on another issue and found that it reflected the assignments I'd
made with the wee script I had been experimenting with for Cal892. Turns out that if you restart Mach the assignments
you program will indeed show in the page.

Craig
Title: Re: re-mapping motors and home switch inputs through lua script
Post by: smurph on January 03, 2017, 11:00:57 PM
The config dialog just reads the profile's Machine.ini file and displays those values.  NOT values that may have been modified at run time by scripts and API calls.  When you hit OK (or Apply) on the config dialog, the profile's Machine.ini will be updated with the values in the dialog (changed or not) and then when the dialog closes, the core will re-read the profile's Machine.ini.  Thus "undoing" any run time modifications.  Even if you hit CANCEL in the config dialog, the run time modification will be undone, as the core will ALWAYS re-read the profile's Machine.ini file.  The core does not know if you pressed OK, or CANCEL on the configuration dialog.  

If you modify the values at run time and then shutdown Mach (without entering the config dialog), the modified values WILL be written to the profile's Machine.ini at shutdown and then next time you start Mach, those values will be used.  

So if you ever want to see the run time modifications in the config dialog, you must also modify the Machine.ini file with the mcProfileWrite*() functions (with the appropriate keys and values) and call mcProfileFlush() afterwards to write them to the disk BEFORE entering the config dialog.

Steve
Title: Re: re-mapping motors and home switch inputs through lua script
Post by: joeaverage on January 05, 2017, 03:20:34 AM
Hi smurph,
that's describes exactly what I see happening... thanks for the explanation. While I might have floundered around
for a bit I've learnt stuff on the way, good fun.

Having a bit of trouble with:
mc.mcGoCookMeSteakand Eggs()
maybe in the next build...LOL

Craig
Title: Re: re-mapping motors and home switch inputs through lua script
Post by: Cal892 on January 24, 2017, 04:19:42 PM
So after working with Mach support I've found that it is possible to use the mcAxisMap and mcAxisUnmap to modify the axis mappings.
The trick is to save or flush the profile at the end of the script using either mcProfileSave() or mcProfileFlush()

Unfortunately it is not possible to edit the pins that each motor has assigned for homing. As I am using a PMDXsmartBOB this is necessary to change from a 2 axis home to a 4 axis.

I've been continually working on this and found that mach can be homed with the 4 axis configuration and then switched to 2 axis without issue (unless you try to home it without changing back)

The problem there is that the changes must be done manually as far as I can tell. Every time I try to home in the middle of a script I can't get it to wait until the homing is complete before continuing. any code after the fact is run immediately and my attempts at pausing/waiting until it's complete with loops seem to cause it to disregard the remaining code entirely.

Does anyone have any tips on how to automatically run code after the homing is complete. I've also tried editing the PLC script and the screenload script to no avail.
Title: Re: re-mapping motors and home switch inputs through lua script
Post by: Cal892 on January 24, 2017, 04:25:31 PM
For references here is my code to change from X,Y axes to X,Y,A,B axes

Code: [Select]
------------4 Axis Mapping-----------
inst = mc.mcGetInstance()
local xAxis = 0
local yAxis = 1
local aAxis = 3
local bAxis = 4
xAxis = tonumber(xAxis)
yAxis = tonumber(yAxis)
aAxis = tonumber(aAxis)
bAxis = tonumber(bAxis)

--motorId, rc = mc.mcAxisGetMotorId(number mInst, number axis, number childId)
local motorIdX, rc = mc.mcAxisGetMotorId(inst, 0, 0);
local motorIdY, rc = mc.mcAxisGetMotorId(inst, 1, 0);
local motorIdA, rc = mc.mcAxisGetMotorId(inst, 0, 1);
local motorIdB, rc = mc.mcAxisGetMotorId(inst, 1, 1);


--rc = mc.mcAxisUnmapMotor(number mInst, number axisId, number motor)
mc.mcAxisUnmapMotor(inst, 0, motorIdX) --Unmap from X axis
mc.mcAxisUnmapMotor(inst, 1, motorIdY) --Unmap from Y axis
mc.mcAxisUnmapMotor(inst, 0, motorIdA) --Unmap motor 2 from X axis
mc.mcAxisUnmapMotor(inst, 1, motorIdB) --Unmap motor 3 from Y axis

--rc = mc.mcAxisMapMotor(number mInst, number axisId, number motorId)
mc.mcAxisMapMotor(inst, 0, 0)    --Map motor 0 to X axis
mc.mcAxisMapMotor(inst, 1, 1)    --Map motor 1 to Y axis
mc.mcAxisMapMotor(inst, 3, 2)    --Map Motor 2 to A axis
mc.mcAxisMapMotor(inst, 4, 3)    --Map motor 3 to B axis

--rc = mc.mcAxisEnable(number mInst, number axisId, number enabled)
mc.mcAxisEnable(inst, 3, true)  --Enable A axis
mc.mcAxisEnable(inst, 4, true)  --Enable B axis

--rc = mc.mcProfileSave(number mInst)
mc.mcProfileSave(inst)


Code: [Select]
-------------2 Axis Mapping-------------
inst = mc.mcGetInstance()
local xAxis = 0
local yAxis = 1
local aAxis = 3
local bAxis = 4
xAxis = tonumber(xAxis)
yAxis = tonumber(yAxis)
aAxis = tonumber(aAxis)
bAxis = tonumber(bAxis)


--rc = mc.mcAxisUnmapMotor(number mInst, number axisId, number motor)
mc.mcAxisUnmapMotor(inst, xAxis, 0) --Unmap motor 0 from X axis
mc.mcAxisUnmapMotor(inst, yAxis, 1) --Unmap motor 1 from Y axis
mc.mcAxisUnmapMotor(inst, aAxis, 2) --Unmap motor 2 from A axis
mc.mcAxisUnmapMotor(inst, bAxis, 3) --Unmap motor 3 from B axis

--rc = mc.mcAxisMapMotor(number mInst, number axisId, number motorId)
mc.mcAxisMapMotor(inst, xAxis, 0)    --Map motor 0 to X axis
mc.mcAxisMapMotor(inst, yAxis, 1)    --Map motor 1 to Y axis
mc.mcAxisMapMotor(inst, xAxis, 2)    --Map Motor 2 to X axis
mc.mcAxisMapMotor(inst, yAxis, 3)    --Map motor 3 to Y axis

--rc = mc.mcAxisEnable(number mInst, number axisId, number enabled)
mc.mcAxisEnable(inst, aAxis, false)  --Disable A axis
mc.mcAxisEnable(inst, bAxis, false)  --Disable B axis

--rc = mc.mcProfileSave(number mInst)
mc.mcProfileSave(inst)
Title: Re: re-mapping motors and home switch inputs through lua script
Post by: DazTheGas on January 24, 2017, 04:45:35 PM
I am stripping my X gantry off this coming weekend and will leave me with 2 Y plates, its one off the things I want to play with is autosquaring, will let you know how it goes.

DazTheGas