File: Quad.cpp

package info (click to toggle)
libapogee3 3.2%2B20221221183454-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,284 kB
  • sloc: cpp: 26,737; sh: 8; makefile: 3
file content (394 lines) | stat: -rw-r--r-- 10,343 bytes parent folder | download | duplicates (2)
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
/*! 
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright(c) 2011 Apogee Imaging Systems, Inc. 
* \class Quad
* \brief camera class for f4320 for Quad
* 
*/ 

#include "Quad.h" 
#include "AscentData.h"
#include "AscentBasedIo.h" 
#include "apgHelper.h" 
#include "CamHelpers.h" 
#include "CamGen2ModeFsm.h" 
#include "CamGen2CcdAcqParams.h" 
#include "ApnCamData.h"
#include "ImgFix.h" 
#include "ApgLogger.h" 
#include "indimacros.h"

#include <sstream>

//////////////////////////// 
// CTOR 
Quad::Quad( ) :   CamGen2Base( CamModel::QUAD ),
                            m_fileName( __BASE_FILE__ ),
                            m_DoPixelReorder( true )
{   
    //alloc and set the camera constants
    m_CameraConsts = std::shared_ptr<PlatformData>( new AscentData() );
}


//////////////////////////// 
// DTOR 
Quad::~Quad() 
{ 

    // trying to leave the camera in a good imaging state
    // from Ticket #111 in the Alta project and ticket #87 in Zenith
    if( m_IsConnected )
    {
        try
        {
            CloseConnection();
        }
        catch( std::exception & err )
        {
            std::string msg ("Exception caught in ~Quad msg = " );
            msg.append( err.what() );
            ApgLogger::Instance().Write(ApgLogger::LEVEL_RELEASE,"error",
                msg);
        }
        catch( ... )
        {
            ApgLogger::Instance().Write(ApgLogger::LEVEL_RELEASE,"error",
            "Unknown exception caught stopping exposure in ~Quad" );
        }
    }

} 

//////////////////////////// 
// OPEN     CONNECTION
void Quad::OpenConnection( const std::string & ioType,
             const std::string & DeviceAddr,
             const uint16_t FirmwareRev,
             const uint16_t Id )
{
#ifdef DEBUGGING_CAMERA
   apgHelper::DebugMsg( "Quad::OpenConnection -> ioType= %s, DeviceAddr = %s, FW = %d, ID = %d", 
       ioType.c_str(), DeviceAddr.c_str(), FirmwareRev, Id  );
#endif
     //create the camera interface
    CreateCamIo( ioType, DeviceAddr );

    // save the input data
    m_FirmwareVersion =  FirmwareRev;
    m_Id = Id;

    //make the input id and firmware rev
    //match the camera we just connected to
    VerifyFrmwrRev();

    VerifyCamId();

    //create the ccd specific object
    CfgCamFromId( m_Id );

    // overwrite cfg matrix data with
    // information from the camera
    UpdateCfgWithStrDbInfo();

    //set the camera mode fsm
    m_CamMode = std::shared_ptr<ModeFsm>( new CamGen2ModeFsm(m_CamIo,
        m_CamCfgData, m_FirmwareVersion) );

    //create the adc and pattern file handler object
    m_CcdAcqSettings = std::shared_ptr<CcdAcqParams>( 
        new CamGen2CcdAcqParams(m_CamCfgData,m_CamIo,m_CameraConsts) );

    m_IsConnected = true;
    LogConnectAndDisconnect( true );

}

//////////////////////////// 
//      CLOSE        CONNECTION
void Quad::CloseConnection()
{
#ifdef DEBUGGING_CAMERA
    apgHelper::DebugMsg( "Quad::DefaultCloseConnection");
#endif

    DefaultCloseConnection();
}

