ESPHome 2025.5.0
Loading...
Searching...
No Matches
atm90e32.h
Go to the documentation of this file.
1#pragma once
2
3#include <unordered_map>
4#include "atm90e32_reg.h"
11
12namespace esphome {
13namespace atm90e32 {
14
16 public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_HIGH,
17 spi::CLOCK_PHASE_TRAILING, spi::DATA_RATE_1MHZ> {
18 public:
19 static const uint8_t PHASEA = 0;
20 static const uint8_t PHASEB = 1;
21 static const uint8_t PHASEC = 2;
22 const char *phase_labels[3] = {"A", "B", "C"};
23 // these registers are not sucessive, so we can't just do 'base + phase'
24 const uint16_t voltage_gain_registers[3] = {ATM90E32_REGISTER_UGAINA, ATM90E32_REGISTER_UGAINB,
25 ATM90E32_REGISTER_UGAINC};
26 const uint16_t current_gain_registers[3] = {ATM90E32_REGISTER_IGAINA, ATM90E32_REGISTER_IGAINB,
27 ATM90E32_REGISTER_IGAINC};
28 const uint16_t voltage_offset_registers[3] = {ATM90E32_REGISTER_UOFFSETA, ATM90E32_REGISTER_UOFFSETB,
29 ATM90E32_REGISTER_UOFFSETC};
30 const uint16_t current_offset_registers[3] = {ATM90E32_REGISTER_IOFFSETA, ATM90E32_REGISTER_IOFFSETB,
31 ATM90E32_REGISTER_IOFFSETC};
32 const uint16_t power_offset_registers[3] = {ATM90E32_REGISTER_POFFSETA, ATM90E32_REGISTER_POFFSETB,
33 ATM90E32_REGISTER_POFFSETC};
34 const uint16_t reactive_power_offset_registers[3] = {ATM90E32_REGISTER_QOFFSETA, ATM90E32_REGISTER_QOFFSETB,
35 ATM90E32_REGISTER_QOFFSETC};
36 const uint16_t over_voltage_flags[3] = {ATM90E32_STATUS_S0_OVPHASEAST, ATM90E32_STATUS_S0_OVPHASEBST,
37 ATM90E32_STATUS_S0_OVPHASECST};
38 const uint16_t voltage_sag_flags[3] = {ATM90E32_STATUS_S1_SAGPHASEAST, ATM90E32_STATUS_S1_SAGPHASEBST,
39 ATM90E32_STATUS_S1_SAGPHASECST};
40 const uint16_t phase_loss_flags[3] = {ATM90E32_STATUS_S1_PHASELOSSAST, ATM90E32_STATUS_S1_PHASELOSSBST,
41 ATM90E32_STATUS_S1_PHASELOSSCST};
42 void loop() override;
43 void setup() override;
44 void dump_config() override;
45 float get_setup_priority() const override;
46 void update() override;
47 void set_voltage_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].voltage_sensor_ = obj; }
48 void set_current_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].current_sensor_ = obj; }
49 void set_power_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].power_sensor_ = obj; }
50 void set_reactive_power_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].reactive_power_sensor_ = obj; }
51 void set_apparent_power_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].apparent_power_sensor_ = obj; }
53 this->phase_[phase].forward_active_energy_sensor_ = obj;
54 }
56 this->phase_[phase].reverse_active_energy_sensor_ = obj;
57 }
58 void set_power_factor_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].power_factor_sensor_ = obj; }
59 void set_phase_angle_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].phase_angle_sensor_ = obj; }
61 this->phase_[phase].harmonic_active_power_sensor_ = obj;
62 }
63 void set_peak_current_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].peak_current_sensor_ = obj; }
64 void set_volt_gain(int phase, uint16_t gain) { this->phase_[phase].voltage_gain_ = gain; }
65 void set_ct_gain(int phase, uint16_t gain) { this->phase_[phase].ct_gain_ = gain; }
66 void set_voltage_offset(uint8_t phase, int16_t offset) { this->offset_phase_[phase].voltage_offset_ = offset; }
67 void set_current_offset(uint8_t phase, int16_t offset) { this->offset_phase_[phase].current_offset_ = offset; }
68 void set_active_power_offset(uint8_t phase, int16_t offset) {
69 this->power_offset_phase_[phase].active_power_offset = offset;
70 }
71 void set_reactive_power_offset(uint8_t phase, int16_t offset) {
72 this->power_offset_phase_[phase].reactive_power_offset = offset;
73 }
74 void set_freq_sensor(sensor::Sensor *freq_sensor) { freq_sensor_ = freq_sensor; }
76 void set_chip_temperature_sensor(sensor::Sensor *chip_temperature_sensor) {
77 chip_temperature_sensor_ = chip_temperature_sensor;
78 }
79 void set_line_freq(int freq) { line_freq_ = freq; }
80 void set_current_phases(int phases) { current_phases_ = phases; }
81 void set_pga_gain(uint16_t gain) { pga_gain_ = gain; }
89 int16_t calibrate_offset(uint8_t phase, bool voltage);
90 int16_t calibrate_power_offset(uint8_t phase, bool reactive);
92#ifdef USE_NUMBER
93 void set_reference_voltage(uint8_t phase, number::Number *ref_voltage) { ref_voltages_[phase] = ref_voltage; }
94 void set_reference_current(uint8_t phase, number::Number *ref_current) { ref_currents_[phase] = ref_current; }
95#endif
96 float get_reference_voltage(uint8_t phase) {
97#ifdef USE_NUMBER
98 return (phase >= 0 && phase < 3 && ref_voltages_[phase]) ? ref_voltages_[phase]->state : 120.0; // Default voltage
99#else
100 return 120.0; // Default voltage
101#endif
102 }
103 float get_reference_current(uint8_t phase) {
104#ifdef USE_NUMBER
105 return (phase >= 0 && phase < 3 && ref_currents_[phase]) ? ref_currents_[phase]->state : 5.0f; // Default current
106#else
107 return 5.0f; // Default current
108#endif
109 }
110 bool using_saved_calibrations_ = false; // Track if stored calibrations are being used
111#ifdef USE_TEXT_SENSOR
112 void check_phase_status();
113 void check_freq_status();
114 void check_over_current();
116 this->phase_status_text_sensor_[phase] = sensor;
117 }
119#endif
120 uint16_t calculate_voltage_threshold(int line_freq, uint16_t ugain, float multiplier);
122
123 protected:
124#ifdef USE_NUMBER
125 number::Number *ref_voltages_[3]{nullptr, nullptr, nullptr};
126 number::Number *ref_currents_[3]{nullptr, nullptr, nullptr};
127#endif
128 uint16_t read16_(uint16_t a_register);
129 int read32_(uint16_t addr_h, uint16_t addr_l);
130 void write16_(uint16_t a_register, uint16_t val);
131 float get_local_phase_voltage_(uint8_t phase);
132 float get_local_phase_current_(uint8_t phase);
133 float get_local_phase_active_power_(uint8_t phase);
134 float get_local_phase_reactive_power_(uint8_t phase);
135 float get_local_phase_apparent_power_(uint8_t phase);
136 float get_local_phase_power_factor_(uint8_t phase);
137 float get_local_phase_forward_active_energy_(uint8_t phase);
138 float get_local_phase_reverse_active_energy_(uint8_t phase);
139 float get_local_phase_angle_(uint8_t phase);
140 float get_local_phase_harmonic_active_power_(uint8_t phase);
141 float get_local_phase_peak_current_(uint8_t phase);
142 float get_phase_voltage_(uint8_t phase);
143 float get_phase_voltage_avg_(uint8_t phase);
144 float get_phase_current_(uint8_t phase);
145 float get_phase_current_avg_(uint8_t phase);
146 float get_phase_active_power_(uint8_t phase);
147 float get_phase_reactive_power_(uint8_t phase);
148 float get_phase_apparent_power_(uint8_t phase);
149 float get_phase_power_factor_(uint8_t phase);
150 float get_phase_forward_active_energy_(uint8_t phase);
151 float get_phase_reverse_active_energy_(uint8_t phase);
152 float get_phase_angle_(uint8_t phase);
153 float get_phase_harmonic_active_power_(uint8_t phase);
154 float get_phase_peak_current_(uint8_t phase);
155 float get_frequency_();
156 float get_chip_temperature_();
163 void write_offsets_to_registers_(uint8_t phase, int16_t voltage_offset, int16_t current_offset);
164 void write_power_offsets_to_registers_(uint8_t phase, int16_t p_offset, int16_t q_offset);
166 bool verify_gain_writes_();
167 bool validate_spi_read_(uint16_t expected, const char *context = nullptr);
168
201
206
211
213 uint16_t voltage_gain{1};
214 uint16_t current_gain{1};
216
220
222#ifdef USE_TEXT_SENSOR
225#endif
227 uint16_t pga_gain_{0x15};
228 int line_freq_{60};
234};
235
236} // namespace atm90e32
237} // namespace esphome
This class simplifies creating components that periodically check a state.
Definition component.h:301
void set_chip_temperature_sensor(sensor::Sensor *chip_temperature_sensor)
Definition atm90e32.h:76
void set_freq_sensor(sensor::Sensor *freq_sensor)
Definition atm90e32.h:74
float get_local_phase_reactive_power_(uint8_t phase)
Definition atm90e32.cpp:310
float get_phase_forward_active_energy_(uint8_t phase)
Definition atm90e32.cpp:393
void set_freq_status_text_sensor(text_sensor::TextSensor *sensor)
Definition atm90e32.h:118
float get_phase_current_avg_(uint8_t phase)
Definition atm90e32.cpp:352
float get_local_phase_apparent_power_(uint8_t phase)
Definition atm90e32.cpp:312
void set_apparent_power_sensor(int phase, sensor::Sensor *obj)
Definition atm90e32.h:51
text_sensor::TextSensor * freq_status_text_sensor_
Definition atm90e32.h:224
ESPPreferenceObject power_offset_pref_
Definition atm90e32.h:218
const uint16_t voltage_gain_registers[3]
Definition atm90e32.h:24
float get_phase_voltage_avg_(uint8_t phase)
Definition atm90e32.cpp:338
void write_power_offsets_to_registers_(uint8_t phase, int16_t p_offset, int16_t q_offset)
Definition atm90e32.cpp:617
const uint16_t current_gain_registers[3]
Definition atm90e32.h:26
float get_reference_voltage(uint8_t phase)
Definition atm90e32.h:96
number::Number * ref_voltages_[3]
Definition atm90e32.h:125
void set_power_sensor(int phase, sensor::Sensor *obj)
Definition atm90e32.h:49
struct esphome::atm90e32::ATM90E32Component::GainCalibration gain_phase_[3]
void set_current_offset(uint8_t phase, int16_t offset)
Definition atm90e32.h:67
const uint16_t current_offset_registers[3]
Definition atm90e32.h:30
static const uint8_t PHASEB
Definition atm90e32.h:20
float get_phase_reverse_active_energy_(uint8_t phase)
Definition atm90e32.cpp:404
void set_reactive_power_offset(uint8_t phase, int16_t offset)
Definition atm90e32.h:71
float get_local_phase_harmonic_active_power_(uint8_t phase)
Definition atm90e32.cpp:326
float get_phase_angle_(uint8_t phase)
Definition atm90e32.cpp:420
float get_local_phase_current_(uint8_t phase)
Definition atm90e32.cpp:306
bool validate_spi_read_(uint16_t expected, const char *context=nullptr)
Definition atm90e32.cpp:859
void set_peak_current_sensor(int phase, sensor::Sensor *obj)
Definition atm90e32.h:63
const uint16_t reactive_power_offset_registers[3]
Definition atm90e32.h:34
const uint16_t over_voltage_flags[3]
Definition atm90e32.h:36
void set_reverse_active_energy_sensor(int phase, sensor::Sensor *obj)
Definition atm90e32.h:55
float get_phase_voltage_(uint8_t phase)
Definition atm90e32.cpp:332
void set_current_sensor(int phase, sensor::Sensor *obj)
Definition atm90e32.h:48
int16_t calibrate_offset(uint8_t phase, bool voltage)
Definition atm90e32.cpp:732
float get_local_phase_reverse_active_energy_(uint8_t phase)
Definition atm90e32.cpp:320
void set_harmonic_active_power_sensor(int phase, sensor::Sensor *obj)
Definition atm90e32.h:60
void set_current_phases(int phases)
Definition atm90e32.h:80
float get_local_phase_forward_active_energy_(uint8_t phase)
Definition atm90e32.cpp:316
struct esphome::atm90e32::ATM90E32Component::OffsetCalibration offset_phase_[3]
void set_active_power_offset(uint8_t phase, int16_t offset)
Definition atm90e32.h:68
uint16_t calculate_voltage_threshold(int line_freq, uint16_t ugain, float multiplier)
Definition atm90e32.cpp:845
float get_local_phase_power_factor_(uint8_t phase)
Definition atm90e32.cpp:314
float get_phase_reactive_power_(uint8_t phase)
Definition atm90e32.cpp:377
void set_enable_gain_calibration(bool flag)
Definition atm90e32.h:88
const uint16_t phase_loss_flags[3]
Definition atm90e32.h:40
float get_phase_apparent_power_(uint8_t phase)
Definition atm90e32.cpp:382
float get_local_phase_voltage_(uint8_t phase)
Definition atm90e32.cpp:304
void set_phase_status_text_sensor(uint8_t phase, text_sensor::TextSensor *sensor)
Definition atm90e32.h:115
void set_pga_gain(uint16_t gain)
Definition atm90e32.h:81
void set_reference_current(uint8_t phase, number::Number *ref_current)
Definition atm90e32.h:94
void write16_(uint16_t a_register, uint16_t val)
Definition atm90e32.cpp:295
ESPPreferenceObject gain_calibration_pref_
Definition atm90e32.h:219
void write_offsets_to_registers_(uint8_t phase, int16_t voltage_offset, int16_t current_offset)
Definition atm90e32.cpp:601
static const uint8_t PHASEA
Definition atm90e32.h:19
struct esphome::atm90e32::ATM90E32Component::PowerOffsetCalibration power_offset_phase_[3]
float get_reference_current(uint8_t phase)
Definition atm90e32.h:103
number::Number * ref_currents_[3]
Definition atm90e32.h:126
float get_phase_peak_current_(uint8_t phase)
Definition atm90e32.cpp:425
void set_voltage_offset(uint8_t phase, int16_t offset)
Definition atm90e32.h:66
float get_phase_harmonic_active_power_(uint8_t phase)
Definition atm90e32.cpp:415
const uint16_t voltage_sag_flags[3]
Definition atm90e32.h:38
float get_phase_active_power_(uint8_t phase)
Definition atm90e32.cpp:372
static const uint8_t PHASEC
Definition atm90e32.h:21
const uint16_t power_offset_registers[3]
Definition atm90e32.h:32
float get_setup_priority() const override
Definition atm90e32.cpp:260
uint16_t read16_(uint16_t a_register)
Definition atm90e32.cpp:265
int read32_(uint16_t addr_h, uint16_t addr_l)
Definition atm90e32.cpp:282
sensor::Sensor * chip_temperature_sensor_
Definition atm90e32.h:226
float get_phase_power_factor_(uint8_t phase)
Definition atm90e32.cpp:387
void set_voltage_sensor(int phase, sensor::Sensor *obj)
Definition atm90e32.h:47
float get_local_phase_angle_(uint8_t phase)
Definition atm90e32.cpp:324
const uint16_t voltage_offset_registers[3]
Definition atm90e32.h:28
void set_ct_gain(int phase, uint16_t gain)
Definition atm90e32.h:65
void set_phase_angle_sensor(int phase, sensor::Sensor *obj)
Definition atm90e32.h:59
void set_reference_voltage(uint8_t phase, number::Number *ref_voltage)
Definition atm90e32.h:93
void set_power_factor_sensor(int phase, sensor::Sensor *obj)
Definition atm90e32.h:58
void set_forward_active_energy_sensor(int phase, sensor::Sensor *obj)
Definition atm90e32.h:52
void set_volt_gain(int phase, uint16_t gain)
Definition atm90e32.h:64
text_sensor::TextSensor * phase_status_text_sensor_[3]
Definition atm90e32.h:223
float get_phase_current_(uint8_t phase)
Definition atm90e32.cpp:366
void set_peak_current_signed(bool flag)
Definition atm90e32.h:75
float get_local_phase_peak_current_(uint8_t phase)
Definition atm90e32.cpp:330
void set_publish_interval_flag_(bool flag)
Definition atm90e32.h:158
int16_t calibrate_power_offset(uint8_t phase, bool reactive)
Definition atm90e32.cpp:748
struct esphome::atm90e32::ATM90E32Component::ATM90E32Phase phase_[3]
void set_enable_offset_calibration(bool flag)
Definition atm90e32.h:87
ESPPreferenceObject offset_pref_
Definition atm90e32.h:217
void set_reactive_power_sensor(int phase, sensor::Sensor *obj)
Definition atm90e32.h:50
float get_local_phase_active_power_(uint8_t phase)
Definition atm90e32.cpp:308
Base-class for all numbers.
Definition number.h:39
Base-class for all sensors.
Definition sensor.h:57
The SPIDevice is what components using the SPI will create.
Definition spi.h:421
AlsGain501 gain
mopeka_std_values val[4]
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:27