ESPHome  2024.4.1
atm90e32.h
Go to the documentation of this file.
1 #pragma once
2 
6 #include "atm90e32_reg.h"
7 
8 namespace esphome {
9 namespace atm90e32 {
10 
12  public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_HIGH,
13  spi::CLOCK_PHASE_TRAILING, spi::DATA_RATE_1MHZ> {
14  public:
15  static const uint8_t PHASEA = 0;
16  static const uint8_t PHASEB = 1;
17  static const uint8_t PHASEC = 2;
18  void loop() override;
19  void setup() override;
20  void dump_config() override;
21  float get_setup_priority() const override;
22  void update() override;
23 
24  void set_voltage_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].voltage_sensor_ = obj; }
25  void set_current_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].current_sensor_ = obj; }
26  void set_power_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].power_sensor_ = obj; }
27  void set_reactive_power_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].reactive_power_sensor_ = obj; }
28  void set_apparent_power_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].apparent_power_sensor_ = obj; }
30  this->phase_[phase].forward_active_energy_sensor_ = obj;
31  }
33  this->phase_[phase].reverse_active_energy_sensor_ = obj;
34  }
35  void set_power_factor_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].power_factor_sensor_ = obj; }
36  void set_phase_angle_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].phase_angle_sensor_ = obj; }
38  this->phase_[phase].harmonic_active_power_sensor_ = obj;
39  }
40  void set_peak_current_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].peak_current_sensor_ = obj; }
41  void set_volt_gain(int phase, uint16_t gain) { this->phase_[phase].voltage_gain_ = gain; }
42  void set_ct_gain(int phase, uint16_t gain) { this->phase_[phase].ct_gain_ = gain; }
43  void set_freq_sensor(sensor::Sensor *freq_sensor) { freq_sensor_ = freq_sensor; }
44  void set_peak_current_signed(bool flag) { peak_current_signed_ = flag; }
45  void set_chip_temperature_sensor(sensor::Sensor *chip_temperature_sensor) {
46  chip_temperature_sensor_ = chip_temperature_sensor;
47  }
48  void set_line_freq(int freq) { line_freq_ = freq; }
49  void set_current_phases(int phases) { current_phases_ = phases; }
50  void set_pga_gain(uint16_t gain) { pga_gain_ = gain; }
51  uint16_t calibrate_voltage_offset_phase(uint8_t /*phase*/);
52  uint16_t calibrate_current_offset_phase(uint8_t /*phase*/);
53 
55 
56  protected:
57  uint16_t read16_(uint16_t a_register);
58  int read32_(uint16_t addr_h, uint16_t addr_l);
59  void write16_(uint16_t a_register, uint16_t val);
60  float get_local_phase_voltage_(uint8_t /*phase*/);
61  float get_local_phase_current_(uint8_t /*phase*/);
62  float get_local_phase_active_power_(uint8_t /*phase*/);
63  float get_local_phase_reactive_power_(uint8_t /*phase*/);
64  float get_local_phase_power_factor_(uint8_t /*phase*/);
65  float get_local_phase_forward_active_energy_(uint8_t /*phase*/);
66  float get_local_phase_reverse_active_energy_(uint8_t /*phase*/);
67  float get_local_phase_angle_(uint8_t /*phase*/);
68  float get_local_phase_harmonic_active_power_(uint8_t /*phase*/);
69  float get_local_phase_peak_current_(uint8_t /*phase*/);
70  float get_phase_voltage_(uint8_t /*phase*/);
71  float get_phase_voltage_avg_(uint8_t /*phase*/);
72  float get_phase_current_(uint8_t /*phase*/);
73  float get_phase_current_avg_(uint8_t /*phase*/);
74  float get_phase_active_power_(uint8_t /*phase*/);
75  float get_phase_reactive_power_(uint8_t /*phase*/);
76  float get_phase_power_factor_(uint8_t /*phase*/);
77  float get_phase_forward_active_energy_(uint8_t /*phase*/);
78  float get_phase_reverse_active_energy_(uint8_t /*phase*/);
79  float get_phase_angle_(uint8_t /*phase*/);
80  float get_phase_harmonic_active_power_(uint8_t /*phase*/);
81  float get_phase_peak_current_(uint8_t /*phase*/);
82  float get_frequency_();
83  float get_chip_temperature_();
86 
87  struct ATM90E32Phase {
88  uint16_t voltage_gain_{7305};
89  uint16_t ct_gain_{27961};
90  uint16_t voltage_offset_{0};
91  uint16_t current_offset_{0};
92  float voltage_{0};
93  float current_{0};
94  float active_power_{0};
95  float reactive_power_{0};
96  float power_factor_{0};
99  float phase_angle_{0};
101  float peak_current_{0};
115  } phase_[3];
116 
119  uint16_t pga_gain_{0x15};
120  int line_freq_{60};
123  bool peak_current_signed_{false};
124 };
125 
126 } // namespace atm90e32
127 } // namespace esphome
float get_local_phase_active_power_(uint8_t)
Definition: atm90e32.cpp:287
void set_chip_temperature_sensor(sensor::Sensor *chip_temperature_sensor)
Definition: atm90e32.h:45
void set_phase_angle_sensor(int phase, sensor::Sensor *obj)
Definition: atm90e32.h:36
float get_setup_priority() const override
Definition: atm90e32.cpp:238
void write16_(uint16_t a_register, uint16_t val)
Definition: atm90e32.cpp:273
void set_harmonic_active_power_sensor(int phase, sensor::Sensor *obj)
Definition: atm90e32.h:37
void set_current_sensor(int phase, sensor::Sensor *obj)
Definition: atm90e32.h:25
void set_reverse_active_energy_sensor(int phase, sensor::Sensor *obj)
Definition: atm90e32.h:32
int read32_(uint16_t addr_h, uint16_t addr_l)
Definition: atm90e32.cpp:260
void set_peak_current_sensor(int phase, sensor::Sensor *obj)
Definition: atm90e32.h:40
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:17
static const uint8_t PHASEB
Definition: atm90e32.h:16
void set_apparent_power_sensor(int phase, sensor::Sensor *obj)
Definition: atm90e32.h:28
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:25
The SPIDevice is what components using the SPI will create.
Definition: spi.h:408
float get_phase_forward_active_energy_(uint8_t)
Definition: atm90e32.cpp:370
void set_ct_gain(int phase, uint16_t gain)
Definition: atm90e32.h:42
static const uint8_t PHASEA
Definition: atm90e32.h:15
struct esphome::atm90e32::ATM90E32Component::ATM90E32Phase phase_[3]
float get_local_phase_forward_active_energy_(uint8_t)
Definition: atm90e32.cpp:293
void set_peak_current_signed(bool flag)
Definition: atm90e32.h:44
uint16_t calibrate_voltage_offset_phase(uint8_t)
Definition: atm90e32.cpp:418
void set_voltage_sensor(int phase, sensor::Sensor *obj)
Definition: atm90e32.h:24
void set_power_factor_sensor(int phase, sensor::Sensor *obj)
Definition: atm90e32.h:35
void set_volt_gain(int phase, uint16_t gain)
Definition: atm90e32.h:41
float get_phase_reverse_active_energy_(uint8_t)
Definition: atm90e32.cpp:380
float get_local_phase_peak_current_(uint8_t)
Definition: atm90e32.cpp:307
void set_power_sensor(int phase, sensor::Sensor *obj)
Definition: atm90e32.h:26
float get_phase_harmonic_active_power_(uint8_t)
Definition: atm90e32.cpp:390
void set_current_phases(int phases)
Definition: atm90e32.h:49
uint16_t read16_(uint16_t a_register)
Definition: atm90e32.cpp:243
sensor::Sensor * chip_temperature_sensor_
Definition: atm90e32.h:118
float get_local_phase_reverse_active_energy_(uint8_t)
Definition: atm90e32.cpp:297
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void set_pga_gain(uint16_t gain)
Definition: atm90e32.h:50
uint16_t calibrate_current_offset_phase(uint8_t)
Definition: atm90e32.cpp:431
void set_reactive_power_sensor(int phase, sensor::Sensor *obj)
Definition: atm90e32.h:27
Base-class for all sensors.
Definition: sensor.h:57
void set_forward_active_energy_sensor(int phase, sensor::Sensor *obj)
Definition: atm90e32.h:29
float get_local_phase_harmonic_active_power_(uint8_t)
Definition: atm90e32.cpp:303
float get_local_phase_power_factor_(uint8_t)
Definition: atm90e32.cpp:291
void set_freq_sensor(sensor::Sensor *freq_sensor)
Definition: atm90e32.h:43
void set_publish_interval_flag_(bool flag)
Definition: atm90e32.h:85
float get_local_phase_reactive_power_(uint8_t)
Definition: atm90e32.cpp:289