CameraHandler.h 7.57 KB
Newer Older
d11's avatar
no    
d11 committed
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
#pragma once

#include <thread>
#include <vector>
#include <mutex>
#include <list>

#define _GNULINUX
#include </usr/local/src/baumer/inc/bgapi2_genicam/bgapi2_genicam.hpp>
#include "../../../../../NomadSpecialModules/src/drivers/gigecam/photonics/DoubleBufferHandler.h"

// the class represents a BGAPI camera object and additional camera specific information
class CCameraControl {
public:
    // this function activates the feature command 
    // the feature command use the ExposureTime feature of the camera
    // the parameter passes the new exposure time to be written to the camera
    void StartFeatureCommandExposure(double exposure_time);

    // this function activates the capture command 
    // the parameter passes the number of images to be captured
    void StartCaptureCommand(unsigned int images);

    // this function checks, if the capture command was activated
    // in this case the capture command is automatically reset
    bool IsCommandCaptureActivated();

    // this function checks, if the feature command was activated
    // in this case the feature command is automatically reset
    bool IsCommandExposureActivated();

    // this function writes the desired exposure time to the passed camera
    bool ExposureFeatureBGAPI();

    // this function makes all necessary preparations on the buffer management to be able to capture images with BGAPI
    // it opens the data stream and creates the camera buffers
    void InitializeBGAPIBufferManagement();

    // this function clears the buffer management and
    // deletes the camera buffers
    void DeinitializeBGAPIBufferManagement();

    // this function capture the desired number of images from the passed camera
    // and store some additional information (BufferInformation struct)
    bool CaptureBGAPIImages(const bool * abort_flag, unsigned int number_of_images);

    // this function starts the streaming for the passed camera
    bool StartStreamingBGAPI();

    // this function stops the streaming for the passed camera
    void StopStreamingBGAPI();

    // this function fill the BufferInformation struct
    void FillBufferInformation( BGAPI2::DataStream* datastream_pointer, BGAPI2::Buffer *buffer);

    // function to add a string with logging information to the logging string list
    void LoggingStringAdd(std::string logging_message);

    // this function returns a logging string and remove this string from the logging string list
    std::string LoggingString();

    // function to check, if some logging information is available
    bool LoggingListEmpty() { return (logging_list_.size() == 0); }

public:
    // buffer exchange strategy (double buffer)
    CDoubleBufferHandler buffer_handler_;
    // BGAPI camera object
    BGAPI2::Device* camera_pointer_;
    // a list to transfer massages, generated by the feature command
    std::list<std::string> feature_command_message_list_;
    // a mutex to synchronize the access to feature_command_message_list_
    std::mutex feature_command_message_list_lock_;
    // a worker thread that captures images from BGAPI
    std::thread capture_thread_;
    // a worker thread that control camera features from BGAPI
    std::thread feature_thread_;
    // a flag which controls the capture command
    bool command_capture_;
    // a flag which shows if the capture command is active
    bool capture_active_;
    // number of images to capture
    unsigned int number_of_images_;
    // number of currently captured images
    unsigned int number_of_captured_images_;
    // number of incomplete images
    unsigned int number_of_incomplete_images_;
    // a flag which controls the feature command
    bool command_feature_;
    // exposure time to be set
    double exposure_time_;
private:
    // mutex to synchronize the access to capture command flag of all cameras
    std::mutex capture_command_lock_;
    // mutex to synchronize the access to feature command flag of all cameras
    std::mutex feature_command_lock_;
    // a list which holds logging strings
    std::list<std::string> logging_list_;
    // a mutex to synchronize the access to the logging list
    std::mutex logging_list_lock_;
};

// the class encapsulate camera search, capture command and feature command
// it also implements the worker threads
class CCameraHandler
{
// ----
// public functions which are used in the context of the main thread and application threads
// to control the main settings from the user application
// ----
public:
    // construction / destruction
    CCameraHandler();
    virtual ~CCameraHandler();

    // this function starts the init thread to search for connected cameras
    // it also passed the buffer handler
    void Start(CDoubleBufferHandler buffer_handler);

    // close all connected cameras and frees all BGAPI resources
    // this function doesn't run in a separate thread
    void Stop();

    // this function initialize the buffer management of all connected cameras
    // it also creates the struct BufferInformation for every camera buffer
    // all cameras use 10 buffers to store the image data
    // the memory for the camera buffers will be allocated inside BGAPI
    // the size of the memory to allocated will determined from the maximum image size
    // this function also starts the worker threads
    void StartCameras();

    // this function stops the worker threads
    // and deinitialize the BGAPI
    void StopCameras();

    // function to check for an initialisation error
    bool IsInitOK();

    // returns the number of connected cameras
    size_t NumberOfCameras();

    // this function returns the number of images taken by the camera that took the least number of images
    unsigned int CapturedImages();

    // function to return the camera control vector
    const std::vector<CCameraControl*>* GetCameraControlVector() { return &camera_control_; }

    // this function returns a logging string and remove this string from the logging string list
    std::string LoggingString();

    // function to check, if some logging information is available
    bool LoggingListEmpty() { return (logging_list_.size() == 0); }

// ----
// public functions which are partly used in the worker threads
// to handle and process the code regarding to main settings
// ----
public:
    // this function initializes the Baumer GAPI SDK and
    // searches for connected cameras
    // all found cameras will be opened to work with
    void InitializeBGAPI();

    // this function deinitialises the Baumer GAPI SDK and
    // close all connected cameras
    void DeinitializeBGAPI();

    // function to signal an initialisation error while start up
    void SetInitError();

    // function to add a string with logging information to the logging string list
    void LoggingStringAdd(std::string logging_message);

    // this function adds a camera object to the camera control vector
    void AddCamera(BGAPI2::Device* camera_pointer);

    // this function set a flag to signal the worker thread to exit
    const bool* FinishWorkerThreads() { return &finish_worker_threads_; }
private:
    // pointer to the buffer handler
    CDoubleBufferHandler buffer_handler_;
    // flag to signal the worker threads to exit themselves
    bool finish_worker_threads_;
    // initialisation thread
    std::thread init_thread;
    // camera control vector holds information regarding synchronisation and
    // the BGAPI camera object
    std::vector<CCameraControl*> camera_control_;
    // flag is set for initialisation errors
    bool init_ok_;
    // a list which holds logging strings
    std::list<std::string> logging_list_;
    // a mutex to synchronize the access to the logging list
    std::mutex logging_list_lock_;
};