]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/robomon/RobomonAtlantis.cpp
Map: edited map flags and created walls
[eurobot/public.git] / src / robomon / RobomonAtlantis.cpp
1 /*
2  * RobomonAtlantis.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 CTU Dragons
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 #include <errno.h>
25
26 #include <orte.h>
27 #include <path_planner.h>
28 #include <robodim.h>
29 #include <sharp.h>
30 #include <trgen.h>
31 #include <map.h>
32 #include <robomath.h>
33 #include <hokuyo.h>
34 #include <actuators.h>
35 #include "PlaygroundScene.h"
36 #include "MiscGui.h"
37 #include "robomon_orte.h"
38 #include "RobomonAtlantis.h"
39 #include "playgroundview.h"
40
41 #include <QCoreApplication>
42 #include <QEvent>
43 #include <QKeyEvent>
44 #include <QDebug>
45 #include <QMessageBox>
46
47 RobomonAtlantis::RobomonAtlantis(QWidget *parent)
48         : QWidget(parent)
49 {
50         QFont font;
51         font.setPointSize(7);
52         setFont(font);
53
54         debugWindowEnabled = false;
55
56         createLeftLayout();
57         createRightLayout();
58
59         QHBoxLayout *mainLayout = new QHBoxLayout;
60         mainLayout->addLayout(leftLayout);
61         mainLayout->addLayout(rightLayout);
62         setLayout(mainLayout);
63
64         createOrte();
65         createRobots();
66         createActions();
67
68         setFocusPolicy(Qt::StrongFocus);
69         sharedMemoryOpened = false;
70         WDBG("Youuuhouuuu!!");
71 }
72
73 /**********************************************************************
74  * GUI
75  **********************************************************************/
76 void RobomonAtlantis::createLeftLayout()
77 {
78         leftLayout = new QVBoxLayout();
79         
80         createDebugGroupBox();
81         debugWindowEnabled = true;
82         createPlaygroundGroupBox();
83         leftLayout->addWidget(playgroundGroupBox);
84         leftLayout->addWidget(debugGroupBox);
85 }
86
87 void RobomonAtlantis::createRightLayout()
88 {
89         rightLayout = new QVBoxLayout();
90         QGridLayout *layout = new QGridLayout();
91         QVBoxLayout *vlayout = new QVBoxLayout();
92         
93         createPositionGroupBox();
94         createMiscGroupBox();
95         createFSMGroupBox();
96         createActuatorsGroupBox();
97         createDIOGroupBox();
98         createPowerGroupBox();
99         createSensorsGroupBox();
100
101         vlayout->addWidget(positionGroupBox);
102         vlayout->addWidget(miscGroupBox);
103         vlayout->addWidget(fsmGroupBox);
104         layout->addLayout(vlayout, 0, 0);
105         layout->addWidget(actuatorsGroupBox, 0, 1);
106 //      layout->addWidget(dioGroupBox, 0, 2);
107
108         rightLayout->addLayout(layout);
109         rightLayout->addWidget(powerGroupBox);
110         rightLayout->addWidget(sensorsGroupBox);
111 }
112
113 void RobomonAtlantis::createPlaygroundGroupBox()
114 {
115         playgroundGroupBox = new QGroupBox(tr("Playground"));
116         QHBoxLayout *layout = new QHBoxLayout();
117
118         playgroundScene = new PlaygroundScene();
119         playgroundSceneView = new PlaygroundView(playgroundScene);
120         //playgroundSceneView->setMinimumWidth(630);
121         //playgroundSceneView->setMinimumHeight(445);
122         playgroundSceneView->setMatrix(QMatrix(1,0,0,-1,0,0), true);
123         playgroundSceneView->fitInView(playgroundScene->itemsBoundingRect());
124         playgroundSceneView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
125         layout->addWidget(playgroundSceneView);
126
127         playgroundGroupBox->setLayout(layout);
128 }
129
130 void RobomonAtlantis::createPositionGroupBox()
131 {
132         positionGroupBox = new QGroupBox(tr("Position state"));
133         positionGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
134         QGridLayout *layout = new QGridLayout();
135         
136         actPosX = new QLineEdit();
137         actPosY = new QLineEdit();
138         actPosPhi = new QLineEdit();
139
140         estPosX = new QLineEdit();
141         estPosY = new QLineEdit();
142         estPosPhi = new QLineEdit();
143         
144         actPosX->setReadOnly(true);
145         actPosY->setReadOnly(true);
146         actPosPhi->setReadOnly(true);
147
148         estPosX->setReadOnly(true);
149         estPosY->setReadOnly(true);
150         estPosPhi->setReadOnly(true);
151
152         layout->addWidget(MiscGui::createLabel("X"), 1, 0);
153         layout->addWidget(MiscGui::createLabel("Y"), 2, 0);
154         layout->addWidget(MiscGui::createLabel("Phi"), 3, 0);
155
156         layout->addWidget(MiscGui::createLabel("X"), 5, 0);
157         layout->addWidget(MiscGui::createLabel("Y"), 6, 0);
158         layout->addWidget(MiscGui::createLabel("Phi"), 7, 0);
159
160         layout->addWidget(MiscGui::createLabel("Reference", Qt::AlignLeft), 0, 1);
161         layout->addWidget(actPosX, 1, 1);
162         layout->addWidget(actPosY, 2, 1);
163         layout->addWidget(actPosPhi, 3, 1);
164
165         layout->addWidget(MiscGui::createLabel("Estimated", Qt::AlignLeft), 4, 1);
166         layout->addWidget(estPosX, 5, 1);
167         layout->addWidget(estPosY, 6, 1);
168         layout->addWidget(estPosPhi, 7, 1);
169
170         positionGroupBox->setLayout(layout);
171 }
172
173 void RobomonAtlantis::createMiscGroupBox()
174 {
175         miscGroupBox = new QGroupBox(tr("Miscellaneous"));
176         miscGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
177         QGridLayout *layout = new QGridLayout();
178
179         showMapPushButton = new QCheckBox(tr("Show &map"));
180         showMapPushButton->setShortcut(tr("m"));
181         
182         layout->addWidget(showMapPushButton, 0, 0);
183         
184         obstacleSimulationCheckBox = new QCheckBox(tr("&Obstacle simulation"));
185         obstacleSimulationCheckBox->setShortcut(tr("o"));
186         layout->addWidget(obstacleSimulationCheckBox);
187
188         startBtn = new QPushButton("Start");
189         layout->addWidget(startBtn);
190         
191         miscGroupBox->setLayout(layout);
192 }
193
194 void RobomonAtlantis::createFSMGroupBox()
195 {
196         fsmGroupBox = new QGroupBox(tr("FSM"));
197         fsmGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
198         QGridLayout *layout = new QGridLayout();
199
200         layout->addWidget(MiscGui::createLabel("Main:"), 1, 0);
201         fsm_main_state = new QLabel();
202         fsm_main_state->setMinimumWidth(100);
203         layout->addWidget(fsm_main_state, 1, 1);
204
205         layout->addWidget(MiscGui::createLabel("Act:"), 2, 0);
206         fsm_act_state = new QLabel();
207         layout->addWidget(fsm_act_state, 2, 1);
208
209         layout->addWidget(MiscGui::createLabel("Motion:"), 3, 0);
210         fsm_motion_state = new QLabel();
211         layout->addWidget(fsm_motion_state, 3, 1);
212
213         fsmGroupBox->setLayout(layout);
214 }
215
216 void RobomonAtlantis::createDebugGroupBox()
217 {
218         debugGroupBox = new QGroupBox(tr("Debug window"));
219         debugGroupBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
220         QHBoxLayout *layout = new QHBoxLayout();
221
222         debugWindow = new QTextEdit();
223         debugWindow->setReadOnly(true);
224
225         layout->addWidget(debugWindow);
226         debugGroupBox->setLayout(layout);
227 }
228
229 void RobomonAtlantis::createActuatorsGroupBox()
230 {
231         actuatorsGroupBox = new QGroupBox(tr("Actuators"));
232         actuatorsGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
233         QHBoxLayout *layout = new QHBoxLayout();
234
235         createMotorsGroupBox();
236         createPickerGroupBox();
237
238         layout->setAlignment(Qt::AlignLeft);
239         layout->addWidget(enginesGroupBox);
240         layout->addWidget(pickerGroupBox);
241         actuatorsGroupBox->setLayout(layout);
242 }
243
244 void RobomonAtlantis::createDIOGroupBox()
245 {
246         dioGroupBox = new QGroupBox(tr("DIO"));
247         QGridLayout *layout = new QGridLayout();
248         QFont font;
249
250         font.setPointSize(5);
251         dioGroupBox->setFont(font);
252
253         for (int i=0; i<8; i++) {
254                 diCheckBox[i] = new QCheckBox(QString("DI%1").arg(i));
255                 doCheckBox[i] = new QCheckBox(QString("D0%1").arg(i));
256                 layout->addWidget(diCheckBox[i], i, 0);
257                 layout->addWidget(doCheckBox[i], i+8, 0);
258         }
259
260         dioGroupBox->setMaximumWidth(70);
261         dioGroupBox->setLayout(layout);
262 }
263
264 void RobomonAtlantis::createPowerGroupBox()
265 {
266         powerGroupBox = new QGroupBox(tr("Power management"));
267         powerGroupBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
268         powerGroupBox->setMaximumWidth(450);
269         QGridLayout *layout = new QGridLayout();
270
271         voltage33CheckBox = new QCheckBox(tr("&3.3V"));
272         voltage50CheckBox = new QCheckBox(tr("&5.0V"));
273         voltage80CheckBox = new QCheckBox(tr("&8.0V"));
274
275         voltage33CheckBox->setShortcut(tr("3"));
276         voltage50CheckBox->setShortcut(tr("5"));
277         voltage80CheckBox->setShortcut(tr("8"));
278
279         layout->addWidget(voltage33CheckBox, 0, 0);
280         layout->addWidget(voltage50CheckBox, 0, 2);
281         layout->addWidget(voltage80CheckBox, 0, 4); //1, 0);
282         layout->addWidget(MiscGui::createLabel("BAT"), 0, 6); //1, 2);
283
284         voltage33LineEdit = new QLineEdit();
285         voltage50LineEdit = new QLineEdit();
286         voltage80LineEdit = new QLineEdit();
287         voltageBATLineEdit = new QLineEdit();
288
289         voltage33LineEdit->setReadOnly(true);
290         voltage50LineEdit->setReadOnly(true);
291         voltage80LineEdit->setReadOnly(true);
292         voltageBATLineEdit->setReadOnly(true);
293
294         layout->addWidget(voltage33LineEdit, 0, 1);
295         layout->addWidget(voltage50LineEdit, 0, 3);
296         layout->addWidget(voltage80LineEdit, 0, 5); //1, 1);
297         layout->addWidget(voltageBATLineEdit, 0, 7); //1, 3);
298
299         powerGroupBox->setLayout(layout);
300 }
301
302 void RobomonAtlantis::createSensorsGroupBox()
303 {
304         sensorsGroupBox = new QGroupBox(tr("Sensors"));
305         sensorsGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
306         sensorsGroupBox->setMaximumWidth(450);
307         QHBoxLayout *layout = new QHBoxLayout();
308
309         layout->addWidget(MiscGui::createLabel("Puck Sharp"));
310         sharpPuck = new QProgressBar();
311         sharpPuck->setFormat("%v mm");
312         sharpPuck->setTextVisible(true);
313         sharpPuck->setMaximum(250);
314         layout->addWidget(sharpPuck);
315
316         layout->addWidget(MiscGui::createLabel("Lift"));
317         liftPos = new QProgressBar();
318         liftPos->setFormat("%v IRC");
319         liftPos->setTextVisible(true);
320         liftPos->setMaximum(1000);
321         layout->addWidget(liftPos);
322         
323         layout->addWidget(MiscGui::createLabel("Pusher"));
324         pusherPos = new QProgressBar();
325         pusherPos->setFormat("%v IRC");
326         pusherPos->setTextVisible(true);
327         pusherPos->setMaximum(250);
328         layout->addWidget(pusherPos);
329         
330         sensorsGroupBox->setLayout(layout);
331 }
332
333 void RobomonAtlantis::createMotorsGroupBox()
334 {
335         enginesGroupBox = new QGroupBox(tr("Motors"));
336         QVBoxLayout *layout = new QVBoxLayout();
337         QHBoxLayout *layout1 = new QHBoxLayout();
338         QHBoxLayout *layout2 = new QHBoxLayout();
339
340         leftMotorSlider = new QSlider(Qt::Vertical);
341         rightMotorSlider = new QSlider(Qt::Vertical);
342         bothMotorsCheckBox = new QCheckBox(tr("Lock both"));
343         stopMotorsPushButton = new QPushButton(tr("Stop Motors"));
344
345         leftMotorSlider->setMinimum(-100);
346         leftMotorSlider->setMaximum(100);
347         leftMotorSlider->setTracking(false);
348         leftMotorSlider->setTickPosition(QSlider::TicksLeft);
349
350         rightMotorSlider->setMinimum(-100);
351         rightMotorSlider->setMaximum(100);
352         rightMotorSlider->setTracking(false);
353         rightMotorSlider->setTickPosition(QSlider::TicksRight);
354
355         stopMotorsPushButton->setMaximumWidth(90);
356
357         layout1->addWidget(leftMotorSlider);
358         layout1->addWidget(MiscGui::createLabel("0"));
359         layout1->addWidget(rightMotorSlider);
360
361         layout2->addWidget(bothMotorsCheckBox);
362
363         layout->addWidget(MiscGui::createLabel("100"));
364         layout->addLayout(layout1);
365         layout->addWidget(MiscGui::createLabel("-100"));
366         layout->addLayout(layout2);
367         layout->addWidget(stopMotorsPushButton);
368         enginesGroupBox->setLayout(layout);
369 }
370
371 void RobomonAtlantis::createPickerGroupBox()
372 {
373         pickerGroupBox = new QGroupBox(tr("Picker"));
374         QVBoxLayout *layout = new QVBoxLayout();
375
376         leftBeltCheckBox = new QCheckBox(tr("L Belt"));
377         rightBeltCheckBox = new QCheckBox(tr("R Belt"));
378         leftChelaCheckBox = new QCheckBox(tr("L Chela"));
379         rightChelaCheckBox = new QCheckBox(tr("R Chela"));
380         leftBeltDial = new QDial();
381         rightBeltDial = new QDial();
382         leftChelaDial = new QDial();
383         rightChelaDial = new QDial();
384         pickPushButton = new QPushButton(tr("Pick!"));
385
386         leftBeltDial->setMinimum(0);
387         leftBeltDial->setMaximum(200);
388         leftBeltDial->setValue(100);
389
390         rightBeltDial->setMinimum(0);
391         rightBeltDial->setMaximum(200);
392         rightBeltDial->setValue(100);
393
394         leftChelaDial->setMinimum(0);
395         leftChelaDial->setMaximum(255);
396         leftChelaDial->setValue(127);
397
398         rightChelaDial->setMinimum(0);
399         rightChelaDial->setMaximum(255);
400         rightChelaDial->setValue(127);
401
402         layout->addWidget(leftChelaDial);
403         layout->addWidget(leftChelaCheckBox);
404         layout->addWidget(rightChelaDial);
405         layout->addWidget(rightChelaCheckBox);
406         layout->addWidget(leftBeltDial);
407         layout->addWidget(leftBeltCheckBox);
408         layout->addWidget(rightBeltDial);
409         layout->addWidget(rightBeltCheckBox);
410         layout->addWidget(pickPushButton);
411
412         pickerGroupBox->setLayout(layout);
413 }
414
415 void RobomonAtlantis::createRobots()
416 {
417         robotActPos = new Robot(QPen(Qt::darkBlue), QBrush(Qt::NoBrush));
418         robotActPos->setZValue(11);
419         robotEstPos = new Robot(QPen(), QBrush(Qt::darkGray));
420         robotEstPos->setZValue(10);
421
422         playgroundScene->addItem(robotActPos);
423         playgroundScene->addItem(robotEstPos);
424 }
425
426 /**********************************************************************
427  * GUI actions
428  **********************************************************************/
429 void RobomonAtlantis::createActions()
430 {
431         /* power management */
432         connect(voltage33CheckBox, SIGNAL(stateChanged(int)), 
433                         this, SLOT(setVoltage33(int)));
434         connect(voltage50CheckBox, SIGNAL(stateChanged(int)), 
435                         this, SLOT(setVoltage50(int)));
436         connect(voltage80CheckBox, SIGNAL(stateChanged(int)), 
437                         this, SLOT(setVoltage80(int)));
438
439         /* motors */
440         connect(leftMotorSlider, SIGNAL(valueChanged(int)), 
441                         this, SLOT(setLeftMotor(int)));
442         connect(rightMotorSlider, SIGNAL(valueChanged(int)), 
443                         this, SLOT(setRightMotor(int)));
444         connect(stopMotorsPushButton, SIGNAL(clicked()), 
445                         this, SLOT(stopMotors()));
446
447         /* DIO */
448         for (int i=0; i<8; i++)
449                 connect(doCheckBox[0], SIGNAL(stateChanged(int)), 
450                                         this, SLOT(setDO(int)));
451
452         /* path planning map */
453         connect(showMapPushButton, SIGNAL(clicked()),
454                         this, SLOT(showMap()));
455         
456         connect(startBtn, SIGNAL(clicked()), this, SLOT(sendStart()));
457
458         /* obstacle simulation */
459         simulationEnabled = 0;
460         connect(obstacleSimulationCheckBox, SIGNAL(stateChanged(int)),
461                         this, SLOT(setSimulation(int)));
462         connect(obstacleSimulationCheckBox, SIGNAL(stateChanged(int)),
463                         this, SLOT(setObstacleSimulation(int)));
464         connect(obstacleSimulationCheckBox, SIGNAL(stateChanged(int)),
465                         playgroundScene, SLOT(showObstacle(int)));
466         connect(playgroundScene, SIGNAL(obstacleChanged(QPointF)), 
467                         this, SLOT(changeObstacle(QPointF)));
468
469         /* Actuators */
470         connect(pickPushButton, SIGNAL(clicked()), this, SLOT(pick()));
471         connect(leftBeltCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setBelts(int)));
472         connect(rightBeltCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setBelts(int)));
473         connect(leftChelaCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setChelae(int)));
474         connect(rightChelaCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setChelae(int)));
475         connect(leftBeltDial, SIGNAL(valueChanged(int)), this, SLOT(setBelts(int)));
476         connect(rightBeltDial, SIGNAL(valueChanged(int)), this, SLOT(setBelts(int)));
477         connect(leftChelaDial, SIGNAL(valueChanged(int)), this, SLOT(setChelae(int)));
478         connect(rightChelaDial, SIGNAL(valueChanged(int)), this, SLOT(setChelae(int)));
479 }
480
481 void RobomonAtlantis::setVoltage33(int state)
482 {
483         if (state)
484                 orte.pwr_ctrl.voltage33 = true;
485         else
486                 orte.pwr_ctrl.voltage33 = false;
487 }
488
489 void RobomonAtlantis::setVoltage50(int state)
490 {
491         if (state)
492                 orte.pwr_ctrl.voltage50 = true;
493         else
494                 orte.pwr_ctrl.voltage50 = false;
495 }
496
497 void RobomonAtlantis::setVoltage80(int state)
498 {
499         if (state)
500                 orte.pwr_ctrl.voltage80 = true;
501         else
502                 orte.pwr_ctrl.voltage80 = false;
503 }
504
505 void RobomonAtlantis::setLeftMotor(int value)
506 {
507         short int leftMotor;
508         short int rightMotor;
509         
510         if(bothMotorsCheckBox->isChecked())
511                 rightMotorSlider->setValue(value);
512
513         leftMotor = (short int)(MOTOR_LIMIT * (leftMotorSlider->value()/100.0));
514         rightMotor = (short int)(MOTOR_LIMIT * (rightMotorSlider->value()/100.0));
515         
516         orte.motion_speed.left = leftMotor;
517         orte.motion_speed.right = rightMotor;
518         
519 }
520
521 void RobomonAtlantis::setRightMotor(int value)
522 {
523         short int leftMotor;
524         short int rightMotor;
525         
526         if(bothMotorsCheckBox->isChecked())
527                 leftMotorSlider->setValue(value);
528
529         leftMotor = (short int)(MOTOR_LIMIT * (leftMotorSlider->value()/100.0));
530         rightMotor = (short int)(MOTOR_LIMIT * (rightMotorSlider->value()/100.0));
531         
532         orte.motion_speed.left = leftMotor;
533         orte.motion_speed.right = rightMotor;
534         
535 }
536
537 void RobomonAtlantis::stopMotors()
538 {
539         leftMotorSlider->setValue(0);
540         rightMotorSlider->setValue(0);
541 }
542
543
544 void RobomonAtlantis::setDO(int state)
545 {
546         Q_UNUSED(state);
547         /* FIXME: digital output control comes here */
548 }
549
550 void RobomonAtlantis::pick()
551 {
552         // TODO: send signal to the fsmact
553 }
554
555 void RobomonAtlantis::setBelts(int value)
556 {
557         Q_UNUSED(value);
558         unsigned char leftBelt;
559         unsigned char rightBelt;
560
561         leftBelt = (unsigned char)leftBeltDial->value();
562         rightBelt = (unsigned char)rightBeltDial->value();
563
564         act_belts(leftBelt, rightBelt);
565 }
566
567 void RobomonAtlantis::setChelae(int value)
568 {
569         Q_UNUSED(value);
570         unsigned char leftChela;
571         unsigned char rightChela;
572
573         leftChela = (unsigned char)leftChelaDial->value();
574         rightChela = (unsigned char)rightChelaDial->value();
575
576         act_chelae(leftChela, rightChela);
577 }
578
579 void RobomonAtlantis::showMap()
580 {
581         openSharedMemory();
582
583         if (sharedMemoryOpened == false)
584                 return;
585
586         mapTimer = new QTimer(this);
587         connect(mapTimer, SIGNAL(timeout()), this, SLOT(paintMap()));
588         mapTimer->start(200);
589
590         disconnect(showMapPushButton, SIGNAL(clicked()), this, SLOT(showMap()));
591         connect(showMapPushButton, SIGNAL(clicked()),
592                                 this, SLOT(showPlayground()));
593         playgroundScene->showMap(true);
594 }
595
596 void RobomonAtlantis::showPlayground()
597 {
598         mapTimer->stop();
599         disconnect(mapTimer, SIGNAL(timeout()), this, SLOT(paintMap()));
600
601         disconnect(showMapPushButton, SIGNAL(clicked()),
602                                 this, SLOT(showPlayground()));
603         connect(showMapPushButton, SIGNAL(clicked()), this, SLOT(showMap()));
604         playgroundScene->showMap(false);
605 }
606
607 void RobomonAtlantis::paintMap()
608 {
609         using namespace Qt;
610         int x, y;
611         struct map *map = ShmapIsMapInit();
612
613         if (!map) return;
614         
615         for(int i=0; i < MAP_WIDTH; i++) {
616                 for(int j=0; j<MAP_HEIGHT; j++) {
617                         QColor color;
618                                 
619                         struct map_cell *cell = &map->cells[j][i];
620                         color = lightGray;
621
622                         if ((cell->flags & MAP_FLAG_WALL) &&
623                             (cell->flags & MAP_FLAG_INVALIDATE_WALL) == 0)
624                                 color = darkYellow;
625                         if (cell->flags & MAP_FLAG_IGNORE_OBST)
626                                 color = darkGreen;
627                         if (cell->flags & MAP_FLAG_SIMULATED_WALL)
628                                 color = yellow;
629                         if (cell->flags & MAP_FLAG_PATH)
630                                 color = darkRed;
631                         if (cell->flags & MAP_FLAG_START)
632                                 color = red;
633                         if (cell->flags & MAP_FLAG_GOAL)
634                                 color = green;
635                         if (cell->detected_obstacle) {
636                                 QColor c1(color), c2(blue);
637                                 double f = (double)cell->detected_obstacle/MAP_NEW_OBSTACLE*0.7;
638                                 QColor c(c1.red()   + (int)(f*(c2.red()   - c1.red())),
639                                          c1.green() + (int)(f*(c2.green() - c1.green())),
640                                          c1.blue()  + (int)(f*(c2.blue()  - c1.blue())));
641                                 color = c;
642                         }
643                         if (cell->flags & MAP_FLAG_DET_OBST)
644                                 color = cyan;
645                         
646                         playgroundScene->setMapColor(i, j, color);
647                 }
648         }
649 }
650
651 void RobomonAtlantis::setSimulation(int state)
652 {
653         if(state) { 
654                 robottype_publisher_hokuyo_scan_create(&orte, 
655                                                        dummy_publisher_callback, this);
656         } else {
657                 if (!simulationEnabled)
658                         return;
659                 robottype_publisher_hokuyo_scan_destroy(&orte);
660         }
661         simulationEnabled = state;
662 }
663
664 /*!     
665         \fn RobomonAtlantis::setObstacleSimulation(int state)
666  */
667 void RobomonAtlantis::setObstacleSimulation(int state)
668 {
669         if (state) {
670                 /* TODO Maybe it is possible to attach only once to Shmap */
671                 ShmapInit(0);
672                 obstacleSimulationTimer = new QTimer(this);
673                 connect(obstacleSimulationTimer, SIGNAL(timeout()), 
674                         this, SLOT(simulateObstaclesHokuyo()));
675                 obstacleSimulationTimer->start(100);
676                 setMouseTracking(true);
677         } else {
678                 if (obstacleSimulationTimer) 
679                         delete obstacleSimulationTimer;
680                 //double distance = 0.8;
681         }
682 }
683
684
685 void RobomonAtlantis::simulateObstaclesHokuyo()
686 {
687         double distance, wall_distance;
688         int i;
689         uint16_t *hokuyo = orte.hokuyo_scan.data;
690         
691         for (i=0; i<HOKUYO_CLUSTER_CNT; i++) {
692                 wall_distance = distanceToWallHokuyo(i);
693                 distance = distanceToObstacleHokuyo(i, simulatedObstacle, SIM_OBST_SIZE_M/*meters*/);
694                 if (wall_distance < distance) 
695                         distance = wall_distance;
696                 hokuyo[i] = distance*1000;
697         }
698         ORTEPublicationSend(orte.publication_hokuyo_scan);
699         
700 }
701
702 void RobomonAtlantis::changeObstacle(QPointF position)
703 {
704         if (!simulationEnabled) {
705                 simulationEnabled = 1;
706                 obstacleSimulationCheckBox->setChecked(true);
707         }
708
709         simulatedObstacle.x = position.x();
710         simulatedObstacle.y = position.y();
711         simulateObstaclesHokuyo();
712 }       
713
714 /**********************************************************************
715  * EVENTS
716  **********************************************************************/
717 bool RobomonAtlantis::event(QEvent *event)
718 {
719         switch (event->type()) {
720                 case QEVENT(QEV_MOTION_STATUS):
721                         emit motionStatusReceivedSignal();
722                         break;
723                 case QEVENT(QEV_ACTUAL_POSITION):
724                         emit actualPositionReceivedSignal();
725                         break;
726                 case QEVENT(QEV_ESTIMATED_POSITION):
727                         emit estimatedPositionReceivedSignal();
728                         break;
729                 case QEVENT(QEV_DI):
730                         emit diReceivedSignal();
731                         break;
732                 case QEVENT(QEV_ACCELEROMETER):
733                         emit accelerometerReceivedSignal();
734                         break;
735                 case QEVENT(QEV_ACCUMULATOR):
736                         emit accumulatorReceivedSignal();
737                         break;
738                 case QEVENT(QEV_POWER_VOLTAGE):
739                         emit powerVoltageReceivedSignal();
740                         break;
741                 case QEVENT(QEV_FSM_MAIN):
742                         fsm_main_state->setText(orte.fsm_main.state_name);
743                         break;
744                 case QEVENT(QEV_FSM_ACT):
745                         fsm_act_state->setText(orte.fsm_act.state_name);
746                         break;
747                 case QEVENT(QEV_FSM_MOTION):
748                         fsm_motion_state->setText(orte.fsm_motion.state_name);
749                         break;
750                 case QEVENT(QEV_PUCK_DISTANCE):
751                         sharpPuck->setValue(orte.puck_distance.distance*1000);
752                         break;
753                 case QEVENT(QEV_LIFT_PUSHER):
754                         liftPos->setValue(orte.actuator_status.lift_pos);
755                         pusherPos->setValue(orte.actuator_status.pusher_pos);
756                         break;
757                 default:
758                         if (event->type() == QEvent::Close)
759                                 closeEvent((QCloseEvent *)event);
760                         else if (event->type() == QEvent::KeyPress)
761                                 keyPressEvent((QKeyEvent *)event);
762                         else if (event->type() == QEvent::KeyRelease)
763                                 keyReleaseEvent((QKeyEvent *)event);
764                         else if (event->type() == QEvent::FocusIn)
765                                 grabKeyboard();
766                         else if (event->type() == QEvent::FocusOut)
767                                 releaseKeyboard();
768                         else {
769                                 event->ignore();
770                                 return false;
771                         }
772                         break;
773         }
774         event->accept();
775         return true;
776 }
777
778 void RobomonAtlantis::keyPressEvent(QKeyEvent *event)
779 {
780         double peak, gain;
781
782         if (event->isAutoRepeat()) {
783                 switch (event->key()) {
784                         case Qt::Key_Down:
785                                 peak = leftMotorSlider->minimum()/2;
786                                 if (leftMotorValue < peak ||
787                                         rightMotorValue < peak)
788                                         gain = 1.01;
789                                 else
790                                         gain = 1.3;
791                                 leftMotorValue *= gain;
792                                 rightMotorValue *= gain;
793                                 leftMotorSlider->setValue((int)leftMotorValue);
794                                 rightMotorSlider->setValue((int)rightMotorValue);
795                                 break;
796
797                         case Qt::Key_Up:
798                         case Qt::Key_Left:
799                         case Qt::Key_Right:
800                                 peak = leftMotorSlider->maximum()/2;
801                                 if (leftMotorValue > peak ||
802                                         rightMotorValue > peak)
803                                         gain = 1.01;
804                                 else
805                                         gain = 1.3;
806                                 leftMotorValue *= gain;
807                                 rightMotorValue *= gain;
808                                 leftMotorSlider->setValue((int)leftMotorValue);
809                                 rightMotorSlider->setValue((int)rightMotorValue);
810                                 break;
811
812                         default:
813                                 event->ignore();
814                                 break;
815                 }
816                 return;
817         }
818
819         switch (event->key()) {
820                 case Qt::Key_Up:
821                         leftMotorValue = 1;
822                         rightMotorValue = 1;
823                         bothMotorsCheckBox->setChecked(true);
824                         leftMotorSlider->setValue((int)leftMotorValue);
825                         setLeftMotor((int)leftMotorValue);
826                         break;
827                 case Qt::Key_Down:
828                         leftMotorValue = -1;
829                         rightMotorValue = -1;
830                         bothMotorsCheckBox->setChecked(true);
831                         leftMotorSlider->setValue((int)leftMotorValue);
832                         setLeftMotor((int)leftMotorValue);
833                         break;
834                 case Qt::Key_Left:
835                         leftMotorValue = -1;
836                         rightMotorValue = 1;
837                         leftMotorSlider->setValue((int)leftMotorValue);
838                         rightMotorSlider->setValue((int)rightMotorValue);
839                         setLeftMotor((int)leftMotorValue);
840                         setRightMotor((int)leftMotorValue);
841                         break;
842                 case Qt::Key_Right:
843                         leftMotorValue = 1;
844                         rightMotorValue = -1;
845                         leftMotorSlider->setValue((int)leftMotorValue);
846                         rightMotorSlider->setValue((int)rightMotorValue);
847                         setLeftMotor((int)leftMotorValue);
848                         setRightMotor((int)rightMotorValue);
849                         break;
850                 default:
851                         event->ignore();
852                         break;
853         }
854         event->accept();
855 }
856
857 void RobomonAtlantis::keyReleaseEvent(QKeyEvent *event)
858 {
859         if (event->isAutoRepeat()) {
860                 event->ignore();
861                 return;
862         }
863
864         switch (event->key()) {
865                 case Qt::Key_Up:
866                 case Qt::Key_Down:
867                 case Qt::Key_Left:
868                 case Qt::Key_Right:
869                         leftMotorValue = 0;
870                         rightMotorValue = 0;
871                         bothMotorsCheckBox->setChecked(false);
872                         leftMotorSlider->setValue((int)leftMotorValue);
873                         rightMotorSlider->setValue((int)rightMotorValue);
874                         break;
875                 default:
876                         event->ignore();
877                         break;
878         }
879         event->accept();
880 }
881
882 void RobomonAtlantis::closeEvent(QCloseEvent *)
883 {
884         robottype_roboorte_destroy(&orte);
885 }
886
887 /**********************************************************************
888  * ORTE
889  **********************************************************************/
890 void RobomonAtlantis::createOrte()
891 {
892         int rv;
893
894         orte.strength = 11;
895         
896         rv = robottype_roboorte_init(&orte);
897         if (rv) {
898                 printf("RobomonAtlantis: Unable to initialize ORTE\n");
899         }
900
901         /* publishers */
902         robottype_publisher_motion_speed_create(&orte, dummy_publisher_callback, NULL);
903
904         robottype_publisher_pwr_ctrl_create(&orte, dummy_publisher_callback, NULL);
905         robottype_publisher_lift_create(&orte, NULL, &orte);
906         robottype_publisher_pusher_create(&orte, NULL, &orte);
907         robottype_publisher_chelae_create(&orte, NULL, &orte);
908         robottype_publisher_belts_create(&orte, NULL, &orte);
909         robottype_publisher_holder_create(&orte, NULL, &orte);
910         robottype_publisher_robot_cmd_create(&orte, NULL, &orte);
911
912         /* subscribers */
913         robottype_subscriber_pwr_voltage_create(&orte, 
914                                 receivePowerVoltageCallBack, this);
915         robottype_subscriber_motion_status_create(&orte, 
916                                 receiveMotionStatusCallBack, this);
917         robottype_subscriber_ref_pos_create(&orte, 
918                                 receiveActualPositionCallBack, this);
919         robottype_subscriber_est_pos_create(&orte, 
920                                 receiveEstimatedPositionCallBack, this);
921         robottype_subscriber_fsm_main_create(&orte, 
922                                              rcv_fsm_main_cb, this);
923         robottype_subscriber_fsm_motion_create(&orte, 
924                                              rcv_fsm_motion_cb, this);
925         robottype_subscriber_fsm_act_create(&orte, 
926                                              rcv_fsm_act_cb, this);
927         robottype_subscriber_puck_distance_create(&orte, generic_rcv_cb, new OrteCallbackInfo(this, QEV_PUCK_DISTANCE));
928         robottype_subscriber_actuator_status_create(&orte, generic_rcv_cb, new OrteCallbackInfo(this, QEV_LIFT_PUSHER));
929
930
931         /*createDISubscriber(this, &orteData);*/
932         /*createAccelerometerSubscriber(this, &orteData);*/
933         /*createAccumulatorSubscriber(this, &orteData);*/
934         /* motors */
935         orte.motion_speed.left = 0;
936         orte.motion_speed.right = 0;
937         /* power management */
938         orte.pwr_ctrl.voltage33 = true;
939         orte.pwr_ctrl.voltage50 = true;
940         orte.pwr_ctrl.voltage80 = true;
941         voltage33CheckBox->setChecked(true);
942         voltage50CheckBox->setChecked(true);
943         voltage80CheckBox->setChecked(true);
944
945         act_init(&orte);
946
947         /* set actions to do when we receive data from orte */
948         connect(this, SIGNAL(motionStatusReceivedSignal()), 
949                         this, SLOT(motionStatusReceived()));
950         connect(this, SIGNAL(actualPositionReceivedSignal()), 
951                         this, SLOT(actualPositionReceived()));
952         connect(this, SIGNAL(estimatedPositionReceivedSignal()), 
953                         this, SLOT(estimatedPositionReceived()));
954         connect(this, SIGNAL(diReceivedSignal()), 
955                         this, SLOT(diReceived()));
956         connect(this, SIGNAL(accelerometerReceivedSignal()), 
957                         this, SLOT(accelerometerReceived()));
958         connect(this, SIGNAL(accumulatorReceivedSignal()), 
959                         this, SLOT(accumulatorReceived()));
960         connect(this, SIGNAL(powerVoltageReceivedSignal()), 
961                         this, SLOT(powerVoltageReceived()));
962 }
963
964 void RobomonAtlantis::motionStatusReceived()
965 {
966         WDBG("ORTE received: motion status");
967 }
968
969 void RobomonAtlantis::actualPositionReceived()
970 {
971         actPosX->setText(QString("%1").arg(orte.ref_pos.x, 0, 'f', 3));
972         actPosY->setText(QString("%1").arg(orte.ref_pos.y, 0, 'f', 3));
973         actPosPhi->setText(QString("%1(%2)")
974                         .arg(DEGREES(orte.ref_pos.phi), 0, 'f', 0)
975                         .arg(orte.ref_pos.phi, 0, 'f', 1));
976         robotActPos->moveRobot(orte.ref_pos.x, 
977                 orte.ref_pos.y, orte.ref_pos.phi);
978 }
979
980 void RobomonAtlantis::estimatedPositionReceived()
981 {
982         estPosX->setText(QString("%1").arg(orte.est_pos.x, 0, 'f', 3));
983         estPosY->setText(QString("%1").arg(orte.est_pos.y, 0, 'f', 3));
984         estPosPhi->setText(QString("%1(%2)")
985                         .arg(DEGREES(orte.est_pos.phi), 0, 'f', 0)
986                         .arg(orte.est_pos.phi, 0, 'f', 1));
987         robotEstPos->moveRobot(orte.est_pos.x, 
988                 orte.est_pos.y, orte.est_pos.phi);
989 }
990
991 void RobomonAtlantis::diReceived()
992 {
993         WDBG("ORTE received: DI");
994 }
995
996 void RobomonAtlantis::accelerometerReceived()
997 {
998         WDBG("ORTE received: accelerometer");
999 }
1000
1001 void RobomonAtlantis::accumulatorReceived()
1002 {
1003         WDBG("ORTE received: accumulator");
1004 }
1005
1006 void RobomonAtlantis::powerVoltageReceived()
1007 {
1008         voltage33LineEdit->setText(QString("%1").arg(
1009                         orte.pwr_voltage.voltage33, 0, 'f', 3));
1010         voltage50LineEdit->setText(QString("%1").arg(
1011                         orte.pwr_voltage.voltage50, 0, 'f', 3));
1012         voltage80LineEdit->setText(QString("%1").arg(
1013                         orte.pwr_voltage.voltage80, 0, 'f', 3));
1014         voltageBATLineEdit->setText(QString("%1").arg(
1015                         orte.pwr_voltage.voltageBAT, 0, 'f', 3));
1016
1017 }
1018
1019 /**********************************************************************
1020  * MISCELLANEOUS
1021  **********************************************************************/
1022 void RobomonAtlantis::openSharedMemory()
1023 {
1024         int segmentId;
1025         int sharedSegmentSize;
1026
1027         if (sharedMemoryOpened)
1028                 return;
1029
1030         sharedSegmentSize = sizeof(unsigned int) * MAP_WIDTH * MAP_HEIGHT;
1031         
1032         /* Get segment identificator in a read only mode  */
1033         segmentId = shmget(SHM_MAP_KEY, sharedSegmentSize, S_IRUSR);
1034         if(segmentId == -1) {
1035                 QMessageBox::critical(this, "robomon",
1036                                 "Unable to open shared memory segment!");
1037                 return;
1038         }
1039         
1040         /* Init Shmap */
1041         ShmapInit(0);
1042         
1043         /* Attach the shared memory segment */
1044         //map =  (_Map*)shmat (segmentId, (void*) 0, 0);
1045
1046         sharedMemoryOpened = true;
1047 }
1048
1049 double RobomonAtlantis::distanceToWallHokuyo(int beamnum)
1050 {
1051         double distance=4.0, min_distance=4.0;
1052         int i,j;
1053         Point wall;
1054         struct map *map = ShmapIsMapInit();
1055
1056         if (!map) return min_distance;
1057         
1058         // Simulate obstacles
1059         for(j=0;j<MAP_HEIGHT;j++) {
1060                 for (i=0;i<MAP_WIDTH;i++) {
1061                         struct map_cell *cell = &map->cells[j][i];
1062                         if( cell->flags & MAP_FLAG_SIMULATED_WALL) {
1063                                 // WALL
1064                                 ShmapCell2Point(i, j, &wall.x, &wall.y);
1065                                 
1066                                 distance = distanceToObstacleHokuyo(beamnum, wall, MAP_CELL_SIZE_M);
1067                                 if (distance<min_distance) min_distance = distance;
1068                         }
1069                 }
1070         }
1071
1072         return min_distance;
1073 }
1074
1075 /** 
1076  * Calculation for Hokuyo simulation. Calculates distance that would
1077  * be returned by Hokuyo sensors, if there is only one obstacle (as
1078  * specified by parameters).
1079  *
1080  * @param beamnum Hokuyo's bean number [0..HOKUYO_CLUSTER_CNT]
1081  * @param obstacle Position of the obstacle (x, y in meters).
1082  * @param obstacleSize Size (diameter) of the obstacle in meters.
1083  * 
1084  * @return Distance measured by sensors in meters.
1085  */    
1086 double RobomonAtlantis::distanceToObstacleHokuyo(int beamnum, Point obstacle, double obstacleSize)
1087 {  
1088         struct est_pos_type e = orte.est_pos;
1089         double sensor_a;
1090         struct sharp_pos s;
1091
1092         s.x = HOKUYO_CENTER_OFFSET_M;
1093         s.y = 0.0;
1094         s.ang = HOKUYO_CLUSTER_TO_RAD(beamnum);
1095
1096         Point sensor(e.x + s.x*cos(e.phi) - s.y*sin(e.phi),
1097                      e.y + s.x*sin(e.phi) + s.y*cos(e.phi));
1098         sensor_a = e.phi + s.ang;
1099         
1100         const double sensorRange = 4.0; /*[meters]*/
1101         
1102         double distance, angle;
1103             
1104         angle = sensor.angleTo(obstacle) - sensor_a;
1105         angle = fmod(angle, 2.0*M_PI);
1106         if (angle > +M_PI) angle -= 2.0*M_PI;
1107         if (angle < -M_PI) angle += 2.0*M_PI;
1108         angle = fabs(angle);
1109         distance = sensor.distanceTo(obstacle)-0.11;
1110         if (angle < atan(obstacleSize/2.0 / (distance+0.001))) {
1111                 // We can see the obstackle from here.
1112                 if (angle < M_PI/2.0) {
1113                     distance = distance/cos(angle);
1114                 }
1115                 if (distance > sensorRange) 
1116                         distance = sensorRange;
1117         } else {
1118                 distance = sensorRange;
1119         }
1120
1121         return distance;
1122 }
1123
1124 void RobomonAtlantis::sendStart()
1125 {
1126         orte.robot_cmd.start = 1;
1127         ORTEPublicationSend(orte.publication_robot_cmd);
1128 }