From a9b4f515d5fd26ffb06c3be35a40dc415f48fd64 Mon Sep 17 00:00:00 2001 From: bogd Date: Mon, 13 May 2024 15:36:19 +0200 Subject: [PATCH] added astro calculation core code --- CMakeLists.txt | 1 + include/CronTimers.h | 2 + src/AstroRelay.c | 168 +++++++++++++++++++++++++++++++++++++++++++ src/RestApiHandler.c | 12 ++++ 4 files changed, 183 insertions(+) create mode 100644 src/AstroRelay.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 6eb1987..5116aad 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,6 +31,7 @@ idf_component_register( src/RestApiHandler.c src/SysComm.c src/SysErr.c + src/AstroRelay.c ${lora_SRCS} ${gprs_SRCS} ${jreadwrite_SRCS} diff --git a/include/CronTimers.h b/include/CronTimers.h index 200e1f8..cffbeed 100644 --- a/include/CronTimers.h +++ b/include/CronTimers.h @@ -75,4 +75,6 @@ void TimeObtainHandler(struct timeval *tm); */ void custom_cron_execute(int obj, int act); +void SetSunTimes(uint32_t t); + #endif /* COMPONENTS_WEBGUIAPP_INCLUDE_CRONTIMERS_H_ */ diff --git a/src/AstroRelay.c b/src/AstroRelay.c new file mode 100644 index 0000000..8f4349e --- /dev/null +++ b/src/AstroRelay.c @@ -0,0 +1,168 @@ + /* Copyright 2024 Bogdan Pilyugin + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * File name: AstroRelay.c + * Project: WebguiappTemplate + * Created on: 2024-05-13 + * Author: bogd + * Description: + */ + +#include +#include +#include +#include "CronTimers.h" +#include "esp_log.h" + +#define C (3.14159265/180.0) +#define B (180.0/3.14159265) +#define zenith 90.8335f + +#define LAT 54.73 +#define LON 20.51 +#define SUN_ANG 0 + +static uint16_t srTime = 0; +static uint16_t ssTime = 0; + +static double Lat, Lon, Ang; + + + +static double GetSunEvent(uint8_t event, uint16_t day); + +uint16_t NumberDayFromUnix(uint32_t t) +{ + time_t clock; + struct tm * tp; + clock = t; + tp = gmtime(&clock); + return ((uint16_t) tp->tm_yday) + 1; +} + +void SetSunConditions(double lat, double lon, double ang) +{ + Lat = lat; + Lon = lon; + Ang = ang; +} + +void SetSunTimes(uint32_t t) +{ + if (1) + { + double tt; + tt = GetSunEvent(0, NumberDayFromUnix(t)); + if (tt > 0) + srTime = (uint16_t) (60.0 * tt); + else + srTime = 0xffff; //no valid sinrise time + tt = GetSunEvent(1, NumberDayFromUnix(t)); + if (tt > 0) + ssTime = (uint16_t) (60.0 * tt); + else + ssTime = 0xffff; //no valid sunset time + } + + ESP_LOGI("ASTRO", "Day number %d", NumberDayFromUnix(t)); + ESP_LOGI("ASTRO", "Sanrise %dh %dm", srTime/60 + 2, srTime - (srTime/60 * 60)); + ESP_LOGI("ASTRO", "Sanset %dh %dm", ssTime/60 + 2 , ssTime - (ssTime/60 * 60)); + +} + +uint16_t GetSunrise(void) +{ + return srTime; +} + +uint16_t GetSunset(void) +{ + return ssTime; +} + + +static double GetSunEvent(uint8_t event, uint16_t day) +{ + double lngHour, t, M, L, RA, sinDec, cosDec, cosH, H, T, UT; + double Lquadrant, RAquadrant; + double zen; + if (SUN_ANG == 0) + zen = zenith + (double) SUN_ANG; //sunrise/set + else + zen = 90.0 + (double) SUN_ANG; //twilight + lngHour = LON / 15; + if (event == 0) + t = day + ((6 - lngHour) / 24); + else + t = day + ((18 - lngHour) / 24); + + M = (0.9856 * t) - 3.289; + L = M + (1.916 * sin(M * C)) + (0.020 * sin(2 * M * C)) + 282.634; + if (L > 360) + { + L = L - 360; + } + else if (L < 0) + { + L = L + 360; + } + + RA = B * atan(0.91764 * tan(L * C)); + if (RA > 360) + { + RA = RA - 360; + } + else if (RA < 0) + { + RA = RA + 360; + } + + Lquadrant = (floor(L / 90)) * 90; + RAquadrant = (floor(RA / 90)) * 90; + RA = RA + (Lquadrant - RAquadrant); + RA = RA / 15; + sinDec = 0.39782 * sin(L * C); + cosDec = cos(asin(sinDec)); + cosH = (cos(zen * C) - (sinDec * sin(LAT * C))) / (cosDec * cos(LAT * C)); + + if (event == 0) + { //rise + if (cosH > 1) + return -1; + H = 360 - B * (acos(cosH)); + } + else + { //set + if (cosH < -1) + return -1; + H = B * (acos(cosH)); + } + + H = H / 15; + T = H + RA - (0.06571 * t) - 6.622; + + UT = T - lngHour; + + if (UT >= 24) + { + UT = UT - 24; + } + else if (UT < 0) + { + UT = UT + 24; + } + return UT; +} + + diff --git a/src/RestApiHandler.c b/src/RestApiHandler.c index fa413da..e27bbb7 100644 --- a/src/RestApiHandler.c +++ b/src/RestApiHandler.c @@ -411,6 +411,16 @@ static void funct_sd_block(char *argres, int rw) } #endif + +static void funct_astro_test(char *argres, int rw) +{ + ESP_LOGI("API", "Astro test executed"); + uint32_t unix = atoi(argres); + SetSunTimes(unix); + +} + + const int hw_rev = CONFIG_BOARD_HARDWARE_REVISION; const bool VAR_TRUE = true; const bool VAR_FALSE = false; @@ -600,6 +610,8 @@ const rest_var_t SystemVariables[] = { 0, "sd_visible", (bool*) (&VAR_FALSE), VAR_BOOL, R, 0, 1 }, #endif + { 0, "astro_test", &funct_astro_test, VAR_FUNCT, RW, 0, 0 }, + }; esp_err_t SetConfVar(char *name, char *val, rest_var_types *tp)