for testing

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 1000
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 intensity
00047 static float lastIntensity;
00048 
00049 float intensity;
00050 
00051 // Latest average feedback period
00052 static float feedbackPeriod;
00053 
00054 static void feedbackRise(void)
00055 {
00056     static float periodSum = 0;
00057     static int numMeasureaments = 0;
00058 
00059     // Read period
00060     float period = feedbackTimer.read();
00061     feedbackTimer.reset();
00062 
00063     if (period > PUMP_FEEDBACK_MAX_PERIOD) {
00064         periodSum = 0;
00065         numMeasureaments = 0;
00066         feedbackPeriod = 0;
00067         return;
00068     }
00069 
00070     periodSum += period;
00071     numMeasureaments++;
00072 
00073     if (numMeasureaments >= PUMP_FEEDBACK_NUM_MEASUREMENTS) {
00074         feedbackPeriod = periodSum / numMeasureaments;
00075         periodSum = 0;
00076         numMeasureaments = 0;
00077     }
00078 }
00079 
00080 void pumpInit(void)
00081 {
00082     // Configure PWM period
00083     pumpPwm.period_us(PUMP_PWM_PERIOD_US);
00084 
00085     // Turn pump off
00086     pumpPwm.pulsewidth_us(PUMP_PULSE_WIDTH_MIN_US);
00087     
00088     // Wait to be sure that signal sent 3 times. 
00089     wait_ms(PUMP_DELAY_MS); 
00090     
00091     // Disable pump pulse. 
00092     pumpPwm.pulsewidth_us(PUMP_DISABLE_PULSE);
00093     
00094 
00095     // Read parameters
00096     FILE* paramFile = fopen(PUMP_FILE, "r");
00097     if (paramFile != NULL) {
00098         fscanf(paramFile, "%f,%f,%f", &a, &b, &c);
00099         fclose(paramFile);
00100     }
00101     
00102     INFO("Used pump coefficients: a=%f, b=%0.6f, c=%0.9f", a, b, c);
00103 
00104     // Set callback for feedback
00105     feedbackInterrupt.rise(&feedbackRise);
00106     feedbackTimer.start();
00107 }
00108 
00109 void pumpSet(float intensity)
00110 {
00111     if (intensity > PUMP_MAX_INTENSITY) {
00112         // Don't support intensity more than 100%
00113         ERROR("Request to set too big pump intensity %f", intensity);
00114         return;
00115     }
00116 
00117     if (lastIntensity == intensity) {
00118         INFO("Discard request to set pump intensity, intensity %f already applied", intensity);
00119         return;
00120     }
00121 
00122     lastIntensity = intensity;
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) * intensity / 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 intensity according to provided RPM
00151     float intensity = a + b * rpm + c * rpm * rpm;
00152 
00153     if (intensity < PUMP_MIN_INTENSITY) {
00154         intensity = PUMP_OFF_INTENSITY;
00155     }
00156     if (intensity > PUMP_MAX_INTENSITY) {
00157         intensity = PUMP_MAX_INTENSITY;
00158     }
00159 
00160     DEBUG1("Set pump intensity: %f", intensity);
00161     pumpSet(intensity);
00162 }
00163 
00164 bool pumpOn(void)
00165 {
00166     return (lastIntensity > PUMP_OFF_INTENSITY);
00167 }
00168 
00169 unsigned char pumpCurrentIntensity(void)
00170 {
00171     return lastIntensity;
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 }