//////////////////////////// 
//      UPDATE     CFG        WITH       STR    DB       INFO
void Quad::UpdateCfgWithStrDbInfo()
{
    CamInfo::StrDb infoStruct = std::dynamic_pointer_cast<AscentBasedIo>(
        m_CamIo)->ReadStrDatabase();

    if( 0 != infoStruct.Ad1Gain.compare("Not Set") )
    {
        std::stringstream ss( infoStruct.Ad1Gain );
        ss >> m_CamCfgData->m_MetaData.DefaultGainLeft;
    }

    if( 0 != infoStruct.Ad1Offset.compare("Not Set") )
    {
        std::stringstream ss( infoStruct.Ad1Offset );
        ss >> m_CamCfgData->m_MetaData.DefaultOffsetLeft;
    }

    if( 0 != infoStruct.Ad2Gain.compare("Not Set") )
    {
        std::stringstream ss( infoStruct.Ad2Gain );
        ss >> m_CamCfgData->m_MetaData.DefaultGainRight;
    }

    if( 0 != infoStruct.Ad2Offset.compare("Not Set") )
    {
        std::stringstream ss( infoStruct.Ad2Offset );
        ss >> m_CamCfgData->m_MetaData.DefaultOffsetRight;
    }

}

//////////////////////////// 
// CFG      CAM       FROM  ID
void Quad::CfgCamFromId( const uint16_t CameraId )
{
     //create and set the camera's cfg data
    DefaultCfgCamFromId( CameraId );
}


//////////////////////////// 
// CREATE      CAMIO        
void Quad::CreateCamIo(const std::string & ioType,
     const std::string & DeviceAddr)
{
        
    CamModel::InterfaceType type = InterfaceHelper::DetermineInterfaceType( ioType );

    m_CamIo = std::shared_ptr<CameraIo>( new AscentBasedIo( type,DeviceAddr ) );
   

    if( !m_CamIo )
    {
        std::string errStr("failed to create a camera interface io object");
        apgHelper::throwRuntimeException( m_fileName, errStr, __LINE__,
            Apg::ErrorType_Connection );
    }



}

//////////////////////////// 
// VERIFY        CAM        ID 
void Quad::VerifyCamId()
{
    const uint16_t id = m_CamIo->GetId();

    if( id != m_Id )
    {
        std::stringstream msg;
        msg << "Error: Expected camera id of " << m_Id << ". Read from camera id of " << id;
        apgHelper::throwRuntimeException( m_fileName, msg.str(), 
            __LINE__, Apg::ErrorType_Connection );
    }

}

//////////////////////////// 
//      FIX      IMG        FROM          CAMERA
void Quad::FixImgFromCamera( const std::vector<uint16_t> & data,
                                            std::vector<uint16_t> & out,  const int32_t rows, 
                                            const int32_t cols)
{
    int32_t offset = 0; 

    switch( m_CamCfgData->m_MetaData.NumAdOutputs )
    {
        case 1:
            offset = m_CcdAcqSettings->GetPixelShift();
            ImgFix::SingleOuputCopy( data, out, rows, cols, offset );
        break;

        case 4:
        {
            uint16_t r =0, c=0;
            ExposureAndGetImgRC( r, c );
            offset = c - cols;
            if( m_DoPixelReorder )
            {
                ImgFix::QuadOuputFix( data, out, rows, cols, offset );
            }
            else
            {
                ImgFix::QuadOuputCopy( data, out, rows, cols, offset );
            }
        }
        break;

        default:
        {
            std::stringstream msg;
            msg << "Invaild number of ad outputs = " << m_CamCfgData->m_MetaData.NumAdOutputs;
            apgHelper::throwRuntimeException( m_fileName, msg.str(), 
                __LINE__, Apg::ErrorType_InvalidUsage );
        }
        break;
    }
    
}


