Grupo T / Mbed OS GRUPOT

main.cpp

Committer:
lucasfontenla
Date:
2018-05-04
Revision:
12:801e58a7137c
Parent:
11:0e22a6fc7f87
Child:
13:0f385bfe3e0d

File content as of revision 12:801e58a7137c:

#include "mbed.h"

// Classes ---------------------------------------------------------------------------------
Timer debounce;

// MACHINE SETUP ---------------------------------------------------------------------------

// drivers output signal
DigitalOut stepX(D2);
DigitalOut dirX(D3);

DigitalOut stepY(D8);
DigitalOut dirY(D9);

DigitalOut stepZ(D11);
DigitalOut dirZ(D12);

// hardware input signal
// end-of-stroke sensors
InterruptIn endY1(D5);
InterruptIn endY2(D13);

// IHM for development
AnalogIn joyX(A0);
AnalogIn joyY(A1);

DigitalIn zUp(D7);
DigitalIn zDwn(D6);

// variables definition
int modeStatus = 0;

float valX;
float valY;
int valZUp;
int valZDwn;

int activeX = 1;
int activeY = 1;
int activeZ = 1;

int x_dir;
int y_dir;
int z_dir;

int x_plus = 1;
int x_minus = 0;
int y_plus = 1;
int y_minus = 0;
int z_plus = 1;
int z_minus = 0;

int x_limit_min = 0;
int x_limit_max = 0;

int y_limit_min = 0;
int y_limit_max = 0;

float ppsMax = 600.0;

int totalX = 0;
int totalY = 0;

float X = 0;
float Y = 0;

int endX1Status;
int endX2Status;

// machine parameters
float xPitch = 3.0;
float yPitch = 3.0;

float motor_steps_by_rotation = 200.0;
float step_mode = 0.5;
float steps_by_rotation = motor_steps_by_rotation/step_mode;

// prototype functions
void setupPins();
void move(int pps, int x_dir, int y_dir, int z_dir, int x_step, int y_step, int z_step);
float distance(int steps, float pitch);

void endX1Int_press(void);
void endX1Int_release(void);
void endX2Int_press(void);
void endX2Int_release(void);

void endY1Int_press(void);
void endY1Int_release(void);
void endY2Int_press(void);
void endY2Int_release(void);

void zeroX(int pps);
void zeroY(int pps);

// MAIN PROGRAM ----------------------------------------------------------------

int contador = 0;

int main(){
    printf("Starting...\r\n");
 
    endX1.fall(&endX1Int_press);
    endX1.rise(&endX1Int_release);
    endX2.fall(&endX2Int_press);
    endX2.rise(&endX2Int_release);
    
    endY1.fall(&endY1Int_press);
    endY1.rise(&endY1Int_release);
    endY2.fall(&endY2Int_press);
    endY2.rise(&endY2Int_release);
    
    if(modeStatus){
        //Código de JOG
        printf("JOG Selected\n");
        while(1){        
            valX = joyX;
            valY = joyY;
            
            valZUp = zUp;
            valZDwn = zDwn;
            
            if(valX > 0.7){
                if(x_dir != x_minus){
                    x_dir = x_minus;
                }
                activeX = 1; 
                totalX-=1;    
            }
            else if(valX < 0.3){
                if(x_dir != x_plus){
                    x_dir = x_plus;   
                }
                activeX = 1;   
                totalX+=1;  
            }
            else{
                activeX = 0;
            }
            
            //----------------------------------------------------------------------
            
            if(valY > 0.7){
                if(y_dir != y_plus){
                    y_dir = y_plus;   
                }
                activeY = 1;  
                totalY+=1;   
            }
            else if(valY < 0.3){
                if(y_dir != y_minus){
                    y_dir = y_minus;    
                }
                activeY = 1; 
                totalY-=1;   
            }
            else{
                activeY = 0;
            }
            
            //----------------------------------------------------------------------   
            
            if(!valZUp && valZDwn){
                if(z_dir != z_minus){
                    z_dir = z_minus;
                    //totalZ-=1;    
                }
                activeZ = 1;    
            }
            else if(!valZDwn && valZUp){
                if(z_dir != z_plus){
                    z_dir = z_plus;
                    //totalZ+=1;   
                }
                activeZ = 1;    
            }
            else {
                activeZ = 0;    
            }
            
            //----------------------------------------------------------------------

            move(ppsMax, x_dir, y_dir, z_dir, activeX, activeY, activeZ);  
            X = distance(totalX, xPitch);
            Y = distance(totalY, yPitch);
            
            if(contador > 10000){
                X = distance(totalX, xPitch);
                Y = distance(totalY, yPitch);
                printf("X: %.2f     Y: %.2f\r\n", X, Y);
                contador = 0;
            }
            
            contador+=1;
        }
    }
    
    else {
        zeroY(ppsMax);
        
        X = distance(totalX, xPitch);
        Y = distance(totalY, yPitch);
        
        printf("X: %.2f     Y: %.2f\r\n", X, Y);
    }  
}  

