Instrument Neutral Distributed Interface INDI 2.2.5
ccd_simulator.h
Go to the documentation of this file.
1/*******************************************************************************
2 Copyright(c) 2017 Jasem Mutlaq. All rights reserved.
3 Copyright(c) 2010 Gerry Rozema. All rights reserved.
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
7 .
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
12 .
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
17*******************************************************************************/
18
19#pragma once
20
21#include <atomic>
22#include <chrono>
23#include <deque>
24#include <mutex>
25#include <random>
26#include <vector>
27#include <map>
28#include <string>
29
30#include <indiccd.h>
31#include "sky_renderer.h"
32#include "simulator_clock.h"
33#include "indifilterinterface.h"
34
55{
56 public:
57
58 enum
59 {
77 };
78
79 CCDSim();
80 virtual ~CCDSim() override = default;
81
82 const char *getDefaultName() override;
83
84 bool initProperties() override;
85 bool updateProperties() override;
86
87 void ISGetProperties(const char *dev) override;
88
89 virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override;
90 virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override;
91 virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override;
92 virtual bool ISSnoopDevice(XMLEle *root) override;
93
94 static void *streamVideoHelper(void *context);
95 void *streamVideo();
96
97 protected:
98
99 bool Connect() override;
100 bool Disconnect() override;
101
102 bool StartExposure(float duration) override;
103 bool StartGuideExposure(float) override;
104
105 bool AbortExposure() override;
106 bool AbortGuideExposure() override;
107
108 void TimerHit() override;
109
110 int DrawCcdFrame(INDI::CCDChip *targetChip);
111
112 virtual IPState GuideNorth(uint32_t) override;
113 virtual IPState GuideSouth(uint32_t) override;
114 virtual IPState GuideEast(uint32_t) override;
115 virtual IPState GuideWest(uint32_t) override;
116
117 virtual bool saveConfigItems(FILE *fp) override;
118 virtual void addFITSKeywords(INDI::CCDChip *targetChip, std::vector<INDI::FITSRecord> &fitsKeyword) override;
119 virtual void activeDevicesUpdated() override;
120 virtual int SetTemperature(double temperature) override;
121 virtual bool UpdateCCDFrame(int x, int y, int w, int h) override;
122 virtual bool UpdateCCDBin(int hor, int ver) override;
123 virtual bool UpdateGuiderBin(int hor, int ver) override;
124
125 virtual bool SetCaptureFormat(uint8_t index) override;
126
127 virtual bool StartStreaming() override;
128 virtual bool StopStreaming() override;
129
130 // Filter
131 bool SelectFilter(int) override;
132 int QueryFilter() override;
133
134 protected:
135
136 bool watchDirectory();
137 bool loadNextImage();
138 bool loadNextFITSImage(const std::string &filename);
139#ifdef HAVE_XISF
140 bool loadNextXISFImage(const std::string &filename);
141#endif
142 bool setupParameters();
143
144 // Updates the planet sprite from xplanet when it is missing, stale, or its parameters changed.
145 // Caller must hold m_PlanetMutex.
146 bool renderPlanet();
147 // Planet center in sensor pixels, from the mount pointing, guide pulses, drift and jitter.
148 // Caller must hold m_PlanetMutex.
149 void planetPosition(double &x, double &y);
150 // Draw the planet into the chip subframe: interleaved 8-bit RGB for streaming,
151 // planar 16-bit RGB for FITS captures. Caller must hold m_PlanetMutex.
152 void drawPlanetRGB8(uint8_t *buffer, double centerX, double centerY);
153 void drawPlanetRGB16(uint16_t *buffer, double centerX, double centerY);
154 // Put the planet back in the frame center at the current pointing and restart the drift
155 void recenterPlanet();
156
157 // Updates currentRA/currentDE (J2000) from the snooped mount position and guide pulses
158 void updatePointing();
159 // Shifts JNow ra (hours) and de (degrees) by the user pointing offset
160 void applyPointingOffset(double &ra, double &de);
161 // Periodic error offset in RA (degrees)
162 float periodicErrorOffset();
163 // Camera rotation (degrees): manual offset + snooped rotator + pier flip, or field rotation on an Alt-Az mount
164 double cameraRotation();
165 // Parallactic angle (degrees) of the pointing at the simulated time, 0 while the location is unknown
166 double parallacticAngle();
167
168 // Turns on/off Bayer RGB simulation.
169 void setBayerEnabled(bool onOff);
170
171 float CalcTimeLeft(timeval start, float req);
172
174
175 // Defect layout of the primary sensor, regenerated whenever the resolution or the
176 // SIM_DEFECTS settings change.
177 void updateDefects();
178 std::vector<SensorDefect> m_Defects;
180 double m_DefectsRate {0};
181
182 // Follows the client's TIME_UTC, so the rendered sky agrees with the telescope simulator's
184
185 // ADU ceiling and magnitude calibration -- updated from INDI properties each frame.
186 int m_MaxVal {65000};
187 float m_LimitingMag {11.5f};
188 float m_SaturationMag {2.0f};
189 float m_Seeing {3.5f}; // arcsec FWHM, updated by focus simulation
190
191 double TemperatureRequest { 0 };
192
193 float ExposureRequest { 0 };
194 struct timeval ExpStart
195 {
196 0, 0
197 };
198
200 struct timeval GuideExpStart
201 {
202 0, 0
203 };
204
205 int testvalue { 0 };
206 bool ShowStarField { true };
207 int m_Bias { 1500 };
208 int m_MaxNoise { 20 };
209 float m_SkyGlow { 40 };
210 // An oag is offset this much from center of scope position (arcminutes)
211 float m_OAGOffset { 0 };
212 float m_TimeFactor { 1 };
213 // With a rotator device "RotatorAngle" is snooped and defined, so the
214 // resulting camera rotation is the addition of offset and rotator angle.
215 // Without a rotator device ("Manual Rotator") the rotator angle is
216 // considered fixed to 0° and the camera rotation is equal to offset
217 float m_RotationOffset { 0 };
218 // The snooped mount is Alt-Az, so the field turns with the parallactic angle
219 bool m_AltAzMount { false };
220
221 bool m_SimulateBayer { false };
222 bool m_SimulateTimeout { false };
223
224 // our zero point calcs used for drawing stars
225 //float k { 0 };
226 //float z { 0 };
227
228 bool AbortGuideFrame { false };
229 bool AbortPrimaryFrame { false };
230
232 float GuideRate { 7 };
233
235 float m_PEPeriod { 0 };
236 // PE max in arcsecs
237 float m_PEMax { 0 };
238
239 double currentRA { 0 };
240 double currentDE { 0 };
241 bool usePE { false };
242 double raPE { 0 }; // JNow RA from snooped EQUATORIAL_PE (hours)
243 double decPE { 0 }; // JNow Dec from snooped EQUATORIAL_PE (degrees)
244 time_t RunStart;
245
246 float guideNSOffset {0};
247 float guideWEOffset {0};
248
249 float m_PolarError { 0 };
250 float m_PolarDrift { 0 };
251
253
255 pthread_t primary_thread;
257
258 std::deque<std::string> m_AllFiles, m_RemainingFiles;
259
260 // And this lives in our simulator settings page
262
264 enum
265 {
269 };
270 // We are going to snoop these from focuser
273
274 // Focuser positions for focusing simulation
275 // FocuserPosition[0] is the position where the scope is in focus
276 // FocuserPosition[1] is the maximal position the focuser may move to (@see FOCUS_MAX in #indifocuserinterface.cpp)
277 // FocuserPosition[2] is the seeing (in arcsec)
278 // We need to have these values here, since we cannot snoop it from the focuser (the focuser does not
279 // publish these values)
281 enum
282 {
286 };
287
289 enum
290 {
293 };
294
295 // Fixed hot and cold pixels on the primary sensor, to test dark subtraction and defect maps.
297 enum
298 {
302 };
303
305
306 // Parallactic angle added to the camera rotation while the mount is Alt-Az
308
309 // Renders the sky away from where the mount points, so solved images disagree with the
310 // mount coordinates. Simulates a bad pointing model for testing alignment.
312 enum
313 {
316 };
317
319
321
323
326
328 inline static const std::vector<std::pair<uint32_t, uint32_t>> Resolutions =
329 {
330 {1280, 1024},
331 {6000, 4000},
332 {0, 0} // Custom
333 };
334
335 // ==================== Planet Simulation ====================
336
337 // Lookup table: average angular diameter at opposition (arcsec)
338 // Moon and Sun are mean apparent diameters
339 inline static const std::map<std::string, float> PlanetAngularDiameter =
340 {
341 {"mercury", 11.0f},
342 {"venus", 35.0f},
343 {"mars", 18.0f},
344 {"jupiter", 42.0f},
345 {"saturn", 42.0f}, // including rings
346 {"uranus", 3.7f},
347 {"neptune", 2.3f},
348 {"moon", 1860.0f}, // ~0.5°
349 {"sun", 1920.0f} // ~0.53°
350 };
351
352 // Planet mode toggle
354 enum
355 {
358 };
359
360 // Planet selection switch (one per body)
362 enum
363 {
373 };
374
375 // Planet refresh interval in minutes, and optional size override in arcsec (0 = use lookup table)
377 enum
378 {
381 };
382
383 // Tracking errors and sky conditions applied to the planet position, for testing tracking
385 enum
386 {
391 };
392
394
395 // xplanet executable path and config file path
397 enum
398 {
401 };
402
403 // Planet simulation state
404 bool m_PlanetMode {false};
405 bool m_IsRGBFormat {false};
406 std::string m_SelectedPlanet {"jupiter"};
408 float m_PlanetSizeOverride {0.0f}; // 0 = use lookup table
409 std::string m_XPlanetBinary {"xplanet"};
410 std::string m_XPlanetConfig {"/usr/share/xplanet/config/default"};
411 // Set to 0 to force a fresh xplanet render
412 std::atomic<time_t> m_LastPlanetRenderTime {0};
413 // Guards the sprite and the planet position, used by both the stream thread and exposures
414 std::mutex m_PlanetMutex;
415 // Planet disk scaled to its size on the sensor, interleaved 8-bit RGB, square
416 std::vector<uint8_t> m_PlanetSprite;
417 uint32_t m_PlanetSpriteSize {0};
418 std::vector<uint8_t> m_PlanetStreamFrame;
419 // Sky position (J2000 degrees) the planet sits at, taken from the pointing when recentered
421 double m_PlanetRA {0};
422 double m_PlanetDE {0};
423 std::chrono::steady_clock::time_point m_PlanetDriftStart;
424 std::mt19937 m_PlanetRandom {std::random_device{}()};
425 // Mount is slewing; the planet is recentered when the slew ends
426 bool m_MountSlewing {false};
429 std::string m_CachedPlanet;
430
431 static const constexpr char* SIMULATOR_TAB = "Simulator Config";
432 static const constexpr char* PLANET_TAB = "Planet";
433};
double temperature
virtual bool UpdateCCDFrame(int x, int y, int w, int h) override
CCD calls this function when CCD Frame dimension needs to be updated in the hardware....
bool m_IsRGBFormat
@ SIM_TESTS_TIMEOUT
@ SIM_TESTS_BAYER
@ SIM_TESTS_CRASH
INDI::PropertyNumber TiltSimulationNP
float periodicErrorOffset()
float m_CachedPixelSizeX
bool m_SimulateBayer
double m_LastTemperature
void recenterPlanet()
bool initProperties() override
Initialize properties initial state and value. The child class must implement this function.
INDI::PropertySwitch PlanetRecenterSP
float guideWEOffset
double cameraRotation()
INDI::PropertyNumber PlanetDriftNP
void setBayerEnabled(bool onOff)
INDI::PropertyNumber FocusSimulationNP
bool StartExposure(float duration) override
Start exposing primary CCD chip.
bool loadNextFITSImage(const std::string &filename)
void applyPointingOffset(double &ra, double &de)
virtual IPState GuideSouth(uint32_t) override
Guide southward for ms milliseconds.
INDI::PropertyText PlanetPathsTP
INDI::PropertyText DirectoryTP
int m_DefectsXRes
INDI::PropertyNumber GainNP
bool m_PlanetPositionValid
std::chrono::steady_clock::time_point m_PlanetDriftStart
@ PLANET_DRIFT_RA
@ PLANET_DRIFT_DE
@ PLANET_TRANSPARENCY
bool ShowStarField
static const constexpr char * SIMULATOR_TAB
std::string m_XPlanetConfig
bool watchDirectory()
float GuideRate
Guide rate is 7 arcseconds per second.
bool AbortGuideFrame
double parallacticAngle()
float m_RotationOffset
bool Disconnect() override
Disconnect from device.
float m_SkyGlow
INDI::PropertyNumber EqPENP
void ISGetProperties(const char *dev) override
define the driver's properties to the client. Usually, only a minimum set of properties are defined t...
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override
Process the client newSwitch command.
void * streamVideo()
@ PLANET_DISABLED
float guideNSOffset
bool StartGuideExposure(float) override
Start exposing guide CCD chip.
virtual bool SetCaptureFormat(uint8_t index) override
SetCaptureFormat Set Active Capture format.
INDI::PropertySwitch SimTestsSP
std::vector< uint8_t > m_PlanetSprite
float m_PolarDrift
pthread_t primary_thread
std::string m_XPlanetBinary
virtual ~CCDSim() override=default
float m_PEMax
void drawPlanetRGB16(uint16_t *buffer, double centerX, double centerY)
bool m_MountSlewing
void TimerHit() override
Callback function to be called once SetTimer duration elapses.
double m_PlanetDE
virtual int SetTemperature(double temperature) override
Set CCD temperature.
static const std::map< std::string, float > PlanetAngularDiameter
const char * getDefaultName() override
std::deque< std::string > m_AllFiles
int m_DefectsHot
INDI::PropertySwitch CoolerSP
virtual bool StartStreaming() override
StartStreaming Start live video streaming.
INDI::PropertyNumber SimulatorSettingsNP
static void * streamVideoHelper(void *context)
void drawPlanetRGB8(uint8_t *buffer, double centerX, double centerY)
@ SIM_FOCUS_POSITION
@ PLANET_XPLANET_PATH
@ PLANET_XPLANET_CONFIG
virtual bool ISSnoopDevice(XMLEle *root) override
Process a snoop event from INDI server. This function is called when a snooped property is updated in...
float m_PolarError
bool setupParameters()
std::atomic< time_t > m_LastPlanetRenderTime
float m_PlanetRefreshMinutes
SkyRenderer m_Renderer
virtual IPState GuideEast(uint32_t) override
Guide easward for ms milliseconds.
bool m_PlanetMode
void updateDefects()
void updatePointing()
time_t RunStart
void planetPosition(double &x, double &y)
float m_PlanetSizeOverride
double raPE
virtual IPState GuideWest(uint32_t) override
Guide westward for ms milliseconds.
float m_OAGOffset
bool renderPlanet()
std::mt19937 m_PlanetRandom
@ SIM_SATURATION
@ SIM_LIMITINGMAG
@ SIM_TIME_FACTOR
@ SIM_PE_PERIOD
@ SIM_OAGOFFSET
@ SIM_POLARDRIFT
int QueryFilter() override
Return current filter position.
double m_PlanetRA
INumber FWHMN[1]
virtual bool StopStreaming() override
StopStreaming Stop live video streaming.
float m_PEPeriod
PEPeriod in minutes.
std::vector< uint8_t > m_PlanetStreamFrame
bool SelectFilter(int) override
Select a new filter position.
bool Connect() override
Connect to the device. INDI::DefaultDevice implementation connects to appropriate connection interfac...
double currentDE
std::vector< SensorDefect > m_Defects
INDI::PropertyNumber FieldRotationNP
INDI::PropertyNumber OffsetNP
bool loadNextImage()
int m_DefectsCold
bool AbortPrimaryFrame
virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override
Process the client newSwitch command.
virtual bool UpdateCCDBin(int hor, int ver) override
CCD calls this function when CCD Binning needs to be updated in the hardware. Derived classes should ...
int testvalue
float m_LimitingMag
static const std::vector< std::pair< uint32_t, uint32_t > > Resolutions
INDI::PropertySwitch ResolutionSP
INumberVectorProperty FWHMNP
std::string m_SelectedPlanet
double m_DefectsRate
virtual IPState GuideNorth(uint32_t) override
Guide northward for ms milliseconds.
bool AbortExposure() override
Abort ongoing exposure.
INDI::PropertyNumber PlanetSettingsNP
bool m_AltAzMount
INDI::PropertySwitch PlanetSelectSP
bool m_SimulateTimeout
INDI::PropertyNumber DefectSimulationNP
float m_CachedFocalLength
std::mutex m_PlanetMutex
float m_TimeFactor
float m_SaturationMag
INDI::PropertyNumber PointingOffsetNP
int streamPredicate
double decPE
@ SIM_COLD_PIXELS
std::string m_CachedPlanet
double TemperatureRequest
uint32_t m_PlanetSpriteSize
bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
INDI::PropertySwitch DirectorySP
SimulatorClock m_Clock
virtual void activeDevicesUpdated() override
activeDevicesUpdated Inform children that ActiveDevices property was updated so they can snoop on the...
double currentRA
float ExposureRequest
int m_MaxNoise
std::deque< std::string > m_RemainingFiles
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
INDI::PropertySwitch PlanetModeSP
bool terminateThread
int m_DefectsYRes
float CalcTimeLeft(timeval start, float req)
virtual bool saveConfigItems(FILE *fp) override
saveConfigItems Save configuration items in XML file.
virtual bool UpdateGuiderBin(int hor, int ver) override
CCD calls this function when Guide head binning is updated by the client. Derived classes should impl...
float m_Seeing
float GuideExposureRequest
virtual void addFITSKeywords(INDI::CCDChip *targetChip, std::vector< INDI::FITSRecord > &fitsKeyword) override
Generate FITS keywords that will be added to FIST/XISF file.
bool AbortGuideExposure() override
Abort ongoing exposure.
int DrawCcdFrame(INDI::CCDChip *targetChip)
static const constexpr char * PLANET_TAB
The CCDChip class provides functionality of a CCD Chip within a CCD.
Definition indiccdchip.h:39
Class to provide general functionality of CCD cameras with a single CCD sensor, or a primary CCD sens...
Definition indiccd.h:108
The SimulatorClock class is the clock of a camera simulator: the system clock, shifted to the time th...
double ra
ISState
Switch state.
Definition indiapi.h:148
IPState
Property state.
Definition indiapi.h:158
struct _INumberVectorProperty INumberVectorProperty
*brief user defined string literal for JSON values *sa std::size_t n
struct xml_ele_ XMLEle
Definition lilxml.h:81
One number descriptor.