forked from sebo-b/TicFocuser-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicFocuser.cpp
457 lines (365 loc) · 13.4 KB
/
TicFocuser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*******************************************************************************
TicFocuser
Copyright (C) 2019-2021 Sebastian Baberowski
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <memory>
#include <string.h>
#include <iostream>
#include <fstream>
#include <string>
#include "TicFocuser_config.h"
#include "TicFocuser.h"
#include "connection/SerialConnection.h"
#include "connection/ticlib/TicDefs.h"
#ifdef WITH_LIBTIC
#include "connection/PololuUsbConnection.h"
#endif
#ifdef WITH_LIBUSB
#include "connection/LibUsbConnection.h"
#endif
#ifdef WITH_BLUETOOTH
#include "connection/BluetoothConnection.h"
#endif
//#include <indilogger.h>
//INDI::Logger::getInstance().print("TIC Focuser NG",INDI::Logger::DBG_WARNING, __FILE__, __LINE__,"jest context");
std::unique_ptr<TicFocuser> ticFocuser(new TicFocuser());
void ISGetProperties(const char *dev)
{
ticFocuser->ISGetProperties(dev);
}
void ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int num)
{
ticFocuser->ISNewSwitch(dev, name, states, names, num);
}
void ISNewText( const char *dev, const char *name, char *texts[], char *names[], int num)
{
ticFocuser->ISNewText(dev, name, texts, names, num);
}
void ISNewNumber(const char *dev, const char *name, double values[], char *names[], int num)
{
ticFocuser->ISNewNumber(dev, name, values, names, num);
}
void ISNewBLOB (const char *dev, const char *name, int sizes[], int blobsizes[], char *blobs[], char *formats[], char *names[], int n)
{
INDI_UNUSED(dev);
INDI_UNUSED(name);
INDI_UNUSED(sizes);
INDI_UNUSED(blobsizes);
INDI_UNUSED(blobs);
INDI_UNUSED(formats);
INDI_UNUSED(names);
INDI_UNUSED(n);
}
void ISSnoopDevice (XMLEle *root)
{
ticFocuser->ISSnoopDevice(root);
}
TicFocuser::TicFocuser():
lastTimerHitError(false),
moveRelInitialValue(-1),
lastFocusDir(FOCUS_INWARD)
{
setVersion(TICFOCUSER_VERSION_MAJOR,TICFOCUSER_VERSION_MINOR);
setSupportedConnections(CONNECTION_NONE);
FI::SetCapability(FOCUSER_CAN_ABS_MOVE | FOCUSER_CAN_REL_MOVE | FOCUSER_CAN_SYNC | FOCUSER_CAN_ABORT | FOCUSER_HAS_BACKLASH);
InfoErrorS = new IText[tic_error_names_ui_size];
}
TicFocuser::~TicFocuser()
{
delete [] InfoErrorS;
}
bool TicFocuser::initProperties()
{
INDI::Focuser::initProperties();
IUFillSwitch(&FocusParkingModeS[0],"FOCUS_PARKON","Enable",ISS_OFF);
IUFillSwitch(&FocusParkingModeS[1],"FOCUS_PARKOFF","Disable",ISS_ON);
IUFillSwitchVector(&FocusParkingModeSP,FocusParkingModeS,2,getDeviceName(),"FOCUS_PARK_MODE","Parking Mode",OPTIONS_TAB,IP_RW,ISR_1OFMANY,60,IPS_IDLE);
IUFillSwitch(&EnergizeFocuserS[0],"ENERGIZE_FOCUSER","Energize focuser",ISS_OFF);
IUFillSwitch(&EnergizeFocuserS[1],"DEENERGIZE_FOCUSER","De-energize focuser",ISS_OFF);
IUFillSwitchVector(&EnergizeFocuserSP,EnergizeFocuserS,2,getDeviceName(),"ENERGIZE_FOCUSER","Energize",MAIN_CONTROL_TAB,IP_RW,ISR_1OFMANY,60,IPS_IDLE);
/***** INFO_TAB */
IUFillText(&InfoS[VIN_VOLTAGE], "VIN_VOLTAGE", "Vin voltage", "");
IUFillText(&InfoS[CURRENT_LIMIT], "CURRENT_LIMIT", "Current limit", "");
IUFillText(&InfoS[STEP_MODE], "STEP_MODE", "Step mode", "");
IUFillText(&InfoS[ENERGIZED], "ENERGIZED", "Energized", "");
IUFillText(&InfoS[OPERATION_STATE], "OPERATION_STATE", "Operational state", "");
IUFillTextVector(&InfoSP, InfoS, InfoTabSize, getDeviceName(), "TIC_INFO", "Tic Info", INFO_TAB, IP_RO, 60, IPS_IDLE);
/***** INFO_TAB > Error */
for (size_t i = 0; i < tic_error_names_ui_size; ++i)
IUFillText(&InfoErrorS[i], tic_error_names_ui[i].name, tic_error_names_ui[i].name, "");
IUFillTextVector(&InfoErrorSP, InfoErrorS, tic_error_names_ui_size, getDeviceName(), "TIC_INFO_ERROR", "Tic Error", INFO_TAB, IP_RO, 60, IPS_IDLE);
/***** Connections */
#ifdef WITH_LIBTIC
registerConnection(new PololuUsbConnection(this));
#endif
#ifdef WITH_LIBUSB
registerConnection(new LibUsbConnection(this));
#endif
#ifdef WITH_BLUETOOTH
registerConnection(new BluetoothConnection(this));
#endif
registerConnection(new SerialConnection(this));
return true;
}
bool TicFocuser::updateProperties()
{
INDI::Focuser::updateProperties();
if (isConnected())
{
defineSwitch(&EnergizeFocuserSP);
defineSwitch(&FocusParkingModeSP);
defineText(&InfoSP);
defineText(&InfoErrorSP);
}
else
{
deleteProperty(FocusParkingModeSP.name);
deleteProperty(EnergizeFocuserSP.name);
deleteProperty(InfoSP.name);
deleteProperty(InfoErrorSP.name);
}
return true;
}
bool TicFocuser::ISNewNumber (const char *dev, const char *name, double values[], char *names[], int n)
{
return INDI::Focuser::ISNewNumber(dev,name,values,names,n);
}
bool TicFocuser::ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
{
// first we check if it's for our device
if (!strcmp(dev, getDeviceName()))
{
// handle parking mode
if(!strcmp(name, FocusParkingModeSP.name))
{
IUUpdateSwitch(&FocusParkingModeSP, states, names, n);
FocusParkingModeSP.s = IPS_OK;
IDSetSwitch(&FocusParkingModeSP, NULL);
return true;
}
if(!strcmp(name, EnergizeFocuserSP.name))
{
bool res;
if (!strcmp(names[0],EnergizeFocuserS[0].name))
res = energizeFocuser();
else
res = deenergizeFocuser();
EnergizeFocuserSP.s = res? IPS_OK: IPS_ALERT;
IDSetSwitch(&EnergizeFocuserSP, NULL);
return true;
}
}
return INDI::Focuser::ISNewSwitch(dev,name,states,names,n);
}
bool TicFocuser::saveConfigItems(FILE *fp)
{
if (!Focuser::saveConfigItems(fp))
return false;
IUSaveConfigSwitch(fp, &FocusParkingModeSP);
return true;
}
bool TicFocuser::Disconnect()
{
// park focuser
if (FocusParkingModeS[0].s != ISS_ON) {
LOG_INFO("Parking mode disabled, parking not performed.");
}
else {
MoveAbsFocuser(0);
}
return Focuser::Disconnect();
}
bool TicFocuser::Connect()
{
bool res = Focuser::Connect();
if (res) {
energizeFocuser(); // Error will be shown by energizeFocuser() function, no need to show it here
}
return res;
}
void TicFocuser::TimerHit()
{
if (!isConnected())
return;
TicConnectionInterface* conn = dynamic_cast<TicConnectionInterface*>(getActiveConnection());
TicDriverInterface& driverInterface = conn->getTicDriverInterface();
TicDriverInterface::TicVariables ticVariables;
bool res = driverInterface.getVariables(&ticVariables);
if (res) {
lastTimerHitError = false;
FocusAbsPosN[0].value = ticVariables.currentPosition;
FocusSyncN[0].value = ticVariables.currentPosition;
if (FocusAbsPosNP.s == IPS_BUSY) {
if (moveRelInitialValue >= 0) {
FocusRelPosN[0].value = abs( moveRelInitialValue - ticVariables.currentPosition);
}
if ( ticVariables.currentPosition == ticVariables.targetPosition) {
FocusAbsPosNP.s = IPS_OK;
FocusRelPosNP.s = IPS_OK;
moveRelInitialValue = -1;
}
}
IDSetNumber(&FocusAbsPosNP, nullptr);
IDSetNumber(&FocusRelPosNP, nullptr);
IDSetNumber(&FocusSyncNP, nullptr);
/** INFO_TAB */
char buf[20];
std::snprintf(buf,sizeof(buf),"%.2f V", ((double)ticVariables.vinVoltage)/1000);
IUSaveText( &InfoS[VIN_VOLTAGE], buf);
if (ticVariables.currentLimit > 1000)
std::snprintf(buf,sizeof(buf),"%.2f A", ((double)ticVariables.currentLimit)/1000);
else
std::snprintf(buf,sizeof(buf),"%d mA", ticVariables.currentLimit);
IUSaveText( &InfoS[CURRENT_LIMIT], buf);
IUSaveText( &InfoS[ENERGIZED], ticVariables.energized? "Yes": "No");
IUSaveText( &InfoS[STEP_MODE], ticVariables.stepMode.c_str());
IUSaveText( &InfoS[OPERATION_STATE], ticVariables.operationalState.c_str());
IDSetText(&InfoSP, nullptr);
/***** INFO_TAB > Error */
for (size_t i = 0; i < tic_error_names_ui_size; ++i)
{
if (tic_error_names_ui[i].code & ticVariables.errorStatus)
IUSaveText(&InfoErrorS[i], "Error");
else
IUSaveText(&InfoErrorS[i], "-");
}
IDSetText(&InfoErrorSP, nullptr);
}
else if (!lastTimerHitError) {
LOGF_ERROR("Cannot receive variables: %s", driverInterface.getLastErrorMsg());
lastTimerHitError = true;
}
SetTimer(POLLMS);
}
bool TicFocuser::energizeFocuser()
{
TicConnectionInterface* conn = dynamic_cast<TicConnectionInterface*>(getActiveConnection());
TicDriverInterface& driverInterface = conn->getTicDriverInterface();
if (!driverInterface.energize())
{
LOGF_ERROR("Cannot energize motor. Error: %s", driverInterface.getLastErrorMsg());
return false;
}
if (!driverInterface.exitSafeStart())
{
LOGF_ERROR("Cannot exit safe start. Error: %s", driverInterface.getLastErrorMsg());
return false;
}
return true;
}
bool TicFocuser::deenergizeFocuser()
{
TicConnectionInterface* conn = dynamic_cast<TicConnectionInterface*>(getActiveConnection());
TicDriverInterface& driverInterface = conn->getTicDriverInterface();
if (!driverInterface.deenergize())
{
LOGF_ERROR("Cannot de-energize motor. Error: %s", driverInterface.getLastErrorMsg());
return false;
}
else
{
LOG_INFO("Focuser de-energized. You must energize it to resume normal operation.");
}
return true;
}
bool TicFocuser::SyncFocuser(uint32_t ticks)
{
TicConnectionInterface* conn = dynamic_cast<TicConnectionInterface*>(getActiveConnection());
TicDriverInterface& driverInterface = conn->getTicDriverInterface();
if (!driverInterface.haltAndSetPosition(ticks))
{
LOGF_ERROR("Cannot sync focuser. Error: %s", driverInterface.getLastErrorMsg());
return false;
}
return true;
}
bool TicFocuser::AbortFocuser()
{
TicConnectionInterface* conn = dynamic_cast<TicConnectionInterface*>(getActiveConnection());
TicDriverInterface& driverInterface = conn->getTicDriverInterface();
if (!driverInterface.haltAndHold())
{
LOGF_ERROR("Cannot abort TIC. Error: %s", driverInterface.getLastErrorMsg());
return false;
}
return true;
}
IPState TicFocuser::MoveFocuser(FocusDirection dir, int speed, uint16_t duration)
{
INDI_UNUSED(dir);
INDI_UNUSED(speed);
INDI_UNUSED(duration);
LOG_ERROR("Focuser does not support timer based motion.");
return IPS_ALERT;
}
IPState TicFocuser::MoveRelFocuser(FocusDirection dir, uint32_t ticks)
{
int32_t absTicks = FocusAbsPosN[0].value;
int32_t targetTicks;
if (dir == FOCUS_OUTWARD)
targetTicks = absTicks + ticks;
else
targetTicks = absTicks - ticks;
IPState ret = MoveAbsFocuser(targetTicks);
moveRelInitialValue = ret == IPS_BUSY? absTicks: -1;
FocusAbsPosNP.s = ret;
IDSetNumber(&FocusAbsPosNP, nullptr);
return ret;
}
IPState TicFocuser::MoveAbsFocuser(uint32_t ticks)
{
if (ticks == FocusAbsPosN[0].value)
{
return IPS_OK;
}
else if (ticks > FocusAbsPosN[0].value)
{
if(lastFocusDir == FOCUS_INWARD && FocusBacklashS[INDI_ENABLED].s == ISS_ON)
{
uint32_t nominal = ticks;
ticks = static_cast<uint32_t>(std::min(ticks + FocusBacklashN[0].value, FocusAbsPosN[0].max));
LOGF_INFO("Apply backlash (in->out): +%d", ticks - nominal);
}
lastFocusDir = FOCUS_OUTWARD;
}
else if (ticks < FocusAbsPosN[0].value && FocusBacklashS[INDI_ENABLED].s == ISS_ON)
{
if(lastFocusDir == FOCUS_OUTWARD)
{
uint32_t nominal = ticks;
ticks = static_cast<uint32_t>(std::max(ticks - FocusBacklashN[0].value, FocusAbsPosN[0].min));
LOGF_INFO("Apply backlash (out->in): %d", ticks - nominal);
}
lastFocusDir = FOCUS_INWARD;
}
if (ticks < FocusAbsPosN[0].min || ticks > FocusAbsPosN[0].max)
{
LOGF_ERROR("Requested position is out of range: %d", ticks);
return IPS_ALERT;
}
TicConnectionInterface* conn = dynamic_cast<TicConnectionInterface*>(getActiveConnection());
TicDriverInterface& driverInterface = conn->getTicDriverInterface();
if (!driverInterface.setTargetPosition(ticks))
{
LOGF_ERROR("Cannot set target position. Error: %s", driverInterface.getLastErrorMsg());
return IPS_ALERT;
}
return IPS_BUSY;
}
bool TicFocuser::SetFocuserBacklash(int32_t steps)
{
INDI_UNUSED(steps);
return true;
}