Measurement of low frequencys based on timing between pulses
Dependents: Energy_Meter_S0_Example
Pulses.h@1:6eb686d7d16a, 2012-11-08 (annotated)
- Committer:
- jocis
- Date:
- Thu Nov 08 07:25:41 2012 +0000
- Revision:
- 1:6eb686d7d16a
- Parent:
- 0:ef402fb370c5
- Child:
- 2:fc21262db17a
added min, max; added function get()
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jocis | 0:ef402fb370c5 | 1 | /* |
jocis | 0:ef402fb370c5 | 2 | * @author Jochen Krapf |
jocis | 0:ef402fb370c5 | 3 | * |
jocis | 0:ef402fb370c5 | 4 | * @section LICENSE |
jocis | 0:ef402fb370c5 | 5 | * |
jocis | 0:ef402fb370c5 | 6 | * Copyright (c) 2012 Jochen Krapf, MIT License |
jocis | 0:ef402fb370c5 | 7 | * |
jocis | 0:ef402fb370c5 | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
jocis | 0:ef402fb370c5 | 9 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
jocis | 0:ef402fb370c5 | 10 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
jocis | 0:ef402fb370c5 | 11 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
jocis | 0:ef402fb370c5 | 12 | * furnished to do so, subject to the following conditions: |
jocis | 0:ef402fb370c5 | 13 | * |
jocis | 0:ef402fb370c5 | 14 | * The above copyright notice and this permission notice shall be included in all copies or |
jocis | 0:ef402fb370c5 | 15 | * substantial portions of the Software. |
jocis | 0:ef402fb370c5 | 16 | * |
jocis | 0:ef402fb370c5 | 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
jocis | 0:ef402fb370c5 | 18 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
jocis | 0:ef402fb370c5 | 19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
jocis | 0:ef402fb370c5 | 20 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
jocis | 0:ef402fb370c5 | 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
jocis | 0:ef402fb370c5 | 22 | * |
jocis | 0:ef402fb370c5 | 23 | * @section DESCRIPTION |
jocis | 1:6eb686d7d16a | 24 | * mbed Pulses Library, for measurement of low frequencys based on timing between pulses |
jocis | 0:ef402fb370c5 | 25 | * |
jocis | 0:ef402fb370c5 | 26 | * Use cases: |
jocis | 0:ef402fb370c5 | 27 | * - Motor rotations (rpm) |
jocis | 0:ef402fb370c5 | 28 | * - Energy meter with SO interface |
jocis | 0:ef402fb370c5 | 29 | * |
jocis | 0:ef402fb370c5 | 30 | * Example: |
jocis | 0:ef402fb370c5 | 31 | * |
jocis | 0:ef402fb370c5 | 32 | * @code |
jocis | 0:ef402fb370c5 | 33 | #include "mbed.h" |
jocis | 0:ef402fb370c5 | 34 | #include "Pulses.h" |
jocis | 0:ef402fb370c5 | 35 | |
jocis | 0:ef402fb370c5 | 36 | Pulses pulses(p8, Pulses::FALL); |
jocis | 0:ef402fb370c5 | 37 | Serial pc(USBTX, USBRX); // tx, rx |
jocis | 0:ef402fb370c5 | 38 | |
jocis | 0:ef402fb370c5 | 39 | int main() { |
jocis | 0:ef402fb370c5 | 40 | // choose on of the following unit scales |
jocis | 0:ef402fb370c5 | 41 | pulses.setFactor(1.0f); // Hz |
jocis | 0:ef402fb370c5 | 42 | pulses.setFactor(60.0f); // rpm |
jocis | 0:ef402fb370c5 | 43 | pulses.setFactor(3600.0f/2000.0f); // kWh; energy meter with SO interface - 2000 pulses per kWh |
jocis | 0:ef402fb370c5 | 44 | |
jocis | 0:ef402fb370c5 | 45 | while(1) { |
jocis | 0:ef402fb370c5 | 46 | pc.printf ( "Pulses: counter=%d act=%.3f average=%.3f\r\n", |
jocis | 0:ef402fb370c5 | 47 | pulses.getCounter(), |
jocis | 0:ef402fb370c5 | 48 | pulses.getAct(), |
jocis | 0:ef402fb370c5 | 49 | pulses.getAverage() ); |
jocis | 0:ef402fb370c5 | 50 | |
jocis | 0:ef402fb370c5 | 51 | wait(3.14); |
jocis | 0:ef402fb370c5 | 52 | } |
jocis | 0:ef402fb370c5 | 53 | } |
jocis | 0:ef402fb370c5 | 54 | * @endcode |
jocis | 0:ef402fb370c5 | 55 | * |
jocis | 0:ef402fb370c5 | 56 | */ |
jocis | 0:ef402fb370c5 | 57 | |
jocis | 0:ef402fb370c5 | 58 | #ifndef MBED_PULSES_H |
jocis | 0:ef402fb370c5 | 59 | #define MBED_PULSES_H |
jocis | 0:ef402fb370c5 | 60 | |
jocis | 0:ef402fb370c5 | 61 | #include "mbed.h" |
jocis | 0:ef402fb370c5 | 62 | |
jocis | 0:ef402fb370c5 | 63 | class TimerDiff : public Timer { |
jocis | 0:ef402fb370c5 | 64 | public: |
jocis | 0:ef402fb370c5 | 65 | TimerDiff(const char *name = NULL) : Timer(name) { |
jocis | 0:ef402fb370c5 | 66 | reset(); |
jocis | 0:ef402fb370c5 | 67 | start(); |
jocis | 0:ef402fb370c5 | 68 | _last = 0; |
jocis | 0:ef402fb370c5 | 69 | }; |
jocis | 0:ef402fb370c5 | 70 | |
jocis | 0:ef402fb370c5 | 71 | unsigned int readDiff_us() { |
jocis | 0:ef402fb370c5 | 72 | unsigned int act = read_us(); |
jocis | 0:ef402fb370c5 | 73 | unsigned int diff; |
jocis | 0:ef402fb370c5 | 74 | |
jocis | 0:ef402fb370c5 | 75 | if ( act < _last ) // overflow |
jocis | 0:ef402fb370c5 | 76 | diff = act - _last; |
jocis | 0:ef402fb370c5 | 77 | else |
jocis | 0:ef402fb370c5 | 78 | diff = act - _last; |
jocis | 0:ef402fb370c5 | 79 | _last = act; |
jocis | 0:ef402fb370c5 | 80 | return diff; |
jocis | 0:ef402fb370c5 | 81 | } |
jocis | 0:ef402fb370c5 | 82 | |
jocis | 0:ef402fb370c5 | 83 | float readDiff() { |
jocis | 0:ef402fb370c5 | 84 | return (float)readDiff_us() * 0.000001; |
jocis | 0:ef402fb370c5 | 85 | } |
jocis | 0:ef402fb370c5 | 86 | |
jocis | 0:ef402fb370c5 | 87 | protected: |
jocis | 0:ef402fb370c5 | 88 | unsigned int _last; |
jocis | 0:ef402fb370c5 | 89 | }; |
jocis | 0:ef402fb370c5 | 90 | |
jocis | 0:ef402fb370c5 | 91 | |
jocis | 0:ef402fb370c5 | 92 | /** A class to calculate frequencys based on timing between pulses. Pulse-frequency can be about or slower than loop/aquisition time |
jocis | 0:ef402fb370c5 | 93 | * |
jocis | 0:ef402fb370c5 | 94 | */ |
jocis | 0:ef402fb370c5 | 95 | class Pulses { |
jocis | 0:ef402fb370c5 | 96 | public: |
jocis | 0:ef402fb370c5 | 97 | |
jocis | 0:ef402fb370c5 | 98 | /** Pulses type format */ |
jocis | 0:ef402fb370c5 | 99 | enum PulseType { |
jocis | 0:ef402fb370c5 | 100 | RISE=1, |
jocis | 0:ef402fb370c5 | 101 | FALL=2, |
jocis | 0:ef402fb370c5 | 102 | CHANGE=3 |
jocis | 0:ef402fb370c5 | 103 | }; |
jocis | 0:ef402fb370c5 | 104 | |
jocis | 0:ef402fb370c5 | 105 | /** constructor of Pulses object |
jocis | 0:ef402fb370c5 | 106 | * |
jocis | 0:ef402fb370c5 | 107 | * @param inPin Pin number of input pin. All port pins except p19 and p20 |
jocis | 0:ef402fb370c5 | 108 | * @param type Type of edge detection. |
jocis | 0:ef402fb370c5 | 109 | * @param timeout Timeout in seconds to handle an offline pulse-generator (standing motor). Max 15 minutes. |
jocis | 0:ef402fb370c5 | 110 | */ |
jocis | 1:6eb686d7d16a | 111 | explicit Pulses(PinName inPin, PulseType type = RISE, unsigned int timeout=600, unsigned int counter=0); |
jocis | 0:ef402fb370c5 | 112 | |
jocis | 0:ef402fb370c5 | 113 | /** Gets the frequency based on the last 2 pulses |
jocis | 0:ef402fb370c5 | 114 | * |
jocis | 1:6eb686d7d16a | 115 | * @return Actual frequencey |
jocis | 0:ef402fb370c5 | 116 | */ |
jocis | 0:ef402fb370c5 | 117 | float getAct(); |
jocis | 0:ef402fb370c5 | 118 | |
jocis | 1:6eb686d7d16a | 119 | /** Gets the average of frequency based on all pulses since last call of this function |
jocis | 0:ef402fb370c5 | 120 | * |
jocis | 0:ef402fb370c5 | 121 | * @return Average frequency. -1 if no new pulses occoured since last call |
jocis | 0:ef402fb370c5 | 122 | */ |
jocis | 0:ef402fb370c5 | 123 | float getAverage(); |
jocis | 0:ef402fb370c5 | 124 | |
jocis | 1:6eb686d7d16a | 125 | /** Gets the average, min, max and summ of frequency based on all pulses since last call of this function |
jocis | 1:6eb686d7d16a | 126 | * |
jocis | 1:6eb686d7d16a | 127 | * @param pAverage Pointer to float value to return average frequency. Use NULL to skip param. -1 if no new pulses occoured since last call. |
jocis | 1:6eb686d7d16a | 128 | * @param pMin Pointer to float value to return min. frequency. Use NULL to skip param. -1 if no new pulses occoured since last call. |
jocis | 1:6eb686d7d16a | 129 | * @param pMax Pointer to float value to return max. frequency. Use NULL to skip param. -1 if no new pulses occoured since last call. |
jocis | 1:6eb686d7d16a | 130 | * @param pSum Pointer to float value to return the accumulated average values. Use NULL to skip param. |
jocis | 1:6eb686d7d16a | 131 | */ |
jocis | 1:6eb686d7d16a | 132 | void get(float *pAverage, float *pMin, float *pMax, float *pSum=NULL); |
jocis | 1:6eb686d7d16a | 133 | |
jocis | 0:ef402fb370c5 | 134 | /** Gets the number of pulses since start |
jocis | 0:ef402fb370c5 | 135 | * |
jocis | 0:ef402fb370c5 | 136 | * @return Number of pulses |
jocis | 0:ef402fb370c5 | 137 | */ |
jocis | 0:ef402fb370c5 | 138 | unsigned int getCounter(); |
jocis | 0:ef402fb370c5 | 139 | |
jocis | 0:ef402fb370c5 | 140 | /** Sets the factor for the getter-functions to convert in another unit (1.0=Hz, 60.0=rpm, ...) |
jocis | 0:ef402fb370c5 | 141 | * |
jocis | 0:ef402fb370c5 | 142 | * @param factor Factor to scale from Hz to user unit |
jocis | 0:ef402fb370c5 | 143 | */ |
jocis | 0:ef402fb370c5 | 144 | void setFactor(float factor); |
jocis | 0:ef402fb370c5 | 145 | |
jocis | 0:ef402fb370c5 | 146 | |
jocis | 0:ef402fb370c5 | 147 | protected: |
jocis | 0:ef402fb370c5 | 148 | |
jocis | 0:ef402fb370c5 | 149 | void callback_in(); |
jocis | 0:ef402fb370c5 | 150 | void callback_timeout(); |
jocis | 0:ef402fb370c5 | 151 | |
jocis | 0:ef402fb370c5 | 152 | InterruptIn _in; |
jocis | 0:ef402fb370c5 | 153 | TimerDiff _timer; |
jocis | 0:ef402fb370c5 | 154 | Ticker _timeout; |
jocis | 0:ef402fb370c5 | 155 | |
jocis | 0:ef402fb370c5 | 156 | PulseType _type; |
jocis | 0:ef402fb370c5 | 157 | |
jocis | 0:ef402fb370c5 | 158 | unsigned int _ActTime; |
jocis | 1:6eb686d7d16a | 159 | unsigned int _MinTime; |
jocis | 1:6eb686d7d16a | 160 | unsigned int _MaxTime; |
jocis | 1:6eb686d7d16a | 161 | unsigned int _AvrTimeSum; |
jocis | 1:6eb686d7d16a | 162 | unsigned int _AvrTimeCount; |
jocis | 0:ef402fb370c5 | 163 | unsigned int _Counter; |
jocis | 0:ef402fb370c5 | 164 | float _Factor; |
jocis | 0:ef402fb370c5 | 165 | bool _bFirst; |
jocis | 0:ef402fb370c5 | 166 | unsigned int _Timeout; |
jocis | 0:ef402fb370c5 | 167 | unsigned int _TimeoutCount; |
jocis | 0:ef402fb370c5 | 168 | |
jocis | 0:ef402fb370c5 | 169 | }; |
jocis | 0:ef402fb370c5 | 170 | |
jocis | 0:ef402fb370c5 | 171 | #endif |