-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
177 lines (139 loc) · 4.83 KB
/
main.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
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
#include <cmath>
#include <chrono>
#include <opencv2/imgproc.hpp>
#include <thread>
#include <iostream>
#include <opencv2/core/mat.hpp>
#include <opencv2/core/types.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <vector>
#define SHOW_FRAMES 1
#define FRAMES_PER_SECOND 24
#define CROP_NUM 300
namespace objects {
// 1 pixel = 1 cm
class Crop {
public:
double x, y, r;
};
class Robot {
public:
double length, width; // cm, cm
double x, y, angle; // cm, cm, degrees
Robot(double x, double y, double angle, double l, double w)
{
this->x = x;
this->y = y;
this->angle = angle;
length = l;
width = w;
};
void move(double speed, double angle) // centimeters per second, degrees
{
this->angle += angle * (speed / FRAMES_PER_SECOND);
x += (speed / FRAMES_PER_SECOND) * std::cos(this->angle * 3.1415/180);
y += (speed / FRAMES_PER_SECOND) * std::sin(this->angle * 3.1415/180);
}
std::vector<Crop> detect(Crop crops[]) {
std::vector<Crop> detected;
double center_x = (int) this->x + (length + 20) * std::cos((this->angle) * 3.1415/180);
double center_y = (int) this->y + (length + 20) * std::sin((this->angle) * 3.1415/180);
for (int i = 0; i < 300; i++)
{
if (std::sqrt((crops[i].x - center_x)*(crops[i].x - center_x) + (crops[i].y - center_y)*(crops[i].y - center_y)) < 70)
{
detected.push_back(crops[i]);
}
}
return detected;
}
};
}
namespace draw {
using namespace objects;
void draw_robot(cv::Mat* img, Robot* r)
{
cv::circle(*img, cv::Point((int) r->x + (r->length + 20) * std::cos((r->angle) * 3.1415/180), (int) r->y + (r->length + 20) * std::sin((r->angle) * 3.1415/180)), 70, cv::Scalar(20, 20, 20), cv::FILLED, cv::LINE_8);
double x1 = r->x + (r->width / 2) * std::cos((r->angle+90) * 3.1415/180) + (r->length / 2) * std::cos((r->angle) * 3.1415/180);
double y1 = r->y + (r->width / 2) * std::sin((r->angle+90) * 3.1415/180) + (r->length / 2) * std::sin((r->angle) * 3.1415/180);
double x2 = r->x + (r->width / 2) * std::cos((r->angle-90) * 3.1415/180) + (r->length / 2) * std::cos((r->angle) * 3.1415/180);
double y2 = r->y + (r->width / 2) * std::sin((r->angle-90) * 3.1415/180) + (r->length / 2) * std::sin((r->angle) * 3.1415/180);
double x3 = r->x + (r->width / 2) * std::cos((r->angle+90) * 3.1415/180) - (r->length / 2) * std::cos((r->angle) * 3.1415/180);
double y3 = r->y + (r->width / 2) * std::sin((r->angle+90) * 3.1415/180) - (r->length / 2) * std::sin((r->angle) * 3.1415/180);
double x4 = r->x + (r->width / 2) * std::cos((r->angle-90) * 3.1415/180) - (r->length / 2) * std::cos((r->angle) * 3.1415/180);
double y4 = r->y + (r->width / 2) * std::sin((r->angle-90) * 3.1415/180) - (r->length / 2) * std::sin((r->angle) * 3.1415/180);
cv::line(*img,cv::Point(x1,y1),cv::Point(x2,y2),cv::Scalar(255,0,0),2);
cv::line(*img,cv::Point(x2,y2),cv::Point(x4,y4),cv::Scalar(255,0,0),2);
cv::line(*img,cv::Point(x3,y3),cv::Point(x4,y4),cv::Scalar(255,0,0),2);
cv::line(*img,cv::Point(x3,y3),cv::Point(x1,y1),cv::Scalar(255,0,0),2);
cv::circle(*img, cv::Point((int) r->x + (r->length / 2) * std::cos((r->angle) * 3.1415/180), (int) r->y + (r->length / 2) * std::sin((r->angle) * 3.1415/180)), 5, cv::Scalar( 0, 0, 255), cv::FILLED, cv::LINE_8);
}
void draw_crops(cv::Mat* img, Crop crops[])
{
Crop c;
for (int i = 0; i < CROP_NUM; i++)
{
c = crops[i];
cv::circle(*img,
cv::Point((int) c.x, (int) c.y),
c.r,
cv::Scalar( 0, 255, 0),
cv::FILLED,
cv::LINE_8 );
}
}
void draw_detected(cv::Mat* img, std::vector<Crop> detected)
{
for (Crop c : detected)
{
cv::circle(*img,
cv::Point((int) c.x, (int) c.y),
c.r,
cv::Scalar( 0, 0, 255),
cv::FILLED,
cv::LINE_8 );
}
}
}
namespace connect {
}
int main(int argc, char* argv[])
{
// setup
std::cout << "NaviSim: START SETUP" << std::endl;
int frame_number = 0;
objects::Robot robot = objects::Robot(925, 600, -90, 82.0, 60.0);
objects::Crop crops[CROP_NUM];
int cx = 1000;
int cy = 950;
for (int i = 0; i < 12; i++) // 12 rows
{
for (int j = 0; j < 25; j++) {
crops[i*25+j].x = cx + 40 * cos((cy - 600) * (3.1415 / 350));
crops[i*25+j].y = cy;
crops[i*25+j].r = 2;
cy -= 30;
}
cx -= 75;
cy = 950;
}
// simulation loop
while (1)
{
frame_number++;
//robot.move(100, -0.2);
std::this_thread::sleep_for(std::chrono::milliseconds(1000 / FRAMES_PER_SECOND));
std::vector<objects::Crop> detected = robot.detect(crops);
// show frame
#if SHOW_FRAMES
std::cout << "NaviSim: FRAME " << frame_number << std::endl;
cv::Mat img = cv::Mat::zeros(1200, 1200, CV_8UC3);
draw::draw_robot(&img, &robot);
draw::draw_crops(&img, crops);
draw::draw_detected(&img, detected);
cv::imshow("NaviSim", img);
cv::waitKey(1);
#endif
}
}