File: inditelescope.cpp

package info (click to toggle)
libindi 0.9.1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,232 kB
  • sloc: ansic: 17,921; cpp: 17,821; xml: 260; makefile: 12
file content (538 lines) | stat: -rw-r--r-- 16,252 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/*******************************************************************************
  Copyright(c) 2011 Gerry Rozema, Jasem Mutlaq. All rights reserved.

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Library General Public
 License version 2 as published by the Free Software Foundation.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Library General Public License for more details.

 You should have received a copy of the GNU Library General Public License
 along with this library; see the file COPYING.LIB.  If not, write to
 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 Boston, MA 02110-1301, USA.
*******************************************************************************/


#include "inditelescope.h"
#include "libs/indicom.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <math.h>
#include <unistd.h>
#include <time.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <termios.h>

#define POLLMS 1000

INDI::Telescope::Telescope()
{
    //ctor
    EqNV = new INumberVectorProperty;
    EqReqNV = new INumberVectorProperty;
    LocationNV = new INumberVectorProperty;
    CoordSV = new ISwitchVectorProperty;
    ParkSV = new ISwitchVectorProperty;
    PortTV = new ITextVectorProperty;
    MovementNSSP = new ISwitchVectorProperty;
    MovementWESP = new ISwitchVectorProperty;
    ConfigSV = new ISwitchVectorProperty;

}

INDI::Telescope::~Telescope()
{
    delete EqNV;
    delete EqReqNV;
    delete LocationNV;
    delete CoordSV;
    delete ParkSV;
    delete PortTV;
    delete MovementNSSP;
    delete MovementWESP;
    delete ConfigSV;
}

bool INDI::Telescope::initProperties()
{
    DefaultDriver::initProperties();

    IUFillNumber(&EqN[0],"RA","RA (hh:mm:ss)","%010.6m",0,24,0,0);
    IUFillNumber(&EqN[1],"DEC","DEC (dd:mm:ss)","%010.6m",-90,90,0,0);
    IUFillNumberVector(EqNV,EqN,2,deviceName(),"EQUATORIAL_EOD_COORD","Eq. Coordinates",MAIN_CONTROL_TAB,IP_RO,60,IPS_IDLE);

    IUFillNumber(&EqReqN[0],"RA","RA (hh:mm:ss)","%010.6m",0,24,0,0);
    IUFillNumber(&EqReqN[1],"DEC","DEC (dd:mm:ss)","%010.6m",-90,90,0,0);
    IUFillNumberVector(EqReqNV,EqReqN,2,deviceName(),"EQUATORIAL_EOD_COORD_REQUEST","GOTO",MAIN_CONTROL_TAB,IP_WO,60,IPS_IDLE);

    IUFillNumber(&LocationN[0],"LAT","Lat (dd:mm:ss)","%010.6m",-90,90,0,0.0);
    IUFillNumber(&LocationN[1],"LONG","Lon (dd:mm:ss)","%010.6m",-180,180,0,0.0 );
    IUFillNumberVector(LocationNV,LocationN,2,deviceName(),"GEOGRAPHIC_COORD","Scope Location",SITE_TAB,IP_RW,60,IPS_OK);

    IUFillSwitch(&CoordS[0],"TRACK","Track",ISS_OFF);
    IUFillSwitch(&CoordS[1],"SLEW","Slew",ISS_OFF);
    IUFillSwitch(&CoordS[2],"SYNC","Sync",ISS_OFF);
    IUFillSwitchVector(CoordSV,CoordS,3,deviceName(),"ON_COORD_SET","On Set",MAIN_CONTROL_TAB,IP_RW,ISR_1OFMANY,60,IPS_IDLE);

    IUFillSwitch(&ConfigS[0], "CONFIG_LOAD", "Load", ISS_OFF);
    IUFillSwitch(&ConfigS[1], "CONFIG_SAVE", "Save", ISS_OFF);
    IUFillSwitch(&ConfigS[2], "CONFIG_DEFAULT", "Default", ISS_OFF);
    IUFillSwitchVector(ConfigSV, ConfigS, 3, deviceName(), "CONFIG_PROCESS", "Configuration", "Options", IP_RW, ISR_1OFMANY, 60, IPS_IDLE);


    IUFillSwitch(&ParkS[0],"PARK","Park",ISS_OFF);
    IUFillSwitchVector(ParkSV,ParkS,1,deviceName(),"TELESCOPE_PARK","Park",MAIN_CONTROL_TAB,IP_RW,ISR_1OFMANY,60,IPS_IDLE);

    IUFillText(&PortT[0],"PORT","Port","/dev/ttyUSB0");
    IUFillTextVector(PortTV,PortT,1,deviceName(),"DEVICE_PORT","Ports",OPTIONS_TAB,IP_RW,60,IPS_IDLE);

    IUFillSwitch(&MovementNSS[MOTION_NORTH], "MOTION_NORTH", "North", ISS_OFF);
    IUFillSwitch(&MovementNSS[MOTION_SOUTH], "MOTION_SOUTH", "South", ISS_OFF);
    IUFillSwitchVector(MovementNSSP, MovementNSS, 2, deviceName(),"TELESCOPE_MOTION_NS", "North/South", MOTION_TAB, IP_RW, ISR_1OFMANY, 60, IPS_IDLE);

    IUFillSwitch(&MovementWES[MOTION_WEST], "MOTION_WEST", "West", ISS_OFF);
    IUFillSwitch(&MovementWES[MOTION_EAST], "MOTION_EAST", "East", ISS_OFF);
    IUFillSwitchVector(MovementWESP, MovementWES, 2, deviceName(),"TELESCOPE_MOTION_WE", "West/East", MOTION_TAB, IP_RW, ISR_1OFMANY, 60, IPS_IDLE);

    TrackState=SCOPE_PARKED;
    return 0;
}

