Instrument Neutral Distributed Interface INDI 2.2.5
test_telescope_simulator.cpp
Go to the documentation of this file.
1/*
2 Regression test for the telescope simulator: alignment sync points recorded for one mount
3 geometry must not be reused after switching between ALTAZ and an equatorial mount type.
4
5 SPDX-License-Identifier: LGPL-2.1-or-later
6*/
7
8#include <gtest/gtest.h>
9
10#include <cstdio>
11#include <cstdlib>
12#include <string>
13#include <unistd.h>
14
15#include "indilogger.h"
16#include "lilxml.h"
18
19char _me[] = "MockScopeSimDriver";
20char *me = _me;
21
23
25{
26 public:
32
33 // Select a mount type through the regular client path (TELESCOPE_MOUNT_TYPE switch).
34 void selectMountType(const char *type)
35 {
36 const char *types[] = { "ALTAZ", "EQ_FORK", "EQ_GEM" };
37 ISState states[3];
38 char *names[3];
39 for (int i = 0; i < 3; i++)
40 {
41 states[i] = strcmp(types[i], type) == 0 ? ISS_ON : ISS_OFF;
42 names[i] = const_cast<char *>(types[i]);
43 }
44 ASSERT_TRUE(ISNewSwitch(getDeviceName(), "TELESCOPE_MOUNT_TYPE", states, names, 3));
45 }
46
47 bool syncAt(double ra, double dec)
48 {
49 return Sync(ra, dec);
50 }
51
52 void setLocation(double latitude, double longitude)
53 {
54 updateLocation(latitude, longitude, 200);
55 }
56
58 {
59 return GetAlignmentDatabase().back();
60 }
61
63 {
65 entry.ObservationJulianDate = 2461300.5;
66 entry.RightAscension = 9.5;
67 entry.Declination = 45.0;
68 entry.PrivateDataSize = 0;
69 GetAlignmentDatabase().push_back(entry);
70 }
71
72 size_t syncPoints()
73 {
74 return GetAlignmentDatabase().size();
75 }
76
77 // Deliver a property update from another driver, as indiserver does for snooped properties.
78 void snoop(const std::string &xml)
79 {
80 LilXML *lp = newLilXML();
81 char errmsg[MAXRBUF] = {0};
82 std::string buffer = xml;
83 XMLEle **nodes = parseXMLChunk(lp, buffer.data(), static_cast<int>(buffer.size()), errmsg);
84 ASSERT_NE(nodes, nullptr) << errmsg;
85 for (int i = 0; nodes[i] != nullptr; i++)
86 {
87 ISSnoopDevice(nodes[i]);
88 delXMLEle(nodes[i]);
89 }
90 free(nodes);
91 delLilXML(lp);
92 }
93
94 void setActivePAC(const char *device)
95 {
96 char *texts[] = { const_cast<char *>(device) };
97 char *names[] = { const_cast<char *>("PAC_DEVICE") };
98 ASSERT_TRUE(ISNewText(getDeviceName(), "ACTIVE_PAC", texts, names, 1));
99 }
100
101 void setPolarError(double ma, double me)
102 {
103 double values[] = { ma, me };
104 char *names[] = { const_cast<char *>("MM_MA"), const_cast<char *>("MM_ME") };
105 ASSERT_TRUE(ISNewNumber(getDeviceName(), "MOUNT_MODEL", values, names, 2));
106 }
107
108 double mountModel(const char *element)
109 {
110 return getNumber("MOUNT_MODEL").findWidgetByName(element)->getValue();
111 }
112};
113
114TEST(TelescopeSimulatorTest, switchingBetweenAltAzAndEquatorialClearsSyncPoints)
115{
117
118 sim.selectMountType("EQ_GEM");
119 sim.addSyncPoint();
120 sim.addSyncPoint();
121 ASSERT_EQ(sim.syncPoints(), 2u);
122
123 // Equatorial -> ALTAZ: RA/Dec-encoded points are meaningless for an Az/Alt mount.
124 sim.selectMountType("ALTAZ");
125 EXPECT_EQ(sim.syncPoints(), 0u);
126
127 // ALTAZ -> equatorial: Az/Alt-encoded points are meaningless for an equatorial mount.
128 sim.addSyncPoint();
129 sim.selectMountType("EQ_FORK");
130 EXPECT_EQ(sim.syncPoints(), 0u);
131}
132
133TEST(TelescopeSimulatorTest, switchingBetweenEquatorialTypesKeepsSyncPoints)
134{
136
137 sim.selectMountType("EQ_GEM");
138 sim.addSyncPoint();
139 ASSERT_EQ(sim.syncPoints(), 1u);
140
141 // EQ_GEM and EQ_FORK use the same encoder RA/Dec encoding.
142 sim.selectMountType("EQ_FORK");
143 EXPECT_EQ(sim.syncPoints(), 1u);
144}
145
146// Re-syncing at (practically) the same place must update the pointing model with the new plate-solved
147// position. Previously the new sync point was silently dropped as a duplicate while Sync() still reported
148// success, so the old position stayed in the model.
149static void checkResyncReplacesOlderPoint(const char *mountType)
150{
152 sim.setLocation(48.2, 16.4);
153 sim.selectMountType(mountType);
154
155 ASSERT_TRUE(sim.syncAt(9.50, 45.00));
156 ASSERT_EQ(sim.syncPoints(), 1u);
157
158 // Same mount position, slightly different solved sky position.
159 ASSERT_TRUE(sim.syncAt(9.51, 45.05));
160 ASSERT_EQ(sim.syncPoints(), 1u);
161 EXPECT_NEAR(sim.lastSyncPoint().RightAscension, 9.51, 1e-9);
162 EXPECT_NEAR(sim.lastSyncPoint().Declination, 45.05, 1e-9);
163}
164
165TEST(TelescopeSimulatorTest, resyncReplacesOlderSyncPointEquatorial)
166{
167 checkResyncReplacesOlderPoint("EQ_GEM");
168}
169
170// No ALTAZ variant here: a freshly constructed simulator points at the zenith, and the math plugin's
171// SanitizePolarEntries() deliberately rewrites sync points above 88 deg altitude, so a zenith sync point
172// cannot be compared directly. The ALTAZ path shares the replacement logic in ScopeSim::Sync().
173
174// ---------------------------------------------------------------------------------------------------------
175// Polar alignment corrector (PAC): the simulator snoops PAC_MANUAL_ADJUSTMENT and moves its polar axis.
176// ---------------------------------------------------------------------------------------------------------
177
178namespace
179{
180const char *PAC = "Alignment Correction Simulator";
181
182std::string manualAdjustment(const char *state, double azStep, double altStep)
183{
184 return std::string("<setNumberVector device='") + PAC + "' name='PAC_MANUAL_ADJUSTMENT' state='" + state + "'>"
185 "<oneNumber name='MANUAL_AZ_STEP'>" + std::to_string(azStep) + "</oneNumber>"
186 "<oneNumber name='MANUAL_ALT_STEP'>" + std::to_string(altStep) + "</oneNumber>"
187 "</setNumberVector>";
188}
189
190std::string correctionGain(double percent)
191{
192 return std::string("<setNumberVector device='") + PAC + "' name='CORRECTION_GAIN' state='Ok'>"
193 "<oneNumber name='GAIN'>" + std::to_string(percent) + "</oneNumber></setNumberVector>";
194}
195
196constexpr double LATITUDE = 51.4683;
197}
198
199// The PAA reports the azimuth error in degrees of azimuth. Near the pole that is 1/cos(latitude) of the
200// angle on the sky that MM_MA measures, so a full correction of the reported error removes MM_MA exactly.
201TEST(TelescopeSimulatorTest, pacMoveCorrectsThePolarAxis)
202{
204 sim.setLocation(LATITUDE, 0);
205 sim.setActivePAC(PAC);
206 sim.setPolarError(30, -20);
207
208 // MM_MA = 30' (axis west) reads as an azimuth error of -30'/cos(lat), MM_ME = -20' (axis high) as an
209 // altitude error of +20'. The client sends the correction, the negative of the error: move East and down.
210 // The XML carries 6 decimals, about 4e-6 arcmin.
211 const double azStep = 30.0 / std::cos(LATITUDE * M_PI / 180) / 60.0;
212 sim.snoop(manualAdjustment("Busy", azStep, -20.0 / 60));
213 EXPECT_NEAR(sim.mountModel("MM_MA"), 0, 1e-4);
214 EXPECT_NEAR(sim.mountModel("MM_ME"), 0, 1e-4);
215}
216
217// The move takes effect when the PAC starts it, once. Clients take the next frame as soon as the PAC reports
218// Ok, so applying the move on Ok left that frame at the old position.
219TEST(TelescopeSimulatorTest, pacMoveIsAppliedOnceWhenItStarts)
220{
222 sim.setLocation(LATITUDE, 0);
223 sim.setActivePAC(PAC);
224 sim.setPolarError(0, 0);
225
226 // Moving the axis up by 30' leaves it 30' high (MM_ME = -30')
227 sim.snoop(manualAdjustment("Busy", 0, 0.5));
228 EXPECT_NEAR(sim.mountModel("MM_ME"), -30, 1e-4);
229
230 // Further updates of the same move, and its completion, do not move the axis again
231 sim.snoop(manualAdjustment("Busy", 0, 0.5));
232 sim.snoop(manualAdjustment("Ok", 0, 0.5));
233 EXPECT_NEAR(sim.mountModel("MM_ME"), -30, 1e-4);
234
235 // A move the driver completes at once (Ok without Busy, e.g. 0/0) moves nothing
236 sim.snoop(manualAdjustment("Ok", 0, 0.5));
237 EXPECT_NEAR(sim.mountModel("MM_ME"), -30, 1e-4);
238
239 // The next move is applied again
240 sim.snoop(manualAdjustment("Busy", 0, -0.5));
241 EXPECT_NEAR(sim.mountModel("MM_ME"), 0, 1e-4);
242}
243
244// The PAC simulator's gain simulates a corrector that moves less than requested.
245TEST(TelescopeSimulatorTest, pacMoveFollowsTheCorrectionGain)
246{
248 sim.setLocation(LATITUDE, 0);
249 sim.setActivePAC(PAC);
250 sim.setPolarError(0, 0);
251
252 sim.snoop(correctionGain(50));
253 sim.snoop(manualAdjustment("Busy", 0, 0.5));
254 EXPECT_NEAR(sim.mountModel("MM_ME"), -15, 1e-4);
255 sim.snoop(manualAdjustment("Ok", 0, 0.5));
256
257 // Selecting another PAC resets the gain to a full move
258 sim.setActivePAC("Other PAC");
259 sim.setActivePAC(PAC);
260 sim.snoop(manualAdjustment("Busy", 0, 0.5));
261 EXPECT_NEAR(sim.mountModel("MM_ME"), -45, 1e-4);
262}
263
264int main(int argc, char **argv)
265{
266 // Never read or write the user's real ~/.indi driver configuration.
267 // Use a file private to this process so that test runs can happen in parallel.
268 const std::string configFile = "/tmp/test_telescope_simulator_" + std::to_string(getpid()) + "_config.xml";
269 setenv("INDICONFIG", configFile.c_str(), 1);
272 ::testing::InitGoogleTest(&argc, argv);
273 int rc = RUN_ALL_TESTS();
274 std::remove(configFile.c_str());
275 std::remove((configFile + ".default").c_str());
276 return rc;
277}
hid_device * device
char _me[]
AlignmentDatabaseType & GetAlignmentDatabase()
Get a reference to the in memory database.
INDI::PropertyNumber getNumber(const char *name) const
static const loggerConf file_off
Definition indilogger.h:219
void configure(const std::string &outputFile, const loggerConf configuration, const int fileVerbosityLevel, const int screenVerbosityLevel)
Method to configure the logger. Called by the DEBUG_CONF() macro. To make implementation easier,...
static Logger & getInstance()
Method to get a reference to the object (i.e., Singleton) It is a static method.
WidgetView< T > * findWidgetByName(const char *name) const
void setActivePAC(const char *device)
double mountModel(const char *element)
bool syncAt(double ra, double dec)
void setLocation(double latitude, double longitude)
void snoop(const std::string &xml)
void setPolarError(double ma, double me)
void selectMountType(const char *type)
const AlignmentDatabaseEntry & lastSyncPoint()
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override
Process the client newSwitch command.
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
virtual bool Sync(double ra, double dec) override
Set the telescope current RA and DEC coordinates to the supplied RA and DEC coordinates.
virtual void ISGetProperties(const char *dev) override
define the driver's properties to the client. Usually, only a minimum set of properties are defined t...
virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override
Process the client newSwitch command.
virtual bool updateLocation(double latitude, double longitude, double elevation) override
Update telescope location settings.
virtual bool ISSnoopDevice(XMLEle *root) override
Process a snoop event from INDI server. This function is called when a snooped property is updated in...
virtual bool initProperties() override
Called to initialize basic properties required all the time.
#define M_PI
Definition dsp.h:52
int main()
double ra
double dec
ISState
Switch state.
Definition indiapi.h:148
@ ISS_OFF
Definition indiapi.h:149
@ ISS_ON
Definition indiapi.h:150
#define MAXRBUF
Definition indidriver.c:52
char * me
Definition indidriver.c:50
#define getDeviceName
LilXML * newLilXML()
Create a new lilxml parser.
Definition lilxml.cpp:150
XMLEle ** parseXMLChunk(LilXML *lp, char *buf, int size, char ynot[])
Process an XML chunk.
Definition lilxml.cpp:215
void delXMLEle(XMLEle *ep)
delXMLEle Delete XML element.
Definition lilxml.cpp:167
void delLilXML(LilXML *lp)
Delete a lilxml parser.
Definition lilxml.cpp:159
A little DOM-style library to handle parsing and processing an XML file.
struct LilXML_ LilXML
Definition lilxml.h:82
struct xml_ele_ XMLEle
Definition lilxml.h:81
uint16_t type
Definition pwc-ioctl.h:0
Entry in the in memory alignment database.
Definition Common.h:152
double RightAscension
Right ascension in decimal hours. N.B. libnova works in decimal degrees so conversion is always neede...
Definition Common.h:190
double Declination
Declination in decimal degrees.
Definition Common.h:193
int PrivateDataSize
This size in bytes of any private data.
Definition Common.h:203
TEST(TelescopeSimulatorTest, switchingBetweenAltAzAndEquatorialClearsSyncPoints)