-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathamurcore.cpp
203 lines (160 loc) · 5.11 KB
/
amurcore.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
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "amurcore.h"
#include "ui_AmurCore.h"
AmurCore::AmurCore(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::AmurCore)
{
ui->setupUi(this);
config = new ConfigProcessor(configName); // Load config file
fillFieldsByConfig(); // Fill fields by config file
outputMat = imread("data/images/no_picture.jpeg");
this->initialize();
}
AmurCore::~AmurCore()
{
capture.release();
delete ui;
}
void AmurCore::fillFieldsByConfig()
{
std::string ip ;
// int result = config->searchString("Amur.Network.address", ip);
// if(ip != ""){
// this->address = ip;
// }
}
void AmurCore::initialize()
{
controls = std::make_shared<Controls>();
sensors = std::make_shared<Sensors>();
map = std::make_shared<map_service::GetMapResponse>();
camHolder = new CamSettingsHolder();
joyState = std::make_shared<JoyState>();
joystickDialog = new JoystickDialog(joyState, this);
mapDialog = new MapDialog(map, this);
robotInfoDialog = new RobotInfoDialog();
amurLogic = new Logic(joyState, controls, sensors);
repo = std::make_shared<RobotRepository>("myRobots.db");
if (!repo->openDatabase()) {
qDebug() << "Cannot open database!";
}
network = std::make_shared<NetworkController>(controls, sensors, map); // TODO - add robot id & &<vector> of robots id
network->runArpingService(arpPort, grpcPort, arpHeader); // Start listening for initial arp message from robots
network->runServer(address_mask); // Start AmurCore gRPC server
connectDialog = new ConnectDialog(this, network, repo);
connMenu();
startTimer();
startCap();
}
void AmurCore::connMenu()
{
connect(joystickDialog, &JoystickDialog::accepted, this, &AmurCore::fetchJoystickId);
// File menu
connect(ui->action_Joystick, SIGNAL(triggered()), this, SLOT(joystickDialogOpen()));
connect(ui->actionE_xit, SIGNAL(triggered(bool)), this, SLOT(close()));
// Robot menu
connect(ui->action_Connect, SIGNAL(triggered()), this, SLOT(connectDialogOpen()));
connect(ui->action_Reboot, SIGNAL(triggered()), this, SLOT(robotReboot()));
connect(ui->action_Halt, SIGNAL(triggered()), this, SLOT(robotHalt()));
connect(ui->actionCamera, SIGNAL(triggered()), this, SLOT(calibDialogOpen()));
connect(ui->action_Map, SIGNAL(triggered()), this, SLOT(mapDialogOpen()));
connect(ui->actionRobot_Info, SIGNAL(triggered()), this, SLOT(robotInfoDialogOpen()));
}
void AmurCore::joystickDialogOpen()
{
joystickDialog->exec();
}
void AmurCore::connectDialogOpen()
{
// if(tcpThread == nullptr){
// tcpThread = new TCP(controls, sensors, hostName);
// tcpThread->addThread();
// }
connectDialog->exec();
}
void AmurCore::calibDialogOpen()
{
CamCalibrate *calibDialog;
calibDialog = new CamCalibrate(&sourceMat, camHolder, this);
connect(this, &AmurCore::timeout, calibDialog, &CamCalibrate::frameUpdate);
connect(calibDialog, &CamCalibrate::finished, calibDialog, &CamCalibrate::deleteLater);
if(camHolder->getReady())
camHolder->setReady(false);
calibDialog->exec();
}
void AmurCore::mapDialogOpen()
{
mapDialog->exec();
}
void AmurCore::robotInfoDialogOpen()
{
robotInfoDialog->exec();
}
void AmurCore::resizeEvent(QResizeEvent *event)
{
Q_UNUSED(event);
QImage qimgOut((uchar*) outputMat.data, outputMat.cols, outputMat.rows, outputMat.step, QImage::Format_RGB888);
ui->OutLabel->setPixmap(QPixmap::fromImage(qimgOut).scaled(
this->width() - 16,
this->height() - 60
));
}
void AmurCore::robotHalt()
{
controls->mutable_system()->set_haltflag(true);
}
void AmurCore::robotReboot()
{
controls->mutable_system()->set_restartflag(true);
}
void AmurCore::fetchJoystickId()
{
joyThread = new Joystick(joyState);
joyThread->addThread();
}
void AmurCore::startTimer()
{
tmrTimer = new QTimer(this);
connect(tmrTimer,SIGNAL(timeout()),this,SLOT(frameUpdate()));
connect(tmrTimer, &QTimer::timeout, this, &AmurCore::timeout);
tmrTimer->start(loopTime); //msec
}
void AmurCore::startCap()
{
capture.open(SOURCE_STREAM);
if(!capture.isOpened())
return;
frameUpdate();
}
void AmurCore::frameUpdate()
{
if(capture.read(sourceMat)){
worker();
}
ui->statusbar->showMessage(statusMessage);
}
void AmurCore::outMat(Mat &toOut)
{
QImage qimgOut((uchar*) toOut.data, toOut.cols, toOut.rows, toOut.step, QImage::Format_RGB888);
ui->OutLabel->setPixmap(QPixmap::fromImage(qimgOut).scaled(
this->width() - 16,
this->height() - 60
));
}
void AmurCore::undistortMat(Mat &inMat, Mat &outMat)
{
if(camHolder->getReady())
outMat = camHolder->remap(inMat);
else
outMat = inMat;
}
void AmurCore::worker()
{
cv::flip(sourceMat, sourceMat, 1);
// cv::resize(sourceMat, sourceMat, Size(320, 240));
undistortMat(sourceMat, undistortedMat);
amurLogic->setSrcMat(&undistortedMat);
outputMat = amurLogic->getOutMat();
outMat(sourceMat);
amurLogic->process();
}