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 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
|
/*
* ExosIIMountControl.hpp
*
* Copyright 2020 Kevin Krüger <kkevin@gmx.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
#pragma once
#include <cstdint>
#include <vector>
#include <string>
#include <limits>
#include <mutex>
#include <condition_variable>
#include <thread>
#include "config.h"
#include "StateMachine.hpp"
#include "CriticalData.hpp"
#include "SerialCommand.hpp"
#include "SerialCommandTransceiver.hpp"
#include "INotifyPointingCoordinatesReceived.hpp"
//The manual states a tracking speed for 0.004°/s everything above is
//considered slewing.
#define TRACK_SLEW_THRESHOLD (0.0045)
#define EXPR_TO_STRING(x) #x
namespace TelescopeMountControl
{
//enum representing the telescope mount state
enum TelescopeMountState
{
//initial state, no serial connection established
Disconnected = 0,
//initial state if the serial connection was established,
//the mount did not report any pointing coordinates yet.
//if an error occurs, the telescope also will be in this state.
Unknown = 1,
//The driver received at least one response from the mount,
//indicating the communication is working, and the mount accepts commands.
Connected = 2,
//If the user issues the park command the telescope likely needs to slew to the parking position,
//this is determined by the differentials of the positions information send by the controller.
ParkingIssued = 3,
//If the status report messages arrive and the telescope is not moving, this is the assumed state.
//the telescope is assumed in park/initial position according to manual
//or if the "park" command is issued.
Parked = 4,
//The controller autonomously desides the motion speeds, and does not report any "state".
//This state is assumed if the motion speeds exceed a certain threshold.
Slewing = 5,
//This state is assumed if the telescope is moves below the slewing threshold.
//its also the default when a "goto" is issued, since the telescope controller automatically tracks the issued coordinates.
Tracking = 6,
//when tracking an object, move to a direction.
MoveWhileTracking = 7,
//This state is reached when issuing the stop command while slewing or tracking.
Idle = 8,
//The error state of the telescope any none defined transition will reset to this state.
FailSafe = 9,
};
//Enum reqresenting the virtuals signal the telescope issues.
enum TelescopeSignals
{
//connect to the telescope.
//need to be exclusive from states so there is no hazzling with communtativity of the xor operator of hashing functions.
Connect = TelescopeMountState::FailSafe + 1,
//disconnect from the telescope.
Disconnect,
//request the geolocation initially.
RequestedGeoLocationReceived,
//when the geolocation is requested this signal gets issued if the controller reports the pointing coordinates.
InitialPointingCoordinatesReceived,
//stop any motion of the telescope.
Stop,
//goto a position and track the position when reached.
GoTo,
//park the telescope.
Park,
//issued when the parking position is reached.
ParkingPositionReached,
//motion above tracking threshold.
Slew,
//motion below tracking threshold.
Track,
//motion to tracking target, when reached this is signaled.
TrackingTargetReached,
//move a certain direction while tracking.
StartMotion,
//stop moving in a certain direction while tracking.
StopMotion,
//used as a token to represent an initialized yet invalid signal.
INVALID,
};
//type definition of the state machine type for convinience.
typedef StateMachine<TelescopeMountState, TelescopeSignals, IStateNotification<TelescopeMountState, TelescopeSignals>>
MountStateMachine;
//store the motion state while tracking.
struct MotionState
{
//move in what direction
SerialDeviceControl::SerialCommandID MotionDirection;
//how many messages per second
uint16_t CommandsPerSecond;
};
//These types have to inherit/implement:
//-The ISerialInterface.hpp as Interface type.
template<class InterfaceType>
class ExosIIMountControl :
public SerialDeviceControl::SerialCommandTransceiver<InterfaceType, TelescopeMountControl::ExosIIMountControl<InterfaceType>>,
public SerialDeviceControl::INotifyPointingCoordinatesReceived,
public IStateNotification<TelescopeMountState, TelescopeSignals>
{
public:
//create a exos controller using a reference of a particular serial implementation.
ExosIIMountControl(InterfaceType &interfaceImplementation) :
SerialDeviceControl::SerialCommandTransceiver<InterfaceType, TelescopeMountControl::ExosIIMountControl<InterfaceType>>
(interfaceImplementation, *this),
mIsMotionControlThreadRunning(false),
mIsMotionControlRunning(false),
mMountStateMachine(*this, TelescopeMountState::Disconnected, TelescopeMountState::FailSafe)
{
SerialDeviceControl::EquatorialCoordinates initialCoordinates;
initialCoordinates.RightAscension = std::numeric_limits<float>::quiet_NaN();
initialCoordinates.Declination = std::numeric_limits<float>::quiet_NaN();
mCurrentPointingCoordinates.Set(initialCoordinates);
mSiteLocationCoordinates.Set(initialCoordinates);
MotionState initialState;
initialState.MotionDirection = SerialDeviceControl::SerialCommandID::NULL_COMMAND_ID;
initialState.CommandsPerSecond = 0;
mMotionState.Set(initialState);
//initialize statemachine:
mMountStateMachine.AddFinalState(TelescopeMountState::Disconnected);
//build transition table:
mMountStateMachine.AddTransition(TelescopeMountState::Disconnected, TelescopeSignals::Disconnect,
TelescopeMountState::Disconnected);
mMountStateMachine.AddTransition(TelescopeMountState::Disconnected, TelescopeSignals::Connect,
TelescopeMountState::Unknown);
mMountStateMachine.AddTransition(TelescopeMountState::Unknown, TelescopeSignals::Disconnect,
TelescopeMountState::Disconnected);
mMountStateMachine.AddTransition(TelescopeMountState::Unknown, TelescopeSignals::InitialPointingCoordinatesReceived,
TelescopeMountState::Connected);
mMountStateMachine.AddTransition(TelescopeMountState::Unknown, TelescopeSignals::RequestedGeoLocationReceived,
TelescopeMountState::Connected);
mMountStateMachine.AddTransition(TelescopeMountState::Connected, TelescopeSignals::Connect, TelescopeMountState::Connected);
mMountStateMachine.AddTransition(TelescopeMountState::Connected, TelescopeSignals::RequestedGeoLocationReceived,
TelescopeMountState::Parked);
mMountStateMachine.AddTransition(TelescopeMountState::Connected, TelescopeSignals::InitialPointingCoordinatesReceived,
TelescopeMountState::Parked);
mMountStateMachine.AddTransition(TelescopeMountState::Connected, TelescopeSignals::Track, TelescopeMountState::Tracking);
mMountStateMachine.AddTransition(TelescopeMountState::Connected, TelescopeSignals::Slew, TelescopeMountState::Slewing);
mMountStateMachine.AddTransition(TelescopeMountState::Connected, TelescopeSignals::Disconnect,
TelescopeMountState::Disconnected);
mMountStateMachine.AddTransition(TelescopeMountState::Parked, TelescopeSignals::Park, TelescopeMountState::Parked);
mMountStateMachine.AddTransition(TelescopeMountState::Parked, TelescopeSignals::Disconnect,
TelescopeMountState::Disconnected);
mMountStateMachine.AddTransition(TelescopeMountState::Parked, TelescopeSignals::Stop, TelescopeMountState::Parked);
mMountStateMachine.AddTransition(TelescopeMountState::Parked, TelescopeSignals::GoTo, TelescopeMountState::Slewing);
mMountStateMachine.AddTransition(TelescopeMountState::Idle, TelescopeSignals::Stop, TelescopeMountState::Idle);
mMountStateMachine.AddTransition(TelescopeMountState::Idle, TelescopeSignals::GoTo, TelescopeMountState::Slewing);
mMountStateMachine.AddTransition(TelescopeMountState::Idle, TelescopeSignals::Park, TelescopeMountState::ParkingIssued);
mMountStateMachine.AddTransition(TelescopeMountState::Idle, TelescopeSignals::Disconnect,
TelescopeMountState::Disconnected);
mMountStateMachine.AddTransition(TelescopeMountState::ParkingIssued, TelescopeSignals::Park,
TelescopeMountState::ParkingIssued);
mMountStateMachine.AddTransition(TelescopeMountState::ParkingIssued, TelescopeSignals::Slew,
TelescopeMountState::ParkingIssued);
mMountStateMachine.AddTransition(TelescopeMountState::ParkingIssued, TelescopeSignals::Track,
TelescopeMountState::ParkingIssued);
mMountStateMachine.AddTransition(TelescopeMountState::ParkingIssued, TelescopeSignals::ParkingPositionReached,
TelescopeMountState::Parked);
mMountStateMachine.AddTransition(TelescopeMountState::ParkingIssued, TelescopeSignals::Stop, TelescopeMountState::Idle);
mMountStateMachine.AddTransition(TelescopeMountState::ParkingIssued, TelescopeSignals::Disconnect,
TelescopeMountState::Disconnected);
mMountStateMachine.AddTransition(TelescopeMountState::Slewing, TelescopeSignals::Stop, TelescopeMountState::Idle);
mMountStateMachine.AddTransition(TelescopeMountState::Slewing, TelescopeSignals::GoTo, TelescopeMountState::Slewing);
mMountStateMachine.AddTransition(TelescopeMountState::Slewing, TelescopeSignals::Track, TelescopeMountState::Tracking);
mMountStateMachine.AddTransition(TelescopeMountState::Slewing, TelescopeSignals::Slew, TelescopeMountState::Slewing);
mMountStateMachine.AddTransition(TelescopeMountState::Slewing, TelescopeSignals::Park, TelescopeMountState::ParkingIssued);
mMountStateMachine.AddTransition(TelescopeMountState::Slewing, TelescopeSignals::Disconnect,
TelescopeMountState::Disconnected);
mMountStateMachine.AddTransition(TelescopeMountState::Tracking, TelescopeSignals::Track, TelescopeMountState::Tracking);
mMountStateMachine.AddTransition(TelescopeMountState::Tracking, TelescopeSignals::Slew, TelescopeMountState::Slewing);
mMountStateMachine.AddTransition(TelescopeMountState::Tracking, TelescopeSignals::GoTo, TelescopeMountState::Slewing);
mMountStateMachine.AddTransition(TelescopeMountState::Tracking, TelescopeSignals::Stop, TelescopeMountState::Idle);
mMountStateMachine.AddTransition(TelescopeMountState::Tracking, TelescopeSignals::StartMotion,
TelescopeMountState::MoveWhileTracking);
mMountStateMachine.AddTransition(TelescopeMountState::Tracking, TelescopeSignals::Park, TelescopeMountState::ParkingIssued);
mMountStateMachine.AddTransition(TelescopeMountState::Tracking, TelescopeSignals::Disconnect,
TelescopeMountState::Disconnected);
mMountStateMachine.AddTransition(TelescopeMountState::MoveWhileTracking, TelescopeSignals::StopMotion,
TelescopeMountState::Tracking);
mMountStateMachine.AddTransition(TelescopeMountState::MoveWhileTracking, TelescopeSignals::StartMotion,
TelescopeMountState::MoveWhileTracking);
mMountStateMachine.AddTransition(TelescopeMountState::MoveWhileTracking, TelescopeSignals::Stop, TelescopeMountState::Idle);
mMountStateMachine.AddTransition(TelescopeMountState::MoveWhileTracking, TelescopeSignals::StopMotion,
TelescopeMountState::Tracking);
mMountStateMachine.AddTransition(TelescopeMountState::MoveWhileTracking, TelescopeSignals::GoTo,
TelescopeMountState::Slewing);
mMountStateMachine.AddTransition(TelescopeMountState::MoveWhileTracking, TelescopeSignals::Track,
TelescopeMountState::MoveWhileTracking);
mMountStateMachine.AddTransition(TelescopeMountState::MoveWhileTracking, TelescopeSignals::Slew,
TelescopeMountState::MoveWhileTracking);
mMountStateMachine.AddTransition(TelescopeMountState::MoveWhileTracking, TelescopeSignals::Park,
TelescopeMountState::ParkingIssued);
mMountStateMachine.AddTransition(TelescopeMountState::MoveWhileTracking, TelescopeSignals::Disconnect,
TelescopeMountState::Disconnected);
mMountStateMachine.Reset();
}
virtual ~ExosIIMountControl()
{
SerialDeviceControl::SerialCommandTransceiver<InterfaceType, TelescopeMountControl::ExosIIMountControl<InterfaceType>>::Stop();
}
//open the serial connection and start the serial reporting.
virtual bool Start()
{
mMountStateMachine.Reset();
mMountStateMachine.DoTransition(TelescopeSignals::Connect);
SerialDeviceControl::SerialCommandTransceiver<InterfaceType, TelescopeMountControl::ExosIIMountControl<InterfaceType>>::Start();
mMotionCommandThread = std::thread(&ExosIIMountControl<InterfaceType>::MotionControlThreadFunction, this);
return true;
}
//Stop the serial reporting and close the serial port.
virtual bool Stop()
{
if(mIsMotionControlThreadRunning.Get())
{
mIsMotionControlThreadRunning.Set(false);
StopMotionToDirection();
mMotionCommandThread.join();
}
bool rc = DisconnectSerial();
rc |= SerialDeviceControl::SerialCommandTransceiver<InterfaceType, TelescopeMountControl::ExosIIMountControl<InterfaceType>>::Stop();
return rc;
}
bool StartMotionToDirection(
SerialDeviceControl::SerialCommandID direction,
uint16_t commandsPerSecond
)
{
//this only works while tracking a target
{
std::lock_guard<std::mutex> notifyLock(mMotionCommandControlMutex);
MotionState initialState;
initialState.MotionDirection = direction;
initialState.CommandsPerSecond = commandsPerSecond;
mMotionState.Set(initialState);
mIsMotionControlRunning.Set(true);
}
mMotionControlCondition.notify_all();
return mMountStateMachine.DoTransition(TelescopeSignals::StartMotion);
}
bool StopMotionToDirection()
{
//this changes back to tracking
MotionState stopState;
stopState.MotionDirection = SerialDeviceControl::SerialCommandID::NULL_COMMAND_ID;
stopState.CommandsPerSecond = 0;
mMotionState.Set(stopState);
mIsMotionControlRunning.Set(false);
mMotionControlCondition.notify_all();
if(mMountStateMachine.CurrentState() != TelescopeMountState::MoveWhileTracking)
{
std::cerr << "motion already disabled." << std::endl;
return true;
}
else
{
return mMountStateMachine.DoTransition(TelescopeSignals::StopMotion);
}
}
//Disconnect from the mount.
bool DisconnectSerial()
{
std::vector<uint8_t> messageBuffer;
if(SerialDeviceControl::SerialCommand::GetDisconnectCommandMessage(messageBuffer))
{
bool rc = SerialDeviceControl::SerialCommandTransceiver<InterfaceType, TelescopeMountControl::ExosIIMountControl<InterfaceType>>::SendMessageBuffer(
&messageBuffer[0], 0, messageBuffer.size());
return rc && mMountStateMachine.DoTransition(TelescopeSignals::Disconnect);
}
else
{
std::cerr << "DisconnectSerial: Failed!" << std::endl;
return false;
}
}
//stop any motion of the telescope.
bool StopMotion()
{
std::vector<uint8_t> messageBuffer;
if(SerialDeviceControl::SerialCommand::GetStopMotionCommandMessage(messageBuffer))
{
bool rc = SerialDeviceControl::SerialCommandTransceiver<InterfaceType, TelescopeMountControl::ExosIIMountControl<InterfaceType>>::SendMessageBuffer(
&messageBuffer[0], 0, messageBuffer.size());
return rc && mMountStateMachine.DoTransition(TelescopeSignals::Stop);
}
else
{
std::cerr << "StopMotion: Failed!" << std::endl;
return false;
}
}
//Order to telescope to go to the parking state.
bool ParkPosition()
{
std::vector<uint8_t> messageBuffer;
if(SerialDeviceControl::SerialCommand::GetParkCommandMessage(messageBuffer))
{
bool rc = SerialDeviceControl::SerialCommandTransceiver<InterfaceType, TelescopeMountControl::ExosIIMountControl<InterfaceType>>::SendMessageBuffer(
&messageBuffer[0], 0, messageBuffer.size());
return rc && mMountStateMachine.DoTransition(TelescopeSignals::Park);
}
else
{
std::cerr << "ParkPosition: Failed!" << std::endl;
return false;
}
}
//GoTo and track the sky position represented by the equatorial coordinates.
bool GoTo(
float rightAscension,
float declination
)
{
std::vector<uint8_t> messageBuffer;
if(SerialDeviceControl::SerialCommand::GetGotoCommandMessage(messageBuffer, rightAscension, declination))
{
bool rc = SerialDeviceControl::SerialCommandTransceiver<InterfaceType, TelescopeMountControl::ExosIIMountControl<InterfaceType>>::SendMessageBuffer(
&messageBuffer[0], 0, messageBuffer.size());
return rc && mMountStateMachine.DoTransition(TelescopeSignals::GoTo);
}
else
{
//TODO: error message
std::cerr << "GoTo: Failed!" << std::endl;
return false;
}
}
//GoTo and track the sky position represented by the equatorial coordinates.
bool Sync(
float rightAscension,
float declination
)
{
std::vector<uint8_t> messageBuffer;
if(SerialDeviceControl::SerialCommand::GetSyncCommandMessage(messageBuffer, rightAscension, declination))
{
std::cerr << "Sent Sync command!" << std::endl;
return SerialDeviceControl::SerialCommandTransceiver<InterfaceType, TelescopeMountControl::ExosIIMountControl<InterfaceType>>::SendMessageBuffer(
&messageBuffer[0], 0, messageBuffer.size());
//return rc && mMountStateMachine.DoTransition(TelescopeSignals::GoTo);
}
else
{
//TODO: error message
std::cerr << "Sync: Failed!" << std::endl;
return false;
}
}
//Set the location of the telesope, using decimal latitude and longitude parameters.
//This does not change to state of the telescope.
bool SetSiteLocation(
float latitude,
float longitude
)
{
std::vector<uint8_t> messageBuffer;
if(SerialDeviceControl::SerialCommand::GetSetSiteLocationCommandMessage(messageBuffer, latitude, longitude))
{
return SerialDeviceControl::SerialCommandTransceiver<InterfaceType, TelescopeMountControl::ExosIIMountControl<InterfaceType>>::SendMessageBuffer(
&messageBuffer[0], 0, messageBuffer.size());
}
else
{
std::cerr << "SetSiteLocation: Failed!" << std::endl;
return false;
}
}
//Set the location of the telesope, using decimal latitude and longitude parameters.
//This does not change to state of the telescope.
bool RequestSiteLocation()
{
std::vector<uint8_t> messageBuffer;
if(SerialDeviceControl::SerialCommand::GetGetSiteLocationCommandMessage(messageBuffer))
{
//std::cout << "Message sent!" << std::endl;
return SerialDeviceControl::SerialCommandTransceiver<InterfaceType, TelescopeMountControl::ExosIIMountControl<InterfaceType>>::SendMessageBuffer(
&messageBuffer[0], 0, messageBuffer.size());
}
else
{
//TODO: error message
std::cerr << "RequestSiteLocation: Failed!" << std::endl;
return false;
}
}
//issue the set time command, using date and time parameters. This does not change the state of the telescope.
bool SetDateTime(
uint16_t year,
uint8_t month,
uint8_t day,
uint8_t hour,
uint8_t minute,
uint8_t second,
int8_t utc_offset
)
{
std::vector<uint8_t> messageBuffer;
if(SerialDeviceControl::SerialCommand::GetSetDateTimeCommandMessage(messageBuffer, year, month, day, hour, minute, second, utc_offset))
{
return SerialDeviceControl::SerialCommandTransceiver<InterfaceType, TelescopeMountControl::ExosIIMountControl<InterfaceType>>::SendMessageBuffer(
&messageBuffer[0], 0, messageBuffer.size());
}
else
{
//TODO:error message.
std::cerr << "SetDateTime: Failed!" << std::endl;
return false;
}
}
template<SerialDeviceControl::SerialCommandID Direction>
bool GuideDirection()
{
std::vector<uint8_t> messageBuffer;
if(SerialDeviceControl::SerialCommand::GetMoveWhileTrackingCommandMessage(messageBuffer,Direction))
{
return SerialDeviceControl::SerialCommandTransceiver<InterfaceType,TelescopeMountControl::ExosIIMountControl<InterfaceType>>::SendMessageBuffer(&messageBuffer[0],0,messageBuffer.size());
}
else
{
//TODO:error message.
return false;
}
}
bool GuideNorth()
{
return GuideDirection<SerialDeviceControl::SerialCommandID::MOVE_NORTH_COMMAND_ID>();
}
bool GuideSouth()
{
return GuideDirection<SerialDeviceControl::SerialCommandID::MOVE_SOUTH_COMMAND_ID>();
}
bool GuideEast()
{
return GuideDirection<SerialDeviceControl::SerialCommandID::MOVE_EAST_COMMAND_ID>();
}
bool GuideWest()
{
return GuideDirection<SerialDeviceControl::SerialCommandID::MOVE_WEST_COMMAND_ID>();
}
template<SerialDeviceControl::SerialCommandID Direction>
bool MoveDirection()
{
std::vector<uint8_t> messageBuffer;
if(SerialDeviceControl::SerialCommand::GetMoveWhileTrackingCommandMessage(messageBuffer, Direction))
{
bool rc = SerialDeviceControl::SerialCommandTransceiver<InterfaceType, TelescopeMountControl::ExosIIMountControl<InterfaceType>>::SendMessageBuffer(
&messageBuffer[0], 0, messageBuffer.size());
return rc && mMountStateMachine.DoTransition(TelescopeSignals::StartMotion);
}
else
{
//TODO:error message.
return false;
}
}
bool MoveNorth()
{
return MoveDirection<SerialDeviceControl::SerialCommandID::MOVE_NORTH_COMMAND_ID>();
}
bool MoveSouth()
{
return MoveDirection<SerialDeviceControl::SerialCommandID::MOVE_SOUTH_COMMAND_ID>();
}
bool MoveEast()
{
return MoveDirection<SerialDeviceControl::SerialCommandID::MOVE_EAST_COMMAND_ID>();
}
bool MoveWest()
{
return MoveDirection<SerialDeviceControl::SerialCommandID::MOVE_WEST_COMMAND_ID>();
}
//Called each time a pair of coordinates was received from the serial interface.
virtual void OnPointingCoordinatesReceived(
float right_ascension,
float declination
)
{
//std::cerr << "Received data : RA: " << right_ascension << " DEC:" << declination << std::endl;
SerialDeviceControl::EquatorialCoordinates lastCoordinates = GetPointingCoordinates();
SerialDeviceControl::EquatorialCoordinates coordinatesReceived;
coordinatesReceived.RightAscension = right_ascension;
coordinatesReceived.Declination = declination;
bool coordinatesNotNan = !std::isnan(right_ascension) && !std::isnan(declination);
mCurrentPointingCoordinates.Set(coordinatesReceived);
SerialDeviceControl::EquatorialCoordinates delta = SerialDeviceControl::EquatorialCoordinates::Delta(lastCoordinates,
coordinatesReceived);
float absDelta = SerialDeviceControl::EquatorialCoordinates::Absolute(delta);
TelescopeMountState currentState = mMountStateMachine.CurrentState();
TelescopeSignals signal = TelescopeSignals::INVALID;
switch(currentState)
{
case TelescopeMountState::Unknown:
if(coordinatesNotNan)
{
signal = TelescopeSignals::InitialPointingCoordinatesReceived;
}
break;
case TelescopeMountState::Connected:
if(std::isnan(absDelta))
{
break;
}
if(!std::isnan(absDelta) && (absDelta > 0.0f))
{
//see if the telescope is moving initially -> previous/externally triggered motion.
if(absDelta > TRACK_SLEW_THRESHOLD)
{
signal = TelescopeSignals::Slew;
}
else
{
signal = TelescopeSignals::Track;
}
}
else
{
//assume parked otherwise.
signal = TelescopeSignals::InitialPointingCoordinatesReceived;
}
break;
case TelescopeMountState::ParkingIssued:
if(absDelta > 0.0f)
{
if(absDelta > TRACK_SLEW_THRESHOLD)
{
signal = TelescopeSignals::Slew;
}
else
{
signal = TelescopeSignals::Track;
}
}
else
{
signal = TelescopeSignals::ParkingPositionReached;
}
break;
case TelescopeMountState::Idle:
break;
case TelescopeMountState::Parked:
break;
case TelescopeMountState::Tracking:
if(absDelta > 0.0f)
{
//may be externally triggered motion
if(absDelta > TRACK_SLEW_THRESHOLD)
{
signal = TelescopeSignals::Slew;
}
else
{
signal = TelescopeSignals::Track;
}
}
break;
case TelescopeMountState::Slewing:
if(absDelta > 0.0f)
{
//may be externally triggered motion
if(absDelta > TRACK_SLEW_THRESHOLD)
{
signal = TelescopeSignals::Slew;
}
else
{
signal = TelescopeSignals::Track;
}
}
default:
break;
}
if(signal != TelescopeSignals::INVALID)
{
mMountStateMachine.DoTransition(signal);
}
}
//Called each time a pair of geo coordinates was received from
//the serial inferface. This happends only by active request
//(GET_SITE_LOCATION_COMMAND_ID)
virtual void OnSiteLocationCoordinatesReceived(
float latitude,
float longitude
)
{
std::cerr << "Received data : LAT: " << latitude << " LON:" << longitude << std::endl;
SerialDeviceControl::EquatorialCoordinates coordinatesReceived;
coordinatesReceived.RightAscension = latitude;
coordinatesReceived.Declination = longitude;
mSiteLocationCoordinates.Set(coordinatesReceived);
mMountStateMachine.DoTransition(TelescopeSignals::RequestedGeoLocationReceived);
}
virtual void OnTransitionChanged(
TelescopeMountState fromState,
TelescopeSignals signal,
TelescopeMountState toState
)
{
if(fromState != toState)
{
std::cerr << "Transition : (" << StateToString(fromState) << "," << SignalToString(signal) << ") -> " << StateToString(
toState) << std::endl;
}
}
virtual void OnErrorStateReached(
TelescopeMountState fromState,
TelescopeSignals signal
)
{
std::cerr << "Reached Error/Fail Safe State: most likly an undefined transition occurred!" << std::endl;
std::cerr << "Transition : (" << StateToString(fromState) << "," << SignalToString(signal) << ") -> ??? tripped this error!"
<< std::endl;
}
//return the current telescope state.
TelescopeMountState GetTelescopeState()
{
return mMountStateMachine.CurrentState();
}
//return the current pointing coordinates.
SerialDeviceControl::EquatorialCoordinates GetPointingCoordinates()
{
return mCurrentPointingCoordinates.Get();
}
//return the current pointing coordinates.
SerialDeviceControl::EquatorialCoordinates GetSiteLocation()
{
return mSiteLocationCoordinates.Get();
}
private:
//mutex protected container for the current coordinates the telescope is pointing at.
SerialDeviceControl::CriticalData<SerialDeviceControl::EquatorialCoordinates> mCurrentPointingCoordinates;
//mutex protected container for the current site location set in the telescope.
SerialDeviceControl::CriticalData<SerialDeviceControl::EquatorialCoordinates> mSiteLocationCoordinates;
//mutex protected container for the current telescope state.
//SerialDeviceControl::CriticalData<TelescopeMountState> mTelescopeState;
//mutex protected state variable of the motion thread.
SerialDeviceControl::CriticalData<bool> mIsMotionControlThreadRunning;
//mutex protected state variable of the motion thread to indicate if a motion is started.
SerialDeviceControl::CriticalData<bool> mIsMotionControlRunning;
//holds the state of motion, direction and rate.
SerialDeviceControl::CriticalData<MotionState> mMotionState;
//motion control thread structure, to periodically sent direction commands.
std::thread mMotionCommandThread;
//mutex used for signaling the thread to start motion command sending,
//this is also used to halt the thread when the motion should be stopped.
std::mutex mMotionCommandControlMutex;
//Condition variable to signal start and stop of motion command sending.
std::condition_variable mMotionControlCondition;
//state machine of the the telescope hardware
MountStateMachine mMountStateMachine;
//Thread function for the motion thread.
void MotionControlThreadFunction()
{
bool isThreadRunning = mIsMotionControlThreadRunning.Get();
bool isMotionRunning;// = mIsMotionControlRunning.Get();
if(!isThreadRunning)
{
mIsMotionControlThreadRunning.Set(true);
std::cerr << "Motion Control Thread started!" << std::endl;
do
{
do
{
std::unique_lock<std::mutex> motionLock(mMotionCommandControlMutex);
isMotionRunning = mIsMotionControlRunning.Get();
if(!isMotionRunning)
{
//initially no motion commands are send, so wait until a motion in either direction is started by the start call.
mMotionControlCondition.wait(motionLock);
isMotionRunning = mIsMotionControlRunning.Get();
if(!isMotionRunning)
{
break;
}
}
MotionState motionState = mMotionState.Get();
//check if motion state is valid.
if
(
motionState.MotionDirection <= SerialDeviceControl::SerialCommandID::NULL_COMMAND_ID ||
motionState.MotionDirection >= SerialDeviceControl::SerialCommandID::STOP_MOTION_COMMAND_ID ||
motionState.CommandsPerSecond == 0
)
{
//motion is tripped but no values are provided -> disable motion and wait again.
mIsMotionControlRunning.Set(false);
motionState.MotionDirection = SerialDeviceControl::SerialCommandID::NULL_COMMAND_ID;
motionState.CommandsPerSecond = 0;
mMotionState.Set(motionState);
break;
}
int waitTime = 1000 / motionState.CommandsPerSecond;
//send command to move to direction.
switch(motionState.MotionDirection)
{
case SerialDeviceControl::SerialCommandID::MOVE_EAST_COMMAND_ID:
MoveEast();
break;
case SerialDeviceControl::SerialCommandID::MOVE_WEST_COMMAND_ID:
MoveWest();
break;
case SerialDeviceControl::SerialCommandID::MOVE_NORTH_COMMAND_ID:
MoveNorth();
break;
case SerialDeviceControl::SerialCommandID::MOVE_SOUTH_COMMAND_ID:
MoveSouth();
break;
default:
break;
}
//wait before next loop.
std::this_thread::sleep_until(std::chrono::system_clock::now() + std::chrono::milliseconds(waitTime));
isMotionRunning = mIsMotionControlRunning.Get();
}
while(isMotionRunning);
isThreadRunning = mIsMotionControlThreadRunning.Get();
}
while(isThreadRunning);
std::cerr << "Motion Control Thread stopped!" << std::endl;
}
}
public:
static std::string SignalToString(TelescopeSignals signal)
{
switch(signal)
{
case TelescopeSignals::Connect:
return EXPR_TO_STRING(TelescopeSignals::Connect);
case TelescopeSignals::Disconnect:
return EXPR_TO_STRING(TelescopeSignals::Disconnect);
case TelescopeSignals::GoTo:
return EXPR_TO_STRING(TelescopeSignals::GoTo);
case TelescopeSignals::InitialPointingCoordinatesReceived:
return EXPR_TO_STRING(TelescopeSignals::InitialPointingCoordinatesReceived);
case TelescopeSignals::ParkingPositionReached:
return EXPR_TO_STRING(TelescopeSignals::ParkingPositionReached);
case TelescopeSignals::RequestedGeoLocationReceived:
return EXPR_TO_STRING(TelescopeSignals::RequestedGeoLocationReceived);
case TelescopeSignals::Slew:
return EXPR_TO_STRING(TelescopeSignals::Slew);
case TelescopeSignals::Track:
return EXPR_TO_STRING(TelescopeSignals::Track);
case TelescopeSignals::TrackingTargetReached:
return EXPR_TO_STRING(TelescopeSignals::TrackingTargetReached);
case TelescopeSignals::StartMotion:
return EXPR_TO_STRING(TelescopeSignals::StartMotion);
case TelescopeSignals::StopMotion:
return EXPR_TO_STRING(TelescopeSignals::StopMotion);
case TelescopeSignals::Park:
return EXPR_TO_STRING(TelescopeSignals::Park);
case TelescopeSignals::Stop:
return EXPR_TO_STRING(TelescopeSignals::Stop);
default:
return "Invalid Signal!";
}
}
static std::string StateToString(TelescopeMountState state)
{
switch(state)
{
case TelescopeMountState::Disconnected:
return EXPR_TO_STRING(TelescopeMountState::Disconnected);
case TelescopeMountState::Connected:
return EXPR_TO_STRING(TelescopeMountState::Connected);
case TelescopeMountState::Parked:
return EXPR_TO_STRING(TelescopeMountState::Parked);
case TelescopeMountState::Idle:
return EXPR_TO_STRING(TelescopeMountState::Idle);
case TelescopeMountState::Unknown:
return EXPR_TO_STRING(TelescopeMountState::Unknown);
case TelescopeMountState::ParkingIssued:
return EXPR_TO_STRING(TelescopeMountState::ParkingIssued);
case TelescopeMountState::Tracking:
return EXPR_TO_STRING(TelescopeMountState::Tracking);
case TelescopeMountState::Slewing:
return EXPR_TO_STRING(TelescopeMountState::Slewing);
case TelescopeMountState::MoveWhileTracking:
return EXPR_TO_STRING(TelescopeMountState::MoveWhileTracking);
case TelescopeMountState::FailSafe:
return EXPR_TO_STRING(TelescopeMountState::FailSafe);
default:
return "Invalid State";
}
}
};
}
|