/
spectrograph_simulator.cpp
372 lines (306 loc) · 11.8 KB
/
spectrograph_simulator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
indi_RadioSim_detector - a software defined radio driver for INDI
Copyright (C) 2017 Ilia Platone
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "spectrograph_simulator.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <indilogger.h>
#include <memory>
#define SPECTRUM_SIZE (256)
#define min(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; })
static pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t condMutex = PTHREAD_MUTEX_INITIALIZER;
std::unique_ptr<RadioSim> receiver(new RadioSim());
void ISGetProperties(const char *dev)
{
receiver->ISGetProperties(dev);
}
void ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int num)
{
receiver->ISNewSwitch(dev, name, states, names, num);
}
void ISNewText(const char *dev, const char *name, char *texts[], char *names[], int num)
{
receiver->ISNewText(dev, name, texts, names, num);
}
void ISNewNumber(const char *dev, const char *name, double values[], char *names[], int num)
{
receiver->ISNewNumber(dev, name, values, names, num);
}
void ISNewBLOB(const char *dev, const char *name, int sizes[], int blobsizes[], char *blobs[], char *formats[],
char *names[], int num)
{
receiver->ISNewBLOB(dev, name, sizes, blobsizes, blobs, formats, names, num);
}
void ISSnoopDevice(XMLEle *root)
{
receiver->ISSnoopDevice(root);
}
RadioSim::RadioSim()
{
streamPredicate = 0;
terminateThread = false;
}
RadioSim::~RadioSim()
{
}
/**************************************************************************************
** Client is asking us to establish connection to the device
***************************************************************************************/
bool RadioSim::Connect()
{
LOG_INFO("Simulator Spectrograph connected successfully!");
// Let's set a timer that checks teleSpectrographs status every POLLMS milliseconds.
// JM 2017-07-31 SetTimer already called in updateProperties(). Just call it once
//SetTimer(POLLMS);
streamPredicate = 0;
terminateThread = false;
// Run threads
std::thread(&RadioSim::streamCaptureHelper, this).detach();
SetTimer(POLLMS);
return true;
}
/**************************************************************************************
** Client is asking us to terminate connection to the device
***************************************************************************************/
bool RadioSim::Disconnect()
{
InIntegration = false;
setBufferSize(1);
pthread_mutex_lock(&condMutex);
streamPredicate = 1;
terminateThread = true;
pthread_cond_signal(&cv);
pthread_mutex_unlock(&condMutex);
LOG_INFO("Simulator Spectrograph disconnected successfully!");
return true;
}
/**************************************************************************************
** INDI is asking us for our default device name
***************************************************************************************/
const char *RadioSim::getDefaultName()
{
return "Spectrograph Simulator";
}
/**************************************************************************************
** INDI is asking us to init our properties.
***************************************************************************************/
bool RadioSim::initProperties()
{
// We set the Spectrograph capabilities
uint32_t cap = SENSOR_CAN_ABORT | SENSOR_HAS_STREAMING | SENSOR_HAS_DSP;
SetCapability(cap);
// Must init parent properties first!
INDI::Spectrograph::initProperties();
setMinMaxStep("SENSOR_INTEGRATION", "SENSOR_INTEGRATION_VALUE", 0.001, 86164.092, 0.001, false);
setMinMaxStep("SPECTROGRAPH_SETTINGS", "SPECTROGRAPH_FREQUENCY", 2.4e+7, 2.0e+9, 1, false);
setMinMaxStep("SPECTROGRAPH_SETTINGS", "SPECTROGRAPH_SAMPLERATE", 1.0e+6, 2.0e+6, 1, false);
setMinMaxStep("SPECTROGRAPH_SETTINGS", "SPECTROGRAPH_GAIN", 0.0, 25.0, 0.1, false);
setMinMaxStep("SPECTROGRAPH_SETTINGS", "SPECTROGRAPH_BANDWIDTH", 0, 0, 0, false);
setMinMaxStep("SPECTROGRAPH_SETTINGS", "SPECTROGRAPH_BITSPERSAMPLE", 16, 16, 0, false);
setIntegrationFileExtension("fits");
// Add Debug, Simulator, and Configuration controls
addAuxControls();
setDefaultPollingPeriod(500);
return true;
}
/********************************************************************************************
** INDI is asking us to update the properties because there is a change in CONNECTION status
** This fucntion is called whenever the device is connected or disconnected.
*********************************************************************************************/
bool RadioSim::updateProperties()
{
if (isConnected())
{
// Inital values
setupParams(1000000, 1420000000, 10000, 10);
// Start the timer
SetTimer(POLLMS);
}
return INDI::Spectrograph::updateProperties();
}
/**************************************************************************************
** Setting up Spectrograph parameters
***************************************************************************************/
void RadioSim::setupParams(float sr, float freq, float bw, float gain)
{
// Our Spectrograph is an 8 bit Spectrograph, 100MHz frequency 1MHz bandwidth.
setFrequency(freq);
setSampleRate(sr);
setBPS(16);
setBandwidth(bw);
setGain(gain);
}
bool RadioSim::ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
{
bool r = false;
if (dev && !strcmp(dev, getDeviceName()) && !strcmp(name, SpectrographSettingsNP.name)) {
for(int i = 0; i < n; i++) {
if (!strcmp(names[i], "SPECTROGRAPH_GAIN")) {
setupParams(getSampleRate(), getFrequency(), getBandwidth(), values[i]);
} else if (!strcmp(names[i], "SPECTROGRAPH_BANDWIDTH")) {
setupParams(getSampleRate(), getFrequency(), values[i], getGain());
} else if (!strcmp(names[i], "SPECTROGRAPH_FREQUENCY")) {
setupParams(getSampleRate(), values[i], getBandwidth(), getGain());
} else if (!strcmp(names[i], "SPECTROGRAPH_SAMPLERATE")) {
setupParams(values[i], getFrequency(), getBandwidth(), getGain());
}
}
IDSetNumber(&SpectrographSettingsNP, nullptr);
}
return processNumber(dev, name, values, names, n) & !r;
}
/**************************************************************************************
** Client is asking us to start an exposure
***************************************************************************************/
bool RadioSim::StartIntegration(double duration)
{
IntegrationRequest = duration;
AbortIntegration();
// Since we have only have one Spectrograph with one chip, we set the exposure duration of the primary Spectrograph
setIntegrationTime(duration);
int to_read = getSampleRate() * getIntegrationTime() * abs(getBPS()) / 8;
setBufferSize(to_read);
InIntegration = true;
gettimeofday(&CapStart, nullptr);
if(HasStreaming()) {
Streamer->setPixelFormat(INDI_MONO, getBPS());
Streamer->setSize(getBufferSize() * 8 / abs(getBPS()), 1);
}
// We're done
return true;
}
/**************************************************************************************
** Client is asking us to abort a capture
***************************************************************************************/
bool RadioSim::AbortIntegration()
{
if(InIntegration) {
InIntegration = false;
}
return true;
}
/**************************************************************************************
** How much longer until exposure is done?
***************************************************************************************/
float RadioSim::CalcTimeLeft()
{
double timesince;
double timeleft;
struct timeval now;
gettimeofday(&now, nullptr);
timesince = (double)(now.tv_sec * 1000.0 + now.tv_usec / 1000) -
(double)(CapStart.tv_sec * 1000.0 + CapStart.tv_usec / 1000);
timesince = timesince / 1000;
timeleft = IntegrationRequest - timesince;
return timeleft;
}
/**************************************************************************************
** Main device loop. We check for capture progress here
***************************************************************************************/
void RadioSim::TimerHit()
{
long timeleft;
if (isConnected() == false)
return; // No need to reset timer if we are not connected anymore
if (InIntegration)
{
timeleft = CalcTimeLeft();
if(timeleft <= 0.0)
{
/* We're done capturing */
LOG_INFO("Integration done, expecting data...");
timeleft = 0.0;
grabData();
}
// This is an over simplified timing method, check SpectrographSimulator and RadioSimSpectrograph for better timing checks
setIntegrationLeft(timeleft);
}
SetTimer(POLLMS);
return;
}
/**************************************************************************************
** Create the spectrum
***************************************************************************************/
void RadioSim::grabData()
{
if(InIntegration) {
LOG_INFO("Downloading...");
InIntegration = false;
uint8_t* continuum;
int size = getBufferSize();
//Fill the continuum
continuum = getBuffer();
for(int i = 0; i < size; i++)
continuum[i] = rand() % 255;
LOG_INFO("Download complete.");
IntegrationComplete();
}
}
//Streamer API functions
bool RadioSim::StartStreaming()
{
pthread_mutex_lock(&condMutex);
streamPredicate = 1;
pthread_mutex_unlock(&condMutex);
pthread_cond_signal(&cv);
return true;
}
bool RadioSim::StopStreaming()
{
pthread_mutex_lock(&condMutex);
streamPredicate = 0;
pthread_mutex_unlock(&condMutex);
pthread_cond_signal(&cv);
return true;
}
void RadioSim::streamCaptureHelper()
{
struct itimerval tframe1, tframe2;
double deltas;
getitimer(ITIMER_REAL, &tframe1);
auto s1 = ((double)tframe2.it_value.tv_sec) + ((double)tframe2.it_value.tv_usec / 1e6);
auto s2 = ((double)tframe2.it_value.tv_sec) + ((double)tframe2.it_value.tv_usec / 1e6);
while (true)
{
pthread_mutex_lock(&condMutex);
while (streamPredicate == 0)
{
pthread_cond_wait(&cv, &condMutex);
}
StartIntegration(1.0 / Streamer->getTargetFPS());
if (terminateThread)
break;
// release condMutex
pthread_mutex_unlock(&condMutex);
// Simulate exposure time
//usleep(ExposureRequest*1e5);
grabData();
getitimer(ITIMER_REAL, &tframe1);
s2 = ((double)tframe2.it_value.tv_sec) + ((double)tframe2.it_value.tv_usec / 1e6);
deltas = fabs(s2 - s1);
if (deltas < IntegrationTime)
usleep(fabs(IntegrationTime - deltas) * 1e6);
int32_t size = getBufferSize();
Streamer->newFrame(getBuffer(), size);
s1 = ((double)tframe1.it_value.tv_sec) + ((double)tframe1.it_value.tv_usec / 1e6);
getitimer(ITIMER_REAL, &tframe2);
}
pthread_mutex_unlock(&condMutex);
}