~vbursian/research-assistant/intervers

« back to all changes in this revision

Viewing changes to RAPlugins/CCDLomo/LomoCCDDriver.cpp

  • Committer: Viktor Bursian at blin-ubuntu
  • Date: 2017-04-07 20:48:57 UTC
  • mfrom: (8.6.2 DN)
  • mto: (8.9.1 DIST)
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: vbursian@gmail.com-20170407204857-787xdycffm8enff0
CCD devices is rewritten + some other changes in DN devices

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
- Uses  RAGUI - Research Assistant Graphical User Interface Library.
5
5
- Uses  Qt v.5.0.2  - http://qt-project.org/
6
6
- Uses  RANet - Research Assistant Net Library.
7
 
- Copyright(C) 2010-2014, Dmitrii Nelson, St.Petersburg, Russia.
 
7
- Copyright(C) 2010-2017, Dmitrii Nelson, St.Petersburg, Russia.
8
8
                          d.nelson@mail.ioffe.ru
9
9
*///////////////////////////////////////////////////////////////////////////////
10
10
#include "LomoCCDDriver.h"
17
17
 
18
18
DEFINE_CLASS_TAG(sLomoCCDDriver)
19
19
 
20
 
csCommand                     sLomoCCDDriver::cStart;
21
 
csCommand                     sLomoCCDDriver::cStop;
22
 
const sDirective<int>  sLomoCCDDriver::cSetIntegrationTime;
23
 
 
24
20
//---------------------------------------------------- sLomoCCDDriver ----------
25
21
 
26
22
void  sLomoCCDDriver::Preconfigure (sNodePtr  DeviceCfg)
30
26
  DeviceSettings = DeviceCfg >> "Settings";
31
27
  if( DeviceSettings.IsCorrect() ){
32
28
    //! @todo{Instruments} Why SetAttr?
 
29
    DeviceSettings |= attr("NumberOfPixels",3648);
33
30
    DeviceSettings |= attr("CCD_SH",64);
34
31
    DeviceSettings |= attr("CCD_ICG",64);
35
 
    DeviceSettings |= attr("IntegrationTime",1);
 
32
    DeviceSettings |= attr("MiddlePixel",1733);
 
33
    DeviceSettings |= attr("WL_0",sPhysValue(0.02389,"nm"));
 
34
    DeviceSettings |= attr("WL_k",sPhysValue(-7.8e-6,_Unitsless_));
36
35
    DeviceSettings |= attr("CCDDeviceName","CCD Utils");
37
36
  };
38
 
  sDriver::Preconfigure(DeviceCfg);
 
37
  sTunableCCDDriver::Preconfigure(DeviceCfg);
 
38
}
 
39
 
 
40
sLomoCCDDriver::~sLomoCCDDriver ()
 
41
{
39
42
}
40
43
 
41
44
sLomoCCDDriver::sLomoCCDDriver (sString   DevicetId
42
45
                               ,sNodePtr  DeviceCfg)
43
 
    :sDriver(DevicetId,DeviceCfg)
 
46
  :sTunableCCDDriver(DevicetId,DeviceCfg)
 
47
  ,WL_0(sPhysValue(1.0,"nm"))
 
48
  ,WL_k(sPhysValue(1,_Unitsless_))
44
49
{
45
50
}
46
51
 
47
52
 
