David Smart / PowerMeasurement
Embed: (wiki syntax)

« Back to documentation index

PowerMeasurement Class Reference

PowerMeasurement Class Reference

The PowerMeasurement class is used to take voltage and current measurements and compute power consumption. More...

#include <PowerMeasurement.h>

Data Structures

struct  RawPowerData_T
 Each raw sample consists of 2 values - the voltage and the current at that moment. More...

Public Types

typedef uint16_t(* GetVoltage_T )(void)
 The callback to get the instantaneous value of the voltage time-synchronous with the current measurement.

Public Member Functions

 PowerMeasurement (AnalogIn *AinList, BusOut *MuxBus=NULL, DigitalOut *Select=NULL, GetVoltage_T callback=NULL, int AinCount=6, int MuxChannels=8)
 The constructor for the PowerMeasurement class is used to create various hardware assignments.
 ~PowerMeasurement ()
 The destructor.
void frequency (float _Hz)
 Define the frequency of the line voltage, which in turn defines the sample-rate.
void period_us (uint32_t uSec)
 Define the measuremenbt interval, as an alternative to setting the frequency.
bool SetFullScaleCurrent (int channel, float fullScaleCurrentCalibration)
 Set the analog input to current calibration value for a channel.
bool SetFullScaleVoltage (float fullScaleVoltageCalibration)
 Set the voltage value representing the full scale measurement.
bool StartMeasurement (int channel)
 Starts a measurement on the specified channel.
bool readable ()
 Determines if the conversion is complete and the results are readable.
float GetRMSCurrent ()
 Get the rms current measurement.
float GetRMSVoltage ()
 Get the rms voltage measurement.
float GetRealPower ()
 Get the real power measurement.
float GetApparentPower ()
 Get the apparent power measurement.
float GetPowerFactor ()
 Get the power factor.
bool GetPeakCurrent (float *negPeak, float *posPeak)
 Get the peak current measurement values from the recent sample.
bool GetRawSample (int sample, RawPowerData_T *rawsample)
 Get the raw sample data for a given sample number.
int GetRawSampleCount (void)
 Get the count of raw samples that are being taken,.

Detailed Description

The PowerMeasurement class is used to take voltage and current measurements and compute power consumption.

This is done as a process that takes some amount of time, as it will sample two inputs over a number of cycles of the AC line input (typically 60 Hz). In order to avoid blocking the CPU any more than necessary it will be processed with a ticker. So, there will be functions to start an acquisition, monitor the acquisition, and extract the results when it is done.

The power measurement is performed on virtual channels, which map to real analog inputs. The virtual channels may be direct-wired to the processor analog inputs, or it may be esternally multiplexed.

Electrically, the voltage measurements are normalized so the A/C swing is biased to mid-range on the A/D and then peak-to-peak is measured from 0 to FFFF.

              NumMuxes
              +----------+   MuxChannels
 Input 1   ---|0        a|--------------------------------------+
 Input 2   ---|1        b|--------------------------------------+
 Input 3   ---|2        c|--------------------------------------+
 ...       ---|          |                                      |
 ...       ---|       inh|------------------------------------+ |
 ...       ---|          |        AinCount                    | |
 ...       ---|          |        +--------------+            | |
 Input 8   ---|7        Y|--------|p15           |            | |
              +----------+        |              |  Select    | |
                                  |           pxx|------------+ |
                             - - -|p16           |              |
                                  |              |              |
                                  |              |              |
                                  |              |  MusBus      |
                                  |           pcc|--------------+
                                  |           pbb|--------------+
                                  |           paa|--------------+
  GetVoltage() ------->           |              |
                                  +--------------+

Definition at line 49 of file PowerMeasurement.h.


Member Typedef Documentation

typedef uint16_t(* GetVoltage_T)(void)

The callback to get the instantaneous value of the voltage time-synchronous with the current measurement.

The called function should return the instantaneous voltage measurement. The value is an unsigned value normalized to the range of 0 to FFFF, with a zero-offset of 32768.

If not voltage measurement system is in place, the user may choose not to define this function, in which case the power measurements cannot be made. The current measurements are still valid, and power can be computed by the users program.

Returns:
a value in the range of 0 to FFFF, biased to the mid-point of 32768 which represents 0.0v.

Definition at line 62 of file PowerMeasurement.h.


Constructor & Destructor Documentation

