Qubit 2020 / presensfirmwareupdate

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pump.cpp Source File

pump.cpp

00001 #include "mbed.h"
00002 #include "pump.h"
00003 #include "light.h"
00004 #include "debug.h"
00005 #include "wifi_events.h"
00006 
00007 // According to specification for DM212 pump PWM period is 15 ms. new pump driver period 4-2019
00008 #define PUMP_PWM_PERIOD_US 15000
00009 
00010 // Pulse width for DM212 pump can be 1-2 ms,
00011 // where 1 ms - pump is off, 2 ms  - pump is on with maximum speed. new pump driver parameters 4-2019
00012 #define PUMP_PULSE_WIDTH_MIN_US 950
00013 #define PUMP_PULSE_WIDTH_MAX_US 2000
00014 
00015 // Zero pulse width is used to disable pulse
00016 #define PUMP_DISABLE_PULSE 0
00017 
00018 // Time to wait to be sure that signal sent 3 times.
00019 // Measured during experiments, can be tuned in the future.
00020 #define PUMP_DELAY_MS 500
00021 
00022 // File with pump parameters
00023 #define PUMP_FILE "/" FSNAME "/Parameters/pump.sys"
00024 
00025 // Measurements number for pump feedback
00026 #define PUMP_FEEDBACK_NUM_MEASUREMENTS 10
00027 
00028 // Maximum period for RPM feedback
00029 #define PUMP_FEEDBACK_MAX_PERIOD 0.5
00030 
00031 // Pump polynom coefficients
00032 // Default values for ideal pump (linear dependency)
00033 static float a = -50.0;
00034 static float b = 0.02;
00035 static float c = 0.0;
00036 
00037 // PWM out to handle pump
00038 static PwmOut pumpPwm(p26);
00039 
00040 // Feedback timer
00041 static Timer feedbackTimer;
00042 
00043 // Feedback interrupt
00044 static InterruptIn feedbackInterrupt(p18);
00045 
00046 // Latest applied pump PercentPump
00047 static float lastPercentPump;
00048 
00049 
00050 // Latest average feedback period
00051 static float feedbackPeriod;
00052 
00053 static void feedbackRise(void)
00054 {
00055     static float periodSum = 0;
00056     static int numMeasureaments = 0;
00057 
00058     // Read period
00059     float period = feedbackTimer.read();
00060     feedbackTimer.reset();
00061 
00062     if (period > PUMP_FEEDBACK_MAX_PERIOD) {
00063         periodSum = 0;
00064         numMeasureaments = 0;
00065         feedbackPeriod = 0;
00066         return;
00067     }
00068 
00069     periodSum += period;
00070     numMeasureaments++;
00071 
00072     if (numMeasureaments >= PUMP_FEEDBACK_NUM_MEASUREMENTS) {
00073         feedbackPeriod = periodSum / numMeasureaments;
00074         periodSum = 0;
00075         numMeasureaments = 0;
00076     }
00077 }
00078 
00079 void pumpInit(void)
00080 {
00081     // Configure PWM period
00082     pumpPwm.period_us(PUMP_PWM_PERIOD_US);
00083 
00084     // Turn pump off
00085     pumpPwm.pulsewidth_us(PUMP_PULSE_WIDTH_MIN_US);
00086     
00087     // Wait to be sure that signal sent 3 times. 
00088     wait_ms(PUMP_DELAY_MS); 
00089     
00090     // Disable pump pulse. 
00091     pumpPwm.pulsewidth_us(PUMP_DISABLE_PULSE);
00092     
00093 
00094     // Read parameters
00095     FILE* paramFile = fopen(PUMP_FILE, "r");
00096     if (paramFile != NULL) {
00097         fscanf(paramFile, "%f,%f,%f", &a, &b, &c);
00098         fclose(paramFile);
00099     }
00100     
00101     INFO("Used pump coefficients: a=%f, b=%0.6f, c=%0.9f", a, b, c);
00102 
00103     // Set callback for feedback
00104     feedbackInterrupt.rise(&feedbackRise);
00105     feedbackTimer.start();
00106 }
00107 
00108 void pumpSet(float PercentPump)
00109 {
00110     if (PercentPump > PUMP_MAX_INTENSITY) {
00111         // Don't support PercentPump more than 100%
00112         ERROR("Request to set too big pump PercentPump %f", PercentPump);
00113         return;
00114     }
00115 
00116 //    if (lastPercentPump == PercentPump) {
00117 //        INFO("Discard request to set pump PercentPump, PercentPump %f already applied", PercentPump);
00118 //        return;
00119 //    }
00120 
00121     lastPercentPump = PercentPump;
00122     
00123 
00124     // As PWM interface for LPC1768 uses the same period for all PWM outs
00125     // read lignt intensity, turn light off and set it back when pump operation finished.
00126     unsigned char lightIntensity = lightRead();
00127 
00128     // Configure PWM period and set pulse for pump.
00129     pumpPwm.period_us(PUMP_PWM_PERIOD_US);
00130 
00131     // Set light off
00132     lightSet(LIGHT_OFF_INTENSITY);
00133 
00134     int pulseWidth = PUMP_PULSE_WIDTH_MIN_US +
00135                      (PUMP_PULSE_WIDTH_MAX_US - PUMP_PULSE_WIDTH_MIN_US) * PercentPump / PUMP_MAX_INTENSITY;
00136     pumpPwm.pulsewidth_us(pulseWidth);
00137 
00138     // Wait to be sure that signal sent 3 times. 
00139     wait_ms(PUMP_DELAY_MS); 
00140     
00141     // Disable pump pulse. 
00142     pumpPwm.pulsewidth_us(PUMP_DISABLE_PULSE);
00143 
00144     // Return light intensity back.
00145     lightSet(lightIntensity);
00146 }
00147 
00148 void pumpSetRpm(int rpm)
00149 {
00150     // Calculate PercentPump according to provided RPM
00151     float PercentPump = a + b * rpm + c * rpm * rpm;
00152 
00153     if (PercentPump < PUMP_MIN_INTENSITY) {
00154         PercentPump = PUMP_OFF_INTENSITY;
00155     }
00156     if (PercentPump > PUMP_MAX_INTENSITY) {
00157         PercentPump = PUMP_MAX_INTENSITY;
00158     }
00159 
00160     DEBUG1("Set pump PercentPump: %f", PercentPump);
00161     pumpSet(PercentPump);
00162 }
00163 
00164 bool pumpOn(void)
00165 {
00166     return (lastPercentPump > PUMP_OFF_INTENSITY);
00167 }
00168 
00169 unsigned char pumpCurrentIntensity(void)
00170 {
00171     return lastPercentPump;
00172 }
00173 
00174 unsigned int pumpSpeed(void)
00175 {
00176     // Calculate pump speed
00177     unsigned int speed = 0;
00178 
00179     if (pumpCurrentIntensity() == 0) {
00180         return 0;
00181     }
00182 
00183     if (feedbackPeriod > 0.0) {
00184         speed = 60 / feedbackPeriod;
00185 
00186     }
00187     DEBUG1("Pump speed: %u", speed);
00188 
00189     static bool pumpErrorSent = false;
00190     if (speed != 0) {
00191         pumpErrorSent = false;
00192     } else if (!pumpErrorSent) {
00193         wifiEventsSendErrorsInd(ERROR_PUMP_RPM_NULL);
00194         pumpErrorSent = true;
00195     }
00196 
00197     return speed;
00198 }
00199 
00200 void pumpGetParams(float* aValue, float* bValue, float* cValue)
00201 {
00202     *aValue = a;
00203     *bValue = b;
00204     *cValue = c;
00205 }
00206 
00207 void pumpSetParams(float aValue, float bValue, float cValue)
00208 {
00209     a = aValue;
00210     b = bValue;
00211     c = cValue;
00212 
00213     FILE* paramFile = fopen(PUMP_FILE, "w");
00214     fprintf(paramFile, "%f,%0.6f,%0.9f", a, b, c);
00215     fclose(paramFile);
00216 }