~ubuntu-branches/ubuntu/quantal/libindi/quantal

« back to all changes in this revision

Viewing changes to drivers/telescope/synscanmount.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Rohan Garg, Rohan Garg, Jonathan Riddell, Jonathan Thomas
  • Date: 2011-07-14 18:18:04 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110714181804-3fybxiy3tpjnk9kq
Tags: 0.8-0ubuntu1
[ Rohan Garg ]
* New upstream release
  - Add boost to build deps
  - Bump soversion to 0.8

[ Jonathan Riddell ]
* Use source format 3.0
* Add kubuntu_01_link_pthreads.diff to fix linking with pthreads

[ Jonathan Thomas ]
* Change Maintainer from MOTU to Ubuntu Developers

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
  Copyright(c) 2010 Gerry Rozema. All rights reserved.
 
3
 
 
4
  This program is free software; you can redistribute it and/or modify it
 
5
  under the terms of the GNU General Public License as published by the Free
 
6
  Software Foundation; either version 2 of the License, or (at your option)
 
7
  any later version.
 
8
 
 
9
  This program is distributed in the hope that it will be useful, but WITHOUT
 
10
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
11
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 
12
  more details.
 
13
 
 
14
  You should have received a copy of the GNU General Public License along with
 
15
  this program; if not, write to the Free Software Foundation, Inc., 59
 
16
  Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
17
 
 
18
  The full GNU General Public License is included in this distribution in the
 
19
  file called LICENSE.
 
20
*******************************************************************************/
 
21
#include "synscanmount.h"
 
22
#include "libs/indicom.h"
 
23
 
 
24
#include <stdio.h>
 
25
#include <stdlib.h>
 
26
#include <string.h>
 
27
#include <stdarg.h>
 
28
#include <math.h>
 
29
#include <unistd.h>
 
30
#include <time.h>
 
31
#include <memory>
 
32
 
 
33
#include <string.h>
 
34
#include <sys/stat.h>
 
35
 
 
36
// We declare an auto pointer to Synscan.
 
37
std::auto_ptr<SynscanMount> synscan(0);
 
38
 
 
39
void ISPoll(void *p);
 
40
 
 
41
 
 
42
void ISInit()
 
43
{
 
44
   static int isInit =0;
 
45
 
 
46
   if (isInit == 1)
 
47
       return;
 
48
 
 
49
    isInit = 1;
 
50
    if(synscan.get() == 0) synscan.reset(new SynscanMount());
 
51
    //IEAddTimer(POLLMS, ISPoll, NULL);
 
52
 
 
53
}
 
54
 
 
55
void ISGetProperties(const char *dev)
 
56
{
 
57
        ISInit();
 
58
        synscan->ISGetProperties(dev);
 
59
}
 
60
 
 
61
void ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int num)
 
62
{
 
63
        ISInit();
 
64
        synscan->ISNewSwitch(dev, name, states, names, num);
 
65
}
 
66
 
 
67
void ISNewText( const char *dev, const char *name, char *texts[], char *names[], int num)
 
68
{
 
69
        ISInit();
 
70
        synscan->ISNewText(dev, name, texts, names, num);
 
71
}
 
72
 
 
73
void ISNewNumber(const char *dev, const char *name, double values[], char *names[], int num)
 
74
{
 
75
        ISInit();
 
76
        synscan->ISNewNumber(dev, name, values, names, num);
 
77
}
 
78
 
 
79
void ISNewBLOB (const char *dev, const char *name, int sizes[], int blobsizes[], char *blobs[], char *formats[], char *names[], int n)
 
80
{
 
81
  INDI_UNUSED(dev);
 
82
  INDI_UNUSED(name);
 
83
  INDI_UNUSED(sizes);
 
84
  INDI_UNUSED(blobsizes);
 
85
  INDI_UNUSED(blobs);
 
86
  INDI_UNUSED(formats);
 
87
  INDI_UNUSED(names);
 
88
  INDI_UNUSED(n);
 
89
}
 
