ES2017 coursework 2

Dependencies:   PID

Fork of ES_CW2_Starter by Edward Stott

Revision:
18:55cd33a3e69f
Parent:
17:9cd9f82027ca
Child:
19:93ca06d2e311
--- a/main.cpp	Thu Mar 09 15:15:37 2017 +0000
+++ b/main.cpp	Thu Mar 09 16:17:12 2017 +0000
@@ -47,8 +47,8 @@
 //Drive state to output table
 const int8_t driveTable[6] = {0x38, 0x2C, 0x0E, 0x0B, 0x23, 0x32};
 
-const int8_t AcwState[7] = {0x00, 0x23, 0x38, 0x32, 0x0E, 0x0B, 0x2C};
-const int8_t cwState[7] = {0x00, 0x0E, 0x23, 0x0B, 0x38, 0x2C, 0x32};
+const int8_t cwState[7] = {0x00, 0x23, 0x38, 0x32, 0x0E, 0x0B, 0x2C};
+const int8_t AcwState[7] = {0x00, 0x0E, 0x23, 0x0B, 0x38, 0x2C, 0x32};
 
 //Status LED
 DigitalOut led1(LED1);
@@ -148,21 +148,23 @@
     if(revsec) spinTimer.attach(&fixedSpeed, spinWait);
 }
 
-void spinToPos(){
+void spinToPos()
+{
     currRevs = 0;
-    
+    int remRevs = 0;
     revsec = 50.0;
     fixedSpeed();
-    while (targetRevs - currRevs > 100){
-        }
-        
+    while (targetRevs - currRevs > 100) {
+        printf("%d\r\n", currRevs);
+    }
+
     remRevs = targetRevs - currRevs;
-    while (remRevs > 3){
+    while (remRevs > 3) {
         revsec = remRevs/3;
         remRevs = targetRevs - currRevs;
-        }
-        
-    
+    }
+
+    motorStop();
 }
 
 void rps()
@@ -217,7 +219,6 @@
 
 
 //Main function
->>>>>>> other
 int main()
 {
     pc.printf("spin\n\r");
@@ -234,6 +235,8 @@
     char ch;
     testpin=0;
     int vartens = 0, varunits = 0, vardecs = 0;
+    int hdrds = 0;
+    float bias = 0;
 
     speedTimer.reset();
     speedTimer.start();
@@ -326,9 +329,10 @@
                     fixedSpeed();
                     break;
                     //If anything unexpected was received
-                    
-                    case 'R':
-                    hdrds = 0; units = 0, tens = 0, decimals = 0;
+
+                case 'R':
+                    hdrds = 0;
+                    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(command[1]=='-') {
@@ -348,7 +352,7 @@
                             tens = command[2] - '0';
                             units = command[3] - '0';
                             decimals = command[5] - '0';
-                        } else if(command[5]=='.'){
+                        } else if(command[5]=='.') {
                             hdrds = command[2] - '0';
                             tens = command[3] - '0';
                             units = command[4] - '0';
@@ -371,8 +375,7 @@
                             tens = command[1] - '0';
                             units = command[2] - '0';
                             decimals = command[4] - '0';
-                        }
-                        else if(command[4]=='.'){
+                        } else if(command[4]=='.') {
                             hdrds = command[1] - '0';
                             tens = command[2] - '0';
                             units = command[3] - '0';
@@ -381,15 +384,15 @@
                     }
                     //Calculate the number of revolutions required
                     targetRevs = float(hdrds)*100 + float(tens)*10 + float(units) + float(decimals)/10;
-                  
+
                     //Print values for verification
                     pc.printf("Target revs: %2.4f\n\r", targetRevs);
 
                     //Run the function to start rotating at a fixed speed
                     spinToPos();
                     break;
-                    
-                    
+
+
                 case 's':
 //                    pc.printf("Revs / sec: %2.2f\r", revs);
 //                    printSpeed.attach(&speedo, 1.0);
@@ -427,6 +430,26 @@
                     controller.setTunings(Kc, Ti, Td);
 //                    controller.setMode(1);
                     break;
+                case 'b':
+                    if(command[1]=='.') {
+                        //Extract decimal rev/s
+                        vardecs = command[2] - '0';
+
+                        //If decimal point is in the third character (eg, V-0.1)
+                    } else if(command[2]=='.') {
+                        varunits = command[1] - '0';
+                        vardecs = command[3] - '0';
+
+                        //If decimal point is in the fourth character (eg, V-10.1)
+                    } else if(command[3]=='.') {
+                        vartens = command[1] - '0';
+                        varunits = command[2] - '0';
+                        vardecs = command[4] - '0';
+                    }
+                    bias = float(vartens)*10 + float(varunits) + float(vardecs)/10;
+                    printf("Bias: %2.1f\r\n", bias);
+                    controller.setBias(bias);
+                    break;
                 case 'l':
                     sing(10000);
                     break;