void INDI::Telescope::ISGetProperties (const char *dev)
{
    //  First we let our parent populate
    //IDLog("INDI::Telescope::ISGetProperties %s\n",dev);
    DefaultDriver::ISGetProperties(dev);

    //  We may need the port set before we can connect
    IDDefText(PortTV,NULL);
    //LoadConfig();

    if(isConnected())
    {
        //  Now we add our telescope specific stuff
        defineSwitch(CoordSV);
        defineNumber(EqNV);
        defineNumber(EqReqNV);
        defineNumber(LocationNV);
        defineSwitch(ParkSV);
        defineSwitch(MovementNSSP);
        defineSwitch(MovementWESP);
		defineSwitch(ConfigSV);

    }
    return;
}

bool INDI::Telescope::updateProperties()
{
    defineText(PortTV);
    if(isConnected())
    {
        //  Now we add our telescope specific stuff
        defineSwitch(CoordSV);
        defineNumber(EqNV);
        defineNumber(EqReqNV);
        defineSwitch(MovementNSSP);
        defineSwitch(MovementWESP);
        defineNumber(LocationNV);
        defineSwitch(ParkSV);

    } else
    {
        //IDLog("INDI::Telescope deleting properties\n");
        deleteProperty(CoordSV->name);
        deleteProperty(EqNV->name);
        deleteProperty(EqReqNV->name);
        deleteProperty(MovementNSSP->name);
        deleteProperty(MovementWESP->name);
        deleteProperty(LocationNV->name);
        deleteProperty(ParkSV->name);

        //initProperties();
    }
	defineSwitch(ConfigSV);

    return true;
}

bool INDI::Telescope::saveConfigItems(FILE *fp)
{

	IUSaveConfigNumber(fp,LocationNV);
    return true;
}

void INDI::Telescope::NewRaDec(double ra,double dec)
{
    //  Lets set our eq values to these numbers
    //  which came from the hardware
    static int last_state=-1;

    if (TrackState == SCOPE_TRACKING && EqReqNV->s != IPS_OK)
    {
        EqReqNV->s = IPS_OK;
        IDSetNumber(EqReqNV, NULL);
    }
    else if ( (TrackState == SCOPE_PARKED || TrackState == SCOPE_IDLE) && EqReqNV->s != IPS_IDLE)
    {
        EqReqNV->s = IPS_IDLE;
        IDSetNumber(EqReqNV, NULL);
    }

    //IDLog("newRA DEC RA %g - DEC %g --- EqN[0] %g --- EqN[1] %g --- EqN.state %d\n", ra, dec, EqN[0].value, EqN[1].value, EqNV->s);
    if (EqN[0].value != ra || EqN[1].value != dec || EqNV->s != last_state)
    {
      //  IDLog("Not equal , send update to client...\n");
        EqN[0].value=ra;
        EqN[1].value=dec;
        last_state = EqNV->s;
        IDSetNumber(EqNV, NULL);
    }

}

bool INDI::Telescope::ReadScopeStatus()
{
    //  Return an error, because we sholdn't get here
    return false;
}

bool INDI::Telescope::Goto(double ra,double dec)
{
    //  if we get here, it's because our derived hardware class
    //  does not support goto
    return false;
}

