summaryrefslogtreecommitdiff
path: root/src/camera/veyeimx287m.h
blob: bdc57d9e10f6b1052e17ea1f2b668f338280ea6d (plain)
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
#pragma once

// c/cpp
#include <cstdint>
#include <linux/videodev2.h>
#include <queue>
#include <thread>

// qt
#include <QQueue>
#include <QReadWriteLock>
#include <QSemaphore>

// orpheus
#include "constants.h"
#include "icamera.h"
#include "image.h"
// TODO: remove this include
#include "printerclient.h"
#include "utils/sem_queue.h"

namespace veye {
namespace imx287m {
class i2c;
} // namespace imx287m
} // namespace veye

class HttpServer;
class IStand;

/*
 * start calibration
 * - send move zRange/16384 mm
 * - wait StepDelay, ignore all pixels
 * - collect 10 (64?) profiles
 * - dump profiles
 */

/*!
 * \brief The VeyeIMX287m class - control and receive data from VEYE IMX287
 * camera
 * \todo
 * - implement `trgnum` - The number of image frames output by one trigger signal
 * in trigger mode. Might be helpful in calibration mode
 * - implement `trgfilter_enable` - for rising/falling edge support
 * - implement `trgexp_delay` - Exposure delay, i.e. the time to turn on the
 * Strobe signal in advance. The difference between trgexp_delay and trgdelay,
 * see manual for details. (which manual?)
 * - implement UDP streams, http is a bit slow even over 1GbE
 * - implement roi
 * - blcmode/blacklevel? (does it work in this camera?)
 */
class VeyeIMX287m : public ICamera
{
    constexpr static char videoDevice[] = "/dev/video0";

public:
    using buffer_t = std::array<uint8_t, img_size>;

public:
    VeyeIMX287m();
    ~VeyeIMX287m() override;

public slots:
    void onMoveFinished() override;

public:
    static std::vector<std::shared_ptr<ICamera>> search();

public:
    bool startStream() override;

    void startCalibration(std::shared_ptr<IStand> stand, double zRangeMm) override;

    bool dequeueImageBuffer(size_t &image);
    std::shared_ptr<Image> getImage() override;

    bool init();

    // parameters
public:
    [[nodiscard]] bool set_autoExposure(const bool enable) override;
    [[nodiscard]] std::optional<bool> get_autoExposure() override;

    [[nodiscard]] bool set_autoGain(const bool enable) override;
    [[nodiscard]] std::optional<bool> get_autoGain() override;

    [[nodiscard]] bool set_exposureTime(const std::chrono::microseconds us) override;
    [[nodiscard]] std::optional<const std::chrono::microseconds> get_exposureTime() override;

    [[nodiscard]] bool set_gain(const float value) override;
    [[nodiscard]] std::optional<float> get_gain() override;

    [[nodiscard]] bool set_triggerExposureDelay(const std::chrono::microseconds us) override;
    [[nodiscard]] std::optional<const std::chrono::microseconds> get_triggerExposureDelay() override;

public:
    /*!
     * \brief processedCounter - count of images processed in current second.
     * Used for performance measurement and bottlenecks analysing
     */
    uint32_t processedCounter{0};

private:
    bool openCam();
    bool initCam();

    bool initI2C();
    // bool initHttpServer();

    void getFrameLoop(std::stop_token stopToken);
    void rotateFrameLoop(std::stop_token stopToken);
    void calcPixelsLoop(std::stop_token stopToken);

private:
    /*!
     * \brief m_cam_fd - camera file descriptor
     */
    int m_cam_fd{-1};

    /*!
     * \brief m_previousFrameCounter - used to detect dropped frames
     */
    std::optional<int64_t> m_previousFrameCounter{};

    static constexpr uint8_t BUFFER_COUNT{4};

    // std::array<Image, BUFFER_COUNT> m_images;
    /*!
     * \brief m_imageMutexes - lock while processing image from m_images
     */
    std::array<std::mutex, BUFFER_COUNT> m_imageMutexes;

    // TODO: split this
    // there should be no chance of changing image by ioctl during futher processing
    struct buffer
    {
        void *mem{nullptr};
        // std::shared_ptr<Image> image{std::make_shared<Image>()};
    };
    std::vector<buffer> m_rawBuffers;

    struct Semaphore
    {
        sem_queue<std::shared_ptr<Image>, BUFFER_COUNT> rawSemQueue;
        // sem_queue<size_t, BUFFER_COUNT> rawSemQueue;
        sem_queue<std::shared_ptr<Image>, BUFFER_COUNT> rotSemQueue;
        // sem_queue<size_t, BUFFER_COUNT> rotSemQueue;
    } m_sync;

    std::mutex m_camMtx;
    /*!
     * \brief m_buffersQueue - queue of buffers which require extracting pixels
     */
    std::queue<std::remove_const<decltype(BUFFER_COUNT)>> m_buffersQueue;

    std::jthread m_streamThread;
    std::jthread m_getThreads[1];
    // std::jthread m_getThreads[4];
    // TODO: sync all loops somehow to guarantee frames order
    std::jthread m_rotateThreads[2];
    std::jthread m_calcPixelsThreads[2];
    std::mutex m_lastImageMtx;
    std::shared_ptr<Image> m_lastProcessedImage{};

    std::shared_ptr<veye::imx287m::i2c> m_i2c;
    std::shared_ptr<HttpServer> m_httpServer;

    // calibration
    // TODO: re-organize this logic
    bool m_isCalibrating{false};
    bool m_ignoreFrames{false};
    std::vector<std::shared_ptr<Pixels>> m_calibrationPixels;
    std::shared_ptr<IStand> m_stand;
    double m_zRangeMm{std::numeric_limits<double>::quiet_NaN()};
    bool m_isMoving{false};
};