00001 #include "ControlWindow.h"
00002 #include <QPalette>
00003 #define PI 3.1415926535898
00004
00005 namespace server{
00006 ControlWindow::ControlWindow(int argc, char **argv, QWidget *parent)
00007 : QWidget(parent),
00008 m_RobotThread(argc, argv)
00009 {
00011 p_upButton = new QPushButton();
00012 p_downButton = new QPushButton();
00013 p_leftButton = new QPushButton();
00014 p_rightButton = new QPushButton();
00015 p_stopButton = new QPushButton(tr("&Stop"));
00016 p_quitButton = new QPushButton(tr("&Quit"));
00017
00018 QPalette palette = p_rightButton->palette();
00019 palette.setColor(QPalette::Button,QColor(255,255,255));
00020 p_rightButton->setAutoFillBackground(true);
00021 p_rightButton->setFlat(true);
00022 p_rightButton->setPalette(palette);
00023 p_rightButton->setIcon(QIcon("images/right.xpm"));
00024 p_rightButton->setIconSize(QSize(50, 50));
00025
00026 palette = p_leftButton->palette();
00027 palette.setColor(QPalette::Button,QColor(255,255,255));
00028 p_leftButton->setAutoFillBackground(true);
00029 p_leftButton->setFlat(true);
00030 p_leftButton->setPalette(palette);
00031 p_leftButton->setIcon(QIcon("images/left.xpm"));
00032 p_leftButton->setIconSize(QSize(50, 50));
00033
00034 palette = p_upButton->palette();
00035 palette.setColor(QPalette::Button,QColor(255,255,255));
00036 p_upButton->setAutoFillBackground(true);
00037 p_upButton->setFlat(true);
00038 p_upButton->setPalette(palette);
00039 p_upButton->setIcon(QIcon("images/up.xpm"));
00040 p_upButton->setIconSize(QSize(50, 50));
00041
00042 palette = p_downButton->palette();
00043 palette.setColor(QPalette::Button,QColor(255,255,255));
00044 p_downButton->setAutoFillBackground(true);
00045 p_downButton->setFlat(true);
00046 p_downButton->setPalette(palette);
00047 p_downButton->setIcon(QIcon("images/down.xpm"));
00048 p_downButton->setIconSize(QSize(50, 50));
00049
00051 leftLayout = new QVBoxLayout();
00052 p_xLayout = new QHBoxLayout();
00053 p_yLayout = new QHBoxLayout();
00054 p_aLayout = new QHBoxLayout();
00055 p_scanLayout = new QHBoxLayout();
00056
00057 p_xLabel = new QLabel();
00058 p_xLabel->setText("X:");
00059 p_xDisplay = new QLineEdit();
00060 p_xDisplay->setText("0.0");
00061
00062 p_yLabel = new QLabel();
00063 p_yLabel->setText("Y:");
00064 p_yDisplay = new QLineEdit();
00065 p_yDisplay->setText("0.0");
00066
00067 p_aLabel = new QLabel();
00068 p_aLabel->setText("Theta: ");
00069 p_aDisplay = new QLineEdit();
00070 p_aDisplay->setText("0.0");
00071
00072 p_scanLabel = new QLabel();
00073 p_scanLabel->setText("Scan: ");
00074 p_scanDisplay = new QLineEdit();
00075 p_scanDisplay->setText("0.0");
00076 p_scanLayout->addWidget(p_scanLabel);
00077 p_scanLayout->addWidget(p_scanDisplay);
00078
00079 p_xLayout->addWidget(p_xLabel);
00080 p_xLayout->addWidget(p_xDisplay);
00081 p_yLayout->addWidget(p_yLabel);
00082 p_yLayout->addWidget(p_yDisplay);
00083 p_aLayout->addWidget(p_aLabel);
00084 p_aLayout->addWidget(p_aDisplay);
00085
00086 leftLayout->addLayout(p_xLayout);
00087 leftLayout->addLayout(p_yLayout);
00088 leftLayout->addLayout(p_aLayout);
00089 leftLayout->addLayout(p_scanLayout);
00090
00092 rightLayout = new QVBoxLayout();
00093 layout = new QHBoxLayout();
00094 layout2 = new QHBoxLayout();
00095 layout3 = new QHBoxLayout();
00096 layout4 = new QHBoxLayout();
00097
00098 mainLayout = new QHBoxLayout();
00099
00100 layout->addWidget(p_upButton);
00101 layout2->addWidget(p_leftButton);
00102 layout2->addWidget(p_stopButton);
00103 layout2->addWidget(p_rightButton);
00104 layout3->addWidget(p_downButton);
00105 layout4->addWidget(p_quitButton, 6);
00106
00107 rightLayout->addLayout(layout);
00108 rightLayout->addLayout(layout2);
00109 rightLayout->addLayout(layout3);
00110 rightLayout->addLayout(layout4);
00111
00112 mainLayout->addLayout(leftLayout);
00113 mainLayout->addLayout(rightLayout);
00114 setLayout(mainLayout);
00115
00116 show();
00117
00118 setWindowTitle(tr("Control Window"));
00119
00120 connect(p_quitButton, SIGNAL(clicked()), this, SLOT(close()));
00121 connect(p_upButton, SIGNAL(clicked()), this, SLOT(goForward()));
00122 connect(p_leftButton, SIGNAL(clicked()), this, SLOT(goLeft()));
00123 connect(p_rightButton, SIGNAL(clicked()), this, SLOT(goRight()));
00124 connect(p_downButton, SIGNAL(clicked()), this, SLOT(goBackward()));
00125 connect(p_stopButton, SIGNAL(clicked()), this, SLOT(halt()));
00126 connect(&m_RobotThread, SIGNAL(newPose(double,double,double)), this, SLOT(updatePoseDisplay(double,double,double)));
00127 connect(&m_RobotThread, SIGNAL(newMidLaser(double)), this, SLOT(updateLaserDisplay(double)));
00128
00129 m_RobotThread.init();
00130 m_RobotThread.start();
00131 }
00132
00133 void ControlWindow::goForward()
00134 {
00135 QString command = "Forward @ 0.5 m/s";
00136
00137 m_RobotThread.setCommand(command);
00138 m_RobotThread.SetSpeed(0.25, 0);
00139 }
00140
00141 void ControlWindow::goBackward()
00142 {
00143 QString command = "Backward @ 0.5 m/s";
00144
00145 m_RobotThread.setCommand(command);
00146 m_RobotThread.SetSpeed(-0.25, 0);
00147 }
00148
00149 void ControlWindow::goRight()
00150 {
00151 QString command = "Turn Right";
00152
00153 double prev_pos = m_RobotThread.getAPos();
00154 prev_pos *= (180.0 / PI);
00155 double next_pos = prev_pos - 90;
00156
00157
00158
00159
00160 next_pos *= (PI / 180.0);
00161 m_RobotThread.setCommand(command);
00162 m_RobotThread.SetSpeed(0, -PI / 6.0);
00163
00164 while (abs(m_RobotThread.getAPos() - next_pos) > 0.001)
00165 { }
00166
00167 m_RobotThread.SetSpeed(0.25, 0);
00168 }
00169
00170 void ControlWindow::goLeft()
00171 {
00172 QString command = "Turn Left";
00173
00174 double prev_pos = m_RobotThread.getAPos();
00175 prev_pos *= (180.0 / PI);
00176 double next_pos = prev_pos + 90;
00177
00178
00179
00180
00181 next_pos *= (PI / 180.0);
00182 m_RobotThread.setCommand(command);
00183 m_RobotThread.SetSpeed(0, PI / 6.0);
00184
00185 while (abs(m_RobotThread.getAPos() - next_pos) > 0.001)
00186 { }
00187
00188 m_RobotThread.SetSpeed(0.25, 0);
00189 }
00190
00191 void ControlWindow::halt()
00192 {
00193 m_RobotThread.SetSpeed(0, 0);
00194 QString command = "Halt";
00195 m_RobotThread.setCommand(command);
00196 }
00197
00198 void ControlWindow::updatePoseDisplay(double x, double y, double theta)
00199 {
00200 QString xPose, yPose, aPose;
00201 xPose.setNum(x);
00202 yPose.setNum(y);
00203 aPose.setNum(theta);
00204
00205 p_xDisplay->setText(xPose);
00206 p_yDisplay->setText(yPose);
00207 p_aDisplay->setText(aPose);
00208 }
00209
00210 void ControlWindow::updateLaserDisplay(double range)
00211 {
00212 QString rangeMeasure;
00213 rangeMeasure.setNum(range);
00214 rangeMeasure += " m";
00215
00216 p_scanDisplay->setText(rangeMeasure);
00217 }
00218
00219 }
00220