//////////////////////////// 
//      START        EXPOSURE
void Quad::StartExposure( const double Duration, const bool IsLight )
{
    //verify the roi is valid
    const uint16_t maxImgRows =  GetMaxImgRows() / GetRoiBinRow();
    if( !IsRoiCenteredAndSymmetric( maxImgRows, 
                  GetRoiStartRow(),  
                  GetRoiNumRows() ) )
    {

        std::stringstream msg;
        msg << "Invaild roi: ";
        msg << "ccd imaging rows = " << maxImgRows;
        msg << "; start row = " << GetRoiStartRow();
        msg << "; # roi rows = " << GetRoiNumRows();
        apgHelper::throwRuntimeException( m_fileName, msg.str(), 
            __LINE__, Apg::ErrorType_InvalidUsage );

    }

    const uint16_t maxImgCols =  GetMaxImgCols() / GetRoiBinCol();
     if( !IsRoiCenteredAndSymmetric( maxImgCols , 
                  GetRoiStartCol(),  
                  GetRoiNumCols() ) )
    {

        std::stringstream msg;
        msg << "Invaild roi: ";
        msg << "ccd imaging cols = " << maxImgCols;
        msg << "; start col = " << GetRoiStartCol();
        msg << "; # roi cols = " << GetRoiNumCols();
        apgHelper::throwRuntimeException( m_fileName, msg.str(), 
            __LINE__, Apg::ErrorType_InvalidUsage );
    }

    // start expsoure
    DefaultStartExposure( Duration, IsLight, true );
}


//////////////////////////// 
//      IS     ROI     CENTERED        AND        SYMMETRIC
bool Quad::IsRoiCenteredAndSymmetric( const uint16_t ccdLen, 
                      const uint16_t startingPos,  
                      const uint16_t roiLen )
{
    const int32_t CENTER = ccdLen / 2;

    const int32_t START_DIFF = startingPos - CENTER;

    if( START_DIFF >= 0 )
    {
        return false;
    }

    const int32_t END_POS = startingPos + roiLen;

    const int32_t END_DIFF = CENTER - END_POS;

    if( END_DIFF >= 0 )
    {
        return false;
    }

    if( END_DIFF != START_DIFF )
    {
        return false;
    }

    return true;
}

//////////////////////////// 
// EXPOSURE    AND     GET        IMG     RC
void Quad::ExposureAndGetImgRC(uint16_t & r, uint16_t & c)
{
#ifdef DEBUGGING_CAMERA
    apgHelper::DebugMsg( "Quad::ExposureAndGetImgRC" );
#endif

    //detemine the exposure height
    r = m_CcdAcqSettings->GetRoiNumRows();

    //detemine the exposure width
    c = m_CcdAcqSettings->GetRoiNumCols() + (m_CcdAcqSettings->GetPixelShift()*2 );

}

//////////////////////////// 
//  GET    NUM        ADC        CHANNELS
int32_t Quad::GetNumAdChannels() 
{ 
#ifdef DEBUGGING_CAMERA
    apgHelper::DebugMsg( "Quad::GetNumAdChannels" );
#endif
    return 2; 
}  

//////////////////////////// 
//      SET     IS    QUAD     BIT
void Quad::SetIsQuadBit()
{
 #ifdef DEBUGGING_CAMERA
    apgHelper::DebugMsg( "Quad::SetIsQuadBit" );
#endif
    //set high this is a quad camera
    m_CamIo->ReadOrWriteReg( CameraRegs::OP_C,
        CameraRegs::OP_C_IS_QUAD_BIT );
}

//////////////////////////// 
//      INIT
void Quad::Init()
{
 #ifdef DEBUGGING_CAMERA
    apgHelper::DebugMsg( "Quad::Init" );
#endif

    DefaultInit();
	SetIsQuadBit();
}

//////////////////////////// 
//  SET      FAN       MODE
void Quad::SetFanMode( const Apg::FanMode mode, const bool PreCondCheck )
{
#ifdef DEBUGGING_CAMERA
    apgHelper::DebugMsg( "Quad::SetFanMode -> mode = %d, PreCondCheck =%d ", 
        mode, PreCondCheck );
#else
    INDI_UNUSED(mode);
    INDI_UNUSED(PreCondCheck);
#endif
    // no op on purpose, no fan control for v108 firmware and hic cameras
    // just ignore request...

}

//////////////////////////// 
//  GET      FAN       MODE
Apg::FanMode Quad::GetFanMode()
{
#ifdef DEBUGGING_CAMERA
    apgHelper::DebugMsg( "Quad::GetFanMode" );
#endif

    // no fan control on ascent based cameras
    return Apg::FanMode_Off;
}