-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpimoco_focuser.cpp
executable file
·311 lines (244 loc) · 8.9 KB
/
pimoco_focuser.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
/*
PiMoCo: Raspberry Pi Telescope Mount and Focuser Control
Copyright (C) 2021 Markus Noga
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 <https://www.gnu.org/licenses/>.
*/
#include "pimoco_focuser.h"
#include <libindi/indilogger.h>
#define CDRIVER_VERSION_MAJOR 1
#define CDRIVER_VERSION_MINOR 0
// Singleton instance
PimocoFocuser focuser;
// C function interface redirecting to singleton
//
extern "C" {
void ISGetProperties(const char *dev) {
focuser.ISGetProperties(dev);
}
void ISNewBLOB(const char *dev, const char *name, int sizes[], int blobsizes[], char *blobs[], char *formats[], char *names[], int n) {
focuser.ISNewBLOB(dev, name, sizes, blobsizes, blobs, formats, names, n);
}
void ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) {
focuser.ISNewNumber(dev, name, values, names, n);
}
void ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) {
focuser.ISNewSwitch(dev, name, states, names, n);
}
void ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) {
focuser.ISNewText(dev, name, texts, names, n);
}
void ISSnoopDevice(XMLEle *root) {
focuser.ISSnoopDevice(root);
}
} // extern "C"
// Public class members
//
PimocoFocuser::PimocoFocuser() : stepper(getDeviceName(), "Focuser", FOCUSER_DIAG0_PIN), spiDeviceFilename("/dev/spidev0.2") {
setVersion(CDRIVER_VERSION_MAJOR, CDRIVER_VERSION_MINOR);
FI::SetCapability(FOCUSER_CAN_ABS_MOVE | FOCUSER_CAN_REL_MOVE | FOCUSER_CAN_ABORT |
// FOCUSER_CAN_REVERSE | // superseded by stepper class controls which serve both mount and focuser
FOCUSER_CAN_SYNC |
// | FOCUSER_HAS_VARIABLE_SPEED // superseded by stepper class controls which serve both mount and focuser
0);
setSupportedConnections(CONNECTION_NONE);
}
PimocoFocuser::~PimocoFocuser() {
stepper.stop();
}
const char *PimocoFocuser::getDefaultName() {
return "Pimoco focuser";
}
bool PimocoFocuser::initProperties() {
if(!INDI::Focuser::initProperties())
return false;
FocusMaxPosN[0].max=2000000000;
IUUpdateMinMax(&FocusMaxPosNP);
stepper.initProperties(MotorN, &MotorNP, MSwitchS, &MSwitchSP, RampN, &RampNP,
"MOTOR", "Motor", "SWITCHES", "Switches", "RAMP", "Ramp", FOCUS_TAB);
// load configuration data from file, as there is no device with own storage
loadConfig(true, MotorNP.name);
loadConfig(true, MSwitchSP.name);
loadConfig(true, RampNP.name);
addDebugControl();
return true;
}
bool PimocoFocuser::updateProperties() {
if(!INDI::Focuser::updateProperties())
return false;
if(!stepper.updateProperties(this, MotorN, &MotorNP, MSwitchS, &MSwitchSP, RampN, &RampNP))
return false;
if(isConnected()) {
// ...
} else {
// ...
}
return true;
}
bool PimocoFocuser::ISNewBLOB(const char *dev, const char *name, int sizes[], int blobsizes[], char *blobs[], char *formats[], char *names[], int n) {
if(dev==NULL || strcmp(dev,getDeviceName()))
return INDI::Focuser::ISNewBLOB(dev, name, sizes, blobsizes, blobs, formats, names, n);
// if(strcmp(name, ...)) { } else
return INDI::Focuser::ISNewBLOB(dev, name, sizes, blobsizes, blobs, formats, names, n);
}
bool PimocoFocuser::ISUpdateNumber(INumberVectorProperty *NP, double values[], char *names[], int n, bool res)
{
if(res) {
IUUpdateNumber(NP, values, names, n);
saveConfig(true, NP->name);
}
NP->s=res ? IPS_OK : IPS_ALERT;
IDSetNumber(NP, NULL);
return res;
}
bool PimocoFocuser::ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) {
if(dev==NULL || strcmp(dev,getDeviceName()))
return INDI::Focuser::ISNewNumber(dev, name, values, names, n);
int res;
if((res=stepper.ISNewNumber(&MotorNP, &RampNP, name, values, names, n)) > 0) {
saveConfig(true, MotorNP.name);
saveConfig(true, RampNP.name);
return true;
} else if(res==0)
return false;
return INDI::Focuser::ISNewNumber(dev, name, values, names, n);
}
bool PimocoFocuser::ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) {
if(dev==NULL || strcmp(dev,getDeviceName()))
return INDI::Focuser::ISNewSwitch(dev, name, states, names, n);
int res;
if((res=stepper.ISNewSwitch(&MSwitchSP, name, states, names, n)) >0) {
saveConfig(true, MSwitchSP.name);
return true;
} else if(res==0)
return false;
return INDI::Focuser::ISNewSwitch(dev, name, states, names, n);
}
bool PimocoFocuser::ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) {
if(dev==NULL || strcmp(dev,getDeviceName()))
return INDI::Focuser::ISNewText(dev, name, texts, names, n);
// if(strcmp(name, ...)) { } else
return INDI::Focuser::ISNewText(dev, name, texts, names, n);
}
bool PimocoFocuser::ISSnoopDevice(XMLEle *root) {
return INDI::Focuser::ISSnoopDevice(root);
}
bool PimocoFocuser::saveConfigItems(FILE *fp){
INDI::Focuser::saveConfigItems(fp);
IUSaveConfigNumber(fp, &MotorNP);
IUSaveConfigSwitch(fp, &MSwitchSP);
IUSaveConfigNumber(fp, &RampNP);
return true;
}
// Protected class members
//
bool PimocoFocuser::Connect() {
LOGF_INFO("Attempting connection on %s", spiDeviceFilename);
if(!stepper.open(spiDeviceFilename)) {
LOGF_WARN("Connection on %s failed", spiDeviceFilename);
return false;
}
if(!ReadFocuserStatus())
return false;
LOGF_INFO("Connection on %s successful", spiDeviceFilename);
uint32_t pp=getPollingPeriod();
if (pp > 0)
SetTimer(pp);
return true;
}
bool PimocoFocuser::Disconnect() {
if(!stepper.close()) {
LOG_WARN("Error closing connection");
return false;
}
LOG_INFO("Successfully closed connection");
return true;
}
bool PimocoFocuser::Handshake() {
return true;
}
void PimocoFocuser::TimerHit() {
if(!isConnected())
return;
ReadFocuserStatus();
SetTimer(getCurrentPollingPeriod());
}
bool PimocoFocuser::ReadFocuserStatus() {
auto pos=0;
if(!stepper.getPosition(&pos)) {
LOG_ERROR("Error reading position");
FocusAbsPosNP.s = IPS_ALERT;
return false;
} else {
auto status=stepper.getStatus();
if((FocusAbsPosNP.s==IPS_BUSY) && (status&Stepper::TMC_STAND_STILL))
LOGF_INFO("Focuser has reached position %u", pos);
FocusAbsPosN[0].value = pos;
FocusAbsPosNP.s = (status & Stepper::TMC_STAND_STILL) ? IPS_OK : IPS_BUSY;
FocusRelPosNP.s = FocusAbsPosNP.s;
IDSetNumber(&FocusAbsPosNP, NULL);
IDSetNumber(&FocusRelPosNP, NULL);
}
return true;
}
IPState PimocoFocuser::MoveAbsFocuser(uint32_t targetTicks) {
if(!stepper.setTargetPosition(targetTicks)) {
LOG_ERROR("Error setting focuser target position");
return IPS_ALERT;
}
return IPS_BUSY;
}
IPState PimocoFocuser::MoveRelFocuser(FocusDirection dir, uint32_t ticks) {
int32_t pos;
if(!stepper.getPosition(&pos)) {
LOG_ERROR("Error getting current focuser position");
return IPS_ALERT;
}
int32_t targetPos=(dir==FOCUS_OUTWARD) ? pos + ticks : pos - ticks;
LOGF_INFO("Focuser is moving by %c%u to target position %u", (dir==FOCUS_OUTWARD)?'+':'-', ticks, targetPos);
if(!stepper.setTargetPosition(targetPos)) {
LOG_ERROR("Error setting focuser target position");
return IPS_ALERT;
}
return IPS_BUSY;
}
bool PimocoFocuser::AbortFocuser() {
LOG_INFO("Stopping focuser motion");
if(!stepper.stop()) {
LOG_ERROR("Error stopping focuser motion");
return false;
}
return true;
}
bool PimocoFocuser::ReverseFocuser(bool enabled) {
LOGF_INFO("Setting direction of focuser motion to %s", enabled ? "reversed" : "normal");
if(!stepper.setInvertMotor(enabled)) {
LOG_ERROR("Error setting direction of focuser motion");
return false;
}
return true;
}
bool PimocoFocuser::SetFocuserSpeed(int speed) {
LOGF_INFO("Setting focuser max speed to %d", speed);
if(!stepper.setMaxGoToSpeed((uint32_t) speed)) {
LOG_ERROR("Error setting focuser max speed");
return false;
}
return true;
}
bool PimocoFocuser::SyncFocuser(uint32_t ticks) {
LOGF_INFO("Syncing focuser position to %u", ticks);
if(!stepper.syncPosition((int32_t) ticks)) {
LOG_ERROR("Error syncing focuser position");
return false;
}
return true;
}