Page 1 of 4

create a c program to run axes.

Posted: Tue Sep 01, 2020 6:53 pm
by irmad
:roll:

Hi Mr Tom,

please correct the c program that I made.
and how to post the correct code on the forum, so that it can be read and understood?
i want to increment axes, when i click init. first I want to try with a short distance (100). with the output DAC2 = 5 volts.
upon hitting the target, bit 152 is off, and DAC2 = 0.

CODE:

#include "KMotionDef.h"

main()
{
//Axis 0

ch0->InputMode=ENCODER_MODE;
ch0->OutputMode=DAC_SERVO_MODE;
ch0->Vel=700;
ch0->Accel=3500;
ch0->Jerk=350000;
ch0->P=2;
ch0->I=0.01;
ch0->D=10;
ch0->FFAccel=0;
ch0->FFVel=0;
ch0->MaxI=200;
ch0->MaxErr=1024;
ch0->MaxOutput=1024;
ch0->DeadBandGain=1;
ch0->DeadBandRange=0;
ch0->InputChan0=0;
ch0->InputChan1=0;
ch0->OutputChan0=0;
ch0->OutputChan1=1;
ch0->MasterAxis=1;
ch0->LimitSwitchOptions=0x100;
ch0->LimitSwitchNegBit=0;
ch0->LimitSwitchPosBit=0;
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=100000;
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=1;
ch0->iir[2].B1=0;
ch0->iir[2].B2=0;
ch0->iir[2].A1=0;
ch0->iir[2].A2=0;

// Axis 1

ch1->InputMode=ENCODER_MODE;
ch1->OutputMode=DAC_SERVO_MODE;
ch1->Vel=700;
ch1->Accel=3500;
ch1->Jerk=350000;
ch1->P=2;
ch1->I=0.01;
ch1->D=10;
ch1->FFAccel=0;
ch1->FFVel=0;
ch1->MaxI=200;
ch1->MaxErr=1024;
ch1->MaxOutput=1024;
ch1->DeadBandGain=1;
ch1->DeadBandRange=0;
ch1->InputChan0=1;
ch1->InputChan1=1;
ch1->OutputChan0=1;
ch1->OutputChan1=1;
ch1->MasterAxis=-1;
ch1->LimitSwitchOptions=0x100;
ch1->LimitSwitchNegBit=0;
ch1->LimitSwitchPosBit=0;
ch1->SoftLimitPos=1e+09;
ch1->SoftLimitNeg=-1e+09;
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=100000;
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=1;
ch1->iir[2].B1=0;
ch1->iir[2].B2=0;
ch1->iir[2].A1=0;
ch1->iir[2].A2=0;




Zero(0); //nol posisi encoder dan posisi yang diperintahkan saat ini
Zero(1);


EnableAxis(0); //aktifkan sumbu dan tetapkan tujuan
EnableAxis(1);

DefineCoordSystem(0,1,-1,-1); // tentukan nomor sumbu chan untuk digunakan sebagai x, y, z, a

if ( EnableAxisDest(0,0) || EnableAxisDest(1,0) )
{
SetBit(152); // to activate the pressure.
DAC(2,1024); // 1024 is 5 volt
}

if ( EnableAxisDest(0,100) || EnableAxisDest(1,100) ) //
{
ClearBit(152); // to non activate the pressure.
DAC(2,0); // 0 is 0 volt
}
}

Re: create a c program to run axes.

Posted: Tue Sep 01, 2020 8:28 pm
by TomKerekes
Hi irmad,

To move an axis use: Move(axis, destination) function not EnableAxisDest.

To wait while an axis is moving use while(!CheckDone(axis)) ;

No need for 'if' statements

Re: create a c program to run axes.

Posted: Wed Sep 02, 2020 12:34 am
by irmad
then, how about this?

ClearBit(152); // to non activate the pressure.
DAC(2,0); // 0 is 0 volt
because I want DAC2 to have a voltage of 5ClearBit (152); // to non activate the pressure.
DAC (2.0); // 0 is 0 volts, and bit 152 is on.

Re: create a c program to run axes.

Posted: Wed Sep 02, 2020 6:59 am
by TomKerekes
I don’t understand the question.

Re: create a c program to run axes.

Posted: Wed Sep 02, 2020 3:00 pm
by irmad
how to enable and disable bit and DAC, when axis hit target?
because previously I tried to do a C program in kmotion.exe, and the axis moved.
but the bit and the DAC are still active / on / output voltage.

Re: create a c program to run axes.

Posted: Wed Sep 02, 2020 3:48 pm
by TomKerekes
Wait until axis is done as described above. Then use SetBit, ClearBit, or DAC functions.

Re: create a c program to run axes.

Posted: Fri Sep 04, 2020 4:13 pm
by irmad
I tried to run it with g code earlier, the results are accurate.
next, can g code be c programmed?
so that it can be run with the pedal manually and auto.

Image

Image

below is the G code for the auto that I made:

Code: Select all

G21 (mm mode)
M5 ( DAC 3.4 volt )
M102 ( Bit 153 on )
M103 ( Bit 154 on )
G1 x-100 y-100 f2100
G4 P3
M111 ( Bit 153 off )
M112 ( Bit 154 off )
M101 ( Bit 152 on )
M6 ( DAC 5 volt )
G1 x0 F4200 
M110 ( Bit 152 off )
M3 ( 0 volt )
M30 (end)

Re: create a c program to run axes.

Posted: Sat Sep 05, 2020 1:17 am
by TomKerekes
I tried to run it with g code earlier, the results are accurate.
next, can g code be c programmed?
so that it can be run with the pedal manually and auto.
I don't fully understand what you are saying.

You might have the pedal perform Cycle Start. See the ExternalButtons.c example

Re: create a c program to run axes.

Posted: Sat Sep 05, 2020 10:36 am
by irmad
I mean, can G code be converted into C program?
so don't use G code anymore.
and what WaitNextTimeSlice ();
and what is it used for?

Re: create a c program to run axes.

Posted: Sat Sep 05, 2020 6:13 pm
by TomKerekes
I mean, can G code be converted into C program?
so don't use G code anymore.
For simple GCode yes, but not sure why you would do this. Operators prefer GCode.


and what WaitNextTimeSlice ();
and what is it used for?
Muti-Threaded programs use time slices. WaitNextTimeSlice() waits until the beginning of the next time slice. See also here. It can be used to do things on a periodic basis. ie every 180us. It also assures ~25us of execution without any possibility of being interrupted.