193 struct __attribute__((packed)) {
204 struct __attribute__((packed)) {
313 int32_t currentRmsMilliAmp,
314 int32_t currentRmsMilliAmpFiltered,
315 int32_t voltageRmsMilliVolt,
std::vector< power_elem_t > PowerVector
Definition: Element.h:9
Analog-Digital conversion.
Definition: cs_ADC.h:84
static constexpr adc_sample_value_id_t getChannelLength()
Get number of samples for each channel in a buffer.
Definition: cs_AdcBuffer.h:109
Circular Buffer implementation.
Definition: cs_CircularBuffer.h:27
Event listener.
Definition: cs_EventListener.h:17
Sample power.
Definition: cs_PowerSampling.h:24
uint8_t pinIndex
Definition: cs_PowerSampling.h:197
uint16_t _consecutiveOvercurrent
Definition: cs_PowerSampling.h:178
struct PowerSampling::@12::@13 flags
adc_buffer_id_t _lastFilteredBufIndex
Definition: cs_PowerSampling.h:215
void filter(adc_buffer_id_t bufIndexIn, adc_buffer_id_t bufIndexOut, adc_channel_id_t channel_id)
Filter the samples.
bool _dimmerFailureDetectionStarted
Keep up whether the last switch off time is valid.
Definition: cs_PowerSampling.h:189
CircularBuffer< int32_t > * _filteredCurrentRmsHistMA
Used to store a history of the current_rms.
Definition: cs_PowerSampling.h:173
uint16_t _slowAvgPowerCount
Definition: cs_PowerSampling.h:148
void enableDifferentialModeVoltage(bool enable)
bool filteredCurrent
Definition: cs_PowerSampling.h:208
bool _lastSwitchOffTicksValid
RTC ticks when the switch was last turned off.
Definition: cs_PowerSampling.h:188
uint32_t _bufSkipCount
Count number of buffers that have been skipped for processing.
Definition: cs_PowerSampling.h:221
void powerSampleAdcDone(adc_buffer_id_t bufIndex)
Called when the sample burst is finished.
void calibratePowerZero(int32_t powerMilliWatt)
Determines measured power usage with no load.
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.
TYPIFY(CONFIG_SOFT_FUSE_CURRENT_THRESHOLD) _currentMilliAmpThreshold
bool current
Definition: cs_PowerSampling.h:206
TYPIFY(CONFIG_CURRENT_MULTIPLIER) _currentMultiplier
Voltage multiplier from settings.
float _powerDiffThresholdMinMilliWatt
Definition: cs_PowerSampling.h:151
switch_state_t _lastSwitchState
Energy used in micro joule.
Definition: cs_PowerSampling.h:186
CircularBuffer< int32_t > * _voltageRmsMilliVoltHist
Used to store a history of the filtered current_rms.
Definition: cs_PowerSampling.h:174
int32_t _avgPowerMilliWatt
Number of times the zero current has been calculated.
Definition: cs_PowerSampling.h:163
bool power
Definition: cs_PowerSampling.h:205
static const uint8_t numUnfilteredBuffers
Definition: cs_PowerSampling.h:98
void changeRange(uint8_t channel, int32_t amount)
int32_t _avgZeroCurrent
Used for storing and calculating the average zero voltage value (times 1024).
Definition: cs_PowerSampling.h:156
static const uint8_t numFilteredBuffersForProcessing
Definition: cs_PowerSampling.h:95
int32_t _avgZeroVoltage
Measured power when there is no load for this board (mW).
Definition: cs_PowerSampling.h:155
PowerVector * _inputSamples
Used for storing the average rms voltage (in mV).
Definition: cs_PowerSampling.h:167
ADC * _adc
Reference to the ADC instance.
Definition: cs_PowerSampling.h:107
uint16_t _consecutiveDimmerOvercurrent
Used to copy a history to (so it can be used to calculate the median)
Definition: cs_PowerSampling.h:177
uint32_t getSkippedBufCount()
Get the number of bufs that have been recently skipped.
void enableSwitchcraft(bool enable)
void removeInvalidBufs()
Remove all buffers from queue that are older than the newest invalid buffer.
bool _isInitialized
Variable to keep up whether power sampling is initialized.
Definition: cs_PowerSampling.h:104
CircularBuffer< switch_state_t > _switchHist
Definition: cs_PowerSampling.h:129
static const uint8_t switchHistSize
Definition: cs_PowerSampling.h:101
uint32_t _lastSwitchOffTicks
Stores the last seen switch state.
Definition: cs_PowerSampling.h:187
int32_t _boardPowerZero
Definition: cs_PowerSampling.h:154
cs_adc_channel_swaps_t _adcChannelSwaps
Definition: cs_PowerSampling.h:218
adc_buffer_id_t _lastBufIndex
Definition: cs_PowerSampling.h:214
float _slowAvgPowerMilliWatt
Definition: cs_PowerSampling.h:147
void calculateSlowAveragePower(float powerMilliWatt, float fastAvgPowerMilliWatt)
void initAverages()
Initialize the moving averages.
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).
TYPIFY(CONFIG_VOLTAGE_ADC_ZERO) _voltageZero
Current multiplier from settings.
CircularBuffer< int32_t > * _currentRmsMilliAmpHist
Used to store a history of the power.
Definition: cs_PowerSampling.h:172
void powerSampleFirstStart()
Initializes and starts the ADC, also starts interval timer.
adc_sample_value_t _lastSoftfuseSamples[AdcBuffer::getChannelLength()]
Definition: cs_PowerSampling.h:127
bool _recalibrateZeroVoltage
Used for storing and calculating the average zero current value (times 1024).
Definition: cs_PowerSampling.h:157
MedianFilter * _filterParams
Used for storing the filtered samples.
Definition: cs_PowerSampling.h:169
float _powerDiffThresholdPart
Definition: cs_PowerSampling.h:150
int64_t _energyUsedmicroJoule
Current threshold when using dimmer from settings.
Definition: cs_PowerSampling.h:184
OperationMode _operationMode
Operation mode of this device.
Definition: cs_PowerSampling.h:110
void enableZeroCrossingInterrupt(ps_zero_crossing_cb_t callback)
Enable zero crossing detection on given channel, generating interrupts.
void calculateEnergy()
Calculate the energy used.
cs_power_samples_header_t _lastSoftfuse
Definition: cs_PowerSampling.h:126
bool isValidBuf(adc_buffer_id_t bufIndex)
Whether the given buffer is valid.
void printBuf(adc_buffer_id_t bufIndex)
static const uint8_t numSwitchSamplesBuffers
Definition: cs_PowerSampling.h:132
union PowerSampling::@12 _logsEnabled
void calculateVoltageZero(adc_buffer_id_t bufIndex)
Calculate the value of the zero line of the voltage samples (the offset).
void init(const boards_config_t *boardConfig)
Init the class.
void enableDifferentialModeCurrent(bool enable)
float _negativePowerThresholdMilliWatt
Definition: cs_PowerSampling.h:152
uint8_t pinCount
Definition: cs_PowerSampling.h:200
void selectNextPin(adc_channel_id_t channel)
void applyAdcConfig(adc_channel_id_t channelIndex)
CircularBuffer< adc_buffer_id_t > _bufferQueue
Queue of buffers we can use for processing.
Definition: cs_PowerSampling.h:124
void initEnergyUsed()
Load energy used from IPC ram.
const boards_config_t * _boardConfig
Definition: cs_PowerSampling.h:112
struct PowerSampling::@11 _adcConfig[2]
bool calculatePower(adc_buffer_id_t bufIndex)
Calculate the average power usage.
const uint16_t slowAvgPowerConvergedCount
Definition: cs_PowerSampling.h:149
float _slowAvgPowerDiscount
Definition: cs_PowerSampling.h:146
adc_channel_config_t config
Definition: cs_PowerSampling.h:194
bool _recalibrateZeroCurrent
Whether or not the zero voltage value should be recalculated.
Definition: cs_PowerSampling.h:158
adc_buffer_seq_nr_t _lastBufSeqNr
Definition: cs_PowerSampling.h:213
_currentMilliAmpThresholdDimmer
Current threshold from settings.
Definition: cs_PowerSampling.h:182
uint16_t _avgZeroCurrentDiscount
Power zero from settings.
Definition: cs_PowerSampling.h:141
bool isVoltageAndCurrentSwapped(adc_buffer_id_t bufIndex, adc_buffer_id_t prevBufIndex)
Checks if voltage and current index are swapped.
cs_power_samples_header_t _lastSwitchSamplesHeader
Definition: cs_PowerSampling.h:130
uint16_t _avgZeroVoltageDiscount
Definition: cs_PowerSampling.h:142
void handleEvent(event_t &event)
handle (crownstone) events
PowerVector * _outputSamples
Used for storing the samples to be filtered.
Definition: cs_PowerSampling.h:168
uint16_t _avgPowerDiscount
Definition: cs_PowerSampling.h:143
CircularBuffer< int32_t > * _powerMilliWattHist
Stores the parameters for the moving median filter.
Definition: cs_PowerSampling.h:171
void handleGetPowerSamples(PowerSamplesType type, uint8_t index, cs_result_t &result)
int32_t _avgVoltageRmsMilliVolt
Used for storing the average rms current (in mA).
Definition: cs_PowerSampling.h:165
TYPIFY(CONFIG_POWER_ZERO) _powerZero
Current zero from settings.
static PowerSampling & getInstance()
Gets a static singleton (no dynamic memory allocation)
Definition: cs_PowerSampling.h:27
void startSampling()
Starts a new power sample burst.
int32_t _avgCurrentRmsMilliAmp
Used to send out the average power (in mW).
Definition: cs_PowerSampling.h:164
cs_adc_restarts_t _adcRestarts
Definition: cs_PowerSampling.h:217
uint32_t asInt
Definition: cs_PowerSampling.h:210
void calculateCurrentZero(adc_buffer_id_t bufIndex)
Calculate the value of the zero line of the current samples.
TYPIFY(CONFIG_CURRENT_ADC_ZERO) _currentZero
Voltage zero from settings.
bool voltage
Definition: cs_PowerSampling.h:207
uint32_t _calibratePowerZeroCountDown
Keep up whether the IGBT failure detection has started yet.
Definition: cs_PowerSampling.h:190
int32_t _histCopy[POWER_SAMPLING_RMS_WINDOW_SIZE]
Used to store a history of the voltage_rms.
Definition: cs_PowerSampling.h:175
void sampleCurrentDone(uint8_t type)
Fill up the current curve and send it out over bluetooth @type specifies over which characteristic th...
adc_sample_value_t _lastSwitchSamples[numSwitchSamplesBuffers *AdcBuffer::getChannelLength()]
Definition: cs_PowerSampling.h:133
TYPIFY(CONFIG_VOLTAGE_MULTIPLIER) _voltageMultiplier
uint16_t _zeroCurrentCount
Number of times the zero voltage has been calculated.
Definition: cs_PowerSampling.h:161
uint16_t _zeroVoltageCount
Whether or not the zero current value should be recalculated.
Definition: cs_PowerSampling.h:160
void storeEnergyUsed()
Store energy used to IPC ram.
Author: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: Oct 9,...
Definition: cs_Event.h:26
#define TICK_INTERVAL_MS
Interval in milliseconds at which tick events are dispatched.
Definition: cs_Config.h:277
#define POWER_SAMPLING_RMS_WINDOW_SIZE
Definition: cs_Config.h:174
PowerSamplesType
Definition: cs_Packets.h:455
void(* ps_zero_crossing_cb_t)()
Authors: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: May 19,...
Definition: cs_PowerSampling.h:19
uint8_t adc_buffer_seq_nr_t
Definition: cs_Typedefs.h:35
uint8_t adc_buffer_id_t
Definition: cs_Typedefs.h:30
int16_t adc_sample_value_t
Definition: cs_Typedefs.h:34
uint8_t adc_channel_id_t
Definition: cs_Typedefs.h:31
OperationMode
Definition: cs_Types.h:440
#define TYPIFY(NAME)
Definition: cs_Types.h:476
@ CONFIG_SOFT_FUSE_CURRENT_THRESHOLD_DIMMER
@ CONFIG_CURRENT_ADC_ZERO
@ CONFIG_VOLTAGE_ADC_ZERO
@ CONFIG_VOLTAGE_MULTIPLIER
@ CONFIG_CURRENT_MULTIPLIER
@ CONFIG_SOFT_FUSE_CURRENT_THRESHOLD
Struct that defines the buffer received from the ADC sampler in scanning mode.
Definition: cs_PowerSampling.h:80
uint16_t voltageIndex
Definition: cs_PowerSampling.h:84
uint16_t currentIndex
Definition: cs_PowerSampling.h:85
uint32_t sampleIntervalUs
Definition: cs_PowerSampling.h:86
uint16_t bufSize
Definition: cs_PowerSampling.h:82
uint16_t numChannels
Definition: cs_PowerSampling.h:83
adc_sample_value_t * buf
Definition: cs_PowerSampling.h:81
uint32_t acPeriodUs
Definition: cs_PowerSampling.h:87
Struct to configure an ADC channel.
Definition: cs_PacketsInternal.h:293
Board configuration.
Definition: cs_Boards.h:169
Definition: cs_Packets.h:450
Definition: cs_Packets.h:445
Definition: cs_PacketsInternal.h:46
Switch state: combination of relay and dimmer state.
Definition: cs_Packets.h:219