Debian Codegraph @

fli_pdf.cpp

raw
1 #if 0
2 FLI PDF
3 INDI Interface for Finger Lakes Instrument Focusers
4 Copyright (C) 2003-2012 Jasem Mutlaq ([email protected])
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 #endif
21
22 #include "fli_pdf.h"
23
24 #include "indidevapi.h"
25 #include "eventloop.h"
26
27 #include <memory>
28 #include <string.h>
29 #include <time.h>
30 #include <math.h>
31 #include <unistd.h>
32 #include <sys/time.h>
33
34 static std::unique_ptr<FLIPDF> fliPDF(new FLIPDF());
35
36 const flidomain_t Domains[] = { FLIDOMAIN_USB, FLIDOMAIN_SERIAL, FLIDOMAIN_PARALLEL_PORT, FLIDOMAIN_INET };
37
38 FLIPDF::FLIPDF()
39 {
40 sim = false;
41
42 FI::SetCapability(FOCUSER_CAN_ABS_MOVE | FOCUSER_CAN_REL_MOVE);
43 }
44
45 const char *FLIPDF::getDefaultName()
46 {
47 return "FLI PDF";
48 }
49
50 bool FLIPDF::initProperties()
51 {
52 // Init parent properties first
53 INDI::Focuser::initProperties();
54
55 IUFillSwitch(&PortS[0], "USB", "USB", ISS_ON);
56 IUFillSwitch(&PortS[1], "SERIAL", "Serial", ISS_OFF);
57 IUFillSwitch(&PortS[2], "PARALLEL", "Parallel", ISS_OFF);
58 IUFillSwitch(&PortS[3], "INET", "INet", ISS_OFF);
59 IUFillSwitchVector(&PortSP, PortS, 4, getDeviceName(), "PORTS", "Port", MAIN_CONTROL_TAB, IP_WO, ISR_1OFMANY, 0,
60 IPS_IDLE);
61
62 IUFillSwitch(&HomeS[0], "Go", "", ISS_OFF);
63 IUFillSwitchVector(&HomeSP, HomeS, 1, getDeviceName(), "Home", "", MAIN_CONTROL_TAB, IP_WO, ISR_1OFMANY, 0,
64 IPS_IDLE);
65
66 IUFillText(&FocusInfoT[0], "Model", "", "");
67 IUFillText(&FocusInfoT[1], "HW Rev", "", "");
68 IUFillText(&FocusInfoT[2], "FW Rev", "", "");
69 IUFillTextVector(&FocusInfoTP, FocusInfoT, 3, getDeviceName(), "Model", "", "Focuser Info", IP_RO, 60, IPS_IDLE);
70 return true;
71 }
72
73 void FLIPDF::ISGetProperties(const char *dev)
74 {
75 INDI::Focuser::ISGetProperties(dev);
76
77 defineProperty(&PortSP);
78
79 addAuxControls();
80 }
81
82 bool FLIPDF::updateProperties()
83 {
84 INDI::Focuser::updateProperties();
85
86 if (isConnected())
87 {
88 defineProperty(&FocusAbsPosNP);
89 defineProperty(&FocusRelPosNP);
90 defineProperty(&HomeSP);
91 defineProperty(&FocusInfoTP);
92 setupParams();
93
94 timerID = SetTimer(getCurrentPollingPeriod());
95 }
96 else
97 {
98 deleteProperty(FocusAbsPosNP.name);
99 deleteProperty(FocusRelPosNP.name);
100 deleteProperty(HomeSP.name);
101 deleteProperty(FocusInfoTP.name);
102
103 rmTimer(timerID);
104 }
105
106 return true;
107 }
108
109 bool FLIPDF::ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
110 {
111 if (strcmp(dev, getDeviceName()) == 0)
112 {
113 /* Home */
114 if (!strcmp(name, HomeSP.name))
115 {
116 if (IUUpdateSwitch(&HomeSP, states, names, n) < 0)
117 return false;
118
119 goHomePosition();
120 return true;
121 }
122
123 /* Ports */
124 if (!strcmp(name, PortSP.name))
125 {
126 if (IUUpdateSwitch(&PortSP, states, names, n) < 0)
127 return false;
128
129 PortSP.s = IPS_OK;
130 IDSetSwitch(&PortSP, nullptr);
131 return true;
132 }
133 }
134
135 // Nobody has claimed this, so, ignore it
136 return INDI::Focuser::ISNewSwitch(dev, name, states, names, n);
137 }
138
139 bool FLIPDF::Connect()
140 {
141 int err = 0;
142
143 IDMessage(getDeviceName(), "Attempting to find the FLI PDF...");
144
145 sim = isSimulation();
146
147 if (sim)
148 return true;
149
150 int portSwitchIndex = IUFindOnSwitchIndex(&PortSP);
151
152 if (findFLIPDF(Domains[portSwitchIndex]) == false)
153 {
154 LOG_ERROR("Error: no focusers were detected.");
155 return false;
156 }
157
158 if ((err = FLIOpen(&fli_dev, FLIFocus.name, FLIDEVICE_FOCUSER | FLIFocus.domain)))
159 {
160 LOGF_ERROR("Error: FLIOpen() failed. %s.", strerror((int)-err));
161 return false;
162 }
163
164 /* Success! */
165 LOG_INFO("Focuser is online. Retrieving basic data.");
166 return true;
167 }
168
169 bool FLIPDF::Disconnect()
170 {
171 int err;
172
173 if (sim)
174 return true;
175
176 if ((err = FLIClose(fli_dev)))
177 {
178 LOGF_ERROR("Error: FLIClose() failed. %s.", strerror((int)-err));
179
180 return false;
181 }
182
183 LOG_INFO("Focuser is offline.");
184 return true;
185 }
186
187 bool FLIPDF::setupParams()
188 {
189 int err = 0;
190
191 char hw_rev[16], fw_rev[16];
192
193 //////////////////////
194 // 1. Get Focuser Model
195 //////////////////////
196 if (!sim && (err = FLIGetModel(fli_dev, FLIFocus.model, 200))) //ToDo: lazy
197 {
198 LOGF_ERROR("FLIGetModel() failed. %s.", strerror((int)-err));
199 return false;
200 }
201
202 if (sim)
203 IUSaveText(&FocusInfoT[0], getDeviceName());
204 else
205 IUSaveText(&FocusInfoT[0], FLIFocus.model);
206
207 ///////////////////////////
208 // 2. Get Hardware revision
209 ///////////////////////////
210 if (sim)
211 FLIFocus.HWRevision = 1;
212 else if ((err = FLIGetHWRevision(fli_dev, &FLIFocus.HWRevision)))
213 {
214 LOGF_ERROR("FLIGetHWRevision() failed. %s.", strerror((int)-err));
215
216 if (isDebug())
217 IDLog("FLIGetHWRevision() failed. %s.\n", strerror((int)-err));
218
219 return false;
220 }
221
222 snprintf(hw_rev, 16, "%ld", FLIFocus.HWRevision);
223 IUSaveText(&FocusInfoT[1], hw_rev);
224
225 ///////////////////////////
226 // 3. Get Firmware revision
227 ///////////////////////////
228 if (sim)
229 FLIFocus.FWRevision = 1;
230 else if ((err = FLIGetFWRevision(fli_dev, &FLIFocus.FWRevision)))
231 {
232 LOGF_ERROR("FLIGetFWRevision() failed. %s.", strerror((int)-err));
233 return false;
234 }
235
236 snprintf(fw_rev, 16, "%ld", FLIFocus.FWRevision);
237 IUSaveText(&FocusInfoT[2], fw_rev);
238
239 IDSetText(&FocusInfoTP, nullptr);
240 ///////////////////////////
241 // 4. Focuser position
242 ///////////////////////////
243 if (sim)
244 FLIFocus.current_pos = 3500;
245 else if ((err = FLIGetStepperPosition(fli_dev, &FLIFocus.current_pos)))
246 {
247 LOGF_ERROR("FLIGetStepperPosition() failed. %s.", strerror((int)-err));
248 return false;
249 }
250
251 ///////////////////////////
252 // 5. Focuser max limit
253 ///////////////////////////
254 if (sim)
255 FLIFocus.max_pos = 50000;
256 else if ((err = FLIGetFocuserExtent(fli_dev, &FLIFocus.max_pos)))
257 {
258 LOGF_ERROR("FLIGetFocuserExtent() failed. %s.", strerror((int)-err));
259 return false;
260 }
261
262 FocusAbsPosN[0].min = 1;
263 FocusAbsPosN[0].max = FLIFocus.max_pos;
264 FocusAbsPosN[0].value = FLIFocus.current_pos;
265
266 IUUpdateMinMax(&FocusAbsPosNP);
267 IDSetNumber(&FocusAbsPosNP, "Setting initial absolute position");
268
269 FocusRelPosN[0].min = 1.;
270 FocusRelPosN[0].max = FLIFocus.max_pos;
271 FocusRelPosN[0].value = 0.;
272
273 IUUpdateMinMax(&FocusRelPosNP);
274 IDSetNumber(&FocusRelPosNP, "Setting initial relative position");
275
276 /////////////////////////////////////////
277 // 6. Focuser speed is set to 100 tick/sec
278 //////////////////////////////////////////
279 FocusSpeedN[0].value = 100;
280 IDSetNumber(&FocusSpeedNP, "Setting initial speed");
281
282 return true;
283 }
284
285 void FLIPDF::goHomePosition()
286 {
287 int err = 0;
288
289 if (!sim && (err = FLIHomeFocuser(fli_dev)))
290 {
291 LOGF_ERROR("FLIHomeFocuser() failed. %s.", strerror((int)-err));
292 return;
293 }
294
295 HomeSP.s = IPS_OK;
296 IUResetSwitch(&HomeSP);
297 IDSetSwitch(&HomeSP, "Moving to home position...");
298 }
299
300 void FLIPDF::TimerHit()
301 {
302 int timerID = -1;
303 int err = 0;
304
305 if (isConnected() == false)
306 return; // No need to Home timer if we are not connected anymore
307
308 if (InStep)
309 {
310 if (sim)
311 {
312 if (FLIFocus.current_pos < StepRequest)
313 {
314 FLIFocus.current_pos += 250;
315 if (FLIFocus.current_pos > StepRequest)
316 FLIFocus.current_pos = StepRequest;
317 }
318 else
319 {
320 FLIFocus.current_pos -= 250;
321 if (FLIFocus.current_pos < StepRequest)
322 FLIFocus.current_pos = StepRequest;
323 }
324 }
325 // while moving, display the remaing steps
326 else if ((err = FLIGetStepsRemaining(fli_dev, &FLIFocus.steps_remaing)))
327 {
328 LOGF_ERROR("FLIGetStepsRemaining() failed. %s.", strerror((int)-err));
329 SetTimer(getCurrentPollingPeriod());
330 return;
331 }
332 if (!FLIFocus.steps_remaing)
333 {
334 InStep = false;
335 FocusAbsPosNP.s = IPS_OK;
336 if (FocusRelPosNP.s == IPS_BUSY)
337 {
338 FocusRelPosNP.s = IPS_OK;
339 IDSetNumber(&FocusRelPosNP, nullptr);
340 }
341 }
342
343 FocusAbsPosN[0].value = FLIFocus.steps_remaing;
344 IDSetNumber(&FocusAbsPosNP, nullptr);
345 }
346 else // we need to display the current position after move finished
347 {
348 if ((err = FLIGetStepperPosition(fli_dev, &FLIFocus.current_pos)))
349 {
350 LOGF_ERROR("FLIGetStepperPosition() failed. %s.", strerror((int)-err));
351 return;
352 }
353 FocusAbsPosN[0].value = FLIFocus.current_pos;
354 IDSetNumber(&FocusAbsPosNP, nullptr);
355 }
356
357 if (timerID == -1)
358 SetTimer(getCurrentPollingPeriod());
359 return;
360 }
361
362 IPState FLIPDF::MoveAbsFocuser(uint32_t targetTicks)
363 {
364 int err = 0;
365
366 if (targetTicks < FocusAbsPosN[0].min || targetTicks > FocusAbsPosN[0].max)
367 {
368 LOG_ERROR("Error, requested absolute position is out of range.");
369 return IPS_ALERT;
370 }
371 long current;
372 if ((err = FLIGetStepperPosition(fli_dev, &current)))
373 {
374 LOGF_ERROR("FLIPDF::MoveAbsFocuser: FLIGetStepperPosition() failed. %s.", strerror((int)-err));
375 return IPS_ALERT;
376 }
377 err = FLIStepMotorAsync(fli_dev, (targetTicks - current));
378 if (!sim && (err))
379 {
380 LOGF_ERROR("FLIStepMotor() failed. %s.", strerror((int)-err));
381 return IPS_ALERT;
382 }
383
384 StepRequest = targetTicks;
385 InStep = true;
386
387 // We are still moving, didn't reach target yet
388 return IPS_BUSY;
389 }
390
391 IPState FLIPDF::MoveRelFocuser(FocusDirection dir, uint32_t ticks)
392 {
393 long cur_rpos = 0;
394 long new_rpos = 0;
395
396 cur_rpos = FLIFocus.current_pos;
397
398 if (dir == FOCUS_INWARD)
399 new_rpos = cur_rpos + ticks;
400 else
401 new_rpos = cur_rpos - ticks;
402
403 return MoveAbsFocuser(new_rpos);
404 }
405
406 bool FLIPDF::findFLIPDF(flidomain_t domain)
407 {
408 char **names;
409 long err;
410
411 LOGF_DEBUG("In find Focuser, the domain is %ld", domain);
412
413 if ((err = FLIList(domain | FLIDEVICE_FOCUSER, &names)))
414 {
415 LOGF_ERROR("FLIList() failed. %s", strerror((int)-err));
416 return false;
417 }
418
419 if (names != nullptr && names[0] != nullptr)
420 {
421 for (int i = 0; names[i] != nullptr; i++)
422 {
423 for (int j = 0; names[i][j] != '\0'; j++)
424 if (names[i][j] == ';')
425 {
426 names[i][j] = '\0';
427 break;
428 }
429 }
430
431 FLIFocus.domain = domain;
432
433 switch (domain)
434 {
435 case FLIDOMAIN_PARALLEL_PORT:
436 FLIFocus.dname = strdup("parallel port");
437 break;
438
439 case FLIDOMAIN_USB:
440 FLIFocus.dname = strdup("USB");
441 break;
442
443 case FLIDOMAIN_SERIAL:
444 FLIFocus.dname = strdup("serial");
445 break;
446
447 case FLIDOMAIN_INET:
448 FLIFocus.dname = strdup("inet");
449 break;
450
451 default:
452 FLIFocus.dname = strdup("Unknown domain");
453 }
454
455 FLIFocus.name = strdup(names[0]);
456
457 if ((err = FLIFreeList(names)))
458 {
459 LOGF_ERROR("FLIFreeList() failed. %s.", strerror((int)-err));
460 return false;
461 }
462
463 } /* end if */
464 else
465 {
466 if ((err = FLIFreeList(names)))
467 {
468 LOGF_ERROR("FLIFreeList() failed. %s.", strerror((int)-err));
469 return false;
470 }
471
472 return false;
473 }
474
475 LOG_DEBUG("FindFLIPDF() finished successfully.");
476
477 return true;
478 }