48
53
void  sLomoCCDDriver::PostConstructor ()
49
54
{
50
 
  sDriver::PostConstructor();
 
55
  sTunableCCDDriver::PostConstructor();
51
56
  try{ //get config parameters
52
57
    get_from<sIntegerNode>(DeviceSettings >> "CCD_SH"   ,CCD_SH);
53
58
    get_from<sIntegerNode>(DeviceSettings >> "CCD_ICG"   ,CCD_ICG);
54
 
    get_from<sIntegerNode>(DeviceSettings >> "IntegrationTime"   ,IntegrationTime);
55
59
    get_from<sTextNode>(DeviceSettings >> "CCDDeviceName" ,CCDDeviceName);
 
60
    get_from<sPhysValueNode>(DeviceSettings >> "WL_0" ,WL_0);
 
61
    get_from<sPhysValueNode>(DeviceSettings >> "WL_k" ,WL_k);
56
62
  }catch(...){
57
63
    SetInvalid(tr("Something wrong in configuration."));
58
64
  }
59
 
  //evolution of the inherited state machine (behaviour)
60
 
  {
61
 
    { // evolution of the State_Warming
62
 
      State_W_Connect = new sState(State_Warming());
63
 
      State_W_Connect
64
 
          ->SetAsInitial()
65
 
          ->OnEnterCall( SLOT(ConnectUSBDevice()) )
66
 
          ->AddTransition(SIGNAL(Connected()),State_Warming()->Fin())
67
 
          ->AddTransition(SIGNAL(ErrorMessage()),State_Cooling());
68
 
    }
69
 
      // evolution of the State_Operating
70
 
    {
71
 
      State_O_Stopped = new sState(State_Operating());
72
 
      State_O_Measuring = new sState(State_Operating(),1);
73
 
      State_O_GetMeasured = new sState(State_Operating());
74
 
      State_O_Stopped
75
 
          ->SetAsInitial()
76
 
          ->AddTransition(cStart,SLOT(StartCCD ()) ,State_O_Measuring)
77
 
          ->AddReaction<int>(cSetIntegrationTime
78
 
                                   ,SLOT(SetCCD_IntegrTime(int)));
79
 
      State_O_Measuring
80
 
          ->AddTransition(State_O_Measuring,SIGNAL(Timeout())
81
 
                         ,State_O_GetMeasured)
82
 
          ->AddTransition(cStop,SLOT(StopCCD ()) ,State_O_Stopped);
83
 
      State_O_GetMeasured
84
 
          ->OnEnterCall( SLOT(GetCCD_Data()) )
85
 
          ->AddTransition(State_O_Measuring);
86
 
    };
87
 
  };
88
65
}
89
66
 
90
67
void  sLomoCCDDriver::ConnectUSBDevice ()
147
124
    }
148
125
}
149
126
 
 
127
void  sLomoCCDDriver::CalculateWavelengths ()
 
128
{
 
129
  sPhysValue dWL = WL_0 + WL_k * WLPosition;
 
130
  sPhysValue LeftPos = WLPosition - dWL*MiddlePixel;
 
131
  sPhysValue RightPos = WLPosition + dWL*((NumOfPixels -1) - MiddlePixel);
 
132
  real dX_0 = ((WL_0 + WL_k * LeftPos)/(sPhysValue(1.0,"nm"))).ToReal();
 
133
  real dX_1 = ((WL_0 + WL_k * RightPos)/(sPhysValue(1.0,"nm"))).ToReal();
 
134
  real dX = (dWL/(sPhysValue(1.0,"nm"))).ToReal(); // in "nm/pixel"
 
135
  real d2X = (dX_1 - dX_0)/NumOfPixels;
 
136
  real  XM = (WLPosition/(sPhysValue(1.0,"nm"))).ToReal(); // in "nm"
 
137
  for (int i = 0; i < NumOfPixels; i++) {
 
138
    Wavelengths.ArrBody[i] = XM + (dX + d2X *(i - MiddlePixel))*(i - MiddlePixel);
 
139
  }
 
140
  emit NewWavelengths (Wavelengths);
 
141
}
 
142
 
