This commit is contained in:
Anton Viktorov 2024-03-22 20:06:36 +01:00
parent e4e4a7aeef
commit 4994c4b39c
2 changed files with 27 additions and 21 deletions

View file

@ -241,12 +241,12 @@ void AS7343Component::calculate_and_publish() {
float irradiance; float irradiance;
float irradiance_photopic; float irradiance_photopic;
float lux; float lux;
float ppfd; float ppfd, par;
float cct, duv, lux2; float cct, duv, lux2;
this->calculate_irradiance(irradiance, irradiance_photopic, lux); this->calculate_irradiance(irradiance, irradiance_photopic, lux);
this->calculate_ppfd(ppfd); this->calculate_ppfd(ppfd, par);
this->calculate_color_params(cct, duv, lux2); this->calculate_color_params(cct, duv, lux2);
// ESP_LOGD(TAG, "BEFORE GLASS ATTENUATION"); // ESP_LOGD(TAG, "BEFORE GLASS ATTENUATION");
@ -260,10 +260,12 @@ void AS7343Component::calculate_and_publish() {
lux *= this->glass_attenuation_factor_; lux *= this->glass_attenuation_factor_;
lux2 *= this->glass_attenuation_factor_; lux2 *= this->glass_attenuation_factor_;
ppfd *= this->glass_attenuation_factor_; ppfd *= this->glass_attenuation_factor_;
par *= this->glass_attenuation_factor_;
ESP_LOGD(TAG, "AFTER GLASS ATTENUATION"); ESP_LOGD(TAG, "AFTER GLASS ATTENUATION");
ESP_LOGD(TAG, " ,Irradiance , %f, W/m²", irradiance); ESP_LOGD(TAG, " ,Irradiance , %f, W/m²", irradiance);
ESP_LOGD(TAG, " ,Irradiance(photopic), %f, W/m²", irradiance_photopic); ESP_LOGD(TAG, " ,Irradiance(photopic), %f, W/m²", irradiance_photopic);
ESP_LOGD(TAG, " ,PAR , %f, W/m²", par);
ESP_LOGD(TAG, " ,PPFD , %f, µmol/s⋅m²", ppfd); ESP_LOGD(TAG, " ,PPFD , %f, µmol/s⋅m²", ppfd);
ESP_LOGD(TAG, " ,Lux(solar coeff) , %f, lx", lux); ESP_LOGD(TAG, " ,Lux(solar coeff) , %f, lx", lux);
ESP_LOGD(TAG, " ,Lux(XYZ) , %f, lx", lux2); ESP_LOGD(TAG, " ,Lux(XYZ) , %f, lx", lux2);
@ -404,7 +406,7 @@ void AS7343Component::calculate_basic_counts() {
} }
} }
void AS7343Component::calculate_ppfd(float &ppfd) { void AS7343Component::calculate_ppfd(float &ppfd, float &par) {
/* /*
Given the spectral irradiance , defined as the radiant flux Given the spectral irradiance , defined as the radiant flux
per unit wavelength and unit area, the photon flux density per unit wavelength and unit area, the photon flux density
@ -430,6 +432,7 @@ void AS7343Component::calculate_ppfd(float &ppfd) {
*/ */
ppfd = 0; ppfd = 0;
par = 0;
// assume we integrate using rectangles - mid point is channel wavelength, width is channel width in nm // assume we integrate using rectangles - mid point is channel wavelength, width is channel width in nm
for (uint8_t i = 0; i < AS7343_NUM_CHANNELS; i++) { for (uint8_t i = 0; i < AS7343_NUM_CHANNELS; i++) {
@ -443,15 +446,17 @@ void AS7343Component::calculate_ppfd(float &ppfd) {
// hack? // hack?
irradiance_in_w_per_m2 *= 10; irradiance_in_w_per_m2 *= 10;
// float photon_flux = irradiance_in_w_per_m2 * CHANNEL_NM[i] * 0.836e-2; // float photon_flux = irradiance_in_w_per_m2 * CHANNEL_NM[i] * 0.836e-2;
// // ESP_LOGD(TAG, "Photon flux (%.0f) = %.2f", CHANNEL_NM[i], photon_flux); // // ESP_LOGD(TAG, "Photon flux (%.0f) = %.2f", CHANNEL_NM[i], photon_flux);
// // assume channels cover whole range // // assume channels cover whole range
// ppfd += photon_flux * CHANNEL_NM_WIDTH[i] / 1e9f; // nm to meters // ppfd += photon_flux * CHANNEL_NM_WIDTH[i] / 1e9f; // nm to meters
ppfd += irradiance_in_w_per_m2; // k = λ / h * c * Na
// 1 W/m2 = λ / 119.565
// https://www.gigahertz-optik.com/en-us/service-and-support/knowledge-base/measurement-of-par/
ppfd += irradiance_in_w_per_m2 * CHANNEL_NM[i] / 119.565;
par += irradiance_in_w_per_m2;
} }
// https://www.controlledenvironments.org/wp-content/uploads/sites/6/2017/06/Ch01.pdf
ppfd *= 4.6; // good approximation 1 W/m2 ≈ 4.6 μmole.m2/s.
} }
void AS7343Component::calculate_irradiance(float &irradiance_in_w_per_m2, float &irradiance_in_w_per_m2_photopic, void AS7343Component::calculate_irradiance(float &irradiance_in_w_per_m2, float &irradiance_in_w_per_m2_photopic,
@ -468,10 +473,9 @@ void AS7343Component::calculate_irradiance(float &irradiance_in_w_per_m2, float
// //
irr_band = this->readings_.basic_counts[i] * CHANNEL_IRRAD_MW_PER_BASIC_COUNT[i] / 1000; irr_band = this->readings_.basic_counts[i] * CHANNEL_IRRAD_MW_PER_BASIC_COUNT[i] / 1000;
//errata hack??? // errata hack???
irr_band *= 10; irr_band *= 10;
irradiance_in_w_per_m2 += irr_band; irradiance_in_w_per_m2 += irr_band;
// photo_band *= CHANNEL_ENERGY_CONTRIBUTION[i]; ?? ideas // photo_band *= CHANNEL_ENERGY_CONTRIBUTION[i]; ?? ideas
@ -520,7 +524,7 @@ void AS7343Component::calculate_color_params(float &cct, float &duv, float &lux)
// ESP_LOGD(TAG, "XYZ: %.2f, %.2f, %.2f", XYZ[0], XYZ[1], XYZ[2]); // ESP_LOGD(TAG, "XYZ: %.2f, %.2f, %.2f", XYZ[0], XYZ[1], XYZ[2]);
// ESP_LOGD(TAG, "x: %.4f, y: %.4f, z: %.4f", x, y, z); // ESP_LOGD(TAG, "x: %.4f, y: %.4f, z: %.4f", x, y, z);
// ESP_LOGD(TAG, "XYZ: CCT: %.2f, lux: %.2f", cct, lux); // ESP_LOGD(TAG, "XYZ: CCT: %.2f, lux: %.2f", cct, lux);
} }
bool AS7343Component::read_all_channels() { bool AS7343Component::read_all_channels() {
@ -571,9 +575,11 @@ bool AS7343Component::read_all_channels() {
bool AS7343Component::is_data_ready() { bool AS7343Component::is_data_ready() {
AS7343RegStatus2 status2{0}; AS7343RegStatus2 status2{0};
status2.raw = this->reg((uint8_t) AS7343Registers::STATUS2).get(); status2.raw = this->reg((uint8_t) AS7343Registers::STATUS2).get();
this->reg((uint8_t) AS7343Registers::STATUS2) = status2.raw;
if (status2.avalid) {
ESP_LOGD(TAG, "Status2 0x%02x, avalid %d, asat_digital %d, asat_analog %d", status2.raw, status2.avalid, ESP_LOGD(TAG, "Status2 0x%02x, avalid %d, asat_digital %d, asat_analog %d", status2.raw, status2.avalid,
status2.asat_digital, status2.asat_analog); status2.asat_digital, status2.asat_analog);
this->reg((uint8_t) AS7343Registers::STATUS2) = status2.raw; }
// return this->read_register_bit((uint8_t) AS7343Registers::STATUS2, 6); // return this->read_register_bit((uint8_t) AS7343Registers::STATUS2, 6);
return status2.avalid; return status2.avalid;

View file

@ -57,7 +57,7 @@ class AS7343Component : public PollingComponent, public i2c::I2CDevice {
bool read_all_channels(); bool read_all_channels();
void calculate_basic_counts(); void calculate_basic_counts();
void calculate_ppfd(float &ppfd); void calculate_ppfd(float &ppfd, float &par);
void calculate_irradiance(float &irradiance, float &irradiance_photopic, float &lux); void calculate_irradiance(float &irradiance, float &irradiance_photopic, float &lux);
void calculate_color_params(float &ct, float &duv, float &lux); void calculate_color_params(float &ct, float &duv, float &lux);