From 3b0bc4183c0966e33f28c5a5c030ca6737f923a7 Mon Sep 17 00:00:00 2001 From: Edward Emelianov Date: Sat, 17 Oct 2026 02:14:52 +0300 Subject: [PATCH] add corrected coordinates conversion; add info and update --- .../10micron_stellarium/10micron_commands.h | 4 +- Daemons/10micron_stellarium/angles.c | 140 +++++++++++++----- Daemons/10micron_stellarium/angles.h | 11 +- Daemons/10micron_stellarium/fitshdr.c | 8 - Daemons/10micron_stellarium/mount.c | 87 ++++++++--- Daemons/10micron_stellarium/mount.h | 20 ++- .../mountdaemon_10micron.creator.user | 2 +- Daemons/10micron_stellarium/server.c | 96 +++++++++--- 8 files changed, 276 insertions(+), 92 deletions(-) diff --git a/Daemons/10micron_stellarium/10micron_commands.h b/Daemons/10micron_stellarium/10micron_commands.h index d34a79c..b53ff2f 100644 --- a/Daemons/10micron_stellarium/10micron_commands.h +++ b/Daemons/10micron_stellarium/10micron_commands.h @@ -34,6 +34,7 @@ #define CMD_GETALT ":GA#" #define CMD_GETAZIM ":GZ#" #define CMD_GETDEC ":GD#" +#define CMD_GETINFO ":Ginfo#" #define CMD_GETSTAT ":Gstat#" #define CMD_GETRA ":GR#" #define CMD_GETPS ":pS#" @@ -41,7 +42,8 @@ // setters #define CMD_SETALT ":Sa%s#" #define CMD_SETDEC ":Sd%s#" -#define CMD_SETTIME ":SLDT%04d-%02d-%02d,%02d:%02d:%02d.%02ld#" +//#define CMD_SETTIME ":SLDT%04d-%02d-%02d,%02d:%02d:%02d.%02ld#" +#define CMD_SETTIME ":SLDT%04d-%02d-%02d,%02d:%02d:%.2f#" #define CMD_SETMINALT ":So%d#" #define CMD_SETRA ":Sr%s#" #define CMD_SETPRESSURE ":SRPRS%.1f#" diff --git a/Daemons/10micron_stellarium/angles.c b/Daemons/10micron_stellarium/angles.c index 93a1bc8..e462491 100644 --- a/Daemons/10micron_stellarium/angles.c +++ b/Daemons/10micron_stellarium/angles.c @@ -392,11 +392,11 @@ bool get_LST(sMJD_t *mjd, double *LST){ } /** - * @brief raha - convert HA to RA and vice versa + * @brief rahaNow - convert HA to RA and vice versa * @param hara - HA or RA * @return RA or HA */ -double raha(double hara){ +double rahaNow(double hara){ double LST; if(get_LST(NULL, &LST)) return eraAnp(LST - hara); return 0.; @@ -460,7 +460,7 @@ bool get_ObsPlace(struct timeval *tval, polarCrds_t *p2000, polarCrds_t *pnow, h } #endif - +#if 0 // used in two functions static bool J2000to_ep_(const polarCrds_t *j2000, polarCrds_t *jepoch, double date1, double date2){ if(!j2000 || !jepoch) return false; @@ -549,8 +549,75 @@ bool JXtoJnow(const polarCrds_t *in, polarCrds_t *out, double Jx){ // and convert J2000 to Jnow return J2000to_ep_(&j2000, out, MJD.tt1, MJD.tt2); } +#endif -#if 0 +static int is_leap_year(int year){ + return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); +} + +static void epoch2timeval(double frac_year, struct timeval *tval){ + if(!tval) return; + int year = (int)frac_year; + double fraction = frac_year - year; + int days_in_year = is_leap_year(year) ? 366 : 365; + double seconds_in_year = days_in_year * 86400.0; + struct tm start_of_year = {0}; + start_of_year.tm_year = year - 1900; + start_of_year.tm_mon = 0; + start_of_year.tm_mday = 1; + start_of_year.tm_hour = 0; + start_of_year.tm_min = 0; + start_of_year.tm_sec = 0; + start_of_year.tm_isdst = 0; + // UNIX time for start of year + time_t unix_start = timegm(&start_of_year); + // total time + fraction *= seconds_in_year; // seconds from year's start + tval->tv_sec = (time_t)(seconds_in_year); // integer fraction + tval->tv_usec = (int)((fraction - tval->tv_sec)* 1e6); + tval->tv_sec += unix_start; +} + +/** + * @brief JxtoJ2000 - convert (with aberrations, prec/nut, etc) coordinates from given epoch to J2000 + * @param in - for `epoch` + * @param out - for J2000 + * @param epoch - like 2050. + * @return false if failed + */ +bool JXtoJ2000(const polarCrds_t *in, polarCrds_t *out, double epoch){ + if(!in || !out || epoch < EPOCH_MIN || epoch > EPOCH_MAX) return false; + struct timeval tv; + epoch2timeval(epoch, &tv); + sMJD_t MJD; + if(!get_MJDt(&tv, &MJD)) return false; + double ri, eo; + eraAtic13(in->ra, in->dec, MJD.tt1, MJD.tt2, &ri, &out->dec, &eo); + out->ra = eraAnp(ri); + // LST @J2000, Greenwich = 18:41:50.548 = 4.894961 + out->ha = eraAnp(J2000LST0 + place.slong - out->ra); + return true; +} + +/** + * @brief J2000toJx - reverse of JxtoJ2000 + * @param in - for J2000 + * @param out - for `epoch` + * @param epoch - like 2030. + * @return false if failed + */ +bool J2000toJX(const polarCrds_t *in, polarCrds_t *out, double epoch){ + if(!in || !out || epoch < EPOCH_MIN || epoch > EPOCH_MAX) return false; + struct timeval tv; + epoch2timeval(epoch, &tv); + sMJD_t MJD; + if(!get_MJDt(&tv, &MJD)) return false; + double ri, eo, lst; + eraAtci13(in->ra, in->dec, 0., 0., 0., 0., MJD.tt1, MJD.tt2, &ri, &out->dec, &eo); + out->ra = eraAnp(ri); + if(get_LST(&MJD, &lst)) out->ha = eraAnp(lst - out->ra); + return true; +} /** * convert geocentric coordinates (nowadays, CIRS) to mean (JD2000, ICRS) @@ -558,50 +625,41 @@ bool JXtoJnow(const polarCrds_t *in, polarCrds_t *out, double Jx){ * @return false if failed */ bool JnowtoJ2000(const polarCrds_t *in, polarCrds_t *out){ + if(!in || !out) return false; bool ret = false; - DBG("appRa: %gdegr, appDecl: %gdegr", RAD2DEG(in->ra), RAD2DEG(in->dec)); -//#define ERFA(f, ...) do{if(f(__VA_ARGS__)){WARNX("Error in " #f); goto rtn;}}while(0) sMJD_t MJD; if(!get_MJDt(NULL, &MJD)) return false; - double ri = 0., di = 0., eo = 0.; - eraAtic13(in->ra, in->dec, MJD.tt1, MJD.tt2, &ri, &di, &eo); - if(out){ - out->ha = 0.; out->eo = 0.; - //out->ra = eraAnp(ri - eo); - out->ra = ri; - out->dec = di; - DBG("JnowtoJ2000: ra=%gdeg, dec=%gdeg", RAD2DEG(out->ra), RAD2DEG(out->dec)); - } + double ri = 0., eo = 0.; + // Jx to J2000 + eraAtic13(in->ra, in->dec, MJD.tt1, MJD.tt2, &ri, &out->dec, &eo); + out->ra = eraAnp(ri); + out->ha = eraAnp(J2000LST0 + place.slong - out->ra); ret = true; -//#undef ERFA return ret; } -/** - * @brief JXtoJnow - convert ra/dec between epochs - * @param in - coordinates for Jx (degrees) - * @param out - Jnow (degrees) - * @param Jx - Epoch for `in` - * @return false if failed - */ -bool JXtoJnow(const polarCrds_t *in, polarCrds_t *out, double Jx){ - DBG("JXRA: %ghrs, JXDec: %gdegr; epoch X: %g", RAD2HRS(in->ra), RAD2DEG(in->dec), Jx); - if(!in) return false; - // these values could be changed (e.g. by user's input) - double pr = 0.0; // RA proper motion (radians/year) - double pd = 0.0; // Dec proper motion (radians/year) - double px = 0.0; // parallax (arcsec) - double rv = 0.0; // radial velocity (km/s, positive if receding) - double ri, di, eo; - eraAtci13(in->ra, in->dec, pr, pd, px, rv, ERFA_DJM0, Jx, &ri, &di, &eo); - LOGDBG("ri: %ghrs, eo: %ghrs, di: %gdeg", RAD2HRS(ri), RAD2HRS(eo), RAD2DEG(di)); - if(out){ - out->ha = 0.; out->eo = 0.; - //out->ra = eraAnp(ri - eo); - out->ra = ri; - out->dec = di; - } +bool J2000toJnow(const polarCrds_t *in, polarCrds_t *out){ + if(!in || !out) return false; + sMJD_t MJD; + if(!get_MJDt(NULL, &MJD)) return false; + double ri, eo; + eraAtci13(in->ra, in->dec, 0., 0., 0., 0., MJD.tt1, MJD.tt2, &ri, &out->dec, &eo); + out->ra = eraAnp(ri); + out->ha = rahaNow(out->ra); return true; } -#endif +/** + * @brief JXtoJnow - convert ra/dec between epochs with aberrations, prec/nut, ... + * @param in - coordinates for Jx + * @param out - Jnow + * @param epoch - Epoch for `in` + * @return false if failed + */ +bool JXtoJnow(const polarCrds_t *in, polarCrds_t *out, double epoch){ + if(!in || !out || epoch < EPOCH_MIN || epoch > EPOCH_MAX) return false; + polarCrds_t j2000; + if(fabs(epoch - 2000.) < 1e-3) j2000 = *in; + else if(!JXtoJ2000(in, &j2000, epoch)) return false; + return J2000toJnow(&j2000, out); +} diff --git a/Daemons/10micron_stellarium/angles.h b/Daemons/10micron_stellarium/angles.h index 896d302..dab9a35 100644 --- a/Daemons/10micron_stellarium/angles.h +++ b/Daemons/10micron_stellarium/angles.h @@ -23,6 +23,12 @@ #define RADEC_STR_MAXLEN 64 +// LST for Greenwich @ J2000 +#define J2000LST0 4.894961 +// min/max Epoch +#define EPOCH_MIN 1950. +#define EPOCH_MAX 2050. + #define RAD2DEG(angle) ((angle) * ERFA_DR2D) #define DEG2RAD(angle) ((angle) * ERFA_DD2R) #define RAD2HRS(angle) ((angle) * 12. / ERFA_DPI) @@ -74,7 +80,7 @@ void norm_RADEC(double *ra, double *dec); void norm_RADECr(double *ra, double *dec); void norm_angle180(double *a); -double raha(double hara); +double rahaNow(double hara); void hor2eq(horizCrds_t *h, polarCrds_t *pc, double sidTime); void eq2horH(polarCrds_t *pc, horizCrds_t *h); @@ -100,5 +106,8 @@ void getPlaceData(placeData_t *pd); bool J2000_to_epoch_pn(const polarCrds_t *j2000, double epoch, polarCrds_t *jepoch); bool epoch_to_J2000_pn(const polarCrds_t *jepoch, double epoch, polarCrds_t *j2000); +bool JXtoJ2000(const polarCrds_t *in, polarCrds_t *out, double epoch); +bool J2000toJX(const polarCrds_t *in, polarCrds_t *out, double epoch); bool JnowtoJ2000(const polarCrds_t *in, polarCrds_t *out); +bool J2000toJnow(const polarCrds_t *in, polarCrds_t *out); bool JXtoJnow(const polarCrds_t *in, polarCrds_t *out, double Jx); diff --git a/Daemons/10micron_stellarium/fitshdr.c b/Daemons/10micron_stellarium/fitshdr.c index 19769c8..8d085e2 100644 --- a/Daemons/10micron_stellarium/fitshdr.c +++ b/Daemons/10micron_stellarium/fitshdr.c @@ -87,17 +87,9 @@ static int printhdr(int fd, const char *key, const char *val, const char *cmnt){ */ void wrhdr(fitsheader_t *HDR){ if(!HDR) return; - static time_t lastcorr = 0; // last time of time/weather corrections sent to mount double curtime = sl_dtime(); //bool haveweather = false; bool poweredON = (HDR->status == MNT_S_ERROR || HDR->status == MNT_S_OFF) ? false : true; - // weather block - if(time(NULL) - HDR->weather.last_update < (int)(1. + MOUNT_CHECK_T * 10.)){ // weather data is good - if(poweredON && time(NULL) - lastcorr > CORRECTIONS_TIMEDIFF){ // make correction once per hour - if(mount_corrdata(&HDR->weather)) lastcorr = time(NULL); - } - //haveweather = true; - }else WARNX("bad weather; curtime - HDR->weather.last_update = %g", curtime - HDR->weather.last_update); if(!hdname){ DBG("hdname not given!"); return; diff --git a/Daemons/10micron_stellarium/mount.c b/Daemons/10micron_stellarium/mount.c index b593724..be440fa 100644 --- a/Daemons/10micron_stellarium/mount.c +++ b/Daemons/10micron_stellarium/mount.c @@ -91,7 +91,7 @@ bool mount_setInpHA(double ha){ if(ha < 0) ha += 24.; if(ha < 0. || ha >= 24.) return false; InpCoords.ha = HRS2RAD(ha); - InpCoords.ra = raha(InpCoords.ha); + InpCoords.ra = rahaNow(InpCoords.ha); InpCTime = sl_dtime(); return true; } @@ -103,7 +103,7 @@ bool mount_setInpHA(double ha){ bool mount_setInpRA(double ra){ if(ra < 0. || ra >= 24.) return false; InpCoords.ra = HRS2RAD(ra); - InpCoords.ha = raha(InpCoords.ra); + InpCoords.ha = rahaNow(InpCoords.ra); InpCTime = sl_dtime(); return true; } @@ -142,6 +142,7 @@ bool mount_setInpZ(double Z){ } // without checking bool mount_setInpEpoch(double epoch){ + if(epoch < EPOCH_MIN || epoch > EPOCH_MAX) return false; InpEpoch = epoch; InpETime = sl_dtime(); return true; @@ -305,10 +306,11 @@ mount_status_t mount_status(){ return MNT_S_OFF; } char resp[64]; - pthread_mutex_lock(&mntdev_mutex); int ntry = 0; + pthread_mutex_lock(&mntdev_mutex); for(; ntry < 3; ++ntry) if(send_cmd_resp(CMD_GETSTAT, resp, sizeof(resp))) break; + pthread_mutex_unlock(&mntdev_mutex); if(ntry == 3){ // mount not responded -> set status OFF curst = MNT_S_OFF; }else{ // parsing of status @@ -319,7 +321,6 @@ mount_status_t mount_status(){ else curst = (mount_status_t) statN; DBG("STATUS: %s (%d)", statuses[curst], curst); } - pthread_mutex_unlock(&mntdev_mutex); atomic_store(&mountstatus, curst); return curst; } @@ -449,7 +450,7 @@ bool mount_point(double ra, double dec){ } ret = true; TagCoords.ra = HRS2RAD(ra); - TagCoords.ha = raha(TagCoords.ra); + TagCoords.ha = rahaNow(TagCoords.ra); TagCoords.dec = DEG2RAD(dec); TagTime = sl_dtime(); LOGMSG("Go to RA/Decl: %s/%s", rastr, decstr); @@ -558,6 +559,51 @@ retn: return ret; } + +bool mount_getinfo(mount_info_t *info){ + if(!info || !mount_dev) return false; + double ra, dec, az, zd, jd; + char pier; + int stat, slew; + char resp[128]; + if(isemulated){ + mount_getaz(&az, &zd); + mount_getcoords(&ra, &dec); + sMJD_t mjd; + get_MJDt(NULL, &mjd); + jd = mjd.MJD; + pier = 'x'; + stat = emulation_status(); + slew = 0; + }else{ + bool OK = false; + pthread_mutex_lock(&mntdev_mutex); + if(send_cmd_resp(CMD_GETINFO, resp, sizeof(resp))) OK = true; + pthread_mutex_unlock(&mntdev_mutex); + if(!OK) return false; + double alt; + if(8 != sscanf(resp, "%lf,%lf,%c,%lf,%lf,%lf,%d,%d", + &ra, &dec, &pier, &az, &alt, &jd, &stat, &slew)) return false; + //LOGDBG("ra=%g, dec=%g, az=%g, alt=%g, jd=%g", ra, dec, az, alt, jd); + zd = 90. - alt; + } + info->ra = HRS2RAD(ra); + info->dec = DEG2RAD(dec); + info->az = DEG2RAD(az); + info->zd = DEG2RAD(zd); + info->JD = jd; + const char *pierside = "Unknown"; + switch(pier){ + case 'W': pierside = "West"; break; + case 'E': pierside = "East"; break; + default : break; + } + strcpy(info->pierside, pierside); + info->status = (mount_status_t) stat; + info->slewing = slew; + return true; +} + /** * @brief mount_stop - stop any moving * @return false if failed @@ -597,7 +643,7 @@ bool mount_tracking_start(){ if(!mount_getcoords(&ra, &dec)) return false; TagCoords.ra = HRS2RAD(ra); TagCoords.dec = DEG2RAD(dec); - TagCoords.ha = raha(TagCoords.ra); + TagCoords.ha = rahaNow(TagCoords.ra); TagTime = sl_dtime(); mount_setInpEpoch(-1.); // Jnow if(isemulated){ @@ -684,19 +730,20 @@ bool mount_getpierside(char *Ps, size_t len){ bool mount_corrdata(weather_data_t *w){ if(isemulated) return true; if(!w) return false; - bool ret = true; // returning value - mount_status_t curst = mount_status(); - if(curst == MNT_S_SLEWING || curst == MNT_S_TRACKING) return true; - DBG("Refresh datetime"); - pthread_mutex_lock(&mntdev_mutex); char buf[128]; buf[127] = 0; - time_t t = time(NULL); - struct tm *stm = localtime(&t); - struct timeval tv; - gettimeofday(&tv, NULL); + bool ret = true; // returning value + + DBG("Refresh datetime"); + pthread_mutex_lock(&mntdev_mutex); + + double curt = sl_dtime() + FRAC_T_TODO; + time_t seconds = (int) curt; + double frac = curt - seconds; + struct tm tm_buf; + struct tm *stm = localtime_r(&seconds, &tm_buf); snprintf(buf, 127, CMD_SETTIME, 1900+stm->tm_year, stm->tm_mon+1, stm->tm_mday, - stm->tm_hour, stm->tm_min, stm->tm_sec, tv.tv_usec/10000); + stm->tm_hour, stm->tm_min, (double)stm->tm_sec + frac); DBG("write: %s", buf); if(!write_cmd(buf, true)){ WARNX("Can't write current date/time"); @@ -740,11 +787,11 @@ bool mount_shutdown(){ * @return false if failed */ bool mount_rawcmd(const char *cmd, char *ans, size_t anslen){ - if(isemulated) return false; + if(!cmd || isemulated) return false; pthread_mutex_lock(&mntdev_mutex); - LOGDBG("Send %s", cmd); + //LOGDBG("Send %s", cmd); bool ret = send_cmd_resp(cmd, ans, anslen); - LOGDBG("Ans: %s", ret ? "true" : "false"); + //LOGDBG("Ans: %s", ret ? "true" : "false"); pthread_mutex_unlock(&mntdev_mutex); return ret; } @@ -759,7 +806,7 @@ static double get_guide_rate(){ char resp[32]; if(send_cmd_resp(CMD_GETGUIDERATE, resp, sizeof(resp))){ double rate = atof(resp); - LOGDBG("get guide rate: %g", rate); + //LOGDBG("get guide rate: %g", rate); if(rate > 0.0 && rate <= SIDEREAL_RATE){ return rate; } diff --git a/Daemons/10micron_stellarium/mount.h b/Daemons/10micron_stellarium/mount.h index 0ff3e8d..cc08f39 100644 --- a/Daemons/10micron_stellarium/mount.h +++ b/Daemons/10micron_stellarium/mount.h @@ -22,11 +22,16 @@ #include "angles.h" +// TODO: this value should be changed by config & proto +// (add this to time, sent to mount) +#define FRAC_T_TODO (.5) + + // input coordinates lifetime - 24hrs #define COORDS_OLD_T (86400) // make datetime/pressure/temperature corrections each CORRECTIONS_TIMEDIFF seconds -#define CORRECTIONS_TIMEDIFF (3600) +#define CORRECTIONS_TIMEDIFF (60) // sidereal tracking rate #define SIDEREAL_RATE 15.041 @@ -56,6 +61,18 @@ typedef enum{ MNT_S_STATAMOUNT // number of statuses } mount_status_t; +// ALL coordinates here are in radians instead of degrees like in other functions! +typedef struct{ + double ra; + double dec; + char pierside[8]; // 'West' or 'East' + double az; + double zd; + double JD; + mount_status_t status; + uint8_t slewing; +} mount_info_t; + void set_emulation_mode(); bool mount_setInpHA(double ha); @@ -79,6 +96,7 @@ mount_status_t mount_status(); bool mount_connect(); void mount_disconnect(); +bool mount_getinfo(mount_info_t *info); bool mount_getcoords(double *ra, double *dec); bool mount_getaz(double *a, double *z); diff --git a/Daemons/10micron_stellarium/mountdaemon_10micron.creator.user b/Daemons/10micron_stellarium/mountdaemon_10micron.creator.user index 8269664..42a78f1 100644 --- a/Daemons/10micron_stellarium/mountdaemon_10micron.creator.user +++ b/Daemons/10micron_stellarium/mountdaemon_10micron.creator.user @@ -1,6 +1,6 @@ - + EnvironmentId diff --git a/Daemons/10micron_stellarium/server.c b/Daemons/10micron_stellarium/server.c index 1000ed4..00ad41e 100644 --- a/Daemons/10micron_stellarium/server.c +++ b/Daemons/10micron_stellarium/server.c @@ -66,6 +66,7 @@ #define CMD_TELZD "telzd" #define CMD_TRACK "track" #define CMD_UNIXT "unixt" +#define CMD_UPDATE "update" // main command socket @@ -79,6 +80,9 @@ volatile bool isrunning = true; // lost weather flag static bool weatherlost = false; +// make weather/time correction even in tracking state +static bool needcorr = false; + // data for FITS-header (also to send user) static fitsheader_t HDR = {0}; static pthread_mutex_t headermutex = PTHREAD_MUTEX_INITIALIZER; @@ -282,7 +286,7 @@ static sl_sock_hresult_e cmd_gotorh(_U_ sl_sock_t *c, _U_ sl_sock_hitem_t *item, if(weatherlost || HDR.status == MNT_S_ERROR) return RESULT_FAIL; polarCrds_t p; mount_getInpCoords(&p); - double RA_rad = raha(p.ha); // re-convert (as time could have passed) + double RA_rad = rahaNow(p.ha); // re-convert (as time could have passed) double ra_h = RAD2HRS(RA_rad); double dec_d = RAD2DEG(p.dec); LOGDBG("Client %d asks to point to HA/Dec", c->fd); @@ -441,18 +445,34 @@ static sl_sock_hresult_e cmd_place(sl_sock_t *c, sl_sock_hitem_t _U_ *item, cons return RESULT_SILENCE; } -static sl_sock_hresult_e cmd_raw(sl_sock_t *c, sl_sock_hitem_t _U_ *item, const char *req){ +// parsing of raw command (for `cmd_raw` and `unknown_command`) +static sl_sock_hresult_e rawcommand(sl_sock_t *c, const char *req){ char buf[128]; - if(!req || !*req) return RESULT_BADVAL; - if(!mount_rawcmd(req, buf, 126)) strcpy(buf, "No answer"); - size_t L = strlen(buf); - buf[L] = '\n'; - buf[L + 1] = 0; - LOGMSG("Client %d asks for raw command '%s'; answer: '%s'", req, buf); + if(!mount_rawcmd(req, buf, 126)){ + LOGMSG("Client %d asks for raw command '%s'; got no answer or error", c->fd, req); + return RESULT_FAIL; + }else{ + size_t L = strlen(buf); + if(L > 126){ L = 126; buf[126] = 0; } + LOGMSG("Client %d asks for raw command '%s'; answer: '%s'", c->fd, req, buf); + buf[L] = '\n'; + buf[L + 1] = 0; + } sl_sock_sendstrmessage(c, buf); return RESULT_SILENCE; } +static sl_sock_hresult_e cmd_raw(sl_sock_t *c, sl_sock_hitem_t _U_ *item, const char *req){ + char cmdbuf[32], *cmdptr = cmdbuf; + if(!req || !*req) return RESULT_BADVAL; + size_t L = strlen(req); + if(L < 1 || L > 29) return RESULT_BADVAL; + if(*req != ':') *cmdptr++ = ':'; + if(req[L-1] != '#') sprintf(cmdptr, "%s#", req); + else strcpy(cmdptr, req); + return rawcommand(c, cmdbuf); +} + static sl_sock_hresult_e cmd_shutdown(sl_sock_t *c, sl_sock_hitem_t *item, const char *req){ double val; static int shtdwnkey = -1; @@ -500,6 +520,19 @@ static sl_sock_hresult_e cmd_corra(sl_sock_t *c, sl_sock_hitem_t _U_ *item, cons return RESULT_FAIL; } +static sl_sock_hresult_e cmd_update(sl_sock_t _U_ *c, sl_sock_hitem_t _U_ *item, const char _U_ *req){ + if(HDR.status == MNT_S_OFF) return RESULT_FAIL; + needcorr = true; + return RESULT_OK; +} + +// default handler: if message starts from ':' and ends by '#' treat it as raw command +static sl_sock_hresult_e unknown_command(struct sl_sock *c, const char *str){ + if(!str || !*str) return RESULT_BADKEY; + size_t L = strlen(str); + if(*str != ':' || str[L-1] != '#') return RESULT_BADKEY; + return rawcommand(c, str); +} // and all handlers collection @@ -535,6 +568,7 @@ static sl_sock_hitem_t handlers[] = { {dtimeh, CMD_UNIXT, "get server's UNIX time", NULL}, {cmd_corra, CMD_CORRA, "correct RA by x arcseconds", NULL}, {cmd_cordec, CMD_CORDEC, "correct Dec by x arcseconds", NULL}, + {cmd_update, CMD_UPDATE, "update time/weather data to mount", NULL}, {NULL, NULL, NULL, NULL} }; @@ -580,6 +614,7 @@ bool server_check(server_sock_t *sockt){ sl_sock_maxclhandler(cmd_socket, toomuch); sl_sock_connhandler(cmd_socket, connected); sl_sock_dischandler(cmd_socket, disconnected); + sl_sock_defmsghandler(cmd_socket, unknown_command); stellarium_sockfd = sl_sock_open(SOCKT_NET, sockt->stellport, 1, 0); if(stellarium_sockfd < 0){ LOGERR("Can't start stellarium socket"); @@ -618,12 +653,15 @@ void server_run(){ if(!mount_connect()){ LOGWARN("Can't connect to mount, will try to reconnect later"); } - DBG("While"); + double tcheck = sl_dtime(), treconnect = 0.; - time_t lastweathertime = 0; + bool notlogged = true; + double lastweathertime = 0; // last weather checking time + double lastcorr = 0.; // last time of time/weather corrections sent to mount + getPlaceData(&HDR.place); mount_get_name(HDR.mountname, MAX_HDR_STRLEN); - bool notlogged = true; + DBG("While"); while(isrunning && cmd_socket && cmd_socket->connected){ usleep(sleept); if(!cmd_socket->rthread){ @@ -634,7 +672,7 @@ void server_run(){ double tnow = sl_dtime(); if(tnow - tcheck >= MOUNT_CHECK_T){ tcheck = tnow; - if(get_weather_data(&HDR.weather) == 0){ + if(get_weather_data(&HDR.weather) == 0){ // for airmass lastweathertime = HDR.weather.last_update; }else{ WARNX("Can't get weather data"); @@ -642,12 +680,23 @@ void server_run(){ // collect data for other fields pthread_mutex_lock(&headermutex); HDR.status = mount_status(); + get_MJDt(NULL, &HDR.MJD); + get_LST(&HDR.MJD, &HDR.sidtime); if(HDR.status != MNT_S_OFF){ + mount_info_t info; + if(mount_getinfo(&info)){ + LOGDBG("Got info; JDtel-JDcomp=%gs", 86400.*(info.JD - ERFA_DJM0 - HDR.MJD.MJD)); + HDR.altaz.az = info.az; + HDR.altaz.zd = info.zd; + HDR.polar.dec = info.dec; + HDR.polar.ra = info.ra; + HDR.polar.ha = eraAnp(HDR.sidtime - HDR.polar.ra); + strcpy(HDR.pierside, info.pierside); + }else LOGWARN("Can't get mount info"); + /* double deg1, deg2; DBG("Take coordinates"); if(mount_getcoords(°1, °2)){ - get_MJDt(NULL, &HDR.MJD); - get_LST(&HDR.MJD, &HDR.sidtime); HDR.polar.ra = HRS2RAD(deg1); HDR.polar.dec = DEG2RAD(deg2); HDR.polar.ha = eraAnp(HDR.sidtime - HDR.polar.ra); @@ -656,19 +705,28 @@ void server_run(){ HDR.altaz.az = DEG2RAD(deg1); HDR.altaz.zd = DEG2RAD(deg2); } + */ if(tnow - lastweathertime > MOUNT_WEATHER_ALRM){ weatherlost = true; if(HDR.status == MNT_S_TRACKING || HDR.status == MNT_S_SLEWING){ LOGERR("Lost meteo connection -> park"); if(mount_park()) lastweathertime = tnow; } - } else weatherlost = false; - }else{ - get_MJDt(NULL, &HDR.MJD); - get_LST(&HDR.MJD, &HDR.sidtime); + } else { + weatherlost = false; + if(tnow - lastweathertime < (int)(1. + MOUNT_CHECK_T * 10.)){ // weather data is good + // make correction once per hour, but only in non-tracking state + if(needcorr || ((HDR.status != MNT_S_TRACKING) && tnow - lastcorr > CORRECTIONS_TIMEDIFF)){ + if(mount_corrdata(&HDR.weather)){ + needcorr = false; + lastcorr = tnow; + } + } + }else WARNX("bad weather; curtime - HDR->weather.last_update = %g", tnow - lastweathertime); + } } getDUT(&HDR.dut); - if(HDR.status == MNT_S_OFF || !mount_getpierside(HDR.pierside, MAX_HDR_STRLEN)) *HDR.pierside = 0; + //if(HDR.status == MNT_S_OFF || !mount_getpierside(HDR.pierside, MAX_HDR_STRLEN)) *HDR.pierside = 0; pthread_mutex_unlock(&headermutex); // and write FITS-header wrhdr(&HDR);