Author Topic: Homing function - Is this unreasonable?  (Read 1962 times)

0 Members and 1 Guest are viewing this topic.

Offline Analias

  • Active Member
  • Posts: 110
    • View Profile
Homing function - Is this unreasonable?
« on: July 28, 2014, 01:48:02 AM »
I'm trying to provide a generic homing procedure that would be run when an ISIG_MOTOR#_PLUS/MINUS signal gets sent while homing.  When I run it on my Sieg X3 mill and trigger a limit switch (all of mine are tied to pin 11 on the BOB) Mach 4 go into lala land - CPU usage jumps to 100% and Mach 4 hangs.  I have to kill the app and restart.

I wanted to make sure I'm not doing something unreasonable in my signal handling code:

Code: [Select]
-- In screen signal handling

local isHoming = false;

SigLib.AddSignalHandler(mc.ISIG_MOTOR_HOME_START, function (state)
    isHoming = (state == 1);
end)

-- Limit switches

SigLib.AddSignalHandler(mc.ISIG_MOTOR0_MINUS, function (state)
    if (isHoming) then
        MySM.MoveMotorOffLimitSwitch(MySM.MOTOR_X);
    end
end)

SigLib.AddSignalHandler(mc.ISIG_MOTOR0_PLUS, function (state)
    if (isHoming) then
        MySM.MoveMotorOffLimitSwitch(MySM.MOTOR_X);
    end
end)

SigLib.AddSignalHandler(mc.ISIG_MOTOR0_MINUS, function (state)
    if (isHoming) then
        MySM.MoveMotorOffLimitSwitch(MySM.MOTOR_X);
    end
end)

In my Lau module:

Code: [Select]
----
--  Test if specified motor is on limit switch (due to homing) and move
--  in appropriate direction to move off of limit switch if needed.
--
--  WARNING : At the moment I am assuming a one-to-one mapping between
--                   motors and axis (ie. motor 0 -> axis 0, motor 1 -> axis 1, ...)
--
--  @param motor (number)   motor selector, 0-6
--
function MyStdMill.MoveMotorOffLimitSwitch (motor)
    local inst = mc.mcGetInstance()
    local directionToMoveOffLimit, isOnLimitSwitch;

    if (DSC.GetFunc('ISAXENBL', motor)) then

        if (DSC.IsPlusLim (motor)) then
            directionToMoveOffLimit = MyStdMill.DIR_NEGATIVE;
            isOnLimitSwitch = DSC.IsPlusLim;
        elseif (DSC.IsMinusLim (motor)) then
            directionToMoveOffLimit = MyStdMill.DIR_POSITIVE;
            isOnLimitSwitch = DSC.IsMinusLim;
        else
            return;
        end

        local axis = { [0]="X", "Y", "Z", "A" };  -- Select axis for Gcode based on motor
        local gcode = string.format("G90 G01 %s%d F5", axis[motor], directionToMoveOffLimit);

        mc.mcCntlSetLastError(inst, string.format("Moving motor %d off axis %s, direction = %d",
                                                                  motor, axis[motor], directionToMoveOffLimit));

        while (isOnLimitSwitch (motor))
        do
            mc.mcCntlGcodeExecute (inst, gcode);
        end

    end
end

Essentially in MoveMotorOffLimitswitch(), I'm checking which limit switch was triggered for the specified motor and choosing which direction to go.  I set "directiontoMoveOffLimit" to a positive or negative one to use as my unit of movement and to choose the direction.  I set "isOnLimitSwitch" to the DSC function for continuing to test for which limit switch we are on to use later in the while loop.  In the while loop I send a "G90 G01 {axis}{direction} F5" to move off the limit switch.


-Freeman
« Last Edit: July 28, 2014, 01:50:04 AM by Analias »
I'm not a complete idiot...
    there are some parts missing.

Offline poppabear

  • Global Moderator
  • *
  • Posts: 2,231
  • Briceville, TN, USA
    • View Profile
    • S S Systems, LLC
Re: Homing function - Is this unreasonable?
« Reply #1 on: July 28, 2014, 09:02:57 AM »
My suspicion is, your While loop is stacking up the G-code movement buffet faster than it can complete the move(s).

Put a "Sleep" in your gcode call:

while (isOnLimitSwitch (motor))  do

     mc.mcCntlGcodeExecute (inst, gcode);
     wx.wxSleep(2); --2 second sleep should give your machine plenty of time to back off the switch

end

BTW: I see your using the DSC in your code, did that last fix I did, fix your issues? If not give me your skype name, and we can see if we can get you straitened out.............

BTW2: I like what your doing with your homing routine, makes good sense.

Scott
Commercial Mach3 & Mach 4, Design/Build/Retrofit CNC and Industrial machines.
http://www.ss-systems-llc.com/

Offline Analias

  • Active Member
  • Posts: 110
    • View Profile
Re: Homing function - Is this unreasonable?
« Reply #2 on: July 29, 2014, 02:01:12 AM »
Actually Scott, I realized that loop was running in an even more aggressive loop of the PLC script.  No wonder it was locking up.  I modified my approach to remove the while loop and to only issue the gcode to move off the limit switches when there was no movement.  This should avoid stacking gcode calls to move off the limit switches.

I haven't tested this yet.

-Freeman

Code: [Select]
----
--  Test if specified motor is on limit switch (due to homing) and move
--  in appropriate direction to move off of limit switch if needed.
--
--  WARNING : At the moment I am assuming a one-to-one mapping between
--            motors and axis (ie. motor 0 -> axis 0, motor 1 -> axis 1, ...)
--
--  @param motor (number)   motor selector, 0-6
--
function MyStdMill.MoveMotorOffLimitSwitch (motor)
    local inst = mc.mcGetInstance()
    local directionToMoveOffLimit, isOnLimitSwitch;

    if (DSC.GetFunc('ISAXENBL', motor)) then

        if (DSC.IsPlusLim (motor)) then
            directionToMoveOffLimit = MyStdMill.DIR_NEGATIVE;
        elseif (DSC.IsMinusLim (motor)) then
            directionToMoveOffLimit = MyStdMill.DIR_POSITIVE;
        else
            return;
        end

        local axis = { [0]="X", "Y", "Z", "A" };
        local gcode = string.format("G90 G01 %s%d F5", axis[motor], directionToMoveOffLimit);

        mc.mcCntlSetLastError(inst, string.format("Moving motor %d off axis %s, direction = %d",
                                                  motor, axis[motor], directionToMoveOffLimit));

        if (DSC.GetFunc('ISAXSTILL', motor)) then
            mc.mcCntlGcodeExecute (inst, gcode); -- only move if other movement is in process
        end
    end
end
I'm not a complete idiot...
    there are some parts missing.

Offline poppabear

  • Global Moderator
  • *
  • Posts: 2,231
  • Briceville, TN, USA
    • View Profile
    • S S Systems, LLC
Re: Homing function - Is this unreasonable?
« Reply #3 on: July 29, 2014, 07:57:25 AM »
Great!!  I am glad to see you got it hammered out!
BTW: I take it the DSC is working out for you now?
Let me know if you run across any other "Easter Eggs" in that module.

Scott
Commercial Mach3 & Mach 4, Design/Build/Retrofit CNC and Industrial machines.
http://www.ss-systems-llc.com/