-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmainWindow.cpp
executable file
·238 lines (202 loc) · 6.61 KB
/
mainWindow.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include "mainWindow.h"
#include <QApplication>
#include <QDebug>
#include <QElapsedTimer>
#include <QMessageBox>
const int IMAGE_PROCESS_PERIOD = 33;
const float MOTOR_SPEED_SACLER = 0.15f;
const float DXL_ANGLE_SACLER = 3.4133333f;
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
mImageProcessTimer(this),
mCurState(MANUAL),
mSerial(this),
mProcessor(){
// basic parameter settings
ui.setupUi(this);
this->setWindowTitle("Calibrating");
mVideoFrame = this->findChild<VideoFrame*>("video");
connect(&mImageProcessTimer, SIGNAL(timeout()), this, SLOT(imageProcess()));
cmdString = new QString();
mSerial.setBaudRate(38400);
// finding & adding ports
const auto& portInfoList = QSerialPortInfo::availablePorts();
if (portInfoList.size() == 0){
ui.serialCombox->addItem("No port");
}
for (int i = 0; i < portInfoList.size(); ++i){
ui.serialCombox->addItem(portInfoList.at(i).portName());
}
serialTheadTimer = new QTimer(this);
sensorUpdateTimer = new QTimer(this);
connect(serialTheadTimer, SIGNAL(timeout()), this, SLOT(readData()));
//connect(&mSerial, SIGNAL(readyRead()), this, SLOT(readData()));
serialTheadTimer->start(1);
connect(sensorUpdateTimer, SIGNAL(timeout()), this, SLOT(updateSensor()));
//sensorUpdateTimer->start(300);
for (int i = 0; i < 5; i++){
isim[i] = new IsimControl(i+1, &mSerial);
}
isimCurrentControl = isim[0];
// this line should be last line of this constructor
mImageProcessTimer.start(IMAGE_PROCESS_PERIOD);
}
MainWindow::~MainWindow() {
delete(serialTheadTimer);
}
void MainWindow::imageProcess() {
cv::Mat result;
QElapsedTimer elapsedTime;
elapsedTime.start();
switch (mCurState) {
case CALIBRATION : {
result = mVideoFrame->curFrame();
if (mProcessor.calibrate(mVideoFrame->curFrame())) {
mCurState = FIND_OBJECT;
setWindowTitle("Find Object");
}
break;
}
case FIND_OBJECT : {
result = mProcessor.findObject(mVideoFrame->curFrame());
break;
}
case FIND_ISIM: {
result = mProcessor.findISIM(mVideoFrame->curFrame());
break;
}
}
this->mVideoFrame->setResult(result);
qDebug() << elapsedTime.elapsed();
}
void MainWindow::serialCtrlBtnClicked(){
static bool isOpen = false;
if (!isOpen){
mSerial.setPortName(ui.serialCombox->currentText());
if (mSerial.open(QIODevice::ReadWrite)){
ui.serialCtrlBtn->setText("CLOSE");
isOpen = true;
}
else{
QMessageBox::critical(this, "Serial error", "Failed to open serial port!");
}
}
else{
mSerial.close();
ui.serialCtrlBtn->setText("OPEN");
isOpen = false;
}
}
void MainWindow::serialSendBtnClicked(){
if (mSerial.isOpen()){
}
else{
QMessageBox::critical(this, "Serial Error", "Serialport is not open!");
}
}
void MainWindow::payloadDetectionBtnClicked(){
}
void MainWindow::readData(){
if (mSerial.canReadLine()){
char data[30];
mSerial.readLine(data, 30);
QString strCmd(data);
*cmdString += strCmd;
ui.serialConsole->append(strCmd);
strCmd.remove("\r\n");
if (strCmd.at(0) == '#'){
return;
}
QString cmd = strCmd.mid(0, 2);
QString cmdWithoutOpcode = strCmd.mid(2);
if ((cmdWithoutOpcode.at(0) > '9' || cmdWithoutOpcode.at(0) < '0')){
return;
}
QStringList *strParams = new QStringList();
*strParams = cmdWithoutOpcode.split('\t');
int debug_int;
float *params = new float[cmdWithoutOpcode.size()];
for (int i = 0; i < strParams->size(); i++)
{
params[i] = (*strParams)[i].toFloat();
}
if (cmd == "PN") {
QMessageBox::information(this, "Ping", "Ping recieved from ISIM!");
}
else if (cmd == "GY") {
isim[((int)params[0]) - 1]->setYaw(params[1]);
}
}
}
void MainWindow::updateSensor(){
if (mSerial.isOpen()){
for (int i = 0; i < 5; i++){
isim[i]->updateGyroscopeData();
QThread::msleep(20);
}
}
}
void MainWindow::pingBtnClicked(){
float pingNulldata[] = { 0, 0, 0 };
isimCurrentControl->sendInstruction(0, 0x05, pingNulldata);
}
void MainWindow::isimControlSelectionChanged(int selectionValue){
isimCurrentControl = isim[selectionValue];
ui.rmotorSpinbox->setValue(isimCurrentControl->getRmotorValue() / MOTOR_SPEED_SACLER);
ui.lmotorSpinbox->setValue(isimCurrentControl->getLmotorValue() / MOTOR_SPEED_SACLER);
ui.rdxlSpinbox->setValue((isimCurrentControl->getRdxlValue() - 512.0f) / DXL_ANGLE_SACLER);
ui.ldxlSpinbox->setValue((isimCurrentControl->getLdxlValue() - 512.0f) / DXL_ANGLE_SACLER);
ui.rmagnetSpinbox->setValue(isimCurrentControl->getRmagnetValue());
ui.lmagnetSpinbox->setValue(isimCurrentControl->getLmagnetValue());
}
void MainWindow::isimHomeSelectionChanged(int selectionValue){
}
void MainWindow::isimControlValueChanged(){
if (mSerial.isOpen()){
QStringList senderNames;
senderNames << "rmotorSpinbox"; //0
senderNames << "lmotorSpinbox"; //1
senderNames << "rdxlSpinbox"; //2
senderNames << "ldxlSpinbox"; //3
senderNames << "rmagnetSpinbox";//4
senderNames << "lmagnetSpinbox";//5
QObject* obj = sender();
switch (senderNames.indexOf( obj->objectName() ))
{
case 0:// right motor value changed
isimCurrentControl->setWheelSpeed(isimCurrentControl->getLmotorValue(),MOTOR_SPEED_SACLER*((QSpinBox*)obj)->value());
break;
case 1:// left motor value changed
isimCurrentControl->setWheelSpeed(MOTOR_SPEED_SACLER*((QSpinBox*)obj)->value(), isimCurrentControl->getRmotorValue());
break;
case 2:// right dxl value changed
isimCurrentControl->setDxlPosition(isimCurrentControl->getLdxlValue(), ((QSpinBox*)obj)->value() * 3.4133333f + 512.0f);
break;
case 3:// left dxl value changed
isimCurrentControl->setDxlPosition(((QSpinBox*)obj)->value() * 3.4133333f + 512.0f, isimCurrentControl->getRdxlValue());
break;
case 4:// right magnet value changed
isimCurrentControl->setMagnetPower(isimCurrentControl->getLmagnetValue(), ((QSpinBox*)obj)->value());
break;
case 5:// left magnet value changed
isimCurrentControl->setMagnetPower(((QSpinBox*)obj)->value() , isimCurrentControl->getRmagnetValue());
break;
}
}
else{
QMessageBox serialErrorMessageBox;
serialErrorMessageBox.setText("Please open serialport first!");
serialErrorMessageBox.exec();
}
}
void MainWindow::ldxlInfoChanged(int){
}
void MainWindow::assemblePathGenBtnClicked(){
}
void MainWindow::keyPressEvent(QKeyEvent* e){
if (e->text() == "g"){
sensorUpdateTimer->start(500);
}
}
void MainWindow::gyroReadBtnClicked(){
isimCurrentControl->updateYaw();
}