26#include <libnova/transform.h>
27#include <libnova/precession.h>
30#include <libnova/utility.h>
36#define SYNSCAN_SLEW_RATES 9
38constexpr uint16_t SynscanLegacyDriver::SLEW_RATE[];
45 strncpy(LastParkRead,
"", 1);
63 return "SynScan Legacy";
92 IUFillText(&BasicMountInfoT[MI_FW_VERSION],
"FW_VERSION",
"Firmware version",
"-");
93 IUFillText(&BasicMountInfoT[MI_MOUNT_CODE],
"MOUNT_CODE",
"Mount code",
"-");
94 IUFillText(&BasicMountInfoT[MI_ALIGN_STATUS],
"ALIGNMENT_STATUS",
"Alignment status",
"-");
95 IUFillText(&BasicMountInfoT[MI_GOTO_STATUS],
"GOTO_STATUS",
"Goto status",
"-");
96 IUFillText(&BasicMountInfoT[MI_POINT_STATUS],
"MOUNT_POINTING_STATUS",
97 "Mount pointing status",
"-");
98 IUFillText(&BasicMountInfoT[MI_TRACK_MODE],
"TRACKING_MODE",
"Tracking mode",
"-");
126 if (!strcmp(name, UseWiFiSP.name))
132 LOG_ERROR(
"Cannot select WiFi mode while already connected. It must be selected when when disconnected.");
138 if (UseWiFiS[WIFI_ENABLED].s ==
ISS_ON)
144 LOG_INFO(
"Driver is configured for WiFi connection to 192.168.4.2 at TCP port 11882");
151 const char *newNames[] = {
"CONNECTION_SERIAL",
"CONNECTION_TCP" };
157 LOG_INFO(
"Driver is configured for serial connection to the hand controller.");
163 const char *newNames[] = {
"CONNECTION_SERIAL",
"CONNECTION_TCP" };
175 char *formats[],
char *names[],
int n)
191 UpdateMountInformation(
false);
208 result = std::stoi(res,
nullptr, 16);
210 catch (std::invalid_argument &)
212 LOGF_ERROR(
"Failed to parse %s to integer.", res.c_str());
224 int bytesWritten = 0;
226 char res[MAX_SYN_BUF] = {0};
241 memset(res, 0, MAX_SYN_BUF);
249 LOG_ERROR(
"Mount is not aligned. Please align the mount first and connection again.");
257 memset(res, 0, MAX_SYN_BUF);
258 LOG_DEBUG(
"Getting Firmware version...");
267 int tmp1 { 0 }, tmp2 { 0 };
289 LOG_WARN(
"Firmware version is too old. Update Synscan firmware to v4.38+");
299 memset(res, 0, MAX_SYN_BUF);
309 MountCode =
static_cast<int>(*
reinterpret_cast<unsigned char *
>(&res[0]));
311 MountCode =
static_cast<int>(*
reinterpret_cast<unsigned char*
>(&res[1]));
317 memset(res, 0, MAX_SYN_BUF);
323 if (res[1] ==
'#' &&
static_cast<int>(res[0]) != 0)
361 char res[MAX_SYN_BUF] = {0};
362 int bytesWritten, bytesRead;
364 long unsigned int n1, n2;
374 LOG_WARN(
"Synscan Mount not responding");
423 memset(res, 0, MAX_SYN_BUF);
432 memset(res, 0, MAX_SYN_BUF);
441 memset(res, 0, MAX_SYN_BUF);
453 memset(res, 0, MAX_SYN_BUF);
478 UpdateMountInformation(
true);
533 memset(res, 0, MAX_SYN_BUF);
541 if (strncmp((
char *)res, LastParkRead, 18) == 0)
572 snprintf(LastParkRead, 20,
"%.20s", res);
576 memset(res, 0, MAX_SYN_BUF);
583 LOG_DEBUG(
"Read current position failed");
587 sscanf(res,
"%lx,%lx#", &n1, &n2);
588 ra =
static_cast<double>(n1) / 0x100000000 * 24.0;
589 dec =
static_cast<double>(n2) / 0x100000000 * 360.0;
607 double DiffAlt { 0 };
616 if (std::abs(DiffAlt) > 4)
620 else if (std::abs(DiffAlt) > 1.2)
624 else if (std::abs(DiffAlt) > 0.5)
628 else if (std::abs(DiffAlt) > 0.2)
632 else if (std::abs(DiffAlt) > 0.025)
636 LOGF_DEBUG(
"Slewing Alt axis: %1.3f-%1.3f -> %1.3f (speed: %d)",
638 if (NewRate != CustomNSSlewRate)
642 CustomNSSlewRate = NewRate;
647 CustomNSSlewRate = NewRate;
656 LOG_DEBUG(
"Slewing on Alt axis finished");
660 DiffAz = (DiffAz + 360) * 2;
661 else if (DiffAz > 180)
662 DiffAz = (DiffAz - 360) * 2;
667 if (std::abs(DiffAz) > 4)
671 else if (std::abs(DiffAz) > 1.2)
675 else if (std::abs(DiffAz) > 0.5)
679 else if (std::abs(DiffAz) > 0.2)
683 else if (std::abs(DiffAz) > 0.025)
687 LOGF_DEBUG(
"Slewing Az axis: %1.3f-%1.3f -> %1.3f (speed: %d)",
689 if (NewRate != CustomWESlewRate)
693 CustomWESlewRate = NewRate;
698 CustomWESlewRate = NewRate;
707 LOG_DEBUG(
"Slewing on Az axis finished");
719 char res[MAX_SYN_BUF] = {0};
720 int bytesWritten, bytesRead;
743 if (bytesRead != 1 || res[0] !=
'#')
745 LOG_DEBUG(
"Timeout waiting for scope to start tracking.");
753 char res[MAX_SYN_BUF] = {0};
754 int bytesWritten, bytesRead;
765 LOG_WARN(
"Wrong answer from the mount");
784 int n1 = J2000Pos.rightascension * 0x1000000 / 24;
785 int n2 = J2000Pos.declination * 0x1000000 / 360;
787 LOGF_DEBUG(
"Goto - JNow RA: %g JNow DE: %g J2000 RA: %g J2000 DE: %g",
ra,
dec, J2000Pos.rightascension,
788 J2000Pos.declination);
793 snprintf(res, MAX_SYN_BUF,
"r%08X,%08X", n1, n2);
795 memset(&res[18], 0, 1);
798 if (bytesRead != 1 || res[0] !=
'#')
800 LOG_DEBUG(
"Timeout waiting for scope to complete goto.");
810 char RAStr[MAX_SYN_BUF] = {0}, DEStr[MAX_SYN_BUF] = {0}, AZStr[MAX_SYN_BUF] = {0}, ATStr[MAX_SYN_BUF] = {0};
816 LOGF_INFO(
"Goto RA: %s DE: %s AZ: %s ALT: %s", RAStr, DEStr, AZStr, ATStr);
829 char res[MAX_SYN_BUF] = {0};
830 int bytesWritten, bytesRead;
834 strncpy(LastParkRead,
"", 1);
849 if (bytesRead != 1 || res[0] !=
'#')
851 LOG_DEBUG(
"Timeout waiting for scope to stop tracking.");
858 if (bytesRead != 1 || res[0] !=
'#')
860 LOG_DEBUG(
"Timeout waiting for scope to respond to park.");
883 LOG_INFO(
"Setting arbitrary park positions is not supported yet.");
890 LOG_DEBUG(
"Setting Park Data to Default.");
902 char res[MAX_SYN_BUF] = {0};
903 int bytesWritten, bytesRead;
913 CustomNSSlewRate = -1;
914 CustomWESlewRate = -1;
925 if (bytesRead != 1 || res[0] !=
'#')
927 LOG_DEBUG(
"Timeout waiting for scope to stop tracking.");
952 PassthruCommand(37, 17, 2, 0, 0);
956 int tt = (CustomNSSlewRate == -1 ?
SlewRate : CustomNSSlewRate);
961 PassthruCommand(37, 17, 2, tt, 0);
965 PassthruCommand(36, 17, 2, tt, 0);
979 PassthruCommand(37, 16, 2, 0, 0);
983 int tt = (CustomWESlewRate == -1 ?
SlewRate : CustomWESlewRate);
988 PassthruCommand(36, 16, 2, tt, 0);
992 PassthruCommand(37, 16, 2, tt, 0);
1005int SynscanLegacyDriver::PassthruCommand(
int cmd,
int target,
int msgsize,
int data,
int numReturn)
1007 char test[20] = {0};
1008 int bytesRead, bytesWritten;
1019 memset(test, 0, 20);
1027 test[7] = numReturn;
1031 memset(test, 0, 20);
1040 retval = retval << 8;
1045 retval = retval << 8;
1061 time_t now = time (
nullptr);
1062 strftime(timeString,
MAXINDINAME,
"%T", gmtime(&now));
1070 char res[MAX_SYN_BUF] = {0};
1071 int bytesWritten = 0, bytesRead = 0;
1083 ln_zonedate localTime;
1085 int offset, daylightflag;
1087 localTime.hours = res[0];
1088 localTime.minutes = res[1];
1089 localTime.seconds = res[2];
1090 localTime.months = res[3];
1091 localTime.days = res[4];
1092 localTime.years = res[5];
1093 offset = (int)res[6];
1097 localTime.gmtoff = offset;
1099 daylightflag = res[7];
1100 localTime.years += 2000;
1101 localTime.gmtoff *= 3600;
1103 ln_zonedate_to_date(&localTime, &utcTime);
1109 sec = (int)utcTime.seconds;
1110 sprintf(utc,
"%04d-%02d-%dT%d:%02d:%02d", utcTime.years, utcTime.months, utcTime.days, utcTime.hours,
1111 utcTime.minutes, sec);
1112 if (daylightflag == 1)
1113 offset = offset + 1;
1114 snprintf(ofs, 16,
"%d", offset);
1121 LOGF_INFO(
"Mount UTC Time %s Offset %d", utc, offset);
1141 char res[MAX_SYN_BUF] = {0};
1142 int bytesWritten = 0, bytesRead = 0;
1153 LOG_WARN(
"Bad echo in ReadLocation");
1169 int a, b, c, d, e, f, g, h;
1179 LOGF_DEBUG(
"Pos %d:%d:%d %d:%d:%d", a, b, c, e, f, g);
1207 char LongitudeStr[32] = {0}, LatitudeStr[32] = {0};
1208 fs_sexa(LongitudeStr, lon, 2, 3600);
1209 fs_sexa(LatitudeStr, lat, 2, 3600);
1210 LOGF_INFO(
"Mount Longitude %s Latitude %s", LongitudeStr, LatitudeStr);
1219 LOG_INFO(
"Mount does not support setting location.");
1227 char res[MAX_SYN_BUF] = {0};
1228 int bytesWritten = 0, bytesRead = 0;
1232 struct ln_zonedate ltm;
1234 ln_date_to_zonedate(utc, <m, (
long)utc_offset * 3600.0);
1242 res[2] = ltm.minutes;
1243 res[3] = (char)(
int)ltm.seconds;
1244 res[4] = ltm.months;
1249 res[7] = (char)(
int)utc_offset;
1254 LOGF_INFO(
"Setting mount date/time to %04d-%02d-%02d %d:%02d:%02d UTC Offset: %d",
1255 ltm.years, ltm.months, ltm.days, ltm.hours, ltm.minutes, ltm.seconds, utc_offset);
1268 LOG_INFO(
"Invalid return from set time");
1277 char res[MAX_SYN_BUF] = {0};
1278 int bytesWritten = 0, bytesRead = 0;
1280 bool IsWest =
false;
1283 ln_lnlat_posn p1 { 0, 0 };
1302 if (longitude > 180)
1304 p1.lng = 360.0 - longitude;
1312 ln_lnlat_to_hlnlat(&p1, &p2);
1313 LOGF_INFO(
"Update location to latitude %d:%d:%1.2f longitude %d:%d:%1.2f",
1314 p2.lat.degrees, p2.lat.minutes, p2.lat.seconds, p2.lng.degrees, p2.lng.minutes, p2.lng.seconds);
1317 res[1] = p2.lat.degrees;
1318 res[2] = p2.lat.minutes;
1319 tmp = p2.lat.seconds + 0.5;
1322 if (p2.lat.neg == 0)
1331 res[5] = p2.lng.degrees;
1332 res[6] = p2.lng.minutes;
1333 s = (int)(p2.lng.seconds + 0.5);
1350 LOG_INFO(
"Invalid response for location setting");
1387 char res[MAX_SYN_BUF] = {0};
1388 int bytesWritten, bytesRead;
1405 int Az = (int)(TargetAltAz.
azimuth * 16777216 / 360);
1411 *
reinterpret_cast<unsigned char*
>(&res[4]) = (
unsigned char)(Az / 65536);
1412 Az -= (Az / 65536) * 65536;
1413 *
reinterpret_cast<unsigned char*
>(&res[5]) = (
unsigned char)(Az / 256);
1414 Az -= (Az / 256) * 256;
1415 *
reinterpret_cast<unsigned char*
>(&res[6]) = (
unsigned char)Az;
1420 int Alt = (int)(TargetAltAz.
altitude * 16777216 / 360);
1426 *
reinterpret_cast<unsigned char*
>(&res[4]) = (
unsigned char)(Alt / 65536);
1427 Alt -= (Alt / 65536) * 65536;
1428 *
reinterpret_cast<unsigned char*
>(&res[5]) = (
unsigned char)(Alt / 256);
1429 Alt -= (Alt / 256) * 256;
1430 *
reinterpret_cast<unsigned char*
>(&res[6]) = (
unsigned char)Alt;
1449 int n1 = J2000Pos.rightascension * 0x1000000 / 24;
1450 int n2 = J2000Pos.declination * 0x1000000 / 360;
1454 snprintf(res, MAX_SYN_BUF,
"s%08X,%08X", n1, n2);
1455 memset(&res[18], 0, 1);
1463 if (bytesRead != 1 || res[0] !=
'#')
1465 LOG_DEBUG(
"Timeout waiting for scope to complete syncing.");
1470 if (IsTrackingBeforeSync)
1476void SynscanLegacyDriver::UpdateMountInformation(
bool inform_client)
1478 bool BasicMountInfoHasChanged =
false;
1479 std::string MountCodeStr = std::to_string(
MountCode);
1484 BasicMountInfoHasChanged =
true;
1486 if (std::string(BasicMountInfoT[MI_MOUNT_CODE].text) != MountCodeStr)
1488 IUSaveText(&BasicMountInfoT[MI_MOUNT_CODE], MountCodeStr.c_str());
1489 BasicMountInfoHasChanged =
true;
1492 if (std::string(BasicMountInfoT[MI_ALIGN_STATUS].text) !=
AlignmentStatus)
1495 BasicMountInfoHasChanged =
true;
1497 if (std::string(BasicMountInfoT[MI_GOTO_STATUS].text) !=
GotoStatus)
1500 BasicMountInfoHasChanged =
true;
1502 if (std::string(BasicMountInfoT[MI_POINT_STATUS].text) !=
PointingStatus)
1505 BasicMountInfoHasChanged =
true;
1507 if (std::string(BasicMountInfoT[MI_TRACK_MODE].text) !=
TrackingMode)
1510 BasicMountInfoHasChanged =
true;
1513 if (BasicMountInfoHasChanged && inform_client)
1517void SynscanLegacyDriver::MountSim()
1519 static struct timeval ltv;
1525 gettimeofday(&tv,
nullptr);
1527 if (ltv.tv_sec == 0 && ltv.tv_usec == 0)
1530 dt = tv.tv_sec - ltv.tv_sec + (tv.tv_usec - ltv.tv_usec) / 1e6;
1533 da = currentSlewRate * dt;
virtual bool saveConfig(bool silent=false, const char *property=nullptr)
Save the current properties in a configuration file.
virtual bool deleteProperty(const char *propertyName)
Delete a property and unregister it. It will also be deleted from all clients.
void defineProperty(INumberVectorProperty *property)
bool isSimulation() const
void addAuxControls()
Add Debug, Simulation, and Configuration options to the driver.
virtual bool ISNewBLOB(const char *dev, const char *name, int sizes[], int blobsizes[], char *blobs[], char *formats[], char *names[], int n)
Process the client newBLOB command.
Connection::Interface * getActiveConnection()
virtual bool Connect()
Connect to the device. INDI::DefaultDevice implementation connects to appropriate connection interfac...
TelescopeStatus TrackState
void SetAxis1Park(double value)
SetRAPark Set current RA/AZ parking position. The data park file (stored in ~/.indi/ParkData....
void SetAxis1ParkDefault(double steps)
SetRAPark Set default RA/AZ parking position.
void SetTelescopeCapability(uint32_t cap, uint8_t slewRateCount)
SetTelescopeCapability sets the Telescope capabilities. All capabilities must be initialized.
virtual bool initProperties() override
Called to initialize basic properties required all the time.
virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override
Process the client newSwitch command.
void setTelescopeConnection(const uint8_t &value)
setTelescopeConnection Set telescope connection mode. Child class should call this in the constructor...
virtual bool updateProperties() override
Called when connected state changes, to add/remove properties.
virtual void SetParked(bool isparked)
SetParked Change the mount parking status. The data park file (stored in ~/.indi/ParkData....
Connection::TCP * tcpConnection
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
IGeographicCoordinates m_Location
void NewRaDec(double ra, double dec)
The child class calls this function when it has updates.
void setPierSide(TelescopePierSide side)
bool InitPark()
InitPark Loads parking data (stored in ~/.indi/ParkData.xml) that contains parking status and parking...
void SetAxis2Park(double steps)
SetDEPark Set current DEC/ALT parking position. The data park file (stored in ~/.indi/ParkData....
void setMountType(TelescopeMountType type)
setMountType Report the mount type in TELESCOPE_MOUNT_TYPE. Drivers call it when the type is fixed by...
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override
Process the client newSwitch command.
void SetParkDataType(TelescopeParkData type)
setParkDataType Sets the type of parking data stored in the park data file and presented to the user.
@ TELESCOPE_HAS_PIER_SIDE
void SetAxis2ParkDefault(double steps)
SetDEParkDefault Set default DEC/ALT parking position.
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
virtual bool initProperties() override
Called to initialize basic properties required all the time.
std::string HandsetFwVersion
virtual void initParking()
virtual bool Sync(double ra, double dec) override
Set the telescope current RA and DEC coordinates to the supplied RA and DEC coordinates.
virtual bool ISNewBLOB(const char *dev, const char *name, int sizes[], int blobsizes[], char *blobs[], char *formats[], char *names[], int n) override
Process the client newBLOB command.
std::string TrackingStatus
virtual bool Connect() override
Connect to the device. INDI::DefaultDevice implementation connects to appropriate connection interfac...
virtual bool updateTime(ln_date *utc, double utc_offset) override
Update telescope time, date, and UTC offset.
virtual bool SetDefaultPark() override
SetDefaultPark Set default coordinates/encoders value as the desired parking position.
virtual const char * getDefaultName() override
int HexStrToInteger(const std::string &str)
virtual bool Goto(double, double) override
Move the scope to the supplied RA and DEC coordinates.
virtual bool Park() override
Park the telescope to its home position.
virtual bool updateLocation(double latitude, double longitude, double elevation) override
Update telescope location settings.
virtual bool updateProperties() override
Called when connected state changes, to add/remove properties.
virtual bool SetCurrentPark() override
SetCurrentPark Set current coordinates/encoders value as the desired parking position.
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override
Process the client newSwitch command.
std::string AlignmentStatus
virtual bool SetSlewRate(int index) override
SetSlewRate Set desired slew rate index.
virtual bool MoveNS(INDI_DIR_NS dir, TelescopeMotionCommand command) override
Start or Stop the telescope motion in the direction dir.
virtual bool AnalyzeMount()
virtual bool UnPark() override
Unpark the telescope if already parked.
virtual bool ReadScopeStatus() override
Read telescope status.
virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override
Process the client newSwitch command.
std::string PointingStatus
virtual bool MoveWE(INDI_DIR_WE dir, TelescopeMotionCommand command) override
Move the telescope in the direction dir.
virtual bool Abort() override
Abort any telescope motion including tracking if possible.
INDI::PropertyNumber TrackRateNP
INDI::PropertySwitch SlewRateSP
INDI::PropertyText TimeTP
INDI::PropertyNumber LocationNP
int tty_write(int fd, const char *buf, int nbytes, int *nbytes_written)
Writes a buffer to fd.
int tty_read(int fd, char *buf, int nbytes, int timeout, int *nbytes_read)
read buffer from terminal
double range24(double r)
range24 Limits a number to be between 0-24 range.
double rangeDec(double decdegrees)
rangeDec Limits declination value to be in -90 to 90 range.
int fs_sexa(char *out, double a, int w, int fracbase)
Converts a sexagesimal number to a string. sprint the variable a in sexagesimal format into out[].
Implementations for common driver routines.
double get_local_sidereal_time(double longitude)
get_local_sidereal_time Returns local sidereal time given longitude and system clock.
void IUFillTextVector(ITextVectorProperty *tvp, IText *tp, int ntp, const char *dev, const char *name, const char *label, const char *group, IPerm p, double timeout, IPState s)
Assign attributes for a text vector property. The vector's auxiliary elements will be set to NULL.
void IUSaveText(IText *tp, const char *newtext)
Function to reliably save new text in a IText.
void IUFillText(IText *tp, const char *name, const char *label, const char *initialText)
Assign attributes for a text property. The text's auxiliary elements will be set to NULL.
int IUUpdateSwitch(ISwitchVectorProperty *svp, ISState *states, char *names[], int n)
Update all switches in a switch vector property.
void IDSetSwitch(const ISwitchVectorProperty *svp, const char *fmt,...)
void IDSetText(const ITextVectorProperty *tvp, const char *fmt,...)
*brief user defined string literal for JSON values *sa std::size_t n
#define LOGF_INFO(fmt,...)
#define LOGF_DEBUG(fmt,...)
#define LOG_ERROR(txt)
Shorter logging macros. In order to use these macros, the function (or method) "getDeviceName()" must...
#define LOGF_ERROR(fmt,...)
#define TRACKRATE_SIDEREAL
void J2000toObserved(IEquatorialCoordinates *J2000pos, double jd, IEquatorialCoordinates *observed)
*J2000toObserved converts catalogue to observed
void EquatorialToHorizontal(IEquatorialCoordinates *object, IGeographicCoordinates *observer, double JD, IHorizontalCoordinates *position)
EquatorialToHorizontal Calculate horizontal coordinates from equatorial coordinates.
void ObservedToJ2000(IEquatorialCoordinates *observed, double jd, IEquatorialCoordinates *J2000pos)
ObservedToJ2000 converts an observed position to a J2000 catalogue position removes aberration,...
*brief create JSON pointer *sa reference_tokens const string_t & a
*brief create JSON pointer *sa reference_tokens const string_t const string_t & b
#define SYNSCAN_SLEW_RATES