26#include <libnova/sidereal_time.h>
27#include <libnova/transform.h>
91 FirmwareTP[0].fill(
"VERSION",
"PWI4 Version",
"");
95 MountGeometryTP[0].fill(
"GEOMETRY",
"Mount Type",
"");
96 MountGeometryTP.fill(
getDeviceName(),
"MOUNT_GEOMETRY",
"Geometry",
100 AltAzNP[AZ_VALUE].fill(
"AZ",
"Azimuth (°)",
"%.4f", 0.0, 360.0, 0.0, 0.0);
101 AltAzNP[ALT_VALUE].fill(
"ALT",
"Altitude (°)",
"%.4f", -90.0, 90.0, 0.0, 0.0);
106 Axis0TelemetryNP[TELEMETRY_RMS_ERROR].fill(
107 "AXIS0_RMS",
"RMS Error (\")",
"%.3f", 0, 1000, 0, 0);
108 Axis0TelemetryNP[TELEMETRY_DIST_TO_TARGET].fill(
109 "AXIS0_DIST",
"Dist to Target (\")",
"%.3f", -1e6, 1e6, 0, 0);
110 Axis0TelemetryNP[TELEMETRY_SERVO_ERROR].fill(
111 "AXIS0_SERVO",
"Servo Error (\")",
"%.3f", -1e6, 1e6, 0, 0);
112 Axis0TelemetryNP[TELEMETRY_MOTOR_CURRENT].fill(
113 "AXIS0_CURRENT",
"Motor Current (A)",
"%.3f", 0, 100, 0, 0);
114 Axis0TelemetryNP.fill(
getDeviceName(),
"AXIS0_TELEMETRY",
"Axis 0 (AZ/RA) Telemetry",
118 Axis1TelemetryNP[TELEMETRY_RMS_ERROR].fill(
119 "AXIS1_RMS",
"RMS Error (\")",
"%.3f", 0, 1000, 0, 0);
120 Axis1TelemetryNP[TELEMETRY_DIST_TO_TARGET].fill(
121 "AXIS1_DIST",
"Dist to Target (\")",
"%.3f", -1e6, 1e6, 0, 0);
122 Axis1TelemetryNP[TELEMETRY_SERVO_ERROR].fill(
123 "AXIS1_SERVO",
"Servo Error (\")",
"%.3f", -1e6, 1e6, 0, 0);
124 Axis1TelemetryNP[TELEMETRY_MOTOR_CURRENT].fill(
125 "AXIS1_CURRENT",
"Motor Current (A)",
"%.3f", 0, 100, 0, 0);
126 Axis1TelemetryNP.fill(
getDeviceName(),
"AXIS1_TELEMETRY",
"Axis 1 (ALT/Dec) Telemetry",
130 DistanceToSunNP[0].fill(
"DIST_TO_SUN",
"Distance (°)",
"%.2f", 0, 180, 0, 0);
131 DistanceToSunNP.fill(
getDeviceName(),
"DISTANCE_TO_SUN",
"Sun Distance",
139 Axis0MotorSP.fill(
getDeviceName(),
"AXIS0_MOTOR",
"Axis 0 (AZ/RA) Motor",
145 Axis1MotorSP.fill(
getDeviceName(),
"AXIS1_MOTOR",
"Axis 1 (ALT/Dec) Motor",
149 FindHomeSP[0].fill(
"FIND_HOME",
"Find Home",
ISS_OFF);
156 ModelFilenameTP[0].fill(
"MODEL_FILE",
"Active Model",
"");
157 ModelFilenameTP.fill(
getDeviceName(),
"MODEL_FILENAME",
"Model File",
161 ModelInfoNP[MODEL_POINTS_TOTAL].fill(
"MODEL_TOTAL",
"Total Points",
"%.0f", 0, 10000, 1, 0);
162 ModelInfoNP[MODEL_POINTS_ENABLED].fill(
"MODEL_ENABLED",
"Enabled Points",
"%.0f", 0, 10000, 1, 0);
163 ModelInfoNP[MODEL_RMS_ERROR].fill(
"MODEL_RMS",
"RMS Error (\")",
"%.3f", 0, 10000, 0, 0);
164 ModelInfoNP.fill(
getDeviceName(),
"MODEL_INFO",
"Model Statistics",
168 ModelOperationSP[MODEL_SAVE_AS_DEFAULT].fill(
"MODEL_SAVE_DEFAULT",
"Save as Default",
ISS_OFF);
169 ModelOperationSP[MODEL_CLEAR_POINTS].fill(
"MODEL_CLEAR",
"Clear All Points",
ISS_OFF);
170 ModelOperationSP.fill(
getDeviceName(),
"MODEL_OPERATION",
"Model Operations",
174 ModelLoadFilenameTP[0].fill(
"MODEL_LOAD_FILE",
"Filename",
"DefaultModel.pxp");
175 ModelLoadFilenameTP.fill(
getDeviceName(),
"MODEL_LOAD",
"Load Model",
179 ModelSaveFilenameTP[0].fill(
"MODEL_SAVE_FILE",
"Filename",
"MyModel.pxp");
180 ModelSaveFilenameTP.fill(
getDeviceName(),
"MODEL_SAVE",
"Save Model",
185 SlewTimeConstantNP[0].fill(
"SLEW_TIME_CONSTANT",
"Time Constant (s)",
186 "%.2f", 0.1, 5.0, 0.1, 0.5);
187 SlewTimeConstantNP.fill(
getDeviceName(),
"MOUNT_SLEW_CONSTANT",
"Slew Aggressiveness",
271 m_HttpClient = std::make_unique<httplib::Client>(
tcpConnection->host(),
273 m_HttpClient->set_connection_timeout(5, 0);
274 m_HttpClient->set_read_timeout(10, 0);
279 LOG_ERROR(
"Cannot communicate with PWI4. Is PWI4 running and reachable?");
284 bool mountConnected = m_Status[
"mount.is_connected"].as<
bool>();
287 LOG_INFO(
"Mount not connected to PWI4. Sending /mount/connect …");
288 if (!dispatch(
"/mount/connect"))
290 LOG_ERROR(
"Failed to connect mount via PWI4.");
293 mountConnected = m_Status[
"mount.is_connected"].as<
bool>();
296 LOG_ERROR(
"Mount still not connected after /mount/connect.");
304 std::string version = m_Status[
"pwi4.version"].as<std::string>();
305 FirmwareTP[0].setText(version.c_str());
306 FirmwareTP.setState(
IPS_OK);
309 int geometry = m_Status[
"mount.geometry"].as<
int>();
310 const char *geomStr =
"Unknown";
318 geomStr =
"Equatorial Fork";
322 geomStr =
"German Equatorial";
326 MountGeometryTP[0].setText(geomStr);
327 MountGeometryTP.setState(
IPS_OK);
330 SlewTimeConstantNP[0].setValue(m_Status[
"mount.slew_time_constant"].as<double>());
331 SlewTimeConstantNP.setState(
IPS_OK);
333 LOGF_INFO(
"Connected to PWI4 %s — mount geometry: %s", version.c_str(), geomStr);
340bool PlaneWave::getStatus()
354 double ra = m_Status[
"mount.ra_apparent_hours"].as<
double>();
355 double de = m_Status[
"mount.dec_apparent_degs"].as<
double>();
358 bool isSlewing = m_Status[
"mount.is_slewing"].as<
bool>();
359 bool isTracking = m_Status[
"mount.is_tracking"].as<
bool>();
367 if (!isSlewing && !isTracking)
399 double az = m_Status[
"mount.azimuth_degs"].as<
double>();
400 double alt = m_Status[
"mount.altitude_degs"].as<
double>();
401 AltAzNP[AZ_VALUE].setValue(az);
402 AltAzNP[ALT_VALUE].setValue(alt);
409 int geometry = m_Status[
"mount.geometry"].as<
int>();
412 double axis0Pos = m_Status[
"mount.axis0.position_degs"].as<
double>();
418 Axis0TelemetryNP[TELEMETRY_RMS_ERROR].setValue(
419 m_Status[
"mount.axis0.rms_error_arcsec"].as<double>());
420 Axis0TelemetryNP[TELEMETRY_DIST_TO_TARGET].setValue(
421 m_Status[
"mount.axis0.dist_to_target_arcsec"].as<double>());
422 Axis0TelemetryNP[TELEMETRY_SERVO_ERROR].setValue(
423 m_Status[
"mount.axis0.servo_error_arcsec"].as<double>());
424 Axis0TelemetryNP[TELEMETRY_MOTOR_CURRENT].setValue(
425 m_Status[
"mount.axis0.measured_current_amps"].as<double>());
426 Axis0TelemetryNP.setState(
IPS_OK);
427 Axis0TelemetryNP.apply();
430 Axis1TelemetryNP[TELEMETRY_RMS_ERROR].setValue(
431 m_Status[
"mount.axis1.rms_error_arcsec"].as<double>());
432 Axis1TelemetryNP[TELEMETRY_DIST_TO_TARGET].setValue(
433 m_Status[
"mount.axis1.dist_to_target_arcsec"].as<double>());
434 Axis1TelemetryNP[TELEMETRY_SERVO_ERROR].setValue(
435 m_Status[
"mount.axis1.servo_error_arcsec"].as<double>());
436 Axis1TelemetryNP[TELEMETRY_MOTOR_CURRENT].setValue(
437 m_Status[
"mount.axis1.measured_current_amps"].as<double>());
438 Axis1TelemetryNP.setState(
IPS_OK);
439 Axis1TelemetryNP.apply();
442 bool axis0Enabled = m_Status[
"mount.axis0.is_enabled"].as<
bool>();
443 bool axis1Enabled = m_Status[
"mount.axis1.is_enabled"].as<
bool>();
447 Axis0MotorSP.setState(
IPS_OK);
448 Axis0MotorSP.apply();
452 Axis1MotorSP.setState(
IPS_OK);
453 Axis1MotorSP.apply();
456 double sunDist = m_Status[
"mount.distance_to_sun_degs"].as<
double>();
457 DistanceToSunNP[0].setValue(sunDist);
460 DistanceToSunNP.apply();
463 std::string modelFile = m_Status[
"mount.model.filename"].as<std::string>();
464 if (modelFile != std::string(ModelFilenameTP[0].
getText()))
466 ModelFilenameTP[0].setText(modelFile.c_str());
468 ModelFilenameTP.apply();
471 ModelInfoNP[MODEL_POINTS_TOTAL].setValue(
472 m_Status[
"mount.model.num_points_total"].as<double>());
473 ModelInfoNP[MODEL_POINTS_ENABLED].setValue(
474 m_Status[
"mount.model.num_points_enabled"].as<double>());
475 ModelInfoNP[MODEL_RMS_ERROR].setValue(
476 m_Status[
"mount.model.rms_error_arcsec"].as<double>());
477 ModelInfoNP.setState(
IPS_OK);
483 double pwi4Lat = m_Status[
"site.latitude_degs"].as<
double>();
484 double pwi4Lon = m_Status[
"site.longitude_degs"].as<
double>();
485 double pwi4Elv = m_Status[
"site.height_meters"].as<
double>();
504 std::ostringstream req;
505 req << std::fixed << std::setprecision(9)
506 <<
"/mount/goto_ra_dec_j2000?ra_hours=" <<
ra
507 <<
"&dec_degs=" <<
dec;
509 if (!dispatch(req.str()))
524 std::ostringstream req;
525 req << std::fixed << std::setprecision(9)
526 <<
"/mount/model/add_point?ra_j2000_hours=" <<
ra
527 <<
"&dec_j2000_degs=" <<
dec;
529 if (!dispatch(req.str()))
532 LOG_INFO(
"Sync: added a pointing-model calibration point at the current position.");
541 if (!dispatch(
"/mount/park"))
554 if (!enableAxis(0,
true))
556 LOG_ERROR(
"Failed to enable axis 0 motor.");
560 if (!enableAxis(1,
true))
562 LOG_ERROR(
"Failed to enable axis 1 motor.");
575 if (!dispatch(
"/mount/set_park_here"))
578 LOG_INFO(
"Park position set to current position.");
597 return dispatch(
"/mount/stop");
607 LOG_ERROR(
"Please unpark the mount before issuing any motion commands.");
611 double rate = getSlewRateArcsecPerSec();
615 std::ostringstream req;
616 req << std::fixed << std::setprecision(4);
619 req <<
"/mount/offset?dec_set_rate_arcsec_per_sec=" << rate;
621 req <<
"/mount/offset?dec_set_rate_arcsec_per_sec=0";
623 return dispatch(req.str());
633 LOG_ERROR(
"Please unpark the mount before issuing any motion commands.");
637 double rate = getSlewRateArcsecPerSec();
641 std::ostringstream req;
642 req << std::fixed << std::setprecision(4);
645 req <<
"/mount/offset?ra_set_rate_arcsec_per_sec=" << rate;
647 req <<
"/mount/offset?ra_set_rate_arcsec_per_sec=0";
649 return dispatch(req.str());
664 LOG_INFO(
"Note: site location is managed by PWI4. "
665 "INDI displays the location configured in PWI4.");
685 if (!dispatch(
"/mount/tracking_on"))
695 std::ostringstream req;
696 req << std::fixed << std::setprecision(6)
697 <<
"/mount/offset?ra_set_rate_arcsec_per_sec=" << dRA
698 <<
"&dec_set_rate_arcsec_per_sec=" << dDE;
700 return dispatch(req.str());
705 return dispatch(
"/mount/offset?ra_reset=0&dec_reset=0");
713 std::ostringstream req;
714 req << std::fixed << std::setprecision(6)
715 <<
"/mount/offset?ra_set_rate_arcsec_per_sec=" << raRate
716 <<
"&dec_set_rate_arcsec_per_sec=" << deRate;
718 return dispatch(req.str());
730 if (!dispatch(
"/mount/tracking_off"))
733 dispatch(
"/mount/offset?ra_reset=0&dec_reset=0");
743 SlewTimeConstantNP.save(fp);
755 if (Axis0MotorSP.isNameMatch(name))
757 Axis0MotorSP.update(states, names,
n);
759 if (enableAxis(0, enable))
761 LOGF_INFO(
"Axis 0 motor %s.", enable ?
"enabled" :
"disabled");
762 Axis0MotorSP.setState(
IPS_OK);
766 LOG_ERROR(
"Failed to change axis 0 motor state.");
769 Axis0MotorSP.apply();
774 if (Axis1MotorSP.isNameMatch(name))
776 Axis1MotorSP.update(states, names,
n);
778 if (enableAxis(1, enable))
780 LOGF_INFO(
"Axis 1 motor %s.", enable ?
"enabled" :
"disabled");
781 Axis1MotorSP.setState(
IPS_OK);
785 LOG_ERROR(
"Failed to change axis 1 motor state.");
788 Axis1MotorSP.apply();
793 if (FindHomeSP.isNameMatch(name))
795 if (dispatch(
"/mount/find_home"))
797 LOG_INFO(
"Home finding sequence started.");
802 LOG_ERROR(
"Failed to start home finding.");
806 FindHomeSP[0].setState(
ISS_OFF);
812 if (ModelOperationSP.isNameMatch(name))
814 ModelOperationSP.update(states, names,
n);
817 if (ModelOperationSP[MODEL_SAVE_AS_DEFAULT].getState() ==
ISS_ON)
819 ok = dispatch(
"/mount/model/save_as_default");
821 LOG_INFO(
"Pointing model saved as default.");
823 LOG_ERROR(
"Failed to save pointing model as default.");
825 else if (ModelOperationSP[MODEL_CLEAR_POINTS].getState() ==
ISS_ON)
827 ok = dispatch(
"/mount/model/clear_points");
829 LOG_INFO(
"All pointing model calibration points cleared.");
831 LOG_ERROR(
"Failed to clear pointing model points.");
835 ModelOperationSP[MODEL_SAVE_AS_DEFAULT].setState(
ISS_OFF);
836 ModelOperationSP[MODEL_CLEAR_POINTS].setState(
ISS_OFF);
838 ModelOperationSP.apply();
854 if (ModelLoadFilenameTP.isNameMatch(name))
856 ModelLoadFilenameTP.update(texts, names,
n);
857 const char *filename = ModelLoadFilenameTP[0].getText();
858 std::string req =
"/mount/model/load?filename=";
863 LOGF_INFO(
"Pointing model '%s' loaded.", filename);
864 ModelLoadFilenameTP.setState(
IPS_OK);
868 LOGF_ERROR(
"Failed to load pointing model '%s'.", filename);
871 ModelLoadFilenameTP.apply();
876 if (ModelSaveFilenameTP.isNameMatch(name))
878 ModelSaveFilenameTP.update(texts, names,
n);
879 const char *filename = ModelSaveFilenameTP[0].getText();
880 std::string req =
"/mount/model/save?filename=";
885 LOGF_INFO(
"Pointing model saved as '%s'.", filename);
886 ModelSaveFilenameTP.setState(
IPS_OK);
890 LOGF_ERROR(
"Failed to save pointing model as '%s'.", filename);
893 ModelSaveFilenameTP.apply();
909 if (SlewTimeConstantNP.isNameMatch(name))
911 SlewTimeConstantNP.update(values, names,
n);
912 double tc = SlewTimeConstantNP[0].getValue();
914 std::ostringstream req;
915 req << std::fixed << std::setprecision(3)
916 <<
"/mount/set_slew_time_constant?value=" << tc;
918 if (dispatch(req.str()))
920 LOGF_INFO(
"Slew time constant set to %.2f s.", tc);
921 SlewTimeConstantNP.setState(
IPS_OK);
925 LOG_ERROR(
"Failed to set slew time constant.");
928 SlewTimeConstantNP.apply();
939bool PlaneWave::enableAxis(
int axis,
bool enable)
941 std::ostringstream req;
942 req << (enable ?
"/mount/enable?axis=" :
"/mount/disable?axis=") << axis;
949double PlaneWave::getSlewRateArcsecPerSec()
const
952 if (idx >= 0 && idx < 4)
953 return SLEW_RATES[idx];
954 return SLEW_RATES[3];
964bool PlaneWave::dispatch(
const std::string &request)
968 LOG_ERROR(
"HTTP client not initialized. Is the mount connected?");
972 auto res = m_HttpClient->Get(request);
982 if (
res->status != 200)
984 LOGF_ERROR(
"HTTP GET %s returned HTTP %d: %s",
985 request.c_str(),
res->status,
986 res->body.empty() ?
"(no body)" :
res->body.c_str());
990 if (
res->body.empty())
992 LOGF_ERROR(
"HTTP GET %s returned an empty body.", request.c_str());
1003 if (inif[
"status"].size() == 0)
1005 LOGF_ERROR(
"Parsed status for %s is empty.", request.c_str());
1009 m_Status = inif[
"status"];
1012 catch (
const std::exception &e)
1014 LOGF_ERROR(
"Failed to parse PWI4 response for %s: %s",
1015 request.c_str(), e.what());
INDI::PropertyText getText(const char *name) const
void setVersion(uint16_t vMajor, uint16_t vMinor)
Set driver version information to be defined in DRIVER_INFO property as vMajor.vMinor.
virtual bool deleteProperty(const char *propertyName)
Delete a property and unregister it. It will also be deleted from all clients.
void defineProperty(INumberVectorProperty *property)
void addAuxControls()
Add Debug, Simulation, and Configuration options to the driver.
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 int AddTrackMode(const char *name, const char *label, bool isDefault=false)
AddTrackMode.
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.
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...
virtual bool saveConfigItems(FILE *fp) override
saveConfigItems Save specific properties in the provide config file handler. Child class usually over...
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_TRACK_RATE
@ TELESCOPE_HAS_PIER_SIDE
@ TELESCOPE_HAS_TRACK_MODE
void SetAxis2ParkDefault(double steps)
SetDEParkDefault Set default DEC/ALT parking position.
virtual bool UnPark() override
UnPark — re-enable both motors before clearing the parked state.
virtual bool Abort() override
Abort.
virtual bool updateLocation(double latitude, double longitude, double elevation) override
virtual bool Sync(double ra, double dec) override
Sync — adds a pointing-model calibration point at the current position.
virtual bool Goto(double ra, double dec) override
Goto — J2000 RA/Dec (INDI convention).
virtual bool SetTrackRate(double raRate, double deRate) override
SetTrackRate — called when the user changes TrackRateNP.
virtual bool Park() override
Park.
virtual bool updateProperties() override
updateProperties
virtual bool SetDefaultPark() override
SetDefaultPark.
virtual bool Handshake() override
Handshake — build the persistent client and verify the mount is live.
virtual bool initProperties() override
initProperties
virtual const char * getDefaultName() override
virtual bool MoveNS(INDI_DIR_NS dir, TelescopeMotionCommand command) override
MoveNS — jog in Dec using a continuous offset rate.
virtual bool SetTrackEnabled(bool enabled) override
SetTrackEnabled.
virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override
ISNewText.
virtual bool updateTime(ln_date *utc, double utc_offset) override
updateTime — PWI4 uses the host PC clock; nothing to push.
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
ISNewNumber.
PlaneWave()
Constructor / Destructor.
virtual bool MoveWE(INDI_DIR_WE dir, TelescopeMotionCommand command) override
MoveWE — jog in RA using a continuous offset rate.
virtual bool SetTrackMode(uint8_t mode) override
SetTrackMode.
virtual bool saveConfigItems(FILE *fp) override
saveConfigItems
virtual bool ReadScopeStatus() override
ReadScopeStatus — called every polling interval.
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override
ISNewSwitch.
virtual bool SetCurrentPark() override
SetCurrentPark — tell PWI4 to store the current position as the park point.
void decode(std::istream &is)
const char * MAIN_CONTROL_TAB
MAIN_CONTROL_TAB Where all the primary controls for the device are located.
const char * INFO_TAB
INFO_TAB Where all the properties for general information are located.
const char * OPTIONS_TAB
OPTIONS_TAB Where all the driver's options are located. Those may include auxiliary controls,...
INDI::PropertyNumber TrackRateNP
INDI::PropertySwitch SlewRateSP
INDI::PropertySwitch TrackModeSP
INDI::PropertyNumber LocationNP
Implementations for common driver routines.
int dispatch(XMLEle *root, char msg[])
*brief user defined string literal for JSON values *sa std::size_t n
#define LOGF_INFO(fmt,...)
#define LOG_ERROR(txt)
Shorter logging macros. In order to use these macros, the function (or method) "getDeviceName()" must...
#define LOGF_ERROR(fmt,...)
std::string to_string(Error error)
IniFileBase< std::less< std::string > > IniFile
std::unique_ptr< PlaneWave > PlaneWave_mount(new PlaneWave())