Bluenet
5.7.0
Bluenet, firmware for nRF52 smart home devices
|
Sample power. More...
#include <cs_PowerSampling.h>
Classes | |
struct | power_t |
Struct that defines the buffer received from the ADC sampler in scanning mode. More... | |
Public Member Functions | |
void | init (const boards_config_t *boardConfig) |
Init the class. More... | |
void | powerSampleFirstStart () |
Initializes and starts the ADC, also starts interval timer. More... | |
void | startSampling () |
Starts a new power sample burst. More... | |
void | powerSampleAdcDone (adc_buffer_id_t bufIndex) |
Called when the sample burst is finished. More... | |
void | sampleCurrentDone (uint8_t type) |
Fill up the current curve and send it out over bluetooth @type specifies over which characteristic the current curve should be sent. More... | |
void | enableZeroCrossingInterrupt (ps_zero_crossing_cb_t callback) |
Enable zero crossing detection on given channel, generating interrupts. More... | |
uint32_t | getSkippedBufCount () |
Get the number of bufs that have been recently skipped. More... | |
void | handleEvent (event_t &event) |
handle (crownstone) events More... | |
Static Public Member Functions | |
static PowerSampling & | getInstance () |
Gets a static singleton (no dynamic memory allocation) More... | |
Private Member Functions | |
PowerSampling () | |
TYPIFY (CONFIG_VOLTAGE_MULTIPLIER) _voltageMultiplier | |
TYPIFY (CONFIG_CURRENT_MULTIPLIER) _currentMultiplier | |
Voltage multiplier from settings. More... | |
TYPIFY (CONFIG_VOLTAGE_ADC_ZERO) _voltageZero | |
Current multiplier from settings. More... | |
TYPIFY (CONFIG_CURRENT_ADC_ZERO) _currentZero | |
Voltage zero from settings. More... | |
TYPIFY (CONFIG_POWER_ZERO) _powerZero | |
Current zero from settings. More... | |
TYPIFY (CONFIG_SOFT_FUSE_CURRENT_THRESHOLD) _currentMilliAmpThreshold | |
void | initEnergyUsed () |
Load energy used from IPC ram. More... | |
void | storeEnergyUsed () |
Store energy used to IPC ram. More... | |
void | initAverages () |
Initialize the moving averages. More... | |
bool | isValidBuf (adc_buffer_id_t bufIndex) |
Whether the given buffer is valid. More... | |
bool | isConsecutiveBuf (adc_buffer_seq_nr_t seqNr, adc_buffer_seq_nr_t prevSeqNr) |
Whether the given sequence nr follows directly after the previous sequence nr. More... | |
void | removeInvalidBufs () |
Remove all buffers from queue that are older than the newest invalid buffer. More... | |
void | calculateVoltageZero (adc_buffer_id_t bufIndex) |
Calculate the value of the zero line of the voltage samples (the offset). More... | |
void | calculateCurrentZero (adc_buffer_id_t bufIndex) |
Calculate the value of the zero line of the current samples. More... | |
void | filter (adc_buffer_id_t bufIndexIn, adc_buffer_id_t bufIndexOut, adc_channel_id_t channel_id) |
Filter the samples. More... | |
bool | isVoltageAndCurrentSwapped (adc_buffer_id_t bufIndex, adc_buffer_id_t prevBufIndex) |
Checks if voltage and current index are swapped. More... | |
bool | calculatePower (adc_buffer_id_t bufIndex) |
Calculate the average power usage. More... | |
void | calculateSlowAveragePower (float powerMilliWatt, float fastAvgPowerMilliWatt) |
void | calibratePowerZero (int32_t powerMilliWatt) |
Determines measured power usage with no load. More... | |
void | calculateEnergy () |
Calculate the energy used. More... | |
void | checkSoftfuse (int32_t currentRmsMilliAmp, int32_t currentRmsMilliAmpFiltered, int32_t voltageRmsMilliVolt, adc_buffer_id_t bufIndex) |
Check if the current goes above a threshold (for long enough). More... | |
void | handleGetPowerSamples (PowerSamplesType type, uint8_t index, cs_result_t &result) |
void | selectNextPin (adc_channel_id_t channel) |
void | enableDifferentialModeCurrent (bool enable) |
void | enableDifferentialModeVoltage (bool enable) |
void | changeRange (uint8_t channel, int32_t amount) |
void | applyAdcConfig (adc_channel_id_t channelIndex) |
void | enableSwitchcraft (bool enable) |
void | printBuf (adc_buffer_id_t bufIndex) |
![]() | |
EventListener () | |
virtual | ~EventListener () |
unregisters the listener. More... | |
virtual void | handleEvent (event_t &event)=0 |
Handle events. More... | |
void | listen () |
Registers this with the EventDispatcher. More... | |
Private Attributes | |
bool | _isInitialized = false |
Variable to keep up whether power sampling is initialized. More... | |
ADC * | _adc |
Reference to the ADC instance. More... | |
OperationMode | _operationMode |
Operation mode of this device. More... | |
const boards_config_t * | _boardConfig = nullptr |
CircularBuffer< adc_buffer_id_t > | _bufferQueue |
Queue of buffers we can use for processing. More... | |
cs_power_samples_header_t | _lastSoftfuse |
adc_sample_value_t | _lastSoftfuseSamples [AdcBuffer::getChannelLength()] = {0} |
CircularBuffer< switch_state_t > | _switchHist |
cs_power_samples_header_t | _lastSwitchSamplesHeader |
adc_sample_value_t | _lastSwitchSamples [numSwitchSamplesBuffers *AdcBuffer::getChannelLength()] = {0} |
uint16_t | _avgZeroCurrentDiscount |
Power zero from settings. More... | |
uint16_t | _avgZeroVoltageDiscount |
uint16_t | _avgPowerDiscount |
float | _slowAvgPowerDiscount |
float | _slowAvgPowerMilliWatt = 0.0f |
uint16_t | _slowAvgPowerCount |
const uint16_t | slowAvgPowerConvergedCount = 1000 |
float | _powerDiffThresholdPart |
float | _powerDiffThresholdMinMilliWatt |
float | _negativePowerThresholdMilliWatt |
int32_t | _boardPowerZero |
int32_t | _avgZeroVoltage |
Measured power when there is no load for this board (mW). More... | |
int32_t | _avgZeroCurrent |
Used for storing and calculating the average zero voltage value (times 1024). More... | |
bool | _recalibrateZeroVoltage |
Used for storing and calculating the average zero current value (times 1024). More... | |
bool | _recalibrateZeroCurrent |
Whether or not the zero voltage value should be recalculated. More... | |
uint16_t | _zeroVoltageCount |
Whether or not the zero current value should be recalculated. More... | |
uint16_t | _zeroCurrentCount |
Number of times the zero voltage has been calculated. More... | |
int32_t | _avgPowerMilliWatt |
Number of times the zero current has been calculated. More... | |
int32_t | _avgCurrentRmsMilliAmp |
Used to send out the average power (in mW). More... | |
int32_t | _avgVoltageRmsMilliVolt |
Used for storing the average rms current (in mA). More... | |
PowerVector * | _inputSamples |
Used for storing the average rms voltage (in mV). More... | |
PowerVector * | _outputSamples |
Used for storing the samples to be filtered. More... | |
MedianFilter * | _filterParams |
Used for storing the filtered samples. More... | |
CircularBuffer< int32_t > * | _powerMilliWattHist |
Stores the parameters for the moving median filter. More... | |
CircularBuffer< int32_t > * | _currentRmsMilliAmpHist |
Used to store a history of the power. More... | |
CircularBuffer< int32_t > * | _filteredCurrentRmsHistMA |
Used to store a history of the current_rms. More... | |
CircularBuffer< int32_t > * | _voltageRmsMilliVoltHist |
Used to store a history of the filtered current_rms. More... | |
int32_t | _histCopy [POWER_SAMPLING_RMS_WINDOW_SIZE] |
Used to store a history of the voltage_rms. More... | |
uint16_t | _consecutiveDimmerOvercurrent = 0 |
Used to copy a history to (so it can be used to calculate the median) More... | |
uint16_t | _consecutiveOvercurrent = 0 |
_currentMilliAmpThresholdDimmer | |
Current threshold from settings. More... | |
int64_t | _energyUsedmicroJoule = 0 |
Current threshold when using dimmer from settings. More... | |
switch_state_t | _lastSwitchState |
Energy used in micro joule. More... | |
uint32_t | _lastSwitchOffTicks |
Stores the last seen switch state. More... | |
bool | _lastSwitchOffTicksValid = false |
RTC ticks when the switch was last turned off. More... | |
bool | _dimmerFailureDetectionStarted = false |
Keep up whether the last switch off time is valid. More... | |
uint32_t | _calibratePowerZeroCountDown = 4000 / TICK_INTERVAL_MS |
Keep up whether the IGBT failure detection has started yet. More... | |
struct { | |
adc_channel_config_t config | |
uint8_t pinIndex = 0 | |
uint8_t pinCount = 0 | |
} | _adcConfig [2] |
union { | |
struct { | |
bool power: 1 | |
bool current: 1 | |
bool voltage: 1 | |
bool filteredCurrent: 1 | |
} flags | |
uint32_t asInt | |
} | _logsEnabled |
adc_buffer_seq_nr_t | _lastBufSeqNr = 0 |
adc_buffer_id_t | _lastBufIndex = 0 |
adc_buffer_id_t | _lastFilteredBufIndex = 0 |
cs_adc_restarts_t | _adcRestarts |
cs_adc_channel_swaps_t | _adcChannelSwaps |
uint32_t | _bufSkipCount = 0 |
Count number of buffers that have been skipped for processing. More... | |
Static Private Attributes | |
static const uint8_t | numFilteredBuffersForProcessing = 4 |
static const uint8_t | numUnfilteredBuffers = 1 |
static const uint8_t | switchHistSize = 3 |
static const uint8_t | numSwitchSamplesBuffers = 6 |
Sample power.
|
private |
|
private |
|
private |
Calculate the value of the zero line of the current samples.
|
private |
Calculate the energy used.
|
private |
Calculate the average power usage.
|
private |
|
private |
Calculate the value of the zero line of the voltage samples (the offset).
|
private |
Determines measured power usage with no load.
When successful, sets the value in state.
Careful: make sure this doesn't interfere with dimmer on failure detection.
|
private |
|
private |
Check if the current goes above a threshold (for long enough).
Emits an event when a softfuse triggers. Stores the current buffer of the last buffer that's above threshold, before the softfuse triggered.
[in] | currentRmsMilliAmp | RMS current in mA of the last AC period. |
[in] | currentRmsMilliAmpFiltered | Filtered (averaged or so) RMS current in mA. |
[in] | voltageRmsMilliVolt | RMS voltage in mV of the last AC period. |
[in] | power | Struct that holds the buffers. |
|
private |
|
private |
|
private |
void PowerSampling::enableZeroCrossingInterrupt | ( | ps_zero_crossing_cb_t | callback | ) |
Enable zero crossing detection on given channel, generating interrupts.
[in] | callback | Function to be called on a zero crossing event. This function will run at interrupt level! |
|
private |
Filter the samples.
|
inlinestatic |
Gets a static singleton (no dynamic memory allocation)
uint32_t PowerSampling::getSkippedBufCount | ( | ) |
Get the number of bufs that have been recently skipped.
Is reset every second.
|
virtual |
handle (crownstone) events
Implements EventListener.
|
private |
void PowerSampling::init | ( | const boards_config_t * | boardConfig | ) |
Init the class.
The board config must remain in memory.
|
private |
Initialize the moving averages.
|
private |
Load energy used from IPC ram.
|
private |
Whether the given sequence nr follows directly after the previous sequence nr.
This can change at any moment (set in interrupt).
|
private |
Whether the given buffer is valid.
This can change at any moment (set in interrupt).
|
private |
Checks if voltage and current index are swapped.
Checks if previous voltage samples look more like this buffer voltage samples or current samples. Assumes previous buffer is valid, and of same size as this buffer.
void PowerSampling::powerSampleAdcDone | ( | adc_buffer_id_t | bufIndex | ) |
Called when the sample burst is finished.
Calculates the power usage, updates the state. Sends the samples if the central is subscribed for that.
void PowerSampling::powerSampleFirstStart | ( | ) |
Initializes and starts the ADC, also starts interval timer.
|
private |
|
private |
Remove all buffers from queue that are older than the newest invalid buffer.
What remains is a queue of consecutive valid buffers.
void PowerSampling::sampleCurrentDone | ( | uint8_t | type | ) |
Fill up the current curve and send it out over bluetooth @type specifies over which characteristic the current curve should be sent.
|
private |
void PowerSampling::startSampling | ( | ) |
Starts a new power sample burst.
Called at a low interval.
|
private |
Store energy used to IPC ram.
|
private |
Voltage zero from settings.
|
private |
Voltage multiplier from settings.
|
private |
Current zero from settings.
|
private |
|
private |
Current multiplier from settings.
|
private |
|
private |
struct { ... } PowerSampling::_adcConfig[2] |
|
private |
|
private |
Used to send out the average power (in mW).
|
private |
|
private |
Number of times the zero current has been calculated.
|
private |
Used for storing the average rms current (in mA).
|
private |
Used for storing and calculating the average zero voltage value (times 1024).
|
private |
Power zero from settings.
|
private |
Measured power when there is no load for this board (mW).
|
private |
|
private |
|
private |
|
private |
Queue of buffers we can use for processing.
If queue size == 1:
|
private |
Count number of buffers that have been skipped for processing.
|
private |
Keep up whether the IGBT failure detection has started yet.
|
private |
Used to copy a history to (so it can be used to calculate the median)
|
private |
|
private |
Current threshold from settings.
|
private |
Used to store a history of the power.
|
private |
Keep up whether the last switch off time is valid.
|
private |
Current threshold when using dimmer from settings.
|
private |
Used to store a history of the current_rms.
|
private |
Used for storing the filtered samples.
|
private |
Used to store a history of the voltage_rms.
|
private |
Used for storing the average rms voltage (in mV).
|
private |
Variable to keep up whether power sampling is initialized.
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
Stores the last seen switch state.
|
private |
RTC ticks when the switch was last turned off.
|
private |
|
private |
|
private |
Energy used in micro joule.
union { ... } PowerSampling::_logsEnabled |
|
private |
|
private |
Operation mode of this device.
|
private |
Used for storing the samples to be filtered.
|
private |
|
private |
|
private |
Stores the parameters for the moving median filter.
|
private |
Whether or not the zero voltage value should be recalculated.
|
private |
Used for storing and calculating the average zero current value (times 1024).
|
private |
|
private |
|
private |
|
private |
|
private |
Used to store a history of the filtered current_rms.
|
private |
Number of times the zero voltage has been calculated.
|
private |
Whether or not the zero current value should be recalculated.
uint32_t PowerSampling::asInt |
adc_channel_config_t PowerSampling::config |
bool PowerSampling::current |
bool PowerSampling::filteredCurrent |
struct { ... } PowerSampling::flags |
|
staticprivate |
|
staticprivate |
|
staticprivate |
uint8_t PowerSampling::pinCount = 0 |
uint8_t PowerSampling::pinIndex = 0 |
bool PowerSampling::power |
|
private |
|
staticprivate |
bool PowerSampling::voltage |