Sven Kugathasan / Mbed OS SKAFMO_2

Dependencies:   PID

Revision:
9:1e2bf281e56f
Parent:
8:c417e4d0cf48
diff -r c417e4d0cf48 -r 1e2bf281e56f main.cpp
--- a/main.cpp	Fri Mar 24 13:41:31 2017 +0000
+++ b/main.cpp	Fri Mar 24 13:51:08 2017 +0000
@@ -4,6 +4,12 @@
 #include "rtos.h"
 #include "PID.h"
 
+#include "ctype.h"
+#include <string>
+#include "stdlib.h"
+#include "math.h"
+
+
 /*_________________________________PIN SETUP__________________________________*/
 
 //PhotoInterrupter Input Pins
@@ -350,10 +356,191 @@
     //float rotation_set = revstates_max;
     //float velocity_set = vel_max;
     
+    
+    void serial_com();
+    
+    
+    
+    
+    
+    
+    
     position_control(rotation_set, velocity_set); 
     
 }
 
+void serial_com(){
+pc.baud(9600);
+float r=0;
+float v=0; //velocity
+int i=0; //hello
+bool r_val=true;
+bool v_val=true;
+int t_loc=0;
+int r_loc=0;
+int v_loc=0;
+char buf[80];
+
+
+string input;
+
+while(1)
+{
+    r=0;
+    v=0;
+    r_val=true;
+    v_val=true;
+    pc.printf("Please enter something\r\n");
+    pc.scanf("%s",&buf);
+    input=buf;
+    pc.printf("The input string is %s\r\n",buf);
+    //cout<<input<<endl;
+    t_loc=input.find('T');
+    r_loc=input.find('R');
+    v_loc=input.find('V');
+    pc.printf("Location of T is %d\r\n",t_loc);
+    pc.printf("Location of R is %d\r\n",r_loc);
+    pc.printf("Location of V is %d\r\n",v_loc);
+    
+    
+
+    if(t_loc==0) //if melody marker present
+        {
+        pc.printf("Note sequence detected\r\n");
+        
+        
+        
+
+
+
+        }
+
+    else if(t_loc==-1) //if no melody marker present
+        {
+        pc.printf("Note sequence NOT detected\r\n");
+        
+        if(r_loc==0 && v_loc==-1 && input.length()>1) //check if first letter is R
+        
+            {
+            pc.printf("Checking for sole R input type...\r\n");
+            
+            for(int j=1; j<input.length();j++)
+                {
+                    if(!isdigit(input[j]) && input[j]!='-' && input[j]!='.')
+                        {
+                            //cout<<"Invalid input"<<endl;
+                            r_val=false;
+                        }
+                
+                }
+
+            if(r_val==true)
+                {
+                r=atof(input.substr(1).c_str());
+                pc.printf("Spin for %.3f number of rotations\r\n",r);
+                
+                
+                }
+            else
+                {
+                pc.printf("Invalid input\r\n");
+                }
+            
+                
+            }
+
+        else if(r_loc==0 && v_loc!=-1 && v_loc < input.length()-1) //check if first letter is R and V is also present
+        
+            {
+            pc.printf("Checking for combined R and V input type...\r\n");
+            
+            
+            for(int j=1; j<v_loc;j++)
+                {
+                    if(!isdigit(input[j]) && input[j]!='-' && input[j]!='.')
+                        {
+                            //cout<<"Invalid input"<<endl;
+                            r_val=false;
+                        }
+                
+                }
+            for(int j=v_loc+1; j<input.length();j++)
+                {
+                    if(!isdigit(input[j]) && input[j]!='-' && input[j]!='.')
+                        {
+                            //cout<<"Invalid input"<<endl;
+                            v_val=false;
+                        }
+                
+                }
+
+            if(r_val==true && v_val==true)
+                {
+                r=atof(input.substr(1,v_loc-1).c_str());
+                v=atof(input.substr(v_loc+1).c_str());
+                if(v<0)
+                    {
+                    v=abs(v);
+                    }
+                pc.printf("Spin for %.3f number of rotations at %.3f speed \r\n",r,v);
+                
+
+                }
+            else
+                {
+                pc.printf("Invalid input\r\n");
+                }
+            
+                
+            }
+        else if(v_loc==0 && input.length()>1) //check if first letter is V
+
+            {
+
+            pc.printf("Checking for sole V input type...\r\n");
+            for(int j=1; j<input.length();j++)
+                {
+                    if(!isdigit(input[j]) && input[j]!='-' && input[j]!='.')
+                        {
+                            //cout<<"Invalid input"<<endl;
+                            v_val=false;
+                        }
+                
+                }
+
+            if(v_val==true)
+                {
+                v=atof(input.substr(1).c_str());
+                
+                pc.printf("Spin at %.3f speed\r\n",v);
+
+                }
+            else
+                {
+                pc.printf("Invalid input\r\n");
+                }
+            
+            }
+        else
+            {
+            pc.printf("Invalid input\r\n");
+            }
+
+
+        }
+
+
+
+
+    
+
+    }
+
+
+
+
+}
+
 
 /*_______________________Testing and Tuning Function__________________________*/