36static std::unique_ptr<AvalonUPAS> upas(
new AvalonUPAS());
58 SpeedNP[0].setLabel(
"Max (mm/min)");
65 FeedRateNP[FEED_AZ].fill(
"FEED_AZ",
"Azimuth (mm/min)",
"%.0f", 10, 1000, 50, 1000);
66 FeedRateNP[FEED_ALT].fill(
"FEED_ALT",
"Altitude (mm/min)",
"%.0f", 10, 1000, 50, 700);
71 MinMoveNP[0].fill(
"MIN_MOVE",
"Minimum (arcmin)",
"%.2f", 0, 10, 0.05, 0.1);
76 GearRatioNP[GEAR_AZ].fill(
"GEAR_AZ",
"Azimuth (mm/arcmin)",
"%.4f", 0.001, 1000, 0.1, 2.0);
77 GearRatioNP[GEAR_ALT].fill(
"GEAR_ALT",
"Altitude (mm/arcmin)",
"%.4f", 0.001, 1000, 0.1, 22.0);
86 AZApproachSP[APPROACH_POSITIVE].fill(
"APPROACH_POSITIVE",
"+",
ISS_ON);
87 AZApproachSP[APPROACH_NEGATIVE].fill(
"APPROACH_NEGATIVE",
"-",
ISS_OFF);
90 ALTApproachSP[APPROACH_POSITIVE].fill(
"APPROACH_POSITIVE",
"+",
ISS_ON);
91 ALTApproachSP[APPROACH_NEGATIVE].fill(
"APPROACH_NEGATIVE",
"-",
ISS_OFF);
96 FirmwareTP[0].fill(
"VERSION",
"Version",
"Unknown");
103 serialConnection->registerHandshake([&]()
153 GearRatioNP.save(fp);
156 AZApproachSP.save(fp);
157 ALTApproachSP.save(fp);
164 PortFD = serialConnection->getPortFD();
167 tcflush(PortFD, TCIOFLUSH);
170 const char softReset =
'\x18';
172 tty_write(PortFD, &softReset, 1, &nbytes);
177 bool grblReady =
false;
178 for (
int i = 0; i < 10 && !grblReady; i++)
181 char line[DRIVER_LEN] = {0};
184 grblReady = strstr(line,
"Grbl") !=
nullptr;
187 LOG_WARN(
"No GRBL startup banner received, trying the status query anyway.");
188 tcflush(PortFD, TCIOFLUSH);
191 char res[DRIVER_LEN] = {0};
192 if (!sendCommand(
"?", res))
195 if (strstr(res,
"MPos") ==
nullptr && strstr(res,
"WPos") ==
nullptr)
197 LOGF_ERROR(
"Unexpected handshake response: %s", res);
205 char ver[DRIVER_LEN] = {0};
206 if (sendCommand(
"$I", ver))
209 const char *tag = strstr(ver,
"[VER:");
213 char version[64] = {0};
214 sscanf(tag,
"[VER:%63[^]:]", version);
215 FirmwareTP[0].setText(version);
219 FirmwareTP[0].setText(ver);
225 LOG_INFO(
"UPAS connected and ready.");
229bool AvalonUPAS::readReverseFlags()
233 tcflush(PortFD, TCIOFLUSH);
238 bool settingsSeen =
false;
239 for (
int i = 0; i < 64; i++)
241 char line[DRIVER_LEN] = {0};
246 if (sscanf(line,
"$3=%d", &mask) == 1)
248 m_ReverseFlags =
static_cast<uint8_t
>(mask & 0x03);
253 LOGF_DEBUG(
"GRBL axis reverse mask $3=%d.", m_ReverseFlags);
254 tcflush(PortFD, TCIOFLUSH);
262 else if ((settingsSeen && strncmp(line,
"ok", 2) == 0) || strncmp(line,
"error", 5) == 0)
266 tcflush(PortFD, TCIOFLUSH);
267 LOG_WARN(
"Could not read the GRBL axis reverse setting ($3), assuming no axis is reversed.");
276 char res[DRIVER_LEN] = {0};
277 if (sendCommand(
"?", res))
283bool AvalonUPAS::parseStatus(
const char *response)
287 const char *mposTag = strstr(response,
"MPos:");
288 if (mposTag ==
nullptr)
291 double azMM = 0.0, altMM = 0.0;
292 if (sscanf(mposTag + 5,
"%lf,%lf", &azMM, &altMM) < 2)
295 const double gearAZ = GearRatioNP[GEAR_AZ].getValue();
296 const double gearALT = GearRatioNP[GEAR_ALT].getValue();
302 char state[16] = {0};
303 const char *start = strchr(response,
'<');
304 if (start !=
nullptr)
305 sscanf(start,
"<%15[^|:>]", state);
308 bool wasMoving = m_IsMoving;
309 m_IsMoving = strcmp(state,
"Jog") == 0 || strcmp(state,
"Run") == 0 || strcmp(state,
"Hold") == 0;
310 const bool fault = strcmp(state,
"Alarm") == 0 || strcmp(state,
"Door") == 0;
315 LOGF_ERROR(
"UPAS reports state %s and does not move. Check the device, then power-cycle it.", state);
345bool AvalonUPAS::belowMinimum(
const char *axis,
double degrees)
347 const double arcmin = std::fabs(degrees) * 60;
348 const double minimum = MinMoveNP[0].getValue();
349 if (arcmin >= minimum)
352 LOGF_INFO(
"%s move of %.3f arcmin is below the minimum of %.2f arcmin, skipped.", axis, arcmin, minimum);
359 if (belowMinimum(
"AZ", degrees))
363 const double mm = degrees * 60 * GearRatioNP[GEAR_AZ].getValue();
364 const double feedRate = feedRateFor(mm, 0);
366 IPState state = jogWithBacklash(mm, 0);
368 LOGF_INFO(
"AZ jog: %.4f deg → %.4f mm @ %.0f mm/min.", degrees, mm, feedRate);
374 if (belowMinimum(
"ALT", degrees))
378 const double mm = degrees * 60 * GearRatioNP[GEAR_ALT].getValue();
379 const double feedRate = feedRateFor(0, mm);
381 IPState state = jogWithBacklash(0, mm);
383 LOGF_INFO(
"ALT jog: %.4f deg → %.4f mm @ %.0f mm/min.", degrees, mm, feedRate);
390 if (belowMinimum(
"AZ", azDegrees))
392 if (belowMinimum(
"ALT", altDegrees))
394 if (azDegrees == 0 && altDegrees == 0)
400 const double mmAZ = azDegrees * 60 * GearRatioNP[GEAR_AZ].getValue();
401 const double mmALT = altDegrees * 60 * GearRatioNP[GEAR_ALT].getValue();
402 const double feedRate = feedRateFor(mmAZ, mmALT);
404 IPState state = jogWithBacklash(mmAZ, mmALT);
406 LOGF_INFO(
"MoveBoth: AZ %.4f° (%.4f mm), ALT %.4f° (%.4f mm) @ %.0f mm/min.",
407 azDegrees, mmAZ, altDegrees, mmALT, feedRate);
411IPState AvalonUPAS::jogWithBacklash(
double azMM,
double altMM)
414 if (!std::isfinite(azMM) || !std::isfinite(altMM))
416 LOG_ERROR(
"Invalid move request, ignored.");
420 const double azApproach = finalApproachMM(
BACKLASH_AZ, azMM);
421 const double altApproach = finalApproachMM(
BACKLASH_ALT, altMM);
423 if (!sendJog(azMM, altMM))
427 if (azApproach != 0 || altApproach != 0)
429 if (!sendJog(azApproach, altApproach))
435 LOGF_INFO(
"Backlash: overshoot and final approach AZ %.4f mm, ALT %.4f mm.", azApproach, altApproach);
442double AvalonUPAS::finalApproachMM(
int axis,
double &mm)
448 const double backlashMM =
BacklashNP[axis].getValue() * GearRatioNP[axis].getValue();
449 const auto &approachSP = (axis ==
BACKLASH_AZ) ? AZApproachSP : ALTApproachSP;
450 const double direction = approachSP[APPROACH_NEGATIVE].getState() ==
ISS_ON ? -1 : 1;
453 if (backlashMM <= 0 || mm * direction >= 0)
456 mm -= direction * backlashMM;
457 return direction * backlashMM;
460bool AvalonUPAS::sendJog(
double azMM,
double altMM)
462 char cmd[DRIVER_LEN] = {0};
463 int len = snprintf(
cmd, DRIVER_LEN,
"$J=G91G21");
465 len += snprintf(
cmd + len, DRIVER_LEN - len,
"X%.4f", azMM);
467 len += snprintf(
cmd + len, DRIVER_LEN - len,
"Y%.4f", altMM);
468 snprintf(
cmd + len, DRIVER_LEN - len,
" F%.0f", feedRateFor(azMM, altMM));
470 char res[DRIVER_LEN] = {0};
471 if (!sendCommand(
cmd, res))
475 if (strncmp(res,
"ok", 2) != 0)
484double AvalonUPAS::feedRateFor(
double azMM,
double altMM)
487 const double maxRate =
SpeedNP[0].getValue();
488 const double azRate = std::min(maxRate, FeedRateNP[FEED_AZ].getValue());
489 const double altRate = std::min(maxRate, FeedRateNP[FEED_ALT].getValue());
493 const double length = std::hypot(azMM, altMM);
497 double feedRate = std::numeric_limits<double>::max();
499 feedRate = std::min(feedRate, azRate * length / std::fabs(azMM));
501 feedRate = std::min(feedRate, altRate * length / std::fabs(altMM));
508 LOGF_INFO(
"UPAS speed limited to %u mm/min.", speed);
515 const char cancel =
'\x85';
517 int rc =
tty_write(PortFD, &cancel, 1, &nbytes);
522 LOGF_ERROR(
"AbortMotion serial write error: %s.", errstr);
534 m_ReverseFlags |= 0x01;
536 m_ReverseFlags &= ~0x01;
538 char cmd[DRIVER_LEN] = {0};
539 snprintf(
cmd, DRIVER_LEN,
"$3=%d", m_ReverseFlags);
541 char res[DRIVER_LEN] = {0};
542 if (!sendCommand(
cmd, res))
545 LOGF_INFO(
"AZ reverse %s (flags=0x%02X).", enabled ?
"enabled" :
"disabled", m_ReverseFlags);
546 return (strncmp(res,
"ok", 2) == 0);
552 m_ReverseFlags |= 0x02;
554 m_ReverseFlags &= ~0x02;
556 char cmd[DRIVER_LEN] = {0};
557 snprintf(
cmd, DRIVER_LEN,
"$3=%d", m_ReverseFlags);
559 char res[DRIVER_LEN] = {0};
560 if (!sendCommand(
cmd, res))
563 LOGF_INFO(
"ALT reverse %s (flags=0x%02X).", enabled ?
"enabled" :
"disabled", m_ReverseFlags);
564 return (strncmp(res,
"ok", 2) == 0);
570 LOGF_INFO(
"Backlash compensation %s.", enabled ?
"enabled" :
"disabled");
575 LOG_INFO(
"Moves against the approach direction overshoot by the backlash. "
576 "Keep at least that margin from the mechanical stops.");
599 if (GearRatioNP.isNameMatch(name))
601 GearRatioNP.update(values, names,
n);
602 GearRatioNP.setState(
IPS_OK);
607 if (FeedRateNP.isNameMatch(name))
609 FeedRateNP.update(values, names,
n);
610 FeedRateNP.setState(
IPS_OK);
615 if (MinMoveNP.isNameMatch(name))
617 MinMoveNP.update(values, names,
n);
618 MinMoveNP.setState(
IPS_OK);
634 if (AZApproachSP.isNameMatch(name) || ALTApproachSP.isNameMatch(name))
636 auto &approachSP = AZApproachSP.isNameMatch(name) ? AZApproachSP : ALTApproachSP;
637 approachSP.update(states, names,
n);
638 approachSP.setState(
IPS_OK);
654bool AvalonUPAS::sendCommand(
const char *
cmd,
char *res,
int cmd_len,
int res_len)
656 int nbytes_written = 0, nbytes_read = 0, rc = -1;
658 tcflush(PortFD, TCIOFLUSH);
663 char hex_cmd[DRIVER_LEN * 3] = {0};
664 hexDump(hex_cmd,
cmd, cmd_len);
672 char formatted[DRIVER_LEN] = {0};
673 snprintf(formatted, DRIVER_LEN,
"%s\n",
cmd);
681 LOGF_ERROR(
"Serial write error: %s.", errstr);
689 rc =
tty_read(PortFD, res, res_len, DRIVER_TIMEOUT, &nbytes_read);
691 rc =
tty_nread_section(PortFD, res, DRIVER_LEN, DRIVER_STOP_CHAR, DRIVER_TIMEOUT, &nbytes_read);
702 if (nbytes_read > 0 && (res[nbytes_read - 1] ==
'\n' || res[nbytes_read - 1] ==
'\r'))
703 res[nbytes_read - 1] =
'\0';
704 if (nbytes_read > 1 && (res[nbytes_read - 2] ==
'\r'))
705 res[nbytes_read - 2] =
'\0';
709 tcflush(PortFD, TCIOFLUSH);
713void AvalonUPAS::hexDump(
char *buf,
const char *data,
int size)
715 for (
int i = 0; i < size; i++)
716 sprintf(buf + 3 * i,
"%02X ",
static_cast<uint8_t
>(data[i]));
718 buf[3 * size - 1] =
'\0';
The AvalonUPAS class drives the Avalon Instruments Universal Polar Alignment System (UPAS) via its GR...
bool initProperties() override
Initialize properties initial state and value. The child class must implement this function.
bool SetBacklashEnabled(bool enabled) override
SetBacklashEnabled Enable or disable hardware backlash compensation.
bool saveConfigItems(FILE *fp) override
saveConfigItems Save specific properties in the provide config file handler. Child class usually over...
bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override
Process the client newSwitch command.
bool AbortMotion() override
AbortMotion Abort all axis motion immediately.
IPState MoveAZ(double degrees) override
Move the azimuth axis by the given number of degrees.
IPState MoveBoth(double azDegrees, double altDegrees) override
MoveBoth Move both axes in a single coordinated operation.
bool ReverseAZ(bool enabled) override
ReverseAZ Reverse the direction of azimuth axis movement.
bool ReverseALT(bool enabled) override
ReverseALT Reverse the direction of altitude axis movement.
const char * getDefaultName() override
bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
bool SetBacklashAZ(int32_t steps) override
SetBacklashAZ Set the azimuth axis backlash compensation value in steps.
void TimerHit() override
Callback function to be called once SetTimer duration elapses.
IPState MoveALT(double degrees) override
MoveALT Move the altitude axis by the given number of degrees.
bool SetBacklashALT(int32_t steps) override
SetBacklashALT Set the altitude axis backlash compensation value in steps.
bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
bool SetPACSpeed(uint16_t speed) override
SetPACSpeed Set the motor speed used for axis movements.
The Serial class manages connection with serial devices including Bluetooth. Serial communication is ...
const char * getDeviceName() const
virtual bool updateProperties()
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
virtual bool saveConfig(bool silent=false, const char *property=nullptr)
Save the current properties in a configuration file.
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
Process the client newSwitch command.
void registerConnection(Connection::Interface *newConnection)
registerConnection Add new connection plugin to the existing connection pool. The connection type sha...
void setDriverInterface(uint32_t value)
setInterface Set driver interface. By default the driver interface is set to GENERAL_DEVICE....
void setVersion(uint16_t vMajor, uint16_t vMinor)
Set driver version information to be defined in DRIVER_INFO property as vMajor.vMinor.
virtual bool deleteProperty(const char *propertyName)
Delete a property and unregister it. It will also be deleted from all clients.
void defineProperty(INumberVectorProperty *property)
virtual bool saveConfigItems(FILE *fp)
saveConfigItems Save specific properties in the provide config file handler. Child class usually over...
uint32_t getCurrentPollingPeriod() const
getCurrentPollingPeriod Return the current polling period.
virtual bool initProperties()
Initialize properties initial state and value. The child class must implement this function.
void addAuxControls()
Add Debug, Simulation, and Configuration options to the driver.
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
Process the client newNumber command.
int SetTimer(uint32_t ms)
Set a timer to call the function TimerHit after ms milliseconds.
friend class PACInterface
INDI::PropertySwitch BacklashSP
bool saveConfigItems(FILE *fp)
saveConfigItems Save PAC interface properties to config file. Call this from your driver's saveConfig...
bool processNumber(const char *dev, const char *name, double values[], char *names[], int n)
Process PAC number properties.
INDI::PropertyNumber SpeedNP
@ POSITION_ALT
Current altitude position in degrees.
@ POSITION_AZ
Current azimuth position in degrees.
void initProperties(const char *group)
initProperties Initialize PAC properties. Call this from your driver's initProperties().
void SetCapability(uint32_t cap)
SetCapability sets the PAC device capabilities. Call this in the driver constructor.
INDI::PropertyNumber PositionNP
INDI::PropertyNumber ManualAdjustmentNP
bool processSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
Process PAC switch properties.
bool updateProperties()
updateProperties Define or delete properties based on connection state. Call this from your driver's ...
INDI::PropertySwitch ALTReverseSP
INDI::PropertySwitch AZReverseSP
INDI::PropertyNumber BacklashNP
const char * MAIN_CONTROL_TAB
MAIN_CONTROL_TAB Where all the primary controls for the device are located.
const char * INFO_TAB
INFO_TAB Where all the properties for general information 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.
*brief user defined string literal for JSON values *sa std::size_t n
#define LOGF_INFO(fmt,...)
#define LOGF_DEBUG(fmt,...)
#define LOG_ERROR(txt)
Shorter logging macros. In order to use these macros, the function (or method) "getDeviceName()" must...
#define LOGF_ERROR(fmt,...)