update

Dependencies:   Stepper mbed SDFileSystem Ultrasonic PinDetect millis

main.cpp

Committer:
rschimpf78
Date:
2018-09-14
Revision:
2:e8f8095464cf
Parent:
0:88834eed3de0
Child:
3:fd2ca631ab44

File content as of revision 2:e8f8095464cf:

#include "mbed.h"
#include "Stepper.h"
#include "string"
DigitalOut in1(D10);///in1 d9 and in2 d10 
DigitalOut in2(D9);
DigitalIn endSwitch(D6); 
DigitalIn homeSwitch(D7);
DigitalIn bottomSwitch(D5);
Stepper mot(D3,D4);  //(D5 to PUL+, D4 to DIR+)
DigitalOut en(D2); 

Serial pc(USBTX, USBRX);

volatile int plates = 1;
    
void position()
{
    char str1[20];
    char str2[20];
    char str3[20];
    strcpy(str1,"HOME");
    strcpy(str2,"ERROR");
    strcpy(str3,"IN BEAM PATH");
    
    if(homeSwitch==0 && endSwitch==1) 
    {
    pc.printf(" pmma state - %s\n",str1);
    }
    else if (homeSwitch==1 && endSwitch==0) 
    {
    pc.printf(" pmma state - %s\n",str3);
    }
    else if (homeSwitch==1 && endSwitch==1) 
    {
    pc.printf(" pmma state - %s\n",str2);
    }
    else 
    {
    pc.printf(" pmma state - else");
    }
}  
void retractPMMA()
{
    while(homeSwitch == 1)
    {
    in1=1;
    in2=0;
    }
    in1=1;
    in2=1;
}
void sendPMMA()                    //Command: Send plates
{ 

    in1=0;
    in2=1;
    wait(9);
    in1=1;
    in2=1;
}

void autoCalibrate()             //if machine is plugged in when the plate selector isn't at the origin...
{
    while(homeSwitch==1)
    {
        retractPMMA();
    }
    
    in1=1;
    in2=1;
    en = 0;                     //...it automatically lowers it to the origin position
    mot.setSpeed(600);
    mot.rotate(1);
    while(bottomSwitch);        //While bottom switch is unpressed
    mot.stop();                 //Stop rotation when switch is pressed (pressed = logical level low)                //Disable driver
    mot.setPositionZero();
    wait(1);
    mot.goesTo(-800);
    while(!mot.stopped());
    mot.stop();
    mot.setPositionZero();
    en=1;     //Set absolute origin for plate selector at switch hit
}
void internalpullups()
{
    bottomSwitch.mode(PullUp);
    homeSwitch.mode(PullUp);
    endSwitch.mode(PullUp);
    wait (.01);
}
void numberofPlates()       // Command: Number of PMMA to send (plates)         
{   
    int stepheight;
    
    if (plates <= 0 || plates >= 17)
    {
        pc.printf(" ERROR invalid range |");
    } 
    else if(endSwitch == 1 && homeSwitch==0)
    { 
        en = 0;
        mot.goesTo((plates-1)*-1680); //plates per step
        while(!mot.stopped());
        stepheight = mot.getPosition();
        pc.printf(" selector height %d steps |",stepheight);
        mot.stop();
        en = 1;
        wait(1);
        sendPMMA();
    }
    else
    {
    pc.printf("Position Error - Cannot Send");  
    }
}
void selectionStatus()
{
    int stepperposition;
    double selectedplates;
    stepperposition = mot.getPosition();
    selectedplates =  stepperposition/1621;
    pc.printf("\n Number of plates currently selected: %d ",selectedplates);
}
void deviceinfo()
{
    printf("\nUnique ID: 0240000041114e4500513007bcf9000a9e51000097969900");
    printf("\nBootloader Version: 0244");
    printf("\nBUILD: 1:4BDA04E+");
    printf("\nBUILD TIMESTAMP=3/18/2018\n\n");
    
}

int main()
{   
    in1=1;
    in2=1;
    en = 1;
    wait(0.1);
    internalpullups();
    wait(0.1);
    autoCalibrate();  
   
    char command[15] = {0};
    pc.printf("\nCommands:\n\n\tsend [N]\t**places N PMMA sheets in beam path\n\tretract\t\t**removes all PMMA from beam path\n\tlocation\t**returns location of PMMA\n\tinfo\t\t**returns device and firmware information\n\n");
    
    while(1) 
    {
        
        pc.printf("\nRequests:  ");
        pc.scanf("%s",&command);
    
        if (strcmp (command, "send") == 0) 
        {   
            pc.scanf(" %d",&plates);
            pc.printf("\nresponse recieved |");
            numberofPlates();
            position();
        }
        else if (strcmp (command, "retract") == 0)
        {   
            pc.printf("\nresponse recieved |");
            retractPMMA();
            position();
        }
        else if (strcmp (command, "location") == 0)
        {
            pc.printf("\nresponse recieved |");
            position();
        }
        else if (strcmp (command, "info") == 0)
        {
            pc.printf("\nresponse recieved |");
            deviceinfo(); 
        }
        else if (strcmp (command, "height") == 0)
        {
            pc.printf("\nresponse recieved |");
            numberofPlates();
        }
     
        else
        {
            pc.printf("\nERROR-command invalid");       
        }
    }
}