Debian Codegraph @

maxdomeiidriver.cpp

raw
1 /*
2 Max Dome II Driver
3 Copyright (C) 2009 Ferran Casarramona ([email protected])
4 Refactored by Juan Menendez
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 This library 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 GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
20 */
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <termios.h>
25 #include <indicom.h>
26 #include <indilogger.h>
27
28 #include "maxdomeiidriver.h"
29
30 #define MAXDOME_TIMEOUT 5 // FD timeout in seconds
31 #define BUFFER_SIZE 16 // Maximum message length
32
33 // Start byte
34 #define START_BYTE 0x01
35
36 // Message Destination
37 #define TO_MAXDOME 0x00
38 #define TO_COMPUTER 0x80
39
40 // Commands available
41 #define ABORT_CMD 0x03 // Abort azimuth movement
42 #define HOME_CMD 0x04 // Move until 'home' position is detected
43 #define GOTO_CMD 0x05 // Go to azimuth position
44 #define SHUTTER_CMD 0x06 // Send a command to Shutter
45 #define STATUS_CMD 0x07 // Retrieve status
46 #define TICKS_CMD 0x09 // Set the number of tick per revolution of the dome
47 #define ACK_CMD 0x0A // ACK (?)
48 #define SETPARK_CMD 0x0B // Set park coordinates and if need to park before to operating shutter
49
50 // Shutter commands
51 #define OPEN_SHUTTER 0x01
52 #define OPEN_UPPER_ONLY_SHUTTER 0x02
53 #define CLOSE_SHUTTER 0x03
54 #define EXIT_SHUTTER 0x04 // Command send to shutter on program exit
55 #define ABORT_SHUTTER 0x07
56
57
58 // Error messages
59 const char *ErrorMessages[] = {
60 "Ok", // no error
61 "No response from MAX DOME", // -1
62 "Invalid declared message length", // -2
63 "Message too short", // -3
64 "Checksum error", // -4
65 "Could not send command", // -5
66 "Response do not match command", // -6
67 ""
68 };
69
70 char device_str[MAXINDIDEVICE] = "MaxDome II";
71
72
73 void hexDump(char *buf, const char *data, int size)
74 {
75 for (int i = 0; i < size; i++)
76 sprintf(buf + 3 * i, "%02X ", (unsigned char)data[i]);
77
78 if (size > 0)
79 buf[3 * size - 1] = '\0';
80 }
81
82 void MaxDomeIIDriver::SetPortFD(int port_fd)
83 {
84 fd = port_fd;
85 }
86
87 // This method is required by the logging macros
88 const char *MaxDomeIIDriver::getDeviceName()
89 {
90 return device_str;
91 }
92
93 void MaxDomeIIDriver::SetDevice(const char *name)
94 {
95 strncpy(device_str, name, MAXINDIDEVICE);
96 }
97
98 /*
99 Opens serial port with proper configuration
100
101 @param device String to device (/dev/ttyS0)
102 @return -1 if can't connect. File descriptor, otherwise.
103 */
104 int MaxDomeIIDriver::Connect(const char *device)
105 {
106 if (tty_connect(device, 19200, 8, 0, 1, &fd) != TTY_OK)
107 return -1;
108
109 return fd;
110 }
111
112 /*
113 Inform Max Dome from a disconection and closes serial port
114
115 @param fd File descriptor
116 @return 0 Ok,
117 */
118 int MaxDomeIIDriver::Disconnect()
119 {
120 //ExitShutter(); // Really don't know why this is needed, but ASCOM driver does it
121 tty_disconnect(fd);
122
123 return 0;
124 }
125
126 /*
127 Calculates or checks the checksum
128 It ignores first byte
129 For security we limit the checksum calculation to BUFFER_SIZE length
130
131 @param msg Pointer to unsigned char array with the message
132 @param len Length of the message
133 @return Checksum byte
134 */
135 signed char computeChecksum(char *msg, int len)
136 {
137 char checksum = 0;
138
139 for (int i=1; i<len && i<BUFFER_SIZE; i++)
140 checksum -= msg[i];
141
142 return checksum;
143 }
144
145 /*
146 Reads a response from MaxDome II
147 It verifies message sintax and checksum.
148 Read data is stored in MaxDomeIIDriver::buffer
149
150 @return
151 - Respose size if message is Ok
152 - -1: no response or no start caracter found
153 - -2: invalid declared message length
154 - -3: message too short
155 - -4: checksum error
156 */
157 int MaxDomeIIDriver::ReadResponse()
158 {
159 int nbytes;
160 int err = TTY_OK;
161
162 buffer[0] = 0x00;
163 buffer[13] = 0x00;
164
165 // Look for a starting byte until time out occurs or BUFFER_SIZE bytes were read
166 while (*buffer != START_BYTE && err == TTY_OK)
167 err = tty_read(fd, buffer, 1, MAXDOME_TIMEOUT, &nbytes);
168
169 if (err != TTY_OK || buffer[0] != START_BYTE) {
170 LOG_ERROR(ErrorMessages[1]);
171 return -1;
172 }
173
174 // Read message length
175 err = tty_read(fd, buffer + 1, 1, MAXDOME_TIMEOUT, &nbytes);
176 if (err != TTY_OK || buffer[1] < 0x02 || buffer[1] > 0x0e) {
177 LOG_ERROR(ErrorMessages[2]);
178 return -2;
179 }
180
181 int len = buffer[1];
182
183 // Read the rest of the message
184 err = tty_read(fd, buffer + 2, len, MAXDOME_TIMEOUT, &nbytes);
185 if (err != TTY_OK || nbytes != len) {
186 LOG_ERROR(ErrorMessages[3]);
187 return -3;
188 }
189
190 if (computeChecksum(buffer, len + 2) != 0) {
191 LOG_ERROR(ErrorMessages[4]);
192 return -4;
193 }
194
195 return len + 2;
196 }
197
198 /*
199 Send a command to MaxDome II
200
201 @param cmdId Command identifier code
202 @param payload Payload data
203 @param payloadLen Length of payload data
204 @return
205 - 0: command received by MAX DOME
206 - -1: no response or no start caracter found
207 - -2: invalid declared message length
208 - -3: message too short
209 - -4: checksum error
210 - -5: couldn't send command
211 - -6: response don't match command
212 */
213 int MaxDomeIIDriver::SendCommand(char cmdId, const char *payload, int payloadLen)
214 {
215 int err;
216 int nbytes;
217 char errmsg[MAXRBUF];
218 char cmd[BUFFER_SIZE];
219 char hexbuf[3*BUFFER_SIZE];
220
221 cmd[0] = START_BYTE;
222 cmd[1] = payloadLen + 2;
223 cmd[2] = cmdId;
224 memcpy(cmd + 3, payload, payloadLen);
225 cmd[3 + payloadLen] = computeChecksum(cmd, 3 + payloadLen);
226
227 hexDump(hexbuf, cmd, 4 + payloadLen);
228 LOGF_DEBUG("CMD (%s)", hexbuf);
229
230 tcflush(fd, TCIOFLUSH);
231
232 if ((err = tty_write(fd, cmd, 4 + payloadLen, &nbytes)) != TTY_OK)
233 {
234 tty_error_msg(err, errmsg, MAXRBUF);
235 LOG_ERROR(errmsg);
236 return -5;
237 }
238
239 nbytes = ReadResponse();
240 if (nbytes < 0)
241 return nbytes;
242
243 if (buffer[2] != (char)(cmdId | TO_COMPUTER)) {
244 LOG_ERROR(ErrorMessages[6]);
245 return -6;
246 }
247
248 hexDump(hexbuf, buffer, nbytes);
249 LOGF_DEBUG("RES (%s)", hexbuf);
250
251 return 0;
252 }
253
254 /*
255 Abort azimuth movement
256
257 @return Same as SendCommand
258 */
259 int MaxDomeIIDriver::AbortAzimuth()
260 {
261 LOG_INFO("Azimuth movement aborted");
262 return SendCommand(ABORT_CMD, nullptr, 0);
263 }
264
265 /*
266 Move until 'home' position is detected
267
268 @return Same as SendCommand
269 */
270 int MaxDomeIIDriver::HomeAzimuth()
271 {
272 LOG_INFO("Homing azimuth");
273 return SendCommand(HOME_CMD, nullptr, 0);
274 }
275
276 /*
277 Go to a new azimuth position
278
279 @param nDir Direcction of the movement. 0 E to W. 1 W to E
280 @param nTicks Ticks from home position in E to W direction.
281 @return Same as SendCommand
282 */
283 int MaxDomeIIDriver::GotoAzimuth(int nDir, int nTicks)
284 {
285 LOGF_DEBUG("Moving dome to azimuth: %d", nTicks);
286
287 char payload[3];
288 payload[0] = (char)nDir;
289 payload[1] = (char)(nTicks / 256);
290 payload[2] = (char)(nTicks % 256);
291
292 return SendCommand(GOTO_CMD, payload, sizeof(payload));
293 }
294
295 /*
296 Ask Max Dome status
297
298 @param shStatus Returns shutter status
299 @param azStatus Returns azimuth status
300 @param azimuthPos Returns azimuth current position (in ticks from home position)
301 @param homePos Returns last position where home was detected
302 @return Same as SendCommand
303 */
304 int MaxDomeIIDriver::Status(ShStatus *shStatus, AzStatus *azStatus,
305 unsigned *azimuthPos, unsigned *homePos)
306 {
307 int ret = SendCommand(STATUS_CMD, nullptr, 0);
308 if (ret != 0) {
309 return ret;
310 }
311
312 *shStatus = (ShStatus)buffer[3];
313 *azStatus = (AzStatus)buffer[4];
314 *azimuthPos = (unsigned)(((uint8_t)buffer[5]) * 256 + ((uint8_t)buffer[6]));
315 *homePos = ((unsigned)buffer[7]) * 256 + ((unsigned)buffer[8]);
316
317 LOGF_DEBUG("Dome status: az=%d home=%d", *azimuthPos, *homePos);
318 return 0;
319 }
320
321 /*
322 Ack comunication
323
324 @return Same as SendCommand
325 */
326 int MaxDomeIIDriver::Ack()
327 {
328 LOG_DEBUG("ACK sent");
329 return SendCommand(ACK_CMD, nullptr, 0);
330 }
331
332 /*
333 Set park coordinates and if need to park before to operating shutter
334
335 @param nParkOnShutter
336 - 0 operate shutter at any azimuth.
337 - 1 go to park position before operating shutter
338 @param nTicks Ticks from home position in E to W direction.
339 @return Same as SendCommand
340 */
341 int MaxDomeIIDriver::SetPark(int nParkOnShutter, int nTicks)
342 {
343 LOGF_INFO("Setting park position: %d", nTicks);
344
345 char payload[3];
346 payload[0] = (char)nParkOnShutter;
347 payload[1] = (char)(nTicks / 256);
348 payload[2] = (char)(nTicks % 256);
349
350 return SendCommand(SETPARK_CMD, payload, sizeof(payload));
351 }
352
353 /*
354 Set ticks per turn of the dome
355
356 @param nTicks Ticks from home position in E to W direction.
357 @return Same as SendCommand
358 */
359 int MaxDomeIIDriver::SetTicksPerTurn(int nTicks)
360 {
361 LOGF_INFO("Setting ticks per turn: %d", nTicks);
362
363 char payload[2];
364 payload[0] = (char)(nTicks / 256);
365 payload[1] = (char)(nTicks % 256);
366
367 return SendCommand(TICKS_CMD, payload, sizeof(payload));
368 }
369
370 ///////////////////////////////////////////////////////////////////////////
371 //
372 // Shutter commands
373 //
374 ///////////////////////////////////////////////////////////////////////////
375
376 /*
377 Opens the shutter fully
378
379 @return Same as SendCommand
380 */
381 int MaxDomeIIDriver::OpenShutter()
382 {
383 LOG_INFO("Opening shutter");
384
385 char payload[] = { OPEN_SHUTTER };
386 return SendCommand(SHUTTER_CMD, payload, sizeof(payload));
387 }
388
389 /*
390 Opens the upper shutter only
391
392 @return Same as SendCommand
393 */
394 int MaxDomeIIDriver::OpenUpperShutterOnly()
395 {
396 LOG_INFO("Opening upper shutter");
397
398 char payload[] = { OPEN_UPPER_ONLY_SHUTTER };
399 return SendCommand(SHUTTER_CMD, payload, sizeof(payload));
400 }
401
402 /*
403 Close the shutter
404
405 @return Same as SendCommand
406 */
407 int MaxDomeIIDriver::CloseShutter()
408 {
409 LOG_INFO("Closing shutter");
410
411 char payload[] = { CLOSE_SHUTTER };
412 return SendCommand(SHUTTER_CMD, payload, sizeof(payload));
413 }
414
415 /*
416 Abort shutter movement
417
418 @return Same as SendCommand
419 */
420 int MaxDomeIIDriver::AbortShutter()
421 {
422 LOG_INFO("Aborting shutter operation");
423
424 char payload[] = { ABORT_SHUTTER };
425 return SendCommand(SHUTTER_CMD, payload, sizeof(payload));
426 }
427
428 /*
429 Exit shutter (?) Normally send at program exit
430
431 @return Same as SendCommand
432 */
433 int MaxDomeIIDriver::ExitShutter()
434 {
435 LOG_INFO("Exiting shutter");
436
437 char payload[] = { EXIT_SHUTTER };
438 return SendCommand(SHUTTER_CMD, payload, sizeof(payload));
439 }