H2M Teststand / Mbed 2 deprecated H2M_Snippets

Dependencies:   mbed

Fork of Low_Cost_PWM by Hans Dampf

Revision:
2:5798dde60822
Parent:
1:dadef2f723e4
Child:
3:77adbd7956b9
--- a/Main.cpp	Mon Sep 08 23:27:06 2014 +0000
+++ b/Main.cpp	Wed Sep 10 11:57:57 2014 +0000
@@ -1,53 +1,140 @@
 #include "mbed.h"
 
+
+#define PI 3.1415926
+#define DMS_torque_factor 0.3
+
 //*******************************************
 //****INPUTS*********************************
-PwmOut Motor_Fet(p21);
+
+PwmOut Motor_Fet(p22);
+
+DigitalIn Light_Barrier(p14);
 
-DigitalIn Light_Barrier(p17);
+DigitalOut Fet_Brake(p21);
+DigitalOut Mux_0(p15);
+DigitalOut Mux_1(p16);
 
-
-
+AnalogIn DMS_Value(p17);
 
 //*******************************************
-//****MAIN***********************************
+//****COMM***********************************
+
+Serial pc(USBTX, USBRX); // tx, rx
+
+//*******************************************
+//****Timer**********************************
+
+Timer Light;
+
+void read_sensors(void);
+
+//****************************************************
+//****MAIN-FUNCTION***********************************
 
 int main()
 {
-    bool Light_stop = 0;
-    int counter = 0, RPM_S = 0;
-    float diff_rpm = 0, spec_rpm = 0;
+    bool lock = 0;
+    int diff_rpm = 0, spec_rpm = 0, time_passed = 0, DMS_Value = 0;
+    int Motor_C = 0, Motor_V = 0, Brake_C = 0, Brake_V = 0;
+    int Temp_0 = 0, Temp_1 = 0, Temp_2 = 0, Aux = 0;
+    float torque = 0, Mech_Power = 0 , act_rps = 0, act_rpm = 0;
+    float Elec_Power = 0;
+    
     
-    Motor_Fet.period_us(20000);
-    Motor_Fet.pulsewidth_us(200);
+    
+    
+    
+    Motor_Fet.period_us(20000);         //20ms period servo signal
+    Motor_Fet.pulsewidth_us(900);       //900us startup value
 
-    //***************((((((((((((((()))))))))))))
-    //***MAIN-PROG***((((((((((((((()))))))))))))
+    
+    //**************************Main-Circle*******************************
    
     while(1) 
     {
         //**************************LIGHTBARRIER*******************************
-        
-        if (!Light_Barrier)         //Detects Falling Edge of the Light_Barrier
-        {
-            if (!Light_stop)        //Asures First Run since detected Edge
-            {      
-                counter++;          
-                Light_stop = 1;     //Asures just one counter-increase per Falling Edge -> Locks counter increase
+            
+                                                    //Light_Barrier_Low = Trigger
+        if (!Light_Barrier)                         //Detects Falling Edge of the Light_Barrier
+        {   
+            
+            if (!lock)                              //Asures First Run since detected Edge
+            {   
+                Light.stop();                       //Stops Timer
+                time_passed = Light.read_us();       
+                Light.start();                      //Starts Timer        
+                lock = 1;                           //Asures just one counter-increase per Falling Edge -> Locks timer
             }         
         }
         else
-        Light_stop = 0;             //Unlocks counter-increase when theres no falling edge
+        lock = 0;                                   //Unlocks timer-lock with High-Level of Light_Barrier
         
         //**************************LIGHTBARRIER*******************************
+        
+        
+        //**************************RPS****************************************
+        
+        act_rps =  (1 / time_passed) * 1E06;
+        act_rpm = act_rps * 60;
+        
+        //**************************RPS****************************************
     
     
         //**************************Specified-rpm******************************
         
+        if (pc.readable())              //Just call if theres a value...
+        {  
+            scanf("%i", &spec_rpm);
+        }    
+              
+        //**************************Specified-rpm******************************
         
-    
+        //**************************Diff-rpm***********************************
+        
+        diff_rpm = spec_rpm - (time_passed * 0.0036);
+        
+        //**************************Diff-rpm***********************************
+        
+        
+        //**************************Set-PWM************************************
+        
+        Motor_Fet.pulsewidth_us();
+        
+        //**************************Set-PWM************************************
+        
+        
+        //**************************Read-Sensors*******************************
+        
+        read_sensors();
+        
+        //**************************Read-Sensors*******************************
+        
+        
+        //**************************DMS****************************************
+        
+        torque = DMS_Value * DMS_torque_factor;             //Calculating torque
     
+        //**************************DMS****************************************
+        
+        //**************************Mech-Power*********************************
+        
+        Mech_Power = torque * 2 * PI * act_rps;                 //Calculating Mechanical Power
+        
+        //**************************Mech-Power*********************************
+        
+        //**************************Elec-Power*********************************
+        
+        Elec_Power = Motor_V * Motor_C;
+        
+        //**************************Elec-Power*********************************
+        
+        
     }
+}
 
+void read_sensors(void)
+{
     
 }
+    
\ No newline at end of file