File: hd1780.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 (332 lines) | stat: -rw-r--r-- 7,467 bytes parent folder | download | duplicates (2)
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
/*
 * Hamlib backend library for the HD 1780 Intellirotor command set.
 *
 * hd1780.c - (C) Nate Bargmann 2003 (n0nb at arrl.net)
 *            (C) Rob Frohne 2008 (kl7na at arrl.net)
 *
 * This shared library provides an API for communicating
 * via serial interface to a Heathkit HD 1780 Intellirotor.
 *
 * Heathkit is a trademark of Heath Company
 *
 *
 *   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 <stdlib.h>             /* Standard library definitions */
#include <string.h>             /* String function definitions */

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

#include "hd1780.h"


/*
 * Private data structure
 */
struct hd1780_rot_priv_data
{
    azimuth_t az;
};

/*
 * Private helper function prototypes
 */

static int hd1780_send_priv_cmd(ROT *rot, const char *cmd);


/*
 * HD 1780 Intellirotor rotor capabilities
 */

const struct rot_caps hd1780_rot_caps =
{
    ROT_MODEL(ROT_MODEL_HD1780),
    .model_name =         "HD 1780 Intellirotor",
    .mfg_name =           "Heathkit",
    .version =            "20220109.0",
    .copyright =          "LGPL",
    .status =             RIG_STATUS_STABLE,
    .rot_type =           ROT_TYPE_OTHER,
    .port_type =          RIG_PORT_SERIAL,
    .serial_rate_min =    300,
    .serial_rate_max =    9600,
    .serial_data_bits =   8,
    .serial_stop_bits =   1,
    .serial_parity =      RIG_PARITY_NONE,
    .serial_handshake =   RIG_HANDSHAKE_NONE,
    .write_delay =        10,
    .post_write_delay =   500,
    .timeout =            60000,
    .retry =              0,

    .min_az =             -180,
    .max_az =             180,
    .min_el =             0,
    .max_el =             0,

    .priv =  NULL,    /* priv */

    .rot_init =           hd1780_rot_init,
    .rot_cleanup =        hd1780_rot_cleanup,
    .set_position =       hd1780_rot_set_position,
    .get_position =       hd1780_rot_get_position,
};


/* ************************************
 *
 * API functions
 *
 * ************************************
 */


/*
 * Initialize data structures
 */

static int hd1780_rot_init(ROT *rot)
{
    struct hd1780_rot_priv_data *priv;

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

    if (!rot)
    {
        return -RIG_EINVAL;
    }

    ROTSTATE(rot)->priv = (struct hd1780_rot_priv_data *)
                          calloc(1, sizeof(struct hd1780_rot_priv_data));

    if (!ROTSTATE(rot)->priv)
    {
        return -RIG_ENOMEM;
    }

    priv = ROTSTATE(rot)->priv;

    ROTPORT(rot)->type.rig = RIG_PORT_SERIAL;

    priv->az = 0;

    return RIG_OK;
}

/*
 * Clean up allocated memory structures
 */

static int hd1780_rot_cleanup(ROT *rot)
{

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

    if (!rot)
    {
        return -RIG_EINVAL;
    }

    if (ROTSTATE(rot)->priv)
    {
        free(ROTSTATE(rot)->priv);
    }

    ROTSTATE(rot)->priv = NULL;

    return RIG_OK;
}


/*
 * Set position
 * HD 1780 protocol supports azimuth only--elevation is ignored
 * Range is converted to an integer string, 0 to 360 degrees
 */

static int hd1780_rot_set_position(ROT *rot, azimuth_t azimuth,
                                   elevation_t elevation)
{
    char cmdstr[8];
    const char execstr[5] = "\r", ok[3];
    int err;

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

    if (!rot)
    {
        return -RIG_EINVAL;
    }

    if (azimuth < hd1780_rot_caps.min_az || azimuth > hd1780_rot_caps.max_az)
    {
        return -RIG_EINVAL;
    }

    if (azimuth < 0) { azimuth = azimuth + 360; }

    SNPRINTF(cmdstr, sizeof(cmdstr), "%03.0f", azimuth);    /* Target bearing */
    err = hd1780_send_priv_cmd(rot, cmdstr);

    if (err != RIG_OK)
    {
        return err;
    }

    err = hd1780_send_priv_cmd(rot, execstr); /* Execute command */

    if (err != RIG_OK)
    {
        return err;
    }

    /* We need to look for the <CR> +<LF> to signify that everything finished.  The HD 1780
     * sends a <CR> when it is finished rotating.
     */
    err = read_block(ROTPORT(rot), (unsigned char *) ok, 2);

    if (err != 2)
    {
        return -RIG_ETRUNC;
    }

    if ((ok[0] != '\r') || (ok[1] != '\n'))
    {
        return -RIG_ETRUNC;
    }


    return RIG_OK;
}


/*
 * Get position
 * Returns current azimuth position in whole degrees.
 * Range returned from Rotor-EZ is an integer, 0 to 359 degrees
 * Elevation is set to 0
 */

static int hd1780_rot_get_position(ROT *rot, azimuth_t *azimuth,
                                   elevation_t *elevation)
{
    const char cmdstr[3] = "b\r";
    char az[7];          /* read azimuth string */
    char *p;
    azimuth_t tmp = 0;
    int err;

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

    if (!rot)
    {
        return -RIG_EINVAL;
    }

    err = hd1780_send_priv_cmd(rot, cmdstr);

    if (err != RIG_OK)
    {
        return err;
    }

    err = read_block(ROTPORT(rot), (unsigned char *) az, AZ_READ_LEN);

    if (err != AZ_READ_LEN)
    {
        return -RIG_ETRUNC;
    }

    /*
     * HD 1780 returns a four octet string consisting of
     * three octets containing the rotor's position in degrees followed by a
     * space and a <CR> and a <LF>.  The
     * space is ignored when passing the string to atof().
     */
    az[4] = 0x00;                 /* NULL terminated string */
    p = az; /* for hd1780 */
    tmp = (azimuth_t)atof(p);
    rig_debug(RIG_DEBUG_TRACE, "%s: \"%s\" after conversion = %.1f\n",
              __func__, p, tmp);

    if (tmp < 0 || tmp > 359)
    {
        return -RIG_EINVAL;
    }

    *azimuth = tmp;
    *elevation = 0;               /* assume aiming at the horizon */

    rig_debug(RIG_DEBUG_TRACE,
              "%s: azimuth = %.1f deg; elevation = %.1f deg\n",
              __func__, *azimuth, *elevation);

    return RIG_OK;
}



/*
 * Send command string to rotor.
 *
 * To make sure that the rotor is ready to take a command, we send it a <CR> and
 * it will normally reply with a ? <CR> <LF>.  If you don't do this, using the
 * w: command of rotctl it is possible to get it out of sequence.  This kind of
 * resets its command buffer before sending the command.
 */

static int hd1780_send_priv_cmd(ROT *rot, const char *cmdstr)
{
    int err;

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

    if (!rot)
    {
        return -RIG_EINVAL;
    }

    err = write_block(ROTPORT(rot), (unsigned char *) cmdstr, strlen(cmdstr));

    if (err != RIG_OK)
    {
        return err;
    }

    return RIG_OK;
}


/*
 * Initialize backend
 */

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

    rot_register(&hd1780_rot_caps);

    return RIG_OK;
}