Dynomotion

Group: DynoMotion Message: 14171 From: fouijar Date: 11/26/2016
Subject: Absolute encoder - Gray to Binary conversion

Hello,

Just a quick question about the function below, I try to divide the resolution of the encoder by 360 degrees to get the angular resolution per degree but I can't get any relevant results:


Bit#1046 G0=1
Bit#1047 G1=0
Bit#1048 G2=1
Bit#1049 G3=0
Bit#1050 G4=1
Bit#1051 G5=0
Bit#1052 G6=1
Bit#1053 G7=0
Bit#1054 G8=0
Bit#1055 G9=1

B9=1 p9=512
B8=1 p8=256
B7=1 p7=128
B6=0 p6=0
B5=0 p5=0
B4=1 p4=16
B3=1 p3=8
B2=0 p2=0
B1=0 p1=0
B0=1 p0=1
D=921    alphaRes=0.000000    alpha=0.000000


Decimal=921

ERes=1024

The problem lies within the division and multiplication operations (alphaRes,alpha)

I think I'm missing something but can't figure what...


The full test program is also included below for reference and is intended to be included within the tool change program.


Thanks for your attention,


Jerome

          
    float AngularPosition(D){
/*This function converts the decimal value from the absolute encoder to the actual positioning angle*/
    float alphaRes;
    float alpha;
    alphaRes=1024/360;//2.844444
    alpha=D*alphaRes;
    printf("D=%d\talphaRes=%f\talpha=%f\n\n",D,alphaRes,alpha);
    return alpha, alphaRes;
   
    }


    /*Full conversion test program*/

#include "KMotionDef.h"


#define QA 40    // define to which IO bits the AB signals are connected
#define QB 41    

#define TAU 0.05 // smoothness factor (Low Pass Time constant seconds)
#define FINAL_TIME .5 // Set final dest after this amount of time with no change
#define ERes 1024 //Encoder Resolution, 10bits

/*INPUTS and OUTPUTS*/
/*INPUTS*/
#define AirCylHomePos 138 //Carousel air cylinder is at home position
#define AirCylToolPos 139 //Carousel air cylinder is at tool change position
#define ToolClamped 140 //Tool is clamped into the spindle
#define ToolUnClamped 141 //Tool is released from spindle
#define Index 137 //Indexing sensor to index the carousel position at each motor turn, CW or CCW
#define CarRef 25 //When active carousel is at reference position corresponding to pocket #1
#define J 1031// Indexing sensor to control the indexing motor CW/CCW
#define K 1030// Indexing sensor to control the indexing motor CW/CCW
#define G1 1046// Konnect: 10bits for the gray code G1=>G10
#define G2 1047
#define G3 1048
#define G4 1049
#define G5 1050
#define G6 1051
#define G7 1052
#define G8 1053
#define G9 1054
#define G10 1055
/*OUTPUTS*/
#define SpindleRef 144 // Performs a Spindle Referencing for tool alignment with Pocket
#define SCW 146 //Spindle CW
#define SCCW 145 //Spindle CCW
#define Coolant 158 //Activate Flood Coolant Pump

#define AirCyl 156 // Activate Air Cylinder for tool change
#define SpinToolRel 157 //Release tool from spindle when high
#define CarCW 154 //Carousel ++Pocket index
#define CarCCW 155 //Carousel --Pocket index

#define TMP 13 // which spare persist to use to transfer data
#include "KflopToKMotionCNCFunctions.c"
#define Zaxis 2


    
/*------------------------------
--Global variables declaration--
------------------------------*/
int x;
int index,CWPos,CCWPos,NSlot,Z_TcPos,ZUp_TcPos;
int *OldTool,*ActTool,*NewTool,*PrevSlot,*ActSlot,*ReqSlot;
unsigned short G,B,D;
unsigned short GRAY[10], BIN[10];
int c,d,p,T,*ptr;
int i,j,k;
int NSlot=10;// highest number tool slot in carousel
int CW();
int CCW();
int Compare();
int LeastTravel();
int Decimal();


 
int main()
{
    InitAux();
    AddKonnect(0,&VirtualBits,VirtualBitsEx);
    int FixtureIndex,Units, TWORD, HWORD, DWORD,ID;
    double NewToolLength,Length,OriginOffsetZ,AxisOffsetZ,XRes,YRes,ZRes;
    double Machinex,Machiney,Machinez,Machinea,Machineb,Machinec;
    double DROx,DROy,DROz,DROa,DROb,DROc;
    GrayToBinary16();
    AngularPosition();
    printf("\nDecimal=%d\n\n",D);
    printf("ERes=%d\n\n",ERes);

}
    unsigned int GrayToBinary16(){
        for(x=0;x<10;x++)
        {
            GRAY[x]=ReadBit(G1+x);
            printf("Bit#%d ",(G1+x));
            printf("G%d=%d\n",x,GRAY[x]);
        }
        printf("\n");
        BIN[9]=*ptr=GRAY[9];
        
        for(x=8;x>=0;x--)
        {            
            BIN[x]=GRAY[x]^BIN[x+1];
        }
        D=0;
        for(x=9;x>=0;x--)
        {
            printf("B%d=%d ",x,BIN[x]);
            p=BIN[x]*(1<<x);
            printf("p%d=%d \n",x,p);
            D=D+p;
        }    
        return D;
    }
                
    float AngularPosition(D){
/*This function converts the decimal value from the absolute encoder to the actual positioning angle*/
    float alphaRes;
    float alpha;
    alphaRes=360/1024;//0.3515625;
    alpha=D*alphaRes;
    printf("D=%d\talphaRes=%f\talpha=%f\n\n",D,alphaRes,alpha);
    return alpha, alphaRes;
    
    }
