File: prosistel.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 (542 lines) | stat: -rw-r--r-- 15,659 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
539
540
541
542
/*
 *  Hamlib Prosistel backend
 *  Copyright (c) 2015 by Dario Ventura IZ7CRX
 *  Copyright (c) 2020 by Mikael Nousiainen OH3BHX
 *
 *
 *   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 "num_stdio.h"

#include "prosistel.h"

#define BUFSZ 128
#define CR "\r"
#define STX "\x02"

struct prosistel_rot_priv_caps
{
    float angle_multiplier;
    char azimuth_id;
    char elevation_id;

    int stop_angle;
};

/**
 * prosistel_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_EPROTO - if a the response does not follow Prosistel protocol
 *   RIG_ETIMEOUT  -  if timeout expires without any characters received.
 */
static int prosistel_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;
    }

    // Remember to check for STXA,G,R or STXA,?,XXX,R 10 bytes
    retval = read_string(rotp, (unsigned char *) data, 20, CR, strlen(CR),
                         0, 1);

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

        goto transaction_quit;
    }

    // Check if reply matches issued command
    if (cmdstr && data[0] == 0x02 && data[3] == cmdstr[2])
    {
        rig_debug(RIG_DEBUG_VERBOSE, "%s Command %c reply received\n", __func__,
                  data[3]);
        retval = RIG_OK;
    }
    else if (cmdstr)
    {
        rig_debug(RIG_DEBUG_VERBOSE,
                  "%s Error Command issued: %c doesn't match reply %c\n", __func__, cmdstr[2],
                  data[3]);
        retval = -RIG_EPROTO;
    }

transaction_quit:
    return retval;
}