150
143
void  sLomoCCDDriver::StartCCD ()
151
144
{
152
 
//    for (int i = 0; i < 10000;i++ ){
153
 
//        TestReading[i] = 0;
154
 
//    }
155
 
//    TestReadCount = 0;
156
 
 
157
145
    BYTE byteToSend = 0x80;
158
146
    ResetTriadeBuffer();
159
147
    PointNo = 0;
160
 
    ReadingError_I_No = 0;
161
 
    ReadingError_II_No = 0;
162
 
    ReadingError_III_No = 0;
163
 
    ReadingError_IV_No = 0;
164
 
    ReadingError_V_No = 0;
165
148
    FT_STATUS CCDDeviceStatus;
166
149
    CCDDeviceStatus = FT_Purge(CCDDeviceHandle,FT_PURGE_RX | FT_PURGE_TX);
167
150
    if(CCDDeviceStatus != FT_OK){
172
155
    CCDDeviceStatus = FT_Write(CCDDeviceHandle,&byteToSend,1,&bytesWritten);
173
156
    if(CCDDeviceStatus != FT_OK)
174
157
        emit ErrorMessage(FormCCDErrorMessage(CCDDeviceStatus));
 
158
    State_O_Measuring->SetTimeout(1);
175
159
}
176
160
 
177
161
void  sLomoCCDDriver::StopCCD ()
182
166
    CCDDeviceStatus = FT_Write(CCDDeviceHandle,&byteToSend,1,&bytesWritten);
183
167
    if(CCDDeviceStatus != FT_OK)
184
168
        emit ErrorMessage(FormCCDErrorMessage(CCDDeviceStatus));
185
 
    qDebug() << "ReadingError_I_No " << ReadingError_I_No;
186
 
    qDebug() << "ReadingError_II_No " << ReadingError_II_No;
187
 
    qDebug() << "ReadingError_III_No " << ReadingError_III_No;
188
 
    qDebug() << "ReadingError_IV_No " << ReadingError_IV_No;
189
 
    qDebug() << "ReadingError_V_No " << ReadingError_V_No;
 
169
    WLDriver.ReleaseExclusiveControl();
190
170
}
191
171
 
192
 
void  sLomoCCDDriver::SetCCD_IntegrTime (int IntegrTime)
 
172
void  sLomoCCDDriver::SetCCD_Parameters (sCCDDriver::sParameters Params)
193
173
{
194
 
    if (IntegrTime > 65535) IntegrationTime = 65535;
195
 
    else if (IntegrTime < 1) IntegrationTime = 1;
196
 
    else IntegrationTime = IntegrTime;
197
 
    if(SendParam(0xC0,IntegrationTime))
198
 
        emit ITimeSet();
 
174
  Parameters = Params;
 
175
  if( ! WLDriver.TakeExclusiveControl() ){
 
176
    emit IsBusy();
 
177
  }else{
 
178
    WORD _ITime = 1;
 
179
    if (Parameters.IntegrationTime/10 > 65535) _ITime = 65535;
 
180
    else if (Parameters.IntegrationTime/10 < 1) _ITime = 1;
 
181
    else _ITime = Parameters.IntegrationTime/10;
 
182
    if(SendParam(0xC0,_ITime))
 
183
      emit ParamsSet();
 
184
  }
199
185
}
200
186
 
201
187
QString  sLomoCCDDriver::FormCCDErrorMessage(FT_STATUS CCDStatus)
277
263
    return true;
278
264
}
279
265
 
280
 