// FUNCTIONS ----------------------------------------------------------------
void endX1Int_press(void){
    printf("X1 - press\n\r");
    x_limit_min = 1;  
}
void endX1Int_release(void){
    printf("X1 - release\n\r");
    x_limit_min = 0; 
}
void endX2Int_press(void){
    printf("X2 - press\n\r");
    x_limit_max = 1;   
}
void endX2Int_release(void){
    printf("X2 - release\n\r");
    x_limit_max = 0;   
}

void endY1Int_press(void){
    printf("Y1 - press\n\r");
    y_limit_min = 1;  
}
void endY1Int_release(void){
    printf("Y1 - release\n\r");
    y_limit_min = 0; 
}
void endY2Int_press(void){
    printf("Y2 - press\n\r");
    y_limit_max = 1;   
}
void endY2Int_release(void){
    printf("Y2 - release\n\r");
    y_limit_max = 0;   
}

void move(int pps, int x_dir, int y_dir, int z_dir, int x_step, int y_step, int z_step){
    float time = 1.0/pps/2.0;
    
    dirX = x_dir;
    dirY = y_dir;
    dirZ = z_dir;
    
    if((x_dir == x_minus) && x_limit_min){
        x_step = 0;    
    }
    if((x_dir == x_plus) && x_limit_max){
        x_step = 0;    
    }
    
    if((y_dir == y_minus) && y_limit_min){
        y_step = 0;    
    }
    if((y_dir == y_plus) && y_limit_max){
        y_step = 0;    
    }
    
    int max_val;
    
    // maior número de passos -------
    if(x_step > y_step){
        if(x_step > z_step){
            max_val = x_step;
        }
        else {
            max_val = z_step;
        }
    } else if(y_step > z_step){
        max_val = y_step;
    }
    else {
        max_val = z_step;    
    }
    
    for(int i = 0; i < max_val; i++){
        if(i >= x_step){
            stepX = 0;
        }
        else { stepX = 1; }
        
        if(i >= y_step){
            stepY = 0;
        }
        else {
            stepY = 1;}
        
        if(i >= z_step){
            stepZ = 0;
        }
        else { stepZ = 1; }
        
        wait(time);
        stepX = 0;
        stepY = 0;
        stepZ = 0;
        wait(time); 
    }
}

void zeroX(int pps){
    float time = 1.0/pps/2.0;
    
    while(!x_limit_min){
        if(dirX != x_minus){
            dirX = x_minus;    
        }
        stepX = 1;
        wait(time);
        stepX = 0;
        wait(time);  
    }
    
    printf("X zero\n\r");
    
    totalX = 0.0;
}

void zeroY(int pps){
    float time = 1.0/pps/2.0;
    
    while(!y_limit_min){
        if(dirY != y_minus){
            dirY = y_minus;    
        }
        stepY = 1;
        wait(time);
        stepY = 0;
        wait(time);  
    }
    
    printf("Y zero\n\r");
    
    totalY = 0.0;
}

float distance(int steps, float pitch){
    float delta_S = (((float)steps)/steps_by_rotation)*pitch;
    return delta_S;
}