-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequestProcessing.cpp
190 lines (164 loc) · 5.49 KB
/
RequestProcessing.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
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "RequestProcessing.h"
#include <string.h>
using TypesOfRequest = Request::TypesOfRequest;
void Request::SetColor(double hue, double saturation, double value)
{
colorPuf_[0] = hue;
colorPuf_[1] = saturation;
colorPuf_[2] = value;
}
void Request::SetDestination(cv::Point2i point)
{
this->destination_ = point;
}
Request &Request::operator=(Request &req)
{
this->colorPuf_ = req.colorPuf_;
this->type_ = req.type_;
return *this;
}
void Controller::MakeRequest(Request &req, Color colorPuf, TypesOfRequest type)
{
req.SetColor(colorPuf);
req.SetType(type);
}
void Controller::FinishRequest(Request &req)
{
req.SetType(TypesOfRequest::None);
}
void Controller::Move(int distance)
{
const char *cmd = "forward";
myMosq::MqttMessage mes{cmd, (1.0)};
mosq_->Publish(&mes);
}
void Controller::Rotate(float cosOfAngle)
{
const char* cmd = "right";
myMosq::MqttMessage mes{cmd, 1.0};
mosq_->Publish(&mes);
}
void Controller::GoHome()
{
std::cout << "Going Home.." << std::endl;
}
void Controller::FiniteAutomate(cv::VideoCapture &cap)
{
mosq_ = std::make_shared<myMosq>("localhost", "/telega", "abot/command/alex");
Detector detector;
float angle = 0;
int distance = 0;
cv::namedWindow("result", cv::WINDOW_NORMAL);
Request currentRequest;
bool isOn = true;
while (isOn)
{
Controller *controller = this;
switch (state_)
{
case States::Running:
{
// Making a request
char c;
std::cout << "Enter \'d\' to deliver\n";
std::cout << "Enter \'e\' to exit\n";
std::cin >> c;
if (c == 'd')
{
mosq_->Subscribe();
for (int i = 0; i < 3; i++)
{
std::cout << mosq_->GetMessage()[i] << " \n";
}
sleep(2);
currentRequest.SetColor(mosq_->GetMessage());
controller->MakeRequest(currentRequest, currentRequest.GetColor(), TypesOfRequest::Deliver);
}
else if (c == 'e')
{
state_ = Disabling;
break;
}
if (currentRequest.GetType() == TypesOfRequest::System)
state_ = Disabling;
else if (currentRequest.GetType() == TypesOfRequest::Deliver)
state_ = DoDeliver;
else
controller->GoHome();
break;
}
case States::Disabling:
isOn = false;
break;
case States::DoDeliver:
{
cv::Mat frame;
// Capture frame-by-frame
cap >> frame;
// If the frame is empty, break immediately
if (frame.empty())
state_ = States::Running;
// Detecting robot
bot_.head = detector.SteppedDetection(frame, headColorHsv_);
bot_.tail = detector.SteppedDetection(frame, tailColorHsv_);
cv::Point2i botCenter = detector.GetMassCenter(bot_.head, bot_.tail);
// Setting destination
if (!currentRequest.GetDestination().x || !currentRequest.GetDestination().y)
currentRequest.SetDestination(detector.SteppedDetection(frame, currentRequest.GetColor()));
std::cout << currentRequest.GetDestination() << std::endl;
std::cout << "Bot: H: " << bot_.head << " T: " << bot_.tail << std::endl;
angle = acos(detector.findAngle(botCenter, bot_.head, currentRequest.GetDestination()));
distance = detector.findDistanceToDestination(botCenter, currentRequest.GetDestination());
std::cout << "Angle : " << angle << " Distance: " << distance << std::endl;
// draw angle lines
line(frame, static_cast<cv::Point>(bot_.head), static_cast<cv::Point>(botCenter), (255, 0, 0));
line(frame, static_cast<cv::Point>(botCenter), static_cast<cv::Point>(currentRequest.GetDestination()), (0, 255, 0));
cv::resize(frame, frame, cv::Size(frame.cols/3,frame.rows/3));
cv::imshow("result", frame);
cv::waitKey(100);
// Deviation for if`s
float errorAngle = 15;
angle = angle * 180 / M_PIf;
int errorDist = 200;
if ( angle > errorAngle )
{
state_ = States::Rotate;
break;
}
else if (distance > errorDist)
{
state_ = States::Move;
break;
}
else
{
state_ = Waiting;
break;
}
break;
}
case States::Move:
std::cout << "Moving " << distance << std::endl;
state_ = DoDeliver;
controller->Move(distance);
break;
case States::Rotate:
std::cout << "Rotating to " << angle * 180 / M_PIf << std::endl;
state_ = DoDeliver;
controller->Rotate(angle);
break;
case States::Waiting:
// while(CupOnTheBot())
// {
// if(CloseToClient())
// cout << "Take your drink!" << endl;
// if(CloseToDispenser())
// cout << "The drink is being poured!" << endl;
// }
state_ = Running;
controller->FinishRequest(currentRequest);
break;
}
}
cv::destroyAllWindows;
}