Cycle Testing

Dependencies:   Stepper mbed HCSR04 millis

Revision:
2:e8f8095464cf
Parent:
0:88834eed3de0
Child:
3:fd2ca631ab44
--- a/main.cpp	Sun Mar 18 23:51:04 2018 +0000
+++ b/main.cpp	Fri Sep 14 19:10:45 2018 +0000
@@ -1,283 +1,186 @@
-/*#include "mbed.h"
-#include <vector>
-//--------Brushed Motor -------------------
-DigitalOut in3(PTC17); 
-DigitalOut in4(PTC16);
-//---------Stepper Motor ------------------
-Stepper mot(PTA1,D2);//d2 and d3
-DigitalOut en(D4); 
-Serial pc(USBTX, USBRX); 
- 
-//--Internal pullup resistors for Bumper Switches---
-DigitalIn bswitch(D1);
-DigitalIn tswitch(D0);
-int main() {
-    vector<int> time;
-    vector<int> plates;
-    int n,t,h,m,s = 0; 
-    int tests = 0;
-    PULp.period(0.0006f); 
-    ENp = 1;
-    DIRp = 0;
-    PULp.write(0.5f); 
-    in3=0;
-    in4=0;
-    pc.printf("\nCommands  ");
-    pc.scanf("%d",&tests); 
-    
-    for (int i = 1; i<=tests;i++){
-        pc.printf("\nEnter the Number of Plates for test %d: ",i);
-        pc.scanf("%d",&n);
-        pc.printf("\nEnter hours and minutes to test these plates\n\nHours: ");
-        pc.scanf("%d",&h);
-        pc.printf("Minutes: ");
-        pc.scanf("%d",&m);
-        pc.printf("Seconds: ");
-        pc.scanf("%d",&s);
-        t=(h*3600)+(m*60)+s;
-        time.push_back(t);
-        plates.push_back(n);
-    }
-    pc.printf("\nTesting in Progress...");
-   for (unsigned y=0; y<time.size(); y++){
-       
-        //--------Turn Stepper on for a certain amount of time based on the number of plates----
-        in3=0;   // stop actuator
-        in4=0;   // stop actuator
-        ENp = 0; 
-        DIRp = 0;
-        wait(1.9*(plates.at(y)-1)); 
-        ENp = 1;
-        wait(0.5);
-        //-------Move Plates Under X ray Beam---------------------------------------------------
-        in3=0;
-        in4=1;
-        wait(14); //reaches end after 10 seconds
-        in3=0;   // stop actuator
-        in4=0;   // stop actuator
-        //-----Test the Plate for specified time ----------------------------------------------
-        wait(time.at(y)); //Leave plates in beam path for the user inputted amount of time
-        //-----Retract plates to original position---------------------------------------------
-        in3=1;
-        in4=0;
-        wait(14); 
-        in3=0; 
-        in4=0; 
-        wait(0.5);
-        //--------Return Stepper to base height with one plate selected------------------------
-        ENp = 0; 
-        DIRp = 1; 
-        wait(2*(plates.at(y)-1));
-        ENp = 1; 
-        //-------------------------------------------------------------------------------------
-        pc.printf("\n\nTest number %d complete", y+1);
-        pc.printf("\nNumber of Plates: %d", plates.at(y));
-        pc.printf("\nLength of Test: %d seconds", time.at(y));
-        }
-        pc.printf("\n\nAll testing complete");
-    }
-*/
-
-/*#include "mbed.h" 
+#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);
- 
-int main() {
-    char c;
-    char buffer[128];
-    pc.gets(buffer, 4);
-    pc.printf("I got '%s'\n", buffer);  
-    if(strcmp(buffer,"on")==0) {
-        printf("success");
-        }   
-    else{
-        printf("fail");
-        }   
-}
-*/
-#include "mbed.h"
-#include "Stepper.h"
-DigitalIn homeSwitch(D8);
-DigitalIn endSwitch(D1);
-DigitalIn bottomSwitch(D0);
-DigitalIn topSwitch(D9);
-Stepper mot(PTA1,D2);
-DigitalOut en(D4); 
-DigitalOut in1(D11); 
-DigitalOut in2(D10);
-Serial pc(USBTX, USBRX);
 