static int prosistel_rot_open(ROT *rot)
{
    const struct prosistel_rot_priv_caps *priv_caps =
        (struct prosistel_rot_priv_caps *) rot->caps->priv;
    char cmdstr[64];
    int retval;

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


    // Disable CPM mode - CPM stands for Continuous Position Monitor operating mode
    // The rotator controller sends position data continuously when CPM is enabled

    // Disable CPM for azimuth if the rotator has an azimuth rotator
    if (rot->caps->rot_type == ROT_TYPE_AZIMUTH
            || rot->caps->rot_type == ROT_TYPE_AZEL)
    {
        num_sprintf(cmdstr, STX"%cS"CR, priv_caps->azimuth_id);
        retval = prosistel_transaction(rot, cmdstr, NULL, 0);

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

    // Disable CPM for elevation if the rotator has an elevation rotator
    if (rot->caps->rot_type == ROT_TYPE_ELEVATION
            || rot->caps->rot_type == ROT_TYPE_AZEL)
    {
        num_sprintf(cmdstr, STX"%cS"CR, priv_caps->elevation_id);
        retval = prosistel_transaction(rot, cmdstr, NULL, 0);

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

    return RIG_OK;
}


static int prosistel_rot_set_position(ROT *rot, azimuth_t az, elevation_t el)
{
    const struct prosistel_rot_priv_caps *priv_caps =
        (struct prosistel_rot_priv_caps *) rot->caps->priv;
    char cmdstr[64];
    int retval = -RIG_EINTERNAL;

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

    // Leading zeros in angle values are optional according to Prosistel protocol documentation

    // Set azimuth only if the rotator has the capability to do so
    // It is an error to set azimuth if it's not supported by the rotator controller
    if (rot->caps->rot_type == ROT_TYPE_AZIMUTH
            || rot->caps->rot_type == ROT_TYPE_AZEL)
    {
        num_sprintf(cmdstr, STX"%cG%.0f"CR, priv_caps->azimuth_id,
                    az * priv_caps->angle_multiplier);
        retval = prosistel_transaction(rot, cmdstr, NULL, 0);

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

    // Set elevation only if the rotator has the capability to do so
    // It is an error to set elevation if it's not supported by the rotator controller
    if (rot->caps->rot_type == ROT_TYPE_ELEVATION
            || rot->caps->rot_type == ROT_TYPE_AZEL)
    {
        num_sprintf(cmdstr, STX"%cG%.0f"CR, priv_caps->elevation_id,
                    el * priv_caps->angle_multiplier);
        retval = prosistel_transaction(rot, cmdstr, NULL, 0);

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

    return retval;
}


static int prosistel_rot_get_position(ROT *rot, azimuth_t *az, elevation_t *el)
{
    const struct prosistel_rot_priv_caps *priv_caps =
        (struct prosistel_rot_priv_caps *) rot->caps->priv;
    char cmdstr[64];
    char data[20];
    float posval;
    int retval = RIG_OK;
    int n;

    // Query azimuth only if the rotator has the capability to do so
    // It is an error to query for azimuth if it's not supported by the rotator controller
    if (rot->caps->rot_type == ROT_TYPE_AZIMUTH
            || rot->caps->rot_type == ROT_TYPE_AZEL)
    {
        char rot_id;

        num_sprintf(cmdstr, STX"%c?"CR, priv_caps->azimuth_id);
        retval = prosistel_transaction(rot, cmdstr, data, sizeof(data));

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

        // Example response of 290 degree azimuth with 3 digits
        // 02 41 2c 3f 2c 32 39 30 2c 52 0d      .A,?,290,R.
        // Example response of 100 degree azimuth with 4 digits
        // 02 41 2c 3f 2c 31 30 30 30 2c 52 0d   .A,?,1000,R.
        n = sscanf(data, "%*c%c,?,%f,%*c.", &rot_id, &posval);

        if (n != 2 || rot_id != priv_caps->azimuth_id)
        {
            rig_debug(RIG_DEBUG_ERR, "%s failed to parse azimuth '%s'\n", __func__, data);
            return -RIG_EPROTO;
        }

        posval /= priv_caps->angle_multiplier;

        rig_debug(RIG_DEBUG_VERBOSE, "%s got position from '%s' converted to %f\n",
                  __func__, data, posval);

        *az = (azimuth_t) posval;
    }
    else
    {
        *az = 0;
    }

    // Query elevation only if the rotator has the capability to do so
    // It is an error to query for elevation if it's not supported by the rotator controller
    if (rot->caps->rot_type == ROT_TYPE_ELEVATION
            || rot->caps->rot_type == ROT_TYPE_AZEL)
    {
        char rot_id;

        num_sprintf(cmdstr, STX"%c?"CR, priv_caps->elevation_id);
        retval = prosistel_transaction(rot, cmdstr, data, sizeof(data));

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

        // Example response of 90 degree elevation with 4 digits
        // 02 42 2c 3f 2c 30 39 30 30 2c 52 0d   .B,?,0900,R.
        // The response will be an error if no elevation is available
        // 02 42 2c 3f 2c 45 2c 30 30 30 30 33 0d    .B,?,E,00003.
        n = sscanf(data, "%*c%c,?,%f,%*c.", &rot_id, &posval);

        if (n != 2 || rot_id != priv_caps->elevation_id)
        {
            rig_debug(RIG_DEBUG_ERR, "%s failed to parse elevation '%s'\n", __func__, data);
            return -RIG_EPROTO;
        }

        posval /= priv_caps->angle_multiplier;

        rig_debug(RIG_DEBUG_VERBOSE, "%s got position from '%s' converted to %f\n",
                  __func__, data, posval);

        *el = (elevation_t) posval;
    }
    else
    {
        *el = 0;
    }

    return retval;
}


static int prosistel_rot_stop(ROT *rot)
{
    const struct prosistel_rot_priv_caps *priv_caps =
        (struct prosistel_rot_priv_caps *) rot->caps->priv;
    char cmdstr[64];
    int retval = -RIG_EINTERNAL;

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

    // Stop azimuth only if the rotator has the capability to do so
    if (rot->caps->rot_type == ROT_TYPE_AZIMUTH
            || rot->caps->rot_type == ROT_TYPE_AZEL)
    {
        num_sprintf(cmdstr, STX"%cG%d"CR, priv_caps->azimuth_id, priv_caps->stop_angle);
        retval = prosistel_transaction(rot, cmdstr, NULL, 0);

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

    // Stop elevation only if the rotator has the capability to do so
    if (rot->caps->rot_type == ROT_TYPE_ELEVATION
            || rot->caps->rot_type == ROT_TYPE_AZEL)
    {
        num_sprintf(cmdstr, STX"%cG%d"CR, priv_caps->elevation_id,
                    priv_caps->stop_angle);
        retval = prosistel_transaction(rot, cmdstr, NULL, 0);

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

    return retval;
}


static const struct prosistel_rot_priv_caps prosistel_rot_az_or_el_priv_caps =
{
    .angle_multiplier = 1.0f,
    /**
     * Angle 977 = soft stop, stops the rotator using using PWM if PWM mode is enabled, otherwise results in a fast stop
     * Angle 999 = fast stop, stops the rotator immediately
     */
    .stop_angle = 997,
    .azimuth_id = 'A',
    .elevation_id = 'E',
};


/*
 * The Prosistel Combi-Track azimuth + elevation controllers use angle values multiplied by 10 and have
 * two "units" with identifier A and B (instead of A and E).
 */
static const struct prosistel_rot_priv_caps prosistel_rot_combitrack_priv_caps =
{
    .angle_multiplier = 10.0f,
    /**
     * Angle 9777 = soft stop, stops the rotator using using PWM if PWM mode is enabled, otherwise results in a fast stop
     * Angle 9999 = fast stop, stops the rotator immediately
     */
    .stop_angle = 9777,
    .azimuth_id = 'A',
    .elevation_id = 'B',
};


// Elevation rotator with Control box D using azimuth logic
static const struct prosistel_rot_priv_caps prosistel_rot_el_cboxaz =
{
    .angle_multiplier = 1.0f,
    .stop_angle = 997,
    .elevation_id = 'A',
};


/*
 * Prosistel rotator capabilities
 */
const struct rot_caps prosistel_d_az_rot_caps =
{
    ROT_MODEL(ROT_MODEL_PROSISTEL_D_AZ),
    .model_name =     "D azimuth",
    .mfg_name =       "Prosistel",
    .version =        "20201215.0",
    .copyright =      "LGPL",
    .status =         RIG_STATUS_STABLE,
    .rot_type =       ROT_TYPE_AZIMUTH,
    .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          = 3000,
    .retry            = 3,

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

    .priv = &prosistel_rot_az_or_el_priv_caps,

    .rot_open = prosistel_rot_open,
    .stop = prosistel_rot_stop,
    .set_position = prosistel_rot_set_position,
    .get_position = prosistel_rot_get_position,
};


const struct rot_caps prosistel_d_el_rot_caps =
{
    ROT_MODEL(ROT_MODEL_PROSISTEL_D_EL),
    .model_name =     "D elevation",
    .mfg_name =       "Prosistel",
    .version =        "20201215.0",
    .copyright =      "LGPL",
    .status =         RIG_STATUS_STABLE,
    .rot_type =       ROT_TYPE_ELEVATION,
    .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          = 3000,
    .retry            = 3,

    .min_az =     0.0,
    .max_az =     0.0,
    .min_el =     0.0,
    .max_el =     90.0,

    .priv = &prosistel_rot_az_or_el_priv_caps,

    .rot_open = prosistel_rot_open,
    .stop = prosistel_rot_stop,
    .set_position = prosistel_rot_set_position,
    .get_position = prosistel_rot_get_position,
};


const struct rot_caps prosistel_combi_track_azel_rot_caps =
{
    ROT_MODEL(ROT_MODEL_PROSISTEL_COMBI_TRACK_AZEL),
    .model_name =     "Combi-Track az+el",
    .mfg_name =       "Prosistel",
    .version =        "20201215.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          = 3000,
    .retry            = 3,

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

    .priv = &prosistel_rot_combitrack_priv_caps,

    .rot_open = prosistel_rot_open,
    .stop = prosistel_rot_stop,
    .set_position = prosistel_rot_set_position,
    .get_position = prosistel_rot_get_position,
};


// Elevation rotator with Control box D using azimuth logic
const struct rot_caps prosistel_d_el_cboxaz_rot_caps =
{
    ROT_MODEL(ROT_MODEL_PROSISTEL_D_EL_CBOXAZ),
    .model_name =     "D elevation CBOX az",
    .mfg_name =       "Prosistel",
    .version =        "20221215.0",
    .copyright =      "LGPL",
    .status =         RIG_STATUS_STABLE,
    .rot_type =       ROT_TYPE_ELEVATION,
    .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          = 3000,
    .retry            = 3,

    .min_az =     0.0,
    .max_az =     0.0,
    .min_el =     0.0,
    .max_el =     90.0,

    .priv = &prosistel_rot_el_cboxaz,

    .rot_open = prosistel_rot_open,
    .stop = prosistel_rot_stop,
    .set_position = prosistel_rot_set_position,
    .get_position = prosistel_rot_get_position,
};


DECLARE_INITROT_BACKEND(prosistel)
{
    rig_debug(RIG_DEBUG_VERBOSE, "%s: _init called\n", __func__);

    rot_register(&prosistel_d_az_rot_caps);
    rot_register(&prosistel_d_el_rot_caps);
    rot_register(&prosistel_combi_track_azel_rot_caps);
    rot_register(&prosistel_d_el_cboxaz_rot_caps);

    return RIG_OK;
}