Rachel Ireland-Jones / Mbed OS FinalYear0
Revision:
16:afe936377d66
Parent:
15:b7ca53716b05
Child:
17:a92d96b65cbc
--- a/main.cpp	Tue Nov 19 19:49:13 2019 +0000
+++ b/main.cpp	Wed Nov 20 14:40:26 2019 +0000
@@ -1,32 +1,35 @@
 //Enhancement 2//
+//Enhancement 2//
+
 #include "mbed.h"
+
 //Status LED
 DigitalOut led(LED1);
- 
+
 //Motor PWM (speed)
 PwmOut PWMA(PA_8);
 PwmOut PWMB(PB_4);
- 
+
 //Motor Direction
 DigitalOut DIRA(PA_9);
 DigitalOut DIRB(PB_10);
- 
+
 //Hall-Effect Sensor Inputs
 DigitalIn HEA1(PB_2);
 DigitalIn HEA2(PB_1);
 DigitalIn HEB1(PB_15);
 DigitalIn HEB2(PB_14);
-  
+
 //On board switch
 DigitalIn SW1(USER_BUTTON);
- 
+
 //Use the serial object so we can use higher speeds
 Serial terminal(USBTX, USBRX);
- 
+
 //Timer used for measuring speeds
 Timer timer;
 Timer timer1;
- 
+
 //Enumerated types
 enum DIRECTION   {FORWARD=0, REVERSE};
 enum PULSE       {NOPULSE=0, PULSE};
@@ -43,7 +46,8 @@
 float dis;
 float trav =0;
 float speedA, speedB = 1.0f;
-float paceA, paceB = 1.0f;
+float paceA = 1.0f;
+float paceB = 1.0f;
 void time()
     {
         //Reset timer and Start
@@ -53,7 +57,7 @@
     //**********************
     //TIME THE FULL SEQUENCE
     //**********************
-    
+
     //Wait for rising edge of A1 and log time
     while (HEA1 == NOPULSE);
     tA1[0] = timer.read_us();
@@ -103,14 +107,14 @@
     speedA = fA/20.8f;
     speedB = fB/20.8f;
     }
-         
+
 void reset()
 {
     timer1.reset();
     time();
 }
 
-void oneRPS ()
+void oneRPS()
 {
     time();
     if (speedA <= 1.0f)
@@ -118,41 +122,43 @@
         paceA +=0.01f;
         wait_ms(25);
     }
-    else if (speedA >= 1.0f)
+    if (speedA >= 1.0f)
     {
         paceA -=0.01f;
         wait_ms(25);
     }
-    else if (speedB <= 1.0f)
+    if (speedB <= 1.0f)
     {
         paceB +=0.01f;
         wait_ms(25);
     }
-    else if (speedB >= 1.0f)
+    if (speedB >= 1.0f)
     {
         paceB -=0.01f;
         wait_ms(25);
     }
+    PWMA.write(paceA);
+    PWMB.write(paceA); 
 }
-      
+
 int main()
 {
-    
+
     //Configure the terminal to high speed
     terminal.baud(115200);
- 
+
     //Set initial motor direction
     DIRA = FORWARD;
     DIRB = FORWARD;
- 
+
     //Set motor period to 100Hz
     PWMA.period_ms(10);
     PWMB.period_ms(10);
- 
+
     //Set initial motor speed to stop
     PWMA.write(0.0f);           //0% duty cycle
     PWMB.write(0.0f);           //0% duty cycle
- 
+
     //Wait for USER button (blue pull-down switch) to start
     terminal.puts("Press USER button to start");
     led = 0;
@@ -165,7 +171,7 @@
     //*********************************************************************
     //FIRST TIME - SYNCHRONISE (YOU SHOULD NOT NEED THIS ONCE IT's RUNNING)
     //*********************************************************************
-    
+
     //Wait for rising edge of A1 and log time
     while (HEA1 == NOPULSE);
     //Wait for rising edge of A2 and log time (30 degrees?)
@@ -182,74 +188,29 @@
     while (HEB1 == PULSE);
     //Wait for falling edge of B2
     while (HEB2 == PULSE);
-    
+
     //Set initial motor speed to stop
     PWMA.write(0.0f);          //Set duty cycle (%)
     PWMB.write(0.0f);          //Set duty cycle (%)
- 
+
     //Wait - give time to start running
     wait(1.0);
     timer1.reset();
     timer1.start();
     //Main polling loop
-    
-    float paceA = 1.0f;
-    float paceB = 1.0f;
+
+
     PWMA.write(paceA);          //Set duty cycle (%)
     PWMB.write(paceB);
     while(1)
     {
-        oneRPS();
-        while(trav <= 1250)  
-            {  
-               PWMA.write(paceA);          //Set duty cycle y  
-               PWMB.write(paceB);
-               time();
-               oneRPS();
-            } 
-        reset();
-        while(trav <= 330)  
-            {  
-               PWMA.write(paceA);           
-               PWMB.write(0.0f);
-               time();
-               oneRPS();
-            }
-        reset();
-        while(trav <= 1457)  
-            {  
-               PWMA.write(paceA);           
-               PWMB.write(paceB);
-               time();
-               oneRPS();
-            }
-        reset();
-        while(trav <= 268)  
-            {  
-               PWMA.write(paceA);           
-               PWMB.write(0.0f);
-               time();
-               oneRPS();
-            }
-        reset();
-        while(trav <= 750)  
-            {  
-               PWMA.write(paceA);           
-               PWMB.write(paceB);
-               time();
-               oneRPS();
-            }
-        reset();
-        while(trav <= 200)  
-            {  
-               PWMA.write(paceA);           
-               PWMB.write(0.0f);
-               time();
-               oneRPS();
-            }  
-        timer.stop();   
-        break;
+        
+        while (trav <= 1250)
+        {
+            oneRPS();
+        }
+    PWMA.write(0.0f);          //Set duty cycle (%)
+    PWMB.write(0.0f);
+    break;
     }
-    PWMA.write(0.0f);           
-    PWMB.write(0.0f);
 }
\ No newline at end of file