bool INDI::Telescope::Sync(double ra,double dec)
{
    //  if we get here, our mount doesn't support sync
    IDMessage(deviceName(),"Mount does not support Sync");
    return false;
}

bool INDI::Telescope::MoveNS(TelescopeMotionNS dir)
{
    IDMessage(deviceName(),"Mount does not support North/South motion");
    IUResetSwitch(MovementNSSP);
    MovementNSSP->s = IPS_IDLE;
    IDSetSwitch(MovementNSSP, NULL);
    return false;
}

bool INDI::Telescope::MoveWE(TelescopeMotionWE dir)
{
    IDMessage(deviceName(),"Mount does not support West/East motion");
    IUResetSwitch(MovementWESP);
    MovementWESP->s = IPS_IDLE;
    IDSetSwitch(MovementWESP, NULL);
    return false;
}

/**************************************************************************************
** Process Text properties
***************************************************************************************/
bool INDI::Telescope::ISNewText (const char *dev, const char *name, char *texts[], char *names[], int n)
{
    //  Ok, lets see if this is a property wer process
    //IDLog("INDI::Telescope got %d new text items name %s\n",n,name);
    //  first check if it's for our device
    if(strcmp(dev,deviceName())==0)
    {
        //  This is for our device
        //  Now lets see if it's something we process here
        if(strcmp(name,PortTV->name)==0)
        {
            //  This is our port, so, lets process it

            //  Some clients insist on sending a port
            //  and they may not be configured for the
            //  correct port
            //  If we are already connected
            //  and running, it makes absolutely no sense
            //  to accept a new port value
            //  so lets just lie to them and say
            //  we did this, but, dont actually change anything
            //if(Connected) return true;

            int rc;
            //IDLog("calling update text\n");
            PortTV->s=IPS_OK;
            rc=IUUpdateText(PortTV,texts,names,n);
            //IDLog("update text returns %d\n",rc);
            //  Update client display
            IDSetText(PortTV,NULL);
            //SaveConfig();
            //  We processed this one, so, tell the world we did it
            return true;
        }

    }

    return DefaultDriver::ISNewText(dev,name,texts,names,n);
}

/**************************************************************************************
**
***************************************************************************************/
bool INDI::Telescope::ISNewNumber (const char *dev, const char *name, double values[], char *names[], int n)
{
    //  first check if it's for our device
    //IDLog("INDI::Telescope::ISNewNumber %s\n",name);
    if(strcmp(dev,deviceName())==0)
    {
        if(strcmp(name,"EQUATORIAL_EOD_COORD_REQUEST")==0)
        {
            //  this is for us, and it is a goto
            bool rc=false;
            double ra=-1;
            double dec=-100;

            for (int x=0; x<n; x++)
            {

                //IDLog("request stuff %s %4.2f\n",names[x],values[x]);

                INumber *eqp = IUFindNumber (EqNV, names[x]);
                if (eqp == &EqN[0])
                {
                    ra = values[x];
                } else if (eqp == &EqN[1])
                {
                    dec = values[x];
                }
            }
            //IDLog("Got %4.2f %4.2f\n",ra,dec);
            if ((ra>=0)&&(ra<=24)&&(dec>=-90)&&(dec<=90))
            {
                //  we got an ra and a dec, both in range
                //  And now we let the underlying hardware specific class
                //  perform the goto
                //  Ok, lets see if we should be doing a goto
                //  or a sync
                ISwitch *sw;
                sw=IUFindSwitch(CoordSV,"SYNC");
                if((sw != NULL)&&( sw->s==ISS_ON ))
                {
                    //IDLog("Got a sync, we dont process yet\n");
                    rc=Sync(ra,dec);
                } else {
                    //  Ensure we are not showing Parked status
                    ParkSV->s=IPS_IDLE;
                    IDSetSwitch(ParkSV,NULL);
                    rc=Goto(ra,dec);
                }
                //  Ok, now we have to put our switches back
            }
            return rc;
        }

        if(strcmp(name,"GEOGRAPHIC_COORD")==0)
        {
            //  Client wants to update the lat/long
            //  For now, we'll allow this, but, in future
            //  If we have lat/lon from gps, we'll prevent this
            //  from being updated
            LocationNV->s=IPS_OK;
            IUUpdateNumber(LocationNV,values,names,n);
            //  Update client display
            IDSetNumber(LocationNV,NULL);
        }
    }



    return DefaultDriver::ISNewNumber(dev,name,values,names,n);
}

