This component is designed for turret type tool changers with a DC motor to rotate the turret. It could also be used as part of the toolchange sequence for a machine with a tool carousel. Tool position is sensed with a single sensor that detects each tool go past and an optional index sensor.

Les

component turret_toolchanger "Simple lathe turret tool changer";

description 
"""
Component to control a simple lathe turret tool changer with a DC motor to 
rotate and a ratchet to hold position. The motor is driven forwards to rotate 
the turret. There is a switch on the ratchet to indicate when one tool has been 
indexed. When the turret has rotated to the correct tool the motor is then 
driven backwards  to hold the turret against the ratchet.

Note at startup
""";

author "Les Newell";
license "GPLv2 or greater";

pin in signed desired_tool "This is the tool to select";
pin out unsigned current_tool "Current turret position.";
pin in bit tool_change "Start tool change sequence";
pin out bit tool_changed "Tool change has completed";
pin in bit force_index "Set this high to force the tool changer to index once 
without recording a tool change. Used to synchronize the turret if you don't 
have a home switch";
pin in bit tool_switch "feedback switch. Should trip once per tool";
pin in bit home_switch "Optional home switch.";
pin out bit motor_fwd "Run the motor forward";
pin out bit motor_rev "Run the motor backwards";
pin out bit fault "Goes true if there is an error while changing tool (timeout 
or tool out of range)";

param rw unsigned debounce =  20 "Debounce switch for the given number of 
milliseconds (assuming 1kHz servo loop). Must be at least 1";
param rw unsigned timeout = 500 "Maximum time for one tool to index in 
milliseconds";
param rw unsigned ntools = 6 "Number of tool stations in the turret";
param rw signed home_position = 0 "position of the turret when home switch is 
active. Set to 0 if you don't have a home switch";
param rw bit tool_switch_invert = 0 "Invert the tool switch. If true the tool 
number increments on the falling edge of tool_switch";
param rw unsigned reverse_time = 500 "Time the reverse motor stays on. Set to 0 
for infinite";

variable bool last_switch;
variable signed change_timer;
variable unsigned debounce_count = 0; 
variable unsigned rev_timer = 0;
variable unsigned force_mode = 0;

function _;


;;

bool switch_tripped(struct __comp_state * __comp_inst)
{
    if(last_switch != tool_switch)
    {
        debounce_count = debounce;
        last_switch = tool_switch;
    }else
    {       
        if(debounce_count)
        {
            debounce_count--;
            if(debounce_count == 0)
            {               
                if(!(last_switch & tool_switch_invert)) // switch has tripped
                {
                    return true;
                }
            }
        }
    }
    return false;
}


FUNCTION(_) {
    if(tool_change)
    {
        if(tool_changed || fault) return; //we are done - wait for motion to 
acknowledge
        if(switch_tripped(__comp_inst))
        {
            change_timer = timeout;
            if(home_switch) //we are on home tool
            {
                current_tool = home_position;
            }else
            {
                (current_tool)++;
                if(current_tool > ntools)
                {
                    current_tool = 1;
                }
            }
        }

        if(current_tool == desired_tool)
        {
            if(motor_fwd) //have been changing tool
            {
                motor_rev = true;
                rev_timer = reverse_time;
            }
            motor_fwd = false;
            tool_changed = true;
        }else
        {
            change_timer--;
            if(!motor_fwd && !fault) //just starting tool change
            {
                if(desired_tool < 1 || desired_tool > ntools) //tool out of 
range
                {
                    rtapi_print_msg(RTAPI_MSG_ERR, "ERROR: Selected tool %d is 
out of range", desired_tool);
                    fault = true;
                    return;
                }  
                if(current_tool < 1) //we don't know where we are
                {
                    if(home_position)
                    {
                        current_tool = -home_position; //force the turret to 
index until it hits home
                    }else
                    {
                        current_tool = 1; //not much else we can do!
                    }
                }
                last_switch = tool_switch;
                change_timer = timeout;
                motor_rev = false;
                motor_fwd = true;
            }
            if(change_timer == 0)
            {
                rtapi_print_msg(RTAPI_MSG_ERR, "ERROR: Tool changer timed out");
                fault = true;
                if(home_position)
                {
                    current_tool = 0; //flag a homing cycle
                }
                motor_fwd = false;
                motor_rev = false;
            }
        }
    }else
    {
        if(force_index && force_mode == 0)
        {
            motor_rev = false;
            motor_fwd = true;
            force_mode = 1;
        }
        
        if(force_mode)
        {
            if(force_mode == 1)
            {
                if(switch_tripped(__comp_inst))
                {
                    force_mode = 2;
                    rev_timer = reverse_time;
                    motor_fwd = false;
                    motor_rev = true;
                }
            }else if(force_index == 0)
            {
                force_mode = 0;
            }
        }
        
        tool_changed = false;
        fault = false;
        if(rev_timer)
        {
            rev_timer--;
            if(rev_timer == 0)
            {
                motor_rev = false;
            }
        }
    }     
}
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Emc-developers mailing list
Emc-developers@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/emc-developers

Reply via email to