]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/robomon/RobomonTuning.cpp
Unify ORTE initialization
[eurobot/public.git] / src / robomon / RobomonTuning.cpp
1 /*
2  * RobomonTuning.cpp                    07/10/31
3  *
4  * Robot`s visualization and control GUI for robot of the
5  * Eurobot 2008 (Mission to Mars).
6  *
7  * Copyright: (c) 2008 DCE Eurobot Dragon Team
8  *            CTU FEE - Department of Control Engineering
9  * Authors: Martin Zidek, Michal Sojka, Tran Duy Khanh
10  * License: GNU GPL v.2
11  */
12
13 #include <QtGui>
14 #include <queue>
15 #include <cstdlib>
16 #include <sys/shm.h>
17 #include <sys/stat.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <math.h>
24
25 #include <orte.h>
26 #include <path_planner.h>
27 #include <robodim.h>
28 #include <sharp.h>
29 #include <trgen.h>
30 #include <map.h>
31 #include <robomath.h>
32 #include "PlaygroundScene.h"
33 #include "MiscGui.h"
34 #include "RobomonTuning.h"
35
36 #include <QCoreApplication>
37 #include <QEvent>
38 #include <QKeyEvent>
39 #include <QDebug>
40 #include <QMessageBox>
41
42 RobomonTuning::RobomonTuning(QWidget *parent)
43         : QWidget(parent)
44 {
45         QFont font;
46         font.setPointSize(7);
47         setFont(font);
48
49
50         createLeftLayout();
51 //      createRightLayout();
52
53         QHBoxLayout *mainLayout = new QHBoxLayout;
54         mainLayout->addLayout(leftLayout);
55 //      mainLayout->addLayout(rightLayout);
56         setLayout(mainLayout);
57
58         createOrte();
59         createActions();
60
61         setFocusPolicy(Qt::StrongFocus);
62 }
63
64 /**********************************************************************
65  * GUI
66  **********************************************************************/
67 void RobomonTuning::createLeftLayout()
68 {
69         leftLayout = new QVBoxLayout();
70         
71         createPwrGroupBox();
72         createCanGroupBox();
73
74         leftLayout->addWidget(pwrGroupBox);
75         leftLayout->addWidget(canGroupBox);
76 }
77
78 void RobomonTuning::createPwrGroupBox()
79 {
80         pwrGroupBox = new QGroupBox(tr("Pwr"));
81         QGridLayout *layout = new QGridLayout();
82
83         pwrAlertCBox = new QCheckBox();
84         
85         layout->addWidget(pwrAlertCBox,0,0);
86
87
88         pwrGroupBox->setLayout(layout);
89 }
90
91 void RobomonTuning::createCanGroupBox()
92 {
93         canGroupBox = new QGroupBox(tr("Can"));
94         QHBoxLayout *layout = new QHBoxLayout();
95
96         canData1 = new QLineEdit(tr("data1"));
97         canData2 = new QLineEdit(tr("data2"));
98         canData3 = new QLineEdit(tr("data3"));
99         canData4 = new QLineEdit(tr("data4"));
100         canData5 = new QLineEdit(tr("data5"));
101         canData6 = new QLineEdit(tr("data6"));
102         canData7 = new QLineEdit(tr("data7"));
103         canData8 = new QLineEdit(tr("data8"));
104         canMsgLen = new QLineEdit(tr("len"));
105         canId = new QLineEdit(tr("ID"));
106
107         canSendPButton = new QPushButton(tr("Send"));
108
109         layout->addWidget(canData1);
110         layout->addWidget(canData2);
111         layout->addWidget(canData3);
112         layout->addWidget(canData4);
113         layout->addWidget(canData5);
114         layout->addWidget(canData6);
115         layout->addWidget(canData7);
116         layout->addWidget(canData8);
117         layout->addWidget(canMsgLen);
118         layout->addWidget(canId);
119         layout->addWidget(canSendPButton);
120
121         canGroupBox->setLayout(layout);
122 }
123
124 /**********************************************************************
125  * GUI actions
126  **********************************************************************/
127 void RobomonTuning::createActions()
128 {
129         connect(pwrAlertCBox, SIGNAL(stateChanged(int)), 
130                         this, SLOT(setPwrAlert(int)));
131
132         connect(canSendPButton, SIGNAL(clicked()), 
133                         this, SLOT(setCanMsg()));
134 }
135
136 void RobomonTuning::setPwrAlert(int value)
137 {
138         if(value) {
139                 orte.pwr_alert.value = 2;
140         }
141         else {
142                 orte.pwr_alert.value = 0;
143         }
144
145
146 }
147
148 void RobomonTuning::setCanMsg()
149 {
150         orte.can_msg.data[0] = canData1->text().toInt();
151         orte.can_msg.data[1] = canData2->text().toInt();
152         orte.can_msg.data[2] = canData3->text().toInt();
153         orte.can_msg.data[3] = canData4->text().toInt();
154         orte.can_msg.data[4] = canData5->text().toInt();
155         orte.can_msg.data[5] = canData6->text().toInt();
156         orte.can_msg.data[6] = canData7->text().toInt();
157         orte.can_msg.data[7] = canData8->text().toInt();
158
159         orte.can_msg.len = canMsgLen->text().toInt();
160         orte.can_msg.id = canId->text().toInt();
161
162         ORTEPublicationSend(orte.publication_can_msg);
163
164 }
165
166 /**********************************************************************
167  * EVENTS
168  **********************************************************************/
169 bool RobomonTuning::event(QEvent *event)
170 {
171         switch (event->type()) {
172                 case QEVENT(QEV_MOTION_STATUS):
173                         break;
174                 case QEVENT(QEV_REFERENCE_POSITION):
175                         break;
176                 case QEVENT(QEV_ESTIMATED_POSITION_ODO):
177                         break;
178                 case QEVENT(QEV_SHARP_LONGS):
179                         break;
180                 case QEVENT(QEV_SHARP_SHORTS):
181                         break;
182                 case QEVENT(QEV_POWER_VOLTAGE):
183                         break;
184                 default:
185                         if (event->type() == QEvent::Close)
186                                 closeEvent((QCloseEvent *)event);
187                         else if (event->type() == QEvent::KeyPress)
188                                 keyPressEvent((QKeyEvent *)event);
189                         else if (event->type() == QEvent::KeyRelease)
190                                 keyReleaseEvent((QKeyEvent *)event);
191                         else if (event->type() == QEvent::FocusIn)
192                                 grabKeyboard();
193                         else if (event->type() == QEvent::FocusOut)
194                                 releaseKeyboard();
195                         else {
196                                 event->ignore();
197                                 return false;
198                         }
199                         break;
200         }
201         event->accept();
202         return true;
203 }
204
205 void RobomonTuning::keyPressEvent(QKeyEvent *event)
206 {
207         if (event->isAutoRepeat()) {
208                 switch (event->key()) {
209                         case Qt::Key_Down:
210                                 break;
211
212                         case Qt::Key_Up:
213                         case Qt::Key_Left:
214                         case Qt::Key_Right:
215                                 break;
216
217                         default:
218                                 event->ignore();
219                                 break;
220                 }
221                 return;
222         }
223
224         switch (event->key()) {
225                 case Qt::Key_Up:
226                         break;
227                 case Qt::Key_Down:
228                         break;
229                 case Qt::Key_Left:
230                         break;
231                 case Qt::Key_Right:
232                         break;
233                 default:
234                         event->ignore();
235                         break;
236         }
237         event->accept();
238 }
239
240 void RobomonTuning::keyReleaseEvent(QKeyEvent *event)
241 {
242         if (event->isAutoRepeat()) {
243                 event->ignore();
244                 return;
245         }
246
247         switch (event->key()) {
248                 case Qt::Key_Up:
249                 case Qt::Key_Down:
250                 case Qt::Key_Left:
251                 case Qt::Key_Right:
252                         break;
253                 default:
254                         event->ignore();
255                         break;
256         }
257         event->accept();
258 }
259
260 void RobomonTuning::closeEvent(QCloseEvent *)
261 {
262         robottype_roboorte_destroy(&orte);
263 }
264
265 /**********************************************************************
266  * ORTE
267  **********************************************************************/
268 void RobomonTuning::createOrte()
269 {
270         memset(&orte, 0, sizeof(orte));
271         if (robottype_roboorte_init(&orte) != 0) {
272                 perror("robottype_roboorte_init");
273                 exit(1);
274         }
275
276         orte.pwr_alert.value = 0;
277
278         /* publishers */
279         robottype_publisher_can_msg_create(&orte, NULL, NULL);
280         robottype_publisher_pwr_ctrl_create(&orte, NULL, NULL);
281         robottype_publisher_pwr_alert_create(&orte, dummy_publisher_callback, NULL);
282
283         /*Power supply hack*/
284         orte.pwr_ctrl.voltage33 = true;
285         orte.pwr_ctrl.voltage50 = true;
286         orte.pwr_ctrl.voltage80 = true;
287 }
288
289 /**********************************************************************
290  * MISCELLANEOUS
291  **********************************************************************/
292
293