PowerMeasurement ( AnalogIn *  AinList,
BusOut *  MuxBus = NULL,
DigitalOut *  Select = NULL,
GetVoltage_T  callback = NULL,
int  AinCount = 6,
int  MuxChannels = 8 
)

The constructor for the PowerMeasurement class is used to create various hardware assignments.

Parameters:
[in]AinListis a pointer to an array of AnalogIn classes
[in]MuxBusis a pointer to a BusOut that is used to select the external multiplexer channel.
[in]Selectis a pointer to a DigitalOut that is used to enable the external multplexer.
[in]v_getis the callback used to get the voltage time synchronous with the current measurement.
[in]AinCountis the count of Analog Inputs to use, from 1 to 6, which maps to p15 thru p20. Default: 6.
[in]MuxChannelsis the count of channels on each external multiplexer. Default: 8.

Definition at line 46 of file PowerMeasurement.cpp.

The destructor.

Definition at line 82 of file PowerMeasurement.cpp.


Member Function Documentation

void frequency ( float  _Hz )

Define the frequency of the line voltage, which in turn defines the sample-rate.

Defines the overall frequency of the line voltage.

Based on this line frequency, the sample-rate for the measurement is set to achieve 'SAMPLES_PER_CYCLE' samples per cycle, and 'CYCLES_PER_SAMPLE' cycles.

Parameters:
[in]Hzsets the line frequency.

Based on this line frequency, the sample-rate for the measurement is set.

Parameters:
[in]Hzsets the line frequency.

Definition at line 93 of file PowerMeasurement.cpp.

float GetApparentPower (  )

Get the apparent power measurement.

This retrieves the apparent power measurement for the channel which just completed measurement.

Note:
This is only valid if the user supplied GetVoltage() function was provided.
Returns:
the apparent power measurement.

This retrieves the apparent power measurement for the channel which just completed measurement.

Returns:
the apparent power measurement.

Definition at line 321 of file PowerMeasurement.cpp.

bool GetPeakCurrent ( float *  negPeak,
float *  posPeak 
)

Get the peak current measurement values from the recent sample.

Note:
if either parameter is null, that data will not be provided.
Parameters:
inout]negPeak is a pointer to the negative going peak current measured.
inout]posPeak is a pointer to the positive going peak current measured.
Returns:
true if a measurement was completed and the data was updated.
false if a measurement has not started or is in process.

Definition at line 291 of file PowerMeasurement.cpp.

float GetPowerFactor (  )

Get the power factor.

Note:
This is only valid if the user supplied GetVoltage() function was provided.
Returns:
the power factor measurement.

Definition at line 330 of file PowerMeasurement.cpp.

bool GetRawSample ( int  sample,
RawPowerData_T rawsample 
)

Get the raw sample data for a given sample number.

If a measurement is complete, or has at least proceeded beyond the desired sample, then pass that raw data to the calling function.

Parameters:
[in]sampleis the sample number of interest, ranging from 0 to N-1.
inout]rawsample is a pointer to where the specified sample will be written.
Returns:
true if the sample was available for the calling function.
false if the same was not available.

Definition at line 339 of file PowerMeasurement.cpp.

int GetRawSampleCount ( void   )

Get the count of raw samples that are being taken,.

Returns:
count of samples that are taken.

Definition at line 349 of file PowerMeasurement.cpp.

float GetRealPower (  )

Get the real power measurement.

This retrieves the real power measurement for the channel which just completed measurement. This is the average of the instantaneous power.

Note:
This is only valid if the user supplied GetVoltage() function was provided.
Returns:
the real power measurement.

This retrieves the real power measurement for the channel which just completed measurement. This is the average of the instantaneous power.

Returns:
the real power measurement.

Definition at line 227 of file PowerMeasurement.cpp.

float GetRMSCurrent (  )

Get the rms current measurement.

This retrieves the rms current measurement for the channel which just completed measurement.

Returns:
the rm current measurement.

Definition at line 274 of file PowerMeasurement.cpp.

float GetRMSVoltage (  )

Get the rms voltage measurement.

This retrieves the rms voltage measurement for the channel which just completed measurement.

Note:
This is only valid if the user supplied GetVoltage() function was provided.
Returns:
the rms voltage measurement.

This retrieves the rms voltage measurement for the channel which just completed measurement.

Returns:
the rms voltage measurement.

Definition at line 251 of file PowerMeasurement.cpp.

