Instrument Neutral Distributed Interface INDI  2.0.2
dome_simulator.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  Dome Simulator
3  Copyright(c) 2014 Jasem Mutlaq. All rights reserved.
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License version 2 as published by the Free Software Foundation.
8  .
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13  .
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 *******************************************************************************/
19 #include "dome_simulator.h"
20 
21 #include "indicom.h"
22 
23 #include <cmath>
24 #include <memory>
25 #include <unistd.h>
26 
27 // We declare an auto pointer to domeSim.
28 static std::unique_ptr<DomeSim> domeSim(new DomeSim());
29 
30 #define DOME_SPEED 10.0 /* 10 degrees per second, constant */
31 #define SHUTTER_TIMER 5.0 /* Shutter closes/open in 5 seconds */
32 
34 {
35  targetAz = 0;
36  shutterTimer = 0;
37  prev_az = 0;
38  prev_alt = 0;
39 
41 }
42 
43 /************************************************************************************
44  *
45 * ***********************************************************************************/
47 {
49 
51 
53 
54  return true;
55 }
56 
57 bool DomeSim::SetupParms()
58 {
59  targetAz = 0;
60  shutterTimer = SHUTTER_TIMER;
61 
62  DomeAbsPosN[0].value = 0;
63 
64  DomeParamN[0].value = 5;
65 
66  IDSetNumber(&DomeAbsPosNP, nullptr);
67  IDSetNumber(&DomeParamNP, nullptr);
68 
69  if (InitPark())
70  {
71  // If loading parking data is successful, we just set the default parking values.
73  }
74  else
75  {
76  // Otherwise, we set all parking data to default in case no parking data is found.
77  SetAxis1Park(90);
79  }
80 
81  return true;
82 }
83 
85 {
86  return "Dome Simulator";
87 }
88 
90 {
92 
93  if (isConnected())
94  {
95  SetupParms();
96  }
97 
98  return true;
99 }
100 
102 {
103  SetTimer(1000); // start the timer
104  return true;
105 }
106 
108 {
109  return true;
110 }
111 
113 {
114  int nexttimer = 1000;
115 
116  if (!isConnected())
117  return; // No need to reset timer if we are not connected anymore
118 
119  if (DomeAbsPosNP.s == IPS_BUSY)
120  {
121  if (targetAz > DomeAbsPosN[0].value)
122  {
123  DomeAbsPosN[0].value += DOME_SPEED;
124  }
125  else if (targetAz < DomeAbsPosN[0].value)
126  {
127  DomeAbsPosN[0].value -= DOME_SPEED;
128  }
129 
130  DomeAbsPosN[0].value = range360(DomeAbsPosN[0].value);
131 
132  if (fabs(targetAz - DomeAbsPosN[0].value) <= DOME_SPEED)
133  {
134  DomeAbsPosN[0].value = targetAz;
135  LOG_INFO("Dome reached requested azimuth angle.");
136 
137  if (getDomeState() == DOME_PARKING)
138  SetParked(true);
139  else if (getDomeState() == DOME_UNPARKING)
140  SetParked(false);
141  else
143  }
144 
145  IDSetNumber(&DomeAbsPosNP, nullptr);
146  }
147 
148  if (DomeShutterSP.s == IPS_BUSY)
149  {
150  if (shutterTimer-- <= 0)
151  {
152  shutterTimer = 0;
154  LOGF_INFO("Shutter is %s.", (DomeShutterS[0].s == ISS_ON ? "open" : "closed"));
155  IDSetSwitch(&DomeShutterSP, nullptr);
156 
157  if (getDomeState() == DOME_UNPARKING)
158  SetParked(false);
159  }
160  }
161  SetTimer(nexttimer);
162 }
163 
165 {
166  if (operation == MOTION_START)
167  {
168  targetAz = (dir == DOME_CW) ? 1e6 : -1e6;
170  }
171  else
172  {
173  targetAz = 0;
175  }
176 
177  IDSetNumber(&DomeAbsPosNP, nullptr);
178  return ((operation == MOTION_START) ? IPS_BUSY : IPS_OK);
179 }
180 
182 {
183  targetAz = az;
184 
185  // Requested position is within one cycle, let's declare it done
186  if (fabs(az - DomeAbsPosN[0].value) < DOME_SPEED)
187  return IPS_OK;
188 
189  // It will take a few cycles to reach final position
190  return IPS_BUSY;
191 }
192 
193 IPState DomeSim::MoveRel(double azDiff)
194 {
195  targetAz = DomeAbsPosN[0].value + azDiff;
196  ;
197 
198  if (targetAz < DomeAbsPosN[0].min)
199  targetAz += DomeAbsPosN[0].max;
200  if (targetAz > DomeAbsPosN[0].max)
201  targetAz -= DomeAbsPosN[0].max;
202 
203  // Requested position is within one cycle, let's declare it done
204  if (fabs(targetAz - DomeAbsPosN[0].value) < DOME_SPEED)
205  return IPS_OK;
206 
207  // It will take a few cycles to reach final position
208  return IPS_BUSY;
209 }
210 
212 {
213  targetAz = DomeParamN[0].value;
214  Dome::ControlShutter(SHUTTER_CLOSE);
215  Dome::MoveAbs(GetAxis1Park());
216 
217  return IPS_BUSY;
218 }
219 
221 {
222  return Dome::ControlShutter(SHUTTER_OPEN);
223 }
224 
226 {
227  INDI_UNUSED(operation);
228  shutterTimer = SHUTTER_TIMER;
229  return IPS_BUSY;
230 }
231 
233 {
234  // If we abort while in the middle of opening/closing shutter, alert.
235  if (DomeShutterSP.s == IPS_BUSY)
236  {
238  IDSetSwitch(&DomeShutterSP, "Shutter operation aborted. Status: unknown.");
239  return false;
240  }
241 
242  return true;
243 }
244 
246 {
247  SetAxis1Park(DomeAbsPosN[0].value);
248  return true;
249 }
250 
252 {
253  // By default set position to 90
254  SetAxis1Park(90);
255  return true;
256 }
The DomeSim class provides an absolute position dome that supports parking, unparking,...
virtual bool Abort() override
Abort all dome motion.
virtual bool SetCurrentPark() override
SetCurrentPark Set current coordinates/encoders value as the desired parking position.
virtual IPState Park() override
Goto Park Position. The park position is an absolute azimuth value.
const char * getDefaultName() override
virtual bool SetDefaultPark() override
SetDefaultPark Set default coordinates/encoders value as the desired parking position.
bool Disconnect() override
Disconnect from device.
void TimerHit() override
Callback function to be called once SetTimer duration elapses.
virtual IPState UnPark() override
UnPark dome. The action of the Unpark command is dome specific, but it may include opening the shutte...
virtual bool initProperties() override
Initilize properties initial state and value. The child class must implement this function.
bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
virtual IPState MoveAbs(double az) override
Move the Dome to an absolute azimuth.
virtual IPState MoveRel(double azDiff) override
Move the Dome to an relative position.
bool Connect() override
Connect to the device. INDI::DefaultDevice implementation connects to appropriate connection interfac...
virtual IPState ControlShutter(ShutterOperation operation) override
Open or Close shutter.
virtual IPState Move(DomeDirection dir, DomeMotionCommand operation) override
Move the Dome in a particular direction.
bool isConnected() const
Definition: basedevice.cpp:520
void addAuxControls()
Add Debug, Simulation, and Configuration options to the driver.
int SetTimer(uint32_t ms)
Set a timer to call the function TimerHit after ms milliseconds.
ISwitch DomeShutterS[2]
Definition: indidome.h:549
double prev_alt
Definition: indidome.h:614
@ DOME_CAN_PARK
Definition: indidome.h:159
@ DOME_CAN_ABS_MOVE
Definition: indidome.h:157
@ DOME_HAS_SHUTTER
Definition: indidome.h:161
@ DOME_CAN_REL_MOVE
Definition: indidome.h:158
@ DOME_CAN_ABORT
Definition: indidome.h:156
void SetParked(bool isparked)
SetParked Change the mount parking status. The data park file (stored in ~/.indi/ParkData....
Definition: indidome.cpp:1633
void SetDomeCapability(uint32_t cap)
SetDomeCapability set the dome capabilities. All capabilities must be initialized.
Definition: indidome.cpp:1561
@ DOME_UNPARKING
Definition: indidome.h:135
@ DOME_PARKING
Definition: indidome.h:134
@ DOME_SYNCED
Definition: indidome.h:133
void SetAxis1Park(double value)
SetRAPark Set current AZ parking position. The data park file (stored in ~/.indi/ParkData....
Definition: indidome.cpp:1861
INumber DomeAbsPosN[1]
Definition: indidome.h:534
INumberVectorProperty DomeParamNP
Definition: indidome.h:542
void SetAxis1ParkDefault(double steps)
SetAxis1Park Set default AZ parking position.
Definition: indidome.cpp:1868
ShutterOperation
Shutter operation command.
Definition: indidome.h:114
@ SHUTTER_CLOSE
Definition: indidome.h:116
@ SHUTTER_OPEN
Definition: indidome.h:115
DomeMotionCommand
Definition: indidome.h:97
@ MOTION_START
Definition: indidome.h:98
virtual bool initProperties() override
Initilize properties initial state and value. The child class must implement this function.
Definition: indidome.cpp:93
double prev_az
Definition: indidome.h:614
INumberVectorProperty DomeAbsPosNP
Definition: indidome.h:533
double GetAxis1Park()
Definition: indidome.cpp:1851
ISwitchVectorProperty DomeShutterSP
Definition: indidome.h:548
virtual bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
Definition: indidome.cpp:279
INumber DomeParamN[1]
Definition: indidome.h:543
void setDomeState(const DomeState &value)
Definition: indidome.cpp:1156
bool InitPark()
InitPark Loads parking data (stored in ~/.indi/ParkData.xml) that contains parking status and parking...
Definition: indidome.cpp:1644
void SetParkDataType(DomeParkData type)
setParkDataType Sets the type of parking data stored in the park data file and presented to the user.
Definition: indidome.cpp:1587
DomeState getDomeState() const
Definition: indidome.h:285
#define SHUTTER_TIMER
#define DOME_SPEED
double max(void)
double min(void)
@ ISS_ON
Definition: indiapi.h:152
IPState
Property state.
Definition: indiapi.h:160
@ IPS_BUSY
Definition: indiapi.h:163
@ IPS_ALERT
Definition: indiapi.h:164
@ IPS_IDLE
Definition: indiapi.h:161
@ IPS_OK
Definition: indiapi.h:162
double range360(double r)
range360 Limits an angle to be between 0-360 degrees.
Definition: indicom.c:1245
Implementations for common driver routines.
#define INDI_UNUSED(x)
Definition: indidevapi.h:131
void IDSetNumber(const INumberVectorProperty *nvp, const char *fmt,...)
Definition: indidriver.c:1211
void IDSetSwitch(const ISwitchVectorProperty *svp, const char *fmt,...)
Definition: indidriver.c:1231
#define LOGF_INFO(fmt,...)
Definition: indilogger.h:82
#define LOG_INFO(txt)
Definition: indilogger.h:74