forked from indilib/indi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfocusmaster.cpp
341 lines (269 loc) · 8.2 KB
/
focusmaster.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
/*******************************************************************************
Copyright(c) 2017 Jasem Mutlaq. All rights reserved.
Televue FocusMaster Driver.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
.
This library 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
Library General Public License for more details.
.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*******************************************************************************/
#include "focusmaster.h"
#include <cmath>
#include <cstring>
#include <memory>
#include <unistd.h>
#define POLLMS_OVERRIDE 1000 /* 1000 ms */
#define FOCUSMASTER_TIMEOUT 1000 /* 1000 ms */
#define MAX_FM_BUF 16
#define FOCUS_SETTINGS_TAB "Settings"
// We declare an auto pointer to FocusMaster.
std::unique_ptr<FocusMaster> focusMaster(new FocusMaster());
FocusMaster::FocusMaster()
{
FI::SetCapability(FOCUSER_CAN_ABORT);
setConnection(CONNECTION_NONE);
}
bool FocusMaster::Connect()
{
handle = hid_open(0x134A, 0x9030, nullptr);
if (handle == nullptr)
{
LOG_ERROR("No FocusMaster focuser found.");
return false;
}
else
{
// N.B. Check here if we have the digital readout gadget.
// if digital readout
//FI::SetCapability(GetFocuserCapability() | FOCUSER_CAN_REL_MOVE | FOCUSER_CAN_ABS_MOVE);
SetTimer(POLLMS_OVERRIDE);
}
return (handle != nullptr);
}
bool FocusMaster::Disconnect()
{
hid_close(handle);
hid_exit();
return true;
}
const char *FocusMaster::getDefaultName()
{
return (const char *)"FocusMaster";
}
bool FocusMaster::initProperties()
{
INDI::Focuser::initProperties();
// Sync to a particular position
IUFillNumber(&SyncN[0], "Ticks", "", "%.f", 0, 100000, 100., 0.);
IUFillNumberVector(&SyncNP, SyncN, 1, getDeviceName(), "Sync", "", MAIN_CONTROL_TAB, IP_RW, 0, IPS_IDLE);
// Full Forward/Reverse Motion
IUFillSwitch(&FullMotionS[FOCUS_INWARD], "FULL_INWARD", "Full Inward", ISS_OFF);
IUFillSwitch(&FullMotionS[FOCUS_OUTWARD], "FULL_OUTWARD", "Full Outward", ISS_OFF);
IUFillSwitchVector(&FullMotionSP, FullMotionS, 2, getDeviceName(), "FULL_MOTION", "Full Motion", MAIN_CONTROL_TAB, IP_RW,
ISR_ATMOST1, 0, IPS_IDLE);
FocusAbsPosN[0].min = SyncN[0].min = 0;
FocusAbsPosN[0].max = SyncN[0].max;
FocusAbsPosN[0].step = SyncN[0].step;
FocusAbsPosN[0].value = 0;
FocusRelPosN[0].max = (FocusAbsPosN[0].max - FocusAbsPosN[0].min) / 2;
FocusRelPosN[0].step = FocusRelPosN[0].max / 100.0;
FocusRelPosN[0].value = 100;
addSimulationControl();
return true;
}
bool FocusMaster::updateProperties()
{
INDI::Focuser::updateProperties();
if (isConnected())
{
defineProperty(&FullMotionSP);
//defineProperty(&SyncNP);
}
else
{
deleteProperty(FullMotionSP.name);
//deleteProperty(SyncNP.name);
}
return true;
}
void FocusMaster::TimerHit()
{
if (!isConnected())
return;
//uint32_t currentTicks = 0;
if (FocusTimerNP.s == IPS_BUSY)
{
float remaining = CalcTimeLeft(focusMoveStart, focusMoveRequest);
if (remaining <= 0)
{
FocusTimerNP.s = IPS_OK;
FocusTimerN[0].value = 0;
AbortFocuser();
}
else
FocusTimerN[0].value = remaining * 1000.0;
IDSetNumber(&FocusTimerNP, nullptr);
}
#if 0
bool rc = getPosition(¤tTicks);
if (rc)
FocusAbsPosN[0].value = currentTicks;
if (FocusAbsPosNP.s == IPS_BUSY || FocusRelPosNP.s == IPS_BUSY)
{
if (targetPosition == FocusAbsPosN[0].value)
{
if (FocusRelPosNP.s == IPS_BUSY)
{
FocusRelPosNP.s = IPS_OK;
IDSetNumber(&FocusRelPosNP, nullptr);
}
FocusAbsPosNP.s = IPS_OK;
LOG_DEBUG("Focuser reached target position.");
}
}
IDSetNumber(&FocusAbsPosNP, nullptr);
#endif
SetTimer(POLLMS_OVERRIDE);
}
bool FocusMaster::ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
{
if (dev != nullptr && strcmp(dev, getDeviceName()) == 0)
{
// Full Motion
if (!strcmp(FullMotionSP.name, name))
{
IUUpdateSwitch(&FullMotionSP, states, names, n);
FocusDirection targetDirection = static_cast<FocusDirection>(IUFindOnSwitchIndex(&FullMotionSP));
FullMotionSP.s = MoveFocuser(targetDirection, 0, 0);
return true;
}
}
return INDI::Focuser::ISNewSwitch(dev, name, states, names, n);
}
bool FocusMaster::ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
{
if (dev != nullptr && strcmp(dev, getDeviceName()) == 0)
{
// Sync
if (strcmp(SyncNP.name, name) == 0)
{
IUUpdateNumber(&SyncNP, values, names, n);
if (!sync(SyncN[0].value))
SyncNP.s = IPS_ALERT;
else
SyncNP.s = IPS_OK;
IDSetNumber(&SyncNP, nullptr);
return true;
}
}
return INDI::Focuser::ISNewNumber(dev, name, values, names, n);
}
IPState FocusMaster::MoveFocuser(FocusDirection dir, int speed, uint16_t duration)
{
INDI_UNUSED(speed);
uint8_t command[MAX_FM_BUF] = {0};
if (dir == FOCUS_INWARD)
{
command[0] = 0x31;
command[1] = 0x21;
}
else
{
command[0] = 0x32;
command[1] = 0x22;
}
sendCommand(command);
gettimeofday(&focusMoveStart, nullptr);
focusMoveRequest = duration / 1000.0;
if (duration > 0 && duration <= POLLMS_OVERRIDE)
{
usleep(duration * 1000);
AbortFocuser();
return IPS_OK;
}
return IPS_BUSY;
}
IPState FocusMaster::MoveAbsFocuser(uint32_t targetTicks)
{
INDI_UNUSED(targetTicks);
FocusAbsPosNP.s = IPS_BUSY;
return IPS_BUSY;
}
IPState FocusMaster::MoveRelFocuser(FocusDirection dir, uint32_t ticks)
{
uint32_t finalTicks = FocusAbsPosN[0].value + (ticks * (dir == FOCUS_INWARD ? -1 : 1));
return MoveAbsFocuser(finalTicks);
}
bool FocusMaster::sendCommand(const uint8_t *command, char *response)
{
INDI_UNUSED(response);
int rc = hid_write(handle, command, 2);
LOGF_DEBUG("CMD <%#02X %#02X>", command[0], command[1]);
if (rc < 0)
{
LOGF_ERROR("<%#02X %#02X>: Error writing to device %s", command[0], command[1], hid_error(handle));
return false;
}
return true;
}
bool FocusMaster::setPosition(uint32_t ticks)
{
INDI_UNUSED(ticks);
return false;
}
bool FocusMaster::getPosition(uint32_t *ticks)
{
INDI_UNUSED(ticks);
return false;
}
bool FocusMaster::AbortFocuser()
{
uint8_t command[MAX_FM_BUF] = {0};
command[0] = 0x30;
command[1] = 0x30;
LOG_DEBUG("Aborting Focuser...");
bool rc = sendCommand(command);
if (rc)
{
if (FullMotionSP.s == IPS_BUSY)
{
IUResetSwitch(&FullMotionSP);
FullMotionSP.s = IPS_IDLE;
IDSetSwitch(&FullMotionSP, nullptr);
}
if (FocusMotionSP.s == IPS_BUSY)
{
IUResetSwitch(&FocusMotionSP);
FocusMotionSP.s = IPS_IDLE;
IDSetSwitch(&FocusMotionSP, nullptr);
}
}
return rc;
}
bool FocusMaster::sync(uint32_t ticks)
{
INDI_UNUSED(ticks);
return false;
}
float FocusMaster::CalcTimeLeft(timeval start, float req)
{
double timesince;
double timeleft;
struct timeval now
{
0, 0
};
gettimeofday(&now, nullptr);
timesince = (double)(now.tv_sec * 1000.0 + now.tv_usec / 1000) - (double)(start.tv_sec * 1000.0 + start.tv_usec / 1000);
timesince = timesince / 1000;
timeleft = req - timesince;
return timeleft;
}