void period_us ( uint32_t  uSec )

Define the measuremenbt interval, as an alternative to setting the frequency.

Defines the measuremenbt interval.

Instead of defining the measurement interval by specifying the line frequency, the period can be directly set.

Parameters:
uSecis the number of microseconds between samples.

Instead of defining the measurement interval by line frequency, the period can be directly set.

Parameters:
uSecis the number of microseconds between samples.

Definition at line 105 of file PowerMeasurement.cpp.

bool readable (  )

Determines if the conversion is complete and the results are readable.

When operating in the asynchronous mode, this API can be used to detect when the conversion is complete.

Returns:
true if the measurement is complete (or if no measurement is in process).
false if the measurement is in process.
true if the measurement is complete (or if no measurement is in process).
false if the measurement is in process.

Definition at line 215 of file PowerMeasurement.cpp.

bool SetFullScaleCurrent ( int  channel,
float  fullScaleCurrentCalibration 
)

Set the analog input to current calibration value for a channel.

Set the voltage to current calibration value for a channel.

Each analog input channel can be configured for the current sensor used on that channel. If the channel has a 30 A current sensor, that channel should be set to 30.0f. If the user calibrates the sensor more precisely, an improved calibration factor (e.g. 31.1) can be defined.

The calibration is based on the full-scale reading from the A/D. As the A/D is a normalized uint16_t value, and biased to approximately mid-supply, the full scale range is then 1/2 of the total range, or approximately 32767. Component tolerances, of the voltage reference, the mid-supply divider, and the current sensing component can affect this.

Parameters:
[in]channeldefines the channel to calibrate.
[in]fullScaleCurrentCalibrationis the calibration factor representing the full-scale current.
Returns:
true if the value is accepted.
false if the channel was incorrect.

Each analog input channel can be configured for the current sensor used on that channel. If the channel has a 30 A current sensor, that channel should be set to 30.0f. If the user calibrates the sensor more precisely, an improved calibration factor (e.g. 31.1) can be defined.

Parameters:
[in]channeldefines the channel to calibrate.
[in]fullScaleCurrentis the calibration factor representing the full-scale current.
Returns:
true if the value is accepted.
false if the channel was incorrect.

Definition at line 122 of file PowerMeasurement.cpp.

bool SetFullScaleVoltage ( float  fullScaleVoltageCalibration )

Set the voltage value representing the full scale measurement.

The GetVoltage callback is expecting a uint16_t as the return value. When configured for a 120V circuit, which measures approximately 170v peak, the fullScaleVoltageCalibration value would be 170.0f.

The calibration is based on the full-scale reading from the A/D. As the A/D is a normalized uint16_t value, and biased to approximately mid-supply, the full scale range is then 1/2 of the total range, or approximately 32767. Component tolerances, of the voltage reference, the mid-supply divider, and the current sensing component can affect this.

Parameters:
[in]fullScaleVoltageCalibrationis the full-scale voltage value.
Returns:
true if the value is accepted, which it will be.

The GetVoltage callback is based on a uint16_t. This API sets the full scale voltage representing the value FFFF. Based on an A/C input, biased to the split supply, this represents an A/D value of 7FFF. When configured for a 120V circuit, which measures approximately 170v peak, the fullScaleVoltage value would be 170.0f.

Parameters:
[in]fullScaleVoltageis the full-scale voltage value.
Returns:
true if the value is accepted.

Definition at line 142 of file PowerMeasurement.cpp.

bool StartMeasurement ( int  channel )

Starts a measurement on the specified channel.

This starts the measurement on a specified channel. The subsystem will then configure the external multiplexer and a/d sampling to gather the the samples.

The actual sampling of the data can be either synchronous, or asynchonous, based on a define value.

Parameters:
[in]channeldefines the channel to measure. This is in the range of 0 to N-1, where N is AinCount * MuxChannels.
Returns:
true if the measurement can be started (in async mode), or if the measurement is complete when operating in synchonous mode.
false if the measurement cannot be started - likely because of an incorrect channel selection.

This starts the measurement on a specified channel.

Parameters:
[in]channeldefines the channel to measure. This is in the range of 0 to N-1, where N is AinCount * MuxChannels.
Returns:
true if the measurement can be started.
false if the measurement cannot be started - likely because of an incorrect channel selection.

Definition at line 158 of file PowerMeasurement.cpp.