Instrument Neutral Distributed Interface INDI  2.0.2
myfocuserpro2.h
Go to the documentation of this file.
1 /*
2  ORIGINAL
3  MyFocuserPro2 Focuser
4  Copyright (C) 2019 Alan Townshend
5 
6  Based on Moonlite Focuser
7  Copyright (C) 2013-2019 Jasem Mutlaq (mutlaqja@ikarustech.com)
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 
23 */
24 
25 #pragma once
26 
27 #include "indifocuser.h"
28 
29 #include <time.h> // for nsleep() R Brown June 2021
30 #include <errno.h> // for nsleep() R Brown June 2021
31 
32 #include <chrono>
33 
34 #define STEPMODE_FULL 1
35 #define STEPMODE_HALF 2
36 #define STEPMODE_QUARTER 4
37 #define STEPMODE_EIGHTH 8
38 #define STEPMODE_SIXTEENTH 16
39 #define STEPMODE_THIRTYSECOND 32
40 #define STEPMODE_SIXTYFOUR 64
41 #define STEPMODE_ONEHUNDREDTWENTYEIGHT 128
42 #define STEPMODE_TWOHUNDREDFIFTYSIX 256
43 
44 #define CDRIVER_VERSION_MAJOR 1
45 #define CDRIVER_VERSION_MINOR 0
46 
47 
49 {
50  public:
51  MyFocuserPro2();
52  virtual ~MyFocuserPro2() override = default;
53 
54  // add step modes for TMC driver R Brown June 2021
58 
60 
62 
63  //typedef enum {TEMP_COMPENSATE_ENABLE, TEMP_COMPENSATE_DISABLE } TemperatureCompensate;
65 
66  const char * getDefaultName() override;
67  virtual bool initProperties() override;
68  virtual bool updateProperties() override;
69  virtual bool ISNewNumber(const char * dev, const char * name, double values[], char * names[], int n) override;
70  virtual bool ISNewSwitch(const char * dev, const char * name, ISState * states, char * names[], int n) override;
71 
72  static void timedMoveHelper(void * context);
73 
74  protected:
79  virtual bool Handshake() override;
80 
88  virtual IPState MoveFocuser(FocusDirection dir, int speed, uint16_t duration) override;
89 
95  virtual IPState MoveAbsFocuser(uint32_t targetTicks) override;
96 
103  virtual IPState MoveRelFocuser(FocusDirection dir, uint32_t ticks) override;
104 
110  virtual bool SyncFocuser(uint32_t ticks) override;
111  virtual bool SetFocuserMaxPosition(uint32_t ticks) override;
112  virtual bool SetFocuserSpeed(int speed) override;
113  virtual bool ReverseFocuser(bool enabled) override;
114  virtual bool AbortFocuser() override;
115  virtual void TimerHit() override;
116  virtual bool saveConfigItems(FILE * fp) override;
117 
118  private:
119  bool Ack();
127  bool sendCommand(const char * cmd, char * res = nullptr);
128 
129  // Get initial focuser parameter when we first connect
130  void getStartupValues();
131 
132  // Read and update Step Mode
133  bool readStepMode();
134 
135  // Read and update Temperature
136  bool readTemperature();
137 
138  // Read and Update T.Compensate
139  bool readTempCompensateEnable();
140 
141  //Read and Update the Temperature Coefficient
142  bool readTemperatureCoefficient();
143 
144  // Read and update Position
145  bool readPosition();
146 
147  // Read and update speed
148  bool readSpeed();
149 
150  //Read and Update Maximum step position
151  bool readMaxPos();
152 
153  // Are we moving?
154  bool isMoving();
155 
156  bool readCoilPowerState();
157 
158  bool readReverseDirection();
159 
160  bool readDisplayVisible();
161 
162  bool readBacklashInEnabled();
163  bool readBacklashOutEnabled();
164 
165  bool readBacklashInSteps();
166  bool readBacklashOutSteps();
167 
168  bool setBacklashInSteps(int16_t steps);
169  bool setBacklashOutSteps(int16_t steps);
170 
171  bool setBacklashInEnabled(bool enabled);
172  bool setBacklashOutEnabled(bool enabled);
173 
174  bool MoveFocuser(uint32_t position);
175 
176  bool setStepMode(FocusStepMode mode);
177 
178  bool setSpeed(uint16_t speed);
179 
180  bool setDisplayVisible(DisplayMode enable);
181 
182  bool setGotoHome();
183 
184  bool setCoilPowerState(CoilPower enable);
185 
186  bool setTemperatureCelsius();
187 
188  bool setTemperatureCalibration(double calibration);
189 
190  bool setTemperatureCoefficient(double coefficient);
191 
192  bool setTemperatureCompensation(bool enable);
193 
194  void timedMoveCallback();
195 
196  double targetPos { 0 }, lastPos { 0 }, lastTemperature { 0 };
197 
198  int msleep(long milliseconds);
199 
200  void clearbufferonerror();
201 
202  // Read Only Temperature Reporting
203  INumber TemperatureN[1];
204  INumberVectorProperty TemperatureNP;
205 
206  // Full/Half...256th Step modes
207  ISwitch StepModeS[9];
208  ISwitchVectorProperty StepModeSP;
209 
210  // Backlash In settings
211  INumber BacklashInStepsN[1];
212  INumberVectorProperty BacklashInStepsNP;
213 
214  // Backlash Out Setting
215  INumber BacklashOutStepsN[1];
216  INumberVectorProperty BacklashOutStepsNP;
217 
218  // Temperature Settings
219  INumber TemperatureSettingN[1];
220  INumberVectorProperty TemperatureSettingNP;
221 
222  // Temperature Compensation Enable/Disable
223  ISwitch TemperatureCompensateS[2];
224  ISwitchVectorProperty TemperatureCompensateSP;
225 
226  //Display On Off
227  ISwitch DisplayS[2];
228  ISwitchVectorProperty DisplaySP;
229 
230  //Goto Home Position
231  ISwitch GotoHomeS[1];
232  ISwitchVectorProperty GotoHomeSP;
233 
234  //CoilPower On Off
235  ISwitch CoilPowerS[2];
236  ISwitchVectorProperty CoilPowerSP;
237 
238  ISwitch BacklashInS[2];
239  ISwitchVectorProperty BacklashInSP;
240 
241  //Backlash Out Enable
242  ISwitch BacklashOutS[2];
243  ISwitchVectorProperty BacklashOutSP;
244 
245  //Focus Speed
246  ISwitch FocusSpeedS[3];
247  ISwitchVectorProperty FocusSpeedSP;
248 
249  // MyFocuserPro2 Buffer
250  static const uint8_t ML_RES { 32 };
251 
252  // MyFocuserPro2 Delimeter
253  static const char ML_DEL { '#' };
254 
255  // mutex controls access to serial port
256  pthread_mutex_t cmdlock;
257 
258  // MyFocuserPro2 Timeouts
259  static const uint8_t MYFOCUSERPRO2_SERIAL_TIMEOUT { 5 };
260  static const uint8_t MYFOCUSERPRO2_TCPIP_TIMEOUT { 10 };
261  static const long MYFOCUSERPRO2_SMALL_DELAY { 50 }; // 50ms delay from send command to read response
262  static const long MYFOCUSERPRO2_RECOVER_DELAY { 200 };
263 
264  // update the temperature one every 5 seconds.
265  static constexpr const uint8_t GET_TEMPERATURE_FREQ{ 10 };
266  uint16_t Temperature_Counter { 0 };
267 
268  // Update position every second
269  static constexpr const uint8_t GET_POSITION_FREQ{ 1 };
270  uint16_t Position_Counter { 0 };
271 
272  static constexpr const char *SETTINGS_TAB {"Settings"};
273 };
virtual bool saveConfigItems(FILE *fp) override
saveConfigItems Saves the Device Port and Focuser Presets in the configuration file
virtual ~MyFocuserPro2() override=default
virtual bool AbortFocuser() override
AbortFocuser all focus motion.
virtual bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
virtual IPState MoveFocuser(FocusDirection dir, int speed, uint16_t duration) override
MoveFocuser Move focuser in a specific direction and speed for period of time.
virtual bool ReverseFocuser(bool enabled) override
ReverseFocuser Reverse focuser motion direction.
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override
Process the client newSwitch command.
virtual bool SyncFocuser(uint32_t ticks) override
SyncFocuser Set the supplied position as the current focuser position.
virtual void TimerHit() override
Callback function to be called once SetTimer duration elapses.
virtual bool SetFocuserSpeed(int speed) override
SetFocuserSpeed Set Focuser speed.
virtual bool SetFocuserMaxPosition(uint32_t ticks) override
SetFocuserMaxPosition Update focuser maximum position. It only updates the PresetNP property limits.
virtual IPState MoveRelFocuser(FocusDirection dir, uint32_t ticks) override
MoveRelFocuser Move focuser for a relative amount of ticks in a specific direction.
virtual bool Handshake() override
Handshake Try to communicate with Focuser and see if there is a valid response.
static void timedMoveHelper(void *context)
virtual bool initProperties() override
Initilize properties initial state and value. The child class must implement this function.
@ ONEHUNDREDTWENTYEIGHT_STEP
Definition: myfocuserpro2.h:56
virtual IPState MoveAbsFocuser(uint32_t targetTicks) override
MoveAbsFocuser Move to an absolute target position.
const char * getDefaultName() override
ISState
Switch state.
Definition: indiapi.h:150
IPState
Property state.
Definition: indiapi.h:160
__u8 cmd[4]
Definition: pwc-ioctl.h:2
One number descriptor.
One switch descriptor.
Number vector property descriptor.
Definition: indiapi.h:319
Switch vector property descriptor.
Definition: indiapi.h:367