-void positionStatus()
+volatile int plates = 1;
+    
+void position()
 {
-     int plateposition;  //plate position -> 1 is home, 2 is traveling, 3 is under beam path 
-     if(homeSwitch==0 && endSwitch==1)
-     {
-      plateposition = 1;
-      pc.printf("\nPlate position is home %d",plateposition);
-     } 
-     else if (homeSwitch==1 && endSwitch==1)
-     {
-      plateposition = 2;
-      pc.printf("\nPlates are currently moving %d",plateposition); 
-     }
-     else if(homeSwitch==1 && endSwitch==0)
-     {
-     plateposition = 3;
-     pc.printf("\nPlates are under beam path %d",plateposition); 
-     }
-     else
-     {
-      pc.printf("\nError, cannot locate plate 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;
-    wait(14);
+    }
+    in1=1;
+    in2=1;
+}
+void sendPMMA()                    //Command: Send plates
+{ 
+
     in1=0;
-    in2=0;
-    positionStatus();
+    in2=1;
+    wait(9);
+    in1=1;
+    in2=1;
 }
 
-void sendPMMA()                                                   //Command: Send plates
- {                                                                //Returns: plates arrived(),plates didn't arrive() 
-    wait(2);
+void autoCalibrate()             //if machine is plugged in when the plate selector isn't at the origin...
+{
+    while(homeSwitch==1)
+    {
+        retractPMMA();
+    }
+    
     in1=1;
-    in2=0;
-    wait(14);
-    in1=0;
-    in2=0;
-    positionStatus();
+    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 autoCalibrate()     //if machine is plugged in when the plate selector isn't at the origin...
-{   
-    en = 0;                      //...it automatically lowers it to the origin position 
-    mot.setSpeed(800);
-    mot.rotate(0);                 
-    while(bottomSwitch);        //While bottom switch is unpressed   
-    mot.stop();                 //Stop rotation when switch is pressed (pressed = logical level low)           
-    en=1;                       //Disable driver
-    mot.setPositionZero();      //Set absolute origin for plate selector at switch hit
-}
-
 void internalpullups()
 {
-    bottomSwitch.mode (PullUp);
-    topSwitch.mode (PullUp);
-    homeSwitch.mode (PullUp);
-    endSwitch.mode (PullUp);
+    bottomSwitch.mode(PullUp);
+    homeSwitch.mode(PullUp);
+    endSwitch.mode(PullUp);
     wait (.01);
 }
-
-void numberofPlates()       // Command: Number of PMMA to send (plates)
-                            // Returns: Actual number of plates selected (selectedplates), selectionerror (1 for command error, 0 for correct command) 
-{    
-     int stepheight,plates,selectionerror;
-     double selectedplates;                        
-     pc.printf("\nEnter the Number of Plates to send ");
-     pc.scanf("%d",&plates);
-     if (plates <= 0)
-     { 
-     pc.printf("\nNot a valid number, must be 1 to 16");
-     selectionerror = 1;
-     }
-     else if (plates >= 17)
-     { 
-     pc.printf("\nNot a valid number, must be 1 to 16");
-     selectionerror = 1;
-     }
-     else
-     {
-     selectionerror = 0;
-     pc.printf("\nCommand Valid\n"); 
-     en = 0;
-     mot.goesTo(plates*1620);
-     while(!mot.stopped());
-     stepheight = mot.getPosition();        
-     selectedplates = stepheight/1620;                 
-     pc.printf("\n Steps taken: %d ",selectedplates);  
-     mot.stop();
-     en = 1;   
+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 stepheight;
-   double selectedplates;
-   stepheight = mot.getPosition();        
-   selectedplates = stepheight/1620;                 
-   pc.printf("\n Number of plates currently selected: %d ",selectedplates);
-     
-}                             
+    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()
-{  
-    char command[256] = { 0 };
+{   
+    in1=1;
+    in2=1;
+    en = 1;
+    wait(0.1);
     internalpullups();
-    wait (.01);
-    autoCalibrate(); //...it automatically lowers it to the origin position for initial power up
-                     
-    while(1)
+    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("\nEnter Command: \n send \n retract \n position (get status) \n selection (get status)" );
-      pc.scanf("\n%s", &command);
-        if (strcmp (command, "send") == 0)
-        {  
-            pc.printf ("\nCommand Recieved: Send");   //confirmation of recieved command
-            numberofPlates();                       //select number of plates to send
-            sendPMMA();                  
+        
+        pc.printf("\nRequests:  ");
+        pc.scanf("%s",&command);
+    
+        if (strcmp (command, "send") == 0) 
+        {   
+            pc.scanf(" %d",&plates);
+            pc.printf("\nresponse recieved |");
+            numberofPlates();
+            position();
         }
-        if (strcmp (command, "retract") == 0)
-        {     
-            pc.printf ("\nCommand Recieved: retract");
-            retractPMMA();   
-        } 
-        if (strcmp (command, "position") == 0)
-        {     
-            pc.printf ("\nCommand Recieved: position");
-            positionStatus();  
+        else if (strcmp (command, "retract") == 0)
+        {   
+            pc.printf("\nresponse recieved |");
+            retractPMMA();
+            position();
         }
-        if (strcmp (command, "selection") == 0)
+        else if (strcmp (command, "location") == 0)
         {
-            pc.printf ("\nCommand Recieved: selection");
-            selectionStatus();
+            pc.printf("\nresponse recieved |");
+            position();
+        }
+        else if (strcmp (command, "info") == 0)
+        {
+            pc.printf("\nresponse recieved |");
+            deviceinfo(); 
         }
-    }  
-}    
-
-
-/* int main () 
-{  
-    int steps, plates;
-    bswitch.mode (PullUp); 
-    wait (.01);
-    mot.setSpeed (600);
-    mot.rotate (0);         //if machine is plugged in when the plate selector isn't at the origin...
-    
-    while (bswitch);        //...it automatically lowers it to the origin positio
-    mot.stop ();            //Stop rotation when switch is pressed (pressed = logical level low)
-    en = 1;                 //Disable driver
-    mot.setPositionZero (); //Set absolute origin for plate selector at switch hit
-    char c[256] = { 0 };
-    
-    while (1)
-    {
-        pc.printf("Choose a command:\nPMMA 1");   
-        pc.scanf ("%s", &c);   
-        
-        if (strcmp (c, "PMMA1") == 0)
-        {  
-            pc.printf ("\n match ");
+        else if (strcmp (command, "height") == 0)
+        {
+            pc.printf("\nresponse recieved |");
+            numberofPlates();
         }
-      
-        if (strcmp (c, "no") == 0)
-        {     
-            pc.printf ("\n not match ");
-        }   
+     
+        else
+        {
+            pc.printf("\nERROR-command invalid");       
+        }
     }
-}
-*/
\ No newline at end of file
+}
\ No newline at end of file