set home joint 3+4

Dependencies:   mbed

main.cpp

Committer:
pbdt1997
Date:
2019-04-20
Revision:
1:5ed21efc1d58
Parent:
0:a3b83d874092
Child:
2:ee6d40dd8fb6
Child:
3:55fae8073ff1
Child:
4:fb0905390ebc

File content as of revision 1:5ed21efc1d58:

#include "mbed.h"

Ticker stepper;
//Ticker stepper2;

Serial pc(USBTX, USBRX);

SPISlave slave(PA_7, PA_6, PA_5, PA_15); //MOSI MISO CLK CS

//prox detects = 0
DigitalIn prox2(PA_14);
DigitalIn prox1(PA_13);

//Stepper Motor 1
DigitalOut PUL_1(D3);
DigitalOut DR_1(PC_0);

//Stepper Motor 2
DigitalOut PUL_2(D4);
DigitalOut DR_2(PC_1);

float q3 = 0, q4 = 0;
float countq3 = 0, countq4 = 0;
bool moveq3 = false, moveq4 = false;
bool stop = false;

//drive stepper motor

void drvStepper1(float angle){
    float step = angle - q3;
    step = step*44.4444444;
    if(step >= 0){
        for(int i=0; i<(int)step; i++){
            DR_1 = 0; //up
            PUL_1 = 1;
            wait_ms(1);
            PUL_1 = 0;
            wait_ms(1);
            countq3++;
        }
    }
    else if(step < 0){
        step = -step;
        for(int i=0; i<(int)step; i++){
            DR_1 = 1; //down
            PUL_1 = 1;
            wait_ms(1);
            PUL_1 = 0;
            wait_ms(1);
            countq3--;
        }
    }
}

void drvStepper2(float angle){
   // printf("hello");
    float step = angle - q4;
    step = step*11.1111111;
    if(step >= 0){
        for(int i=0; i<(int)step; i++){
            DR_2 = 0; //up
            PUL_2 = 1;
            wait_ms(1);
            PUL_2 = 0;
            wait_ms(1);
            countq4++;
        }
    }
    else if(step < 0){
        step = -step;
        for(int i=0; i<(int)step; i++){
            DR_2 = 1; //down
            PUL_2 = 1;
            wait_ms(1);
            PUL_2 = 0;
            wait_ms(1);
            countq4--;
        }
    }
}

void ISR_stepper(){
    if(moveq3 == true){
        drvStepper1(q3);
        moveq3 = false;
    }
    if(moveq4 == true){
        drvStepper2(q4);
        moveq4 = false;
    }
}

void setHome(){
    while(prox2 == 1){
        drvStepper2(1);
    }
    wait(0.5);
    drvStepper2(-138.51);
    wait(0.5);
    countq4 = 0;
    q4 = 0;
    while(prox1 == 1){
        drvStepper1(-1);
    }
    wait(0.5);
    drvStepper1(18);
    wait(0.5);
    countq3 = 0;
    q3 = 0;
}

int main() {
 //   stepper.attach(&ISR_stepper, 0.01);
//    stepper2.attach_us(&drvStepper2, 20);
    pc.baud(9600);
    setHome();
    printf("q3 = %.2f, q4 = %.2f\n", countq3,countq4);
    while(1){
    moveq4 = true;
    q4 = 10;
//    printf("done\n");
//        moveq3 = false;
//        break;
    wait(5);    
    }
//    drvStepper1(2000);
}