File: celestron.c

package info (click to toggle)
hamlib 4.6.5-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,984 kB
  • sloc: ansic: 262,996; sh: 6,135; cpp: 1,578; perl: 876; makefile: 855; python: 148; awk: 58; xml: 26
file content (264 lines) | stat: -rw-r--r-- 6,787 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
/*
 *  Hamlib Rotator backend - Celestron
 *  Copyright (c) 2011 by Stephane Fillod
 *
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 2.1 of the License, or (at your option) any later version.
 *
 *   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
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */


#include <stdio.h>
#include <string.h>

#include "hamlib/rotator.h"
#include "serial.h"
#include "register.h"

#include "celestron.h"

#define ACK "#"

#define BUFSZ 128

/**
 * celestron_transaction
 *
 * cmdstr - Command to be sent to the rig.
 * data - Buffer for reply string.  Can be NULL, indicating that no reply is
 *        is needed, but answer will still be read.
 * data_len - in: Size of buffer. It is the caller's responsibility to provide
 *            a large enough buffer for all possible replies for a command.
 *
 * returns:
 *   RIG_OK  -  if no error occurred.
 *   RIG_EIO  -  if an I/O error occurred while sending/receiving data.
 *   RIG_ETIMEOUT  -  if timeout expires without any characters received.
 */
static int
celestron_transaction(ROT *rot, const char *cmdstr,
                      char *data, size_t data_len)
{
    hamlib_port_t *rotp = ROTPORT(rot);
    int retval;
    int retry_read = 0;
    char replybuf[BUFSZ];

transaction_write:

    rig_flush(rotp);

    if (cmdstr)
    {
        retval = write_block(rotp, (unsigned char *) cmdstr, strlen(cmdstr));

        if (retval != RIG_OK)
        {
            goto transaction_quit;
        }
    }

    /* Always read the reply to know whether the cmd went OK */
    if (!data)
    {
        data = replybuf;
    }

    if (!data_len)
    {
        data_len = BUFSZ;
    }

    /* the answer */
    memset(data, 0, data_len);
    retval = read_string(rotp, (unsigned char *) data, data_len,
                         ACK, strlen(ACK), 0, 1);

    if (retval < 0)
    {
        if (retry_read++ < rotp->retry)
        {
            goto transaction_write;
        }

        goto transaction_quit;
    }

    /* check for acknowledge */
    if (retval < 1 || data[retval - 1] != '#')
    {
        rig_debug(RIG_DEBUG_ERR, "%s: unexpected response, len %d: '%s'\n", __func__,
                  retval, data);
        return -RIG_EPROTO;
    }

    data[retval - 1] = '\0';

    retval = RIG_OK;
transaction_quit:
    return retval;
}


static int
celestron_set_position(ROT *rot, azimuth_t az, elevation_t el)
{
    char cmdstr[32];
    int retval;

    rig_debug(RIG_DEBUG_TRACE, "%s called: %f %f\n", __func__, az, el);

    /*
      Note: if the telescope has not been aligned, the RA/DEC values will not be meaningful and the AZM-ALT values will
          be relative to where the telescope was powered on. After alignment, RA/DEC values will reflect the actual sky,
          azimuth will be indexed to North equals 0 and altitude will be indexed with 0 equal to the orientation where the optical
          tube is perpendicular to the azimuth axis.
     */

    SNPRINTF(cmdstr, sizeof(cmdstr), "B%04X,%04X",
             (unsigned)((az / 360.) * 65535),
             (unsigned)((el / 360.) * 65535));

    retval = celestron_transaction(rot, cmdstr, NULL, 0);

    return retval;
}

static int
celestron_get_position(ROT *rot, azimuth_t *az, elevation_t *el)
{
    char posbuf[32];
    int retval;
    unsigned w;

    rig_debug(RIG_DEBUG_TRACE, "%s called\n", __func__);

    /* Get Azm-Alt */
    retval = celestron_transaction(rot, "Z", posbuf, sizeof(posbuf));

    if (retval != RIG_OK || strlen(posbuf) < 9 || posbuf[4] != ',')
    {
        return retval < 0 ? retval : -RIG_EPROTO;
    }

    if (sscanf(posbuf, "%04X", &w) != 1)
    {
        return -RIG_EPROTO;
    }

    *az = ((azimuth_t)w * 360.) / 65536.;

    if (sscanf(posbuf + 5, "%04X", &w) != 1)
    {
        return -RIG_EPROTO;
    }

    *el = ((elevation_t)w * 360.) / 65536.;

    rig_debug(RIG_DEBUG_TRACE, "%s: (az, el) = (%.1f, %.1f)\n",
              __func__, *az, *el);

    return RIG_OK;
}

static int
celestron_stop(ROT *rot)
{
    int retval;

    rig_debug(RIG_DEBUG_TRACE, "%s called\n", __func__);

    /* Cancel Goto */
    retval = celestron_transaction(rot, "M", NULL, 0);

    return retval;
}

static const char *
celestron_get_info(ROT *rot)
{
    static char info[32];
    char str[8];

    rig_debug(RIG_DEBUG_TRACE, "%s called\n", __func__);

    if (celestron_transaction(rot, "V", str, sizeof(str)) != RIG_OK)
    {
        return NULL;
    }

    SNPRINTF(info, sizeof(info), "V%c.%c", str[0], str[1]);

    return info;
}



/* ************************************************************************* */
/*
 * Celestron Nexstar telescope(rotator) capabilities.
 *
 * Protocol documentation:
 *  from Celestron:
 *    http://www.celestron.com/c3/images/files/downloads/1154108406_nexstarcommprot.pdf
 *  from Orion Teletrack Az-G:
 *    http://content.telescope.com/rsc/img/catalog/product/instructions/29295.pdf
 */

const struct rot_caps nexstar_rot_caps =
{
    ROT_MODEL(ROT_MODEL_NEXSTAR),
    .model_name =     "NexStar",  // Any Celestron starting with version 1.2
    .mfg_name =       "Celestron",
    .version =        "20220109.0",
    .copyright =      "LGPL",
    .status =         RIG_STATUS_STABLE,
    .rot_type =       ROT_TYPE_AZEL,
    .port_type =      RIG_PORT_SERIAL,
    .serial_rate_min  = 9600,
    .serial_rate_max  = 9600,
    .serial_data_bits = 8,
    .serial_stop_bits = 1,
    .serial_parity    = RIG_PARITY_NONE,
    .serial_handshake = RIG_HANDSHAKE_NONE,
    .write_delay      = 0,
    .post_write_delay = 0,
    .timeout          = 3500, /* worst case scenario */
    .retry            = 1,

    .min_az =     0.0,
    .max_az =     360.0,
    .min_el =     0.0,
    .max_el =     180.0,

    .get_position = celestron_get_position,
    .set_position = celestron_set_position,
    .stop         = celestron_stop,
    .get_info     = celestron_get_info,
};

/* ************************************************************************* */

DECLARE_INITROT_BACKEND(celestron)
{
    rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);

    rot_register(&nexstar_rot_caps);

    return RIG_OK;
}

/* ************************************************************************* */
/* end of file */