PixyCamera.cpp

Mich Bar, 09/27/2017 09:59 am

Download (4.7 kB)

 
1
//
2
// Created by TingMiao on 20/11/2016.
3
// Modify by Barbare - 09/2017
4
//
5
#include <string>
6
#include <iostream>
7
#include <chrono>
8
#include "PixyCamera.h"
9
#include "pixy.h"
10
#include <thread>
11
#include <sys/stat.h>
12
#include "colors.h"
13

    
14
using namespace cv;
15
using namespace std;
16
using namespace std::chrono;
17

    
18
struct Block blocks[25];
19
int PixyCamera::Test() {
20

    
21
    int return_value = 0;
22
    int t = 0;
23

    
24
    cout << "testing Pixy...\n";
25

    
26
    while (t < 3 && return_value == 0) {
27

    
28
        switch(t) {
29
            case 0:
30
                return_value = TestInit();
31
                break;
32
            case 1:
33
                return_value = TestStop();
34
                break;
35
            case 2:
36
                return_value = TestExposure();
37
                break;
38
            default:
39
                break;
40
        }
41

    
42
        t++;
43
    }
44

    
45
    if (return_value == 0) {
46
        cout << "tesing Pixy success\n";
47

    
48
    } else {
49
        cout << "tesing Pixy failed\n";
50
    }
51

    
52
    return return_value;
53
}
54

    
55
int PixyCamera::Stop() {
56

    
57
    int return_value;
58
    int32_t response;
59

    
60
    return_value = pixy_command("stop", END_OUT_ARGS, &response, END_IN_ARGS);
61

    
62
    printf("STOP returned %d response %d\n", return_value, response);
63

    
64
    return return_value;
65
}
66

    
67
int PixyCamera::TestInit() {
68

    
69
    int return_value;
70

    
71
    return_value = pixy_init();
72

    
73
    printf("initialized Pixy result : %d\n", return_value);
74

    
75
    if(return_value != 0)
76
    {
77
        // Error initializing Pixy //
78
        cout << "pixy init error: ";
79
        pixy_error(return_value);
80
    }
81

    
82
    return return_value;
83
}
84

    
85
int PixyCamera::TestStop() {
86

    
87
    int return_value = this->Stop();
88

    
89
    if(return_value != 0)
90
    {
91
        // Error initializing Pixy //
92
        cout << "pixy stop error: ";
93
        pixy_error(return_value);
94
    }
95

    
96
    return return_value;
97
}
98

    
99
int PixyCamera::TestExposure() {
100

    
101
    int return_value;
102
    uint8_t gain;
103
    uint16_t compensation;
104

    
105
    return_value = pixy_cam_get_exposure_compensation (&gain, &compensation);
106
    printf("getECV returned %d values: 0, %d, %d\n", return_value, gain, compensation);
107

    
108
    if(return_value != 0)
109
    {
110
        // Error initializing Pixy //
111
        cout << "pixy exposure error: ";
112
        pixy_error(return_value);
113
    }
114

    
115
    return return_value;
116
}
117

    
118
//Test getAxis for receive block data signature
119
void PixyCamera::GetAxis() {
120
    char buf[4096];
121
        fflush(stdout);
122
        int blocks_copied = pixy_get_blocks(25, &blocks[0]);
123
        if (blocks_copied < 0) {
124
                dprintf(2, "pixy_get_blocks() : ");
125
                pixy_error(blocks_copied);
126
                usleep(250000);
127
        }
128
        if (blocks_copied > 0) {
129
        for (int index = 0; index != blocks_copied; index++) {
130
            blocks->print(buf);
131
            dprintf(1, "%s\n", buf);
132
        }
133
        }
134
        usleep(250001);
135
}
136

    
137
Mat PixyCamera::GetOneFrame() {
138
    int return_value;
139
    unsigned char *pixels;  //returned pointer to video frame buffer
140
    int32_t response, fourcc;
141
    int8_t renderflags;
142
    uint16_t width, height;
143
    uint32_t  numPixels;
144

    
145
    response = 0;
146
    return_value = pixy_command("cam_getFrame",  // String id for remote procedure
147
                                0x01, 0x21,      // mode 0 = 1280x800 25 fps
148
                                0x02,   0,        // xoffset
149
                                0x02,   0,         // yoffset
150
                                0x02, 320,       // width
151
                                0x02, 200,       // height (56 max @ 1280 w)
152
                                0,              // separator
153
                                &response,      // pointer to mem address for return value
154
                                &fourcc,         //contrary to docs, the next 5 args are needed
155
                                &renderflags,
156
                                &width,
157
                                &height,
158
                                &numPixels,
159
                                &pixels,        // pointer to mem address for returned frame
160
                                0);
161

    
162
    Mat raw_frame;
163
        if (fourcc == CV_FOURCC('B', 'A', '8', '1')) {
164
        dprintf(2, "TEST BA81\n");
165
        raw_frame = m_render.renderBA81(renderflags, width, height, numPixels, pixels);
166
        m_render.renderTimeStamp(raw_frame);
167
    }
168
    else if (fourcc == CV_FOURCC('C', 'C', 'B', '2'))
169
    {
170
        dprintf(2, "TEST CCB2\n");
171
    }
172
    return raw_frame;
173
}
174

    
175
int PixyCamera::Preview() {
176

    
177
    namedWindow("Preview", WINDOW_NORMAL);
178

    
179
    for(long i = 0;; i++)
180
    {
181
        if (i % 10 == 0) cout << i << " frames previewed" << endl;
182

    
183
        Mat pixy_image = GetOneFrame();
184
                GetAxis();
185

    
186
        imshow("Recording", pixy_image);
187

    
188
        if (waitKey(30) == 27 ) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
189
        {
190
            cout << "esc key is pressed by user" << endl;
191

    
192
            break;
193
        }
194
    }
195

    
196
    cout << "Finished Preview" << endl;
197
    return 0;
198

    
199
}
200