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 strategyButton= new QPushButton(tr("Strategy"));
187 layout->addWidget(strategyButton);
189 miscGroupBox->setLayout(layout);
192 void RobomonAtlantis::createFSMGroupBox()
194 fsmGroupBox = new QGroupBox(tr("FSM"));
195 fsmGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
196 QGridLayout *layout = new QGridLayout();
198 layout->addWidget(MiscGui::createLabel("Main:"), 1, 0);
199 fsm_main_state = new QLabel();
200 fsm_main_state->setMinimumWidth(100);
201 fsm_main_state->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
202 layout->addWidget(fsm_main_state, 1, 1);
204 layout->addWidget(MiscGui::createLabel("Act:"), 2, 0);
205 fsm_act_state = new QLabel();
206 layout->addWidget(fsm_act_state, 2, 1);
208 layout->addWidget(MiscGui::createLabel("Motion:"), 3, 0);
209 fsm_motion_state = new QLabel();
210 layout->addWidget(fsm_motion_state, 3, 1);
212 fsmGroupBox->setLayout(layout);
215 void RobomonAtlantis::createDebugGroupBox()
217 debugGroupBox = new QGroupBox(tr("Debug window"));
218 debugGroupBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
219 QHBoxLayout *layout = new QHBoxLayout();
221 debugWindow = new QTextEdit();
222 debugWindow->setReadOnly(true);
224 layout->addWidget(debugWindow);
225 debugGroupBox->setLayout(layout);
228 void RobomonAtlantis::createActuatorsGroupBox()
230 actuatorsGroupBox = new QGroupBox(tr("Actuators"));
231 actuatorsGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
232 QHBoxLayout *layout = new QHBoxLayout();
233 // vidle = new QDial();
235 // vidle->setMinimum(VIDLE_VYSIP);
236 // vidle->setMaximum((VIDLE_UP-VIDLE_VYSIP)+VIDLE_VYSIP);
237 // vidle->setEnabled(true);
239 //createMotorsGroupBox();
241 layout->setAlignment(Qt::AlignLeft);
242 // layout->addWidget(vidle);
243 //layout->addWidget(enginesGroupBox);
244 actuatorsGroupBox->setLayout(layout);
247 void RobomonAtlantis::createPowerGroupBox()
249 powerGroupBox = new QGroupBox(tr("Power management"));
250 powerGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
251 QGridLayout *layout = new QGridLayout();
253 voltage33CheckBox = new QCheckBox(tr("&3.3V"));
254 voltage50CheckBox = new QCheckBox(tr("&5.0V"));
255 voltage80CheckBox = new QCheckBox(tr("&8.0V"));
257 voltage33CheckBox->setShortcut(tr("3"));
258 voltage50CheckBox->setShortcut(tr("5"));
259 voltage80CheckBox->setShortcut(tr("8"));
261 layout->addWidget(voltage33CheckBox, 0, 0);
262 layout->addWidget(voltage50CheckBox, 1, 0);
263 layout->addWidget(voltage80CheckBox, 2, 0);
264 layout->addWidget(MiscGui::createLabel("BAT"), 3, 0);
266 voltage33LineEdit = new QLineEdit();
267 voltage50LineEdit = new QLineEdit();
268 voltage80LineEdit = new QLineEdit();
269 voltageBATLineEdit = new QLineEdit();
271 voltage33LineEdit->setReadOnly(true);
272 voltage50LineEdit->setReadOnly(true);
273 voltage80LineEdit->setReadOnly(true);
274 voltageBATLineEdit->setReadOnly(true);
276 layout->addWidget(voltage33LineEdit, 0, 1);
277 layout->addWidget(voltage50LineEdit, 1, 1);
278 layout->addWidget(voltage80LineEdit, 2, 1);
279 layout->addWidget(voltageBATLineEdit, 3, 1);
281 powerGroupBox->setLayout(layout);
284 void RobomonAtlantis::createRobots()
286 robotRefPos = new Robot("Ref", QPen(Qt::darkBlue), QBrush(Qt::NoBrush));
287 robotRefPos->setZValue(11);
288 trailRefPos = new Trail(QPen(Qt::darkBlue));
289 trailRefPos->setZValue(11);
291 robotEstPosBest = new Robot("Est", QPen(), QBrush(Qt::darkGray));
292 robotEstPosBest->setZValue(10);
293 trailEstPosBest = new Trail(QPen());
294 trailEstPosBest->setZValue(10);
296 robotEstPosOdo = new Robot("Mot", QPen(Qt::white), QBrush(Qt::darkRed));
297 robotEstPosOdo->setZValue(10);
298 trailOdoPos = new Trail(QPen(Qt::red));
299 trailOdoPos->setZValue(10);
301 robotEstPosIndepOdo = new Robot("Odo", QPen(Qt::white), QBrush(Qt::darkGreen));
302 robotEstPosIndepOdo->setZValue(10);
303 trailPosIndepOdo = new Trail(QPen(Qt::green));
304 trailPosIndepOdo->setZValue(10);
306 playgroundScene->addItem(robotRefPos);
307 playgroundScene->addItem(robotEstPosBest);
308 playgroundScene->addItem(robotEstPosIndepOdo);
309 playgroundScene->addItem(robotEstPosOdo);
313 playgroundScene->addItem(trailRefPos);
314 playgroundScene->addItem(trailPosIndepOdo);
315 playgroundScene->addItem(trailOdoPos);
317 hokuyoScan = new HokuyoScan();
318 hokuyoScan->setZValue(10);
319 playgroundScene->addItem(hokuyoScan);
323 void RobomonAtlantis::createMap()
325 mapImage = new Map();
326 mapImage->setZValue(5);
327 mapImage->setTransform(QTransform().scale(MAP_CELL_SIZE_MM, MAP_CELL_SIZE_MM), true);
330 playgroundScene->addItem(mapImage);
333 /**********************************************************************
335 **********************************************************************/
336 void RobomonAtlantis::createActions()
338 /* power management */
339 connect(voltage33CheckBox, SIGNAL(stateChanged(int)),
340 this, SLOT(setVoltage33(int)));
341 connect(voltage50CheckBox, SIGNAL(stateChanged(int)),
342 this, SLOT(setVoltage50(int)));
343 connect(voltage80CheckBox, SIGNAL(stateChanged(int)),
344 this, SLOT(setVoltage80(int)));
347 // connect(leftMotorSlider, SIGNAL(valueChanged(int)),
348 // this, SLOT(setLeftMotor(int)));
349 // connect(rightMotorSlider, SIGNAL(valueChanged(int)),
350 // this, SLOT(setRightMotor(int)));
351 // connect(stopMotorsPushButton, SIGNAL(clicked()),
352 // this, SLOT(stopMotors()));
354 connect(startPlug, SIGNAL(stateChanged(int)), this, SLOT(sendStart(int)));
355 connect(colorChoser, SIGNAL(stateChanged(int)), this, SLOT(setTeamColor(int)));
356 connect(strategyButton, SIGNAL(pressed()), this, SLOT(changeStrategy_1()));
357 connect(strategyButton, SIGNAL(released()), this, SLOT(changeStrategy_0()));
359 /* obstacle simulation */
360 simulationEnabled = 0;
361 connect(obstacleSimulationCheckBox, SIGNAL(stateChanged(int)),
362 this, SLOT(setSimulation(int)));
363 connect(obstacleSimulationCheckBox, SIGNAL(stateChanged(int)),
364 this, SLOT(setObstacleSimulation(int)));
365 connect(obstacleSimulationCheckBox, SIGNAL(stateChanged(int)),
366 playgroundScene, SLOT(showObstacle(int)));
367 connect(playgroundScene, SIGNAL(obstacleChanged(QPointF)),
368 this, SLOT(changeObstacle(QPointF)));
371 void RobomonAtlantis::changeStrategy_1()
373 orte.robot_switches.strategy = true;
374 ORTEPublicationSend(orte.publication_robot_switches);
377 void RobomonAtlantis::changeStrategy_0()
379 orte.robot_switches.strategy = false;
380 ORTEPublicationSend(orte.publication_robot_switches);
383 void RobomonAtlantis::setVoltage33(int state)
386 orte.pwr_ctrl.voltage33 = true;
388 orte.pwr_ctrl.voltage33 = false;
391 void RobomonAtlantis::setVoltage50(int state)
394 orte.pwr_ctrl.voltage50 = true;
396 orte.pwr_ctrl.voltage50 = false;
399 void RobomonAtlantis::setVoltage80(int state)
402 orte.pwr_ctrl.voltage80 = true;
404 orte.pwr_ctrl.voltage80 = false;
407 // void RobomonAtlantis::setLeftMotor(int value)
409 // short int leftMotor;
410 // short int rightMotor;
412 // if(bothMotorsCheckBox->isChecked())
413 // rightMotorSlider->setValue(value);
415 // leftMotor = (short int)(MOTOR_LIMIT * (leftMotorSlider->value()/100.0));
416 // rightMotor = (short int)(MOTOR_LIMIT * (rightMotorSlider->value()/100.0));
418 // orte.motion_speed.left = leftMotor;
419 // orte.motion_speed.right = rightMotor;
423 // void RobomonAtlantis::setRightMotor(int value)
425 // short int leftMotor;
426 // short int rightMotor;
428 // if(bothMotorsCheckBox->isChecked())
429 // leftMotorSlider->setValue(value);
431 // leftMotor = (short int)(MOTOR_LIMIT * (leftMotorSlider->value()/100.0));
432 // rightMotor = (short int)(MOTOR_LIMIT * (rightMotorSlider->value()/100.0));
434 // orte.motion_speed.left = leftMotor;
435 // orte.motion_speed.right = rightMotor;
439 // void RobomonAtlantis::stopMotors()
441 // leftMotorSlider->setValue(0);
442 // rightMotorSlider->setValue(0);
445 void RobomonAtlantis::useOpenGL(bool use)
447 playgroundSceneView->useOpenGL(&use);
450 void RobomonAtlantis::showMap(bool show)
454 if (sharedMemoryOpened == false)
458 mapTimer = new QTimer(this);
459 connect(mapTimer, SIGNAL(timeout()), this, SLOT(paintMap()));
460 mapTimer->start(200);
462 if(mapTimer != NULL) {
464 disconnect(mapTimer, SIGNAL(timeout()), this, SLOT(paintMap()));
467 mapImage->setVisible(show);
470 void RobomonAtlantis::paintMap()
473 struct map *map = ShmapIsMapInit();
477 for(int i = 0; i < MAP_WIDTH; i++) {
478 for(int j = 0; j < MAP_HEIGHT; j++) {
481 struct map_cell *cell = &map->cells[j][i];
484 if ((cell->flags & MAP_FLAG_WALL) &&
485 (cell->flags & MAP_FLAG_INVALIDATE_WALL) == 0)
487 if (cell->flags & MAP_FLAG_IGNORE_OBST)
489 if (cell->flags & MAP_FLAG_SIMULATED_WALL)
491 if (cell->flags & MAP_FLAG_PATH)
493 if (cell->flags & MAP_FLAG_START)
495 if (cell->flags & MAP_FLAG_GOAL)
497 if (cell->flags & MAP_FLAG_PLAN_MARGIN) {
498 QColor c(240, 170, 50); /* orange */
501 if (cell->detected_obstacle) {
502 QColor c1(color), c2(blue);
503 double f = (double)cell->detected_obstacle/MAP_NEW_OBSTACLE*0.7;
504 QColor c(c1.red() + (int)(f*(c2.red() - c1.red())),
505 c1.green() + (int)(f*(c2.green() - c1.green())),
506 c1.blue() + (int)(f*(c2.blue() - c1.blue())));
509 if (cell->flags & MAP_FLAG_DET_OBST)
513 mapImage->setPixelColor(i, MAP_HEIGHT - j - 1, color);
518 void RobomonAtlantis::setSimulation(int state)
521 robottype_publisher_hokuyo_scan_create(&orte, NULL, this);
523 if (!simulationEnabled)
525 robottype_publisher_hokuyo_scan_destroy(&orte);
527 simulationEnabled = state;
531 \fn RobomonAtlantis::setObstacleSimulation(int state)
533 void RobomonAtlantis::setObstacleSimulation(int state)
536 /* TODO Maybe it is possible to attach only once to Shmap */
538 obstacleSimulationTimer = new QTimer(this);
539 connect(obstacleSimulationTimer, SIGNAL(timeout()),
540 this, SLOT(simulateObstaclesHokuyo()));
541 obstacleSimulationTimer->start(100);
542 setMouseTracking(true);
544 if (obstacleSimulationTimer)
545 delete obstacleSimulationTimer;
546 //double distance = 0.8;
551 void RobomonAtlantis::simulateObstaclesHokuyo()
553 double distance, wall_distance;
555 uint16_t *hokuyo = orte.hokuyo_scan.data;
557 for (i=0; i<HOKUYO_ARRAY_SIZE; i++) {
558 wall_distance = distanceToWallHokuyo(i);
560 distance = distanceToCircularObstacleHokuyo(i, simulatedObstacle, SIM_OBST_SIZE_M);
561 if (wall_distance < distance)
562 distance = wall_distance;
563 hokuyo[i] = distance*1000;
565 ORTEPublicationSend(orte.publication_hokuyo_scan);
569 void RobomonAtlantis::changeObstacle(QPointF position)
571 if (!simulationEnabled) {
572 simulationEnabled = 1;
573 obstacleSimulationCheckBox->setChecked(true);
576 simulatedObstacle.x = position.x();
577 simulatedObstacle.y = position.y();
578 simulateObstaclesHokuyo();
581 /**********************************************************************
583 **********************************************************************/
584 bool RobomonAtlantis::event(QEvent *event)
586 switch (event->type()) {
587 case QEVENT(QEV_MOTION_STATUS):
588 emit motionStatusReceivedSignal();
590 case QEVENT(QEV_HOKUYO_SCAN):
591 hokuyoScan->newScan(&orte.hokuyo_scan);
593 case QEVENT(QEV_JAWS_CMD):
594 robotEstPosBest->setJaws(orte.jaws_cmd.req_pos.left);
595 robotRefPos->setJaws(orte.jaws_cmd.req_pos.left);
596 robotEstPosIndepOdo->setJaws(orte.jaws_cmd.req_pos.left);
597 robotEstPosOdo->setJaws(orte.jaws_cmd.req_pos.left);
599 case QEVENT(QEV_REFERENCE_POSITION):
600 emit actualPositionReceivedSignal();
602 case QEVENT(QEV_ESTIMATED_POSITION_INDEP_ODO):
603 estPosX->setText(QString("%1").arg(orte.est_pos_indep_odo.x, 0, 'f', 3));
604 estPosY->setText(QString("%1").arg(orte.est_pos_indep_odo.y, 0, 'f', 3));
605 estPosPhi->setText(QString("%1(%2)")
606 .arg(DEGREES(orte.est_pos_indep_odo.phi), 0, 'f', 0)
607 .arg(orte.est_pos_indep_odo.phi, 0, 'f', 1));
608 robotEstPosIndepOdo->moveRobot(orte.est_pos_indep_odo.x,
609 orte.est_pos_indep_odo.y, orte.est_pos_indep_odo.phi);
610 trailPosIndepOdo->addPoint(QPointF(orte.est_pos_indep_odo.x,
611 orte.est_pos_indep_odo.y));
613 case QEVENT(QEV_ESTIMATED_POSITION_ODO):
614 robotEstPosOdo->moveRobot(orte.est_pos_odo.x,
615 orte.est_pos_odo.y, orte.est_pos_odo.phi);
616 trailOdoPos->addPoint(QPointF(orte.est_pos_odo.x,
617 orte.est_pos_odo.y));
619 case QEVENT(QEV_ESTIMATED_POSITION_BEST):
620 robotEstPosBest->moveRobot(orte.est_pos_best.x,
621 orte.est_pos_best.y, orte.est_pos_best.phi);
622 trailEstPosBest->addPoint(QPointF(orte.est_pos_best.x,
623 orte.est_pos_best.y));
624 hokuyoScan->setPosition(orte.est_pos_best.x,
626 orte.est_pos_best.phi);
628 case QEVENT(QEV_POWER_VOLTAGE):
629 emit powerVoltageReceivedSignal();
631 case QEVENT(QEV_FSM_MAIN):
632 fsm_main_state->setText(orte.fsm_main.state_name);
634 case QEVENT(QEV_FSM_ACT):
635 fsm_act_state->setText(orte.fsm_act.state_name);
637 case QEVENT(QEV_FSM_MOTION):
638 fsm_motion_state->setText(orte.fsm_motion.state_name);
641 if (event->type() == QEvent::Close)
642 closeEvent((QCloseEvent *)event);
643 else if (event->type() == QEvent::KeyPress)
644 keyPressEvent((QKeyEvent *)event);
645 else if (event->type() == QEvent::KeyRelease)
646 keyReleaseEvent((QKeyEvent *)event);
647 else if (event->type() == QEvent::FocusIn)
649 else if (event->type() == QEvent::FocusOut)
661 void RobomonAtlantis::keyPressEvent(QKeyEvent *event)
663 // double peak, gain;
665 if (event->isAutoRepeat()) {
666 switch (event->key()) {
667 // case Qt::Key_Down:
668 // peak = leftMotorSlider->minimum()/2;
669 // if (leftMotorValue < peak ||
670 // rightMotorValue < peak)
674 // leftMotorValue *= gain;
675 // rightMotorValue *= gain;
676 // leftMotorSlider->setValue((int)leftMotorValue);
677 // rightMotorSlider->setValue((int)rightMotorValue);
681 // case Qt::Key_Left:
682 // case Qt::Key_Right:
683 // peak = leftMotorSlider->maximum()/2;
684 // if (leftMotorValue > peak ||
685 // rightMotorValue > peak)
689 // leftMotorValue *= gain;
690 // rightMotorValue *= gain;
691 // leftMotorSlider->setValue((int)leftMotorValue);
692 // rightMotorSlider->setValue((int)rightMotorValue);
702 switch (event->key()) {
704 // leftMotorValue = 1;
705 // rightMotorValue = 1;
706 // bothMotorsCheckBox->setChecked(true);
707 // leftMotorSlider->setValue((int)leftMotorValue);
708 // setLeftMotor((int)leftMotorValue);
710 // case Qt::Key_Down:
711 // leftMotorValue = -1;
712 // rightMotorValue = -1;
713 // bothMotorsCheckBox->setChecked(true);
714 // leftMotorSlider->setValue((int)leftMotorValue);
715 // setLeftMotor((int)leftMotorValue);
717 // case Qt::Key_Left:
718 // leftMotorValue = -1;
719 // rightMotorValue = 1;
720 // leftMotorSlider->setValue((int)leftMotorValue);
721 // rightMotorSlider->setValue((int)rightMotorValue);
722 // setLeftMotor((int)leftMotorValue);
723 // setRightMotor((int)leftMotorValue);
725 // case Qt::Key_Right:
726 // leftMotorValue = 1;
727 // rightMotorValue = -1;
728 // leftMotorSlider->setValue((int)leftMotorValue);
729 // rightMotorSlider->setValue((int)rightMotorValue);
730 // setLeftMotor((int)leftMotorValue);
731 // setRightMotor((int)rightMotorValue);
740 void RobomonAtlantis::keyReleaseEvent(QKeyEvent *event)
742 if (event->isAutoRepeat()) {
747 switch (event->key()) {
749 // case Qt::Key_Down:
750 // case Qt::Key_Left:
751 // case Qt::Key_Right:
752 // leftMotorValue = 0;
753 // rightMotorValue = 0;
754 // bothMotorsCheckBox->setChecked(false);
755 // leftMotorSlider->setValue((int)leftMotorValue);
756 // rightMotorSlider->setValue((int)rightMotorValue);
765 void RobomonAtlantis::closeEvent(QCloseEvent *)
767 robottype_roboorte_destroy(&orte);
770 /**********************************************************************
772 **********************************************************************/
773 void RobomonAtlantis::createOrte()
777 memset(&orte, 0, sizeof(orte));
778 rv = robottype_roboorte_init(&orte);
780 printf("RobomonAtlantis: Unable to initialize ORTE\n");
784 robottype_publisher_motion_speed_create(&orte, dummy_publisher_callback, NULL);
786 robottype_publisher_pwr_ctrl_create(&orte, dummy_publisher_callback, NULL);
787 robottype_publisher_robot_cmd_create(&orte, NULL, &orte);
788 robottype_publisher_robot_switches_create(&orte, dummy_publisher_callback, &orte);
791 robottype_subscriber_pwr_voltage_create(&orte,
792 receivePowerVoltageCallBack, this);
793 robottype_subscriber_motion_status_create(&orte,
794 receiveMotionStatusCallBack, this);
795 robottype_subscriber_ref_pos_create(&orte,
796 receiveActualPositionCallBack, this);
797 robottype_subscriber_est_pos_odo_create(&orte,
798 generic_rcv_cb, new OrteCallbackInfo(this, QEV_ESTIMATED_POSITION_ODO));
799 robottype_subscriber_est_pos_indep_odo_create(&orte,
800 generic_rcv_cb, new OrteCallbackInfo(this, QEV_ESTIMATED_POSITION_INDEP_ODO));
801 robottype_subscriber_est_pos_best_create(&orte,
802 generic_rcv_cb, new OrteCallbackInfo(this, QEV_ESTIMATED_POSITION_BEST));
803 robottype_subscriber_hokuyo_scan_create(&orte,
804 generic_rcv_cb, new OrteCallbackInfo(this, QEV_HOKUYO_SCAN));
805 robottype_subscriber_jaws_cmd_create(&orte,
806 generic_rcv_cb, new OrteCallbackInfo(this, QEV_JAWS_CMD));
807 robottype_subscriber_fsm_main_create(&orte,
808 rcv_fsm_main_cb, this);
809 robottype_subscriber_fsm_motion_create(&orte,
810 rcv_fsm_motion_cb, this);
811 robottype_subscriber_fsm_act_create(&orte,
812 rcv_fsm_act_cb, this);
815 orte.motion_speed.left = 0;
816 orte.motion_speed.right = 0;
818 /* power management */
819 orte.pwr_ctrl.voltage33 = true;
820 orte.pwr_ctrl.voltage50 = true;
821 orte.pwr_ctrl.voltage80 = true;
822 voltage33CheckBox->setChecked(true);
823 voltage50CheckBox->setChecked(true);
824 voltage80CheckBox->setChecked(true);
828 /* set actions to do when we receive data from orte */
829 connect(this, SIGNAL(motionStatusReceivedSignal()),
830 this, SLOT(motionStatusReceived()));
831 connect(this, SIGNAL(actualPositionReceivedSignal()),
832 this, SLOT(actualPositionReceived()));
833 connect(this, SIGNAL(powerVoltageReceivedSignal()),
834 this, SLOT(powerVoltageReceived()));
837 void RobomonAtlantis::motionStatusReceived()
839 WDBG("ORTE received: motion status");
842 void RobomonAtlantis::actualPositionReceived()
844 actPosX->setText(QString("%1").arg(orte.ref_pos.x, 0, 'f', 3));
845 actPosY->setText(QString("%1").arg(orte.ref_pos.y, 0, 'f', 3));
846 actPosPhi->setText(QString("%1(%2)")
847 .arg(DEGREES(orte.ref_pos.phi), 0, 'f', 0)
848 .arg(orte.ref_pos.phi, 0, 'f', 1));
849 robotRefPos->moveRobot(orte.ref_pos.x,
850 orte.ref_pos.y, orte.ref_pos.phi);
851 trailRefPos->addPoint(QPointF(orte.ref_pos.x, orte.ref_pos.y));
854 void RobomonAtlantis::powerVoltageReceived()
856 voltage33LineEdit->setText(QString("%1").arg(
857 orte.pwr_voltage.voltage33, 0, 'f', 3));
858 voltage50LineEdit->setText(QString("%1").arg(
859 orte.pwr_voltage.voltage50, 0, 'f', 3));
860 voltage80LineEdit->setText(QString("%1").arg(
861 orte.pwr_voltage.voltage80, 0, 'f', 3));
862 voltageBATLineEdit->setText(QString("%1").arg(
863 orte.pwr_voltage.voltageBAT, 0, 'f', 3));
867 /**********************************************************************
869 **********************************************************************/
870 void RobomonAtlantis::openSharedMemory()
873 int sharedSegmentSize;
875 if (sharedMemoryOpened)
878 sharedSegmentSize = sizeof(unsigned int) * MAP_WIDTH * MAP_HEIGHT;
880 /* Get segment identificator in a read only mode */
881 segmentId = shmget(SHM_MAP_KEY, sharedSegmentSize, S_IRUSR);
882 if(segmentId == -1) {
883 QMessageBox::critical(this, "robomon",
884 "Unable to open shared memory segment!");
891 /* Attach the shared memory segment */
892 //map = (_Map*)shmat (segmentId, (void*) 0, 0);
894 sharedMemoryOpened = true;
897 double RobomonAtlantis::distanceToWallHokuyo(int beamnum)
899 double distance=4.0, min_distance=4.0;
902 struct map *map = ShmapIsMapInit();
904 if (!map) return min_distance;
906 // Simulate obstacles
907 for(j=0;j<MAP_HEIGHT;j++) {
908 for (i=0;i<MAP_WIDTH;i++) {
909 struct map_cell *cell = &map->cells[j][i];
910 if( cell->flags & MAP_FLAG_SIMULATED_WALL) {
912 ShmapCell2Point(i, j, &wall.x, &wall.y);
914 distance = distanceToObstacleHokuyo(beamnum, wall, MAP_CELL_SIZE_M);
915 if (distance<min_distance) min_distance = distance;
923 double RobomonAtlantis::distanceToCircularObstacleHokuyo(int beamnum, Point center, double diameter)
925 struct robot_pos_type e = orte.est_pos_best;
929 s.x = HOKUYO_CENTER_OFFSET_M;
931 s.ang = HOKUYO_INDEX_TO_RAD(beamnum);
933 Point sensor(e.x + s.x*cos(e.phi) - s.y*sin(e.phi),
934 e.y + s.x*sin(e.phi) + s.y*cos(e.phi));
935 sensor_a = e.phi + s.ang;
937 const double sensorRange = 4.0; /*[meters]*/
939 double distance = sensorRange;
942 angle = sensor.angleTo(center) - sensor_a;
943 angle = fmod(angle, 2.0*M_PI);
944 if (angle > +M_PI) angle -= 2.0*M_PI;
945 if (angle < -M_PI) angle += 2.0*M_PI;
948 double k = tan(sensor_a);
949 double r = diameter / 2.0;
952 double B = 2 * (sensor.y*k - center.x - k*k*sensor.x - center.y*k);
953 double C = center.x*center.x + center.y*center.y +
954 k*k*sensor.x*sensor.x - 2*sensor.y*k*sensor.x +
955 sensor.y*sensor.y + 2*k*sensor.x*center.y -
956 2*sensor.y*center.y - r*r;
958 double D = B*B - 4*A*C;
963 ob1.x = (-B + sqrt(D)) / (2*A);
964 ob2.x = (-B - sqrt(D)) / (2*A);
965 ob1.y = k * (ob1.x - sensor.x) + sensor.y;
966 ob2.y = k * (ob2.x - sensor.x) + sensor.y;
968 double distance1 = sensor.distanceTo(ob1);
969 double distance2 = sensor.distanceTo(ob2);
970 distance = (distance1 < distance2) ? distance1 : distance2;
974 ob.y = k * (ob.x - sensor.x) + sensor.y;
975 distance = sensor.distanceTo(ob);
977 distance = distance + (drand48()-0.5)*3.0e-2;
978 if (D < 0 || angle > atan(r / distance))
979 distance = sensorRange;
980 if (distance > sensorRange)
981 distance = sensorRange;
987 * Calculation for Hokuyo simulation. Calculates distance that would
988 * be returned by Hokuyo sensors, if there is only one obstacle (as
989 * specified by parameters).
991 * @param beamnum Hokuyo's bean number [0..HOKUYO_CLUSTER_CNT]
992 * @param obstacle Position of the obstacle (x, y in meters).
993 * @param obstacleSize Size (diameter) of the obstacle in meters.
995 * @return Distance measured by sensors in meters.
997 double RobomonAtlantis::distanceToObstacleHokuyo(int beamnum, Point obstacle, double obstacleSize)
999 struct robot_pos_type e = orte.est_pos_best;
1003 s.x = HOKUYO_CENTER_OFFSET_M;
1005 s.ang = HOKUYO_INDEX_TO_RAD(beamnum);
1007 Point sensor(e.x + s.x*cos(e.phi) - s.y*sin(e.phi),
1008 e.y + s.x*sin(e.phi) + s.y*cos(e.phi));
1009 sensor_a = e.phi + s.ang;
1011 const double sensorRange = 4.0; /*[meters]*/
1013 double distance, angle;
1015 angle = sensor.angleTo(obstacle) - sensor_a;
1016 angle = fmod(angle, 2.0*M_PI);
1017 if (angle > +M_PI) angle -= 2.0*M_PI;
1018 if (angle < -M_PI) angle += 2.0*M_PI;
1019 angle = fabs(angle);
1020 distance = sensor.distanceTo(obstacle) - obstacleSize/2.0;
1021 if (angle < atan(obstacleSize/2.0 / distance)) {
1022 // We can see the obstackle from here.
1023 if (angle < M_PI/2.0) {
1024 distance = distance/cos(angle);
1026 if (distance > sensorRange)
1027 distance = sensorRange;
1029 distance = sensorRange;
1035 void RobomonAtlantis::sendStart(int plug)
1037 orte.robot_cmd.start_condition = plug ? 0 : 1;
1038 ORTEPublicationSend(orte.publication_robot_cmd);
1041 void RobomonAtlantis::setTeamColor(int plug)
1043 orte.robot_switches.team_color = plug ? 1 : 0;
1044 ORTEPublicationSend(orte.publication_robot_switches);
1047 void RobomonAtlantis::resetTrails()
1049 trailRefPos->reset();
1050 trailEstPosBest->reset();
1051 trailPosIndepOdo->reset();
1052 trailOdoPos->reset();
1055 void RobomonAtlantis::showTrails(bool show)
1057 trailRefPos->setVisible(show && robotRefPos->isVisible());
1058 trailEstPosBest->setVisible(show && robotEstPosBest->isVisible());
1059 trailPosIndepOdo->setVisible(show && robotEstPosIndepOdo->isVisible());
1060 trailOdoPos->setVisible(show && robotEstPosOdo->isVisible());
1063 void RobomonAtlantis::showShapeDetect(bool show)
1065 hokuyoScan->showShapeDetect = show;