Measurement of low frequencys based on timing between pulses

Dependents:   Energy_Meter_S0_Example

Committer:
jocis
Date:
Thu Nov 08 12:01:02 2012 +0000
Revision:
2:fc21262db17a
Parent:
1:6eb686d7d16a
Child:
3:36dd0d59fdc8
minor bug fixes

Who changed what in which revision?

UserRevisionLine numberNew 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 2:fc21262db17a 27 * - Frequency counter for frequ. about or below sample rate (Hz)
jocis 0:ef402fb370c5 28 * - Motor rotations (rpm)
jocis 0:ef402fb370c5 29 * - Energy meter with SO interface
jocis 0:ef402fb370c5 30 *
jocis 2:fc21262db17a 31 *
jocis 2:fc21262db17a 32 */
jocis 2:fc21262db17a 33
jocis 2:fc21262db17a 34 #ifndef MBED_PULSES_H
jocis 2:fc21262db17a 35 #define MBED_PULSES_H
jocis 2:fc21262db17a 36
jocis 2:fc21262db17a 37 #include "mbed.h"
jocis 2:fc21262db17a 38
jocis 2:fc21262db17a 39 /** A class to calculate frequencys based on timing between pulses. Pulse-frequency can be about or slower than loop/aquisition time
jocis 2:fc21262db17a 40 *
jocis 0:ef402fb370c5 41 * Example:
jocis 0:ef402fb370c5 42 *
jocis 0:ef402fb370c5 43 * @code
jocis 0:ef402fb370c5 44 #include "mbed.h"
jocis 0:ef402fb370c5 45 #include "Pulses.h"
jocis 0:ef402fb370c5 46
jocis 0:ef402fb370c5 47 Pulses pulses(p8, Pulses::FALL);
jocis 0:ef402fb370c5 48 Serial pc(USBTX, USBRX); // tx, rx
jocis 0:ef402fb370c5 49
jocis 0:ef402fb370c5 50 int main() {
jocis 0:ef402fb370c5 51 // choose on of the following unit scales
jocis 0:ef402fb370c5 52 pulses.setFactor(1.0f); // Hz
jocis 0:ef402fb370c5 53 pulses.setFactor(60.0f); // rpm
jocis 0:ef402fb370c5 54 pulses.setFactor(3600.0f/2000.0f); // kWh; energy meter with SO interface - 2000 pulses per kWh
jocis 0:ef402fb370c5 55
jocis 0:ef402fb370c5 56 while(1) {
jocis 0:ef402fb370c5 57 pc.printf ( "Pulses: counter=%d act=%.3f average=%.3f\r\n",
jocis 2:fc21262db17a 58 ,
jocis 0:ef402fb370c5 59 pulses.getAct(),
jocis 2:fc21262db17a 60 pulses.getAverage(),
jocis 2:fc21262db17a 61 pulses.getCounter() );
jocis 0:ef402fb370c5 62
jocis 0:ef402fb370c5 63 wait(3.14);
jocis 0:ef402fb370c5 64 }
jocis 0:ef402fb370c5 65 }
jocis 0:ef402fb370c5 66 * @endcode
jocis 0:ef402fb370c5 67 */
jocis 0:ef402fb370c5 68 class Pulses {
jocis 0:ef402fb370c5 69 public:
jocis 0:ef402fb370c5 70
jocis 0:ef402fb370c5 71 /** Pulses type format */
jocis 0:ef402fb370c5 72 enum PulseType {
jocis 0:ef402fb370c5 73 RISE=1,
jocis 0:ef402fb370c5 74 FALL=2,
jocis 0:ef402fb370c5 75 CHANGE=3
jocis 0:ef402fb370c5 76 };
jocis 0:ef402fb370c5 77
jocis 0:ef402fb370c5 78 /** constructor of Pulses object
jocis 0:ef402fb370c5 79 *
jocis 2:fc21262db17a 80 * @param inPin Pin number of input pin. All port pins are possible except p19 and p20
jocis 0:ef402fb370c5 81 * @param type Type of edge detection.
jocis 0:ef402fb370c5 82 * @param timeout Timeout in seconds to handle an offline pulse-generator (standing motor). Max 15 minutes.
jocis 2:fc21262db17a 83 * @param counter Start value of the internal pulses counter to offset pSum (in get()) e.g. after reboot
jocis 0:ef402fb370c5 84 */
jocis 1:6eb686d7d16a 85 explicit Pulses(PinName inPin, PulseType type = RISE, unsigned int timeout=600, unsigned int counter=0);
jocis 0:ef402fb370c5 86
jocis 2:fc21262db17a 87 /** Gets the frequency based on the last 2 pulses. It's only a snapshot and its not representative.
jocis 0:ef402fb370c5 88 *
jocis 1:6eb686d7d16a 89 * @return Actual frequencey
jocis 0:ef402fb370c5 90 */
jocis 0:ef402fb370c5 91 float getAct();
jocis 0:ef402fb370c5 92
jocis 1:6eb686d7d16a 93 /** Gets the average of frequency based on all pulses since last call of this function
jocis 0:ef402fb370c5 94 *
jocis 0:ef402fb370c5 95 * @return Average frequency. -1 if no new pulses occoured since last call
jocis 0:ef402fb370c5 96 */
jocis 0:ef402fb370c5 97 float getAverage();
jocis 0:ef402fb370c5 98
jocis 1:6eb686d7d16a 99 /** Gets the average, min, max and summ of frequency based on all pulses since last call of this function
jocis 1:6eb686d7d16a 100 *
jocis 1:6eb686d7d16a 101 * @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 102 * @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 103 * @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 104 * @param pSum Pointer to float value to return the accumulated average values. Use NULL to skip param.
jocis 1:6eb686d7d16a 105 */
jocis 1:6eb686d7d16a 106 void get(float *pAverage, float *pMin, float *pMax, float *pSum=NULL);
jocis 1:6eb686d7d16a 107
jocis 2:fc21262db17a 108 /** Gets the number of pulses from the input pin since start
jocis 0:ef402fb370c5 109 *
jocis 0:ef402fb370c5 110 * @return Number of pulses
jocis 0:ef402fb370c5 111 */
jocis 0:ef402fb370c5 112 unsigned int getCounter();
jocis 0:ef402fb370c5 113
jocis 0:ef402fb370c5 114 /** Sets the factor for the getter-functions to convert in another unit (1.0=Hz, 60.0=rpm, ...)
jocis 0:ef402fb370c5 115 *
jocis 0:ef402fb370c5 116 * @param factor Factor to scale from Hz to user unit
jocis 0:ef402fb370c5 117 */
jocis 0:ef402fb370c5 118 void setFactor(float factor);
jocis 0:ef402fb370c5 119
jocis 0:ef402fb370c5 120
jocis 0:ef402fb370c5 121 protected:
jocis 2:fc21262db17a 122 // ISR
jocis 0:ef402fb370c5 123 void callback_in();
jocis 0:ef402fb370c5 124 void callback_timeout();
jocis 0:ef402fb370c5 125
jocis 0:ef402fb370c5 126 InterruptIn _in;
jocis 2:fc21262db17a 127 Timer _timer;
jocis 0:ef402fb370c5 128 Ticker _timeout;
jocis 0:ef402fb370c5 129
jocis 0:ef402fb370c5 130 PulseType _type;
jocis 0:ef402fb370c5 131
jocis 2:fc21262db17a 132 unsigned int _lastTimer;
jocis 0:ef402fb370c5 133 unsigned int _ActTime;
jocis 1:6eb686d7d16a 134 unsigned int _MinTime;
jocis 1:6eb686d7d16a 135 unsigned int _MaxTime;
jocis 1:6eb686d7d16a 136 unsigned int _AvrTimeSum;
jocis 1:6eb686d7d16a 137 unsigned int _AvrTimeCount;
jocis 0:ef402fb370c5 138 unsigned int _Counter;
jocis 0:ef402fb370c5 139 float _Factor;
jocis 0:ef402fb370c5 140 bool _bFirst;
jocis 0:ef402fb370c5 141 unsigned int _Timeout;
jocis 0:ef402fb370c5 142 unsigned int _TimeoutCount;
jocis 0:ef402fb370c5 143
jocis 0:ef402fb370c5 144 };
jocis 0:ef402fb370c5 145
jocis 0:ef402fb370c5 146 #endif