Group: DynoMotion Message: 14172 From: Tom Kerekes Date: 11/26/2016
Subject: Re: Absolute encoder - Gray to Binary conversion
In C if you divide integer types the compiler will do integer division. So:

1024/360 = 2

360/1024 = 0

Instead do 1024.0f / 360.0f to do 32-bit floating point division.

Other issues I see is 

that your function doesn't have a parameter type specified. 

All functions should be declared before they are used. 

You are returning two values. Only one can be returned. 

HTH
Regards
TK

On Nov 26, 2016, at 12:05 PM, fouijar@... [DynoMotion] <DynoMotion@yahoogroups.com> wrote:

 

Hello,

Just a quick question about the function below, I try to divide the resolution of the encoder by 360 degrees to get the angular resolution per degree but I can't get any relevant results:


Bit#1046 G0=1
Bit#1047 G1=0
Bit#1048 G2=1
Bit#1049 G3=0
Bit#1050 G4=1
Bit#1051 G5=0
Bit#1052 G6=1
Bit#1053 G7=0
Bit#1054 G8=0
Bit#1055 G9=1

B9=1 p9=512
B8=1 p8=256
B7=1 p7=128
B6=0 p6=0
B5=0 p5=0
B4=1 p4=16
B3=1 p3=8
B2=0 p2=0
B1=0 p1=0
B0=1 p0=1
D=921    alphaRes=0.000000    alpha=0.000000


Decimal=921

ERes=1024

The problem lies within the division and multiplication operations (alphaRes,alpha)

I think I'm missing something but can't figure what...


The full test program is also included below for reference and is intended to be included within the tool change program.


Thanks for your attention,


Jerome

          
    float AngularPosition(D){
/*This function converts the decimal value from the absolute encoder to the actual positioning angle*/
    float alphaRes;
    float alpha;
    alphaRes=1024/360;//2.844444
    alpha=D*alphaRes;
    printf("D=%d\talphaRes=%f\talpha=%f\n\n",D,alphaRes,alpha);
    return alpha, alphaRes;
   
    }


    /*Full conversion test program*/

#include "KMotionDef.h"


#define QA 40    // define to which IO bits the AB signals are connected
#define QB 41    

#define TAU 0.05 // smoothness factor (Low Pass Time constant seconds)
#define FINAL_TIME .5 // Set final dest after this amount of time with no change
#define ERes 1024 //Encoder Resolution, 10bits

/*INPUTS and OUTPUTS*/
/*INPUTS*/
#define AirCylHomePos 138 //Carousel air cylinder is at home position
#define AirCylToolPos 139 //Carousel air cylinder is at tool change position
#define ToolClamped 140 //Tool is clamped into the spindle
#define ToolUnClamped 141 //Tool is released from spindle
#define Index 137 //Indexing sensor to index the carousel position at each motor turn, CW or CCW
#define CarRef 25 //When active carousel is at reference position corresponding to pocket #1
#define J 1031// Indexing sensor to control the indexing motor CW/CCW
#define K 1030// Indexing sensor to control the indexing motor CW/CCW
#define G1 1046// Konnect: 10bits for the gray code G1=>G10
#define G2 1047
#define G3 1048
#define G4 1049
#define G5 1050
#define G6 1051
#define G7 1052
#define G8 1053
#define G9 1054
#define G10 1055
/*OUTPUTS*/
#define SpindleRef 144 // Performs a Spindle Referencing for tool alignment with Pocket
#define SCW 146 //Spindle CW
#define SCCW 145 //Spindle CCW
#define Coolant 158 //Activate Flood Coolant Pump

