DoHomeIndexFunction debounce time

Moderators: TomKerekes, dynomotion

Post Reply
roberto.gotti
Posts: 7
Joined: Tue Dec 20, 2022 8:57 pm

DoHomeIndexFunction debounce time

Post by roberto.gotti » Tue Mar 31, 2026 5:09 pm

Hi Tom,
we have some problem during axis homing.
It often happens, but only on one axis, that during the dog switch search phase, the axis stops searching before actually finding it and zeros in the wrong place.
I'm worried that it might be due to some noise picked up by the sensor cable.
As a zero function we are using the standard version found in the examples. (HomeIndexFunction.c)
Considering the define "DEBOUNCE_VAL 150", how can I calculate the duration in actual time for the value of 150?

int DoHomeIndexFunction(int axis, // axis number to home
float speed, // speed to move toward home
int dir, // direction to move toward home (+1 or -1)
int Dogbit, // Dog bit number to watch for (use -1 for none)
int Dogpolarity, // Dog polarity to wait for (1 or 0)
float indexspeed, // speed to move while searching for index
int indexbit, // index bit number to watch for (use -1 for none)
int indexpolarity, // index polarity to wait for (1 or 0)
float offset, // amount to move inside limits
float Set_New_Pos) // Set New Position
{
printf("Dogbit - %d \nDogpolarity - %d\nindexbit %d\nindexbitpola %d\n", Dogbit, Dogpolarity, indexbit, indexpolarity);
printf("ofs - %f \nnewpos - %f\n", offset, Set_New_Pos);
printf("speed - %f \nindex - %f\n", speed, indexspeed);
//return 1;

if (Dogbit != -1) // limit bit specified?
{
#define DEBOUNCE_VAL 150
int debounce=DEBOUNCE_VAL;
// search for dog
Jog(axis, speed * -dir); // jog slowly in opposite dir
while (1) // loop until Limit bit goes to specified polarity
{
if(ReadBit(Dogbit) != Dogpolarity)
debounce=DEBOUNCE_VAL;
else
debounce--;
if(debounce==0)break;

if (!chan[axis].Enable) return 1; // abort/exit if disabled
}

// release dog
Jog(axis, indexspeed * dir); // jog slowly in opposite dir
while (1) // loop until Limit bit goes to specified polarity
{
if(ReadBit(Dogbit) == Dogpolarity)
debounce=DEBOUNCE_VAL;
else
debounce--;
if(debounce==0)break;

if (!chan[axis].Enable) return 1; // abort/exit if disabled
}
}
.........

Thanks for your suggestion.
Roberto

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

Re: DoHomeIndexFunction debounce time

Post by TomKerekes » Tue Mar 31, 2026 10:37 pm

Hi Roberto,

That is a tight loop so will execute based on the speed of the CPU. I timed it at 1.3us /loop. But if the Thread gets pre-empted about 150us will be added each time it gets pre-empted. Below is the code I used to time it.

You might add a WaitNextTimeSlice in the loop to force each loop to be 180us.

Or a WaitUntil to force the loop to execute in a defined time like 3us (unless it gets pre-empted).

Its really best to eliminate the source of the noise.

HTH


Code: Select all

#include "KMotionDef.h"

int DoHomeIndexFunction(int axis,          // axis number to home
                        float speed,       // speed to move toward home
                        int dir,           // direction to move toward home (+1 or -1)
                        int Dogbit,        // Dog bit number to watch for (use -1 for none)
                        int Dogpolarity,   // Dog polarity to wait for (1 or 0)
                        float indexspeed,  // speed to move while searching for index
                        int indexbit,      // index bit number to watch for (use -1 for none)
                        int indexpolarity, // index polarity to wait for (1 or 0)
                        float offset,      // amount to move inside limits
                        float Set_New_Pos) // Set New Position
{
    printf("Dogbit - %d \nDogpolarity - %d\nindexbit %d\nindexbitpola %d\n", Dogbit, Dogpolarity, indexbit,
           indexpolarity);
    printf("ofs - %f \nnewpos - %f\n", offset, Set_New_Pos);
    printf("speed - %f \nindex - %f\n", speed, indexspeed);
    // return 1;

    if (Dogbit != -1) // limit bit specified?
    {
        double T0, T1;
#define DEBOUNCE_VAL 20
        WaitNextTimeSlice();  // ensure we get a full time slice to begin with

        int debounce = DEBOUNCE_VAL;
        // search for dog
        Jog(axis, speed * -dir); // jog slowly in opposite dir
        T0 = Time_sec();
        while (1) // loop until Limit bit goes to specified polarity
        {
            if (ReadBit(Dogbit) != Dogpolarity)
                debounce = DEBOUNCE_VAL;
            else
                debounce--;
            if (debounce == 0)
                break;

            if (!chan[axis].Enable)
                return 1; // abort/exit if disabled
        }
        T1 = Time_sec();
        printf("Time = %fus\n", (T1 - T0) * 1e6);

        // release dog
        Jog(axis, indexspeed * dir); // jog slowly in opposite dir
        while (1)                    // loop until Limit bit goes to specified polarity
        {
            if (ReadBit(Dogbit) == Dogpolarity)
                debounce = DEBOUNCE_VAL;
            else
                debounce--;
            if (debounce == 0)
                break;

            if (!chan[axis].Enable)
                return 1; // abort/exit if disabled
        }
    }
}


int main()
{
    DoHomeIndexFunction(0,     // axis number to home
                        10000, // speed to move toward home
                        1,     // direction to move toward home (+1 or -1)
                        0,     // Dog bit number to watch for (use -1 for none)
                        0,     // Dog polarity to wait for (1 or 0)
                        10000, // speed to move while searching for index
                        1,     // index bit number to watch for (use -1 for none)
                        0,     // index polarity to wait for (1 or 0)
                        1000,  // amount to move inside limits
                        0);    // Set New Position

    return 0;
}
Regards,

Tom Kerekes
Dynomotion, Inc.

roberto.gotti
Posts: 7
Joined: Tue Dec 20, 2022 8:57 pm

Re: DoHomeIndexFunction debounce time

Post by roberto.gotti » Thu Apr 02, 2026 4:22 pm

Hi Tom,
Thanks a lot for your suggestion.
I agree with you about "eliminate the source of the noise" as best option.

Post Reply