Go to the documentation of this file.00001 #include <qt4/QtNetwork/QtNetwork>
00002
00003 #include "Server.h"
00004 namespace server {
00005
00006 Server::Server(int argc, char** argv, QObject* pParent)
00007 : QObject(pParent),
00008 m_RobotThread(argc, argv)
00009 {
00010
00011 m_pTcpServer = new QTcpServer(this);
00012 if (!m_pTcpServer->listen(QHostAddress::Any, 5512)) {
00013 std::cerr << tr("TCP Server").toStdString(),
00014 tr("Unable to start the server: %1.\n")
00015 .arg(m_pTcpServer->errorString()).toStdString();
00016 exit(-1);
00017 return;
00018 }
00019
00020 QString ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
00021 std::cout << tr("The server is running on\n\nIP: %1\nport: %2\n\n"
00022 "Run the Client now.")
00023 .arg(ipAddress).arg(m_pTcpServer->serverPort()).toStdString() << std::endl;
00024
00025 connect(m_pTcpServer, SIGNAL(newConnection()), SLOT(NewClientConnection()));
00026 m_RobotThread.init();
00027 m_RobotThread.start();
00028 }
00029
00030 void Server::NewClientConnection() {
00031 QTcpSocket * pClientSocket = m_pTcpServer->nextPendingConnection();
00032 if(pClientSocket) {
00033 connect(pClientSocket, SIGNAL(disconnected()), pClientSocket, SLOT(deleteLater()));
00034 connect(pClientSocket, SIGNAL(readyRead()), SLOT(NewClientCommand()));
00035 }
00036 }
00037
00038 void Server::writeData()
00039 {
00040
00041
00042
00043
00044
00045
00046 }
00047
00048 void Server::NewClientCommand() {
00049
00050
00051
00052 const QRegExp rxlen("^(\\w+)\\s+(-*\\d*\\.?\\d*)\\s+(-*\\d*\\.?\\d*)$");
00053
00054 int length;
00055
00056 QTcpSocket * pClientSocket = qobject_cast<QTcpSocket *>(sender());
00057
00058 while(pClientSocket->bytesAvailable() > 0) {
00059 length = static_cast<int>(pClientSocket->read(1).at(0));
00060 QString text(pClientSocket->read(length));
00061
00062 if (rxlen.indexIn(text) > -1) {
00063 QString command = rxlen.cap(1);
00064 double speed = rxlen.cap(2).toDouble();
00065 QString l_speed; QString a_speed;
00066 double changeInAngle = rxlen.cap(3).toDouble();
00067 l_speed.setNum(speed); a_speed.setNum(changeInAngle);
00068
00069 QString toRos = command + ", m/sec: " + l_speed + ", rad/sec: " + a_speed;
00070 std::cout << "Command: " << command.toStdString();
00071 m_RobotThread.setCommand(toRos);
00072
00073 if (command == "SetSpeed")
00074 {
00075 m_RobotThread.SetSpeed(speed, changeInAngle);
00076 std::cout << "\tX m/sec: " << speed << "\t radians/sec: " <<
00077 changeInAngle;
00078 }
00079
00080 else if (command == "getPosition")
00081 {
00082 std::stringstream stm;
00083 stm << "("; stm << m_RobotThread.getXPos();
00084 stm << ", "; stm << m_RobotThread.getYPos();
00085 stm << ", "; stm << m_RobotThread.getAPos();
00086 stm << ")";
00087
00088 std::string pos = stm.str();
00089
00090 pClientSocket->write(pos.c_str());
00091 }
00092
00093 else if (command == "getXSpeed")
00094 {
00095 std::stringstream stm;
00096 stm << m_RobotThread.getXSpeed();
00097 pClientSocket->write(stm.str().c_str());
00098 }
00099
00100 else if (command == "getASpeed")
00101 {
00102 std::stringstream stm;
00103 stm << m_RobotThread.getASpeed();
00104 pClientSocket->write(stm.str().c_str());
00105 }
00106
00107 else if (command == "setPosition") {
00108 double xPos = speed;
00109 double yPos = changeInAngle;
00110
00111 QList<double> pose;
00112 pose.push_back(xPos);
00113 pose.push_back(yPos);
00114 m_RobotThread.setPose(pose);
00115 }
00116
00117 else if (command == "setGoal")
00118 {
00119 double xPos = speed, yPos = changeInAngle;
00120
00121 geometry_msgs::Point goal;
00122 goal.x = xPos;
00123 goal.y = yPos;
00124 goal.z = PI/2;
00125 m_RobotThread.goToXYZ(goal);
00126 }
00127
00128 std::cout << std::endl;
00129 }
00130 }
00131 }
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187 }