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>
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(QWidget *parent)
55 debugWindowEnabled = false;
60 QHBoxLayout *mainLayout = new QHBoxLayout;
61 mainLayout->addLayout(leftLayout);
62 mainLayout->addLayout(rightLayout);
63 setLayout(mainLayout);
70 // connect(vidle, SIGNAL(valueChanged(int)),
71 // robotEstPosBest, SLOT(setVidle(int)));
73 setFocusPolicy(Qt::StrongFocus);
74 sharedMemoryOpened = false;
75 WDBG("Youuuhouuuu!!");
78 /**********************************************************************
80 **********************************************************************/
81 void RobomonAtlantis::createLeftLayout()
83 leftLayout = new QVBoxLayout();
85 createDebugGroupBox();
86 debugWindowEnabled = true;
87 createPlaygroundGroupBox();
88 leftLayout->addWidget(playgroundGroupBox);
89 //leftLayout->addWidget(debugGroupBox); // FIXME: move this to separate tab
92 void RobomonAtlantis::createRightLayout()
94 rightLayout = new QVBoxLayout();
96 createPositionGroupBox();
99 createActuatorsGroupBox();
100 createPowerGroupBox();
102 rightLayout->addWidget(positionGroupBox);
103 rightLayout->addWidget(miscGroupBox);
104 rightLayout->addWidget(fsmGroupBox);
105 rightLayout->addWidget(powerGroupBox);
106 rightLayout->addWidget(actuatorsGroupBox);
109 void RobomonAtlantis::createPlaygroundGroupBox()
111 playgroundGroupBox = new QGroupBox(tr("Playground"));
112 QHBoxLayout *layout = new QHBoxLayout();
114 playgroundScene = new PlaygroundScene();
115 playgroundSceneView = new PlaygroundView(playgroundScene);
116 //playgroundSceneView->setMinimumWidth(630);
117 //playgroundSceneView->setMinimumHeight(445);
118 playgroundSceneView->setMatrix(QMatrix(1,0,0,-1,0,0), true);
119 playgroundSceneView->fitInView(playgroundScene->itemsBoundingRect());
120 playgroundSceneView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
121 playgroundSceneView->setMouseTracking(true);
122 layout->addWidget(playgroundSceneView);
124 playgroundGroupBox->setLayout(layout);
127 void RobomonAtlantis::createPositionGroupBox()
129 positionGroupBox = new QGroupBox(tr("Position state"));
130 positionGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
131 QGridLayout *layout = new QGridLayout();
133 actPosX = new QLineEdit();
134 actPosY = new QLineEdit();
135 actPosPhi = new QLineEdit();
137 estPosX = new QLineEdit();
138 estPosY = new QLineEdit();
139 estPosPhi = new QLineEdit();
141 actPosX->setReadOnly(true);
142 actPosY->setReadOnly(true);
143 actPosPhi->setReadOnly(true);
145 estPosX->setReadOnly(true);
146 estPosY->setReadOnly(true);
147 estPosPhi->setReadOnly(true);
149 layout->addWidget(MiscGui::createLabel("X"), 1, 0);
150 layout->addWidget(MiscGui::createLabel("Y"), 2, 0);
151 layout->addWidget(MiscGui::createLabel("Phi"), 3, 0);
153 layout->addWidget(MiscGui::createLabel("X"), 5, 0);
154 layout->addWidget(MiscGui::createLabel("Y"), 6, 0);
155 layout->addWidget(MiscGui::createLabel("Phi"), 7, 0);
157 layout->addWidget(MiscGui::createLabel("Reference", Qt::AlignLeft), 0, 1);
158 layout->addWidget(actPosX, 1, 1);
159 layout->addWidget(actPosY, 2, 1);
160 layout->addWidget(actPosPhi, 3, 1);
162 layout->addWidget(MiscGui::createLabel("Estimated (indep. odo.)", Qt::AlignLeft), 4, 1);
163 layout->addWidget(estPosX, 5, 1);
164 layout->addWidget(estPosY, 6, 1);
165 layout->addWidget(estPosPhi, 7, 1);
167 positionGroupBox->setLayout(layout);
170 void RobomonAtlantis::createMiscGroupBox()
172 miscGroupBox = new QGroupBox(tr("Miscellaneous"));
173 miscGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
174 QGridLayout *layout = new QGridLayout();
176 obstacleSimulationCheckBox = new QCheckBox(tr("&Obstacle simulation"));
177 obstacleSimulationCheckBox->setShortcut(tr("o"));
178 layout->addWidget(obstacleSimulationCheckBox);
180 startPlug = new QCheckBox("&Start plug");
181 layout->addWidget(startPlug);
183 colorChoser = new QCheckBox("&Team color");
184 layout->addWidget(colorChoser);
186 miscGroupBox->setLayout(layout);
189 void RobomonAtlantis::createFSMGroupBox()
191 fsmGroupBox = new QGroupBox(tr("FSM"));
192 fsmGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
193 QGridLayout *layout = new QGridLayout();
195 layout->addWidget(MiscGui::createLabel("Main:"), 1, 0);
196 fsm_main_state = new QLabel();
197 fsm_main_state->setMinimumWidth(100);
198 fsm_main_state->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
199 layout->addWidget(fsm_main_state, 1, 1);
201 layout->addWidget(MiscGui::createLabel("Act:"), 2, 0);
202 fsm_act_state = new QLabel();
203 layout->addWidget(fsm_act_state, 2, 1);
205 layout->addWidget(MiscGui::createLabel("Motion:"), 3, 0);
206 fsm_motion_state = new QLabel();
207 layout->addWidget(fsm_motion_state, 3, 1);
209 fsmGroupBox->setLayout(layout);
212 void RobomonAtlantis::createDebugGroupBox()
214 debugGroupBox = new QGroupBox(tr("Debug window"));
215 debugGroupBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
216 QHBoxLayout *layout = new QHBoxLayout();
218 debugWindow = new QTextEdit();
219 debugWindow->setReadOnly(true);
221 layout->addWidget(debugWindow);
222 debugGroupBox->setLayout(layout);
225 void RobomonAtlantis::createActuatorsGroupBox()
227 actuatorsGroupBox = new QGroupBox(tr("Actuators"));
228 actuatorsGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
229 QHBoxLayout *layout = new QHBoxLayout();
230 // vidle = new QDial();
232 // vidle->setMinimum(VIDLE_VYSIP);
233 // vidle->setMaximum((VIDLE_UP-VIDLE_VYSIP)+VIDLE_VYSIP);
234 // vidle->setEnabled(true);
236 //createMotorsGroupBox();
238 layout->setAlignment(Qt::AlignLeft);
239 // layout->addWidget(vidle);
240 //layout->addWidget(enginesGroupBox);
241 actuatorsGroupBox->setLayout(layout);
244 void RobomonAtlantis::createPowerGroupBox()
246 powerGroupBox = new QGroupBox(tr("Power management"));
247 powerGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
248 QGridLayout *layout = new QGridLayout();
250 voltage33CheckBox = new QCheckBox(tr("&3.3V"));
251 voltage50CheckBox = new QCheckBox(tr("&5.0V"));
252 voltage80CheckBox = new QCheckBox(tr("&8.0V"));
254 voltage33CheckBox->setShortcut(tr("3"));
255 voltage50CheckBox->setShortcut(tr("5"));
256 voltage80CheckBox->setShortcut(tr("8"));
258 layout->addWidget(voltage33CheckBox, 0, 0);
259 layout->addWidget(voltage50CheckBox, 1, 0);
260 layout->addWidget(voltage80CheckBox, 2, 0);
261 layout->addWidget(MiscGui::createLabel("BAT"), 3, 0);
263 voltage33LineEdit = new QLineEdit();
264 voltage50LineEdit = new QLineEdit();
265 voltage80LineEdit = new QLineEdit();
266 voltageBATLineEdit = new QLineEdit();
268 voltage33LineEdit->setReadOnly(true);
269 voltage50LineEdit->setReadOnly(true);
270 voltage80LineEdit->setReadOnly(true);
271 voltageBATLineEdit->setReadOnly(true);
273 layout->addWidget(voltage33LineEdit, 0, 1);
274 layout->addWidget(voltage50LineEdit, 1, 1);
275 layout->addWidget(voltage80LineEdit, 2, 1);
276 layout->addWidget(voltageBATLineEdit, 3, 1);
278 powerGroupBox->setLayout(layout);
281 void RobomonAtlantis::createRobots()
283 robotRefPos = new Robot("Ref", QPen(Qt::darkBlue), QBrush(Qt::NoBrush));
284 robotRefPos->setZValue(11);
285 trailRefPos = new Trail(QPen(Qt::darkBlue));
286 trailRefPos->setZValue(11);
288 robotEstPosBest = new Robot("Est", QPen(), QBrush(Qt::darkGray));
289 robotEstPosBest->setZValue(10);
290 trailEstPosBest = new Trail(QPen());
291 trailEstPosBest->setZValue(10);
293 robotEstPosOdo = new Robot("Mot", QPen(Qt::white), QBrush(Qt::darkRed));
294 robotEstPosOdo->setZValue(10);
295 trailOdoPos = new Trail(QPen(Qt::red));
296 trailOdoPos->setZValue(10);
298 robotEstPosIndepOdo = new Robot("Odo", QPen(Qt::white), QBrush(Qt::darkGreen));
299 robotEstPosIndepOdo->setZValue(10);
300 trailPosIndepOdo = new Trail(QPen(Qt::green));
301 trailPosIndepOdo->setZValue(10);
303 playgroundScene->addItem(robotRefPos);
304 playgroundScene->addItem(robotEstPosBest);
305 playgroundScene->addItem(robotEstPosIndepOdo);
306 playgroundScene->addItem(robotEstPosOdo);
310 playgroundScene->addItem(trailRefPos);
311 playgroundScene->addItem(trailPosIndepOdo);
312 playgroundScene->addItem(trailOdoPos);
314 hokuyoScan = new HokuyoScan();
315 hokuyoScan->setZValue(10);
316 playgroundScene->addItem(hokuyoScan);
320 void RobomonAtlantis::createMap()
322 mapImage = new Map();
323 mapImage->setZValue(5);
324 mapImage->setTransform(QTransform().scale(MAP_CELL_SIZE_MM, MAP_CELL_SIZE_MM), true);
327 playgroundScene->addItem(mapImage);
330 /**********************************************************************
332 **********************************************************************/
333 void RobomonAtlantis::createActions()
335 /* power management */
336 connect(voltage33CheckBox, SIGNAL(stateChanged(int)),
337 this, SLOT(setVoltage33(int)));
338 connect(voltage50CheckBox, SIGNAL(stateChanged(int)),
339 this, SLOT(setVoltage50(int)));
340 connect(voltage80CheckBox, SIGNAL(stateChanged(int)),
341 this, SLOT(setVoltage80(int)));
344 // connect(leftMotorSlider, SIGNAL(valueChanged(int)),
345 // this, SLOT(setLeftMotor(int)));
346 // connect(rightMotorSlider, SIGNAL(valueChanged(int)),
347 // this, SLOT(setRightMotor(int)));
348 // connect(stopMotorsPushButton, SIGNAL(clicked()),
349 // this, SLOT(stopMotors()));
351 connect(startPlug, SIGNAL(stateChanged(int)), this, SLOT(sendStart(int)));
352 connect(colorChoser, SIGNAL(stateChanged(int)), this, SLOT(setTeamColor(int)));
354 /* obstacle simulation */
355 simulationEnabled = 0;
356 connect(obstacleSimulationCheckBox, SIGNAL(stateChanged(int)),
357 this, SLOT(setSimulation(int)));
358 connect(obstacleSimulationCheckBox, SIGNAL(stateChanged(int)),
359 this, SLOT(setObstacleSimulation(int)));
360 connect(obstacleSimulationCheckBox, SIGNAL(stateChanged(int)),
361 playgroundScene, SLOT(showObstacle(int)));
362 connect(playgroundScene, SIGNAL(obstacleChanged(QPointF)),
363 this, SLOT(changeObstacle(QPointF)));
366 void RobomonAtlantis::setVoltage33(int state)
369 orte.pwr_ctrl.voltage33 = true;
371 orte.pwr_ctrl.voltage33 = false;
374 void RobomonAtlantis::setVoltage50(int state)
377 orte.pwr_ctrl.voltage50 = true;
379 orte.pwr_ctrl.voltage50 = false;
382 void RobomonAtlantis::setVoltage80(int state)
385 orte.pwr_ctrl.voltage80 = true;
387 orte.pwr_ctrl.voltage80 = false;
390 // void RobomonAtlantis::setLeftMotor(int value)
392 // short int leftMotor;
393 // short int rightMotor;
395 // if(bothMotorsCheckBox->isChecked())
396 // rightMotorSlider->setValue(value);
398 // leftMotor = (short int)(MOTOR_LIMIT * (leftMotorSlider->value()/100.0));
399 // rightMotor = (short int)(MOTOR_LIMIT * (rightMotorSlider->value()/100.0));
401 // orte.motion_speed.left = leftMotor;
402 // orte.motion_speed.right = rightMotor;
406 // void RobomonAtlantis::setRightMotor(int value)
408 // short int leftMotor;
409 // short int rightMotor;
411 // if(bothMotorsCheckBox->isChecked())
412 // leftMotorSlider->setValue(value);
414 // leftMotor = (short int)(MOTOR_LIMIT * (leftMotorSlider->value()/100.0));
415 // rightMotor = (short int)(MOTOR_LIMIT * (rightMotorSlider->value()/100.0));
417 // orte.motion_speed.left = leftMotor;
418 // orte.motion_speed.right = rightMotor;
422 // void RobomonAtlantis::stopMotors()
424 // leftMotorSlider->setValue(0);
425 // rightMotorSlider->setValue(0);
428 void RobomonAtlantis::useOpenGL(bool use)
430 playgroundSceneView->useOpenGL(&use);
433 void RobomonAtlantis::showMap(bool show)
437 if (sharedMemoryOpened == false)
441 mapTimer = new QTimer(this);
442 connect(mapTimer, SIGNAL(timeout()), this, SLOT(paintMap()));
443 mapTimer->start(200);
445 if(mapTimer != NULL) {
447 disconnect(mapTimer, SIGNAL(timeout()), this, SLOT(paintMap()));
450 mapImage->setVisible(show);
453 void RobomonAtlantis::paintMap()
456 struct map *map = ShmapIsMapInit();
460 for(int i = 0; i < MAP_WIDTH; i++) {
461 for(int j = 0; j < MAP_HEIGHT; j++) {
464 struct map_cell *cell = &map->cells[j][i];
467 if (cell->flags & MAP_FLAG_PLAN_MASK)
469 if ((cell->flags & MAP_FLAG_WALL) &&
470 (cell->flags & MAP_FLAG_INVALIDATE_WALL) == 0)
472 if (cell->flags & MAP_FLAG_IGNORE_OBST)
474 if (cell->flags & MAP_FLAG_SIMULATED_WALL)
476 if (cell->flags & MAP_FLAG_PATH)
478 if (cell->flags & MAP_FLAG_START)
480 if (cell->flags & MAP_FLAG_GOAL)
482 if (cell->flags & MAP_FLAG_PLAN_MARGIN) {
483 QColor c(240, 170, 50); /* orange */
486 if (cell->detected_obstacle) {
487 QColor c1(color), c2(blue);
488 double f = (double)cell->detected_obstacle/MAP_NEW_OBSTACLE*0.7;
489 QColor c(c1.red() + (int)(f*(c2.red() - c1.red())),
490 c1.green() + (int)(f*(c2.green() - c1.green())),
491 c1.blue() + (int)(f*(c2.blue() - c1.blue())));
494 if (cell->flags & MAP_FLAG_DET_OBST)
498 mapImage->setPixelColor(i, MAP_HEIGHT - j - 1, color);
503 void RobomonAtlantis::setSimulation(int state)
506 robottype_publisher_hokuyo_scan_create(&orte, NULL, this);
508 if (!simulationEnabled)
510 robottype_publisher_hokuyo_scan_destroy(&orte);
512 simulationEnabled = state;
516 \fn RobomonAtlantis::setObstacleSimulation(int state)
518 void RobomonAtlantis::setObstacleSimulation(int state)
521 /* TODO Maybe it is possible to attach only once to Shmap */
523 obstacleSimulationTimer = new QTimer(this);
524 connect(obstacleSimulationTimer, SIGNAL(timeout()),
525 this, SLOT(simulateObstaclesHokuyo()));
526 obstacleSimulationTimer->start(100);
527 setMouseTracking(true);
529 if (obstacleSimulationTimer)
530 delete obstacleSimulationTimer;
531 //double distance = 0.8;
536 void RobomonAtlantis::simulateObstaclesHokuyo()
538 double distance, wall_distance;
540 uint16_t *hokuyo = orte.hokuyo_scan.data;
542 for (i=0; i<HOKUYO_ARRAY_SIZE; i++) {
543 wall_distance = distanceToWallHokuyo(i);
545 distance = distanceToCircularObstacleHokuyo(i, simulatedObstacle, SIM_OBST_SIZE_M);
546 if (wall_distance < distance)
547 distance = wall_distance;
548 hokuyo[i] = distance*1000;
550 ORTEPublicationSend(orte.publication_hokuyo_scan);
554 void RobomonAtlantis::changeObstacle(QPointF position)
556 if (!simulationEnabled) {
557 simulationEnabled = 1;
558 obstacleSimulationCheckBox->setChecked(true);
561 simulatedObstacle.x = position.x();
562 simulatedObstacle.y = position.y();
563 simulateObstaclesHokuyo();
566 /**********************************************************************
568 **********************************************************************/
569 bool RobomonAtlantis::event(QEvent *event)
571 switch (event->type()) {
572 case QEVENT(QEV_MOTION_STATUS):
573 emit motionStatusReceivedSignal();
575 case QEVENT(QEV_HOKUYO_SCAN):
576 hokuyoScan->newScan(&orte.hokuyo_scan);
578 // case QEVENT(QEV_VIDLE_CMD):
579 // robotEstPosBest->setVidle(orte.vidle_cmd.req_pos);
581 case QEVENT(QEV_REFERENCE_POSITION):
582 emit actualPositionReceivedSignal();
584 case QEVENT(QEV_ESTIMATED_POSITION_INDEP_ODO):
585 estPosX->setText(QString("%1").arg(orte.est_pos_indep_odo.x, 0, 'f', 3));
586 estPosY->setText(QString("%1").arg(orte.est_pos_indep_odo.y, 0, 'f', 3));
587 estPosPhi->setText(QString("%1(%2)")
588 .arg(DEGREES(orte.est_pos_indep_odo.phi), 0, 'f', 0)
589 .arg(orte.est_pos_indep_odo.phi, 0, 'f', 1));
590 robotEstPosIndepOdo->moveRobot(orte.est_pos_indep_odo.x,
591 orte.est_pos_indep_odo.y, orte.est_pos_indep_odo.phi);
592 trailPosIndepOdo->addPoint(QPointF(orte.est_pos_indep_odo.x,
593 orte.est_pos_indep_odo.y));
595 case QEVENT(QEV_ESTIMATED_POSITION_ODO):
596 robotEstPosOdo->moveRobot(orte.est_pos_odo.x,
597 orte.est_pos_odo.y, orte.est_pos_odo.phi);
598 trailOdoPos->addPoint(QPointF(orte.est_pos_odo.x,
599 orte.est_pos_odo.y));
601 case QEVENT(QEV_ESTIMATED_POSITION_BEST):
602 robotEstPosBest->moveRobot(orte.est_pos_best.x,
603 orte.est_pos_best.y, orte.est_pos_best.phi);
604 trailEstPosBest->addPoint(QPointF(orte.est_pos_best.x,
605 orte.est_pos_best.y));
606 hokuyoScan->setPosition(orte.est_pos_best.x,
608 orte.est_pos_best.phi);
610 case QEVENT(QEV_POWER_VOLTAGE):
611 emit powerVoltageReceivedSignal();
613 case QEVENT(QEV_FSM_MAIN):
614 fsm_main_state->setText(orte.fsm_main.state_name);
616 case QEVENT(QEV_FSM_ACT):
617 fsm_act_state->setText(orte.fsm_act.state_name);
619 case QEVENT(QEV_FSM_MOTION):
620 fsm_motion_state->setText(orte.fsm_motion.state_name);
623 if (event->type() == QEvent::Close)
624 closeEvent((QCloseEvent *)event);
625 else if (event->type() == QEvent::KeyPress)
626 keyPressEvent((QKeyEvent *)event);
627 else if (event->type() == QEvent::KeyRelease)
628 keyReleaseEvent((QKeyEvent *)event);
629 else if (event->type() == QEvent::FocusIn)
631 else if (event->type() == QEvent::FocusOut)
643 void RobomonAtlantis::keyPressEvent(QKeyEvent *event)
645 // double peak, gain;
647 if (event->isAutoRepeat()) {
648 switch (event->key()) {
649 // case Qt::Key_Down:
650 // peak = leftMotorSlider->minimum()/2;
651 // if (leftMotorValue < peak ||
652 // rightMotorValue < peak)
656 // leftMotorValue *= gain;
657 // rightMotorValue *= gain;
658 // leftMotorSlider->setValue((int)leftMotorValue);
659 // rightMotorSlider->setValue((int)rightMotorValue);
663 // case Qt::Key_Left:
664 // case Qt::Key_Right:
665 // peak = leftMotorSlider->maximum()/2;
666 // if (leftMotorValue > peak ||
667 // rightMotorValue > peak)
671 // leftMotorValue *= gain;
672 // rightMotorValue *= gain;
673 // leftMotorSlider->setValue((int)leftMotorValue);
674 // rightMotorSlider->setValue((int)rightMotorValue);
684 switch (event->key()) {
686 // leftMotorValue = 1;
687 // rightMotorValue = 1;
688 // bothMotorsCheckBox->setChecked(true);
689 // leftMotorSlider->setValue((int)leftMotorValue);
690 // setLeftMotor((int)leftMotorValue);
692 // case Qt::Key_Down:
693 // leftMotorValue = -1;
694 // rightMotorValue = -1;
695 // bothMotorsCheckBox->setChecked(true);
696 // leftMotorSlider->setValue((int)leftMotorValue);
697 // setLeftMotor((int)leftMotorValue);
699 // case Qt::Key_Left:
700 // leftMotorValue = -1;
701 // rightMotorValue = 1;
702 // leftMotorSlider->setValue((int)leftMotorValue);
703 // rightMotorSlider->setValue((int)rightMotorValue);
704 // setLeftMotor((int)leftMotorValue);
705 // setRightMotor((int)leftMotorValue);
707 // case Qt::Key_Right:
708 // leftMotorValue = 1;
709 // rightMotorValue = -1;
710 // leftMotorSlider->setValue((int)leftMotorValue);
711 // rightMotorSlider->setValue((int)rightMotorValue);
712 // setLeftMotor((int)leftMotorValue);
713 // setRightMotor((int)rightMotorValue);
722 void RobomonAtlantis::keyReleaseEvent(QKeyEvent *event)
724 if (event->isAutoRepeat()) {
729 switch (event->key()) {
731 // case Qt::Key_Down:
732 // case Qt::Key_Left:
733 // case Qt::Key_Right:
734 // leftMotorValue = 0;
735 // rightMotorValue = 0;
736 // bothMotorsCheckBox->setChecked(false);
737 // leftMotorSlider->setValue((int)leftMotorValue);
738 // rightMotorSlider->setValue((int)rightMotorValue);
747 void RobomonAtlantis::closeEvent(QCloseEvent *)
749 robottype_roboorte_destroy(&orte);
752 /**********************************************************************
754 **********************************************************************/
755 void RobomonAtlantis::createOrte()
761 memset(&orte, 0, sizeof(orte));
762 rv = robottype_roboorte_init(&orte);
764 printf("RobomonAtlantis: Unable to initialize ORTE\n");
768 robottype_publisher_motion_speed_create(&orte, dummy_publisher_callback, NULL);
770 robottype_publisher_pwr_ctrl_create(&orte, dummy_publisher_callback, NULL);
771 robottype_publisher_robot_cmd_create(&orte, NULL, &orte);
772 robottype_publisher_robot_switches_create(&orte, dummy_publisher_callback, &orte);
775 robottype_subscriber_pwr_voltage_create(&orte,
776 receivePowerVoltageCallBack, this);
777 robottype_subscriber_motion_status_create(&orte,
778 receiveMotionStatusCallBack, this);
779 robottype_subscriber_ref_pos_create(&orte,
780 receiveActualPositionCallBack, this);
781 robottype_subscriber_est_pos_odo_create(&orte,
782 generic_rcv_cb, new OrteCallbackInfo(this, QEV_ESTIMATED_POSITION_ODO));
783 robottype_subscriber_est_pos_indep_odo_create(&orte,
784 generic_rcv_cb, new OrteCallbackInfo(this, QEV_ESTIMATED_POSITION_INDEP_ODO));
785 robottype_subscriber_est_pos_best_create(&orte,
786 generic_rcv_cb, new OrteCallbackInfo(this, QEV_ESTIMATED_POSITION_BEST));
787 robottype_subscriber_hokuyo_scan_create(&orte,
788 generic_rcv_cb, new OrteCallbackInfo(this, QEV_HOKUYO_SCAN));
789 robottype_subscriber_vidle_cmd_create(&orte,
790 generic_rcv_cb, new OrteCallbackInfo(this, QEV_VIDLE_CMD));
791 robottype_subscriber_fsm_main_create(&orte,
792 rcv_fsm_main_cb, this);
793 robottype_subscriber_fsm_motion_create(&orte,
794 rcv_fsm_motion_cb, this);
795 robottype_subscriber_fsm_act_create(&orte,
796 rcv_fsm_act_cb, this);
799 orte.motion_speed.left = 0;
800 orte.motion_speed.right = 0;
802 /* power management */
803 orte.pwr_ctrl.voltage33 = true;
804 orte.pwr_ctrl.voltage50 = true;
805 orte.pwr_ctrl.voltage80 = true;
806 voltage33CheckBox->setChecked(true);
807 voltage50CheckBox->setChecked(true);
808 voltage80CheckBox->setChecked(true);
812 /* set actions to do when we receive data from orte */
813 connect(this, SIGNAL(motionStatusReceivedSignal()),
814 this, SLOT(motionStatusReceived()));
815 connect(this, SIGNAL(actualPositionReceivedSignal()),
816 this, SLOT(actualPositionReceived()));
817 connect(this, SIGNAL(powerVoltageReceivedSignal()),
818 this, SLOT(powerVoltageReceived()));
821 void RobomonAtlantis::motionStatusReceived()
823 WDBG("ORTE received: motion status");
826 void RobomonAtlantis::actualPositionReceived()
828 actPosX->setText(QString("%1").arg(orte.ref_pos.x, 0, 'f', 3));
829 actPosY->setText(QString("%1").arg(orte.ref_pos.y, 0, 'f', 3));
830 actPosPhi->setText(QString("%1(%2)")
831 .arg(DEGREES(orte.ref_pos.phi), 0, 'f', 0)
832 .arg(orte.ref_pos.phi, 0, 'f', 1));
833 robotRefPos->moveRobot(orte.ref_pos.x,
834 orte.ref_pos.y, orte.ref_pos.phi);
835 trailRefPos->addPoint(QPointF(orte.ref_pos.x, orte.ref_pos.y));
838 void RobomonAtlantis::powerVoltageReceived()
840 voltage33LineEdit->setText(QString("%1").arg(
841 orte.pwr_voltage.voltage33, 0, 'f', 3));
842 voltage50LineEdit->setText(QString("%1").arg(
843 orte.pwr_voltage.voltage50, 0, 'f', 3));
844 voltage80LineEdit->setText(QString("%1").arg(
845 orte.pwr_voltage.voltage80, 0, 'f', 3));
846 voltageBATLineEdit->setText(QString("%1").arg(
847 orte.pwr_voltage.voltageBAT, 0, 'f', 3));
851 /**********************************************************************
853 **********************************************************************/
854 void RobomonAtlantis::openSharedMemory()
857 int sharedSegmentSize;
859 if (sharedMemoryOpened)
862 sharedSegmentSize = sizeof(unsigned int) * MAP_WIDTH * MAP_HEIGHT;
864 /* Get segment identificator in a read only mode */
865 segmentId = shmget(SHM_MAP_KEY, sharedSegmentSize, S_IRUSR);
866 if(segmentId == -1) {
867 QMessageBox::critical(this, "robomon",
868 "Unable to open shared memory segment!");
875 /* Attach the shared memory segment */
876 //map = (_Map*)shmat (segmentId, (void*) 0, 0);
878 sharedMemoryOpened = true;
881 double RobomonAtlantis::distanceToWallHokuyo(int beamnum)
883 double distance=4.0, min_distance=4.0;
886 struct map *map = ShmapIsMapInit();
888 if (!map) return min_distance;
890 // Simulate obstacles
891 for(j=0;j<MAP_HEIGHT;j++) {
892 for (i=0;i<MAP_WIDTH;i++) {
893 struct map_cell *cell = &map->cells[j][i];
894 if( cell->flags & MAP_FLAG_SIMULATED_WALL) {
896 ShmapCell2Point(i, j, &wall.x, &wall.y);
898 distance = distanceToObstacleHokuyo(beamnum, wall, MAP_CELL_SIZE_M);
899 if (distance<min_distance) min_distance = distance;
907 double RobomonAtlantis::distanceToCircularObstacleHokuyo(int beamnum, Point center, double diameter)
909 struct robot_pos_type e = orte.est_pos_best;
913 s.x = HOKUYO_CENTER_OFFSET_M;
915 s.ang = HOKUYO_INDEX_TO_RAD(beamnum);
917 Point sensor(e.x + s.x*cos(e.phi) - s.y*sin(e.phi),
918 e.y + s.x*sin(e.phi) + s.y*cos(e.phi));
919 sensor_a = e.phi + s.ang;
921 const double sensorRange = 4.0; /*[meters]*/
923 double distance = sensorRange;
926 angle = sensor.angleTo(center) - sensor_a;
927 angle = fmod(angle, 2.0*M_PI);
928 if (angle > +M_PI) angle -= 2.0*M_PI;
929 if (angle < -M_PI) angle += 2.0*M_PI;
932 double k = tan(sensor_a);
933 double r = diameter / 2.0;
936 double B = 2 * (sensor.y*k - center.x - k*k*sensor.x - center.y*k);
937 double C = center.x*center.x + center.y*center.y +
938 k*k*sensor.x*sensor.x - 2*sensor.y*k*sensor.x +
939 sensor.y*sensor.y + 2*k*sensor.x*center.y -
940 2*sensor.y*center.y - r*r;
942 double D = B*B - 4*A*C;
947 ob1.x = (-B + sqrt(D)) / (2*A);
948 ob2.x = (-B - sqrt(D)) / (2*A);
949 ob1.y = k * (ob1.x - sensor.x) + sensor.y;
950 ob2.y = k * (ob2.x - sensor.x) + sensor.y;
952 double distance1 = sensor.distanceTo(ob1);
953 double distance2 = sensor.distanceTo(ob2);
954 distance = (distance1 < distance2) ? distance1 : distance2;
958 ob.y = k * (ob.x - sensor.x) + sensor.y;
959 distance = sensor.distanceTo(ob);
961 distance = distance + (drand48()-0.5)*3.0e-2;
962 if (D < 0 || angle > atan(r / distance))
963 distance = sensorRange;
964 if (distance > sensorRange)
965 distance = sensorRange;
971 * Calculation for Hokuyo simulation. Calculates distance that would
972 * be returned by Hokuyo sensors, if there is only one obstacle (as
973 * specified by parameters).
975 * @param beamnum Hokuyo's bean number [0..HOKUYO_CLUSTER_CNT]
976 * @param obstacle Position of the obstacle (x, y in meters).
977 * @param obstacleSize Size (diameter) of the obstacle in meters.
979 * @return Distance measured by sensors in meters.
981 double RobomonAtlantis::distanceToObstacleHokuyo(int beamnum, Point obstacle, double obstacleSize)
983 struct robot_pos_type e = orte.est_pos_best;
987 s.x = HOKUYO_CENTER_OFFSET_M;
989 s.ang = HOKUYO_INDEX_TO_RAD(beamnum);
991 Point sensor(e.x + s.x*cos(e.phi) - s.y*sin(e.phi),
992 e.y + s.x*sin(e.phi) + s.y*cos(e.phi));
993 sensor_a = e.phi + s.ang;
995 const double sensorRange = 4.0; /*[meters]*/
997 double distance, angle;
999 angle = sensor.angleTo(obstacle) - sensor_a;
1000 angle = fmod(angle, 2.0*M_PI);
1001 if (angle > +M_PI) angle -= 2.0*M_PI;
1002 if (angle < -M_PI) angle += 2.0*M_PI;
1003 angle = fabs(angle);
1004 distance = sensor.distanceTo(obstacle) - obstacleSize/2.0;
1005 if (angle < atan(obstacleSize/2.0 / distance)) {
1006 // We can see the obstackle from here.
1007 if (angle < M_PI/2.0) {
1008 distance = distance/cos(angle);
1010 if (distance > sensorRange)
1011 distance = sensorRange;
1013 distance = sensorRange;
1019 void RobomonAtlantis::sendStart(int plug)
1021 orte.robot_cmd.start_condition = plug ? 0 : 1;
1022 ORTEPublicationSend(orte.publication_robot_cmd);
1025 void RobomonAtlantis::setTeamColor(int plug)
1027 orte.robot_switches.team_color = plug ? 1 : 0;
1028 ORTEPublicationSend(orte.publication_robot_switches);
1031 void RobomonAtlantis::resetTrails()
1033 trailRefPos->reset();
1034 trailEstPosBest->reset();
1035 trailPosIndepOdo->reset();
1036 trailOdoPos->reset();
1039 void RobomonAtlantis::showTrails(bool show)
1041 trailRefPos->setVisible(show && robotRefPos->isVisible());
1042 trailEstPosBest->setVisible(show && robotEstPosBest->isVisible());
1043 trailPosIndepOdo->setVisible(show && robotEstPosIndepOdo->isVisible());
1044 trailOdoPos->setVisible(show && robotEstPosOdo->isVisible());
1047 void RobomonAtlantis::showShapeDetect(bool show)
1049 hokuyoScan->showShapeDetect = show;