90
void ISSnoopDevice (XMLEle *root)
 
91
{
 
92
    INDI_UNUSED(root);
 
93
}
 
94
 
 
95
SynscanMount::SynscanMount()
 
96
{
 
97
    //ctor
 
98
}
 
99
 
 
100
SynscanMount::~SynscanMount()
 
101
{
 
102
    //dtor
 
103
}
 
104
 
 
105
const char * SynscanMount::getDefaultName()
 
106
{
 
107
    return "SynScan";
 
108
}
 
109
 
 
110
bool SynscanMount::initProperties()
 
111
{
 
112
    if (isDebug())
 
113
        IDLog("Synscan::init_properties\n");
 
114
 
 
115
    //setDeviceName("Synscan");
 
116
    INDI::Telescope::initProperties();
 
117
 
 
118
    return true;
 
119
}
 
120
void SynscanMount::ISGetProperties (const char *dev)
 
121
{
 
122
    if (isDebug())
 
123
        IDLog("Enter SynscanMount::ISGetProperties %s\n",dev);
 
124
 
 
125
    //  First we let our parent class do it's thing
 
126
    INDI::Telescope::ISGetProperties(dev);
 
127
 
 
128
    //  Now we add anything that's specific to this telescope
 
129
    //  or we could just load from a skeleton file too
 
130
    return;
 
131
}
 
132
 
 
133
bool SynscanMount::ReadScopeStatus()
 
134
{
 
135
    char str[20];
 
136
    int bytesWritten, bytesRead;
 
137
    int numread;
 
138
    double ra,dec;
 
139
    int n1,n2;
 
140
 
 
141
    //IDLog("Read Status\n");
 
142
    //tty_write(PortFD,(unsigned char *)"Ka",2, &bytesWritten);  //  test for an echo
 
143
 
 
144
    tty_write(PortFD,"Ka",2, &bytesWritten);  //  test for an echo
 
145
    tty_read(PortFD,str,2,2, &bytesRead);  //  Read 2 bytes of response
 
146
    if(str[1] != '#')
 
147
    {
 
148
        //  this is not a correct echo
 
149
        if (isDebug())
 
150
            IDLog("ReadStatus Echo Fail\n");
 
151
        IDMessage(deviceName(),"Mount Not Responding");
 
152
        return false;
 
153
    }
 
154
 
 
155
    if(TrackState==SCOPE_SLEWING)
 
156
    {
 
157
        //  We have a slew in progress
 
158
        //  lets see if it's complete
 
159
        //  This only works for ra/dec goto commands
 
160
        //  The goto complete flag doesn't trip for ALT/AZ commands
 
161
        memset(str,0,3);
 
162
        tty_write(PortFD,"L",1, &bytesWritten);
 
163
        numread=tty_read(PortFD,str,2,3, &bytesRead);
 
164
        if(str[0]!=48)
 
165
        {
 
166
            //  Nothing to do here
 
167
        } else
 
168
        {
 
169
            if(TrackState==SCOPE_PARKING) TrackState=SCOPE_PARKED;
 
170
            else TrackState=SCOPE_TRACKING;
 
171
        }
 
172
    }
 
173
    if(TrackState==SCOPE_PARKING)
 
174
    {
 
175
        //  ok, lets try read where we are
 
176
        //  and see if we have reached the park position
 
177
        memset(str,0,20);
 
178
        tty_write(PortFD,"Z",1, &bytesWritten);
 
179
        numread=tty_read(PortFD,str,10,2, &bytesRead);
 
180
        //IDLog("PARK READ %s\n",str);
 
181
        if(strncmp((char *)str,"0000,4000",9)==0)
 
182
        {
 
183
            TrackState=SCOPE_PARKED;
 
184
            ParkSV->s=IPS_OK;
 
185
            IDSetSwitch(ParkSV,NULL);
 
186
            IDMessage(deviceName(),"Telescope is Parked.");
 
187
        }
 
188
 
 
189
    }
 
190
 
 
191
    memset(str,0,20);
 
192
    tty_write(PortFD,"e",1, &bytesWritten);
 
193
    numread=tty_read(PortFD,str,18,1, &bytesRead);
 
194
    if (numread!=18)
 
195
    {
 
196
        IDLog("read status bytes didn't get a full read\n");
 
197
        return false;
 
198
    }
 
199
    // bytes read is as expected
 
200
    sscanf((char *)str,"%x",&n1);
 
201
    sscanf((char *)&str[9],"%x",&n2);
 
202
    ra=(double)n1/0x100000000*24.0;
 
203
    dec=(double)n2/0x100000000*360.0;
 
204
    NewRaDec(ra,dec);
 
205
    return true;
 
206
}
 
