para el ventilador

Dependencies:   QEI FastPWM

Revision:
6:d38287621cca
Parent:
5:bd5fc0510e7b
Child:
9:95fdcdc0977e
--- a/stepper_motor_driver.cpp	Sun Apr 19 05:38:41 2020 +0000
+++ b/stepper_motor_driver.cpp	Mon Apr 20 11:39:30 2020 +0000
@@ -4,7 +4,7 @@
 #include "project_defines.h"
 #include "stepper_motor_driver.h"
 #include "buttons.h"
-#include "tft_interface.h"
+#include "nextion_interface.h"
 #include "FastPWM.h"
 #include "ventilator.h"
 
@@ -14,9 +14,6 @@
 DigitalOut  stepper_en(STEPPER_ENABLE_PIN);
 DigitalOut  stepper_dir(STEPPER_DIRECTION_PIN);
 
-// For testing pusposes only
-Serial pc (USBTX, USBRX);
-
 
 /* Variable definition */
 
@@ -75,8 +72,6 @@
 uint32_t expiration_stepper_pulse_period_us = STEPPER_DRIVER_DEFAULT_PULSE_PERIOD_US;
 uint32_t calibration_stepper_pulse_period_us = STEPPER_DRIVER_DEFAULT_PULSE_PERIOD_US;
 
-//uint32_t min_inspiration_ramp_period_us;
-//uint32_t min_expiration_ramp_period_us;
 
 // Volume setpoint (250mL, 300mL, 350ml ... 750ml) expressed in steps
 uint32_t volume_in_steps[VOLUME_SETPOINT_INDEX_LIMIT] = {
@@ -135,6 +130,7 @@
     float total_time, total_time_sec, insp_time_sec, exp_time_sec;
     float inspiration_stepper_pulse_period_sec, expiration_stepper_pulse_period_sec;
     float ramp_max_pulse_freq_hz, ramp_time_sec, short_ramp_time_sec;
+    float inspiration_stepper_regular_frequency_hz;
     
     /* Update the value of the volume setpoint (expressed in steps) */
     stepper_volume_index = (volume_setpoint - VOLUME_SETPOINT_MINIMUM_VALUE) / VOLUME_SETPOINT_STEP;
@@ -148,27 +144,39 @@
     
     /* Calculate stepper pulse period for inspiration */
     regular_steps = inspiration_stepper_pulses_setpoint - (2 * RAMP_STEPS);
-    ramp_max_pulse_freq_hz = ((float)inspiration_stepper_pulses_setpoint) / insp_time_sec;
+    /* Asume an initial value for the max frrquency of the ramp */
+    ramp_max_pulse_freq_hz = TUNING_FACTOR * (((float)inspiration_stepper_pulses_setpoint) / insp_time_sec);
+    /* Calculate the period ramp and the total time spent in doing the ramps */
     ramp_time_sec = Stepper_Calculate_Insp_Period_Ramp(ramp_max_pulse_freq_hz);
-    inspiration_stepper_pulse_period_sec =  (insp_time_sec - (2*ramp_time_sec)) / (float)regular_steps;
-    inspiration_stepper_pulse_period_us = (uint32_t)(1000000.0 * inspiration_stepper_pulse_period_sec);
+    inspiration_stepper_regular_frequency_hz = ((float)regular_steps) / (insp_time_sec - (2*ramp_time_sec));
+    
+    if(inspiration_stepper_regular_frequency_hz > STEPPER_MAX_FREQUENCY_HZ){
+        /* Calculate the period ramp and the total time spent in doing the ramps */
+        ramp_max_pulse_freq_hz = (float)STEPPER_MAX_FREQUENCY_HZ;
+        ramp_time_sec = Stepper_Calculate_Insp_Period_Ramp(ramp_max_pulse_freq_hz);
+        inspiration_stepper_regular_frequency_hz = ((float)regular_steps) / (insp_time_sec - (2*ramp_time_sec));
+    }
+    
+    inspiration_stepper_pulse_period_us = (uint32_t)(1000000.0 / inspiration_stepper_regular_frequency_hz);
     
     /* Calculate stepper pulse period for expiration */
     regular_steps = inspiration_stepper_pulses_setpoint - (RAMP_STEPS + SHORT_RAMP_STEPS);
-    ramp_max_pulse_freq_hz = ((float)inspiration_stepper_pulses_setpoint) / exp_time_sec;
+    ramp_max_pulse_freq_hz = TUNING_FACTOR * (((float)inspiration_stepper_pulses_setpoint) / exp_time_sec);
     ramp_time_sec = Stepper_Calculate_Exp_Period_Ramp(ramp_max_pulse_freq_hz);
     short_ramp_time_sec = Stepper_Calculate_Exp_Short_Period_Ramp(ramp_max_pulse_freq_hz);
     expiration_stepper_pulse_period_sec =  (exp_time_sec - (ramp_time_sec + short_ramp_time_sec)) / (float)regular_steps;
+    
     expiration_stepper_pulse_period_us = (uint32_t)(1000000.0 * expiration_stepper_pulse_period_sec);
     
     /* Just for testing purposes */
+    /*
     pc.printf("Cantidad de pulsos: %d \n\n\r", inspiration_stepper_pulses_setpoint);
     pc.printf("Frecuencia de arranque: %d Hz\n\r", STEPPER_START_BRAKE_FREQUENCY_HZ);
     pc.printf("Frecuencia de pulsos de ida: %f Hz\n\r", 1.0 / inspiration_stepper_pulse_period_sec);
     pc.printf("Periodo de pulsos de ida en us: %d us\n\r", inspiration_stepper_pulse_period_us);
     pc.printf("Frecuencia de pulsos de vuelta: %f Hz\n\r", 1.0 / expiration_stepper_pulse_period_us);
     pc.printf("Periodo de pulsos de vuelta en us: %d us\n\r", expiration_stepper_pulse_period_us);
-    
+    */
 }
 
 
@@ -310,8 +318,8 @@
         case Stepper_Limit_Sensor_Error:
             // Stop the system
             stepper_en = STEPPER_DISABLED;
-            tft->locate(340,VALUES_ROW2_Y_POS);
-            tft->printf("error"); 
+          //  tft->locate(340,VALUES_ROW2_Y_POS);
+           // tft->printf("error"); 
             stepper_driver_state = Stepper_Stand_By;
             break;  
          // --------------------------------------------------------------------