-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmerlinDetector.cpp
More file actions
1611 lines (1437 loc) · 57 KB
/
merlinDetector.cpp
File metadata and controls
1611 lines (1437 loc) · 57 KB
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* merlinDetector.cpp
*
* This is a driver for the Merlin detector (Quad chip version but supports other chip counts)
*
* The driver is designed to communicate with the chip via the matching Labview controller over TCP/IP
*
* Author: Giles Knap
* Diamond Light Source Ltd.
*
* Created: Jan 06 2012
*
* Original Source from pilatusDetector by Mark Rivers
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <stdint.h>
// #include <epicsTime.h>
#include <epicsThread.h>
#include <epicsEvent.h>
#include <epicsMutex.h>
#include <epicsString.h>
#include <epicsStdio.h>
#include <epicsMutex.h>
#include <cantProceed.h>
#include <iocsh.h>
#include <epicsExport.h>
#include <asynOctetSyncIO.h>
#include "ADDriver.h"
#include "mpxConnection.h"
#include "merlinDetector.h"
#define MAX(a,b) a>b ? a : b
#define MIN(a,b) a<b ? a : b
/** This thread controls acquisition, reads image files to get the image data, and
* does the callbacks to send it to higher layers
* It is totally decoupled from the command thread and simply waits for data
* frames to be sent on the data channel (TCP) regardless of the state in the command
* thread and TCP channel */
void merlinDetector::merlinTask()
{
int status = asynSuccess;
int imageCounter; // number of ndarrays sent to plugins
int numImagesCounter; // number of images received
int counterDepth;
int imagSize;
NDArray * pImage;
epicsTimeStamp startTime;
const char *functionName = "merlinTask";
size_t dims[2];
int arrayCallbacks;
int nread;
char *bigBuff;
char aquisitionHeader[MPX_ACQUISITION_HEADER_LEN + 1];
int triggerMode;
NDAttributeList *imageAttr = new NDAttributeList();
// do not enter this thread until the IOC is initialised. This is because we are getting blocks of
// data on the data channel at startup after we have had a buffer overrun
while (startingUp)
{
epicsThreadSleep(.5);
}
this->lock();
// allocate a buffer for reading in images from labview over network
switch (detType)
{
case UomXBPM:
imagSize = MAX_BUFF_UOM;
break;
case Merlin:
case MerlinXBPM: imagSize = MPX_IMG_FRAME_LEN24;
break;
case MerlinQuad:
imagSize = MAX_BUFF_MERLIN_QUAD;
break;
default:
imagSize = MAX_BUFF_UOM;
break;
}
bigBuff = (char*) calloc(imagSize, 1);
/* Loop forever */
while (1)
{
// Get the current time
epicsTimeGetCurrent(&startTime);
// Acquire an image from the data channel
memset(bigBuff, 0, MPX_IMG_FRAME_LEN);
/* We release the mutex when waiting because this takes a long time and
* we need to allow abort operations to get through */
this->unlock();
// wait for the next data frame packet - this function spends most of its time here
status = cmdConnection->mpxRead(this->pasynLabViewData, bigBuff,
imagSize, &nread, 10);
/* If there was an error jump to bottom of loop */
if (status)
{
if (status == asynTimeout)
status = asynSuccess; // timeouts are expected
else
{
asynPrint(this->pasynLabViewData, ASYN_TRACE_ERROR,
"%s:%s: error in Labview data channel response, status=%d\n",
driverName, functionName, status);
setStringParam(ADStatusMessage,
"Error in Labview data channel response");
// wait before trying again - otherwise socket error creates a tight loop
epicsThreadSleep(5);
}
this->lock();
continue;
}
this->lock();
asynPrint(this->pasynUserSelf, ASYN_TRACE_MPX,
"\nReceived image frame of %d bytes\n", nread);
if (pasynTrace->getTraceMask((pasynUserSelf))
& (ASYN_TRACE_MPX_VERBOSE))
{
dataConnection->dumpData(bigBuff, nread);
}
merlinDataHeader header = dataConnection->parseDataHeader(bigBuff);
if (header != MPXAcquisitionHeader)
{
getIntegerParam(ADNumImagesCounter, &numImagesCounter);
numImagesCounter++;
setIntegerParam(ADNumImagesCounter, numImagesCounter);
if (imagesRemaining > 0)
imagesRemaining--;
getIntegerParam(NDArrayCounter, &imageCounter);
imageCounter++;
setIntegerParam(NDArrayCounter, imageCounter);
}
getIntegerParam(NDArrayCallbacks, &arrayCallbacks);
if (arrayCallbacks)
{
getIntegerParam(merlinCounterDepth, &counterDepth);
int idim;
/* Get an image buffer from the pool */
getIntegerParam(ADMaxSizeX, &idim);
dims[0] = idim;
getIntegerParam(ADMaxSizeY, &idim);
dims[1] = idim;
if (header == MPXAcquisitionHeader)
{
// this is an acquisition header
strncpy(aquisitionHeader, bigBuff, MPX_ACQUISITION_HEADER_LEN);
aquisitionHeader[MPX_ACQUISITION_HEADER_LEN] = 0;
}
else if (header == MPXQuadDataHeader)
{
int pixelSize;
int offset, profileSelect;
asynPrint(this->pasynUserSelf, ASYN_TRACE_MPX,
"Creating a Quad Merlin Image NDArray\n");
// Parse the header and use the information to determine the
// size of the NDArray
imageAttr->clear();
dataConnection->parseMqDataFrame(imageAttr, bigBuff, &(dims[0]),
&(dims[1]), &pixelSize, &offset, &profileSelect);
pImage = NULL;
if (pixelSize == 8)
{
pImage = copyToNDArray8(dims, bigBuff, offset);
}
else if (pixelSize == 16)
{
pImage = copyToNDArray16(dims, bigBuff, offset);
}
else if (pixelSize == 32)
{
pImage = copyToNDArray32(dims, bigBuff, offset);
}
else
{
asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
"Unsupported bit depth %d\n", pixelSize);
setStringParam(ADStatusMessage,
"Error: Unsupported bit depth");
}
if (pImage == NULL)
{
continue;
}
imageAttr->copy(pImage->pAttributeList);
}
else if (header == MPXProfileHeader)
{
int profileMask = 0;
asynPrint(this->pasynUserSelf, ASYN_TRACE_MPX,
"Creating a Profile NDArray\n");
imageAttr->clear();
pImage = NULL;
// dataConnection->parseDataFrame(imageAttr, bigBuff, header,
// &(dims[0]), &(dims[1]), &dummy2, &profileMask);
// TODO do profiles using 2.0 release of documentation
// TODO instead of above we should call parseMqDataFrame
// TODO in fact not sure we need a separate clause at this point.
if (profileMask
!= (MPXPROFILES_XPROFILE | MPXPROFILES_YPROFILE
| MPXPROFILES_SUM))
{
asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
"%s:%s: unsupported PROFILES mode %d\n", driverName,
functionName, profileMask);
}
else
{
pImage = copyProfileToNDArray32(dims, bigBuff, profileMask);
}
if (pImage == NULL)
continue;
imageAttr->copy(pImage->pAttributeList);
}
else
{
asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
"Unknown header type %d\n", header);
}
// for Data frames - complete the NDAttributes, pass the NDArray on
if (header == MPXProfileHeader || header == MPXQuadDataHeader)
{
// Put the frame number and time stamp into the buffer
pImage->uniqueId = imageCounter;
pImage->timeStamp = startTime.secPastEpoch
+ startTime.nsec / 1.e9;
// string attributes are global in HDF5 plugin so the most recent
// acquisition header is applied to all files
pImage->pAttributeList->add("Acquisition Header", "",
NDAttrString, aquisitionHeader);
/* Get any attributes that have been defined for this driver */
this->getAttributes(pImage->pAttributeList);
// Call the NDArray callback
if (header == MPXQuadDataHeader)
{
doCallbacksGenericPointer(pImage, NDArrayData, 0);
}
else
{
// address 1 on the port is used for profiles
// TODO use of port 1 is not working in NDPluginBase so
// currently reverting to use the same address
// (i.e. setting Merlin1:ROI:NDArrayAddress has no effect
doCallbacksGenericPointer(pImage, NDArrayData, 0);
}
/* Free the image buffer */
pImage->release();
}
}
// If we are using SW triggers then reset the trigger to 0 when an image is
// received
status = getIntegerParam(ADTriggerMode, &triggerMode);
if (triggerMode == TMSoftwareTrigger)
{
// software trigger resets when image received
setIntegerParam(merlinSoftwareTrigger, 0);
}
// If all the expected images have been received then the driver can
// complete the acquisition and return to waiting for acquisition state
if (imagesRemaining == 0)
{
setIntegerParam(ADAcquire, 0);
setIntegerParam(ADStatus, ADStatusIdle);
}
/* Call the callbacks to update any changes */
callParamCallbacks();
}
// release the image buffer (in reality this does not get called
// We need a thread shutdown signal)
free(bigBuff);
}
/** helper functions for endian conversion
*
*/
inline void merlinDetector::endian_swap(unsigned short& x)
{
if (detType == Merlin || detType == MerlinQuad)
{
x = (x >> 8) | (x << 8);
}
}
inline void merlinDetector::endian_swap(unsigned int& x)
{
if (detType == Merlin || detType == MerlinQuad)
{
x = (x >> 24) | ((x << 8) & 0x00FF0000) | ((x >> 8) & 0x0000FF00)
| (x << 24);
}
}
inline void merlinDetector::endian_swap(uint64_t& x)
{
if (detType == Merlin || detType == MerlinQuad)
{
x = ((((x) & 0x00000000000000FFLL) << 0x38)
| (((x) & 0x000000000000FF00LL) << 0x28)
| (((x) & 0x0000000000FF0000LL) << 0x18)
| (((x) & 0x00000000FF000000LL) << 0x08)
| (((x) & 0x000000FF00000000LL) >> 0x08)
| (((x) & 0x0000FF0000000000LL) >> 0x18)
| (((x) & 0x00FF000000000000LL) >> 0x28)
| (((x) & 0xFF00000000000000LL) >> 0x38));
}
}
void merlinDetector::fromLabViewStr(const char *str)
{
setStringParam(ADStringFromServer, str);
}
void merlinDetector::toLabViewStr(const char *str)
{
setStringParam(ADStringToServer, str);
}
/** Helper function to copy a 64bit profile buffer into a 32Bit NDArray
*
*
*/
NDArray* merlinDetector::copyProfileToNDArray32(size_t *dims, char *buffer,
int profileMask)
{
epicsUInt32 *pData;
epicsUInt32 *pWaveForm;
uint64_t *pSrc;
size_t x;
int y;
size_t profileDims[2];
profileDims[0] = MAX(dims[0], dims[1]);
profileDims[1] = 2;
// for profiles we do a max dim * 2 array to hold both x
// and y profile
NDArray* pImage = this->pNDArrayPool->alloc(2, profileDims, NDUInt32, 0,
NULL);
asynPrint(this->pasynUserSelf, ASYN_TRACE_MPX,
"%s:%s: Creating profile waveforms xsize %lu. ysize %lu\n",
driverName, "copyProfileToNDArray32", dims[0], dims[1]);
if (pImage == NULL)
{
asynPrint(this->pasynLabViewData, ASYN_TRACE_ERROR,
"%s:%s: unable to allocate NDArray from pool\n", driverName,
"copyProfileToNDArray32");
setStringParam(ADStatusMessage,
"Error: run out of buffers in detector driver");
}
else
{
// Copy the X,Y profile data into the (size * 2) NDArray
// and into the X,Y waveforms
pData = (epicsUInt32*) pImage->pData;
for (x = 0, pWaveForm = (epicsUInt32 *) profileX, pSrc =
(uint64_t *) ((buffer + MPX_IMG_HDR_LEN)); x < dims[0];
x++, pWaveForm++, pSrc++, pData++)
{
endian_swap(*pSrc);
*pWaveForm = (epicsUInt32) *pSrc;
*pData = (epicsUInt32) *pSrc;
}
// Invert the Y profile (merlin origin is at bottom left)
for (y = dims[1] - 1, pWaveForm = (epicsUInt32 *) profileY; y >= 0;
y--, pWaveForm++, pSrc++, pData++)
{
endian_swap(*pSrc);
*pWaveForm = (epicsUInt32) *pSrc;
*pData = (epicsUInt32) *pSrc;
}
doCallbacksInt32Array(profileY, dims[1], merlinProfileY, 0);
doCallbacksInt32Array(profileX, dims[0], merlinProfileX, 0);
}
return pImage;
}
// Todo make the following three functions a single template function
/** Helper function to copy a 8 bit buffer into an NDArray
*
*/
NDArray* merlinDetector::copyToNDArray8(size_t *dims, char *buffer, int offset)
{
// copy the data into NDArray, switching to little endien and
// Inverting in the Y axis (merlin origin is at bottom left)
epicsUInt8 *pData, *pSrc;
size_t x, y;
NDArray* pImage = this->pNDArrayPool->alloc(2, dims, NDUInt8, 0, NULL);
if (pImage == NULL)
{
asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
"%s:%s: unable to allocate NDArray from pool\n", driverName,
"copyToNDArray8");
setStringParam(ADStatusMessage,
"Error: run out of buffers in detector driver");
}
else
{
for (y = 0; y < dims[1]; y++)
{
for (x = 0, pData = (epicsUInt8 *) pImage->pData + y * dims[0], pSrc =
(epicsUInt8 *) (buffer + offset)
+ (dims[1] - y) * dims[0]; x < dims[0];
x++, pData++, pSrc++)
{
*pData = *pSrc;
}
}
}
return pImage;
}
/** Helper function to copy a 16 bit buffer into an NDArray
*
*/
NDArray* merlinDetector::copyToNDArray16(size_t *dims, char *buffer, int offset)
{
// copy the data into NDArray, switching to little endien and
// Inverting in the Y axis (merlin origin is at bottom left)
epicsUInt16 *pData, *pSrc;
size_t x, y;
NDArray* pImage = this->pNDArrayPool->alloc(2, dims, NDUInt16, 0, NULL);
if (pImage == NULL)
{
asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
"%s:%s: unable to allocate NDArray from pool\n", driverName,
"copyToNDArray16");
setStringParam(ADStatusMessage,
"Error: run out of buffers in detector driver");
}
else
{
for (y = 0; y < dims[1]; y++)
{
for (x = 0, pData = (epicsUInt16 *) pImage->pData + y * dims[0], pSrc =
(epicsUInt16 *) (buffer + offset)
+ (dims[1] - y) * dims[0]; x < dims[0];
x++, pData++, pSrc++)
{
*pData = *pSrc;
endian_swap(*pData);
}
}
}
return pImage;
}
/** Helper function to copy a 32 bit buffer into an NDArray
*
*/
NDArray* merlinDetector::copyToNDArray32(size_t* dims, char* buffer, int offset)
{
epicsUInt32 *pData, *pSrc;
size_t x, y;
NDArray* pImage = this->pNDArrayPool->alloc(2, dims, NDUInt32, 0, NULL);
if (pImage == NULL)
{
asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
"%s:%s: unable to allocate NDArray from pool\n", driverName,
"copyToNDArray32");
setStringParam(ADStatusMessage,
"Error: run out of buffers in detector driver");
}
else
{
for (y = 0; y < dims[1]; y++)
{
for (x = 0, pData = (epicsUInt32 *) pImage->pData + y * dims[0], pSrc =
(epicsUInt32 *) (buffer + offset)
+ (dims[1] - y) * dims[0]; x < dims[0];
x++, pData++, pSrc++)
{
*pData = *pSrc;
endian_swap(*pData);
}
}
}
return pImage;
}
asynStatus merlinDetector::setModeCommands(int function)
{
asynStatus status;
char value[MPX_MAXLINE];
int counter1Enabled, continuousEnabled;
if (function == merlinEnableCounter1)
{
status = getIntegerParam(merlinEnableCounter1, &counter1Enabled);
if ((status != asynSuccess)
|| (counter1Enabled < 0 || counter1Enabled > 1))
{
counter1Enabled = 0;
setIntegerParam(merlinEnableCounter1, counter1Enabled);
}
epicsSnprintf(value, MPX_MAXLINE, "%d", counter1Enabled);
cmdConnection->mpxSet(MPXVAR_ENABLECOUNTER1, value,
Labview_DEFAULT_TIMEOUT);
}
if (function == merlinContinuousRW)
{
status = getIntegerParam(merlinContinuousRW, &continuousEnabled);
if ((status != asynSuccess)
|| (continuousEnabled < 0 || continuousEnabled > 1))
{
continuousEnabled = 0;
setIntegerParam(merlinContinuousRW, continuousEnabled);
}
epicsSnprintf(value, MPX_MAXLINE, "%d", continuousEnabled);
cmdConnection->mpxSet(MPXVAR_CONTINUOUSRW, value,
Labview_DEFAULT_TIMEOUT);
}
epicsThreadSleep(.01);
// now get the values again from the device -- it may reset them to consistent values
// (presently only one of merlinContinuousRW or merlinEnableCounter1 can be set at a time)
status = cmdConnection->mpxGet(MPXVAR_ENABLECOUNTER1,
Labview_DEFAULT_TIMEOUT);
if (status == asynSuccess)
setIntegerParam(merlinEnableCounter1,
atoi(cmdConnection->fromLabviewValue));
status = cmdConnection->mpxGet(MPXVAR_CONTINUOUSRW,
Labview_DEFAULT_TIMEOUT);
if (status == asynSuccess)
setIntegerParam(merlinContinuousRW,
atoi(cmdConnection->fromLabviewValue));
return (asynSuccess);
}
/* Set ROI parameters on detector - only supported on Manchester BPM
*
*/
asynStatus merlinDetector::setROI()
{
char value[MPX_MAXLINE];
NDDimension_t arrayDims[DIMS];
bool roiRequired;
int param;
if (detType == UomXBPM)
{
// determine ROI parameters
memset(arrayDims, 0, sizeof(NDDimension_t) * DIMS);
getIntegerParam(ADMinX, ¶m);
arrayDims[0].offset = param;
getIntegerParam(ADMinY, ¶m);
arrayDims[1].offset = param;
getIntegerParam(ADSizeX, ¶m);
arrayDims[0].size = param;
getIntegerParam(ADSizeY, ¶m);
arrayDims[1].size = param;
getIntegerParam(ADBinX, &arrayDims[0].binning);
getIntegerParam(ADBinY, &arrayDims[1].binning);
getIntegerParam(ADReverseX, &arrayDims[0].reverse);
getIntegerParam(ADReverseY, &arrayDims[1].reverse);
// validate ROI Parameters
for (int dim = 0; dim < DIMS; dim++)
{
NDDimension_t* pDim = &arrayDims[dim];
pDim->offset = MAX(pDim->offset, 0);
pDim->offset = MIN(pDim->offset, maxSize[dim] - 1);
pDim->size = MAX(pDim->size, 1);
pDim->size = MIN(pDim->size,
maxSize[dim] - pDim->offset);
pDim->binning = MAX(pDim->binning, 1);
pDim->binning = MIN(pDim->binning, (int) pDim->size);
}
// Write back ROI parameters that may have changed
setIntegerParam(ADMinX, arrayDims[0].offset);
setIntegerParam(ADMinY, arrayDims[1].offset);
setIntegerParam(ADSizeX, arrayDims[0].size);
setIntegerParam(ADSizeY, arrayDims[1].size);
setIntegerParam(ADBinX, arrayDims[0].binning);
setIntegerParam(ADBinY, arrayDims[1].binning);
roiRequired = arrayDims[0].offset != 0 || arrayDims[1].offset != 0
|| arrayDims[0].size != maxSize[0]
|| arrayDims[1].size != maxSize[1] || arrayDims[0].binning != 1
|| arrayDims[1].binning != 1 || arrayDims[0].reverse != 0
|| arrayDims[1].reverse != 0;
epicsSnprintf(value, MPX_MAXLINE, "%lu %lu %lu %lu", arrayDims[0].offset,
arrayDims[1].offset, arrayDims[0].size, arrayDims[1].size);
cmdConnection->mpxSet(MPXVAR_ROI, value, Labview_DEFAULT_TIMEOUT);
}
return asynSuccess;
}
asynStatus merlinDetector::setAcquireParams()
{
int triggerMode;
char value[MPX_MAXLINE];
asynStatus status;
// char *substr = NULL;
// int pixelCutOff = 0;
// avoid chatty startup which keeps setting these values
if (startingUp)
return asynSuccess;
if (detType == MerlinXBPM || detType == UomXBPM)
{
int exposures, val;
getIntegerParam(ADNumExposures, &exposures);
epicsSnprintf(value, MPX_MAXLINE, "%d", exposures);
cmdConnection->mpxSet(MPXVAR_IMAGESTOSUM, value,
Labview_DEFAULT_TIMEOUT);
getIntegerParam(merlinEnableBackgroundCorr, &val);
epicsSnprintf(value, MPX_MAXLINE, "%d", val);
cmdConnection->mpxSet(MPXVAR_ENABLEBACKROUNDCORR, value,
Labview_DEFAULT_TIMEOUT);
getIntegerParam(merlinEnableImageSum, &val);
epicsSnprintf(value, MPX_MAXLINE, "%d", val);
cmdConnection->mpxSet(MPXVAR_ENABLEIMAGEAVERAGE, value,
Labview_DEFAULT_TIMEOUT);
}
int numImages;
status = getIntegerParam(ADNumImages, &numImages);
if ((status != asynSuccess) || (numImages < 1))
{
numImages = 1;
setIntegerParam(ADNumImages, numImages);
}
int numExposures;
status = getIntegerParam(ADNumExposures, &numExposures);
if ((status != asynSuccess) || (numExposures < 1))
{
numExposures = 1;
setIntegerParam(ADNumExposures, numExposures);
}
int counterDepth;
status = getIntegerParam(merlinCounterDepth, &counterDepth);
if ((status != asynSuccess) || (counterDepth != 6 && counterDepth != 12 &&
counterDepth != 24)) // currently limited to 6/12/24 bit
{
counterDepth = 12;
setIntegerParam(merlinCounterDepth, counterDepth);
}
double acquireTime;
status = getDoubleParam(ADAcquireTime, &acquireTime);
if ((status != asynSuccess) || (acquireTime < 0.))
{
acquireTime = 1.;
setDoubleParam(ADAcquireTime, acquireTime);
}
double acquirePeriod;
status = getDoubleParam(ADAcquirePeriod, &acquirePeriod);
if ((status != asynSuccess) || (acquirePeriod < 0.))
{
acquirePeriod = 1.0;
setDoubleParam(ADAcquirePeriod, acquirePeriod);
}
callParamCallbacks();
// set the values enmasse - an attempt to fix the strange GUI updates caused by slow comms (failed)
epicsSnprintf(value, MPX_MAXLINE, "%d", numExposures);
cmdConnection->mpxSet(MPXVAR_NUMFRAMESPERTRIGGER, value,
Labview_DEFAULT_TIMEOUT);
epicsSnprintf(value, MPX_MAXLINE, "%d", counterDepth);
cmdConnection->mpxSet(MPXVAR_COUNTERDEPTH, value, Labview_DEFAULT_TIMEOUT);
epicsSnprintf(value, MPX_MAXLINE, "%f", acquireTime * 1000); // translated into millisec
cmdConnection->mpxSet(MPXVAR_ACQUISITIONTIME, value,
Labview_DEFAULT_TIMEOUT);
epicsSnprintf(value, MPX_MAXLINE, "%f", acquirePeriod * 1000); // translated into millisec
cmdConnection->mpxSet(MPXVAR_ACQUISITIONPERIOD, value,
Labview_DEFAULT_TIMEOUT);
status = getIntegerParam(ADTriggerMode, &triggerMode);
if (status != asynSuccess)
triggerMode = TMInternal;
// merlin individually controls how start and stop triggers are read
// here we translate the chosen trigger mode into a combination of start
// and stop modes
switch (triggerMode)
{
case TMInternal:
cmdConnection->mpxSet(MPXVAR_TRIGGERSTART, TMTrigInternal,
Labview_DEFAULT_TIMEOUT);
cmdConnection->mpxSet(MPXVAR_TRIGGERSTOP, TMTrigInternal,
Labview_DEFAULT_TIMEOUT);
break;
case TMExternalEnable:
cmdConnection->mpxSet(MPXVAR_TRIGGERSTART, TMTrigRising,
Labview_DEFAULT_TIMEOUT);
cmdConnection->mpxSet(MPXVAR_TRIGGERSTOP, TMTrigFalling,
Labview_DEFAULT_TIMEOUT);
break;
case TMExternalTriggerLow:
cmdConnection->mpxSet(MPXVAR_TRIGGERSTART, TMTrigFalling,
Labview_DEFAULT_TIMEOUT);
cmdConnection->mpxSet(MPXVAR_TRIGGERSTOP, TMTrigInternal,
Labview_DEFAULT_TIMEOUT);
break;
case TMExternalTriggerHigh:
cmdConnection->mpxSet(MPXVAR_TRIGGERSTART, TMTrigRising,
Labview_DEFAULT_TIMEOUT);
cmdConnection->mpxSet(MPXVAR_TRIGGERSTOP, TMTrigInternal,
Labview_DEFAULT_TIMEOUT);
break;
case TMExternalTriggerRising:
cmdConnection->mpxSet(MPXVAR_TRIGGERSTART, TMTrigRising,
Labview_DEFAULT_TIMEOUT);
cmdConnection->mpxSet(MPXVAR_TRIGGERSTOP, TMTrigRising,
Labview_DEFAULT_TIMEOUT);
break;
case TMLVDSExternalEnable:
cmdConnection->mpxSet(MPXVAR_TRIGGERSTART, TMLVDSTrigRising,
Labview_DEFAULT_TIMEOUT);
cmdConnection->mpxSet(MPXVAR_TRIGGERSTOP, TMLVDSTrigFalling,
Labview_DEFAULT_TIMEOUT);
break;
case TMLVDSExternalTriggerLow:
cmdConnection->mpxSet(MPXVAR_TRIGGERSTART, TMLVDSTrigFalling,
Labview_DEFAULT_TIMEOUT);
cmdConnection->mpxSet(MPXVAR_TRIGGERSTOP, TMTrigInternal,
Labview_DEFAULT_TIMEOUT);
break;
case TMLVDSExternalTriggerHigh:
cmdConnection->mpxSet(MPXVAR_TRIGGERSTART, TMLVDSTrigRising,
Labview_DEFAULT_TIMEOUT);
cmdConnection->mpxSet(MPXVAR_TRIGGERSTOP, TMTrigInternal,
Labview_DEFAULT_TIMEOUT);
break;
case TMLVDSExternalTriggerRising:
cmdConnection->mpxSet(MPXVAR_TRIGGERSTART, TMLVDSTrigRising,
Labview_DEFAULT_TIMEOUT);
cmdConnection->mpxSet(MPXVAR_TRIGGERSTOP, TMLVDSTrigRising,
Labview_DEFAULT_TIMEOUT);
break;
case TMSoftwareTrigger:
cmdConnection->mpxSet(MPXVAR_TRIGGERSTART, TMTrigSoftware,
Labview_DEFAULT_TIMEOUT);
break;
}
// read the acquire period back from the server so that it can insert
// the readback time if necessary
cmdConnection->mpxGet(MPXVAR_ACQUISITIONPERIOD, Labview_DEFAULT_TIMEOUT);
if (status == asynSuccess)
setDoubleParam(ADAcquirePeriod,
atof(cmdConnection->fromLabviewValue) / 1000); // translated into secs
return (asynSuccess);
}
asynStatus merlinDetector::getThreshold()
{
int status;
if (startingUp)
return asynSuccess;
/* Read back the actual setting, in case we are out of bounds.*/
status = cmdConnection->mpxGet(MPXVAR_THRESHOLD0, Labview_DEFAULT_TIMEOUT);
if (status == asynSuccess)
setDoubleParam(merlinThreshold0,
atof(cmdConnection->fromLabviewValue));
status = cmdConnection->mpxGet(MPXVAR_THRESHOLD1, Labview_DEFAULT_TIMEOUT);
if (status == asynSuccess)
setDoubleParam(merlinThreshold1,
atof(cmdConnection->fromLabviewValue));
status = cmdConnection->mpxGet(MPXVAR_THRESHOLD2, Labview_DEFAULT_TIMEOUT);
if (status == asynSuccess)
setDoubleParam(merlinThreshold2,
atof(cmdConnection->fromLabviewValue));
status = cmdConnection->mpxGet(MPXVAR_THRESHOLD3, Labview_DEFAULT_TIMEOUT);
if (status == asynSuccess)
setDoubleParam(merlinThreshold3,
atof(cmdConnection->fromLabviewValue));
status = cmdConnection->mpxGet(MPXVAR_THRESHOLD4, Labview_DEFAULT_TIMEOUT);
if (status == asynSuccess)
setDoubleParam(merlinThreshold4,
atof(cmdConnection->fromLabviewValue));
status = cmdConnection->mpxGet(MPXVAR_THRESHOLD5, Labview_DEFAULT_TIMEOUT);
if (status == asynSuccess)
setDoubleParam(merlinThreshold5,
atof(cmdConnection->fromLabviewValue));
status = cmdConnection->mpxGet(MPXVAR_THRESHOLD6, Labview_DEFAULT_TIMEOUT);
if (status == asynSuccess)
setDoubleParam(merlinThreshold6,
atof(cmdConnection->fromLabviewValue));
status = cmdConnection->mpxGet(MPXVAR_THRESHOLD7, Labview_DEFAULT_TIMEOUT);
if (status == asynSuccess)
setDoubleParam(merlinThreshold7,
atof(cmdConnection->fromLabviewValue));
status = cmdConnection->mpxGet(MPXVAR_OPERATINGENERGY,
Labview_DEFAULT_TIMEOUT);
if (status == asynSuccess)
setDoubleParam(merlinOperatingEnergy,
atof(cmdConnection->fromLabviewValue));
callParamCallbacks();
return (asynSuccess);
}
asynStatus merlinDetector::updateThresholdScanParms()
{
asynStatus status = asynSuccess;
char valueStr[MPX_MAXLINE];
int thresholdScan;
double start, stop, step;
if (startingUp)
return asynSuccess;
getDoubleParam(merlinStartThresholdScan, &start);
getDoubleParam(merlinStopThresholdScan, &stop);
getDoubleParam(merlinStepThresholdScan, &step);
getIntegerParam(merlinThresholdScan, &thresholdScan);
epicsSnprintf(valueStr, MPX_MAXLINE, "%f", start);
status = cmdConnection->mpxSet(MPXVAR_THSTART, valueStr,
Labview_DEFAULT_TIMEOUT);
if (status == asynSuccess)
{
epicsSnprintf(valueStr, MPX_MAXLINE, "%f", stop);
status = cmdConnection->mpxSet(MPXVAR_THSTOP, valueStr,
Labview_DEFAULT_TIMEOUT);
}
if (status == asynSuccess)
{
epicsSnprintf(valueStr, MPX_MAXLINE, "%f", step);
status = cmdConnection->mpxSet(MPXVAR_THSTEP, valueStr,
Labview_DEFAULT_TIMEOUT);
}
if (status == asynSuccess)
{
epicsSnprintf(valueStr, MPX_MAXLINE, "%d", thresholdScan);
status = cmdConnection->mpxSet(MPXVAR_THSSCAN, valueStr,
Labview_DEFAULT_TIMEOUT);
}
/* Read back the actual setting, in case we are out of bounds.*/
status = cmdConnection->mpxGet(MPXVAR_THSTART, Labview_DEFAULT_TIMEOUT);
if (status == asynSuccess)
setDoubleParam(merlinStartThresholdScan,
atof(cmdConnection->fromLabviewValue));
status = cmdConnection->mpxGet(MPXVAR_THSTEP, Labview_DEFAULT_TIMEOUT);
if (status == asynSuccess)
setDoubleParam(merlinStepThresholdScan,
atof(cmdConnection->fromLabviewValue));
status = cmdConnection->mpxGet(MPXVAR_THSTOP, Labview_DEFAULT_TIMEOUT);
if (status == asynSuccess)
setDoubleParam(merlinStopThresholdScan,
atof(cmdConnection->fromLabviewValue));
status = cmdConnection->mpxGet(MPXVAR_THSSCAN, Labview_DEFAULT_TIMEOUT);
if (status == asynSuccess)
setIntegerParam(merlinThresholdScan,
atoi(cmdConnection->fromLabviewValue));
return status;
}
static void merlinTaskC(void *drvPvt)
{
merlinDetector *pPvt = (merlinDetector *) drvPvt;
pPvt->merlinTask();
}
static void merlinStatusC(void *drvPvt)
{
merlinDetector *pPvt = (merlinDetector *) drvPvt;
pPvt->merlinStatus();
}
/** This thread periodically read the detector status (temperature, humidity, etc.)
It does not run if we are acquiring data, to avoid polling Labview when taking data.*/
void merlinDetector::merlinStatus()
{
int result = asynSuccess;
int status = 0;
int statusCode;
// let the startup script complete before attempting I/O
epicsThreadSleep(4);
startingUp = 0;
this->lock();
// make sure important grouped variables are set to agree with
// IOCs auto saved values
setAcquireParams();
setROI();
updateThresholdScanParms();
getThreshold();
result = cmdConnection->mpxGet(MPXVAR_GETSOFTWAREVERSION,
Labview_DEFAULT_TIMEOUT);
statusCode = atoi(cmdConnection->fromLabviewValue);
// initial status
setIntegerParam(ADStatus, ADStatusIdle);
this->unlock();
while (1)
{
epicsThreadSleep(4);
this->lock();
getIntegerParam(ADStatus, &status);
if (status == ADStatusIdle)
{
setStringParam(ADStatusMessage, "Waiting for acquire command");
callParamCallbacks();
}
this->unlock();
}
}
/** sets one of the 6 modes for Merlin versions since Quad Merlin
* these modes combine sensible combinations of 5 individual settings
* on the device
*
* \param[in] mode the mode number
*/
asynStatus merlinDetector::SetQuadMode(int mode)
{
char value[MPX_MAXLINE];
asynStatus result = asynSuccess;
int bits = 12;
int colourMode = 0;
int enableCounter1 = 0;
int continuousRW = 0;
int chargeSumming = 0;