50 lastStatusData.reserve(7);
68 ReloadFirmwareSP[0].fill(
"RELOAD",
"Reload",
ISS_OFF);
74 DerotateNP[0].fill(
"RATE",
"Rate (arcsec/s)",
"%.f", -3600, 3600, 1, 0);
79 RotatorSpeedNP[0].fill(
"SPEED",
"Speed (steps/sec)",
"%.f", 10, 4500, 10, 500);
91 FirmwareTP[0].fill(
"VERSION",
"Version",
"NA");
98 wifi.initProperties(
"WiFi",
99 [
this](
const char *name) {
loadConfig(
true, name); },
108 wifi.ISGetProperties();
113 if (wifi.ISNewText(dev, name, texts, names,
n))
150 return "Pegasus FalconV2";
166 if (!getRotatorSpeed())
167 LOG_WARN(
"Failed to read rotator speed during connection.");
177 if (wifi.ISNewNumber(dev, name, values, names,
n))
183 if (DerotateNP.isNameMatch(name))
187 if (
isConfigLoading() ||
n < 1 || strcmp(names[0], DerotateNP[0].getName()) != 0)
193 const int32_t rate =
static_cast<int32_t
>(std::max(DerotateNP[0].getMin(),
194 std::min(DerotateNP[0].getMax(), values[0])));
195 if (setDerotation(rate))
197 DerotateNP[0].setValue(rate);
199 LOGF_INFO(
"De-rotation is enabled at %d arcsec/s.", rate);
201 LOG_INFO(
"De-rotation is disabled.");
202 DerotateNP.setState(
IPS_OK);
211 if (RotatorSpeedNP.isNameMatch(name))
213 const uint32_t speed =
static_cast<uint32_t
>(values[0]);
214 if (setRotatorSpeed(speed))
216 RotatorSpeedNP[0].setValue(values[0]);
217 LOGF_INFO(
"Rotator speed set to %u steps/sec.", speed);
218 RotatorSpeedNP.setState(
IPS_OK);
222 RotatorSpeedNP.apply();
226 return Rotator::ISNewNumber(dev, name, values, names,
n);
234 if (wifi.ISNewSwitch(dev, name, states, names,
n))
240 if (ReloadFirmwareSP.isNameMatch(name))
243 ReloadFirmwareSP.apply();
249 if (CableLimitSP.isNameMatch(name))
252 const bool enabled = onSwitch !=
nullptr && CableLimitSP[
INDI_ENABLED].isNameMatch(onSwitch);
253 if (setCableLimit(enabled))
255 CableLimitSP.update(states, names,
n);
256 CableLimitSP.setState(
IPS_OK);
260 LOG_WARN(
"Cable limit disabled. The rotator can now turn without limit, watch the cables.");
264 CableLimitSP.apply();
269 return Rotator::ISNewSwitch(dev, name, states, names,
n);
277 char cmd[DRIVER_LEN] = {0}, res[DRIVER_LEN] = {0};
278 snprintf(
cmd, DRIVER_LEN,
"MD:%.2f", angle);
279 if (sendCommand(
cmd, res) && !strncmp(res,
"MD:", 3))
281 m_TargetAngle = angle;
282 m_MoveStarted = std::chrono::steady_clock::now();
284 m_DirectionChecked =
false;
288 LOGF_ERROR(
"Unexpected move response: %s", res);
297 char res[DRIVER_LEN] = {0};
298 if (sendCommand(
"FH", res) && !strcmp(res,
"FH:1"))
301 m_MoveStarted = std::chrono::steady_clock::time_point();
302 m_DirectionChecked =
true;
314 char cmd[DRIVER_LEN] = {0}, res[DRIVER_LEN] = {0};
315 snprintf(
cmd, DRIVER_LEN,
"FN:%d", enabled ? 1 : 0);
316 if (sendCommand(
cmd, res))
318 return (!strncmp(res,
cmd, 4));
329 char cmd[DRIVER_LEN] = {0}, res[DRIVER_LEN] = {0};
330 snprintf(
cmd, DRIVER_LEN,
"SD:%.2f", angle);
331 return sendCommand(
cmd, res) && !strncmp(res,
"SD:", 3);
337bool PegasusFalconV2::getCableLimit()
340 char res[DRIVER_LEN] = {0};
342 if (!sendCommand(
"FLIM", res) || sscanf(res,
"FLIM:%d", &enabled) != 1)
344 LOGF_WARN(
"Could not read the cable limit [Res=%s].", res);
350 CableLimitSP.setState(
IPS_OK);
354bool PegasusFalconV2::setCableLimit(
bool enabled)
356 char cmd[DRIVER_LEN] = {0},
res[DRIVER_LEN] = {0};
357 snprintf(
cmd, DRIVER_LEN,
"FLIM:%d", enabled ? 1 : 0);
358 if (sendCommand(
cmd, res) && !strcmp(res,
cmd))
361 LOGF_ERROR(
"Unexpected cable limit response: %s", res);
365bool PegasusFalconV2::reloadFirmware()
368 return sendCommand(
"FF");
374bool PegasusFalconV2::setDerotation(int32_t rate)
376 char cmd[DRIVER_LEN] = {0},
res[DRIVER_LEN] = {0};
377 snprintf(
cmd, DRIVER_LEN,
"DR:%d", rate);
378 return sendCommand(
cmd, res) && !strncmp(res,
"DR:", 3);
384bool PegasusFalconV2::getRotatorSpeed()
386 char res[DRIVER_LEN] = {0};
387 if (sendCommand(
"FS", res))
390 std::vector<std::string> result = split(res,
":");
391 if (result.size() == 2 && result[0] ==
"FS")
395 const uint32_t speed = std::stoul(result[1]);
396 RotatorSpeedNP[0].setValue(speed);
397 RotatorSpeedNP.setState(
IPS_OK);
398 LOGF_INFO(
"Rotator speed is %u steps/sec.", speed);
404 LOGF_ERROR(
"Failed to parse speed response: %s", res);
410 LOGF_ERROR(
"Invalid speed response format: %s", res);
421bool PegasusFalconV2::setRotatorSpeed(uint32_t speed)
423 char cmd[DRIVER_LEN] = {0},
res[DRIVER_LEN] = {0};
424 snprintf(
cmd, DRIVER_LEN,
"FS:%u", speed);
425 if (sendCommand(
cmd, res))
428 return (!strncmp(res,
"FS:", 3));
440 wifi.saveConfigItems(fp);
458bool PegasusFalconV2::getFirmware()
460 char res[DRIVER_LEN] = {0};
461 if (sendCommand(
"FV", res))
463 FirmwareTP[0].setText(res + 3);
473bool PegasusFalconV2::getStatusData()
475 char res[DRIVER_LEN] = {0};
476 if (sendCommand(
"FA", res))
478 std::vector<std::string> result = split(res,
":");
479 if (result.size() != 6)
481 LOG_WARN(
"Received wrong number of detailed sensor data. Retrying...");
490 bool running =
false, reversed =
false;
493 position = std::stod(result[1]);
494 running = std::stoi(result[2]) == 1;
495 reversed = std::stoi(result[5]) == 1;
497 catch (
const std::exception &e)
499 LOGF_WARN(
"Invalid status data (%s): %s", e.what(), res);
508 double distance = std::fmod(std::fabs(position - m_TargetAngle), 360.0);
509 distance = std::min(distance, 360.0 - distance);
510 if (distance > TARGET_TOLERANCE && std::chrono::steady_clock::now() - m_MoveStarted < MOVE_START_TIMEOUT)
519 auto wrap = [](
double d)
521 return 180.0 - std::fmod(540.0 - d, 360.0);
523 const double shortWay = wrap(m_TargetAngle - m_StartAngle);
524 const double moved = wrap(position - m_StartAngle);
525 if (std::fabs(moved) > 1.0)
527 m_DirectionChecked =
true;
528 if (std::fabs(shortWay) > 1.0 && std::fabs(shortWay) < 179.0 && moved * shortWay < 0)
529 LOGF_WARN(
"Rotator takes the long way round to %.2f (%.0f instead of %.0f degrees), "
530 "because the short way would cross the cable limit.",
531 m_TargetAngle, 360.0 - std::fabs(shortWay), std::fabs(shortWay));
545 if (reversed != wasReversed)
552 lastStatusData = result;
562bool PegasusFalconV2::sendCommand(
const char *
cmd,
char * res,
int cmd_len,
int res_len)
564 int nbytes_written = 0, nbytes_read = 0, rc = -1;
566 tcflush(
PortFD, TCIOFLUSH);
570 char hex_cmd[DRIVER_LEN * 3] = {0};
571 hexDump(hex_cmd,
cmd, cmd_len);
579 char formatted_command[DRIVER_LEN] = {0};
580 snprintf(formatted_command, DRIVER_LEN,
"%s\n",
cmd);
588 LOGF_ERROR(
"Serial write error: %s.", errstr);
596 rc =
tty_read(
PortFD, res, res_len, DRIVER_TIMEOUT, &nbytes_read);
610 char hex_res[DRIVER_LEN * 3] = {0};
611 hexDump(hex_res, res, res_len);
617 res[nbytes_read - 1] = 0;
618 if (nbytes_read > 1 && res[nbytes_read - 2] ==
'\r')
619 res[nbytes_read - 2] = 0;
623 tcflush(
PortFD, TCIOFLUSH);
631void PegasusFalconV2::hexDump(
char * buf,
const char * data, uint32_t size)
633 for (uint32_t i = 0; i < size; i++)
634 sprintf(buf + 3 * i,
"%02X ",
static_cast<uint8_t
>(data[i]));
637 buf[3 * size - 1] =
'\0';
643std::vector<std::string> PegasusFalconV2::split(
const std::string &input,
const std::string ®ex)
646 std::regex re(regex);
647 std::sregex_token_iterator
648 first{input.begin(), input.end(), re, -1},
650 return {first, last};
656void PegasusFalconV2::cleanupResponse(
char *response)
658 std::string s(response);
659 s.erase(std::remove_if(s.begin(), s.end(),
662 return std::isspace(x);
664 snprintf(response, DRIVER_LEN,
"%s", s.c_str());
virtual bool saveConfig(bool silent=false, const char *property=nullptr)
Save the current properties in a configuration file.
bool isConfigLoading() const
isConfigLoading Check if driver configuration is currently in the process of getting loaded.
void setVersion(uint16_t vMajor, uint16_t vMinor)
Set driver version information to be defined in DRIVER_INFO property as vMajor.vMinor.
virtual bool loadConfig(bool silent=false, const char *property=nullptr)
Load the last saved configuration file.
virtual bool deleteProperty(const char *propertyName)
Delete a property and unregister it. It will also be deleted from all clients.
void defineProperty(INumberVectorProperty *property)
uint32_t getCurrentPollingPeriod() const
getCurrentPollingPeriod Return the current polling period.
void addAuxControls()
Add Debug, Simulation, and Configuration options to the driver.
Connection::Interface * getActiveConnection()
int SetTimer(uint32_t ms)
Set a timer to call the function TimerHit after ms milliseconds.
virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n)
Process the client newSwitch command.
Provides generic container for INDI properties.
void SetCapability(uint32_t cap)
SetRotatorCapability sets the Rotator capabilities. All capabilities must be initialized.
INDI::PropertySwitch ReverseRotatorSP
INDI::PropertyNumber GotoRotatorNP
Connection::Serial * serialConnection
virtual bool initProperties() override
Initialize properties initial state and value. The child class must implement this function.
virtual bool Handshake()
perform handshake with device to check communication
virtual void ISGetProperties(const char *dev) override
define the driver's properties to the client. Usually, only a minimum set of properties are defined t...
virtual bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
virtual bool saveConfigItems(FILE *fp) override
saveConfigItems Saves the reverse direction property in the configuration file
Connection::TCP * tcpConnection
static constexpr int TCP_PORT
Port of the TCP command socket.
bool login(int portFD)
Log in on the TCP command socket with the hotspot password.
virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override
Process the client newSwitch command.
virtual bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override
Process the client newSwitch command.
virtual bool saveConfigItems(FILE *fp) override
saveConfigItems Saves the reverse direction property in the configuration file
virtual bool AbortRotator() override
AbortRotator Abort all motion.
virtual void ISGetProperties(const char *dev) override
define the driver's properties to the client. Usually, only a minimum set of properties are defined t...
virtual void TimerHit() override
Callback function to be called once SetTimer duration elapses.
virtual bool initProperties() override
Initialize properties initial state and value. The child class must implement this function.
virtual bool ReverseRotator(bool enabled) override
reverse action ("FN:0" disabled, "FN:1" enabled)
const char * getDefaultName() override
virtual IPState MoveRotator(double angle) override
move to degrees (Command "MD:nn.nn"; Response "MD:nn.nn")
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
virtual bool SyncRotator(double angle) override
SyncRotator Set current angle as the supplied angle without moving the rotator.
const char * MAIN_CONTROL_TAB
MAIN_CONTROL_TAB Where all the primary controls for the device are located.
int tty_write(int fd, const char *buf, int nbytes, int *nbytes_written)
Writes a buffer to fd.
int tty_read(int fd, char *buf, int nbytes, int timeout, int *nbytes_read)
read buffer from terminal
int tty_write_string(int fd, const char *buf, int *nbytes_written)
Writes a null terminated string to fd.
void tty_error_msg(int err_code, char *err_msg, int err_msg_len)
Retrieve the tty error message.
int tty_nread_section(int fd, char *buf, int nsize, char stop_char, int timeout, int *nbytes_read)
read buffer from terminal with a delimiter
Implementations for common driver routines.
const char * IUFindOnSwitchName(ISState *states, char *names[], int n)
Returns the name of the first ON switch it finds in the supplied arguments.
*brief user defined string literal for JSON values *sa std::size_t n
#define LOGF_INFO(fmt,...)
#define LOGF_WARN(fmt,...)
#define LOGF_DEBUG(fmt,...)
#define LOGF_ERROR(fmt,...)