para el ventilador

Dependencies:   QEI FastPWM

Revision:
3:45299e7882b9
Parent:
2:6f618b905d4f
Child:
4:d42e1953243c
--- a/stepper_motor_driver.cpp	Thu Apr 16 13:22:16 2020 +0000
+++ b/stepper_motor_driver.cpp	Fri Apr 17 11:22:43 2020 +0000
@@ -1,3 +1,4 @@
+
 
 #include "mbed.h"
 #include "project_defines.h"
@@ -5,6 +6,7 @@
 #include "buttons.h"
 #include "tft_interface.h"
 #include "FastPWM.h"
+#include "ventilator.h"
 
 /* Object definition */
 InterruptIn stepper_pulse_feedback(STEPPER_PULSE_FEEDBACK_PIN);
@@ -12,8 +14,13 @@
 DigitalOut  stepper_en(STEPPER_ENABLE_PIN);
 DigitalOut  stepper_dir(STEPPER_DIRECTION_PIN);
 
+// For testing pusposes only
+Serial pc (USBTX, USBRX);
+
+
 /* Variable definition */
 
+volatile uint8_t stepper_parameters_update_flag = 0;
 
 // ----------------------- Tables used for calculations ------------------------
 
@@ -39,18 +46,27 @@
         0.781, 0.801, 0.820, 0.837, 0.853, 0.867, 0.881, 0.893, 0.904, 0.914,
         0.923, 0.931, 0.939, 0.945, 0.951, 0.957, 0.961, 0.966, 0.969, 0.973,
         0.976, 0.979, 0.981, 0.983, 0.985, 0.987, 0.988, 0.989, 0.991, 0.992,
-        0.993, 0.994, 0.994, 0.995, 0.995, 0.996, 0.996, 0.997, 0.997, 0.998f
+        0.993, 0.994, 0.994, 0.995, 0.995, 0.996, 0.996, 0.997, 0.997, 0.998
 };
 
 #else
     #error "Seleccionar un valor permitido para RAMP_STEPS: Puede ser 50 o 100"
 #endif
+
+const  float short_sigma_ramp[SHORT_RAMP_STEPS] = {
+      0.002, 0.004, 0.007, 0.011, 0.018, 0.029, 0.047, 0.076, 0.119, 0.182,
+      0.269, 0.378, 0.500, 0.622, 0.731, 0.818, 0.881, 0.924, 0.953, 0.971,
+      0.982, 0.989, 0.993, 0.996, 0.998
+};
+
+
                                         
 // -----------------------------------------------------------------------------
 
 uint32_t calibration_period_rise_ramp_us[RAMP_STEPS];
 uint32_t inspiration_period_rise_ramp_us[RAMP_STEPS];
 uint32_t expiration_period_rise_ramp_us[RAMP_STEPS];
+uint32_t expiration_period_short_rise_ramp_us[SHORT_RAMP_STEPS];
 
 // These parameters are configurer through the graphic interface */
 uint32_t inspiration_stepper_pulses_setpoint = STEPPER_DRIVER_INSPIRATION_PULSES_DEFAULT ;
@@ -61,17 +77,17 @@
 
 // Volume setpoint (250mL, 300mL, 350ml ... 750ml) expressed in steps
 uint32_t volume_in_steps[VOLUME_SETPOINT_INDEX_LIMIT] = {
-                                                            1600, //250 mL
-                                                            1700, //300 mL
-                                                            1800, //350 mL
-                                                            1900, //400 mL
-                                                            2000, //450 mL
-                                                            2100, //500 mL
-                                                            2200, //550 mL
-                                                            2300, //600 mL    
-                                                            2400, //650 mL
-                                                            2500, //700 mL
-                                                            2600  //750 mL
+                                                       720,   //  1600, //250 mL
+                                                       765,   //  1700, //300 mL
+                                                       810,   //  1800, //350 mL
+                                                       855,   //  1900, //400 mL
+                                                       900,   //  2000, //450 mL
+                                                       945,   //  2100, //500 mL
+                                                       990,   //  2200, //550 mL
+                                                       1035,   //  2300, //600 mL    
+                                                       1080,   //  2400, //650 mL
+                                                       1125,   //  2500, //700 mL
+                                                       1170   //  2600  //750 mL
 };  
 
 
@@ -91,7 +107,8 @@
     stepper_pulse.period_us((int)calibration_stepper_pulse_period_us);
     stepper_pulse.write(0.0f);
     stepper_pulse_feedback.fall(&Stepper_Driver_Pulse_Signal_Update);