#define AirCyl 156 // Activate Air Cylinder for tool change
#define SpinToolRel 157 //Release tool from spindle when high
#define CarCW 154 //Carousel ++Pocket index
#define CarCCW 155 //Carousel --Pocket index

#define TMP 13 // which spare persist to use to transfer data
#include "KflopToKMotionCNCFunctions.c"
#define Zaxis 2


    
/*------------------------------
--Global variables declaration--
------------------------------*/
int x;
int index,CWPos,CCWPos,NSlot,Z_TcPos,ZUp_TcPos;
int *OldTool,*ActTool,*NewTool,*PrevSlot,*ActSlot,*ReqSlot;
unsigned short G,B,D;
unsigned short GRAY[10], BIN[10];
int c,d,p,T,*ptr;
int i,j,k;
int NSlot=10;// highest number tool slot in carousel
int CW();
int CCW();
int Compare();
int LeastTravel();
int Decimal();


 
int main()
{
    InitAux();
    AddKonnect(0,&VirtualBits,VirtualBitsEx);
    int FixtureIndex,Units, TWORD, HWORD, DWORD,ID;
    double NewToolLength,Length,OriginOffsetZ,AxisOffsetZ,XRes,YRes,ZRes;
    double Machinex,Machiney,Machinez,Machinea,Machineb,Machinec;
    double DROx,DROy,DROz,DROa,DROb,DROc;
    GrayToBinary16();
    AngularPosition();
    printf("\nDecimal=%d\n\n",D);
    printf("ERes=%d\n\n",ERes);

}
    unsigned int GrayToBinary16(){
        for(x=0;x<10;x++)
        {
            GRAY[x]=ReadBit(G1+x);
            printf("Bit#%d ",(G1+x));
            printf("G%d=%d\n",x,GRAY[x]);
        }
        printf("\n");
        BIN[9]=*ptr=GRAY[9];
        
        for(x=8;x>=0;x--)
        {            
            BIN[x]=GRAY[x]^BIN[x+1];
        }
        D=0;
        for(x=9;x>=0;x--)
        {
            printf("B%d=%d ",x,BIN[x]);
            p=BIN[x]*(1<<x);
            printf("p%d=%d \n",x,p);
            D=D+p;
        }    
        return D;
    }
                
    float AngularPosition(D){
/*This function converts the decimal value from the absolute encoder to the actual positioning angle*/
    float alphaRes;
    float alpha;
    alphaRes=360/1024;//0.3515625;
    alpha=D*alphaRes;
    printf("D=%d\talphaRes=%f\talpha=%f\n\n",D,alphaRes,alpha);
    return alpha, alphaRes;
    
    }

Group: DynoMotion Message: 14179 From: fouijar Date: 11/28/2016
Subject: Re: Absolute encoder - Gray to Binary conversion
Hello Tom,

Thanks for your input, long time no programming...
I did change the program as you instructed me to and it works fine. I still need to do many changes to the main program to use it but functionality first, fine tuning later.

I didn't see any code for absolute encoder use on this forum and will  post my "generic"  code within my repertory for anyone interested.
I think your advice would be greatly appreciated to optimize the code quality, questions on the matter will certainly come later.

As always thanks for the great support, best regards,

Jérôme
Group: DynoMotion Message: 14180 From: Tom Kerekes Date: 11/28/2016
Subject: Re: Absolute encoder - Gray to Binary conversion

Hi Jérôme,

That would be nice of you.

We'd prefer if you put it in the wiki instead.  Maybe here:

http://www.dynomotion.com/wiki/index.php?title=Main_Page#Software-Specific_Problems_and_Resolutions


If you do I will try to optimize it.  It would be fun to time it now with:

double T0=Time_sec();

.

.

.


double T1=Time_sec();

printf("Time=%fus\n",(T1-T0)*1e6);

Regards
TK

On 11/28/2016 5:50 PM, fouijar@... [DynoMotion] wrote:
 

Hello Tom,

Thanks for your input, long time no programming...
I did change the program as you instructed me to and it works fine. I still need to do many changes to the main program to use it but functionality first, fine tuning later.

I didn't see any code for absolute encoder use on this forum and will  post my "generic"  code within my repertory for anyone interested.
I think your advice would be greatly appreciated to optimize the code quality, questions on the matter will certainly come later.

As always thanks for the great support, best regards,

Jérôme