ESPHome  2024.8.3
atm90e32.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "atm90e32_reg.h"
8 #include "esphome/core/helpers.h"
10 
11 namespace esphome {
12 namespace atm90e32 {
13 
15  public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_HIGH,
16  spi::CLOCK_PHASE_TRAILING, spi::DATA_RATE_1MHZ> {
17  public:
18  static const uint8_t PHASEA = 0;
19  static const uint8_t PHASEB = 1;
20  static const uint8_t PHASEC = 2;
21  void loop() override;
22  void setup() override;
23  void dump_config() override;
24  float get_setup_priority() const override;
25  void update() override;
26  void set_voltage_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].voltage_sensor_ = obj; }
27  void set_current_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].current_sensor_ = obj; }
28  void set_power_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].power_sensor_ = obj; }
29  void set_reactive_power_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].reactive_power_sensor_ = obj; }
30  void set_apparent_power_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].apparent_power_sensor_ = obj; }
32  this->phase_[phase].forward_active_energy_sensor_ = obj;
33  }
35  this->phase_[phase].reverse_active_energy_sensor_ = obj;
36  }
37  void set_power_factor_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].power_factor_sensor_ = obj; }
38  void set_phase_angle_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].phase_angle_sensor_ = obj; }
40  this->phase_[phase].harmonic_active_power_sensor_ = obj;
41  }
42  void set_peak_current_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].peak_current_sensor_ = obj; }
43  void set_volt_gain(int phase, uint16_t gain) { this->phase_[phase].voltage_gain_ = gain; }
44  void set_ct_gain(int phase, uint16_t gain) { this->phase_[phase].ct_gain_ = gain; }
45  void set_freq_sensor(sensor::Sensor *freq_sensor) { freq_sensor_ = freq_sensor; }
46  void set_peak_current_signed(bool flag) { peak_current_signed_ = flag; }
47  void set_chip_temperature_sensor(sensor::Sensor *chip_temperature_sensor) {
48  chip_temperature_sensor_ = chip_temperature_sensor;
49  }
50  void set_line_freq(int freq) { line_freq_ = freq; }
51  void set_current_phases(int phases) { current_phases_ = phases; }
52  void set_pga_gain(uint16_t gain) { pga_gain_ = gain; }
56  uint16_t calibrate_voltage_offset_phase(uint8_t /*phase*/);
57  uint16_t calibrate_current_offset_phase(uint8_t /*phase*/);
59 
60  protected:
61  uint16_t read16_(uint16_t a_register);
62  int read32_(uint16_t addr_h, uint16_t addr_l);
63  void write16_(uint16_t a_register, uint16_t val);
64  float get_local_phase_voltage_(uint8_t /*phase*/);
65  float get_local_phase_current_(uint8_t /*phase*/);
66  float get_local_phase_active_power_(uint8_t /*phase*/);
67  float get_local_phase_reactive_power_(uint8_t /*phase*/);
68  float get_local_phase_power_factor_(uint8_t /*phase*/);
69  float get_local_phase_forward_active_energy_(uint8_t /*phase*/);
70  float get_local_phase_reverse_active_energy_(uint8_t /*phase*/);
71  float get_local_phase_angle_(uint8_t /*phase*/);
72  float get_local_phase_harmonic_active_power_(uint8_t /*phase*/);
73  float get_local_phase_peak_current_(uint8_t /*phase*/);
74  float get_phase_voltage_(uint8_t /*phase*/);
75  float get_phase_voltage_avg_(uint8_t /*phase*/);
76  float get_phase_current_(uint8_t /*phase*/);
77  float get_phase_current_avg_(uint8_t /*phase*/);
78  float get_phase_active_power_(uint8_t /*phase*/);
79  float get_phase_reactive_power_(uint8_t /*phase*/);
80  float get_phase_power_factor_(uint8_t /*phase*/);
81  float get_phase_forward_active_energy_(uint8_t /*phase*/);
82  float get_phase_reverse_active_energy_(uint8_t /*phase*/);
83  float get_phase_angle_(uint8_t /*phase*/);
84  float get_phase_harmonic_active_power_(uint8_t /*phase*/);
85  float get_phase_peak_current_(uint8_t /*phase*/);
86  float get_frequency_();
87  float get_chip_temperature_();
90  void restore_calibrations_();
91 
92  struct ATM90E32Phase {
93  uint16_t voltage_gain_{0};
94  uint16_t ct_gain_{0};
95  uint16_t voltage_offset_{0};
96  uint16_t current_offset_{0};
97  float voltage_{0};
98  float current_{0};
99  float active_power_{0};
100  float reactive_power_{0};
101  float power_factor_{0};
104  float phase_angle_{0};
106  float peak_current_{0};
120  } phase_[3];
121 
122  struct Calibration {
123  uint16_t voltage_offset_{0};
124  uint16_t current_offset_{0};
125  } offset_phase_[3];
126 
128 
131  uint16_t pga_gain_{0x15};
132  int line_freq_{60};
135  bool peak_current_signed_{false};
137 };
138 
139 } // namespace atm90e32
140 } // namespace esphome
float get_local_phase_active_power_(uint8_t)
Definition: atm90e32.cpp:339
void set_enable_offset_calibration(bool flag)
Definition: atm90e32.h:55
void set_chip_temperature_sensor(sensor::Sensor *chip_temperature_sensor)
Definition: atm90e32.h:47
void set_phase_angle_sensor(int phase, sensor::Sensor *obj)
Definition: atm90e32.h:38
float get_setup_priority() const override
Definition: atm90e32.cpp:290
void write16_(uint16_t a_register, uint16_t val)
Definition: atm90e32.cpp:325
void set_harmonic_active_power_sensor(int phase, sensor::Sensor *obj)
Definition: atm90e32.h:39
void set_current_sensor(int phase, sensor::Sensor *obj)
Definition: atm90e32.h:27
void set_reverse_active_energy_sensor(int phase, sensor::Sensor *obj)
Definition: atm90e32.h:34
int read32_(uint16_t addr_h, uint16_t addr_l)
Definition: atm90e32.cpp:312
struct esphome::atm90e32::ATM90E32Component::Calibration offset_phase_[3]
void set_peak_current_sensor(int phase, sensor::Sensor *obj)
Definition: atm90e32.h:42
mopeka_std_values val[4]
This class simplifies creating components that periodically check a state.
Definition: component.h:283
static const uint8_t PHASEC
Definition: atm90e32.h:20
static const uint8_t PHASEB
Definition: atm90e32.h:19
void set_apparent_power_sensor(int phase, sensor::Sensor *obj)
Definition: atm90e32.h:30
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:25
The SPIDevice is what components using the SPI will create.
Definition: spi.h:391
float get_phase_forward_active_energy_(uint8_t)
Definition: atm90e32.cpp:422
void set_ct_gain(int phase, uint16_t gain)
Definition: atm90e32.h:44
AlsGain gain
static const uint8_t PHASEA
Definition: atm90e32.h:18
struct esphome::atm90e32::ATM90E32Component::ATM90E32Phase phase_[3]
float get_local_phase_forward_active_energy_(uint8_t)
Definition: atm90e32.cpp:345
void set_peak_current_signed(bool flag)
Definition: atm90e32.h:46
uint16_t calibrate_voltage_offset_phase(uint8_t)
Definition: atm90e32.cpp:470
void set_voltage_sensor(int phase, sensor::Sensor *obj)
Definition: atm90e32.h:26
void set_power_factor_sensor(int phase, sensor::Sensor *obj)
Definition: atm90e32.h:37
void set_volt_gain(int phase, uint16_t gain)
Definition: atm90e32.h:43
float get_phase_reverse_active_energy_(uint8_t)
Definition: atm90e32.cpp:432
float get_local_phase_peak_current_(uint8_t)
Definition: atm90e32.cpp:359
void set_power_sensor(int phase, sensor::Sensor *obj)
Definition: atm90e32.h:28
float get_phase_harmonic_active_power_(uint8_t)
Definition: atm90e32.cpp:442
void set_current_phases(int phases)
Definition: atm90e32.h:51
uint16_t read16_(uint16_t a_register)
Definition: atm90e32.cpp:295
sensor::Sensor * chip_temperature_sensor_
Definition: atm90e32.h:130
float get_local_phase_reverse_active_energy_(uint8_t)
Definition: atm90e32.cpp:349
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void set_pga_gain(uint16_t gain)
Definition: atm90e32.h:52
uint16_t calibrate_current_offset_phase(uint8_t)
Definition: atm90e32.cpp:483
void set_reactive_power_sensor(int phase, sensor::Sensor *obj)
Definition: atm90e32.h:29
Base-class for all sensors.
Definition: sensor.h:57
void set_forward_active_energy_sensor(int phase, sensor::Sensor *obj)
Definition: atm90e32.h:31
float get_local_phase_harmonic_active_power_(uint8_t)
Definition: atm90e32.cpp:355
float get_local_phase_power_factor_(uint8_t)
Definition: atm90e32.cpp:343
void set_freq_sensor(sensor::Sensor *freq_sensor)
Definition: atm90e32.h:45
void set_publish_interval_flag_(bool flag)
Definition: atm90e32.h:89
float get_local_phase_reactive_power_(uint8_t)
Definition: atm90e32.cpp:341