2 * RobomonAtlantis.cpp 07/10/31
4 * Robot`s visualization and control GUI for robot of the
5 * Eurobot 2008 (Mission to Mars).
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
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
27 #include <path_planner.h>
33 #include <lidar_params.h>
34 #include <actuators.h>
35 #include "PlaygroundScene.h"
37 #include "robomon_orte.h"
38 #include "RobomonAtlantis.h"
39 #include "playgroundview.h"
42 #include <QCoreApplication>
46 #include <QMessageBox>
48 RobomonAtlantis::RobomonAtlantis(QStatusBar *_statusBar)
49 : QWidget(0), statusBar(_statusBar)
55 debugWindowEnabled = false;
60 QHBoxLayout *mainLayout = new QHBoxLayout;
61 mainLayout->addLayout(leftLayout);
62 mainLayout->addLayout(rightLayout);
63 setLayout(mainLayout);
70 setFocusPolicy(Qt::StrongFocus);
71 sharedMemoryOpened = false;
72 WDBG("Youuuhouuuu!!");
75 /**********************************************************************
77 **********************************************************************/
78 void RobomonAtlantis::createLeftLayout()
80 leftLayout = new QVBoxLayout();
82 createDebugGroupBox();
83 debugWindowEnabled = true;
84 createPlaygroundGroupBox();
85 leftLayout->addWidget(playgroundGroupBox);
86 //leftLayout->addWidget(debugGroupBox); // FIXME: move this to separate tab
89 void RobomonAtlantis::createRightLayout()
91 rightLayout = new QVBoxLayout();
93 createPositionGroupBox();
94 createObstSimGroupBox();
97 createActuatorsGroupBox();
98 createPowerGroupBox();
100 rightLayout->addWidget(positionGroupBox);
101 rightLayout->addWidget(obstSimGroupBox);
102 rightLayout->addWidget(miscGroupBox);
103 rightLayout->addWidget(fsmGroupBox);
104 rightLayout->addWidget(actuatorsGroupBox);
107 void RobomonAtlantis::createPlaygroundGroupBox()
109 playgroundGroupBox = new QGroupBox(tr("Playground"));
110 QHBoxLayout *layout = new QHBoxLayout();
112 playgroundScene = new PlaygroundScene();
113 playgroundSceneView = new PlaygroundView(playgroundScene);
114 playgroundSceneView->setMatrix(QMatrix(1,0,0,-1,0,0), true);
115 playgroundSceneView->fitInView(playgroundScene->itemsBoundingRect());
116 playgroundSceneView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
117 playgroundSceneView->setMouseTracking(true);
118 layout->addWidget(playgroundSceneView);
120 playgroundGroupBox->setLayout(layout);
123 void RobomonAtlantis::createPositionGroupBox()
125 positionGroupBox = new QGroupBox(tr("Position state"));
126 positionGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
127 QGridLayout *layout = new QGridLayout();
129 actPosX = new QLineEdit();
130 actPosY = new QLineEdit();
131 actPosPhi = new QLineEdit();
133 estPosX = new QLineEdit();
134 estPosY = new QLineEdit();
135 estPosPhi = new QLineEdit();
137 actPosX->setReadOnly(true);
138 actPosY->setReadOnly(true);
139 actPosPhi->setReadOnly(true);
141 estPosX->setReadOnly(true);
142 estPosY->setReadOnly(true);
143 estPosPhi->setReadOnly(true);
145 layout->addWidget(MiscGui::createLabel("X"), 1, 0);
146 layout->addWidget(MiscGui::createLabel("Y"), 2, 0);
147 layout->addWidget(MiscGui::createLabel("Phi"), 3, 0);
149 layout->addWidget(MiscGui::createLabel("X"), 5, 0);
150 layout->addWidget(MiscGui::createLabel("Y"), 6, 0);
151 layout->addWidget(MiscGui::createLabel("Phi"), 7, 0);
153 layout->addWidget(MiscGui::createLabel("Reference", Qt::AlignLeft), 0, 1);
154 layout->addWidget(actPosX, 1, 1);
155 layout->addWidget(actPosY, 2, 1);
156 layout->addWidget(actPosPhi, 3, 1);
158 layout->addWidget(MiscGui::createLabel("Estimated (indep. odo.)", Qt::AlignLeft), 4, 1);
159 layout->addWidget(estPosX, 5, 1);
160 layout->addWidget(estPosY, 6, 1);
161 layout->addWidget(estPosPhi, 7, 1);
163 positionGroupBox->setLayout(layout);
166 void RobomonAtlantis::createMiscGroupBox()
168 miscGroupBox = new QGroupBox(tr("Miscellaneous"));
169 miscGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
170 QGridLayout *layout = new QGridLayout();
172 startPlug = new QCheckBox("&Start plug");
173 layout->addWidget(startPlug);
175 colorChoser = new QCheckBox("&Team color");
176 layout->addWidget(colorChoser);
178 strategyButton= new QPushButton(tr("Strategy"));
179 layout->addWidget(strategyButton);
181 miscGroupBox->setLayout(layout);
184 void RobomonAtlantis::createObstSimGroupBox()
186 obstSimGroupBox = new QGroupBox(tr("Obstacle simulation"));
187 obstSimGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
188 QGridLayout *layout = new QGridLayout();
190 hokuyoSimCheckBox = new QCheckBox(tr("&Hokuyo lidar simulation"));
191 hokuyoSimCheckBox->setShortcut(tr("h"));
192 layout->addWidget(hokuyoSimCheckBox);
194 sick331SimCheckBox = new QCheckBox(tr("Sick Tim &331 lidar simulation"));
195 sick331SimCheckBox->setShortcut(tr("3"));
196 layout->addWidget(sick331SimCheckBox);
198 sick551SimCheckBox = new QCheckBox(tr("Sick Tim &551 lidar simulation"));
199 sick551SimCheckBox->setShortcut(tr("5"));
200 layout->addWidget(sick551SimCheckBox);
202 obstSimGroupBox->setLayout(layout);
205 void RobomonAtlantis::createFSMGroupBox()
207 fsmGroupBox = new QGroupBox(tr("FSM"));
208 fsmGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
209 QGridLayout *layout = new QGridLayout();
211 layout->addWidget(MiscGui::createLabel("Main:"), 1, 0);
212 fsm_main_state = new QLabel();
213 fsm_main_state->setMinimumWidth(100);
214 fsm_main_state->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
215 layout->addWidget(fsm_main_state, 1, 1);
217 layout->addWidget(MiscGui::createLabel("Act:"), 2, 0);
218 fsm_act_state = new QLabel();
219 layout->addWidget(fsm_act_state, 2, 1);
221 layout->addWidget(MiscGui::createLabel("Motion:"), 3, 0);
222 fsm_motion_state = new QLabel();
223 layout->addWidget(fsm_motion_state, 3, 1);
225 fsmGroupBox->setLayout(layout);
228 void RobomonAtlantis::createDebugGroupBox()
230 debugGroupBox = new QGroupBox(tr("Debug window"));
231 debugGroupBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
232 QHBoxLayout *layout = new QHBoxLayout();
234 debugWindow = new QTextEdit();
235 debugWindow->setReadOnly(true);
237 layout->addWidget(debugWindow);
238 debugGroupBox->setLayout(layout);
241 void RobomonAtlantis::createActuatorsGroupBox()
243 actuatorsGroupBox = new QGroupBox(tr("Actuators"));
244 actuatorsGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
245 QHBoxLayout *layout = new QHBoxLayout();
247 layout->setAlignment(Qt::AlignLeft);
248 actuatorsGroupBox->setLayout(layout);
251 void RobomonAtlantis::createPowerGroupBox()
253 powerGroupBox = new QGroupBox(tr("Power management"));
254 powerGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
255 QGridLayout *layout = new QGridLayout();
257 voltage33CheckBox = new QCheckBox(tr("&3.3V"));
258 voltage50CheckBox = new QCheckBox(tr("&5.0V"));
259 voltage80CheckBox = new QCheckBox(tr("&8.0V"));
261 voltage33CheckBox->setShortcut(tr("3"));
262 voltage50CheckBox->setShortcut(tr("5"));
263 voltage80CheckBox->setShortcut(tr("8"));
265 layout->addWidget(voltage33CheckBox, 0, 0);
266 layout->addWidget(voltage50CheckBox, 1, 0);
267 layout->addWidget(voltage80CheckBox, 2, 0);
268 layout->addWidget(MiscGui::createLabel("BAT"), 3, 0);
270 voltage33LineEdit = new QLineEdit();
271 voltage50LineEdit = new QLineEdit();
272 voltage80LineEdit = new QLineEdit();
273 voltageBATLineEdit = new QLineEdit();
275 voltage33LineEdit->setReadOnly(true);
276 voltage50LineEdit->setReadOnly(true);
277 voltage80LineEdit->setReadOnly(true);
278 voltageBATLineEdit->setReadOnly(true);
280 layout->addWidget(voltage33LineEdit, 0, 1);
281 layout->addWidget(voltage50LineEdit, 1, 1);
282 layout->addWidget(voltage80LineEdit, 2, 1);
283 layout->addWidget(voltageBATLineEdit, 3, 1);
285 powerGroupBox->setLayout(layout);
288 void RobomonAtlantis::createRobots()
290 robotRefPos = new Robot("Ref", QPen(Qt::darkBlue), QBrush(Qt::NoBrush));
291 robotRefPos->setZValue(11);
292 trailRefPos = new Trail(QPen(Qt::darkBlue));
293 trailRefPos->setZValue(11);
295 robotEstPosBest = new Robot("Est", QPen(), QBrush(Qt::darkGray));
296 robotEstPosBest->setZValue(10);
297 trailEstPosBest = new Trail(QPen());
298 trailEstPosBest->setZValue(10);
300 robotEstPosOdo = new Robot("Mot", QPen(Qt::white), QBrush(Qt::darkRed));
301 robotEstPosOdo->setZValue(10);
302 trailOdoPos = new Trail(QPen(Qt::red));
303 trailOdoPos->setZValue(10);
305 robotEstPosIndepOdo = new Robot("Odo", QPen(Qt::white), QBrush(Qt::darkGreen));
306 robotEstPosIndepOdo->setZValue(10);
307 trailPosIndepOdo = new Trail(QPen(Qt::green));
308 trailPosIndepOdo->setZValue(10);
310 playgroundScene->addItem(robotRefPos);
311 playgroundScene->addItem(robotEstPosBest);
312 playgroundScene->addItem(robotEstPosIndepOdo);
313 playgroundScene->addItem(robotEstPosOdo);
317 playgroundScene->addItem(trailRefPos);
318 playgroundScene->addItem(trailPosIndepOdo);
319 playgroundScene->addItem(trailOdoPos);
321 hokuyoScan = new LidarScan(hokuyo_params);
322 hokuyoScan->setZValue(10);
323 playgroundScene->addItem(hokuyoScan);
325 sickScan = new LidarScan(sick_params);
326 sickScan->setZValue(10);
327 playgroundScene->addItem(sickScan);
329 sick551Scan = new LidarScan(sick551_params);
330 sick551Scan->setZValue(10);
331 playgroundScene->addItem(sick551Scan);
334 void RobomonAtlantis::createMap()
336 mapImage = new Map();
337 mapImage->setZValue(5);
338 mapImage->setTransform(QTransform().scale(MAP_CELL_SIZE_MM, MAP_CELL_SIZE_MM), true);
340 playgroundScene->addItem(mapImage);
343 /**********************************************************************
345 **********************************************************************/
346 void RobomonAtlantis::createActions()
348 /* power management */
349 connect(voltage33CheckBox, SIGNAL(stateChanged(int)),
350 this, SLOT(setVoltage33(int)));
351 connect(voltage50CheckBox, SIGNAL(stateChanged(int)),
352 this, SLOT(setVoltage50(int)));
353 connect(voltage80CheckBox, SIGNAL(stateChanged(int)),
354 this, SLOT(setVoltage80(int)));
356 connect(startPlug, SIGNAL(stateChanged(int)), this, SLOT(sendStart(int)));
357 connect(colorChoser, SIGNAL(stateChanged(int)), this, SLOT(setTeamColor(int)));
358 connect(strategyButton, SIGNAL(pressed()), this, SLOT(changeStrategy_1()));
359 connect(strategyButton, SIGNAL(released()), this, SLOT(changeStrategy_0()));
361 /* obstacle simulation */
362 hokuyoSimEnabled = 0;
364 sick551SimEnabled = 0;
366 connect(hokuyoSimCheckBox, SIGNAL(stateChanged(int)),
367 this, SLOT(setHokuyoSimulation(int)));
368 connect(hokuyoSimCheckBox, SIGNAL(stateChanged(int)),
369 this, SLOT(setHokuyoObstacleSimulation(int)));
371 connect(sick331SimCheckBox, SIGNAL(stateChanged(int)),
372 this, SLOT(setSick331Simulation(int)));
373 connect(sick331SimCheckBox, SIGNAL(stateChanged(int)),
374 this, SLOT(setSick331ObstacleSimulation(int)));
376 connect(sick551SimCheckBox, SIGNAL(stateChanged(int)),
377 this, SLOT(setSick551Simulation(int)));
378 connect(sick551SimCheckBox, SIGNAL(stateChanged(int)),
379 this, SLOT(setSick551ObstacleSimulation(int)));
381 connect(playgroundScene, SIGNAL(obstacleChanged(QPointF)),
382 this, SLOT(changeObstacle(QPointF)));
385 void RobomonAtlantis::changeStrategy_1()
387 orte.robot_switches.strategy = true;
388 ORTEPublicationSend(orte.publication_robot_switches);
391 void RobomonAtlantis::changeStrategy_0()
393 orte.robot_switches.strategy = false;
394 ORTEPublicationSend(orte.publication_robot_switches);
397 void RobomonAtlantis::setVoltage33(int state)
400 orte.pwr_ctrl.voltage33 = true;
402 orte.pwr_ctrl.voltage33 = false;
405 void RobomonAtlantis::setVoltage50(int state)
408 orte.pwr_ctrl.voltage50 = true;
410 orte.pwr_ctrl.voltage50 = false;
413 void RobomonAtlantis::setVoltage80(int state)
416 orte.pwr_ctrl.voltage80 = true;
418 orte.pwr_ctrl.voltage80 = false;
421 void RobomonAtlantis::useOpenGL(bool use)
423 playgroundSceneView->useOpenGL(&use);
426 void RobomonAtlantis::showMap(bool show)
430 if (sharedMemoryOpened == false)
434 mapTimer = new QTimer(this);
435 connect(mapTimer, SIGNAL(timeout()), this, SLOT(paintMap()));
436 mapTimer->start(200);
438 if(mapTimer != NULL) {
440 disconnect(mapTimer, SIGNAL(timeout()), this, SLOT(paintMap()));
443 mapImage->setVisible(show);
446 void RobomonAtlantis::paintMap()
449 struct map *map = ShmapIsMapInit();
453 for(int i = 0; i < MAP_WIDTH; i++) {
454 for(int j = 0; j < MAP_HEIGHT; j++) {
457 struct map_cell *cell = &map->cells[j][i];
460 if (cell->flags & MAP_FLAG_WALL)
462 if (cell->flags & MAP_FLAG_IGNORE_OBST)
464 if (cell->flags & MAP_FLAG_SIMULATED_WALL)
466 if (cell->flags & MAP_FLAG_PATH)
468 if (cell->flags & MAP_FLAG_START)
470 if (cell->flags & MAP_FLAG_GOAL)
472 if (cell->flags & MAP_FLAG_PLAN_MARGIN) {
473 QColor c(240, 170, 50); /* orange */
476 if (cell->detected_obstacle) {
477 QColor c1(color), c2(blue);
478 double f = (double)cell->detected_obstacle/MAP_NEW_OBSTACLE*0.7;
479 QColor c(c1.red() + (int)(f*(c2.red() - c1.red())),
480 c1.green() + (int)(f*(c2.green() - c1.green())),
481 c1.blue() + (int)(f*(c2.blue() - c1.blue())));
484 if (cell->flags & MAP_FLAG_DET_OBST)
488 mapImage->setPixelColor(i, MAP_HEIGHT - j - 1, color);
493 void RobomonAtlantis::setHokuyoSimulation(int state)
496 robottype_publisher_hokuyo_scan_create(&orte, NULL, this);
498 if (!hokuyoSimEnabled)
500 robottype_publisher_hokuyo_scan_destroy(&orte);
502 hokuyoSimEnabled = state;
505 void RobomonAtlantis::setSick331Simulation(int state)
508 robottype_publisher_sick_scan_create(&orte, NULL, this);
512 robottype_publisher_sick_scan_destroy(&orte);
514 sickSimEnabled = state;
517 void RobomonAtlantis::setSick551Simulation(int state)
520 robottype_publisher_sick551_scan_create(&orte, NULL, this);
522 if (!sick551SimEnabled)
524 robottype_publisher_sick551_scan_destroy(&orte);
526 sick551SimEnabled = state;
529 void RobomonAtlantis::setHokuyoObstacleSimulation(int state)
532 /* TODO Maybe it is possible to attach only once to Shmap */
534 obstacleSimulationTimerHokuyo = new QTimer(this);
535 connect(obstacleSimulationTimerHokuyo, SIGNAL(timeout()),
536 this, SLOT(simulateObstaclesHokuyo()));
537 obstacleSimulationTimerHokuyo->start(100);
538 setMouseTracking(true);
539 hokuyoScan->setVisible(true);
541 if (obstacleSimulationTimerHokuyo)
542 delete obstacleSimulationTimerHokuyo;
543 // Hide scans of lidars
544 hokuyoScan->setVisible(false);
548 void RobomonAtlantis::setSick331ObstacleSimulation(int state)
551 /* TODO Maybe it is possible to attach only once to Shmap */
553 obstacleSimulationTimerSick331 = new QTimer(this);
554 connect(obstacleSimulationTimerSick331, SIGNAL(timeout()),
555 this, SLOT(simulateObstaclesSick()));
556 obstacleSimulationTimerSick331->start(100);
557 setMouseTracking(true);
558 sickScan->setVisible(true);
560 if (obstacleSimulationTimerSick331)
561 delete obstacleSimulationTimerSick331;
562 // Hide scans of lidars
563 sickScan->setVisible(false);
567 void RobomonAtlantis::setSick551ObstacleSimulation(int state)
570 /* TODO Maybe it is possible to attach only once to Shmap */
572 obstacleSimulationTimerSick551 = new QTimer(this);
573 connect(obstacleSimulationTimerSick551, SIGNAL(timeout()),
574 this, SLOT(simulateObstaclesSick551()));
575 obstacleSimulationTimerSick551->start(100);
576 setMouseTracking(true);
577 sick551Scan->setVisible(true);
579 if (obstacleSimulationTimerSick551)
580 delete obstacleSimulationTimerSick551;
581 // Hide scans of lidars
582 sick551Scan->setVisible(false);
586 void RobomonAtlantis::simulateObstaclesLidar(const struct lidar_params lidar)
588 double distance, wall_distance;
590 unsigned int data_lenght = 0;
591 uint16_t *lidar_data = NULL;
592 switch (lidar.type) {
594 lidar_data = orte.hokuyo_scan.data;
595 data_lenght = hokuyo_params.data_lenght;
598 lidar_data = orte.sick_scan.data;
599 data_lenght = sick_params.data_lenght;
602 lidar_data = orte.sick551_scan.data;
603 data_lenght = sick551_params.data_lenght;
609 for (i = 0; i < data_lenght; i++) {
610 wall_distance = distanceToWallLidar(lidar, i);
611 distance = distanceToCircularObstacleLidar(lidar, i, simulatedObstacle, SIM_OBST_SIZE_M);
612 if (wall_distance < distance)
613 distance = wall_distance;
614 lidar_data[i] = distance*1000;
617 switch (lidar.type) {
619 orte.hokuyo_scan.data_lenght = hokuyo_params.data_lenght;
620 orte.hokuyo_scan.lidar_type = hokuyo_params.type;
621 ORTEPublicationSend(orte.publication_hokuyo_scan);
624 orte.sick_scan.data_lenght = sick_params.data_lenght;
625 orte.sick_scan.lidar_type = sick_params.type;
626 ORTEPublicationSend(orte.publication_sick_scan);
629 orte.sick551_scan.data_lenght = sick551_params.data_lenght;
630 orte.sick551_scan.lidar_type = sick551_params.type;
631 ORTEPublicationSend(orte.publication_sick551_scan);
638 void RobomonAtlantis::simulateObstaclesHokuyo()
640 simulateObstaclesLidar(hokuyo_params);
643 void RobomonAtlantis::simulateObstaclesSick()
645 simulateObstaclesLidar(sick_params);
648 void RobomonAtlantis::simulateObstaclesSick551()
650 simulateObstaclesLidar(sick551_params);
653 void RobomonAtlantis::changeObstacle(QPointF position)
655 if (!hokuyoSimEnabled && !sickSimEnabled && !sick551SimEnabled) {
656 hokuyoSimEnabled = 1;
658 sick551SimEnabled = 1;
659 hokuyoSimCheckBox->setChecked(true);
660 sick331SimCheckBox->setChecked(true);
661 sick551SimCheckBox->setChecked(true);
664 simulatedObstacle.x = position.x();
665 simulatedObstacle.y = position.y();
668 /**********************************************************************
670 **********************************************************************/
671 bool RobomonAtlantis::event(QEvent *event)
673 switch (event->type()) {
674 case QEVENT(QEV_MOTION_STATUS):
675 emit motionStatusReceivedSignal();
677 case QEVENT(QEV_SICK_SCAN):
678 sickScan->newScan(&orte.sick_scan);
680 case QEVENT(QEV_SICK551_SCAN):
681 sick551Scan->newScan(&orte.sick551_scan);
683 case QEVENT(QEV_HOKUYO_SCAN):
684 hokuyoScan->newScan(&orte.hokuyo_scan);
686 case QEVENT(QEV_REFERENCE_POSITION):
687 emit actualPositionReceivedSignal();
689 case QEVENT(QEV_ESTIMATED_POSITION_INDEP_ODO):
690 estPosX->setText(QString("%1").arg(orte.est_pos_indep_odo.x, 0, 'f', 3));
691 estPosY->setText(QString("%1").arg(orte.est_pos_indep_odo.y, 0, 'f', 3));
692 estPosPhi->setText(QString("%1(%2)")
693 .arg(DEGREES(orte.est_pos_indep_odo.phi), 0, 'f', 0)
694 .arg(orte.est_pos_indep_odo.phi, 0, 'f', 1));
695 robotEstPosIndepOdo->moveRobot(orte.est_pos_indep_odo.x,
696 orte.est_pos_indep_odo.y, orte.est_pos_indep_odo.phi);
697 trailPosIndepOdo->addPoint(QPointF(orte.est_pos_indep_odo.x,
698 orte.est_pos_indep_odo.y));
700 case QEVENT(QEV_ESTIMATED_POSITION_ODO):
701 robotEstPosOdo->moveRobot(orte.est_pos_odo.x,
702 orte.est_pos_odo.y, orte.est_pos_odo.phi);
703 trailOdoPos->addPoint(QPointF(orte.est_pos_odo.x,
704 orte.est_pos_odo.y));
706 case QEVENT(QEV_ESTIMATED_POSITION_BEST):
707 robotEstPosBest->moveRobot(orte.est_pos_best.x,
708 orte.est_pos_best.y, orte.est_pos_best.phi);
709 trailEstPosBest->addPoint(QPointF(orte.est_pos_best.x,
710 orte.est_pos_best.y));
711 hokuyoScan->setPosition(orte.est_pos_best.x,
713 orte.est_pos_best.phi);
714 sickScan->setPosition(orte.est_pos_best.x,
716 orte.est_pos_best.phi);
717 sick551Scan->setPosition(orte.est_pos_best.x,
719 orte.est_pos_best.phi);
721 case QEVENT(QEV_POWER_VOLTAGE):
722 emit powerVoltageReceivedSignal();
724 case QEVENT(QEV_FSM_MAIN):
725 fsm_main_state->setText(orte.fsm_main.state_name);
727 case QEVENT(QEV_FSM_ACT):
728 fsm_act_state->setText(orte.fsm_act.state_name);
730 case QEVENT(QEV_FSM_MOTION):
731 fsm_motion_state->setText(orte.fsm_motion.state_name);
734 if (event->type() == QEvent::Close)
735 closeEvent((QCloseEvent *)event);
736 else if (event->type() == QEvent::KeyPress)
737 keyPressEvent((QKeyEvent *)event);
738 else if (event->type() == QEvent::KeyRelease)
739 keyReleaseEvent((QKeyEvent *)event);
740 else if (event->type() == QEvent::FocusIn)
742 else if (event->type() == QEvent::FocusOut)
754 void RobomonAtlantis::keyPressEvent(QKeyEvent *event)
756 // double peak, gain;
758 if (event->isAutoRepeat()) {
759 switch (event->key()) {
760 // case Qt::Key_Down:
761 // peak = leftMotorSlider->minimum()/2;
762 // if (leftMotorValue < peak ||
763 // rightMotorValue < peak)
767 // leftMotorValue *= gain;
768 // rightMotorValue *= gain;
769 // leftMotorSlider->setValue((int)leftMotorValue);
770 // rightMotorSlider->setValue((int)rightMotorValue);
774 // case Qt::Key_Left:
775 // case Qt::Key_Right:
776 // peak = leftMotorSlider->maximum()/2;
777 // if (leftMotorValue > peak ||
778 // rightMotorValue > peak)
782 // leftMotorValue *= gain;
783 // rightMotorValue *= gain;
784 // leftMotorSlider->setValue((int)leftMotorValue);
785 // rightMotorSlider->setValue((int)rightMotorValue);
795 switch (event->key()) {
797 // leftMotorValue = 1;
798 // rightMotorValue = 1;
799 // bothMotorsCheckBox->setChecked(true);
800 // leftMotorSlider->setValue((int)leftMotorValue);
801 // setLeftMotor((int)leftMotorValue);
803 // case Qt::Key_Down:
804 // leftMotorValue = -1;
805 // rightMotorValue = -1;
806 // bothMotorsCheckBox->setChecked(true);
807 // leftMotorSlider->setValue((int)leftMotorValue);
808 // setLeftMotor((int)leftMotorValue);
810 // case Qt::Key_Left:
811 // leftMotorValue = -1;
812 // rightMotorValue = 1;
813 // leftMotorSlider->setValue((int)leftMotorValue);
814 // rightMotorSlider->setValue((int)rightMotorValue);
815 // setLeftMotor((int)leftMotorValue);
816 // setRightMotor((int)leftMotorValue);
818 // case Qt::Key_Right:
819 // leftMotorValue = 1;
820 // rightMotorValue = -1;
821 // leftMotorSlider->setValue((int)leftMotorValue);
822 // rightMotorSlider->setValue((int)rightMotorValue);
823 // setLeftMotor((int)leftMotorValue);
824 // setRightMotor((int)rightMotorValue);
833 void RobomonAtlantis::keyReleaseEvent(QKeyEvent *event)
835 if (event->isAutoRepeat()) {
840 switch (event->key()) {
842 // case Qt::Key_Down:
843 // case Qt::Key_Left:
844 // case Qt::Key_Right:
845 // leftMotorValue = 0;
846 // rightMotorValue = 0;
847 // bothMotorsCheckBox->setChecked(false);
848 // leftMotorSlider->setValue((int)leftMotorValue);
849 // rightMotorSlider->setValue((int)rightMotorValue);
858 void RobomonAtlantis::closeEvent(QCloseEvent *)
860 robottype_roboorte_destroy(&orte);
863 /**********************************************************************
865 **********************************************************************/
866 void RobomonAtlantis::createOrte()
870 memset(&orte, 0, sizeof(orte));
871 rv = robottype_roboorte_init(&orte);
873 printf("RobomonAtlantis: Unable to initialize ORTE\n");
877 robottype_publisher_motion_speed_create(&orte, dummy_publisher_callback, NULL);
879 robottype_publisher_pwr_ctrl_create(&orte, dummy_publisher_callback, NULL);
880 robottype_publisher_robot_cmd_create(&orte, NULL, &orte);
881 robottype_publisher_robot_switches_create(&orte, dummy_publisher_callback, &orte);
884 robottype_subscriber_pwr_voltage_create(&orte,
885 receivePowerVoltageCallBack, this);
886 robottype_subscriber_motion_status_create(&orte,
887 receiveMotionStatusCallBack, this);
888 robottype_subscriber_ref_pos_create(&orte,
889 receiveActualPositionCallBack, this);
890 robottype_subscriber_est_pos_odo_create(&orte,
891 generic_rcv_cb, new OrteCallbackInfo(this, QEV_ESTIMATED_POSITION_ODO));
892 robottype_subscriber_est_pos_indep_odo_create(&orte,
893 generic_rcv_cb, new OrteCallbackInfo(this, QEV_ESTIMATED_POSITION_INDEP_ODO));
894 robottype_subscriber_est_pos_best_create(&orte,
895 generic_rcv_cb, new OrteCallbackInfo(this, QEV_ESTIMATED_POSITION_BEST));
896 robottype_subscriber_sick_scan_create(&orte,
897 generic_rcv_cb, new OrteCallbackInfo(this, QEV_SICK_SCAN));
898 robottype_subscriber_sick551_scan_create(&orte,
899 generic_rcv_cb, new OrteCallbackInfo(this, QEV_SICK551_SCAN));
900 robottype_subscriber_hokuyo_scan_create(&orte,
901 generic_rcv_cb, new OrteCallbackInfo(this, QEV_HOKUYO_SCAN));
902 robottype_subscriber_fsm_main_create(&orte,
903 rcv_fsm_main_cb, this);
904 robottype_subscriber_fsm_motion_create(&orte,
905 rcv_fsm_motion_cb, this);
906 robottype_subscriber_fsm_act_create(&orte,
907 rcv_fsm_act_cb, this);
910 orte.motion_speed.left = 0;
911 orte.motion_speed.right = 0;
913 /* power management */
914 orte.pwr_ctrl.voltage33 = true;
915 orte.pwr_ctrl.voltage50 = true;
916 orte.pwr_ctrl.voltage80 = true;
917 voltage33CheckBox->setChecked(true);
918 voltage50CheckBox->setChecked(true);
919 voltage80CheckBox->setChecked(true);
923 /* set actions to do when we receive data from orte */
924 connect(this, SIGNAL(motionStatusReceivedSignal()),
925 this, SLOT(motionStatusReceived()));
926 connect(this, SIGNAL(actualPositionReceivedSignal()),
927 this, SLOT(actualPositionReceived()));
928 connect(this, SIGNAL(powerVoltageReceivedSignal()),
929 this, SLOT(powerVoltageReceived()));
932 void RobomonAtlantis::motionStatusReceived()
934 WDBG("ORTE received: motion status");
937 void RobomonAtlantis::actualPositionReceived()
939 actPosX->setText(QString("%1").arg(orte.ref_pos.x, 0, 'f', 3));
940 actPosY->setText(QString("%1").arg(orte.ref_pos.y, 0, 'f', 3));
941 actPosPhi->setText(QString("%1(%2)")
942 .arg(DEGREES(orte.ref_pos.phi), 0, 'f', 0)
943 .arg(orte.ref_pos.phi, 0, 'f', 1));
944 robotRefPos->moveRobot(orte.ref_pos.x,
945 orte.ref_pos.y, orte.ref_pos.phi);
946 trailRefPos->addPoint(QPointF(orte.ref_pos.x, orte.ref_pos.y));
949 void RobomonAtlantis::powerVoltageReceived()
951 voltage33LineEdit->setText(QString("%1").arg(
952 orte.pwr_voltage.voltage33, 0, 'f', 3));
953 voltage50LineEdit->setText(QString("%1").arg(
954 orte.pwr_voltage.voltage50, 0, 'f', 3));
955 voltage80LineEdit->setText(QString("%1").arg(
956 orte.pwr_voltage.voltage80, 0, 'f', 3));
957 voltageBATLineEdit->setText(QString("%1").arg(
958 orte.pwr_voltage.voltageBAT, 0, 'f', 3));
962 /**********************************************************************
964 **********************************************************************/
965 void RobomonAtlantis::openSharedMemory()
968 int sharedSegmentSize;
970 if (sharedMemoryOpened)
973 sharedSegmentSize = sizeof(unsigned int) * MAP_WIDTH * MAP_HEIGHT;
975 /* Get segment identificator in a read only mode */
976 segmentId = shmget(SHM_MAP_KEY, sharedSegmentSize, S_IRUSR);
977 if(segmentId == -1) {
978 statusBar->showMessage("No external map found - creating a new map.");
984 /* Attach the shared memory segment */
985 //map = (_Map*)shmat (segmentId, (void*) 0, 0);
987 sharedMemoryOpened = true;
990 double RobomonAtlantis::distanceToWallLidar(const struct lidar_params lidar, int beamnum)
992 double distance = 4.0, min_distance = 4.0;
995 struct map *map = ShmapIsMapInit();
1000 // Simulate obstacles
1001 for(j = 0; j < MAP_HEIGHT; j++) {
1002 for (i = 0; i < MAP_WIDTH; i++) {
1003 struct map_cell *cell = &map->cells[j][i];
1004 if( cell->flags & MAP_FLAG_SIMULATED_WALL) {
1006 ShmapCell2Point(i, j, &wall.x, &wall.y);
1008 distance = distanceToObstacleLidar(lidar, beamnum, wall, MAP_CELL_SIZE_M);
1009 if (distance < min_distance)
1010 min_distance = distance;
1015 return min_distance;
1018 double RobomonAtlantis::distanceToCircularObstacleLidar(const struct lidar_params lidar, int beamnum, Point center, double diameter)
1020 struct robot_pos_type e = orte.est_pos_best;
1024 s.x = lidar.center_offset_m;
1026 s.ang = index2rad(lidar, beamnum);
1028 Point sensor(e.x + s.x*cos(e.phi) - s.y*sin(e.phi),
1029 e.y + s.x*sin(e.phi) + s.y*cos(e.phi));
1030 sensor_a = e.phi + s.ang;
1032 const double sensorRange = 4.0; /*[meters]*/
1034 double distance = sensorRange;
1037 angle = sensor.angleTo(center) - sensor_a;
1038 angle = fmod(angle, 2.0 * M_PI);
1039 if (angle > +M_PI) angle -= 2.0 * M_PI;
1040 if (angle < -M_PI) angle += 2.0 * M_PI;
1041 angle = fabs(angle);
1043 double k = tan(sensor_a);
1044 double r = diameter / 2.0;
1046 double A = 1 + k * k;
1047 double B = 2 * (sensor.y * k - center.x - k * k * sensor.x - center.y * k);
1048 double C = center.x * center.x + center.y * center.y +
1049 k * k * sensor.x * sensor.x - 2*sensor.y*k*sensor.x +
1050 sensor.y * sensor.y + 2 * k * sensor.x *center.y -
1051 2 * sensor.y * center.y - r * r;
1053 double D = B * B - 4 * A * C;
1058 ob1.x = (-B + sqrt(D)) / (2 * A);
1059 ob2.x = (-B - sqrt(D)) / (2 * A);
1060 ob1.y = k * (ob1.x - sensor.x) + sensor.y;
1061 ob2.y = k * (ob2.x - sensor.x) + sensor.y;
1063 double distance1 = sensor.distanceTo(ob1);
1064 double distance2 = sensor.distanceTo(ob2);
1065 distance = (distance1 < distance2) ? distance1 : distance2;
1066 } else if (D == 0) {
1068 ob.x = -B / (2 * A);
1069 ob.y = k * (ob.x - sensor.x) + sensor.y;
1070 distance = sensor.distanceTo(ob);
1072 distance = distance + (drand48() - 0.5) * 3.0e-2;
1073 if (D < 0 || angle > atan(r / distance))
1074 distance = sensorRange;
1075 if (distance > sensorRange)
1076 distance = sensorRange;
1082 * Calculation for Lidar simulation. Calculates distance that would
1083 * be returned by Lidar sensors, if there is only one obstacle (as
1084 * specified by parameters).
1086 * @param beamnum Lidar's bean number [0..LIDAR_CLUSTER_CNT]
1087 * @param obstacle Position of the obstacle (x, y in meters).
1088 * @param obstacleSize Size (diameter) of the obstacle in meters.
1090 * @return Distance measured by sensors in meters.
1092 double RobomonAtlantis::distanceToObstacleLidar(const struct lidar_params lidar, int beamnum, Point obstacle, double obstacleSize)
1094 struct robot_pos_type e = orte.est_pos_best;
1098 s.x = lidar.center_offset_m;
1100 s.ang = index2rad(lidar, beamnum);
1102 Point sensor(e.x + s.x * cos(e.phi) - s.y * sin(e.phi),
1103 e.y + s.x * sin(e.phi) + s.y * cos(e.phi));
1104 sensor_a = e.phi + s.ang;
1106 const double sensorRange = 4.0; /*[meters]*/
1108 double distance, angle;
1110 angle = sensor.angleTo(obstacle) - sensor_a;
1111 angle = fmod(angle, 2.0 * M_PI);
1112 if (angle > +M_PI) angle -= 2.0 * M_PI;
1113 if (angle < -M_PI) angle += 2.0 * M_PI;
1114 angle = fabs(angle);
1115 distance = sensor.distanceTo(obstacle) - obstacleSize/2.0;
1116 if (angle < atan(obstacleSize/2.0 / distance)) {
1117 // We can see the obstackle from here.
1118 if (angle < M_PI/2.0) {
1119 distance = distance / cos(angle);
1121 if (distance > sensorRange)
1122 distance = sensorRange;
1124 distance = sensorRange;
1130 void RobomonAtlantis::sendStart(int plug)
1132 orte.robot_cmd.start_condition = plug ? 0 : 1;
1133 ORTEPublicationSend(orte.publication_robot_cmd);
1136 void RobomonAtlantis::setTeamColor(int plug)
1138 orte.robot_switches.team_color = plug ? 1 : 0;
1139 ORTEPublicationSend(orte.publication_robot_switches);
1142 void RobomonAtlantis::resetTrails()
1144 trailRefPos->reset();
1145 trailEstPosBest->reset();
1146 trailPosIndepOdo->reset();
1147 trailOdoPos->reset();
1150 void RobomonAtlantis::showTrails(bool show)
1152 trailRefPos->setVisible(show && robotRefPos->isVisible());
1153 trailEstPosBest->setVisible(show && robotEstPosBest->isVisible());
1154 trailPosIndepOdo->setVisible(show && robotEstPosIndepOdo->isVisible());
1155 trailOdoPos->setVisible(show && robotEstPosOdo->isVisible());
1158 void RobomonAtlantis::showShapeDetect(bool show)
1160 hokuyoScan->showShapeDetect = show;