-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain1.cpp
126 lines (108 loc) · 3.02 KB
/
main1.cpp
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
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <math.h>
#include <typeinfo>
#include <opencv4/opencv2/opencv.hpp>
#include <opencv4/opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
#include "camera.h"
#include "ArmorDector.h"
#include "general.h"
#include "ArmorDector.h"
#include "opencv_extend.h"
#include "serial.h"
#include "log.h"
#define IMAGE_CENTER_X 327
#define IMAGE_CENTER_Y 230
#define FOCUS_PIXAL 1269
#define PI (3.14159265459)
cv::Mat img = cv::Mat(480, 640, CV_8UC3, (0, 0, 0));
Mycamera mycamera;
ArmorDetector Arm;
Serial serial;
static bool sendTarget(Serial &serial, float x, float y)
{
static short x_tmp, y_tmp, z_tmp;
uint8_t buff[10];
union f_data {
float temp;
unsigned char fdata[4];
} float_data_x, float_data_y;
float_data_x.temp = x;
float_data_y.temp = y;
buff[0] = 's';
buff[1] = static_cast<char>(float_data_x.fdata[0]);
buff[2] = static_cast<char>(float_data_x.fdata[1]);
buff[3] = static_cast<char>(float_data_x.fdata[2]);
buff[4] = static_cast<char>(float_data_x.fdata[3]);
buff[5] = static_cast<char>(float_data_y.fdata[0]);
buff[6] = static_cast<char>(float_data_y.fdata[1]);
buff[7] = static_cast<char>(float_data_y.fdata[2]);
buff[8] = static_cast<char>(float_data_y.fdata[3]);
buff[9] = 'e';
return serial.WriteData(buff, sizeof(buff));
}
bool sendBoxPosition(cv::Point point)
{
float dx = point.x - IMAGE_CENTER_X;
float dy = point.y - IMAGE_CENTER_Y;
float yaw = atan(dx / FOCUS_PIXAL) * 180 / PI;
float pitch = atan(dy / FOCUS_PIXAL) * 180 / PI;
DLOG_INFO << " "
<< " yaw: " << yaw << " pitch " << pitch;
return sendTarget(serial, yaw, pitch);
}
int main(int argc, char const *argv[])
{
mycamera.openWorkThread();
bool err = serial.InitPort();
GLogWrapper glog(argv[0]);
while (err)
{
DLOG_WARNING << "can't open dev/usb";
}
DLOG_INFO << "arm begin";
while (1)
{
mycamera.getiamge().copyTo(img);
if (img.size().width != 640 || img.size().height != 480)
{
LOG_ERROR << "size error";
continue;
}
cv::imshow("src1", img);
Arm.loadImg(img);
Arm.setEnemyColor(BLUE);
int find_flag = Arm.detect();
if (find_flag != 0)
{
std::vector<cv::Point2f> Points = Arm.getArmorVertex();
cv::Point aimPoint;
aimPoint.x = aimPoint.y = 0;
for (const auto &point : Points)
{
aimPoint.x += point.x;
aimPoint.y += point.y;
}
aimPoint.x = aimPoint.x / 4;
aimPoint.y = aimPoint.y / 4;
sendBoxPosition(aimPoint);
}
else
{
DLOG_INFO << "can't find enemy";
}
if (cv::waitKey(10) >= 0)
{
break;
}
if (mycamera.endmain_flag == 1)
{
break;
}
}
}