/**************************************************************************************
**
***************************************************************************************/
bool INDI::Telescope::ISNewSwitch (const char *dev, const char *name, ISState *states, char *names[], int n)
{
    //IDLog("Enter IsNewSwitch for %s\n",name);
    //for(int x=0; x<n; x++) {
    //    IDLog("Switch %s %d\n",names[x],states[x]);
    //}

    if(strcmp(dev,deviceName())==0)
    {
        //  This one is for us
        if(strcmp(name,"ON_COORD_SET")==0)
        {
            //  client is telling us what to do with co-ordinate requests
            CoordSV->s=IPS_OK;
            IUUpdateSwitch(CoordSV,states,names,n);
            //  Update client display
            IDSetSwitch(CoordSV,NULL);

            for(int x=0; x<n; x++)
            {


            }
            return true;
        }

        if(strcmp(name,"TELESCOPE_PARK")==0)
        {
            Park();
        }

        if(strcmp(name,"TELESCOPE_MOTION_NS")==0)
        {
            IUUpdateSwitch(MovementNSSP,states,names,n);

            MovementNSSP->s = IPS_BUSY;

            if (MovementNSS[MOTION_NORTH].s == ISS_ON)
                MoveNS(MOTION_NORTH);
            else
                MoveNS(MOTION_SOUTH);

            return true;
        }

        if(strcmp(name,"TELESCOPE_MOTION_WE")==0)
        {
            IUUpdateSwitch(MovementWESP,states,names,n);

            MovementWESP->s = IPS_BUSY;

            if (MovementWES[MOTION_WEST].s == ISS_ON)
                MoveWE(MOTION_WEST);
            else
                MoveWE(MOTION_EAST);

            return true;
        }

    }

    //  Nobody has claimed this, so, ignore it
    return DefaultDriver::ISNewSwitch(dev,name,states,names,n);
}


bool INDI::Telescope::Connect()
{
    //  Parent class is wanting a connection
    if (isDebug())
        IDLog("INDI::Telescope arrived in connect with %s\n",PortT[0].text);
    bool rc=false;

    if(isConnected()) return true;


    if (isDebug())
        IDLog("Telescope Calling Connect\n");

    rc=Connect(PortT[0].text);

    if (isDebug())
        IDLog("Telescope Connect returns %d\n",rc);

    if(rc)
        SetTimer(POLLMS);
    return rc;
}


bool INDI::Telescope::Connect(const char *port)
{
    //  We want to connect to a port
    //  For now, we will assume it's a serial port
    //struct termios tty;

    int connectrc=0;
    char errorMsg[MAXRBUF];
    bool rc;

	IDLog("Indi Telescope connecting to %s\n",port);

    if ( (connectrc = tty_connect(port, 9600, 8, 0, 1, &PortFD)) != TTY_OK)
    {
        tty_error_msg(connectrc, errorMsg, MAXRBUF);

        //if (isDebug())
            IDLog("Failed to connect o port %s. Error: %s", port, errorMsg);
        IDMessage(deviceName(), "Failed to connect to port %s. Error: %s", port, errorMsg);

        return false;

    }
	IDLog("Port Fd %d\n",PortFD);
    /* Flush the input (read) buffer */

    //tcflush(PortFD,TCIOFLUSH);

    /* Test connection */
    rc=ReadScopeStatus();
    if(rc)
    {
        //  We got a valid scope status read
        IDMessage(deviceName(),"Telescope is online.");
        return rc;
    }

    //  Ok, we didn't get a valid read
    //  So, we need to close our handle and send error messages
    close(PortFD);
    //IDMessage(deviceName(),"Didn't find a synscan mount on Serial Port");

    return false;
}

bool INDI::Telescope::Disconnect()
{
    //  We dont actually close the serial connection
    //  because clients can screw each other up if we allow that
    //Connected=false;

	IDLog("IndiTelescope Disconnect\n");

    close(PortFD);
    IDMessage(deviceName(),"Telescope is offline.");

    return true;
}


void INDI::Telescope::TimerHit()
{
    //IDLog("Telescope Timer Hit\n");
    if(isConnected())
    {
        bool rc;

        rc=ReadScopeStatus();
        //IDLog("TrackState after read is %d\n",TrackState);
        if(rc == false)
        {
            //  read was not good
            EqNV->s=IPS_ALERT;
            IDSetNumber(EqNV, NULL);
        }      

        SetTimer(POLLMS);
    }
}


bool INDI::Telescope::Park()
{
    //  We want to park our telescope
    //  but the scope doesn't seem to support park
    //  or it wouldn't have gotten here
    return false;
}