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 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
|
/*
* Hamlib Rotator backend - INDI integration
* Copyright (c) 2020 by Norbert Varga HA2NON <nonoo@nonoo.hu>
*
*
* 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 "indi_wrapper.hpp"
#include <math.h>
#include <limits.h>
#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
static std::unique_ptr<RotINDIClient> indi_wrapper_client(new RotINDIClient());
int RotINDIClient::setSpeed(int speedPercent)
{
if (!mTelescope || !mTelescope->isConnected())
{
rig_debug(RIG_DEBUG_ERR, "indi: telescope not connected\n");
return -RIG_EIO;
}
ISwitchVectorProperty *switchVector =
mTelescope->getSwitch("TELESCOPE_SLEW_RATE");
if (!switchVector)
{
rig_debug(RIG_DEBUG_ERR,
"indi: unable to find telescope or TELESCOPE_SLEW_RATE switch\n");
return -RIG_EPROTO;
}
if (speedPercent < 0)
{
speedPercent = 0;
}
if (speedPercent > 100)
{
speedPercent = 100;
}
unsigned int speed = DIV_ROUND_UP(speedPercent, 10);
for (unsigned int i = 1; i <= 10; i++)
{
char switchName[4];
snprintf(switchName, sizeof(switchName), "%ux", i);
ISwitch *slewrate = IUFindSwitch(switchVector, switchName);
if (slewrate)
{
if (speed == i)
{
rig_debug(RIG_DEBUG_VERBOSE, "indi: setting speed %s\n", switchName);
slewrate->s = ISS_ON;
}
else
{
slewrate->s = ISS_OFF;
}
}
else
{
rig_debug(RIG_DEBUG_ERR, "indi: unable to find switch member %s\n", switchName);
return -RIG_EPROTO;
}
}
sendNewSwitch(switchVector);
return RIG_OK;
}
int RotINDIClient::move(int direction, int speedPercent)
{
if (!mTelescope || !mTelescope->isConnected())
{
rig_debug(RIG_DEBUG_ERR, "indi: telescope not connected\n");
return -RIG_EIO;
}
int err = setSpeed(speedPercent);
if (err != RIG_OK)
{
return err;
}
ISwitchVectorProperty *switchVector =
mTelescope->getSwitch("TELESCOPE_MOTION_NS");
if (!switchVector)
{
rig_debug(RIG_DEBUG_ERR,
"indi: unable to find telescope or TELESCOPE_MOTION_NS switch\n");
return -RIG_EPROTO;
}
ISwitch *motion_north = IUFindSwitch(switchVector, "MOTION_NORTH");
if (!motion_north)
{
rig_debug(RIG_DEBUG_ERR, "indi: unable to find switch member MOTION_NORTH\n");
return -RIG_EPROTO;
}
if (direction & ROT_MOVE_UP)
{
rig_debug(RIG_DEBUG_VERBOSE, "indi: moving up\n");
motion_north->s = ISS_ON;
}
else
{
motion_north->s = ISS_OFF;
}
ISwitch *motion_south = IUFindSwitch(switchVector, "MOTION_SOUTH");
if (!motion_south)
{
rig_debug(RIG_DEBUG_ERR, "indi: unable to find switch member MOTION_SOUTH\n");
return -RIG_EPROTO;
}
if (direction & ROT_MOVE_DOWN)
{
rig_debug(RIG_DEBUG_VERBOSE, "indi: moving down\n");
motion_south->s = ISS_ON;
}
else
{
motion_south->s = ISS_OFF;
}
sendNewSwitch(switchVector);
switchVector = mTelescope->getSwitch("TELESCOPE_MOTION_WE");
if (!switchVector)
{
rig_debug(RIG_DEBUG_ERR,
"indi: unable to find telescope or TELESCOPE_MOTION_WE switch\n");
return -RIG_EPROTO;
}
ISwitch *motion_west = IUFindSwitch(switchVector, "MOTION_WEST");
if (!motion_west)
{
rig_debug(RIG_DEBUG_ERR, "indi: unable to find switch member MOTION_WEST\n");
return -RIG_EPROTO;
}
if (direction & ROT_MOVE_LEFT)
{
rig_debug(RIG_DEBUG_VERBOSE, "indi: moving left\n");
motion_west->s = ISS_ON;
}
else
{
motion_west->s = ISS_OFF;
}
ISwitch *motion_east = IUFindSwitch(switchVector, "MOTION_EAST");
if (!motion_east)
{
rig_debug(RIG_DEBUG_ERR, "indi: unable to find switch member MOTION_RIGHT\n");
return -RIG_EPROTO;
}
if (direction & ROT_MOVE_RIGHT)
{
rig_debug(RIG_DEBUG_VERBOSE, "indi: moving right\n");
motion_east->s = ISS_ON;
}
else
{
motion_east->s = ISS_OFF;
}
sendNewSwitch(switchVector);
return RIG_OK;
}
int RotINDIClient::stop()
{
if (!mTelescope || !mTelescope->isConnected())
{
rig_debug(RIG_DEBUG_ERR, "indi: telescope not connected\n");
return -RIG_EIO;
}
ISwitchVectorProperty *switchVector =
mTelescope->getSwitch("TELESCOPE_ABORT_MOTION");
if (!switchVector)
{
rig_debug(RIG_DEBUG_ERR,
"indi: unable to find telescope or TELESCOPE_ABORT_MOTION switch\n");
return -RIG_EPROTO;
}
ISwitch *sw = IUFindSwitch(switchVector, "ABORT");
if (!sw)
{
rig_debug(RIG_DEBUG_ERR, "indi: unable to find switch member ABORT_MOTION\n");
return -RIG_EPROTO;
}
sw->s = ISS_ON;
sendNewSwitch(switchVector);
return RIG_OK;
}
int RotINDIClient::park()
{
if (!mTelescope)
{
return -RIG_EIO;
}
if (!mTelescope->isConnected())
{
rig_debug(RIG_DEBUG_ERR, "indi: telescope not connected\n");
return -RIG_EIO;
}
ISwitchVectorProperty *switchVector = mTelescope->getSwitch("TELESCOPE_PARK");
if (!switchVector)
{
rig_debug(RIG_DEBUG_ERR,
"indi: unable to find telescope or TELESCOPE_PARK switch\n");
return -RIG_EPROTO;
}
ISwitch *unpark = IUFindSwitch(switchVector, "UNPARK");
if (!unpark)
{
rig_debug(RIG_DEBUG_ERR, "indi: unable to find switch member UNPARK\n");
return -RIG_EPROTO;
}
unpark->s = ISS_OFF;
ISwitch *park = IUFindSwitch(switchVector, "PARK");
if (!park)
{
rig_debug(RIG_DEBUG_ERR, "indi: unable to find switch member PARK\n");
return -RIG_EPROTO;
}
park->s = ISS_ON;
sendNewSwitch(switchVector);
return RIG_OK;
}
int RotINDIClient::unPark()
{
if (!mTelescope || !mTelescope->isConnected())
{
rig_debug(RIG_DEBUG_ERR, "indi: telescope not connected\n");
return -RIG_EIO;
}
ISwitchVectorProperty *switchVector = mTelescope->getSwitch("TELESCOPE_PARK");
if (!switchVector)
{
rig_debug(RIG_DEBUG_ERR,
"indi: unable to find telescope or TELESCOPE_PARK switch\n");
return -RIG_EPROTO;
}
ISwitch *park2 = IUFindSwitch(switchVector, "PARK");
if (!park2)
{
rig_debug(RIG_DEBUG_ERR, "indi: unable to find switch member PARK\n");
return -RIG_EPROTO;
}
park2->s = ISS_OFF;
ISwitch *unpark = IUFindSwitch(switchVector, "UNPARK");
if (!unpark)
{
rig_debug(RIG_DEBUG_ERR, "indi: unable to find switch member UNPARK\n");
return -RIG_EPROTO;
}
unpark->s = ISS_ON;
sendNewSwitch(switchVector);
return RIG_OK;
}
void RotINDIClient::position(azimuth_t *az, elevation_t *el)
{
*az = mAz;
*el = mEl;
}
double RotINDIClient::getPositionDiffBetween(double deg1, double deg2)
{
return fabs(deg1 - deg2);
}
double RotINDIClient::getPositionDiffOutside(double deg1, double deg2,
double minDeg, double maxDeg)
{
if (deg1 < deg2)
{
return getPositionDiffBetween(minDeg, deg1) + getPositionDiffBetween(deg2,
maxDeg);
}
return getPositionDiffBetween(minDeg, deg2) + getPositionDiffBetween(deg1,
maxDeg);
}
double RotINDIClient::getPositionDiff(double deg1, double deg2, double minDeg,
double maxDeg)
{
double between = getPositionDiffBetween(deg1, deg2);
double outside = getPositionDiffOutside(deg1, deg2, minDeg, maxDeg);
if (between < outside)
{
return between;
}
return outside;
}
int RotINDIClient::setPosition(azimuth_t az, elevation_t el)
{
if (!mTelescope || !mTelescope->isConnected())
{
rig_debug(RIG_DEBUG_ERR, "indi: telescope not connected\n");
return -RIG_EIO;
}
if (fabs(mDstAz - az) < 0.001 && fabs(mDstEl - el) < 0.001)
{
rig_debug(RIG_DEBUG_VERBOSE,
"indi: ignoring new position, already approaching\n");
return RIG_OK;
}
// Each coordinate set command stops and restarts the rotator, so we have to avoid unnecessary
// set calls by defining a range of distance from the new az/el coords which we ignore until
// we are closer to the new az/el coords than this value.
const int quickJumpRangeDegrees = 10;
double currDstNewDstAzDiff = getPositionDiff(mDstAz, az, 0, 360);
double currDstNewDstElDiff = getPositionDiff(mDstEl, el, -90, 90);
double currDstNewDstDistance = sqrt(pow(currDstNewDstAzDiff,
2) + pow(currDstNewDstElDiff, 2));
double currPosNewDstAzDiff = getPositionDiff(mAz, az, 0, 360);
double currPosNewDstElDiff = getPositionDiff(mEl, el, -90, 90);
double currPosNewDstDistance = sqrt(pow(currPosNewDstAzDiff,
2) + pow(currPosNewDstElDiff, 2));
if (currDstNewDstDistance < quickJumpRangeDegrees
&& currPosNewDstDistance > quickJumpRangeDegrees)
{
rig_debug(RIG_DEBUG_VERBOSE,
"indi: ignoring new position, approaching quickly, newDst/currDst distance: %f newDst/currPos distance: %f\n",
currDstNewDstDistance, currPosNewDstDistance);
return RIG_OK;
}
rig_debug(RIG_DEBUG_VERBOSE, "indi: setting position to az: %f el: %f\n", az,
el);
mDstAz = az;
mDstEl = el;
ISwitchVectorProperty *switchVector = mTelescope->getSwitch("ON_COORD_SET");
if (!switchVector)
{
rig_debug(RIG_DEBUG_ERR,
"indi: unable to find telescope or ON_COORD_SET switch\n");
return -RIG_EPROTO;
}
ISwitch *slew = IUFindSwitch(switchVector, "SLEW");
if (!slew)
{
rig_debug(RIG_DEBUG_ERR, "indi: unable to find switch member SLEW\n");
return -RIG_EPROTO;
}
slew->s = ISS_OFF;
ISwitch *track = IUFindSwitch(switchVector, "TRACK");
if (!track)
{
rig_debug(RIG_DEBUG_ERR, "indi: unable to find switch member TRACK\n");
return -RIG_EPROTO;
}
track->s = ISS_ON;
ISwitch *sync = IUFindSwitch(switchVector, "SYNC");
if (!sync)
{
rig_debug(RIG_DEBUG_ERR, "indi: unable to find switch member SYNC\n");
return -RIG_EPROTO;
}
sync->s = ISS_OFF;
sendNewSwitch(switchVector);
INumberVectorProperty *property = mTelescope->getNumber("HORIZONTAL_COORD");
if (!property)
{
rig_debug(RIG_DEBUG_ERR,
"indi: unable to find telescope or HORIZONTAL_COORD property\n");
return -RIG_EPROTO;
}
property->np[0].value = az;
property->np[1].value = el;
sendNewNumber(property);
return RIG_OK;
}
// cppcheck-suppress unusedFunction
void RotINDIClient::removeDevice(INDI::BaseDevice *dp) {}
// cppcheck-suppress unusedFunction
void RotINDIClient::newProperty(INDI::Property *property)
{
std::string name(property->getName());
if (!mTelescope && name == "TELESCOPE_INFO")
{
mTelescope = property->getBaseDevice();
rig_debug(RIG_DEBUG_VERBOSE, "indi: using device %s\n",
mTelescope->getDeviceName());
watchDevice(mTelescope->getDeviceName());
if (!mTelescope->isConnected())
{
connectDevice(mTelescope->getDeviceName());
}
mDstAz = INT_MAX;
mDstEl = INT_MAX;
}
if (name == "HORIZONTAL_COORD")
{
mAz = property->getNumber()->np[0].value;
mEl = property->getNumber()->np[1].value;
}
}
// cppcheck-suppress unusedFunction
void RotINDIClient::removeProperty(INDI::Property *property) {}
// cppcheck-suppress unusedFunction
void RotINDIClient::newBLOB(IBLOB *bp) {}
// cppcheck-suppress unusedFunction
void RotINDIClient::newSwitch(ISwitchVectorProperty *svp) {}
// cppcheck-suppress unusedFunction
void RotINDIClient::newNumber(INumberVectorProperty *nvp)
{
std::string name(nvp->name);
if (name == "HORIZONTAL_COORD")
{
mAz = nvp->np[0].value;
mEl = nvp->np[1].value;
}
}
// cppcheck-suppress unusedFunction
void RotINDIClient::newMessage(INDI::BaseDevice *dp, int messageID) {}
// cppcheck-suppress unusedFunction
void RotINDIClient::newText(ITextVectorProperty *tvp) {}
// cppcheck-suppress unusedFunction
void RotINDIClient::newLight(ILightVectorProperty *lvp) {}
// cppcheck-suppress unusedFunction
void RotINDIClient::newDevice(INDI::BaseDevice *dp) {}
// cppcheck-suppress unusedFunction
void RotINDIClient::serverConnected()
{
rig_debug(RIG_DEBUG_VERBOSE, "indi: server connected\n");
}
// cppcheck-suppress unusedFunction
void RotINDIClient::serverDisconnected(int exit_code)
{
rig_debug(RIG_DEBUG_VERBOSE, "indi: server disconnected\n");
}
const char *RotINDIClient::getInfo(void)
{
if (mTelescope && mTelescope->isConnected())
{
static char info[128];
snprintf(info, sizeof(info), "using INDI device %s",
mTelescope->getDeviceName());
return info;
}
return "no INDI device connected";
}
void RotINDIClient::close(void)
{
if (mTelescope && mTelescope->isConnected())
{
disconnectDevice(mTelescope->getDeviceName());
}
disconnectServer();
}
extern "C" int indi_wrapper_set_position(ROT *rot, azimuth_t az,
elevation_t el)
{
int res;
rig_debug(RIG_DEBUG_TRACE, "%s called: az=%f el=%f\n", __func__, az, el);
res = indi_wrapper_client->unPark();
if (res != RIG_OK)
{
return res;
}
return indi_wrapper_client->setPosition(az, el);
}
extern "C" int indi_wrapper_get_position(ROT *rot, azimuth_t *az,
elevation_t *el)
{
rig_debug(RIG_DEBUG_TRACE, "%s called\n", __func__);
indi_wrapper_client->position(az, el);
return RIG_OK;
}
extern "C" int indi_wrapper_stop(ROT *rot)
{
rig_debug(RIG_DEBUG_TRACE, "%s called\n", __func__);
return indi_wrapper_client->stop();
}
extern "C" int indi_wrapper_park(ROT *rot)
{
rig_debug(RIG_DEBUG_TRACE, "%s called\n", __func__);
return indi_wrapper_client->park();
}
extern "C" int indi_wrapper_move(ROT *rot, int direction, int speed)
{
rig_debug(RIG_DEBUG_TRACE, "%s called\n", __func__);
return indi_wrapper_client->move(direction, speed);
}
extern "C" const char *indi_wrapper_get_info(ROT *rot)
{
rig_debug(RIG_DEBUG_TRACE, "%s called\n", __func__);
return indi_wrapper_client->getInfo();
}
extern "C" int indi_wrapper_open(ROT *rot)
{
rig_debug(RIG_DEBUG_TRACE, "%s called\n", __func__);
indi_wrapper_client->setServer("localhost", 7624);
if (!indi_wrapper_client->connectServer())
{
rig_debug(RIG_DEBUG_ERR, "indi: server refused connection\n");
return -RIG_EPROTO;
}
return RIG_OK;
}
extern "C" int indi_wrapper_close(ROT *rot)
{
rig_debug(RIG_DEBUG_TRACE, "%s called\n", __func__);
indi_wrapper_client->close();
return RIG_OK;
}
|