| 1 |
/* |
| 2 |
* SerialCommand.cpp |
| 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 |
#include "SerialCommand.hpp" |
| 25 |
|
| 26 |
using SerialDeviceControl::SerialCommand; |
| 27 |
|
| 28 |
#include <iostream> |
| 29 |
#include <sstream> |
| 30 |
#include <iomanip> |
| 31 |
|
| 32 |
//TODO: change logging, |
| 33 |
#ifdef USE_CERR_LOGGING |
| 34 |
#include <iostream> |
| 35 |
#endif |
| 36 |
|
| 37 |
#define ERROR_NULL_BUFFER ("buffer is null pointer.") |
| 38 |
#define ERROR_INVALID_RA_RANGE ("invalid range for right ascension.") |
| 39 |
#define ERROR_INVALID_DEC_RANGE ("invalid range for declination.") |
| 40 |
#define ERROR_INVALID_LAT_RANGE ("invalid range for latitude.") |
| 41 |
#define ERROR_INVALID_LON_RANGE ("invalid range for longitude.") |
| 42 |
#define ERROR_INVALID_YEAR_RANGE ("invalid range for year.") |
| 43 |
#define ERROR_INVALID_MONTH_RANGE ("invalid range for month.") |
| 44 |
#define ERROR_INVALID_DAY_RANGE ("invalid range for day.") |
| 45 |
#define ERROR_INVALID_HOUR_RANGE ("invalid range for hour.") |
| 46 |
#define ERROR_INVALID_MINUTE_RANGE ("invalid range for minute.") |
| 47 |
#define ERROR_INVALID_SECOND_RANGE ("invalid range for second.") |
| 48 |
#define ERROR_INVALID_RANGE_FEBURARY ("the february can only have 29 days at maximum!") |
| 49 |
#define ERROR_INVALID_RANGE_THIRTYONE ("the provided month can only have 31 days!") |
| 50 |
#define ERROR_INVALID_RANGE_THIRTY ("the provided month can only have 30 days!") |
| 51 |
#define ERROR_INVALID_RANGE_NO_LEAP_YEAR ("the provided date is invalid, februry can only have 29 days in leap years!") |
| 52 |
#define ERROR_INVALID_DIRECTION ("the direction provided is invalid!") |
| 53 |
|
| 54 |
|
| 55 |
uint8_t SerialCommand::MessageHeader[4] = {0x55, 0xaa, 0x01, 0x09}; |
| 56 |
|
| 57 |
void SerialCommand::PushHeader( |
| 58 |
std::vector<uint8_t> &buffer |
| 59 |
) |
| 60 |
{ |
| 61 |
buffer.push_back(SerialCommand::MessageHeader[0]); |
| 62 |
buffer.push_back(SerialCommand::MessageHeader[1]); |
| 63 |
buffer.push_back(SerialCommand::MessageHeader[2]); |
| 64 |
buffer.push_back(SerialCommand::MessageHeader[3]); |
| 65 |
} |
| 66 |
|
| 67 |
void SerialCommand::push_bytes( |
| 68 |
std::vector<uint8_t> &buffer, |
| 69 |
uint8_t byte, |
| 70 |
size_t count |
| 71 |
) |
| 72 |
{ |
| 73 |
if(count < 1) |
| 74 |
{ |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
for(size_t i = 0; i < count; i++) |
| 79 |
{ |
| 80 |
buffer.push_back(byte); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
void SerialCommand::push_float_bytes( |
| 85 |
std::vector<uint8_t> &buffer, |
| 86 |
FloatByteConverter &values |
| 87 |
) |
| 88 |
{ |
| 89 |
buffer.push_back(values.bytes[0]); |
| 90 |
buffer.push_back(values.bytes[1]); |
| 91 |
buffer.push_back(values.bytes[2]); |
| 92 |
buffer.push_back(values.bytes[3]); |
| 93 |
} |
| 94 |
|
| 95 |
//The following two commands are the simplest. They only consist of the header and the command id, padding the remaining bytes with zeros. |
| 96 |
//Gracefully disconnect from the GoTo Controller. |
| 97 |
bool SerialCommand::GetDisconnectCommandMessage( |
| 98 |
std::vector<uint8_t> &buffer |
| 99 |
) |
| 100 |
{ |
| 101 |
PushHeader(buffer); |
| 102 |
|
| 103 |
buffer.push_back(SerialCommandID::DISCONNET_COMMAND_ID); |
| 104 |
|
| 105 |
push_bytes(buffer, 0x00, 8); |
| 106 |
|
| 107 |
return true; |
| 108 |
} |
| 109 |
|
| 110 |
//The following two commands are the simplest. They only consist of the header and the command id, padding the remaining bytes with zeros. |
| 111 |
//This command stops the telescope if, it is moving, or tracking. |
| 112 |
bool SerialCommand::GetStopMotionCommandMessage( |
| 113 |
std::vector<uint8_t> &buffer |
| 114 |
) |
| 115 |
{ |
| 116 |
PushHeader(buffer); |
| 117 |
|
| 118 |
buffer.push_back(SerialCommandID::STOP_MOTION_COMMAND_ID); |
| 119 |
|
| 120 |
push_bytes(buffer, 0x00, 8); |
| 121 |
|
| 122 |
return true; |
| 123 |
} |
| 124 |
|
| 125 |
//This slews the telescope back to the initial position or home position. |
| 126 |
bool SerialCommand::GetParkCommandMessage( |
| 127 |
std::vector<uint8_t> &buffer |
| 128 |
) |
| 129 |
{ |
| 130 |
PushHeader(buffer); |
| 131 |
|
| 132 |
buffer.push_back(SerialCommandID::PARK_COMMAND_ID); |
| 133 |
|
| 134 |
push_bytes(buffer, 0x00, 8); |
| 135 |
|
| 136 |
return true; |
| 137 |
} |
| 138 |
|
| 139 |
bool SerialCommand::GetGetSiteLocationCommandMessage( |
| 140 |
std::vector<uint8_t> &buffer |
| 141 |
) |
| 142 |
{ |
| 143 |
PushHeader(buffer); |
| 144 |
|
| 145 |
buffer.push_back(SerialCommandID::GET_SITE_LOCATION_COMMAND_ID); |
| 146 |
|
| 147 |
push_bytes(buffer, 0x00, 8); |
| 148 |
|
| 149 |
return true; |
| 150 |
} |
| 151 |
|
| 152 |
//This command slews the telescope to the coordinates provided. It is autonomous, and the change of slewing speeds are not allowed. |
| 153 |
bool SerialCommand::GetGotoCommandMessage( |
| 154 |
std::vector<uint8_t> &buffer, |
| 155 |
float decimal_right_ascension, |
| 156 |
float decimal_declination |
| 157 |
) |
| 158 |
{ |
| 159 |
if(decimal_right_ascension < 0 || decimal_right_ascension > 24) |
| 160 |
{ |
| 161 |
#ifdef USE_CERR_LOGGING |
| 162 |
std::cerr << ERROR_INVALID_RA_RANGE << std::endl; |
| 163 |
#endif |
| 164 |
return false; |
| 165 |
} |
| 166 |
|
| 167 |
if(decimal_declination < -90 || decimal_declination > 90) |
| 168 |
{ |
| 169 |
#ifdef USE_CERR_LOGGING |
| 170 |
std::cerr << ERROR_INVALID_DEC_RANGE << std::endl; |
| 171 |
#endif |
| 172 |
return false; |
| 173 |
} |
| 174 |
|
| 175 |
PushHeader(buffer); |
| 176 |
|
| 177 |
buffer.push_back(SerialCommandID::GOTO_COMMAND_ID); |
| 178 |
|
| 179 |
FloatByteConverter ra_bytes; |
| 180 |
FloatByteConverter dec_bytes; |
| 181 |
|
| 182 |
ra_bytes.decimal_number = decimal_right_ascension; |
| 183 |
dec_bytes.decimal_number = decimal_declination; |
| 184 |
|
| 185 |
push_float_bytes(buffer, ra_bytes); |
| 186 |
push_float_bytes(buffer, dec_bytes); |
| 187 |
|
| 188 |
return true; |
| 189 |
} |
| 190 |
|
| 191 |
//This command syncs the telescope to the coordinates provided. It should be useful when doing plate solvings. |
| 192 |
bool SerialCommand::GetSyncCommandMessage( |
| 193 |
std::vector<uint8_t> &buffer, |
| 194 |
float decimal_right_ascension, |
| 195 |
float decimal_declination |
| 196 |
) |
| 197 |
{ |
| 198 |
if(decimal_right_ascension < 0 || decimal_right_ascension > 24) |
| 199 |
{ |
| 200 |
#ifdef USE_CERR_LOGGING |
| 201 |
std::cerr << ERROR_INVALID_RA_RANGE << std::endl; |
| 202 |
#endif |
| 203 |
return false; |
| 204 |
} |
| 205 |
|
| 206 |
if(decimal_declination < -90 || decimal_declination > 90) |
| 207 |
{ |
| 208 |
#ifdef USE_CERR_LOGGING |
| 209 |
std::cerr << ERROR_INVALID_DEC_RANGE << std::endl; |
| 210 |
#endif |
| 211 |
return false; |
| 212 |
} |
| 213 |
|
| 214 |
PushHeader(buffer); |
| 215 |
|
| 216 |
buffer.push_back(SerialCommandID::SYNC_COMMAND_ID); |
| 217 |
|
| 218 |
FloatByteConverter ra_bytes; |
| 219 |
FloatByteConverter dec_bytes; |
| 220 |
|
| 221 |
ra_bytes.decimal_number = decimal_right_ascension; |
| 222 |
dec_bytes.decimal_number = decimal_declination; |
| 223 |
|
| 224 |
push_float_bytes(buffer, ra_bytes); |
| 225 |
push_float_bytes(buffer, dec_bytes); |
| 226 |
|
| 227 |
return true; |
| 228 |
} |
| 229 |
|
| 230 |
//This sets the site location of the mount, it just supports longitude and latitude, but no elevation. |
| 231 |
bool SerialCommand::GetSetSiteLocationCommandMessage( |
| 232 |
std::vector<uint8_t> &buffer, |
| 233 |
float decimal_latitude, |
| 234 |
float decimal_longitude |
| 235 |
) |
| 236 |
{ |
| 237 |
if(decimal_latitude < -90 || decimal_latitude > 90) |
| 238 |
{ |
| 239 |
#ifdef USE_CERR_LOGGING |
| 240 |
std::cerr << ERROR_INVALID_LAT_RANGE << std::endl; |
| 241 |
#endif |
| 242 |
return false; |
| 243 |
} |
| 244 |
|
| 245 |
if(decimal_longitude < -180 || decimal_longitude > 180) |
| 246 |
{ |
| 247 |
#ifdef USE_CERR_LOGGING |
| 248 |
std::cerr << ERROR_INVALID_LON_RANGE << std::endl; |
| 249 |
#endif |
| 250 |
return false; |
| 251 |
} |
| 252 |
|
| 253 |
PushHeader(buffer); |
| 254 |
|
| 255 |
buffer.push_back(SerialCommandID::SET_SITE_LOCATION_COMMAND_ID); |
| 256 |
|
| 257 |
FloatByteConverter lat_bytes; |
| 258 |
FloatByteConverter lon_bytes; |
| 259 |
|
| 260 |
lat_bytes.decimal_number = decimal_latitude; |
| 261 |
lon_bytes.decimal_number = decimal_longitude; |
| 262 |
|
| 263 |
push_float_bytes(buffer, lon_bytes); |
| 264 |
push_float_bytes(buffer, lat_bytes); |
| 265 |
|
| 266 |
return true; |
| 267 |
} |
| 268 |
|
| 269 |
//This message sets the dates and time of the telescope mount, the values are simple binary coded decimals (BCD). |
| 270 |
//Also the controller accepts any value, even if it is incorrect eg. 99:99:99 as a time and 9999-99-99 are possible, so checking for validity is encouraged. |
| 271 |
//further a check for leap years is advisable. |
| 272 |
bool SerialCommand::GetSetDateTimeCommandMessage( |
| 273 |
std::vector<uint8_t> &buffer, |
| 274 |
uint16_t year, |
| 275 |
uint8_t month, |
| 276 |
uint8_t day, |
| 277 |
uint8_t hour, |
| 278 |
uint8_t minute, |
| 279 |
uint8_t second, |
| 280 |
int8_t utc_offset |
| 281 |
) |
| 282 |
{ |
| 283 |
if(year > 9999) |
| 284 |
{ |
| 285 |
#ifdef USE_CERR_LOGGING |
| 286 |
std::cerr << ERROR_INVALID_YEAR_RANGE << std::endl; |
| 287 |
#endif |
| 288 |
return false; |
| 289 |
} |
| 290 |
|
| 291 |
if(month < 1 || month > 12) |
| 292 |
{ |
| 293 |
#ifdef USE_CERR_LOGGING |
| 294 |
std::cerr << ERROR_INVALID_MONTH_RANGE << " " << month << std::endl; |
| 295 |
#endif |
| 296 |
return false; |
| 297 |
} |
| 298 |
|
| 299 |
if(day < 1 || day > 31) |
| 300 |
{ |
| 301 |
#ifdef USE_CERR_LOGGING |
| 302 |
std::cerr << ERROR_INVALID_DAY_RANGE << std::endl; |
| 303 |
#endif |
| 304 |
return false; |
| 305 |
} |
| 306 |
|
| 307 |
if(hour > 24) |
| 308 |
{ |
| 309 |
#ifdef USE_CERR_LOGGING |
| 310 |
std::cerr << ERROR_INVALID_HOUR_RANGE << std::endl; |
| 311 |
#endif |
| 312 |
return false; |
| 313 |
} |
| 314 |
|
| 315 |
if(minute > 59) |
| 316 |
{ |
| 317 |
#ifdef USE_CERR_LOGGING |
| 318 |
std::cerr << ERROR_INVALID_MINUTE_RANGE << std::endl; |
| 319 |
#endif |
| 320 |
return false; |
| 321 |
} |
| 322 |
|
| 323 |
if(second > 59) |
| 324 |
{ |
| 325 |
#ifdef USE_CERR_LOGGING |
| 326 |
std::cerr << ERROR_INVALID_SECOND_RANGE << std::endl; |
| 327 |
#endif |
| 328 |
return false; |
| 329 |
} |
| 330 |
|
| 331 |
//check if the february is in its bounds |
| 332 |
if(month == DateMonths::February && day > 29) |
| 333 |
{ |
| 334 |
#ifdef USE_CERR_LOGGING |
| 335 |
std::cerr << ERROR_INVALID_RANGE_FEBURARY << std::endl; |
| 336 |
#endif |
| 337 |
return false; |
| 338 |
} |
| 339 |
|
| 340 |
switch(month) |
| 341 |
{ |
| 342 |
case DateMonths::January: |
| 343 |
case DateMonths::March: |
| 344 |
case DateMonths::May: |
| 345 |
case DateMonths::July: |
| 346 |
case DateMonths::August: |
| 347 |
case DateMonths::October: |
| 348 |
case DateMonths::December: |
| 349 |
{ |
| 350 |
if(day > 31) |
| 351 |
{ |
| 352 |
#ifdef USE_CERR_LOGGING |
| 353 |
std::cerr << ERROR_INVALID_RANGE_THIRTYONE << std::endl; |
| 354 |
#endif |
| 355 |
return false; |
| 356 |
} |
| 357 |
} |
| 358 |
break; |
| 359 |
|
| 360 |
case DateMonths::April: |
| 361 |
case DateMonths::June: |
| 362 |
case DateMonths::September: |
| 363 |
case DateMonths::November: |
| 364 |
if(day > 30) |
| 365 |
{ |
| 366 |
#ifdef USE_CERR_LOGGING |
| 367 |
std::cerr << ERROR_INVALID_RANGE_THIRTY << std::endl; |
| 368 |
#endif |
| 369 |
return false; |
| 370 |
} |
| 371 |
break; |
| 372 |
} |
| 373 |
|
| 374 |
if(year > 0 && (year % 4) != 0) |
| 375 |
{ |
| 376 |
//common year. |
| 377 |
if(day > 28) |
| 378 |
{ |
| 379 |
#ifdef USE_CERR_LOGGING |
| 380 |
std::cerr << ERROR_INVALID_RANGE_NO_LEAP_YEAR << std::endl; |
| 381 |
#endif |
| 382 |
return false; |
| 383 |
} |
| 384 |
} |
| 385 |
else if(year > 0 && (year % 100) == 0) |
| 386 |
{ |
| 387 |
//leap year. |
| 388 |
} |
| 389 |
else if(year > 0 && (year % 400) != 0) |
| 390 |
{ |
| 391 |
//common year. |
| 392 |
if(day > 28) |
| 393 |
{ |
| 394 |
#ifdef USE_CERR_LOGGING |
| 395 |
std::cerr << ERROR_INVALID_RANGE_NO_LEAP_YEAR << std::endl; |
| 396 |
#endif |
| 397 |
return false; |
| 398 |
} |
| 399 |
} |
| 400 |
else |
| 401 |
{ |
| 402 |
//leap year. |
| 403 |
} |
| 404 |
|
| 405 |
PushHeader(buffer); |
| 406 |
|
| 407 |
buffer.push_back(SerialCommandID::SET_DATE_TIME_COMMAND_ID); |
| 408 |
|
| 409 |
//when received the incomming byte is offset by -12 for some reason, so adjust for this. probably offset sign handling. |
| 410 |
|
| 411 |
|
| 412 |
uint8_t local_hiYear = (uint8_t)(year / 100); |
| 413 |
uint8_t local_loYear = (uint8_t)(year % 100); |
| 414 |
uint8_t local_month = (uint8_t)month; |
| 415 |
uint8_t local_day = (uint8_t)day; |
| 416 |
|
| 417 |
//TODO: Be aware, the firmware only supports dates prior to 10000-01-01, please fix this at the appropriate time! |
| 418 |
buffer.push_back(local_hiYear); |
| 419 |
buffer.push_back(local_loYear); |
| 420 |
buffer.push_back(local_month); |
| 421 |
buffer.push_back(local_day); |
| 422 |
|
| 423 |
uint8_t local_hour = (uint8_t)hour; |
| 424 |
uint8_t local_minute = (uint8_t)minute; |
| 425 |
uint8_t local_second = (uint8_t)second; |
| 426 |
uint8_t utc_shift = (uint8_t)(utc_offset + 12); |
| 427 |
|
| 428 |
buffer.push_back(local_hour); //handbox uses local time as your clock shows, and calculates back to the UTC from that. |
| 429 |
buffer.push_back(local_minute); |
| 430 |
buffer.push_back(local_second); |
| 431 |
buffer.push_back(utc_shift); //TODO: offset range limiting... |
| 432 |
|
| 433 |
return true; |
| 434 |
} |
| 435 |
|
| 436 |
//move the telescope in a certain direction. Use the first 4 command IDs for a particular direction. |
| 437 |
bool SerialCommand::GetMoveWhileTrackingCommandMessage( |
| 438 |
std::vector<uint8_t> &buffer, |
| 439 |
SerialCommandID direction |
| 440 |
) |
| 441 |
{ |
| 442 |
if(direction < SerialCommandID::MOVE_EAST_COMMAND_ID || direction > SerialCommandID::MOVE_SOUTH_COMMAND_ID) |
| 443 |
{ |
| 444 |
#ifdef USE_CERR_LOGGING |
| 445 |
std::cerr << ERROR_INVALID_DIRECTION << std::endl; |
| 446 |
#endif |
| 447 |
return false; |
| 448 |
} |
| 449 |
|
| 450 |
PushHeader(buffer); |
| 451 |
buffer.push_back(direction); |
| 452 |
|
| 453 |
buffer.push_back(0xC8); |
| 454 |
push_bytes(buffer, 0x00, 3); |
| 455 |
|
| 456 |
buffer.push_back(0xC8); |
| 457 |
push_bytes(buffer, 0x00, 3); |
| 458 |
|
| 459 |
return true; |
| 460 |
} |