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>
26 #include <path_planner.h>
33 #include <actuators.h>
34 #include "PlaygroundScene.h"
36 #include "robomon_orte.h"
37 #include "RobomonAtlantis.h"
38 #include "playgroundview.h"
40 #include <QCoreApplication>
44 #include <QMessageBox>
46 RobomonAtlantis::RobomonAtlantis(QWidget *parent)
53 debugWindowEnabled = false;
58 QHBoxLayout *mainLayout = new QHBoxLayout;
59 mainLayout->addLayout(leftLayout);
60 mainLayout->addLayout(rightLayout);
61 setLayout(mainLayout);
67 setFocusPolicy(Qt::StrongFocus);
68 sharedMemoryOpened = false;
69 WDBG("Youuuhouuuu!!");
72 /**********************************************************************
74 **********************************************************************/
75 void RobomonAtlantis::createLeftLayout()
77 leftLayout = new QVBoxLayout();
79 createDebugGroupBox();
80 debugWindowEnabled = true;
81 createPlaygroundGroupBox();
82 leftLayout->addWidget(playgroundGroupBox);
83 leftLayout->addWidget(debugGroupBox);
86 void RobomonAtlantis::createRightLayout()
88 rightLayout = new QVBoxLayout();
89 QGridLayout *layout = new QGridLayout();
90 QVBoxLayout *vlayout = new QVBoxLayout();
92 createPositionGroupBox();
95 createActuatorsGroupBox();
97 createPowerGroupBox();
98 createSensorsGroupBox();
100 vlayout->addWidget(positionGroupBox);
101 vlayout->addWidget(miscGroupBox);
102 vlayout->addWidget(fsmGroupBox);
103 layout->addLayout(vlayout, 0, 0);
104 layout->addWidget(actuatorsGroupBox, 0, 1);
105 // layout->addWidget(dioGroupBox, 0, 2);
107 rightLayout->addLayout(layout);
108 rightLayout->addWidget(powerGroupBox);
109 rightLayout->addWidget(sensorsGroupBox);
112 void RobomonAtlantis::createPlaygroundGroupBox()
114 playgroundGroupBox = new QGroupBox(tr("Playground"));
115 QHBoxLayout *layout = new QHBoxLayout();
117 playgroundScene = new PlaygroundScene();
118 playgroundSceneView = new PlaygroundView(playgroundScene);
119 //playgroundSceneView->setMinimumWidth(630);
120 //playgroundSceneView->setMinimumHeight(445);
121 playgroundSceneView->setMatrix(QMatrix(1,0,0,-1,0,0), true);
122 playgroundSceneView->fitInView(playgroundScene->itemsBoundingRect());
123 playgroundSceneView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
124 layout->addWidget(playgroundSceneView);
126 playgroundGroupBox->setLayout(layout);
129 void RobomonAtlantis::createPositionGroupBox()
131 positionGroupBox = new QGroupBox(tr("Position state"));
132 positionGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
133 QGridLayout *layout = new QGridLayout();
135 actPosX = new QLineEdit();
136 actPosY = new QLineEdit();
137 actPosPhi = new QLineEdit();
139 estPosX = new QLineEdit();
140 estPosY = new QLineEdit();
141 estPosPhi = new QLineEdit();
143 actPosX->setReadOnly(true);
144 actPosY->setReadOnly(true);
145 actPosPhi->setReadOnly(true);
147 estPosX->setReadOnly(true);
148 estPosY->setReadOnly(true);
149 estPosPhi->setReadOnly(true);
151 layout->addWidget(MiscGui::createLabel("X"), 1, 0);
152 layout->addWidget(MiscGui::createLabel("Y"), 2, 0);
153 layout->addWidget(MiscGui::createLabel("Phi"), 3, 0);
155 layout->addWidget(MiscGui::createLabel("X"), 5, 0);
156 layout->addWidget(MiscGui::createLabel("Y"), 6, 0);
157 layout->addWidget(MiscGui::createLabel("Phi"), 7, 0);
159 layout->addWidget(MiscGui::createLabel("Reference", Qt::AlignLeft), 0, 1);
160 layout->addWidget(actPosX, 1, 1);
161 layout->addWidget(actPosY, 2, 1);
162 layout->addWidget(actPosPhi, 3, 1);
164 layout->addWidget(MiscGui::createLabel("Estimated", Qt::AlignLeft), 4, 1);
165 layout->addWidget(estPosX, 5, 1);
166 layout->addWidget(estPosY, 6, 1);
167 layout->addWidget(estPosPhi, 7, 1);
169 positionGroupBox->setLayout(layout);
172 void RobomonAtlantis::createMiscGroupBox()
174 miscGroupBox = new QGroupBox(tr("Miscellaneous"));
175 QGridLayout *layout = new QGridLayout();
177 showMapPushButton = new QCheckBox(tr("Show &map"));
178 showMapPushButton->setShortcut(tr("m"));
180 layout->addWidget(showMapPushButton, 0, 0);
182 obstacleSimulationCheckBox = new QCheckBox(tr("&Obstacle simulation"));
183 obstacleSimulationCheckBox->setShortcut(tr("o"));
184 layout->addWidget(obstacleSimulationCheckBox);
186 miscGroupBox->setLayout(layout);
189 void RobomonAtlantis::createFSMGroupBox()
191 fsmGroupBox = new QGroupBox(tr("FSM"));
192 QGridLayout *layout = new QGridLayout();
194 layout->addWidget(MiscGui::createLabel("Main:"), 1, 0);
195 fsm_main_state = new QLabel();
196 layout->addWidget(fsm_main_state, 1, 1);
198 layout->addWidget(MiscGui::createLabel("Act:"), 2, 0);
199 fsm_act_state = new QLabel();
200 layout->addWidget(fsm_act_state, 2, 1);
202 layout->addWidget(MiscGui::createLabel("Motion:"), 3, 0);
203 fsm_motion_state = new QLabel();
204 layout->addWidget(fsm_motion_state, 3, 1);
206 fsmGroupBox->setLayout(layout);
209 void RobomonAtlantis::createDebugGroupBox()
211 debugGroupBox = new QGroupBox(tr("Debug window"));
212 QHBoxLayout *layout = new QHBoxLayout();
214 debugWindow = new QTextEdit();
215 debugWindow->setReadOnly(true);
217 layout->addWidget(debugWindow);
218 debugGroupBox->setLayout(layout);
221 void RobomonAtlantis::createActuatorsGroupBox()
223 actuatorsGroupBox = new QGroupBox(tr("Actuators"));
224 QHBoxLayout *layout = new QHBoxLayout();
226 createMotorsGroupBox();
227 createPickerGroupBox();
229 layout->setAlignment(Qt::AlignLeft);
230 layout->addWidget(enginesGroupBox);
231 layout->addWidget(pickerGroupBox);
232 actuatorsGroupBox->setLayout(layout);
235 void RobomonAtlantis::createDIOGroupBox()
237 dioGroupBox = new QGroupBox(tr("DIO"));
238 QGridLayout *layout = new QGridLayout();
241 font.setPointSize(5);
242 dioGroupBox->setFont(font);
244 for (int i=0; i<8; i++) {
245 diCheckBox[i] = new QCheckBox(QString("DI%1").arg(i));
246 doCheckBox[i] = new QCheckBox(QString("D0%1").arg(i));
247 layout->addWidget(diCheckBox[i], i, 0);
248 layout->addWidget(doCheckBox[i], i+8, 0);
251 dioGroupBox->setMaximumWidth(70);
252 dioGroupBox->setLayout(layout);
255 void RobomonAtlantis::createPowerGroupBox()
257 powerGroupBox = new QGroupBox(tr("Power management"));
258 QGridLayout *layout = new QGridLayout();
260 voltage33CheckBox = new QCheckBox(tr("&3.3V"));
261 voltage50CheckBox = new QCheckBox(tr("&5.0V"));
262 voltage80CheckBox = new QCheckBox(tr("&8.0V"));
264 voltage33CheckBox->setShortcut(tr("3"));
265 voltage50CheckBox->setShortcut(tr("5"));
266 voltage80CheckBox->setShortcut(tr("8"));
268 layout->addWidget(voltage33CheckBox, 0, 0);
269 layout->addWidget(voltage50CheckBox, 0, 2);
270 layout->addWidget(voltage80CheckBox, 0, 4); //1, 0);
271 layout->addWidget(MiscGui::createLabel("BAT"), 0, 6); //1, 2);
273 voltage33LineEdit = new QLineEdit();
274 voltage50LineEdit = new QLineEdit();
275 voltage80LineEdit = new QLineEdit();
276 voltageBATLineEdit = new QLineEdit();
278 voltage33LineEdit->setReadOnly(true);
279 voltage50LineEdit->setReadOnly(true);
280 voltage80LineEdit->setReadOnly(true);
281 voltageBATLineEdit->setReadOnly(true);
283 layout->addWidget(voltage33LineEdit, 0, 1);
284 layout->addWidget(voltage50LineEdit, 0, 3);
285 layout->addWidget(voltage80LineEdit, 0, 5); //1, 1);
286 layout->addWidget(voltageBATLineEdit, 0, 7); //1, 3);
288 powerGroupBox->setLayout(layout);
291 void RobomonAtlantis::createSensorsGroupBox()
293 sensorsGroupBox = new QGroupBox(tr("Sensors"));
294 QVBoxLayout *layout = new QVBoxLayout();
296 createSharpSensorsLayout();
298 layout->addLayout(sharpSensorsLayout);
299 sensorsGroupBox->setLayout(layout);
302 void RobomonAtlantis::createMotorsGroupBox()
304 enginesGroupBox = new QGroupBox(tr("Motors"));
305 QVBoxLayout *layout = new QVBoxLayout();
306 QHBoxLayout *layout1 = new QHBoxLayout();
307 QHBoxLayout *layout2 = new QHBoxLayout();
309 leftMotorSlider = new QSlider(Qt::Vertical);
310 rightMotorSlider = new QSlider(Qt::Vertical);
311 bothMotorsCheckBox = new QCheckBox(tr("Lock both"));
312 stopMotorsPushButton = new QPushButton(tr("Stop Motors"));
314 leftMotorSlider->setMinimum(-100);
315 leftMotorSlider->setMaximum(100);
316 leftMotorSlider->setTracking(false);
317 leftMotorSlider->setTickPosition(QSlider::TicksLeft);
319 rightMotorSlider->setMinimum(-100);
320 rightMotorSlider->setMaximum(100);
321 rightMotorSlider->setTracking(false);
322 rightMotorSlider->setTickPosition(QSlider::TicksRight);
324 stopMotorsPushButton->setMaximumWidth(90);
326 layout1->addWidget(leftMotorSlider);
327 layout1->addWidget(MiscGui::createLabel("0"));
328 layout1->addWidget(rightMotorSlider);
330 layout2->addWidget(bothMotorsCheckBox);
332 layout->addWidget(MiscGui::createLabel("100"));
333 layout->addLayout(layout1);
334 layout->addWidget(MiscGui::createLabel("-100"));
335 layout->addLayout(layout2);
336 layout->addWidget(stopMotorsPushButton);
337 enginesGroupBox->setLayout(layout);
340 void RobomonAtlantis::createPickerGroupBox()
342 pickerGroupBox = new QGroupBox(tr("Picker"));
343 QVBoxLayout *layout = new QVBoxLayout();
345 leftBeltCheckBox = new QCheckBox(tr("L Belt"));
346 rightBeltCheckBox = new QCheckBox(tr("R Belt"));
347 leftChelaCheckBox = new QCheckBox(tr("L Chela"));
348 rightChelaCheckBox = new QCheckBox(tr("R Chela"));
349 leftBeltDial = new QDial();
350 rightBeltDial = new QDial();
351 leftChelaDial = new QDial();
352 rightChelaDial = new QDial();
353 pickPushButton = new QPushButton(tr("Pick!"));
355 leftBeltDial->setMinimum(0);
356 leftBeltDial->setMaximum(200);
357 leftBeltDial->setValue(100);
359 rightBeltDial->setMinimum(0);
360 rightBeltDial->setMaximum(200);
361 rightBeltDial->setValue(100);
363 leftChelaDial->setMinimum(0);
364 leftChelaDial->setMaximum(255);
365 leftChelaDial->setValue(127);
367 rightChelaDial->setMinimum(0);
368 rightChelaDial->setMaximum(255);
369 rightChelaDial->setValue(127);
371 layout->addWidget(leftChelaDial);
372 layout->addWidget(leftChelaCheckBox);
373 layout->addWidget(rightChelaDial);
374 layout->addWidget(rightChelaCheckBox);
375 layout->addWidget(leftBeltDial);
376 layout->addWidget(leftBeltCheckBox);
377 layout->addWidget(rightBeltDial);
378 layout->addWidget(rightBeltCheckBox);
379 layout->addWidget(pickPushButton);
381 pickerGroupBox->setLayout(layout);
384 void RobomonAtlantis::createSharpSensorsLayout()
386 sharpSensorsLayout = new QVBoxLayout();
387 QGridLayout *layout3 = new QGridLayout();
389 sharpSensorsLayout->addLayout(layout3);
392 void RobomonAtlantis::createRobots()
394 robotActPos = new Robot(QPen(), QBrush(Qt::darkGray));
395 robotActPos->setZValue(10);
396 robotEstPos = new Robot(QPen(Qt::darkBlue), QBrush(Qt::NoBrush));
397 robotEstPos->setZValue(11);
399 playgroundScene->addItem(robotActPos);
400 playgroundScene->addItem(robotEstPos);
403 /**********************************************************************
405 **********************************************************************/
406 void RobomonAtlantis::createActions()
408 /* power management */
409 connect(voltage33CheckBox, SIGNAL(stateChanged(int)),
410 this, SLOT(setVoltage33(int)));
411 connect(voltage50CheckBox, SIGNAL(stateChanged(int)),
412 this, SLOT(setVoltage50(int)));
413 connect(voltage80CheckBox, SIGNAL(stateChanged(int)),
414 this, SLOT(setVoltage80(int)));
417 connect(leftMotorSlider, SIGNAL(valueChanged(int)),
418 this, SLOT(setLeftMotor(int)));
419 connect(rightMotorSlider, SIGNAL(valueChanged(int)),
420 this, SLOT(setRightMotor(int)));
421 connect(stopMotorsPushButton, SIGNAL(clicked()),
422 this, SLOT(stopMotors()));
425 for (int i=0; i<8; i++)
426 connect(doCheckBox[0], SIGNAL(stateChanged(int)),
427 this, SLOT(setDO(int)));
429 /* path planning map */
430 connect(showMapPushButton, SIGNAL(clicked()),
431 this, SLOT(showMap()));
433 /* obstacle simulation */
434 simulationEnabled = 0;
435 connect(obstacleSimulationCheckBox, SIGNAL(stateChanged(int)),
436 this, SLOT(setSimulation(int)));
437 connect(obstacleSimulationCheckBox, SIGNAL(stateChanged(int)),
438 this, SLOT(setObstacleSimulation(int)));
439 connect(obstacleSimulationCheckBox, SIGNAL(stateChanged(int)),
440 playgroundScene, SLOT(showObstacle(int)));
441 connect(playgroundScene, SIGNAL(obstacleChanged(QPointF)),
442 this, SLOT(changeObstacle(QPointF)));
445 connect(pickPushButton, SIGNAL(clicked()), this, SLOT(pick()));
446 connect(leftBeltCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setBelts(int)));
447 connect(rightBeltCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setBelts(int)));
448 connect(leftChelaCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setChelae(int)));
449 connect(rightChelaCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setChelae(int)));
450 connect(leftBeltDial, SIGNAL(valueChanged(int)), this, SLOT(setBelts(int)));
451 connect(rightBeltDial, SIGNAL(valueChanged(int)), this, SLOT(setBelts(int)));
452 connect(leftChelaDial, SIGNAL(valueChanged(int)), this, SLOT(setChelae(int)));
453 connect(rightChelaDial, SIGNAL(valueChanged(int)), this, SLOT(setChelae(int)));
456 void RobomonAtlantis::setVoltage33(int state)
459 orte.pwr_ctrl.voltage33 = true;
461 orte.pwr_ctrl.voltage33 = false;
464 void RobomonAtlantis::setVoltage50(int state)
467 orte.pwr_ctrl.voltage50 = true;
469 orte.pwr_ctrl.voltage50 = false;
472 void RobomonAtlantis::setVoltage80(int state)
475 orte.pwr_ctrl.voltage80 = true;
477 orte.pwr_ctrl.voltage80 = false;
480 void RobomonAtlantis::setLeftMotor(int value)
483 short int rightMotor;
485 if(bothMotorsCheckBox->isChecked())
486 rightMotorSlider->setValue(value);
488 leftMotor = (short int)(MOTOR_LIMIT * (leftMotorSlider->value()/100.0));
489 rightMotor = (short int)(MOTOR_LIMIT * (rightMotorSlider->value()/100.0));
491 orte.motion_speed.left = leftMotor;
492 orte.motion_speed.right = rightMotor;
496 void RobomonAtlantis::setRightMotor(int value)
499 short int rightMotor;
501 if(bothMotorsCheckBox->isChecked())
502 leftMotorSlider->setValue(value);
504 leftMotor = (short int)(MOTOR_LIMIT * (leftMotorSlider->value()/100.0));
505 rightMotor = (short int)(MOTOR_LIMIT * (rightMotorSlider->value()/100.0));
507 orte.motion_speed.left = leftMotor;
508 orte.motion_speed.right = rightMotor;
512 void RobomonAtlantis::stopMotors()
514 leftMotorSlider->setValue(0);
515 rightMotorSlider->setValue(0);
519 void RobomonAtlantis::setDO(int state)
522 /* FIXME: digital output control comes here */
525 void RobomonAtlantis::pick()
527 // TODO: send signal to the fsmact
530 void RobomonAtlantis::setBelts(int value)
533 unsigned char leftBelt;
534 unsigned char rightBelt;
536 leftBelt = (unsigned char)leftBeltDial->value();
537 rightBelt = (unsigned char)rightBeltDial->value();
539 act_belts(leftBelt, rightBelt);
542 void RobomonAtlantis::setChelae(int value)
545 unsigned char leftChela;
546 unsigned char rightChela;
548 leftChela = (unsigned char)leftChelaDial->value();
549 rightChela = (unsigned char)rightChelaDial->value();
551 act_chelae(leftChela, rightChela);
554 void RobomonAtlantis::showMap()
558 if (sharedMemoryOpened == false)
561 mapTimer = new QTimer(this);
562 connect(mapTimer, SIGNAL(timeout()), this, SLOT(paintMap()));
563 mapTimer->start(200);
565 disconnect(showMapPushButton, SIGNAL(clicked()), this, SLOT(showMap()));
566 connect(showMapPushButton, SIGNAL(clicked()),
567 this, SLOT(showPlayground()));
568 playgroundScene->showMap(true);
571 void RobomonAtlantis::showPlayground()
574 disconnect(mapTimer, SIGNAL(timeout()), this, SLOT(paintMap()));
576 disconnect(showMapPushButton, SIGNAL(clicked()),
577 this, SLOT(showPlayground()));
578 connect(showMapPushButton, SIGNAL(clicked()), this, SLOT(showMap()));
579 playgroundScene->showMap(false);
582 void RobomonAtlantis::paintMap()
586 struct map *map = ShmapIsMapInit();
590 for(int i=0; i < MAP_WIDTH; i++) {
591 for(int j=0; j<MAP_HEIGHT; j++) {
594 struct map_cell *cell = &map->cells[j][i];
596 if (cell->flags & MAP_FLAG_WALL)
598 if (cell->flags & MAP_FLAG_IGNORE_OBST)
600 if (cell->flags & MAP_FLAG_SIMULATED_WALL)
602 if (cell->flags & MAP_FLAG_PATH)
604 if (cell->flags & MAP_FLAG_START)
606 if (cell->flags & MAP_FLAG_GOAL)
608 if (cell->detected_obstacle) {
609 QColor c1(color), c2(blue);
610 double f = (double)cell->detected_obstacle/MAP_NEW_OBSTACLE*0.7;
611 QColor c(c1.red() + (int)(f*(c2.red() - c1.red())),
612 c1.green() + (int)(f*(c2.green() - c1.green())),
613 c1.blue() + (int)(f*(c2.blue() - c1.blue())));
616 if (cell->flags & MAP_FLAG_DET_OBST)
619 playgroundScene->setMapColor(i, j, color);
624 void RobomonAtlantis::setSimulation(int state)
627 robottype_publisher_hokuyo_scan_create(&orte,
628 dummy_publisher_callback, this);
630 if (!simulationEnabled)
632 robottype_publisher_hokuyo_scan_destroy(&orte);
634 simulationEnabled = state;
638 \fn RobomonAtlantis::setObstacleSimulation(int state)
640 void RobomonAtlantis::setObstacleSimulation(int state)
643 /* TODO Maybe it is possible to attach only once to Shmap */
645 obstacleSimulationTimer = new QTimer(this);
646 connect(obstacleSimulationTimer, SIGNAL(timeout()),
647 this, SLOT(simulateObstaclesHokuyo()));
648 obstacleSimulationTimer->start(100);
649 setMouseTracking(true);
651 if (obstacleSimulationTimer)
652 delete obstacleSimulationTimer;
653 //double distance = 0.8;
658 void RobomonAtlantis::simulateObstaclesHokuyo()
660 double distance, wall_distance;
662 uint16_t *hokuyo = orte.hokuyo_scan.data;
664 for (i=0; i<HOKUYO_CLUSTER_CNT; i++) {
665 wall_distance = distanceToWallHokuyo(i);
666 distance = distanceToObstacleHokuyo(i, simulatedObstacle, SIM_OBST_SIZE_M/*meters*/);
667 if (wall_distance < distance)
668 distance = wall_distance;
669 hokuyo[i] = distance*1000;
671 ORTEPublicationSend(orte.publication_hokuyo_scan);
675 void RobomonAtlantis::changeObstacle(QPointF position)
677 if (!simulationEnabled) {
678 simulationEnabled = 1;
679 obstacleSimulationCheckBox->setChecked(true);
682 simulatedObstacle.x = position.x();
683 simulatedObstacle.y = position.y();
684 simulateObstaclesHokuyo();
687 /**********************************************************************
689 **********************************************************************/
690 bool RobomonAtlantis::event(QEvent *event)
692 switch (event->type()) {
693 case QEVENT(QEV_MOTION_STATUS):
694 emit motionStatusReceivedSignal();
696 case QEVENT(QEV_ACTUAL_POSITION):
697 emit actualPositionReceivedSignal();
699 case QEVENT(QEV_ESTIMATED_POSITION):
700 emit estimatedPositionReceivedSignal();
703 emit diReceivedSignal();
705 case QEVENT(QEV_ACCELEROMETER):
706 emit accelerometerReceivedSignal();
708 case QEVENT(QEV_ACCUMULATOR):
709 emit accumulatorReceivedSignal();
711 case QEVENT(QEV_POWER_VOLTAGE):
712 emit powerVoltageReceivedSignal();
714 case QEVENT(QEV_FSM_MAIN):
715 fsm_main_state->setText(orte.fsm_main.state_name);
717 case QEVENT(QEV_FSM_ACT):
718 fsm_act_state->setText(orte.fsm_act.state_name);
720 case QEVENT(QEV_FSM_MOTION):
721 fsm_motion_state->setText(orte.fsm_motion.state_name);
724 if (event->type() == QEvent::Close)
725 closeEvent((QCloseEvent *)event);
726 else if (event->type() == QEvent::KeyPress)
727 keyPressEvent((QKeyEvent *)event);
728 else if (event->type() == QEvent::KeyRelease)
729 keyReleaseEvent((QKeyEvent *)event);
730 else if (event->type() == QEvent::FocusIn)
732 else if (event->type() == QEvent::FocusOut)
744 void RobomonAtlantis::keyPressEvent(QKeyEvent *event)
748 if (event->isAutoRepeat()) {
749 switch (event->key()) {
751 peak = leftMotorSlider->minimum()/2;
752 if (leftMotorValue < peak ||
753 rightMotorValue < peak)
757 leftMotorValue *= gain;
758 rightMotorValue *= gain;
759 leftMotorSlider->setValue((int)leftMotorValue);
760 rightMotorSlider->setValue((int)rightMotorValue);
766 peak = leftMotorSlider->maximum()/2;
767 if (leftMotorValue > peak ||
768 rightMotorValue > peak)
772 leftMotorValue *= gain;
773 rightMotorValue *= gain;
774 leftMotorSlider->setValue((int)leftMotorValue);
775 rightMotorSlider->setValue((int)rightMotorValue);
785 switch (event->key()) {
789 bothMotorsCheckBox->setChecked(true);
790 leftMotorSlider->setValue((int)leftMotorValue);
791 setLeftMotor((int)leftMotorValue);
795 rightMotorValue = -1;
796 bothMotorsCheckBox->setChecked(true);
797 leftMotorSlider->setValue((int)leftMotorValue);
798 setLeftMotor((int)leftMotorValue);
803 leftMotorSlider->setValue((int)leftMotorValue);
804 rightMotorSlider->setValue((int)rightMotorValue);
805 setLeftMotor((int)leftMotorValue);
806 setRightMotor((int)leftMotorValue);
810 rightMotorValue = -1;
811 leftMotorSlider->setValue((int)leftMotorValue);
812 rightMotorSlider->setValue((int)rightMotorValue);
813 setLeftMotor((int)leftMotorValue);
814 setRightMotor((int)rightMotorValue);
823 void RobomonAtlantis::keyReleaseEvent(QKeyEvent *event)
825 if (event->isAutoRepeat()) {
830 switch (event->key()) {
837 bothMotorsCheckBox->setChecked(false);
838 leftMotorSlider->setValue((int)leftMotorValue);
839 rightMotorSlider->setValue((int)rightMotorValue);
848 void RobomonAtlantis::closeEvent(QCloseEvent *)
850 robottype_roboorte_destroy(&orte);
853 /**********************************************************************
855 **********************************************************************/
856 void RobomonAtlantis::createOrte()
862 rv = robottype_roboorte_init(&orte);
864 printf("RobomonAtlantis: Unable to initialize ORTE\n");
868 robottype_publisher_motion_speed_create(&orte, dummy_publisher_callback, NULL);
870 robottype_publisher_pwr_ctrl_create(&orte, dummy_publisher_callback, NULL);
871 robottype_publisher_lift_create(&orte, NULL, &orte);
872 robottype_publisher_pusher_create(&orte, NULL, &orte);
873 robottype_publisher_chelae_create(&orte, NULL, &orte);
874 robottype_publisher_belts_create(&orte, NULL, &orte);
875 robottype_publisher_holder_create(&orte, NULL, &orte);
878 robottype_subscriber_pwr_voltage_create(&orte,
879 receivePowerVoltageCallBack, this);
880 robottype_subscriber_motion_status_create(&orte,
881 receiveMotionStatusCallBack, this);
882 robottype_subscriber_ref_pos_create(&orte,
883 receiveActualPositionCallBack, this);
884 robottype_subscriber_est_pos_create(&orte,
885 receiveEstimatedPositionCallBack, this);
886 robottype_subscriber_fsm_main_create(&orte,
887 rcv_fsm_main_cb, this);
888 robottype_subscriber_fsm_motion_create(&orte,
889 rcv_fsm_motion_cb, this);
890 robottype_subscriber_fsm_act_create(&orte,
891 rcv_fsm_act_cb, this);
893 //robottype_subscriber_actuator_status_create(orte, receiveActuatorStatusCallBack, this);
896 /*createDISubscriber(this, &orteData);*/
897 /*createAccelerometerSubscriber(this, &orteData);*/
898 /*createAccumulatorSubscriber(this, &orteData);*/
900 orte.motion_speed.left = 0;
901 orte.motion_speed.right = 0;
902 /* power management */
903 orte.pwr_ctrl.voltage33 = true;
904 orte.pwr_ctrl.voltage50 = true;
905 orte.pwr_ctrl.voltage80 = true;
906 voltage33CheckBox->setChecked(true);
907 voltage50CheckBox->setChecked(true);
908 voltage80CheckBox->setChecked(true);
912 /* set actions to do when we receive data from orte */
913 connect(this, SIGNAL(motionStatusReceivedSignal()),
914 this, SLOT(motionStatusReceived()));
915 connect(this, SIGNAL(actualPositionReceivedSignal()),
916 this, SLOT(actualPositionReceived()));
917 connect(this, SIGNAL(estimatedPositionReceivedSignal()),
918 this, SLOT(estimatedPositionReceived()));
919 connect(this, SIGNAL(diReceivedSignal()),
920 this, SLOT(diReceived()));
921 connect(this, SIGNAL(accelerometerReceivedSignal()),
922 this, SLOT(accelerometerReceived()));
923 connect(this, SIGNAL(accumulatorReceivedSignal()),
924 this, SLOT(accumulatorReceived()));
925 connect(this, SIGNAL(powerVoltageReceivedSignal()),
926 this, SLOT(powerVoltageReceived()));
929 void RobomonAtlantis::motionStatusReceived()
931 WDBG("ORTE received: motion status");
934 void RobomonAtlantis::actualPositionReceived()
936 actPosX->setText(QString("%1").arg(orte.ref_pos.x, 0, 'f', 3));
937 actPosY->setText(QString("%1").arg(orte.ref_pos.y, 0, 'f', 3));
938 actPosPhi->setText(QString("%1(%2)")
939 .arg(DEGREES(orte.ref_pos.phi), 0, 'f', 0)
940 .arg(orte.ref_pos.phi, 0, 'f', 1));
941 robotActPos->moveRobot(orte.ref_pos.x,
942 orte.ref_pos.y, orte.ref_pos.phi);
945 void RobomonAtlantis::estimatedPositionReceived()
947 estPosX->setText(QString("%1").arg(orte.est_pos.x, 0, 'f', 3));
948 estPosY->setText(QString("%1").arg(orte.est_pos.y, 0, 'f', 3));
949 estPosPhi->setText(QString("%1(%2)")
950 .arg(DEGREES(orte.est_pos.phi), 0, 'f', 0)
951 .arg(orte.est_pos.phi, 0, 'f', 1));
952 robotEstPos->moveRobot(orte.est_pos.x,
953 orte.est_pos.y, orte.est_pos.phi);
956 void RobomonAtlantis::diReceived()
958 WDBG("ORTE received: DI");
961 void RobomonAtlantis::accelerometerReceived()
963 WDBG("ORTE received: accelerometer");
966 void RobomonAtlantis::accumulatorReceived()
968 WDBG("ORTE received: accumulator");
971 void RobomonAtlantis::powerVoltageReceived()
973 voltage33LineEdit->setText(QString("%1").arg(
974 orte.pwr_voltage.voltage33, 0, 'f', 3));
975 voltage50LineEdit->setText(QString("%1").arg(
976 orte.pwr_voltage.voltage50, 0, 'f', 3));
977 voltage80LineEdit->setText(QString("%1").arg(
978 orte.pwr_voltage.voltage80, 0, 'f', 3));
979 voltageBATLineEdit->setText(QString("%1").arg(
980 orte.pwr_voltage.voltageBAT, 0, 'f', 3));
984 /**********************************************************************
986 **********************************************************************/
987 void RobomonAtlantis::openSharedMemory()
990 int sharedSegmentSize;
992 if (sharedMemoryOpened)
995 sharedSegmentSize = sizeof(char) * MAP_WIDTH * MAP_HEIGHT;
997 /* Get segment identificator in a read only mode */
998 segmentId = shmget(SHM_MAP_KEY, sharedSegmentSize, S_IRUSR);
999 if(segmentId == -1) {
1000 QMessageBox::critical(this, "robomon",
1001 "Unable to open shared memory segment!");
1008 /* Attach the shared memory segment */
1009 //map = (_Map*)shmat (segmentId, (void*) 0, 0);
1011 sharedMemoryOpened = true;
1014 double RobomonAtlantis::distanceToWallHokuyo(int beamnum)
1016 double distance=4.0, min_distance=4.0;
1019 struct map *map = ShmapIsMapInit();
1021 if (!map) return min_distance;
1023 // Simulate obstacles
1024 for(j=0;j<MAP_HEIGHT;j++) {
1025 for (i=0;i<MAP_WIDTH;i++) {
1026 struct map_cell *cell = &map->cells[j][i];
1027 if( cell->flags & MAP_FLAG_SIMULATED_WALL) {
1029 ShmapCell2Point(i, j, &wall.x, &wall.y);
1031 distance = distanceToObstacleHokuyo(beamnum, wall, MAP_CELL_SIZE_M);
1032 if (distance<min_distance) min_distance = distance;
1037 return min_distance;
1041 * Calculation for Hokuyo simulation. Calculates distance that would
1042 * be returned by Hokuyo sensors, if there is only one obstacle (as
1043 * specified by parameters).
1045 * @param beamnum Hokuyo's bean number [0..HOKUYO_CLUSTER_CNT]
1046 * @param obstacle Position of the obstacle (x, y in meters).
1047 * @param obstacleSize Size (diameter) of the obstacle in meters.
1049 * @return Distance measured by sensors in meters.
1051 double RobomonAtlantis::distanceToObstacleHokuyo(int beamnum, Point obstacle, double obstacleSize)
1053 struct est_pos_type e = orte.est_pos;
1057 s.x = HOKUYO_CENTER_OFFSET_M;
1059 s.ang = HOKUYO_CLUSTER_TO_RAD(beamnum);
1061 Point sensor(e.x + s.x*cos(e.phi) - s.y*sin(e.phi),
1062 e.y + s.x*sin(e.phi) + s.y*cos(e.phi));
1063 sensor_a = e.phi + s.ang;
1065 const double sensorRange = 4.0; /*[meters]*/
1067 double distance, angle;
1069 angle = sensor.angleTo(obstacle) - sensor_a;
1070 angle = fmod(angle, 2.0*M_PI);
1071 if (angle > +M_PI) angle -= 2.0*M_PI;
1072 if (angle < -M_PI) angle += 2.0*M_PI;
1073 angle = fabs(angle);
1074 distance = sensor.distanceTo(obstacle)-0.11;
1075 if (angle < atan(obstacleSize/2.0 / (distance+0.001))) {
1076 // We can see the obstackle from here.
1077 if (angle < M_PI/2.0) {
1078 distance = distance/cos(angle);
1080 if (distance > sensorRange)
1081 distance = sensorRange;
1083 distance = sensorRange;