Debian Codegraph @

SerialCommand.hpp

raw
1 /*
2 * SerialCommand.hpp
3 *
4 * Copyright 2020 Kevin Krüger <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 *
21 *
22 */
23
24 #ifndef _SERIALCOMMAND_H_INCLUDED_
25 #define _SERIALCOMMAND_H_INCLUDED_
26
27 #include <cstdint>
28 #include <vector>
29 #include <chrono>
30 #include <cmath>
31 #include <ctime>
32 #include <libnova/julian_day.h>
33 #include "config.h"
34
35 #define MESSAGE_FRAME_SIZE (13)
36
37 namespace SerialDeviceControl
38 {
39 //After determining the message frame size and structure,
40 //these commands where found to affect the telescope controller (Handbox + Motors).
41 //If an invalid command is issued the telescope controller stops reporting its status,
42 //until another valid command is issued.
43 //Command IDs which did not stop the report messages are considered effective and are listed below:
44 enum SerialCommandID
45 {
46 //a null command message.
47 NULL_COMMAND_ID = (uint8_t) 0x00,
48
49 //these command IDs move the telescope in certain direction while tracking:
50 //move the telescope "east".
51 MOVE_EAST_COMMAND_ID = (uint8_t)0x01,
52
53 //move the telescope "west".
54 MOVE_WEST_COMMAND_ID = (uint8_t)0x02,
55
56 //move the telescope "north".
57 MOVE_NORTH_COMMAND_ID = (uint8_t)0x04,
58
59 //move the telescope "south".
60 MOVE_SOUTH_COMMAND_ID = (uint8_t)0x08,
61
62 //immediatly stops slewing the telescope.
63 STOP_MOTION_COMMAND_ID = (uint8_t)0x1D,
64
65 //slews the telescope back into the park/initial position.
66 PARK_COMMAND_ID = (uint8_t)0x1E,
67
68 //Requests the site location geodesic coordinates from the controller.
69 GET_SITE_LOCATION_COMMAND_ID = (uint8_t)0x1f,
70
71 //Tell the mount to gracefully disconnect the driver from the serial protocol. Stops the mount from sending status reports.
72 DISCONNET_COMMAND_ID = (uint8_t) 0x22,
73
74 //Slews the telescope to the equatorial coordinates provided.
75 GOTO_COMMAND_ID = (uint8_t)0x23,
76
77 //Tell the controller to match/align to the delivered coordinates. This updates the scope alignment.
78 SYNC_COMMAND_ID = (uint8_t)0x24,
79
80 //sets the site location on the telescope controller.
81 SET_SITE_LOCATION_COMMAND_ID = (uint8_t)0x25,
82
83 //sets time and date on the telescope controller.
84 SET_DATE_TIME_COMMAND_ID = (uint8_t)0x26,
85
86 //If the GET_SITE_LOCATION message was sent, the controller responds with this message, along side the Geo-Coordinates.
87 TELESCOPE_SITE_LOCATION_REPORT_COMMAND_ID = (uint8_t)0xfe,
88
89 //This id is used when the telescope is in an untracked state. Found in the C# implementation but apparently not used.
90 TELESCOPE_POSITION_REPORT_UNTRACKED_COMMAND_ID = (uint8_t)0xf0,
91
92 //This id is used by the telescope controller to announce its pointing coordinates.
93 TELESCOPE_POSITION_REPORT_COMMAND_ID = (uint8_t)0xff
94 };
95
96 //Simple data structure for a coordinate pair.
97 struct EquatorialCoordinates
98 {
99 //The time stamp when this coordinates where received.
100 std::chrono::time_point<std::chrono::system_clock> TimeStamp;
101
102 //decimal value of the right ascension.
103 float RightAscension;
104
105 //decimal value of the declination.
106 float Declination;
107
108 static EquatorialCoordinates Delta(EquatorialCoordinates &first, EquatorialCoordinates &second)
109 {
110 EquatorialCoordinates result;
111
112 result.RightAscension = first.RightAscension - second.RightAscension;
113 result.Declination = first.RightAscension - second.RightAscension;
114
115 return result;
116 }
117
118 static float Absolute(EquatorialCoordinates &deltaCoordinates)
119 {
120 float a = deltaCoordinates.RightAscension;
121 float b = deltaCoordinates.Declination;
122
123 float abs = std::fabs(a * a + b * b);
124
125 return abs;
126 }
127 };
128
129 //Enum with month names for easy legibility.
130 enum DateMonths
131 {
132 January = 1,
133 February = 2,
134 March = 3,
135 April = 4,
136 May = 5,
137 June = 6,
138 July = 7,
139 August = 8,
140 September = 9,
141 October = 10,
142 November = 11,
143 December = 12,
144 };
145
146 //helper union to read out float bytes without the hazzle with pointers
147 union FloatByteConverter
148 {
149 uint8_t bytes[4];
150 float decimal_number;
151 };
152
153 //Simple static class providing the message generation mechanisms.
154 //The message frame size is 13 bytes, a 4 byte header/preamble,
155 //a one byte command followed by 2 to 6 arguments,
156 //distributed over the 8 remaining bytes.
157 //see the Get...Message() implementations for details.
158 //Since the serial protocol is faily simple, a lot of the error handling is on the client side to avoid the controller to go haywire.
159 class SerialCommand
160 {
161 private:
162
163
164 //helper function pushing a number of bytes into the buffer, for padding.
165 static void push_bytes(std::vector<uint8_t> &buffer, uint8_t byte, size_t count);
166
167 //simple constant containing the message header as of firmware V2.3.
168 static uint8_t MessageHeader[4];
169
170 //helper function to push the float values into the buffer
171 static void push_float_bytes(std::vector<uint8_t> &buffer, FloatByteConverter &values);
172
173 public:
174 //Gracefully disconnect from the GoTo Controller.
175 //returns false if an error occurs.
176 static bool GetDisconnectCommandMessage(std::vector<uint8_t> &buffer);
177
178 //put the stop message into the buffer provided.
179 //returns false if an error occurs.
180 static bool GetStopMotionCommandMessage(std::vector<uint8_t> &buffer);
181
182 //put the park message into the buffer provided.
183 //returns false if an error occurs.
184 static bool GetParkCommandMessage(std::vector<uint8_t> &buffer);
185
186 //put the get site location message into the buffer provided.
187 //returns false if an error occurs.
188 static bool GetGetSiteLocationCommandMessage(std::vector<uint8_t> &buffer);
189
190 //put the goto message corresponding to the coordinates provided into the buffer provided.
191 //returns false if an error occurs.
192 static bool GetGotoCommandMessage(std::vector<uint8_t> &buffer, float decimal_right_ascension, float decimal_declination);
193
194 //put the sync message corresponding to the coordinates provided into the buffer provided.
195 //This function is used when plate solving.
196 //returns false if an error occurs.
197 static bool GetSyncCommandMessage(std::vector<uint8_t> &buffer, float decimal_right_ascension, float decimal_declination);
198
199 //put the set site location message corresponding the coordinates provided into the buffer provided
200 //returns false if an error occurs.
201 static bool GetSetSiteLocationCommandMessage(std::vector<uint8_t> &buffer, float decimal_latitude, float decimal_longitude);
202
203 //put the date time message corresponding to the time/date provided into the buffer provided.
204 //returns false if an error occurs.
205 static bool GetSetDateTimeCommandMessage(std::vector<uint8_t> &buffer, uint16_t year, uint8_t month, uint8_t day,
206 uint8_t hour, uint8_t minute, uint8_t second, int8_t utc_offset);
207
208 //move the telescope in a certain direction. Use the first 4 command IDs for a particular direction.
209 static bool GetMoveWhileTrackingCommandMessage(std::vector<uint8_t> &buffer, SerialCommandID direction);
210
211 //helper function pushing the header into the buffer.
212 static void PushHeader(std::vector<uint8_t> &buffer);
213
214
215 };
216 }
217
218 #endif