component gearchangehermle; pin in float speed_command_IN; pin in bit switch-range0; pin in bit switch-range1; pin in bit switch-range2; pin in bit switch-range3; pin in float siggen-IN; pin out float speed-command_OUT; pin out bit feedrate-stop; pin out bit dc-motor-on; pin out bit dc-motor-cw; pin out bit dc-motor-ccw; param rw float ratio1=8.658; param rw float ratio2=3.03; param rw float ratio3=1; param rw float max-speed1 = 693; param rw float max-speed2 = 1982; param rw float max-speed3 = 6000; author "huber"; license "GPL"; function _; ;; FUNCTION(_){ int gear_change_active = 0; int spindle_is_slow = 0; //Conditions for CW motor movement if(speed_command_IN ==0 && switch_range1 == 1){ dc_motor_cw=1; gear_change_active = 1; if(speed_command_IN > max_speed1 && switch_range1 == 1){ dc_motor_cw=1; gear_change_active = 1; } if(speed_command_IN > max_speed1 && switch_range0 == 1){ dc_motor_cw=1; gear_change_active = 1; } if(speed_command_IN > max_speed2 && switch_range2 == 1){ dc_motor_cw=1; gear_change_active = 1; } //Conditions for CCW movement if(speed_command_IN <= max_speed2 && switch_range3 == 1){ dc_motor_ccw=1; gear_change_active = 1; } if(speed_command_IN <= max_speed1 && switch_range2 == 1){ dc_motor_ccw=1; gear_change_active = 1; } if(speed_command_IN <= max_speed1 && speed_command_IN != 0 && switch_range0 == 1){ dc_motor_ccw=1; gear_change_active = 1; } // checking if switch matches required gear range if(speed_command_IN <= max_speed1 && speed_command_IN != 0 && switch_range1 == 1){ dc_motor_cw=0; dc_motor_ccw=0; gear_change_active = 0; } if(speed_command_IN == 0 && switch_range0 == 1){ dc_motor_cw=0; dc_motor_ccw=0; gear_change_active = 0; } if(speed_command_IN > max_speed1 && speed_command_IN <= max_speed2 && switch_range2 == 1){ dc_motor_cw=0; dc_motor_ccw=0; gear_change_active = 0; } if(speed_command_IN > max_speed2 && switch_range3 == 1){ dc_motor_cw=0; dc_motor_ccw=0; gear_change_active = 0; } //Scale the Output speed, according to gear range if(gear_change_active == 0) { dc_motor_on=0; dc_motor_cw=0; dc_motor_ccw=0; feedrate_stop = 1; if (speed_command_IN > max_speed2 && switch_range3 == 1){ speed_command_OUT = speed_command_IN / ratio3; } if (speed_command_IN > max_speed1 && switch_range2 == 1){ speed_command_OUT = speed_command_IN / ratio2; } if (speed_command_IN <= max_speed1 && speed_command_IN != 0 && switch_range1 == 1){ speed_command_OUT = speed_command_IN / ratio1; } if (speed_command_IN == 0){ speed_command_OUT = speed_command_IN; } else { // create a 5Hz sine wave and connect it to speed_command_OUT // wait a short time, then set spindle_is_slow =1 to avoid cahnging gear when slwowing down // maybe use a value created by a siggen in HAL set scale in comp speed_command_OUT = siggen_IN*0.1; feedrate_stop = 1; } //interlock to prevent a short circuit when simultaniously powering the motor CW and CCW if(dc_motor_cw==1){ dc_motor_ccw=0; } if(dc_motor_ccw==1){ dc_motor_cw=0; } //activate dc_motor_on (a short delay would be nice to ensure no short circuits) if(gear_change_active==1 && spindle_is_slow ==1){ if(dc_motor_cw==1||dc_motor_ccw==1){ dc_motor_on=1; } } }