31 #define SCOPEDOME_TIMEOUT 2
33 static const uint8_t header = 0xaa;
40 rc = write(ConnectionTest);
45 if (
cmd != ConnectionTest)
50 rc = write(StopSafeCommunication);
52 return (
cmd == StopSafeCommunication);
55 uint8_t ScopeDomeUSB21::CRC(uint8_t crc, uint8_t data)
59 for (
int i = 0; i < 8; i++)
62 crc = ((crc >> 1) ^ 0x8C);
69 int ScopeDomeUSB21::writeBuf(
Command cmd, uint8_t len, uint8_t *buff)
71 int BytesToWrite = len + 4;
72 int nbytes_written = 0, rc = -1;
75 uint8_t cbuf[BytesToWrite];
77 cbuf[3] = CRC(0, cbuf[0]);
79 cbuf[3] = CRC(cbuf[3], cbuf[1]);
81 cbuf[3] = CRC(cbuf[3], cbuf[2]);
83 for (
int i = 0; i < len; i++)
85 cbuf[i + 4] = buff[i];
86 cbuf[3] = CRC(cbuf[3], buff[i]);
89 tcflush(PortFD, TCIOFLUSH);
94 if ((rc =
tty_write(PortFD, (
const char *)cbuf,
sizeof(cbuf), &nbytes_written)) !=
TTY_OK)
104 int nbytes_written = 0, rc = -1;
108 tcflush(PortFD, TCIOFLUSH);
111 cbuf[3] = CRC(0, cbuf[0]);
113 cbuf[3] = CRC(cbuf[3], cbuf[1]);
115 cbuf[3] = CRC(cbuf[3], cbuf[2]);
120 LOGF_DEBUG(
"write cmd: %x %x %x %x", cbuf[0], cbuf[1], cbuf[2], cbuf[3]);
121 if ((rc =
tty_write(PortFD, (
const char *)cbuf,
sizeof(cbuf), &nbytes_written)) !=
TTY_OK)
124 LOGF_ERROR(
"Error writing command: %s. Cmd: %d", errstr,
cmd);
129 int ScopeDomeUSB21::readBuf(
Command &
cmd, uint8_t len, uint8_t *buff)
131 int nbytes_read = 0, rc = -1;
132 int BytesToRead = len + 4;
133 uint8_t cbuf[BytesToRead];
140 LOGF_ERROR(
"Error reading: %s. Cmd: %d", errstr, prevcmd);
144 LOGF_DEBUG(
"readbuf cmd: %x %x %x %x", cbuf[0], cbuf[1], cbuf[2], cbuf[3]);
145 uint8_t Checksum = CRC(0, cbuf[0]);
146 Checksum = CRC(Checksum, cbuf[1]);
147 cmd = (Command)cbuf[2];
148 Checksum = CRC(Checksum, cbuf[2]);
150 for (
int i = 0; i < len; i++)
152 buff[i] = cbuf[i + 4];
153 Checksum = CRC(Checksum, cbuf[i + 4]);
156 if (cbuf[3] != Checksum)
158 LOGF_ERROR(
"readbuf checksum error, cmd: %d", prevcmd);
159 return CHECKSUM_ERROR;
161 if (
cmd == FunctionNotSupported)
163 LOG_ERROR(
"readbuf not supported error");
164 return FUNCTION_NOT_SUPPORTED_BY_FIRMWARE;
169 LOGF_ERROR(
"readbuf packet length error, cmd: %d", prevcmd);
170 return PACKET_LENGTH_ERROR;
177 int nbytes_read = 0, rc = -1;
179 uint8_t cbuf[4] = { 0 };
186 LOGF_ERROR(
"Error reading: %s. Cmd %d", errstr, prevcmd);
190 LOGF_DEBUG(
"read cmd: %x %x %x %x", cbuf[0], cbuf[1], cbuf[2], cbuf[3]);
191 uint8_t Checksum = CRC(0, cbuf[0]);
192 Checksum = CRC(Checksum, cbuf[1]);
193 cmd = (Command)cbuf[2];
194 Checksum = CRC(Checksum, cbuf[2]);
196 if (cbuf[3] != Checksum || cbuf[1] != 0)
198 LOGF_ERROR(
"read checksum error, cmd: %d", prevcmd);
199 return CHECKSUM_ERROR;
205 err = MOTION_CONFLICT;
208 case FunctionNotSupported:
209 LOG_ERROR(
"read function not supported");
210 err = FUNCTION_NOT_SUPPORTED;
226 bool ScopeDomeUSB21::readFloat(
Command cmd,
float &dst)
236 rc = readBuf(c, 4, (uint8_t *)&value);
240 while (rc != 0 && --retryCount);
250 bool ScopeDomeUSB21::readU8(
Command cmd, uint8_t &dst)
260 rc = readBuf(c, 1, &value);
264 while (rc != 0 && --retryCount);
274 bool ScopeDomeUSB21::readS8(
Command cmd, int8_t &dst)
284 rc = readBuf(c, 1, (uint8_t *)&value);
288 while (rc != 0 && --retryCount);
298 bool ScopeDomeUSB21::readU16(
Command cmd, uint16_t &dst)
308 rc = readBuf(c, 2, (uint8_t *)&value);
312 while (rc != 0 && --retryCount);
322 bool ScopeDomeUSB21::readS16(
Command cmd, int16_t &dst)
332 rc = readBuf(c, 2, (uint8_t *)&value);
336 while (rc != 0 && --retryCount);
346 bool ScopeDomeUSB21::readU32(
Command cmd, uint32_t &dst)
356 rc = readBuf(c, 4, (uint8_t *)&value);
360 while (rc != 0 && --retryCount);
370 bool ScopeDomeUSB21::readS32(
Command cmd, int32_t &dst)
380 rc = readBuf(c, 4, (uint8_t *)&value);
384 while (rc != 0 && --retryCount);
394 int ScopeDomeUSB21::readBuffer(
Command cmd,
int len, uint8_t *cbuf)
403 rc = readBuf(c, len, cbuf);
407 while (rc != 0 && --retryCount);
422 int ScopeDomeUSB21::writeU8(
Command cmd, uint8_t value)
424 int rc = writeBuf(
cmd, 1, &value);
433 int ScopeDomeUSB21::writeU16(
Command cmd, uint16_t value)
435 int rc = writeBuf(
cmd, 2, (uint8_t *)&value);
444 int ScopeDomeUSB21::writeU32(
Command cmd, uint32_t value)
446 int rc = writeBuf(
cmd, 4, (uint8_t *)&value);
455 int ScopeDomeUSB21::writeBuffer(
Command cmd,
int len, uint8_t *cbuf)
457 int rc = writeBuf(
cmd, len, cbuf);
466 void ScopeDomeUSB21::reconnect()
473 int rc = readU16(GetStatus, status);
474 rc |= readS16(GetCounter, counter);
475 rc |= readBuffer(GetAllDigitalExt, 5, digitalSensorState);
486 uint16_t fwVersion = 0;
487 readU16(GetVersionFirmware, fwVersion);
488 main = fwVersion / 100.0;
490 uint8_t fwVersionRotary = 0;
491 readU8(GetVersionFirmwareRotary, fwVersionRotary);
492 rotary = (fwVersionRotary + 9) / 10.0;
497 uint32_t stepsPerRevolution = 0;
498 readU32(GetImpPerTurn, stepsPerRevolution);
499 return stepsPerRevolution;
515 channel = IN_CLOSED1;
521 channel = IN_CLOSED1;
524 channel = IN_ROT_LINK;
539 channel = OUT_RELAY1;
557 int ch = (int)channel;
559 uint8_t bit = 1 << (ch & 7);
565 return writeU8(onOff ==
ISS_ON ? SetDigitalChannel : ClearDigitalChannel, (uint8_t)channel);
575 readS32(GetCounterExt, counterExt);
581 uint8_t calibrationNeeded =
false;
582 readU8(IsFullSystemCalReq, calibrationNeeded);
583 return calibrationNeeded != 0;
593 writeCmd(FullSystemCal);
622 writeCmd(ResetCounter);
623 writeCmd(ResetCounterExt);
630 writeU16(CCWRotation, -steps);
634 writeU16(CWRotation, steps);
650 info.
label =
"Shutter link strength";
657 info.
label =
"Shutter internal power";
664 info.
label =
"Shutter battery power";
671 info.
label =
"Card internal power";
678 info.
label =
"Card battery power";
685 info.
label =
"Temperature in dome";
692 info.
label =
"Temperature outside dome";
698 info.
propName =
"TEMP_DOME_HUMIDITY";
699 info.
label =
"Temperature humidity sensor";
706 info.
label =
"Humidity";
713 info.
label =
"Pressure";
720 info.
label =
"Dew point";
740 readU8(GetLinkStrength, linkStrength);
741 value = linkStrength;
746 static int count = 0;
747 if (linkStrength == 0)
763 readFloat(GetAnalog1, sensors[0]);
767 readFloat(GetAnalog2, sensors[1]);
771 readFloat(GetMainAnalog1, sensors[2]);
775 readFloat(GetMainAnalog2, sensors[3]);
779 readFloat(GetTempIn, sensors[4]);
783 readFloat(GetTempOut, sensors[5]);
787 readFloat(GetTempHum, sensors[6]);
791 readFloat(GetHum, sensors[7]);
795 readFloat(GetPressure, sensors[8]);
799 value = parent->getDewPoint(sensors[7], sensors[6]);
824 info.
label =
"Telescope";
828 info.
label =
"Light";
836 info.
label =
"Relay 1 (reset)";
840 info.
label =
"Relay 2 (heater)";
844 info.
label =
"Relay 3";
848 info.
label =
"Relay 4";
931 info.
label =
"Az counter";
935 info.
label =
"Dome at home";
939 info.
label =
"Shutter 1 open";
943 info.
label =
"Shutter 1 closed";
947 info.
label =
"Shutter 2 open";
951 info.
label =
"Shutter 2 closed";
955 info.
label =
"Scope at home";
959 info.
label =
"Rain sensor";
963 info.
label =
"Cloud sensor";
967 info.
label =
"Observatory safe";
971 info.
label =
"Rotary link";
975 info.
label =
"Free input";
1032 writeU8(NegHomeSensorActiveState, negate);
virtual void calibrate() override
virtual void abort() override
virtual void getFirmwareVersions(double &main, double &rotary) override
virtual double getSensorValue(size_t index) override
virtual ISState getInputValue(size_t index) override
virtual int updateState() override
virtual int getRotationCounterExt() override
virtual uint32_t getStepsPerRevolution() override
virtual size_t getNumberOfRelays() override
virtual size_t getNumberOfInputs() override
virtual ISState getRelayState(size_t index) override
virtual int setOutputState(AbstractOutput output, ISState state) override
virtual void resetCounter() override
virtual uint32_t getStatus() override
virtual ISState getInputState(AbstractInput input) override
virtual void findHome() override
virtual bool detect() override
virtual size_t getNumberOfSensors() override
virtual int getRotationCounter() override
virtual RelayInfo getRelayInfo(size_t index) override
virtual bool isCalibrationNeeded() override
virtual SensorInfo getSensorInfo(size_t index) override
virtual void setRelayState(size_t index, ISState state) override
virtual void controlShutter(ShutterOperation operation) override
virtual void setHomeSensorPolarity(HomeSensorPolarity polarity) override
virtual InputInfo getInputInfo(size_t index) override
virtual void move(int steps) override
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
void tty_error_msg(int err_code, char *err_msg, int err_msg_len)
Retrieve the tty error message.
Implementations for common driver routines.
#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,...)
Command
The Command enum includes all the command types sent to the various devices (motor,...
@ value
the parser finished reading a JSON value
#define SCOPEDOME_TIMEOUT