1 /*
2    Optec Gemini Focuser Rotator INDI driver
3    Copyright (C) 2017 Jasem Mutlaq (mutlaqja@ikarustech.com)
4 
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9 
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13     Lesser General Public License for more details.
14 
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18 
19 */
20 
21 #pragma once
22 
23 #include "indifocuser.h"
24 #include "indirotatorinterface.h"
25 
26 #include <map>
27 
28 class Gemini : public INDI::Focuser, public INDI::RotatorInterface
29 {
30     public:
31         Gemini();
32         ~Gemini();
33 
34         enum
35         {
36             FOCUS_A_COEFF,
37             FOCUS_B_COEFF,
38             FOCUS_C_COEFF,
39             FOCUS_D_COEFF,
40             FOCUS_E_COEFF,
41             FOCUS_F_COEFF
42         };
43         enum
44         {
45             STATUS_MOVING,
46             STATUS_HOMING,
47             STATUS_HOMED,
48             STATUS_FFDETECT,
49             STATUS_TMPPROBE,
50             STATUS_REMOTEIO,
51             STATUS_HNDCTRL,
52             STATUS_REVERSE,
53             STATUS_UNKNOWN
54         };
55         enum
56         {
57             GOTO_CENTER,
58             GOTO_HOME
59         };
60         typedef enum
61         {
62             DEVICE_FOCUSER,
63             DEVICE_ROTATOR
64         } DeviceType;
65 
66         virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override;
67         virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override;
68         virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override;
69 
70     protected:
71 
72         virtual bool Handshake() override;
73         virtual const char *getDefaultName() override;
74 
75         virtual bool initProperties() override;
76         virtual bool updateProperties() override;
77         virtual bool saveConfigItems(FILE *fp) override;
78 
79         // Focuser Functions
80         virtual IPState MoveAbsFocuser(uint32_t targetPosition) override;
81         virtual IPState MoveRelFocuser(FocusDirection dir, uint32_t ticks) override;
82         virtual IPState MoveFocuser(FocusDirection dir, int speed, uint16_t duration) override;
83         virtual bool AbortFocuser() override;
84 
85         virtual bool SetFocuserBacklash(int32_t steps) override;
86         virtual bool SetFocuserBacklashEnabled(bool enabled) override;
87 
88         virtual void TimerHit() override;
89 
90         // Misc functions
91         bool ack();
92         bool isResponseOK();
93 
94     protected:
95         // Move from private to public to validate
96         bool focuserConfigurationComplete = false;
97         bool rotatorConfigurationComplete = false;
98 
99         // Rotator Overrides
100         virtual IPState HomeRotator() override;
101         virtual IPState MoveRotator(double angle) override;
102         virtual bool ReverseRotator(bool enabled) override;
103         virtual bool SetRotatorBacklash(int32_t steps) override;
104         virtual bool SetRotatorBacklashEnabled(bool enabled) override;
105 
106     private:
107         uint32_t focuserSimPosition = 0;
108         uint32_t rotatorSimPosition = 0;
109         uint32_t rotatorSimPA = 0;
110         uint32_t targetFocuserPosition = 0;
111         uint32_t targetRotatorPosition = 0;
112         uint32_t targetRotatorAngle = 0;
113         uint32_t maxControllerTicks = 0;
114 
115         ISState focuserSimStatus[8];
116         ISState rotatorSimStatus[8];
117 
118         bool simCompensationOn;
119         char focusTarget[8];
120 
121         struct timeval focusMoveStart;
122         float focusMoveRequest;
123 
124         ////////////////////////////////////////////////////////////
125         // Focuser Functions
126         ///////////////////////////////////////////////////////////
127 
128         // Get functions
129         bool getFocusConfig();
130         bool getFocusStatus();
131 
132         // Set functions
133 
134         // Position
135         bool setFocusPosition(u_int16_t position);
136 
137         // Temperature
138         bool setTemperatureCompensation(bool enable);
139         bool setTemperatureCompensationMode(char mode);
140         bool setTemperatureCompensationCoeff(char mode, int16_t coeff);
141         bool setTemperatureCompensationOnStart(bool enable);
142 
143         // Backlash
144         bool setBacklashCompensation(DeviceType type, bool enable);
145         bool setBacklashCompensationSteps(DeviceType type, uint16_t steps);
146 
147         // Motion functions
148         bool home(DeviceType type);
149         bool halt(DeviceType type);
150         bool center(DeviceType type);
151         bool homeOnStart(DeviceType type, bool enable);
152 
153         ////////////////////////////////////////////////////////////
154         // Focuser Properties
155         ///////////////////////////////////////////////////////////
156 
157         // Set/Get Temperature
158         INumber TemperatureN[1];
159         INumberVectorProperty TemperatureNP;
160 
161         // Enable/Disable temperature compnesation
162         ISwitch TemperatureCompensateS[2];
163         ISwitchVectorProperty TemperatureCompensateSP;
164 
165         // Enable/Disable temperature compnesation on start
166         ISwitch TemperatureCompensateOnStartS[2];
167         ISwitchVectorProperty TemperatureCompensateOnStartSP;
168 
169         // Temperature Coefficient
170         INumber TemperatureCoeffN[5];
171         INumberVectorProperty TemperatureCoeffNP;
172 
173         // Temperature Coefficient Mode
174         ISwitch TemperatureCompensateModeS[5];
175         ISwitchVectorProperty TemperatureCompensateModeSP;
176 
177         // Enable/Disable backlash
178         //    ISwitch FocuserBacklashCompensationS[2];
179         //    ISwitchVectorProperty FocuserFocusBacklashSP;
180 
181         // Backlash Value
182         //    INumber FocusBacklashN[1];
183         //    INumberVectorProperty FocusBacklashNP;
184 
185         // Home On Start
186         ISwitch FocuserHomeOnStartS[2];
187         ISwitchVectorProperty FocuserHomeOnStartSP;
188 
189         // Go to home/center
190         ISwitch FocuserGotoS[2];
191         ISwitchVectorProperty FocuserGotoSP;
192 
193         // Status indicators
194         ILight FocuserStatusL[8];
195         ILightVectorProperty FocuserStatusLP;
196 
197         bool isFocuserAbsolute;
198         bool isFocuserHoming;
199 
200         ////////////////////////////////////////////////////////////
201         // Rotator Functions
202         ///////////////////////////////////////////////////////////
203 
204         // Get functions
205         bool getRotatorConfig();
206         bool getRotatorStatus();
207 
208         IPState MoveAbsRotatorTicks(uint32_t targetTicks);
209         IPState MoveAbsRotatorAngle(double angle);
210         bool reverseRotator(bool enable);
211 
212         ////////////////////////////////////////////////////////////
213         // Rotator Properties
214         ///////////////////////////////////////////////////////////
215 
216         // Status
217         ILight RotatorStatusL[8];
218         ILightVectorProperty RotatorStatusLP;
219 
220         // Rotator Steps
221         INumber RotatorAbsPosN[1];
222         INumberVectorProperty RotatorAbsPosNP;
223 
224 #if 0
225         // Reverse Direction
226         ISwitch RotatorReverseS[2];
227         ISwitchVectorProperty RotatorReverseSP;
228 
229 
230 
231         // Rotator Degrees or PA (Position Angle)
232         INumber RotatorAbsAngleN[1];
233         INumberVectorProperty RotatorAbsAngleNP;
234 
235         // Abort
236         ISwitch AbortRotatorS[1];
237         ISwitchVectorProperty AbortRotatorSP;
238 
239         // Go to home/center
240         ISwitch RotatorGotoS[2];
241         ISwitchVectorProperty RotatorGotoSP;
242 #endif
243 
244         // Home On Start
245         ISwitch RotatorHomeOnStartS[2];
246         ISwitchVectorProperty RotatorHomeOnStartSP;
247 
248         bool isRotatorHoming;
249 
250         ////////////////////////////////////////////////////////////
251         // Hub Functions
252         ///////////////////////////////////////////////////////////
253 
254         // Led level
255         bool setLedLevel(int level);
256 
257         // Device Nickname
258         bool setNickname(DeviceType type, const char *nickname);
259 
260         // Misc functions
261         bool resetFactory();
262         float calcTimeLeft(timeval, float);
263 
264         ////////////////////////////////////////////////////////////
265         // Hub Properties
266         ///////////////////////////////////////////////////////////
267 
268         // Reset to Factory setting
269         ISwitch ResetS[1];
270         ISwitchVectorProperty ResetSP;
271 
272         // Focus and rotator name configure in the HUB
273         IText HFocusNameT[2] {};
274         ITextVectorProperty HFocusNameTP;
275 
276         // Led Intensity Value
277         INumber LedN[1];
278         INumberVectorProperty LedNP;
279 
280         uint32_t DBG_FOCUS;
281 };
282