-    stepper_driver_state = Stepper_Inspiration_Finish;  
+    stepper_driver_state = Stepper_Inspiration_Finish;
+    stepper_parameters_update_flag = 1; 
     // Just for testing purposes
     //inspiration_stepper_pulses_setpoint = volume_in_steps[10];
 }
@@ -108,6 +125,31 @@
 }    
 
 
+void Stepper_Update_Parameters(void){
+    
+    int8_t stepper_volume_index;
+    float total_time, total_time_sec, insp_time_sec, exp_time_sec;
+    
+    /* Update the value of the volume setpoint (expressed in steps) */
+    stepper_volume_index = (volume_setpoint - VOLUME_SETPOINT_MINIMUM_VALUE) / VOLUME_SETPOINT_STEP;
+    inspiration_stepper_pulses_setpoint = volume_in_steps[stepper_volume_index];
+    
+    /* Update the value of the pulse periods in microseconds */
+    total_time = inspiration_time + expiration_time;
+    total_time_sec = 60 / resp_frequency;
+    insp_time_sec = (total_time_sec * inspiration_time) / total_time;
+    exp_time_sec = (total_time_sec * expiration_time) / total_time;
+    inspiration_stepper_pulse_period_us = (uint32_t)((1000000.0 * insp_time_sec) / inspiration_stepper_pulses_setpoint);
+    expiration_stepper_pulse_period_us = (uint32_t)((1000000.0 * exp_time_sec) / inspiration_stepper_pulses_setpoint);
+    
+    /* Just for testing purposes */
+    pc.printf("Periodo de pulsos de ida: %d us\n\r", inspiration_stepper_pulse_period_us);
+    pc.printf("Periodo de pulsos de vuelta: %d us\n\r", expiration_stepper_pulse_period_us);
+    pc.printf("Cantidad de pulsos: %d \n\n\r", inspiration_stepper_pulses_setpoint);
+
+    
+}
+
 
 void Stepper_Calculate_Period_Ramp(void){
     
@@ -137,6 +179,18 @@
         expiration_period_rise_ramp_us[i] = (uint32_t)period_i_us;
     }
 
+    /* Period short rise ramp for expiration */
+    initial_frequency = (float)STEPPER_START_BRAKE_FREQUENCY_HZ;
+    final_frequency = 1000000.0 / (float)expiration_stepper_pulse_period_us;
+    frequency_ramp_range = final_frequency - initial_frequency;
+    
+    for(i = 0; i < SHORT_RAMP_STEPS; i++){
+        frequency_i_hz = initial_frequency + (frequency_ramp_range * sigma_ramp[i]);
+        period_i_us = 1000000.0 / frequency_i_hz;
+        expiration_period_short_rise_ramp_us[i] = (uint32_t)period_i_us;
+    }
+
+
 }
 
 
@@ -180,6 +234,12 @@
             break;
         // -----------------------------------------------------------------------     
         case Stepper_Expiration_Finish:
+        
+            if(stepper_parameters_update_flag){
+                stepper_parameters_update_flag = 0;
+                Stepper_Update_Parameters();
+                Stepper_Calculate_Period_Ramp();
+            }
                 
             stepper_pulse_counter = 0;  
             stepper_ramp_pulse_counter = 0;      
@@ -188,7 +248,7 @@
             stepper_pulse.write(0.5);
             stepper_driver_state = Stepper_Inspiration_Rising_Ramp;         
 
-                break;
+            break;
         // -------------------------------------------------------------------- 
         case Stepper_Limit_Sensor_Error:
             // Stop the system
@@ -282,15 +342,16 @@
             break; 
         // --------------------------------------------------------------------    
         case Stepper_Expiration_Falling_Ramp:  
-            if(stepper_ramp_pulse_counter < RAMP_STEPS){
+            if(stepper_ramp_pulse_counter < SHORT_RAMP_STEPS){
                 stepper_ramp_pulse_counter++;
-                stepper_pulse.period_us((int)expiration_period_rise_ramp_us[RAMP_STEPS - stepper_ramp_pulse_counter]);
+                stepper_pulse.period_us((int)expiration_period_short_rise_ramp_us[SHORT_RAMP_STEPS - stepper_ramp_pulse_counter]);
                 stepper_pulse.write(0.5);  
-            }else if(stepper_ramp_pulse_counter == RAMP_STEPS){   
+            }else if(stepper_ramp_pulse_counter == SHORT_RAMP_STEPS){   
                 stepper_pulse.period_us((int)STEPPER_START_BRAKE_PERIOD_US);
                 stepper_pulse.write(0.5);
                 stepper_ramp_pulse_counter++;             
             }else{
+               stepper_ramp_pulse_counter = 0;
                stepper_pulse.write(0.0);
                stepper_driver_state = Stepper_Expiration_Finish;
             }