1 /*
2  *  Hamlib Rotator backend - Celestron
3  *  Copyright (c) 2011 by Stephane Fillod
4  *
5  *   This library is free software; you can redistribute it and/or
6  *   modify it under the terms of the GNU Lesser General Public
7  *   License as published by the Free Software Foundation; either
8  *   version 2.1 of the License, or (at your option) any later version.
9  *
10  *   This library is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *   Lesser General Public License for more details.
14  *
15  *   You should have received a copy of the GNU Lesser General Public
16  *   License along with this library; if not, write to the Free Software
17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  */
20 
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <math.h>
30 #include <ctype.h>
31 
32 #include "hamlib/rotator.h"
33 #include "serial.h"
34 #include "misc.h"
35 #include "register.h"
36 
37 #include "celestron.h"
38 
39 #define ACK "#"
40 
41 #define BUFSZ 128
42 
43 /**
44  * celestron_transaction
45  *
46  * cmdstr - Command to be sent to the rig.
47  * data - Buffer for reply string.  Can be NULL, indicating that no reply is
48  *        is needed, but answer will still be read.
49  * data_len - in: Size of buffer. It is the caller's responsibily to provide
50  *            a large enough buffer for all possible replies for a command.
51  *
52  * returns:
53  *   RIG_OK  -  if no error occurred.
54  *   RIG_EIO  -  if an I/O error occurred while sending/receiving data.
55  *   RIG_ETIMEOUT  -  if timeout expires without any characters received.
56  */
57 static int
celestron_transaction(ROT * rot,const char * cmdstr,char * data,size_t data_len)58 celestron_transaction(ROT *rot, const char *cmdstr,
59                       char *data, size_t data_len)
60 {
61     struct rot_state *rs;
62     int retval;
63     int retry_read = 0;
64     char replybuf[BUFSZ];
65 
66     rs = &rot->state;
67 
68 transaction_write:
69 
70     rig_flush(&rs->rotport);
71 
72     if (cmdstr)
73     {
74         retval = write_block(&rs->rotport, cmdstr, strlen(cmdstr));
75 
76         if (retval != RIG_OK)
77         {
78             goto transaction_quit;
79         }
80     }
81 
82     /* Always read the reply to know whether the cmd went OK */
83     if (!data)
84     {
85         data = replybuf;
86     }
87 
88     if (!data_len)
89     {
90         data_len = BUFSZ;
91     }
92 
93     /* the answer */
94     memset(data, 0, data_len);
95     retval = read_string(&rs->rotport, data, data_len, ACK, strlen(ACK));
96 
97     if (retval < 0)
98     {
99         if (retry_read++ < rot->state.rotport.retry)
100         {
101             goto transaction_write;
102         }
103 
104         goto transaction_quit;
105     }
106 
107     /* check for acknowledge */
108     if (retval < 1 || data[retval - 1] != '#')
109     {
110         rig_debug(RIG_DEBUG_ERR, "%s: unexpected response, len %d: '%s'\n", __func__,
111                   retval, data);
112         return -RIG_EPROTO;
113     }
114 
115     data[retval - 1] = '\0';
116 
117     retval = RIG_OK;
118 transaction_quit:
119     return retval;
120 }
121 
122 
123 static int
celestron_set_position(ROT * rot,azimuth_t az,elevation_t el)124 celestron_set_position(ROT *rot, azimuth_t az, elevation_t el)
125 {
126     char cmdstr[32];
127     int retval;
128 
129     rig_debug(RIG_DEBUG_TRACE, "%s called: %f %f\n", __func__, az, el);
130 
131     /*
132       Note: if the telescope has not been aligned, the RA/DEC values will not be meaningful and the AZM-ALT values will
133           be relative to where the telescope was powered on. After alignment, RA/DEC values will reflect the actual sky,
134           azimuth will be indexed to North equals 0 and altitude will be indexed with 0 equal to the orientation where the optical
135           tube is perpendicular to the azimuth axis.
136      */
137 
138     sprintf(cmdstr, "B%04X,%04X",
139             (unsigned)((az / 360.) * 65535),
140             (unsigned)((el / 360.) * 65535));
141 
142     retval = celestron_transaction(rot, cmdstr, NULL, 0);
143 
144     return retval;
145 }
146 
147 static int
celestron_get_position(ROT * rot,azimuth_t * az,elevation_t * el)148 celestron_get_position(ROT *rot, azimuth_t *az, elevation_t *el)
149 {
150     char posbuf[32];
151     int retval;
152     unsigned w;
153 
154     rig_debug(RIG_DEBUG_TRACE, "%s called\n", __func__);
155 
156     /* Get Azm-Alt */
157     retval = celestron_transaction(rot, "Z", posbuf, sizeof(posbuf));
158 
159     if (retval != RIG_OK || strlen(posbuf) < 9 || posbuf[4] != ',')
160     {
161         return retval < 0 ? retval : -RIG_EPROTO;
162     }
163 
164     if (sscanf(posbuf, "%04X", &w) != 1)
165     {
166         return -RIG_EPROTO;
167     }
168 
169     *az = ((azimuth_t)w * 360.) / 65536.;
170 
171     if (sscanf(posbuf + 5, "%04X", &w) != 1)
172     {
173         return -RIG_EPROTO;
174     }
175 
176     *el = ((elevation_t)w * 360.) / 65536.;
177 
178     rig_debug(RIG_DEBUG_TRACE, "%s: (az, el) = (%.1f, %.1f)\n",
179               __func__, *az, *el);
180 
181     return RIG_OK;
182 }
183 
184 static int
celestron_stop(ROT * rot)185 celestron_stop(ROT *rot)
186 {
187     int retval;
188 
189     rig_debug(RIG_DEBUG_TRACE, "%s called\n", __func__);
190 
191     /* Cancel Goto */
192     retval = celestron_transaction(rot, "M", NULL, 0);
193 
194     return retval;
195 }
196 
197 static const char *
celestron_get_info(ROT * rot)198 celestron_get_info(ROT *rot)
199 {
200     static char info[16];
201     char str[8];
202 
203     rig_debug(RIG_DEBUG_TRACE, "%s called\n", __func__);
204 
205     if (celestron_transaction(rot, "V", str, sizeof(str)) != RIG_OK)
206     {
207         return NULL;
208     }
209 
210     sprintf(info, "V%c.%c", str[0], str[1]);
211 
212     return info;
213 }
214 
215 
216 
217 /* ************************************************************************* */
218 /*
219  * Celestron Nexstar telescope(rotator) capabilities.
220  *
221  * Protocol documentation:
222  *  from Celestron:
223  *    http://www.celestron.com/c3/images/files/downloads/1154108406_nexstarcommprot.pdf
224  *  from Orion Teletrack Az-G:
225  *    http://content.telescope.com/rsc/img/catalog/product/instructions/29295.pdf
226  */
227 
228 const struct rot_caps nexstar_rot_caps =
229 {
230     ROT_MODEL(ROT_MODEL_NEXSTAR),
231     .model_name =     "NexStar",  // Any Celestron starting with version 1.2
232     .mfg_name =       "Celestron",
233     .version =        "20110821.0",
234     .copyright =      "LGPL",
235     .status =         RIG_STATUS_UNTESTED,
236     .rot_type =       ROT_TYPE_AZEL,
237     .port_type =      RIG_PORT_SERIAL,
238     .serial_rate_min  = 9600,
239     .serial_rate_max  = 9600,
240     .serial_data_bits = 8,
241     .serial_stop_bits = 1,
242     .serial_parity    = RIG_PARITY_NONE,
243     .serial_handshake = RIG_HANDSHAKE_NONE,
244     .write_delay      = 0,
245     .post_write_delay = 0,
246     .timeout          = 3500, /* worst case scenario */
247     .retry            = 1,
248 
249     .min_az =     0.0,
250     .max_az =     360.0,
251     .min_el =     0.0,
252     .max_el =     180.0,
253 
254     .get_position = celestron_get_position,
255     .set_position = celestron_set_position,
256     .stop         = celestron_stop,
257     .get_info     = celestron_get_info,
258 };
259 
260 /* ************************************************************************* */
261 
DECLARE_INITROT_BACKEND(celestron)262 DECLARE_INITROT_BACKEND(celestron)
263 {
264     rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
265 
266     rot_register(&nexstar_rot_caps);
267 
268     return RIG_OK;
269 }
270 
271 /* ************************************************************************* */
272 /* end of file */
273 
274