This repository has been archived by the owner on Oct 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaskMain.cpp
173 lines (138 loc) · 4.26 KB
/
taskMain.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
#include "taskMain.h"
void taskMain(MyRio_I2c & i2c, MotorController & mc1, MotorController & mc2, ServoController & s1, cv::VideoCapture & cap, std::vector<std::vector<int16_t>> field)
{
Position pos(0, 0, 0);
pos = moveShift(pos, &i2c, mc1, mc2, 0, -450, 250, 20);
int cnt = 0;
s1.openLeft();
s1.openRight();
std::vector<Dot> dots = QR(pos, field, cap);
Position ST = pos;
dots.push_back(dots[0]);
cout << "\n";
for (int i = 0; i < 5; ++i)
{
std::cout << dots[i].x << " " << dots[i].y << " " << dots[i].theta << std::endl;
}
cout << "\n";
print_map(field);
pii current = pii(dots[0].x, dots[0].y);
int cube_color[6];
int object_color[6];
for (int i = 0; i < 6; ++i)
{
cube_color[i] = -1;
object_color[i] = -1;
}
int k = 1;
int needed = 2;
if (hypotl((dots[2].x - dots[0].x), (dots[2].y - dots[0].y)) < hypotl((dots[0].x - dots[1].x),(dots[0].y - dots[1].y)))
std::swap(dots[1], dots[2]);
if (hypotl((dots[3].x - dots[0].x), (dots[3].y - dots[0].y)) < hypotl((dots[0].x - dots[1].x), (dots[0].y - dots[1].y)))
std::swap(dots[1], dots[3]);
if (hypotl((dots[3].x - dots[1].x), (dots[3].y - dots[1].y)) < hypotl((dots[2].x - dots[1].x), (dots[2].y - dots[1].y)))
std::swap(dots[2], dots[3]);
std::vector<pii> points = aStar(current, current, field); // build the path
pos = goTo(points, pos, dots[k].theta, &i2c, mc1, mc2); // get to the point
while (k < 5)
{
Lidar lidar;
grid(&lidar, &field, &pos);
if (k == 4)
dots[4].theta = dots[3].theta;
pii start = current; // start point
pii goal(dots[k].x, dots[k].y); // finish point
std::vector<pii> points = aStar(start, goal, field); // build the path
std::cout << "path: " << k << "\n";
for (int i = 0; i < points.size(); ++i)
std::cout << (int)points[i].first << " " << (int)points[i].second << '\n';
std::cout << "\n";
pos = goTo(points, pos, dots[k].theta, &i2c, mc1, mc2); // get to the point
current = goal;
if (k == 4) // last point (finish)
{
++k;
continue;
}
shtuka(&i2c, mc1, mc2, &lidar);
pos = moveRobot(pos, &i2c, mc1, mc2, 0, 0, 0, true, true); // motors reset
pos = Position(dots[k].x * 115, dots[k].y * 115, dots[k].theta); // reset position
if(cnt == 2 || k == 3)
{
cube_color[k] = needed;
if (k == 1)
{
if (cube_color[2] == object_color[3])
object_color[k] = cube_color[3];
if (cube_color[3] == object_color[2])
object_color[k] = cube_color[2];
}
else if (k == 2)
{
if (cube_color[3] == object_color[1])
object_color[k] = cube_color[1];
if (cube_color[1] == object_color[3])
object_color[k] = cube_color[1];
}
else
{
object_color[k] = 2;
}
}
if (cube_color[k] < 1)
{
cube_color[k] = checkCube(&cap);
if (cube_color[k] > 0)
++cnt;
}
if (object_color[k] < 1)
object_color[k] = checkObject(&cap);
std::cout << "\n";
std::cout << "colors : " << k << " | " << cube_color[k] << " " << object_color[k] << "\n";
std::cout << "\n";
if (cube_color[k] != needed)
{
if (object_color[k + 1] < 0)
{
std::swap(dots[k], dots[k + 1]);
std::swap(object_color[k], object_color[k + 1]);
std::swap(cube_color[k], cube_color[k + 1]);
}
else if (k + 2 != 4)
{
std::swap(dots[k], dots[k + 2]);
std::swap(object_color[k], object_color[k + 2]);
std::swap(cube_color[k], cube_color[k + 2]);
}
continue;
}
else
{
needed = object_color[k];
if (cube_color[k + 1] > 0 && cube_color[k + 1] != needed && k == 1)
{
std::swap(dots[k + 1], dots[k + 2]);
std::swap(object_color[k + 1], object_color[k + 2]);
std::swap(cube_color[k + 1], cube_color[k + 2]);
}
}
bool left = ((k == 1) || (k == 3));
bool change = (k > 1);
pos = moveRobot(pos, &i2c, mc1, mc2, 0, 0, 0, true, true);
pos = takeCube(pos, &i2c, mc1, mc2, s1, left, change); // collect object
++k;
if (k == 4)
{
s1.closeLeft();
s1.closeRight();
}
}
s1.closeLeft();
s1.closeRight();
std::cout << pos.x << " " << pos.y << "\n";
std::cout << ST.x << " " << ST.y << "\n";
pos = cellShift(&i2c, mc1, mc2, pos, ST, true);
pos = moveRobot(pos, &i2c, mc1, mc2, 0, 0, 0, true, true); // motors reset
Lidar lidar;
toWall(17, 5, 0, &i2c, mc1, mc2, &lidar);
}