Measurement of low frequencys based on timing between pulses

Dependents:   Energy_Meter_S0_Example

Committer:
jocis
Date:
Thu Nov 08 14:40:51 2012 +0000
Revision:
3:36dd0d59fdc8
Parent:
2:fc21262db17a
rework of documentation, 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 3:36dd0d59fdc8 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 3:36dd0d59fdc8 57 pc.printf ( "Pulses: act=%.3f average=%.3f counter=%d \r\n",
jocis 0:ef402fb370c5 58 pulses.getAct(),
jocis 2:fc21262db17a 59 pulses.getAverage(),
jocis 2:fc21262db17a 60 pulses.getCounter() );
jocis 0:ef402fb370c5 61
jocis 0:ef402fb370c5 62 wait(3.14);
jocis 0:ef402fb370c5 63 }
jocis 0:ef402fb370c5 64 }
jocis 3:36dd0d59fdc8 65 * @endcode
jocis 3:36dd0d59fdc8 66 */
jocis 0:ef402fb370c5 67 class Pulses {
jocis 0:ef402fb370c5 68 public:
jocis 0:ef402fb370c5 69
jocis 0:ef402fb370c5 70 /** Pulses type format */
jocis 0:ef402fb370c5 71 enum PulseType {
jocis 3:36dd0d59fdc8 72 RISE=1, //< Trigger on rising edge
jocis 3:36dd0d59fdc8 73 FALL=2, //< Trigger on falling edge
jocis 3:36dd0d59fdc8 74 CHANGE=3 //< Trigger on both edges. On symmetrical pulses you can improve precision by 2
jocis 0:ef402fb370c5 75 };
jocis 0:ef402fb370c5 76
jocis 0:ef402fb370c5 77 /** constructor of Pulses object
jocis 3:36dd0d59fdc8 78 *
jocis 3:36dd0d59fdc8 79 * @param inPin Pin number of input pin. All port pins are possible except p19 and p20
jocis 3:36dd0d59fdc8 80 * @param type Type of edge detection. RISE, FALL, CHANGE
jocis 3:36dd0d59fdc8 81 * @param timeout Timeout in seconds to handle a stopped pulse-generator (standing motor). Max 15 minutes.
jocis 3:36dd0d59fdc8 82 * @param counter Start value of the internal pulses counter to offset pSum (in get()) e.g. after reboot
jocis 3:36dd0d59fdc8 83 */
jocis 3:36dd0d59fdc8 84 explicit Pulses(PinName inPin, PulseType type=FALL, unsigned int timeout=600, unsigned int counter=0);
jocis 0:ef402fb370c5 85
jocis 2:fc21262db17a 86 /** Gets the frequency based on the last 2 pulses. It's only a snapshot and its not representative.
jocis 3:36dd0d59fdc8 87 *
jocis 3:36dd0d59fdc8 88 * @return Actual frequency
jocis 3:36dd0d59fdc8 89 */
jocis 0:ef402fb370c5 90 float getAct();
jocis 0:ef402fb370c5 91
jocis 1:6eb686d7d16a 92 /** Gets the average of frequency based on all pulses since last call of this function
jocis 3:36dd0d59fdc8 93 *
jocis 3:36dd0d59fdc8 94 * @return Average frequency. -1 if no new pulses occurred since last call
jocis 3:36dd0d59fdc8 95 */
jocis 0:ef402fb370c5 96 float getAverage();
jocis 0:ef402fb370c5 97
jocis 3:36dd0d59fdc8 98 /** Gets the average, min, max and sum of frequency based on all pulses since last call of this function
jocis 3:36dd0d59fdc8 99 *
jocis 3:36dd0d59fdc8 100 * @param pAverage Pointer to float value to return average frequency. Use NULL to skip param. -1 if no new pulses occurred since last call.
jocis 3:36dd0d59fdc8 101 * @param pMin Pointer to float value to return min. frequency. Use NULL to skip param. -1 if no new pulses occurred since last call.
jocis 3:36dd0d59fdc8 102 * @param pMax Pointer to float value to return max. frequency. Use NULL to skip param. -1 if no new pulses occurred since last call.
jocis 3:36dd0d59fdc8 103 * @param pSum Pointer to float value to return the accumulated average values. Use NULL to skip param.
jocis 3:36dd0d59fdc8 104 */
jocis 1:6eb686d7d16a 105 void get(float *pAverage, float *pMin, float *pMax, float *pSum=NULL);
jocis 1:6eb686d7d16a 106
jocis 2:fc21262db17a 107 /** Gets the number of pulses from the input pin since start
jocis 3:36dd0d59fdc8 108 *
jocis 3:36dd0d59fdc8 109 * @return Number of pulses
jocis 3:36dd0d59fdc8 110 */
jocis 0:ef402fb370c5 111 unsigned int getCounter();
jocis 3:36dd0d59fdc8 112
jocis 0:ef402fb370c5 113 /** Sets the factor for the getter-functions to convert in another unit (1.0=Hz, 60.0=rpm, ...)
jocis 3:36dd0d59fdc8 114 *
jocis 3:36dd0d59fdc8 115 * @param factor Factor to scale from Hz to user unit
jocis 3:36dd0d59fdc8 116 */
jocis 0:ef402fb370c5 117 void setFactor(float factor);
jocis 3:36dd0d59fdc8 118
jocis 0:ef402fb370c5 119
jocis 0:ef402fb370c5 120 protected:
jocis 2:fc21262db17a 121 // ISR
jocis 0:ef402fb370c5 122 void callback_in();
jocis 0:ef402fb370c5 123 void callback_timeout();
jocis 0:ef402fb370c5 124
jocis 0:ef402fb370c5 125 InterruptIn _in;
jocis 2:fc21262db17a 126 Timer _timer;
jocis 0:ef402fb370c5 127 Ticker _timeout;
jocis 3:36dd0d59fdc8 128
jocis 0:ef402fb370c5 129 PulseType _type;
jocis 3:36dd0d59fdc8 130
jocis 2:fc21262db17a 131 unsigned int _lastTimer;
jocis 0:ef402fb370c5 132 unsigned int _ActTime;
jocis 1:6eb686d7d16a 133 unsigned int _MinTime;
jocis 1:6eb686d7d16a 134 unsigned int _MaxTime;
jocis 1:6eb686d7d16a 135 unsigned int _AvrTimeSum;
jocis 1:6eb686d7d16a 136 unsigned int _AvrTimeCount;
jocis 0:ef402fb370c5 137 unsigned int _Counter;
jocis 0:ef402fb370c5 138 float _Factor;
jocis 0:ef402fb370c5 139 bool _bFirst;
jocis 0:ef402fb370c5 140 unsigned int _Timeout;
jocis 0:ef402fb370c5 141 unsigned int _TimeoutCount;
jocis 3:36dd0d59fdc8 142
jocis 0:ef402fb370c5 143 };
jocis 0:ef402fb370c5 144
jocis 0:ef402fb370c5 145 #endif