/* -------- Этот вариант мой - работает хреново ---------
281
 
void sLomoCCDDriver::AddByte(BYTE DataByte)
282
 
{
283
 
    TriadeBuffer[NumInTriade ++] = DataByte;
284
 
    if((DataByte & 0x80) == 0) {     //Промежуточный байт
285
 
        if(NumInTriade == 3) {       // Лишний байт
286
 
            TriadeBuffer[0] = TriadeBuffer[1];
287
 
            TriadeBuffer[1] = TriadeBuffer[2];
288
 
            NumInTriade = 2;
289
 
            ReadingError_I_No++;  // Счетчик ошибок +1
290
 
        }
291
 
    } else {                      //Командный байт
292
 
        if(NumInTriade < 2) {     // Не хватает байтов данных
293
 
            TriadeBuffer[0] = 1;  // Заполняем их единицами
294
 
            TriadeBuffer[1] = 1;  // Оба для определенности
295
 
            ReadingError_II_No++;     // Счетчик ошибок +1
296
 
        }
297
 
        NumInTriade = 0;
298
 
        BYTE CommandCode = DataByte & 0xF0;
299
 
        if(CommandCode == FRAME_START) { // Заполняем первую точку
300
 
            PointNo = 0;
301
 
            DataArray.ArrBody[PointNo ++] = ConvertTriadeBuffer();
302
 
        } else if(CommandCode == FRAME_MIDDLE) {
303
 
            if(PointNo == PIXELSNUM - 1) { // Сбой в числе точек -
304
 
                PointNo = 0;               // бракуем весь кадр
305
 
                NumInTriade = 0;
306
 
                ReadingError_III_No++;  // Счетчик ошибок +1
307
 
            }else if(PointNo == 0) { // Середина вместо начала -
308
 
                NumInTriade = 0;     // бракуем весь кадр
309
 
                ReadingError_IV_No++;  // Счетчик ошибок +1
310
 
            }else { // Все в порядке - добавляем точку
311
 
                DataArray.ArrBody[PointNo ++] = ConvertTriadeBuffer();
312
 
                NumInTriade = 0;
313
 
            }
314
 
        } else if(CommandCode == FRAME_END) {
315
 
            if(PointNo == PIXELSNUM - 1) { //Конец фрейма
316
 
                DataArray.ArrBody[PointNo ++] = ConvertTriadeBuffer();
317
 
                NumInTriade = 0;
318
 
                PointNo = 0;
319
 
                emit NewData(DataArray);
320
 
            } else {             // Сбой в числе точек -
321
 
                PointNo = 0;     // бракуем весь кадр
322
 
                NumInTriade = 0;
323
 
                ReadingError_V_No++;  // Счетчик ошибок +1
324
 
            }
325
 
         } else {            // Пришло непонятно что -
326
 
            PointNo = 0;     // бракуем весь кадр
327
 
            NumInTriade = 0;
328
 
            ReadingError_VI_No++;  // Счетчик ошибок +1
329
 
         };
330
 
    }
331
 
}
332
 
-------------------------------------------------------------------*/
333
 
 
334
266
// ------ Это взято у ЛОМО и слегка модифицировано -----------------
335
267
 
336
268
void  sLomoCCDDriver::AddByte(BYTE  DataByte )
337
269
{
338
 
//   if (TestReadCount < 10000) TestReading[TestReadCount ++] = DataByte;
339
 
 
340
270
   TriadeBuffer[NumInTriade ++] =  DataByte ;
341
271
   if(( DataByte & 0x80) == 0) {    //Промежуточный байт
342
272
      if(NumInTriade == 3) {        // Лишний байт
343
273
         TriadeBuffer[0] = TriadeBuffer[1];
344
274
         TriadeBuffer[1] = TriadeBuffer[2];
345
275
         NumInTriade = 2;
346
 
         ReadingError_I_No++;   // Счетчик ошибок +1
347
276
      }
348
277
      return;
349
278
   }else {                       //Командный байт
351
280
         NumInTriade = 0;
352
281
         PointNo = 0;
353
282
         DataArray.Reset();
354
 
         ReadingError_II_No++;   // Счетчик ошибок +1
355
283
         return;
356
284
      }
357
285
      BYTE CommandCode =  DataByte & 0xF0;
364
292
         NumInTriade = 0;
365
293
         return;
366
294
      }else if(CommandCode == FRAME_MIDDLE) {
367
 
         if(PointNo == PIXELSNUM - 1) {
 
295
         if(PointNo == NumOfPixels - 1) {
368
296
            DataArray.Reset();
369
297
            PointNo = 0;
370
298
            NumInTriade = 0;
371
 
            ReadingError_III_No++; // Счетчик ошибок +1
372
299
            return;
373
300
         }else if(PointNo == 0) {
374
301
            NumInTriade = 0;
379
306
            return;
380
307
         }
381
308
      }else if(CommandCode == FRAME_END) {
382
 
         if(PointNo == PIXELSNUM - 1) { //Конец фрейма
 
309
         if(PointNo == NumOfPixels - 1) { //Конец фрейма
383
310
            DataArray.ArrBody[PointNo ++] = ConvertTriadeBuffer();
384
311
            NumInTriade = 0;
385
312
            PointNo = 0;
393
320
            NumInTriade = 0;
394
321
            DataArray.Reset();
395
322
            PointNo = 0;
396
 
            ReadingError_IV_No++;
397
323
            return;
398
324
         }
399
325
      }else {
400
 
         ReadingError_V_No ++;
401
326
         NumInTriade = 0;
402
327
         DataArray.Reset();
403
328
         PointNo = 0;