207
 
 
208
bool SynscanMount::Goto(double ra,double dec)
 
209
{
 
210
    char str[20];
 
211
    int n1,n2;
 
212
    int numread, bytesWritten, bytesRead;
 
213
 
 
214
    //  not fleshed in yet
 
215
    tty_write(PortFD,"Ka",2, &bytesWritten);  //  test for an echo
 
216
    tty_read(PortFD,str,2,2, &bytesRead);  //  Read 2 bytes of response
 
217
    if(str[1] != '#') {
 
218
        //  this is not a correct echo
 
219
        //  so we are not talking to a mount properly
 
220
        return false;
 
221
    }
 
222
    //  Ok, mount is alive and well
 
223
    //  so, lets format up a goto command
 
224
    n1=ra*0x1000000/24;
 
225
    n2=dec*0x1000000/360;
 
226
    n1=n1<<8;
 
227
    n2=n2<<8;
 
228
    sprintf((char *)str,"r%08X,%08X",n1,n2);
 
229
    tty_write(PortFD,str,18, &bytesWritten);
 
230
    TrackState=SCOPE_SLEWING;
 
231
    numread=tty_read(PortFD,str,1,60, &bytesRead);
 
232
    if (numread!=1||str[0]!='#')
 
233
    {
 
234
        if (isDebug())
 
235
            IDLog("Timeout waiting for scope to complete slewing.");
 
236
        return false;
 
237
    }
 
238
 
 
239
    return true;
 
240
}
 
241
 
 
242
bool SynscanMount::Park()
 
243
{
 
244
    char str[20];
 
245
    int numread, bytesWritten, bytesRead;
 
246
 
 
247
 
 
248
    memset(str,0,3);
 
249
    tty_write(PortFD,"Ka",2, &bytesWritten);  //  test for an echo
 
250
    tty_read(PortFD,str,2,2, &bytesRead);  //  Read 2 bytes of response
 
251
    if(str[1] != '#')
 
252
    {
 
253
        //  this is not a correct echo
 
254
        //  so we are not talking to a mount properly
 
255
        return false;
 
256
    }
 
257
    //  Now we stop tracking
 
258
    tty_write(PortFD,"T0",2, &bytesWritten);
 
259
    numread=tty_read(PortFD,str,1,60, &bytesRead);
 
260
    if (numread!=1||str[0]!='#')
 
261
    {
 
262
        if (isDebug())
 
263
            IDLog("Timeout waiting for scope to stop tracking.");
 
264
        return false;
 
265
    }
 
266
 
 
267
    //sprintf((char *)str,"b%08X,%08X",0x0,0x40000000);
 
268
    tty_write(PortFD,"B0000,4000",10, &bytesWritten);
 
269
    numread=tty_read(PortFD,str,1,60, &bytesRead);
 
270
    if (numread!=1||str[0]!='#')
 
271
    {
 
272
        if (isDebug())
 
273
            IDLog("Timeout waiting for scope to respond to park.");
 
274
        return false;
 
275
    }
 
276
 
 
277
    TrackState=SCOPE_PARKING;
 
278
    IDMessage(deviceName(),"Parking Telescope...");
 
279
    return true;
 
280
}