revised code
Embed:
(wiki syntax)
Show/hide line numbers
Calculate_flow.cpp
00001 /**---------------------------------------------------------------------------- 00002 * 00003 * \file frequency_detector.cpp 00004 -- -- 00005 -- ECEN 5803 Mastering Embedded System Architecture -- 00006 -- Project 1 Module 4 -- 00007 -- Microcontroller Firmware -- 00008 -- frequency_detector.cpp -- 00009 -- -- 00010 ------------------------------------------------------------------------------- 00011 -- 00012 -- Designed for: University of Colorado at Boulder 00013 -- 00014 -- 00015 -- Designed by: Tim Scherr 00016 -- Revised by: Naved Maududi and Bryan Cisneros 00017 -- 00018 -- Version: 2.1 00019 -- Date of current revision: 2017-09-25 00020 -- Target Microcontroller: Freescale MKL25ZVMT4 00021 -- Tools used: ARM mbed compiler 00022 -- ARM mbed SDK 00023 -- Freescale FRDM-KL25Z Freedom Board 00024 -- 00025 -- 00026 Functional Description: 00027 This file contains calcuate the flow of fluid that is being measured. The flow i 00028 measured in gallons per minute. The flow function takes in two inputs, which is the 00029 frequency and temperature. Frequency is calculated from the vortex analog signal on 00030 ADC channel 2. The temperature is determined from an internal sensor. 00031 -- 00032 -- Copyright (c) 2015 Tim Scherr All rights reserved. 00033 */ 00034 00035 00036 #include "shared.h" 00037 #include <math.h> 00038 00039 #define BLUFF_BODY_INCHES (0.5) 00040 #define BLUFF_BODY_METERS (0.0127) 00041 #define PID_INCHES (2.9) 00042 #define PID_INCHES_SQUARED (8.41) 00043 #define PID_METERS (0.07366) 00044 #define INCHES_IN_METER (39.37) 00045 00046 uint16_t flow = 0; 00047 00048 float calculateViscosity(uint16_t temperature) 00049 { 00050 float power = 247.8/((float)temperature - 140.0); 00051 return (float)0.000024 * pow(10,(power)); 00052 } 00053 00054 float calculateDensity(uint16_t temperature) 00055 { 00056 return 1000 * (1 - (((float)temperature+288.9414)/(508929.2*((float)temperature+68.12963)))*pow(((float)temperature-3.9863),2)); 00057 } 00058 00059 float calculateReynoldsNumber(float density, float viscosity, float velocity) 00060 { 00061 return density * velocity * PID_METERS / viscosity; 00062 } 00063 00064 float calculateStrouhalNumber(float reynoldsNumber) 00065 { 00066 return 0.2684 - 1.0356/sqrt(reynoldsNumber); 00067 } 00068 00069 float calculateVelocity(uint16_t frequency, float strouhalNumber) 00070 { 00071 return (float)frequency * BLUFF_BODY_METERS / strouhalNumber; 00072 } 00073 00074 float convertVelocityMetersToInches(float velocityMeters) 00075 { 00076 return velocityMeters * INCHES_IN_METER; 00077 } 00078 00079 void calculateFlow(void) 00080 { 00081 float velocity_prev = 0; 00082 float velocity = 10; 00083 float velocity_dif = 0.1; 00084 float strouhalNumber = 0; 00085 float reynoldsNumber = 0; 00086 float density = 0; 00087 float viscosity = 0; 00088 00089 while (velocity_dif > 0.01 && !isnan(velocity_dif)) 00090 { 00091 velocity_prev = velocity; 00092 viscosity = calculateViscosity(temperature); 00093 density = calculateDensity(temperature); 00094 reynoldsNumber = calculateReynoldsNumber(density, viscosity, velocity_prev); 00095 strouhalNumber = calculateStrouhalNumber(reynoldsNumber); 00096 velocity = calculateVelocity(freq_value, strouhalNumber); 00097 velocity_dif = fabs((velocity - velocity_prev)/(velocity + velocity_prev)); 00098 } 00099 00100 flow = (2.45 * PID_INCHES_SQUARED * velocity); 00101 }
Generated on Tue Jul 12 2022 20:46:01 by
1.7.2