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
|
/*
* Hamlib SatEL backend - main file
* Copyright (c) 2021 Joshua Lynch
*
*
* 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 "hamlib/rig.h"
#include <strings.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <stdbool.h>
#include "hamlib/rotator.h"
#include "serial.h"
#include "register.h"
#include "satel.h"
/**
* Protocol documentation.
*
* Apparently, the system is modeled after this one:
* “An Inexpensive Az-El Rotator System”
* "Dec, 1999, QST article by Jim Koehler, VE5FP
*
* '?' - returns 'SatEL\r\n'. a good test to see if there's
* connectivity.
*
* 'g' - enable motion. nothing happens without this enabled.
*
* 'z' - display rotator status. contains current Az/El among other
* things. here's an example:
*
* Motion ENABLED
* Mode 0 - azimuth break at NORTH
* Time: 2001/00/00 00:00:07
* Azimuth = 000 Absolute = 000
* Elevation = 000
*
* Number of stored positions: 00
*
*
* '*' - reset the rotator controller.
*
* 'pAZ EL\r\n' - tell the rotator where to point where AZ is the
* integer azimuth and EL is the integer
* elevation. e.g. 'p010 045\n'. the controller will
* report the current pointing status after the
* operation has completed.
*
* NOTE: The SatEL system changed a few commands as described in the
* user's manual. They are not used here. You can find the manual for
* this rotator here:
*
* http://www.codeposse.com/~jlynch/SatEL%20Az-EL.pdf
*
*/
/**
* Idiosyncrasies
*
* - the controller does zero input checking. you can put it into an
* incredibly bad state very easily.
*
* - the controller doesn't accept any data whilst moving the
* rotators. In fact, you can put the controller into a bad state on
* occasion if you try and send it commands while its slewing the
* rotators. this means we have a really long read timeout so we can
* wait for the rotators to slew around before accepting any more
* commands.
*
*/
#define BUF_SIZE 256
typedef struct satel_stat satel_stat_t;
struct satel_stat
{
bool motion_enabled;
// int mode;
// time_t time;
// int absolute;
int az;
int el;
};
static int satel_cmd(ROT *rot, char *cmd, int cmdlen, char *res, int reslen)
{
int ret;
hamlib_port_t *rotp = ROTPORT(rot);
rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
rig_flush(rotp);
ret = write_block(rotp, (unsigned char *) cmd, cmdlen);
if (ret != RIG_OK)
{
return ret;
}
if (reslen > 0 && res != NULL)
{
ret = read_string(rotp, (unsigned char *) res, reslen, "\n", 1, 0, 1);
if (ret < 0)
{
return ret;
}
}
return RIG_OK;
}
static int satel_read_status(ROT *rot, satel_stat_t *stat)
{
char resbuf[BUF_SIZE];
char *p;
int ret;
hamlib_port_t *rotp = ROTPORT(rot);
// read motion state
ret = read_string(rotp, (unsigned char *) resbuf, BUF_SIZE, "\n", 1, 0, 1);
if (ret < 0)
{
return ret;
}
stat->motion_enabled = strcmp(resbuf, "Motion ENABLED") == 0 ? true : false;
// XXX skip mode
ret = read_string(rotp, (unsigned char *) resbuf, BUF_SIZE, "\n", 1, 0, 1);
if (ret < 0)
{
return ret;
}
// XXX skip time
ret = read_string(rotp, (unsigned char *) resbuf, BUF_SIZE, "\n", 1, 0, 1);
if (ret < 0)
{
return ret;
}
// read azimuth line
ret = read_string(rotp, (unsigned char *) resbuf, BUF_SIZE, "\n", 1, 0, 1);
if (ret < 0)
{
return ret;
}
p = resbuf + 10;
p[3] = '\0';
stat->az = (int)strtof(p, NULL);
// read elevation line
ret = read_string(rotp, (unsigned char *) resbuf, BUF_SIZE, "\n", 1, 0, 1);
if (ret < 0)
{
return ret;
}
p = resbuf + 12;
p[3] = '\0';
stat->el = (int)strtof(p, NULL);
// skip blank line
ret = read_string(rotp, (unsigned char *) resbuf, BUF_SIZE, "\n", 1, 0, 1);
if (ret < 0)
{
return ret;
}
// XXX skip stored position count
ret = read_string(rotp, (unsigned char *) resbuf, BUF_SIZE, "\n", 1, 0, 1);
if (ret < 0)
{
return ret;
}
return RIG_OK;
}
static int satel_get_status(ROT *rot, satel_stat_t *stat)
{
int ret;
ret = satel_cmd(rot, "z", 1, NULL, 0);
if (ret != RIG_OK)
{
return ret;
}
return satel_read_status(rot, stat);
}
static int satel_rot_open(ROT *rot)
{
char resbuf[BUF_SIZE];
int ret;
rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
// are we connected?
ret = satel_cmd(rot, "?", 1, resbuf, BUF_SIZE);
if (ret != RIG_OK)
{
return ret;
}
ret = strncasecmp("SatEL", resbuf, 5);
if (ret != 0)
{
return -RIG_EIO;
}
// yep, reset system
ret = satel_cmd(rot, "*", 1, NULL, 0);
if (ret != RIG_OK)
{
return ret;
}
return RIG_OK;
}
static int satel_rot_set_position(ROT *rot, azimuth_t az, elevation_t el)
{
char cmdbuf[BUF_SIZE];
int ret;
satel_stat_t stat;
rig_debug(RIG_DEBUG_VERBOSE, "%s called: %.2f %.2f\n", __func__,
az, el);
ret = satel_get_status(rot, &stat);
if (ret < 0)
{
return ret;
}
if (stat.motion_enabled == false)
{
ret = satel_cmd(rot, "g", 1, NULL, 0);
if (ret != RIG_OK)
{
return ret;
}
}
SNPRINTF(cmdbuf, BUF_SIZE, "p%d %d\r\n", (int)az, (int)el);
ret = satel_cmd(rot, cmdbuf, strlen(cmdbuf), NULL, 0);
if (ret != RIG_OK)
{
return ret;
}
// wait-for, read and discard the status message
ret = satel_read_status(rot, &stat);
if (ret < 0)
{
return ret;
}
return RIG_OK;
}
static int satel_rot_get_position(ROT *rot, azimuth_t *az, elevation_t *el)
{
int ret;
satel_stat_t stat;
rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
ret = satel_get_status(rot, &stat);
if (ret < 0)
{
return ret;
}
*az = stat.az;
*el = stat.el;
return RIG_OK;
}
static int satel_rot_stop(ROT *rot)
{
rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
// send reset command
return satel_cmd(rot, "*", 1, NULL, 0);
}
static const char *satel_rot_get_info(ROT *rot)
{
rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
return "SatEL rotator";
}
/*
* Satel rotator capabilities.
*/
const struct rot_caps satel_rot_caps =
{
ROT_MODEL(ROT_MODEL_SATEL),
.model_name = "SatEL",
.mfg_name = "SatEL",
.version = "20210123.0",
.copyright = "LGPL",
.status = RIG_STATUS_STABLE,
.rot_type = ROT_TYPE_AZEL,
.port_type = RIG_PORT_SERIAL,
.serial_rate_max = 9600,
.serial_rate_min = 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 = 60000,
.retry = 0,
.min_az = 0.,
.max_az = 360.,
.min_el = 0.,
.max_el = 90.,
.rot_open = satel_rot_open,
.get_position = satel_rot_get_position,
.set_position = satel_rot_set_position,
.stop = satel_rot_stop,
.get_info = satel_rot_get_info,
.priv = NULL, /* priv */
};
DECLARE_INITROT_BACKEND(satel)
{
rig_debug(RIG_DEBUG_VERBOSE, "%s: _init called\n", __func__);
rot_register(&satel_rot_caps);
return RIG_OK;
}
|