-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
executable file
·345 lines (279 loc) · 11.4 KB
/
Copy pathtest.cpp
File metadata and controls
executable file
·345 lines (279 loc) · 11.4 KB
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
//
// Created by root on 2021/7/11.
//
#include <sstream>
#include <string>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/core.hpp>
#include <omp.h>
#include "data.h"
#include "detect.h"
#include "states.h"
#include "serial.h"
#include "realSenseDriver.h"
using namespace std;
using namespace cv;
int write_count = 0;
int main()
{
//declare and initialize SUB UVC protocol camera
VideoCapture usb_cap(0);
usb_cap.set(CAP_PROP_FPS , 30);//高度
usb_cap.set(CAP_PROP_FRAME_WIDTH , 640);//宽度
usb_cap.set(CAP_PROP_FRAME_HEIGHT, 480);//高度
usb_cap.set(CAP_PROP_FOURCC , VideoWriter::fourcc('M', 'J', 'P', 'G'));//高度
//declare and initialize realsense camera
// VideoCapture deep_cap("");
//
// deep_cap.InitCam();
//
// deep_cap.SetCam();
//
// deep_cap.StartGrab();
//declare and initialize detector and serial
Detector usb_detector, deep_detector;
usb_detector.initialize(USBCAMERA);
deep_detector.initialize(DEEPCAMERA);
Serial serial;
serial.init_port();
//declare and initialize variables
Mat deep_src,dst;
Mat usb_src, dst1;
//deep_cap.Grab(deep_src);
usb_cap.read(usb_src);
LOGM("UVC Camera Image Size : %d * %d", usb_src.cols, usb_src.rows);
LOGM("Realsense Camera Image Size : %d * %d", deep_src.cols, deep_src.rows);
ReceiveData receive_data{};
stringstream ss;
//declare and initialize states
State state{};
state.initialize();
char cur_case;
int nearing2_lost_cnt = 0;
int nearing1_lost_cnt = 0;
is_success_started = true;
push_count = 0;
bool push_update_flag = false;
while(true)
{
#ifdef DEBUG_
//输出当时状态及初始化计时器
LOGM("MISSION : %s", mission_names[state.mission_state].c_str());
int64 start = getTickCount();
#endif
deep_src = imread("/home/ljh/文档/LightElectronic/0823/27.jpg");
//双路采集图像及神经网络推理,tensrRT对于模型推理的加速效果不明显,可使用deepstream工具链尝试加速
//#pragma omp parallel sections default(none) shared(usb_cap, usb_src, usb_detector, deep_src, deep_detector, state)
// {
//#pragma omp section
{
usb_cap.read(usb_src);
usb_detector.detect_target(usb_src, USBCAMERA, state.mission_state, state.is_push, state.is_check_target_in_yellow);
}
//#pragma omp section
{
//deep_cap.Grab(deep_src);
deep_detector.detect_target(deep_src, DEEPCAMERA, state.mission_state, false, state.is_check_target_in_yellow);
}
// }
#ifdef DEBUG_
//计时器停止,以上部分为程序中最耗时的部分
float time = (getTickCount() - start) / getTickFrequency();
//输出时间
LOGM("Time : %lf ms", time);
#endif
//清除状态量
state.clear();
//cur_case末尾表示usb相机是否找到目标,倒数第二位表示深度相机是否找到目标
cur_case = 0;
if(usb_detector.is_find_target)
cur_case = 0x01;
if(deep_detector.is_find_target)
cur_case |= 0x02;
switch (state.mission_state) {
case DETECTING:
nearing2_lost_cnt = 0;
nearing1_lost_cnt = 0;
push_update_flag = false;
//两个相机均找到目标,选择可信度较高的目标作为夹取目标
if(cur_case == 3)
{
if (usb_detector.target_confidence > deep_detector.target_confidence)
cur_case = 1;
else
cur_case = 2;
}
//根据找到目标的不同情况分别处理
{
switch (cur_case) {
case 0:
state.is_target_found = false;
break;
case 1:
state.is_target_found = true;
state.is_target_close = true;
state.angle = usb_detector.angle;
state.distance = usb_detector.distance;
state.target_type = usb_detector.get_target_type();
state.mission_state = NEARING2;
break;
case 2:
state.is_target_found = true;
state.angle = deep_detector.angle;
//deep_cap.measure(deep_detector.target_box);
//state.distance = deep_cap.dist;
state.target_type = deep_detector.get_target_type();
state.mission_state = NEARING1;
break;
default:
break;
}
}
break;
case NEARING1:
//这里USB相机找到目标的优先级更高,先于深度相机的目标判断。原因为:小车不能把垃圾推开碾过,当近距离内出现目标时,
//可能其置信度不如深度相机找出的目标置信度高,但仍应选择较近的目标作为夹取目标
if((cur_case&0x1) > 0)
{
//usb相机找到目标
#ifdef DEBUG_
if(state.target_type != usb_detector.target_type)
LOGW("Target Changed from %s to %s", target_types[state.target_type].c_str(), target_types[usb_detector.target_type].c_str());
#endif
state.is_target_found = true;
state.is_target_close = true;
state.angle = usb_detector.angle;
state.distance = usb_detector.distance;
state.target_type = usb_detector.get_target_type();
state.mission_state = NEARING2;
break;
}
if((cur_case&0x2) == 0)
{
//连续5帧未找到目标则回到detecting,否则角度不变,距离每次乘一个系数减小
if(nearing1_lost_cnt > (push_count >= 5)?(20):(5))
{
state.is_target_found = false;
state.mission_state = DETECTING;
}
else
{
state.is_target_found = true;
state.mission_state = NEARING1;
state.angle = 0;
state.distance *= 0.8;
nearing1_lost_cnt++;
}
}
else
{
state.is_target_found = true;
state.angle = deep_detector.angle;
//deep_cap.measure(deep_detector.target_box);
//state.distance = deep_cap.dist;
state.target_type = deep_detector.get_target_type();
}
break;
case NEARING2:
if((cur_case&0x1) == 0)
{
if(nearing2_lost_cnt > 6)
{
state.is_target_found = false;
state.mission_state = DETECTING;
}
else
{
state.is_target_found = true;
state.mission_state = NEARING2;
//state.angle /= 2.0;
state.distance /= 2.0;
state.angle = 0;
nearing2_lost_cnt++;
}
}
else
{
state.is_target_found = true;
usb_detector.if_get_clamp_position();
if((usb_detector.target_type != BATTERY && fabs(usb_detector.angle) < 8) || usb_detector.target_type == BATTERY && fabs(usb_detector.angle) < 5)
state.is_target_in_center = true;
if(receive_data.is_arrive_first_position
&& (fabs(usb_detector.angle) < 8 || state.is_target_in_center)
&& ((usb_detector.target_type == BATTERY && fabs(usb_detector.distance) < 35)
|| (usb_detector.target_type == BOTTLE && fabs(usb_detector.distance) < 20)
|| (usb_detector.target_type == PERICARP && fabs(usb_detector.distance) < 35)
|| (fabs(usb_detector.distance) < 20)))
{
state.is_get_clamp_position = true;
state.target_type = usb_detector.get_target_type();
state.mission_state = PICKUP;
usb_detector.last_target_type = usb_detector.target_type;
usb_detector.clear_target_array();
}
state.distance = usb_detector.distance;
state.angle = usb_detector.angle;
}
break;
case PICKUP:
if(!receive_data.is_clamp_complete)
break;
state.mission_state = PUTBACK;
break;
case PUTBACK:
//putback状态实际上完全失效,现已变为推取状态时使用用于更新推取垃圾的种类发送
//if(receive_data.is_front_area == 0x00)
// break;
if(receive_data.is_putback_complete == 0x01)
{
state.mission_state = DETECTING;
usb_detector.clear_target_array();
break;
}
if(state.is_push == 0x01)
{
state.second_target_type = usb_detector.get_target_type();
if(!push_update_flag)
{
push_count++;
push_update_flag = true;
}
}
// deep_detector.preprocess(deep_src);
// deep_detector.if_get_putback_position();
//
// state.direction = deep_detector.direction;
// state.is_get_putback_position = deep_detector.is_get_putback_position;
break;
default:
break;
}
serial.pack(state);
serial.write_data();
serial.read_data(receive_data);
state.is_push = receive_data.is_push;
state.is_check_target_in_yellow = receive_data.is_check_target_in_yellow;
if(receive_data.is_restart)
{
LOGE("Process Stoped by STM32");
is_success_started = false;
serial.pack(state);
serial.write_data();
exit(1);
}
#ifdef DEBUG_
rectangle(deep_src, deep_detector.target_box, Scalar(0, 0, 255));
rectangle(usb_src, usb_detector.target_box, Scalar(0, 0, 255));
pyrDown(usb_src, usb_src);
pyrDown(deep_src, deep_src);
imshow("deep", deep_src);
imshow("usb", usb_src);
if(!deep_detector.close_area_image.empty())
imshow("close_area_image", deep_detector.close_area_image);
if(waitKey(10) == 27)
break;
#endif
}
return 0;
}