ES2017 coursework 2

Dependencies:   PID

Fork of ES_CW2_Starter by Edward Stott

Revision:
7:5932ed0bad6d
Parent:
6:4edbe75736d9
Child:
8:77627657da80
--- a/main.cpp	Thu Mar 02 17:04:14 2017 +0000
+++ b/main.cpp	Thu Mar 02 19:05:49 2017 +0000
@@ -48,7 +48,8 @@
 DigitalOut led1(LED1);
 
 //Photointerrupter inputs
-DigitalIn I1(I1pin);
+//DigitalIn I1(I1pin);
+InterruptIn I1(I1pin);
 DigitalIn I2(I2pin);
 DigitalIn I3(I3pin);
 
@@ -66,6 +67,11 @@
 float spinWait = 10;
 float revsec = 0;
 
+//Timer used for calculating speed
+Timer speedTimer;
+float revs = 0, revtimer = 0;
+Ticker printSpeed;
+
 Serial pc(SERIAL_TX, SERIAL_RX);
 
 int8_t orState = 0;    //Rotor offset at motor state 0
@@ -74,8 +80,6 @@
 
 int i=0;
 
-
-
 //Set a given drive state
 void motorOut(int8_t driveState)
 {
@@ -129,11 +133,27 @@
     if(revsec) spinTimer.attach(&fixedSpeed, spinWait);
 }
 
+void rps()
+{
+//      pc.printf("Tick\r\n");
+    speedTimer.stop();
+    revtimer = speedTimer.read_ms();
+    revs = 1000/(revtimer);
+//      pc.printf("Revs / sec: %2.2f\r", revs);
+    speedTimer.start();
+}
+
+void speedo()
+{
+    pc.printf("Revs / sec: %2.4f\r", revs);
+    printSpeed.attach(&speedo, 1.0);
+}
+
 //Main function
 int main()
 {
     pc.printf("Hello\n\r");
-    
+
     //Run the motor synchronisation
     orState = motorHome();
     //orState is subtracted from future rotor state inputs to align rotor and motor states
@@ -144,18 +164,21 @@
     int index=0;
     int units = 0, tens = 0, decimals = 0;
     char ch;
-    
+
+    speedTimer.start();
+    I1.rise(&rps);
+
     while(1) {
         //Toggle LED so we know something's happening
         clk = !clk;
-        
+
         //If there's a character to read from the serial port
         if (pc.readable()) {
-            
+
             //Clear index counter and control variables
             index = 0;
-            revsec = spinWait = 0;
-            
+//            revsec = spinWait = 0;
+
             //Read each value from the serial port until Enter key is pressed
             do {
                 //Read character
@@ -164,50 +187,53 @@
                 pc.putc(ch);
                 //Add character to input array
                 command[index++]=ch;  // put it into the value array and increment the index
-            //d10 and d13 used for detecting Enter key on Windows/Unix/Mac
+                //d10 and d13 used for detecting Enter key on Windows/Unix/Mac
             } while(ch != 10 && ch != 13);
-            
+
             //Start new line on terminal for printing data
             pc.putc('\n');
             pc.putc('\r');
-            
+
             //Analyse the input string
             switch (command[0]) {
-                //If a V was typed...
+                    //If a V was typed...
                 case 'V':
-                
-                //For each character received, subtract ASCII 0 from ASCII
-                //representation to obtain the integer value of the number
-                   
+                    units = 0, tens = 0, decimals = 0;
+                    //For each character received, subtract ASCII 0 from ASCII
+                    //representation to obtain the integer value of the number
+
                     //If decimal point is in the second character (eg, V.1)
                     if(command[1]=='.') {
                         //Extract decimal rev/s
                         decimals = command[2] - '0';
-                    
-                    //If decimal point is in the third character (eg, V0.1)
+
+                        //If decimal point is in the third character (eg, V0.1)
                     } else if(command[2]=='.') {
                         units = command[1] - '0';
                         decimals = command[3] - '0';
-                    
-                    //If decimal point is in the fourth character (eg, V10.1)
+
+                        //If decimal point is in the fourth character (eg, V10.1)
                     } else if(command[3]=='.') {
                         tens = command[1] - '0';
                         units = command[2] - '0';
                         decimals = command[4] - '0';
                     }
-                    
+
                     //Calculate the number of revolutions per second required
                     revsec = float(tens)*10 + float(units) + float(decimals)/10;
                     //Calculate the required wait period
                     spinWait = (1/revsec)/6;
-                    
+
                     //Print values for verification
-                    pc.printf("Rev/S: %2.2f, Wait: %2.2f\n\r", revsec, spinWait);
-                    
+                    pc.printf("Rev/S: %2.4f, Wait: %2.4f\n\r", revsec, spinWait);
+
                     //Run the function to start rotating at a fixed speed
                     fixedSpeed();
                     break;
-                //If anything unexpected was received
+                    //If anything unexpected was received
+                case 's':
+                    pc.printf("Revs / sec: %2.2f\r", revs);
+                    break;
                 default:
                     //Set speed variables to zero to stop motor spinning
                     revsec=0;
@@ -216,6 +242,8 @@
                     break;
             }
         }
+//        printSpeed.attach(&speedo, 1.0);
+//        pc.printf("Revs / sec: %2.2f\r", revs);
     }
 
 }