Instrument Neutral Distributed Interface INDI 2.2.5
lx200basic.cpp
Go to the documentation of this file.
1#if 0
2LX200 Basic Driver
3Copyright (C) 2015 Jasem Mutlaq ([email protected])
4
5This library is free software;
6you can redistribute it and / or
7modify it under the terms of the GNU Lesser General Public
8License as published by the Free Software Foundation;
9either
10version 2.1 of the License, or (at your option) any later version.
11
12This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY;
14without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16Lesser General Public License for more details.
17
18You should have received a copy of the GNU Lesser General Public
19License along with this library;
20if not, write to the Free Software
21Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 - 1301 USA
22
23#endif
24
25#include "lx200basic.h"
26
27#include "indicom.h"
28#include "lx200driver.h"
29
30#include <libnova/sidereal_time.h>
31
32#include <cmath>
33#include <memory>
34#include <cstring>
35#include <unistd.h>
36
37/* Simulation Parameters */
38#define SLEWRATE 1 /* slew rate, degrees/s */
39#define SIDRATE 0.004178 /* sidereal rate, degrees/s */
40
41/* Our telescope auto pointer */
42static std::unique_ptr<LX200Basic> telescope(new LX200Basic());
43
44/**************************************************************************************
45** LX200 Basic constructor
46***************************************************************************************/
48{
49 setVersion(2, 1);
50
51 DBG_SCOPE = INDI::Logger::getInstance().addDebugLevel("Scope Verbose", "SCOPE");
52
54
55 LOG_DEBUG("Initializing from LX200 Basic device...");
56}
57
58/**************************************************************************************
59**
60***************************************************************************************/
62{
63 INDI_UNUSED(enable);
65}
66
67/**************************************************************************************
68**
69***************************************************************************************/
71{
72 return "LX200 Basic";
73}
74
75/**************************************************************************************
76**
77***************************************************************************************/
79{
80 /* Make sure to init parent properties first */
82
83 // Slew threshold
84 IUFillNumber(&SlewAccuracyN[0], "SlewRA", "RA (arcmin)", "%10.6m", 0., 60., 1., 3.0);
85 IUFillNumber(&SlewAccuracyN[1], "SlewDEC", "Dec (arcmin)", "%10.6m", 0., 60., 1., 3.0);
88
90
92
94 currentDEC = LocationNP[LOCATION_LATITUDE].getValue() > 0 ? 90 : -90;
95
96 return true;
97}
98
99/**************************************************************************************
100**
101***************************************************************************************/
103{
105
106 if (isConnected())
107 {
109
110 // We don't support NSWE controls
113
114 getBasicData();
115 }
116 else
117 {
119 }
120
121 return true;
122}
123
124/**************************************************************************************
125**
126***************************************************************************************/
128{
129 if (getLX200RA(PortFD, &currentRA) != 0)
130 {
131 LOG_ERROR("Error communication with telescope.");
132 return false;
133 }
134
135 return true;
136}
137
138/**************************************************************************************
139**
140***************************************************************************************/
142{
143 const double dx = targetRA - currentRA;
144 const double dy = targetDEC - currentDEC;
145 return fabs(dx) <= (SlewAccuracyN[0].value / (900.0)) && fabs(dy) <= (SlewAccuracyN[1].value / 60.0);
146}
147
148/**************************************************************************************
149**
150***************************************************************************************/
152{
153 if (!isConnected())
154 return false;
155
156 if (isSimulation())
157 {
158 mountSim();
159 return true;
160 }
161
163 {
164 EqNP.setState(IPS_ALERT);
165 LOG_ERROR("Error reading RA/DEC.");
166 EqNP.apply();
167 return false;
168 }
169
171 {
172 // Check if LX200 is done slewing
173 if (isSlewComplete())
174 {
176 LOG_INFO("Slew is complete. Tracking...");
177 }
178 }
179
181
182 return true;
183}
184
185/**************************************************************************************
186**
187***************************************************************************************/
188bool LX200Basic::Goto(double r, double d)
189{
190 targetRA = r;
191 targetDEC = d;
192 char RAStr[64] = {0}, DecStr[64] = {0};
193
194 fs_sexa(RAStr, targetRA, 2, 3600);
195 fs_sexa(DecStr, targetDEC, 2, 3600);
196
197 // If moving, let's stop it first.
198 if (EqNP.getState() == IPS_BUSY)
199 {
200 if (!isSimulation() && abortSlew(PortFD) < 0)
201 {
202 AbortSP.setState(IPS_ALERT);
203 LOG_ERROR("Abort slew failed.");
204 AbortSP.apply();
205 return false;
206 }
207
208 AbortSP.setState(IPS_OK);
209 EqNP.setState(IPS_IDLE);
210 LOG_ERROR("Slew aborted.");
211 AbortSP.apply();
212 EqNP.apply();
213
214 // sleep for 100 mseconds
215 usleep(100000);
216 }
217
218 if (!isSimulation())
219 {
221 {
222 EqNP.setState(IPS_ALERT);
223 LOG_ERROR("Error setting RA/DEC.");
224 EqNP.apply();
225 return false;
226 }
227
228 int err = 0;
229
230 /* Slew reads the '0', that is not the end of the slew */
231 if ((err = Slew(PortFD)))
232 {
233 EqNP.setState(IPS_ALERT);
234 LOGF_ERROR("Error Slewing to JNow RA %s - DEC %s", RAStr, DecStr);
235 EqNP.apply();
236 slewError(err);
237 return false;
238 }
239 }
240
242 //EqNP.s = IPS_BUSY;
243
244 LOGF_INFO("Slewing to RA: %s - DEC: %s", RAStr, DecStr);
245 return true;
246}
247
248/**************************************************************************************
249**
250***************************************************************************************/
251bool LX200Basic::Sync(double ra, double dec)
252{
253 char syncString[256] = {0};
254
255 if (!isSimulation() && (setObjectRA(PortFD, ra) < 0 || (setObjectDEC(PortFD, dec)) < 0))
256 {
257 EqNP.setState(IPS_ALERT);
258 LOG_ERROR("Error setting RA/DEC. Unable to Sync.");
259 EqNP.apply();
260 return false;
261 }
262
263 if (!isSimulation() && ::Sync(PortFD, syncString) < 0)
264 {
265 EqNP.setState(IPS_ALERT);
266 LOG_ERROR("Synchronization failed.");
267 EqNP.apply();
268 return false;
269 }
270
271 currentRA = ra;
272 currentDEC = dec;
273
274 LOG_INFO("Synchronization successful.");
275
276 EqNP.setState(IPS_OK);
277
279
280 return true;
281}
282
283/**************************************************************************************
284**
285***************************************************************************************/
286bool LX200Basic::ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
287{
288 if (dev != nullptr && strcmp(dev, getDeviceName()) == 0)
289 {
290 if (!strcmp(name, SlewAccuracyNP.name))
291 {
292 if (IUUpdateNumber(&SlewAccuracyNP, values, names, n) < 0)
293 return false;
294
296
297 if (SlewAccuracyN[0].value < 3 || SlewAccuracyN[1].value < 3)
298 IDSetNumber(&SlewAccuracyNP, "Warning: Setting the slew accuracy too low may result in a dead lock");
299
300 IDSetNumber(&SlewAccuracyNP, nullptr);
301 return true;
302 }
303 }
304
305 return INDI::Telescope::ISNewNumber(dev, name, values, names, n);
306}
307
308/**************************************************************************************
309**
310***************************************************************************************/
312{
313 if (!isSimulation() && abortSlew(PortFD) < 0)
314 {
315 LOG_ERROR("Failed to abort slew.");
316 return false;
317 }
318
319 EqNP.setState(IPS_IDLE);
321 EqNP.apply();
322
323 LOG_INFO("Slew aborted.");
324 return true;
325}
326
327/**************************************************************************************
328**
329***************************************************************************************/
331{
332 // Make sure short
334
335 // Get current RA/DEC
338
339 EqNP.apply();
340}
341
342/**************************************************************************************
343**
344***************************************************************************************/
346{
347 static struct timeval ltv;
348 struct timeval tv;
349 double dt, da, dx;
350 int nlocked;
351
352 /* update elapsed time since last poll, don't presume exactly POLLMS */
353 gettimeofday(&tv, nullptr);
354
355 if (ltv.tv_sec == 0 && ltv.tv_usec == 0)
356 ltv = tv;
357
358 dt = tv.tv_sec - ltv.tv_sec + (tv.tv_usec - ltv.tv_usec) / 1e6;
359 ltv = tv;
360 da = SLEWRATE * dt;
361
362 /* Process per current state. We check the state of EQUATORIAL_COORDS and act accordingly */
363 switch (TrackState)
364 {
365 case SCOPE_TRACKING:
366 /* RA moves at sidereal, Dec stands still */
367 currentRA += (SIDRATE * dt / 15.);
368 break;
369
370 case SCOPE_SLEWING:
371 /* slewing - nail it when both within one pulse @ SLEWRATE */
372 nlocked = 0;
373
374 dx = targetRA - currentRA;
375
376 if (fabs(dx) <= da)
377 {
379 nlocked++;
380 }
381 else if (dx > 0)
382 currentRA += da / 15.;
383 else
384 currentRA -= da / 15.;
385
386 dx = targetDEC - currentDEC;
387 if (fabs(dx) <= da)
388 {
390 nlocked++;
391 }
392 else if (dx > 0)
393 currentDEC += da;
394 else
395 currentDEC -= da;
396
397 if (nlocked == 2)
398 {
400 }
401
402 break;
403
404 default:
405 break;
406 }
407
409}
410
411/**************************************************************************************
412**
413***************************************************************************************/
414void LX200Basic::slewError(int slewCode)
415{
416 EqNP.setState(IPS_ALERT);
417
418 if (slewCode == 1)
419 {
420 LOG_INFO("Object below horizon");
421 EqNP.apply();
422 }
423 else if (slewCode == 2)
424 {
425 LOG_INFO("Object below the minimum elevation limit.");
426 EqNP.apply();
427 }
428 else
429 {
430 LOG_INFO("Slew failed.");
431 EqNP.apply();
432 }
433}
434
435/**************************************************************************************
436**
437***************************************************************************************/
439{
442 return true;
443}
bool isConnected() 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)
bool isSimulation() const
void addAuxControls()
Add Debug, Simulation, and Configuration options to the driver.
static Logger & getInstance()
Method to get a reference to the object (i.e., Singleton) It is a static method.
int addDebugLevel(const char *debugLevelName, const char *LoggingLevelName)
Adds a new debugging level to the driver.
TelescopeStatus TrackState
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 updateProperties() override
Called when connected state changes, to add/remove properties.
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.
virtual bool saveConfigItems(FILE *fp) override
saveConfigItems Save specific properties in the provide config file handler. Child class usually over...
void setMountType(TelescopeMountType type)
setMountType Report the mount type in TELESCOPE_MOUNT_TYPE. Drivers call it when the type is fixed by...
virtual bool saveConfigItems(FILE *fp) override
saveConfigItems Save specific properties in the provide config file handler. Child class usually over...
bool isSlewComplete()
void getBasicData()
virtual bool Handshake() override
perform handshake with device to check communication
double currentRA
Definition lx200basic.h:57
double currentDEC
Definition lx200basic.h:57
virtual bool updateProperties() override
Called when connected state changes, to add/remove properties.
double targetRA
Definition lx200basic.h:56
virtual const char * getDefaultName() override
INumber SlewAccuracyN[2]
Definition lx200basic.h:53
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.
void slewError(int slewCode)
virtual bool Abort() override
Abort any telescope motion including tracking if possible.
uint32_t DBG_SCOPE
Definition lx200basic.h:58
double targetDEC
Definition lx200basic.h:56
virtual bool Goto(double, double) override
Move the scope to the supplied RA and DEC coordinates.
virtual void debugTriggered(bool enable) override
Inform driver that the debug option was triggered. This function is called after setDebug is triggere...
INumberVectorProperty SlewAccuracyNP
Definition lx200basic.h:54
virtual bool ReadScopeStatus() override
Read telescope status.
void mountSim()
virtual bool initProperties() override
Called to initialize basic properties required all the time.
const char * OPTIONS_TAB
OPTIONS_TAB Where all the driver's options are located. Those may include auxiliary controls,...
INDI::PropertySwitch AbortSP
INDI::PropertySwitch MovementNSSP
INDI::PropertyNumber EqNP
INDI::PropertySwitch MovementWESP
INDI::PropertyNumber LocationNP
#define SLEWRATE
Definition ieqpro.cpp:35
double ra
double dec
#define NARRAY(a)
Handy macro to find the number of elements in array a[]. Must be used with actual array,...
Definition indiapi.h:498
@ IP_RW
Definition indiapi.h:184
@ IPS_BUSY
Definition indiapi.h:161
@ IPS_ALERT
Definition indiapi.h:162
@ IPS_IDLE
Definition indiapi.h:159
@ IPS_OK
Definition indiapi.h:160
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[].
Definition indicom.c:130
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 IUFillNumberVector(INumberVectorProperty *nvp, INumber *np, int nnp, const char *dev, const char *name, const char *label, const char *group, IPerm p, double timeout, IPState s)
Assign attributes for a number vector property. The vector's auxiliary elements will be set to NULL.
Definition indidevapi.c:490
void IUSaveConfigNumber(FILE *fp, const INumberVectorProperty *nvp)
Add a number vector property value to the configuration file.
Definition indidevapi.c:23
void IUFillNumber(INumber *np, const char *name, const char *label, const char *format, double min, double max, double step, double value)
Assign attributes for a number property. The number's auxiliary elements will be set to NULL.
Definition indidevapi.c:398
#define INDI_UNUSED(x)
Definition indidevapi.h:132
void IDSetNumber(const INumberVectorProperty *nvp, const char *fmt,...)
int IUUpdateNumber(INumberVectorProperty *nvp, double values[], char *names[], int n)
Update all numbers in a number vector property.
*brief user defined string literal for JSON values *sa std::size_t n
#define LOGF_INFO(fmt,...)
Definition indilogger.h:82
#define LOG_DEBUG(txt)
Definition indilogger.h:75
#define LOG_ERROR(txt)
Shorter logging macros. In order to use these macros, the function (or method) "getDeviceName()" must...
Definition indilogger.h:72
#define LOGF_ERROR(fmt,...)
Definition indilogger.h:80
#define LOG_INFO(txt)
Definition indilogger.h:74
#define getDeviceName
#define SIDRATE
int setObjectRA(int fd, double ra, bool addSpace)
int setObjectDEC(int fd, double dec, bool addSpace)
int checkLX200EquatorialFormat(int fd)
int abortSlew(int fd)
int Slew(int fd)
void setLX200Debug(const char *deviceName, unsigned int debug_level)
#define getLX200DEC(fd, x)
#define getLX200RA(fd, x)
*brief create JSON pointer *sa reference_tokens const string_t & a