Instrument Neutral Distributed Interface INDI  1.9.5
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
57 
59 
61 
62  //typedef enum {TEMP_COMPENSATE_ENABLE, TEMP_COMPENSATE_DISABLE } TemperatureCompensate;
64 
65  const char * getDefaultName() override;
66  virtual bool initProperties() override;
67  virtual bool updateProperties() override;
68  virtual bool ISNewNumber(const char * dev, const char * name, double values[], char * names[], int n) override;
69  virtual bool ISNewSwitch(const char * dev, const char * name, ISState * states, char * names[], int n) override;
70 
71  static void timedMoveHelper(void * context);
72 
73  protected:
78  virtual bool Handshake() override;
79 
87  virtual IPState MoveFocuser(FocusDirection dir, int speed, uint16_t duration) override;
88 
94  virtual IPState MoveAbsFocuser(uint32_t targetTicks) override;
95 
102  virtual IPState MoveRelFocuser(FocusDirection dir, uint32_t ticks) override;
103 
109  virtual bool SyncFocuser(uint32_t ticks) override;
110  virtual bool SetFocuserMaxPosition(uint32_t ticks) override;
111  virtual bool SetFocuserSpeed(int speed) override;
112  virtual bool ReverseFocuser(bool enabled) override;
113  virtual bool AbortFocuser() override;
114  virtual void TimerHit() override;
115  virtual bool saveConfigItems(FILE * fp) override;
116 
117  private:
118  bool Ack();
126  bool sendCommand(const char * cmd, char * res = nullptr);
127 
128  // Get initial focuser parameter when we first connect
129  void getStartupValues();
130 
131  // Read and update Step Mode
132  bool readStepMode();
133 
134  // Read and update Temperature
135  bool readTemperature();
136 
137  // Read and Update T.Compensate
138  bool readTempCompensateEnable();
139 
140  //Read and Update the Temperature Coefficient
141  bool readTemperatureCoefficient();
142 
143  // Read and update Position
144  bool readPosition();
145 
146  // Read and update speed
147  bool readSpeed();
148 
149  //Read and Update Maximum step position
150  bool readMaxPos();
151 
152  // Are we moving?
153  bool isMoving();
154 
155  bool readCoilPowerState();
156 
157  bool readReverseDirection();
158 
159  bool readDisplayVisible();
160 
161  bool readBacklashInEnabled();
162  bool readBacklashOutEnabled();
163 
164  bool readBacklashInSteps();
165  bool readBacklashOutSteps();
166 
167  bool setBacklashInSteps(int16_t steps);
168  bool setBacklashOutSteps(int16_t steps);
169 
170  bool setBacklashInEnabled(bool enabled);
171  bool setBacklashOutEnabled(bool enabled);
172 
173  bool MoveFocuser(uint32_t position);
174 
175  bool setStepMode(FocusStepMode mode);
176 
177  bool setSpeed(uint16_t speed);
178 
179  bool setDisplayVisible(DisplayMode enable);
180 
181  bool setGotoHome();
182 
183  bool setCoilPowerState(CoilPower enable);
184 
185  bool setTemperatureCelsius();
186 
187  bool setTemperatureCalibration(double calibration);
188 
189  bool setTemperatureCoefficient(double coefficient);
190 
191  bool setTemperatureCompensation(bool enable);
192 
193  void timedMoveCallback();
194 
195  double targetPos { 0 }, lastPos { 0 }, lastTemperature { 0 };
196 
197  int msleep(long milliseconds);
198 
199  void clearbufferonerror();
200 
201  // Read Only Temperature Reporting
202  INumber TemperatureN[1];
203  INumberVectorProperty TemperatureNP;
204 
205  // Full/Half...256th Step modes
206  ISwitch StepModeS[9];
207  ISwitchVectorProperty StepModeSP;
208 
209  // Backlash In settings
210  INumber BacklashInStepsN[1];
211  INumberVectorProperty BacklashInStepsNP;
212 
213  // Backlash Out Setting
214  INumber BacklashOutStepsN[1];
215  INumberVectorProperty BacklashOutStepsNP;
216 
217  // Temperature Settings
218  INumber TemperatureSettingN[1];
219  INumberVectorProperty TemperatureSettingNP;
220 
221  // Temperature Compensation Enable/Disable
222  ISwitch TemperatureCompensateS[2];
223  ISwitchVectorProperty TemperatureCompensateSP;
224 
225  //Display On Off
226  ISwitch DisplayS[2];
227  ISwitchVectorProperty DisplaySP;
228 
229  //Goto Home Position
230  ISwitch GotoHomeS[1];
231  ISwitchVectorProperty GotoHomeSP;
232 
233  //CoilPower On Off
234  ISwitch CoilPowerS[2];
235  ISwitchVectorProperty CoilPowerSP;
236 
237  ISwitch BacklashInS[2];
238  ISwitchVectorProperty BacklashInSP;
239 
240  //Backlash Out Enable
241  ISwitch BacklashOutS[2];
242  ISwitchVectorProperty BacklashOutSP;
243 
244  //Focus Speed
245  ISwitch FocusSpeedS[3];
246  ISwitchVectorProperty FocusSpeedSP;
247 
248  // MyFocuserPro2 Buffer
249  static const uint8_t ML_RES { 32 };
250 
251  // MyFocuserPro2 Delimeter
252  static const char ML_DEL { '#' };
253 
254  // mutex controls access to serial port
255  pthread_mutex_t cmdlock;
256 
257  // MyFocuserPro2 Timeouts
258  static const uint8_t MYFOCUSERPRO2_SERIAL_TIMEOUT { 5 };
259  static const uint8_t MYFOCUSERPRO2_TCPIP_TIMEOUT { 10 };
260  static const long MYFOCUSERPRO2_SMALL_DELAY { 50 }; // 50ms delay from send command to read response
261  static const long MYFOCUSERPRO2_RECOVER_DELAY { 200 };
262 
263  // update the temperature one every 5 seconds.
264  static constexpr const uint8_t GET_TEMPERATURE_FREQ{ 10 };
265  uint16_t Temperature_Counter { 0 };
266 
267  // Update position every second
268  static constexpr const uint8_t GET_POSITION_FREQ{ 1 };
269  uint16_t Position_Counter { 0 };
270 
271  static constexpr const char *SETTINGS_TAB {"Settings"};
272 };
MyFocuserPro2::MoveAbsFocuser
virtual IPState MoveAbsFocuser(uint32_t targetTicks) override
MoveAbsFocuser Move to an absolute target position.
Definition: myfocuserpro2.cpp:1258
MyFocuserPro2::SIXTYFOUR_STEP
@ SIXTYFOUR_STEP
Definition: myfocuserpro2.h:56
MyFocuserPro2::HALF_STEP
@ HALF_STEP
Definition: myfocuserpro2.h:55
cmd
__u8 cmd[4]
Definition: pwc-ioctl.h:4
MyFocuserPro2::DISPLAY_ON
@ DISPLAY_ON
Definition: myfocuserpro2.h:60
IPState
IPState
Property state.
Definition: indiapi.h:158
MyFocuserPro2::TemperatureCompensate
TemperatureCompensate
Definition: myfocuserpro2.h:63
ISwitch
One switch descriptor.
MyFocuserPro2::timedMoveHelper
static void timedMoveHelper(void *context)
Definition: myfocuserpro2.cpp:1241
MyFocuserPro2
Definition: myfocuserpro2.h:48
MyFocuserPro2::EIGHTH_STEP
@ EIGHTH_STEP
Definition: myfocuserpro2.h:55
MyFocuserPro2::getDefaultName
const char * getDefaultName() override
Definition: myfocuserpro2.cpp:215
INumber
One number descriptor.
MyFocuserPro2::ISNewNumber
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
Definition: myfocuserpro2.cpp:1121
MyFocuserPro2::QUARTER_STEP
@ QUARTER_STEP
Definition: myfocuserpro2.h:55
MyFocuserPro2::TEMP_COMPENSATE_ENABLE
@ TEMP_COMPENSATE_ENABLE
Definition: myfocuserpro2.h:63
MyFocuserPro2::TWOHUNDREDFIFTYSIX_STEP
@ TWOHUNDREDFIFTYSIX_STEP
Definition: myfocuserpro2.h:56
MyFocuserPro2::Handshake
virtual bool Handshake() override
Handshake Try to communicate with Focuser and see if there is a valid response.
Definition: myfocuserpro2.cpp:199
MyFocuserPro2::MyFocuserPro2
MyFocuserPro2()
Definition: myfocuserpro2.cpp:37
indifocuser.h
MyFocuserPro2::saveConfigItems
virtual bool saveConfigItems(FILE *fp) override
saveConfigItems Saves the Device Port and Focuser Presets in the configuration file
Definition: myfocuserpro2.cpp:1368
_INumberVectorProperty
Number vector property descriptor.
Definition: indiapi.h:317
MyFocuserPro2::COIL_POWER_OFF
@ COIL_POWER_OFF
Definition: myfocuserpro2.h:58
MyFocuserPro2::SetFocuserSpeed
virtual bool SetFocuserSpeed(int speed) override
SetFocuserSpeed Set Focuser speed.
Definition: myfocuserpro2.cpp:1196
MyFocuserPro2::~MyFocuserPro2
virtual ~MyFocuserPro2() override=default
MyFocuserPro2::ONEHUNDREDTWENTYEIGHT_STEP
@ ONEHUNDREDTWENTYEIGHT_STEP
Definition: myfocuserpro2.h:56
MyFocuserPro2::TEMP_COMPENSATE_DISABLE
@ TEMP_COMPENSATE_DISABLE
Definition: myfocuserpro2.h:63
MyFocuserPro2::MoveFocuser
virtual IPState MoveFocuser(FocusDirection dir, int speed, uint16_t duration) override
MoveFocuser Move focuser in a specific direction and speed for period of time.
Definition: myfocuserpro2.cpp:1216
MyFocuserPro2::COIL_POWER_ON
@ COIL_POWER_ON
Definition: myfocuserpro2.h:58
MyFocuserPro2::FULL_STEP
@ FULL_STEP
Definition: myfocuserpro2.h:55
MyFocuserPro2::SIXTEENTH_STEP
@ SIXTEENTH_STEP
Definition: myfocuserpro2.h:55
MyFocuserPro2::FocusStepMode
FocusStepMode
Definition: myfocuserpro2.h:55
name
const char * name
Definition: indiserver.c:116
MyFocuserPro2::AbortFocuser
virtual bool AbortFocuser() override
AbortFocuser all focus motion.
Definition: myfocuserpro2.cpp:1363
MyFocuserPro2::DisplayMode
DisplayMode
Definition: myfocuserpro2.h:60
MyFocuserPro2::updateProperties
virtual bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
Definition: myfocuserpro2.cpp:159
MyFocuserPro2::ReverseFocuser
virtual bool ReverseFocuser(bool enabled) override
ReverseFocuser Reverse focuser motion direction.
Definition: myfocuserpro2.cpp:841
INDI::FocuserInterface::FocusDirection
FocusDirection
Definition: indifocuserinterface.h:66
MyFocuserPro2::ISNewSwitch
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override
Process the client newSwitch command.
Definition: myfocuserpro2.cpp:924
MyFocuserPro2::SyncFocuser
virtual bool SyncFocuser(uint32_t ticks) override
SyncFocuser Set the supplied position as the current focuser position.
Definition: myfocuserpro2.cpp:787
MyFocuserPro2::MoveRelFocuser
virtual IPState MoveRelFocuser(FocusDirection dir, uint32_t ticks) override
MoveRelFocuser Move focuser for a relative amount of ticks in a specific direction.
Definition: myfocuserpro2.cpp:1269
ISState
ISState
Switch state.
Definition: indiapi.h:148
MyFocuserPro2::initProperties
virtual bool initProperties() override
Initilize properties initial state and value. The child class must implement this function.
Definition: myfocuserpro2.cpp:49
MyFocuserPro2::TimerHit
virtual void TimerHit() override
Callback function to be called once SetTimer duration elapses.
Definition: myfocuserpro2.cpp:1296
INDI::Focuser
Definition: indifocuser.h:43
MyFocuserPro2::THIRTYSECOND_STEP
@ THIRTYSECOND_STEP
Definition: myfocuserpro2.h:56
MyFocuserPro2::CoilPower
CoilPower
Definition: myfocuserpro2.h:58
MyFocuserPro2::DISPLAY_OFF
@ DISPLAY_OFF
Definition: myfocuserpro2.h:60
_ISwitchVectorProperty
Switch vector property descriptor.
Definition: indiapi.h:365
MyFocuserPro2::SetFocuserMaxPosition
virtual bool SetFocuserMaxPosition(uint32_t ticks) override
SetFocuserMaxPosition Update focuser maximum position. It only updates the PresetNP property limits.
Definition: myfocuserpro2.cpp:1202