C noob having a rough time tweaking the linear tool changer code

Moderators: TomKerekes, dynomotion

Garage14
Posts: 27
Joined: Thu Jan 02, 2020 10:57 pm

Re: C noob having a rough time tweaking the linear tool changer code

Post by Garage14 » Thu Jul 21, 2022 7:04 pm

I do have 20320 under all axis. Switching to machine coordinates shows the offset and helped me find it. I used one of the homing templates in the C programs folder and now I see that after setting the limit switch to provide a home location there is a value to move the axis off of the limit switch after touching off. This value in counts equals how far off I am on my tool holder and the position at "home" with machine coordinates turned on shows this -2.5mm value as well. I need to get all code to use machine coordinates or have the floating origin zero at the point of limit switch activation instead of after moving off.
Simple home 3 axis

Code: Select all

#include "KMotionDef.h"

// Configuration and Homing program for a 3 axis System
// Limit switches are disabled and used as a home switch
// then they are re-enabled


main()
{
	int SaveXLimits,SaveYLimits,SaveZLimits;  //place to save limit switch settings
	
	DisableAxis(0);  // Disable all axes
	DisableAxis(1);
	DisableAxis(2);

	// Set the axis parameters here
	// after everything is configured in the KMotion Screens
	// use copy C Code to clipboard on the configuration screen
	// then paste here.  Repeat for each axis
	
	ch0->InputMode=ENCODER_MODE;
	ch0->OutputMode=DAC_SERVO_MODE;
	ch0->Vel=800000;
	ch0->Accel=1e+06;
	ch0->Jerk=3e+06;
	ch0->P=5;
	ch0->I=0.0005;
	ch0->D=1;
	ch0->FFAccel=0;
	ch0->FFVel=0;
	ch0->MaxI=1000;
	ch0->MaxErr=1000;
	ch0->MaxOutput=2047;
	ch0->DeadBandGain=1;
	ch0->DeadBandRange=0;
	ch0->InputChan0=0;
	ch0->InputChan1=1;
	ch0->OutputChan0=0;
	ch0->OutputChan1=1;
	ch0->MasterAxis=-1;
	ch0->LimitSwitchOptions=0x11f;
	ch0->LimitSwitchNegBit=140;
	ch0->LimitSwitchPosBit=140;
	ch0->SoftLimitPos=1e+09;
	ch0->SoftLimitNeg=-1e+09;
	ch0->InputGain0=-1;
	ch0->InputGain1=1;
	ch0->InputOffset0=0;
	ch0->InputOffset1=0;
	ch0->OutputGain=1;
	ch0->OutputOffset=0;
	ch0->SlaveGain=1;
	ch0->BacklashMode=BACKLASH_OFF;
	ch0->BacklashAmount=0;
	ch0->BacklashRate=0;
	ch0->invDistPerCycle=1;
	ch0->Lead=0;
	ch0->MaxFollowingError=2000;
	ch0->StepperAmplitude=20;

	ch0->iir[0].B0=1;
	ch0->iir[0].B1=0;
	ch0->iir[0].B2=0;
	ch0->iir[0].A1=0;
	ch0->iir[0].A2=0;

	ch0->iir[1].B0=1;
	ch0->iir[1].B1=0;
	ch0->iir[1].B2=0;
	ch0->iir[1].A1=0;
	ch0->iir[1].A2=0;

	ch0->iir[2].B0=0.0166094;
	ch0->iir[2].B1=0.0332189;
	ch0->iir[2].B2=0.0166094;
	ch0->iir[2].A1=1.60679;
	ch0->iir[2].A2=-0.673229;
    
	ch1->InputMode=ENCODER_MODE;
	ch1->OutputMode=DAC_SERVO_MODE;
	ch1->Vel=600000;
	ch1->Accel=1e+06;
	ch1->Jerk=1.5e+06;
	ch1->P=5;
	ch1->I=0.0003;
	ch1->D=0.5;
	ch1->FFAccel=0;
	ch1->FFVel=0;
	ch1->MaxI=1000;
	ch1->MaxErr=1000;
	ch1->MaxOutput=2047;
	ch1->DeadBandGain=1;
	ch1->DeadBandRange=0;
	ch1->InputChan0=1;
	ch1->InputChan1=1;
	ch1->OutputChan0=1;
	ch1->OutputChan1=3;
	ch1->MasterAxis=-1;
	ch1->LimitSwitchOptions=0x11f;
	ch1->LimitSwitchNegBit=141;
	ch1->LimitSwitchPosBit=141;
	ch1->SoftLimitPos=1e+30;
	ch1->SoftLimitNeg=-1e+30;
	ch1->InputGain0=1;
	ch1->InputGain1=1;
	ch1->InputOffset0=0;
	ch1->InputOffset1=0;
	ch1->OutputGain=1;
	ch1->OutputOffset=0;
	ch1->SlaveGain=1;
	ch1->BacklashMode=BACKLASH_OFF;
	ch1->BacklashAmount=0;
	ch1->BacklashRate=0;
	ch1->invDistPerCycle=1;
	ch1->Lead=0;
	ch1->MaxFollowingError=2000;
	ch1->StepperAmplitude=20;

	ch1->iir[0].B0=1;
	ch1->iir[0].B1=0;
	ch1->iir[0].B2=0;
	ch1->iir[0].A1=0;
	ch1->iir[0].A2=0;

	ch1->iir[1].B0=1;
	ch1->iir[1].B1=0;
	ch1->iir[1].B2=0;
	ch1->iir[1].A1=0;
	ch1->iir[1].A2=0;

	ch1->iir[2].B0=0.016609;
	ch1->iir[2].B1=0.033219;
	ch1->iir[2].B2=0.016609;
	ch1->iir[2].A1=1.60679;
	ch1->iir[2].A2=-0.673229;

	ch2->InputMode=ENCODER_MODE;
	ch2->OutputMode=DAC_SERVO_MODE;
	ch2->Vel=50000;
	ch2->Accel=100000;
	ch2->Jerk=1e+06;
	ch2->P=5;
	ch2->I=0.001;
	ch2->D=1;
	ch2->FFAccel=0;
	ch2->FFVel=0;
	ch2->MaxI=1000;
	ch2->MaxErr=1000;
	ch2->MaxOutput=2047;
	ch2->DeadBandGain=1;
	ch2->DeadBandRange=0;
	ch2->InputChan0=2;
	ch2->InputChan1=0;
	ch2->OutputChan0=2;
	ch2->OutputChan1=0;
	ch2->MasterAxis=-1;
	ch2->LimitSwitchOptions=0x11f;
	ch2->LimitSwitchNegBit=142;
	ch2->LimitSwitchPosBit=142;
	ch2->SoftLimitPos=1e+09;
	ch2->SoftLimitNeg=-1e+09;
	ch2->InputGain0=1;
	ch2->InputGain1=1;
	ch2->InputOffset0=0;
	ch2->InputOffset1=0;
	ch2->OutputGain=1;
	ch2->OutputOffset=0;
	ch2->SlaveGain=1;
	ch2->BacklashMode=BACKLASH_OFF;
	ch2->BacklashAmount=0;
	ch2->BacklashRate=0;
	ch2->invDistPerCycle=1;
	ch2->Lead=0;
	ch2->MaxFollowingError=2000;
	ch2->StepperAmplitude=20;

	ch2->iir[0].B0=1;
	ch2->iir[0].B1=0;
	ch2->iir[0].B2=0;
	ch2->iir[0].A1=0;
	ch2->iir[0].A2=0;

	ch2->iir[1].B0=1;
	ch2->iir[1].B1=0;
	ch2->iir[1].B2=0;
	ch2->iir[1].A1=0;
	ch2->iir[1].A2=0;

	ch2->iir[2].B0=0.0166094;
	ch2->iir[2].B1=0.0332189;
	ch2->iir[2].B2=0.0166094;
	ch2->iir[2].A1=1.60679;
	ch2->iir[2].A2=-0.673229;

	
	
	
	// disable the limits (first save how they were set) 
	SaveXLimits = ch0->LimitSwitchOptions;
	SaveYLimits = ch1->LimitSwitchOptions;
	SaveZLimits = ch2->LimitSwitchOptions;
	ch0->LimitSwitchOptions = 0;
	ch1->LimitSwitchOptions = 0;
	ch2->LimitSwitchOptions = 0;
	
	// enable all 3 axes and begin servoing where we are 
	
	EnableAxis(0);
	EnableAxis(1);
	EnableAxis(2);

 
	// Home Z up first - jog until it sees the limit

    Jog(2,7500);             // jog slowly positive
    while (ReadBit(142)) ;  // loop until IO bit goes high
    Jog(2,0);				// stop
    while (!CheckDone(2)) ; // loop until motion completes 
	DisableAxis(2);			// disable the axis
    Zero(2);				// Zero the position
	EnableAxis(2);			// re-enable the ServoTick
	Move(2,-1200.0);		// move some amount inside the limits
    while (!CheckDone(2)) ; // loop until motion completes 
	ch2->LimitSwitchOptions = SaveZLimits;  // restore limit settings


	// Home X next - jog until it sees the limit

    Jog(0,15000);  			// jog slowly positive
    while (ReadBit(140)) ;  // loop until IO bit goes high
    Jog(0,0);				// stop
    while (!CheckDone(0)) ; // loop until motion completes 
	DisableAxis(0);			// disable the axis
    Zero(0);				// Zero the position
	EnableAxis(0);			// re-enable the ServoTick
	Move(0,-2000.0);			// move some amount inside the limits
    while (!CheckDone(0)) ; // loop until motion completes 
	ch0->LimitSwitchOptions = SaveXLimits;  // restore limit settings


	// Home Y next - jog until it sees the limit

    Jog(1,15000);  			// jog slowly positive
    while (ReadBit(141)) ;  // loop until IO bit goes high
    Jog(1,0);				// stop
    while (!CheckDone(1)) ; // loop until motion completes 
	DisableAxis(1);			// disable the axis
    Zero(1);				// Zero the position
	EnableAxis(1);			// re-enable the ServoTick
	Move(1,-3000.0);			// move some amount inside the limits
    while (!CheckDone(1)) ; // loop until motion completes 
	ch1->LimitSwitchOptions = SaveYLimits;  // restore limit settings

	DefineCoordSystem(0,1,2,-1);  //Define XYZ coordinated motion axes
}

User avatar
TomKerekes
Posts: 2868
Joined: Mon Dec 04, 2017 1:49 am

Re: C noob having a rough time tweaking the linear tool changer code

Post by TomKerekes » Thu Jul 21, 2022 11:12 pm

Ok. Glad you figured it out.
Regards,

Tom Kerekes
Dynomotion, Inc.

Post Reply