H2M Teststand / Mbed 2 deprecated H2M_Snippets

Dependencies:   mbed

Fork of Low_Cost_PWM by Hans Dampf

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Main.cpp Source File

Main.cpp

00001 #include "mbed.h"
00002 
00003 
00004 #define PI 3.1415926
00005 #define DMS_torque_factor 0.3
00006 
00007 //*******************************************
00008 //****INPUTS*********************************
00009 
00010 PwmOut Motor_Fet(p22);
00011 
00012 DigitalIn Light_Barrier(p14);
00013 
00014 DigitalOut Fet_Brake(p21);
00015 DigitalOut Mux_0(p15);
00016 DigitalOut Mux_1(p16);
00017 
00018 AnalogIn DMS_Value(p17);
00019 
00020 //*******************************************
00021 //****COMM***********************************
00022 
00023 Serial pc(USBTX, USBRX); // tx, rx
00024 
00025 //*******************************************
00026 //****Timer**********************************
00027 
00028 Timer Light;
00029 
00030 void read_sensors(void);
00031 
00032 //****************************************************
00033 //****MAIN-FUNCTION***********************************
00034 
00035 int main()
00036 {
00037     bool lock = 0;
00038     int diff_rpm = 0, spec_rpm = 0, time_passed = 0, DMS_Value = 0;
00039     int Motor_C = 0, Motor_V = 0, Brake_C = 0, Brake_V = 0;
00040     int Temp_0 = 0, Temp_1 = 0, Temp_2 = 0, Aux = 0;
00041     float torque = 0, Mech_Power = 0 , act_rps = 0, act_rpm = 0;
00042     float Elec_Power = 0;
00043     
00044     
00045     
00046     
00047     
00048     Motor_Fet.period_us(20000);         //20ms period servo signal
00049     Motor_Fet.pulsewidth_us(900);       //900us startup value
00050 
00051     
00052     //**************************Main-Circle*******************************
00053    
00054     while(1) 
00055     {
00056         //**************************LIGHTBARRIER*******************************
00057             
00058                                                     //Light_Barrier_Low = Trigger
00059         if (!Light_Barrier)                         //Detects Falling Edge of the Light_Barrier
00060         {   
00061             
00062             if (!lock)                              //Asures First Run since detected Edge
00063             {   
00064                 Light.stop();                       //Stops Timer
00065                 time_passed = Light.read_us();       
00066                 Light.start();                      //Starts Timer        
00067                 lock = 1;                           //Asures just one counter-increase per Falling Edge -> Locks timer
00068             }         
00069         }
00070         else
00071         lock = 0;                                   //Unlocks timer-lock with High-Level of Light_Barrier
00072         
00073         //**************************LIGHTBARRIER*******************************
00074         
00075         
00076         //**************************RPS****************************************
00077         
00078         act_rps =  (1 / time_passed) * 1E06;
00079         act_rpm = act_rps * 60;
00080         
00081         //**************************RPS****************************************
00082     
00083     
00084         //**************************Specified-rpm******************************
00085         
00086         if (pc.readable())              //Just call if theres a value...
00087         {  
00088             scanf("%i", &spec_rpm);
00089         }    
00090               
00091         //**************************Specified-rpm******************************
00092         
00093         //**************************Diff-rpm***********************************
00094         
00095         diff_rpm = spec_rpm - (time_passed * 0.0036);
00096         
00097         //**************************Diff-rpm***********************************
00098         
00099         
00100         //**************************Set-PWM************************************
00101         
00102         Motor_Fet.pulsewidth_us();
00103         
00104         //**************************Set-PWM************************************
00105         
00106         
00107         //**************************Read-Sensors*******************************
00108         
00109         read_sensors();
00110         
00111         //**************************Read-Sensors*******************************
00112         
00113         
00114         //**************************DMS****************************************
00115         
00116         torque = DMS_Value * DMS_torque_factor;                 //Calculating torque
00117     
00118         //**************************DMS****************************************
00119         
00120         //**************************Mech-Power*********************************
00121         
00122         Mech_Power = torque * 2 * PI * act_rps;                 //Calculating Mechanical Power
00123         
00124         //**************************Mech-Power*********************************
00125         
00126         //**************************Elec-Power*********************************
00127         
00128         Elec_Power = Motor_V * Motor_C;
00129         
00130         //**************************Elec-Power*********************************
00131         
00132         
00133     }
00134 }
00135 
00136 void read_sensors(void)
00137 {
00138     int  Messwert_0 = 0, Messwert_1 = 0;
00139  
00140     Mux_0 = 0, Mux_1 = 0;
00141  
00142     //Selection of Multiplexer states
00143  
00144     for (int i=0; i<=4; i++) {
00145         
00146  
00147         Mux_0 = i&0x01;
00148         Mux_1 = (i>>1)&0x01;
00149         wait(0.00001);
00150  
00151         Messwert_0 = Messkanal_0.read_u16();
00152         Messwert_1 = Messkanal_1.read_u16();
00153  
00154         switch(i) {
00155             case 0: {
00156                 Temperatur_1 = Messwert_0 ;
00157                 Motorspannung = Messwert_1 * 0.000515 - 0.05 ; //Gute Näherung von 2- ~23V. Dannach zu niedrig.Z-Diode
00158                 break;
00159             }
00160  
00161             case 1: {
00162                 Temperatur_2 = Messwert_0  ;
00163                 Motorstrom = Messwert_1 * 0.000396 - 4.15;//
00164                 break;
00165             }
00166  
00167             case 2: {
00168                 Temperatur_3 = Messwert_0;
00169                 Bremsenspannung = Messwert_1 * 0.000515 - 0.05;//Gute Näherung von 2- ~23V. Dannach zu niedrig.Z-Diode
00170                 break;
00171             }
00172  
00173             case 3: {
00174                 Aux = Messwert_0 / 1629;
00175                 Bremsenstrom = Messwert_1; //* 0.00032 - 15.8;
00176                 break;
00177             }
00178  
00179  
00180         }
00181  
00182     }
00183 }
00184