Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Labor Systemanalyse und Optimierung
mBot
mBot Konsole
Commits
74c46363
Commit
74c46363
authored
Jul 01, 2020
by
Fabian Löbbers
Browse files
init Commit
parent
95714a1c
Changes
1
Hide whitespace changes
Inline
Side-by-side
mBot_Konsole/konsole.cpp
0 → 100644
View file @
74c46363
#include
"konsole.h"
#include
"ui_konsole.h"
#include
"QDateTime"
Konsole
::
Konsole
(
QWidget
*
parent
)
:
QMainWindow
(
parent
),
ui
(
new
Ui
::
Konsole
)
{
ui
->
setupUi
(
this
);
init_Nachricht
();
}
Konsole
::~
Konsole
()
{
delete
ui
;
}
void
Konsole
::
init_Nachricht
(){
//Init Nachricht, die in der UI im Konsolenbereich bei Programmstart ausgegeben wird
ui
->
tf_konsole
->
append
(
"So läufts:"
);
ui
->
tf_konsole
->
append
(
"Sorge dafür, dass Bot und PC im gleichen Netzwerk sind."
);
ui
->
tf_konsole
->
append
(
"Drücke Autoconnect. Alternativ kannst du auch direkt die IP eingeben und Verbinden drücken"
);
ui
->
tf_konsole
->
append
(
"Benutze die Steuerfelder rechts, um den Bot zu steuern."
);
ui
->
tf_konsole
->
append
(
"ODER"
);
ui
->
tf_konsole
->
append
(
"Tippe unten einen Befehl ein"
);
ui
->
tf_konsole
->
append
(
"Tippe 'help' für mehr Infos"
);
ui
->
tf_konsole
->
append
(
""
);
}
void
Konsole
::
on_bt_verbinden_clicked
()
{
socket
=
new
QUdpSocket
(
this
);
ip_addr
=
ui
->
tf_ipaddr
->
text
();
port
=
ui
->
tf_port
->
text
().
toUShort
();
socket
->
bind
(
QHostAddress
(
ip_addr
),
port
);
connect
(
socket
,
SIGNAL
(
readyRead
()),
this
,
SLOT
(
updateKonsole
()));
qDebug
()
<<
"Verbinden"
;
ping
();
}
void
Konsole
::
ping
(){
//Test Funktion
sendCommand
(
"PING "
);
}
QString
Konsole
::
updateKonsole
(){
qDebug
()
<<
"Kommt an"
;
QByteArray
Buffer
;
Buffer
.
resize
(
socket
->
pendingDatagramSize
());
//Buffer wird erstellt und resized, da NAchrichten immer unterschiedlich lang sind
QHostAddress
sender
;
quint16
senderPort
;
socket
->
readDatagram
(
Buffer
.
data
(),
Buffer
.
size
(),
&
sender
,
&
senderPort
);
//read DAtagramm ließt die NAchricht
if
(
Buffer
!=
""
){
//Abfangen, dass der Buffer auch gefüllt ist.
qDebug
()
<<
"Message from: "
<<
sender
.
toString
();
qDebug
()
<<
"Message Port: "
<<
senderPort
;
qDebug
()
<<
"Buffer: "
<<
Buffer
;
QString
message
=
"Roboter <["
+
sender
.
toString
()
+
"]"
+
QTime
::
currentTime
().
toString
()
+
"> "
+
Buffer
;
ui
->
tf_konsole
->
append
(
message
);
if
(
firstrun
){
connectionSucc
();
firstrun
=
false
;
}
ui
->
tf_ipaddr
->
setText
(
sender
.
toString
());
}
else
{
qDebug
()
<<
"Keine Antwort"
;
//Ist die Antwort leer, wird "Keine Antwort" ausgegeben
}
return
Buffer
;
}
void
Konsole
::
sendCommand
(
QString
command_str
){
//Sendet Nachricht an IP und PORT per UDP
if
(
command_str
==
"help"
){
ui
->
tf_konsole
->
append
(
helptext
);
return
;
}
ip_addr
=
ui
->
tf_ipaddr
->
text
();
port
=
ui
->
tf_port
->
text
().
toUShort
();
qDebug
()
<<
port
;
qDebug
()
<<
ip_addr
;
QByteArray
Data
;
Data
.
append
(
QString
::
number
(
commandID
)
+
" "
+
command_str
+
" ;"
);
// damit der mBot weiß, wann der Befehl zuende ist
commandID
+=
1
;
try
{
// Fängt fehler ab
socket
->
writeDatagram
(
Data
,
QHostAddress
(
ip_addr
),
port
);
//Sendet DATA per UDP, an IP und PORT
}
catch
(...){
qDebug
()
<<
"Senden fehlgeschlagen"
;
}
}
void
Konsole
::
on_bt_send_clicked
(){
sendCommand
(
ui
->
tf_nachricht
->
text
());
QString
message
=
"Server <"
+
QTime
::
currentTime
().
toString
()
+
"> "
+
ui
->
tf_nachricht
->
text
();
ui
->
tf_konsole
->
append
(
message
);
ui
->
tf_nachricht
->
clear
();
}
void
Konsole
::
on_tf_nachricht_returnPressed
()
{
on_bt_send_clicked
();
}
void
Konsole
::
on_dial_winkel_rechts_sliderMoved
(
int
position
)
{
if
(
position
<
0
){
position
=
position
*-
1
;
ui
->
dial_winkel_rechts
->
setValue
(
position
);
}
ui
->
tf_winkel_rechts
->
setText
(
QString
::
number
(
position
));
}
void
Konsole
::
on_dial_winkel_links_sliderMoved
(
int
position
)
{
if
(
position
>
0
){
position
=
position
*-
1
;
ui
->
dial_winkel_links
->
setValue
(
position
);
}
ui
->
tf_winkel_links
->
setText
(
QString
::
number
(
position
*-
1
));
}
void
Konsole
::
on_slider_clicks_sliderMoved
(
int
position
)
{
ui
->
lb_clicks
->
setText
(
QString
::
number
(
position
));
}
void
Konsole
::
connectionSucc
(){
//Bei erfolgreicher Verbindung, werden die Steuerfelder ENABLED
ui
->
cb_watchdog
->
setEnabled
(
true
);
ui
->
tf_nachricht
->
setEnabled
(
true
);
ui
->
bt_send
->
setEnabled
(
true
);
ui
->
tf_ipaddr
->
setEnabled
(
false
);
ui
->
bt_verbinden
->
setEnabled
(
false
);
ui
->
bt_autoconnect
->
setEnabled
(
false
);
ui
->
tf_port
->
setEnabled
(
false
);
ui
->
bt_up
->
setEnabled
(
true
);
ui
->
bt_down
->
setEnabled
(
true
);
ui
->
bt_turnleft
->
setEnabled
(
true
);
ui
->
bt_turnright
->
setEnabled
(
true
);
ui
->
bt_hupe
->
setEnabled
(
true
);
ui
->
bt_hupe
->
setFocus
();
if
(
ui
->
cb_watchdog
->
isChecked
()){
sendCommand
(
"WDE"
);
}
else
{
sendCommand
(
"WDD"
);
}
sendCommand
(
"SP "
+
ui
->
lb_speed
->
text
());
}
void
Konsole
::
on_bt_autoconnect_clicked
()
{
QString
addr
=
"192.168.0.255"
;
//als IP wird die BROADCAST adresse des Subnetzes verwendet
ui
->
tf_ipaddr
->
setText
(
"192.168.0.255"
);
//diese wird in das Textfeld eingefügt
qint32
port
=
ui
->
tf_port
->
text
().
toInt
();
//port wird aus textfeld gelesen
socket
=
new
QUdpSocket
(
this
);
//Neues UDP Socket wird erstellt
socket
->
bind
(
QHostAddress
(
addr
),
port
);
//Socket wird an Adresse udn port gebunden
connect
(
socket
,
SIGNAL
(
readyRead
()),
this
,
SLOT
(
updateKonsole
()));
//socket wird mit readyready verbunden und löst ping() aus
sendCommand
(
"PING "
);
//Ping wird gesendet
}
void
Konsole
::
on_bt_up_clicked
()
{
QString
value
=
QString
::
number
(
ui
->
slider_clicks
->
value
());
QString
command
=
"GO "
+
value
+
" "
+
value
;
sendCommand
(
command
);
}
void
Konsole
::
on_bt_turnleft_clicked
()
{
int
winkel
=
ui
->
tf_winkel_links
->
text
().
toInt
();
double
faktor
=
842.0
/
360.0
;
//anpassungsfaktor: für 360 Graddrehung benötigt der Bot 842 Schritte
QString
value
=
QString
::
number
(
winkel
*
faktor
);
QString
command
=
"GO -"
+
value
+
" "
+
value
;
sendCommand
(
command
);
}
void
Konsole
::
on_bt_turnright_clicked
()
{
int
winkel
=
ui
->
tf_winkel_rechts
->
text
().
toInt
();
double
faktor
=
842.0
/
360.0
;
//anpassungsfaktor: für 360 Graddrehung benötigt der Bot 842 Schritte
QString
value
=
QString
::
number
(
winkel
*
faktor
);
QString
command
=
"GO "
+
value
+
" -"
+
value
;
sendCommand
(
command
);
}
void
Konsole
::
on_bt_down_clicked
()
{
QString
value
=
QString
::
number
(
ui
->
slider_clicks
->
value
());
QString
command
=
"GO -"
+
value
+
" -"
+
value
;
sendCommand
(
command
);
}
void
Konsole
::
on_bt_hupe_clicked
()
{
QString
command
=
"HUPE"
;
sendCommand
(
command
);
}
void
Konsole
::
on_cb_watchdog_stateChanged
(
int
arg1
)
{
if
(
arg1
){
sendCommand
(
"WDE"
);
}
else
{
sendCommand
(
"WDD"
);
}
}
void
Konsole
::
on_slider_speed_sliderMoved
(
int
position
)
{
ui
->
lb_speed
->
setText
(
QString
::
number
(
position
));
}
void
Konsole
::
on_slider_speed_sliderReleased
()
{
sendCommand
(
"SP "
+
ui
->
lb_speed
->
text
());
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment