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"
39 #include <QCoreApplication>
43 #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();
83 leftLayout->addWidget(playgroundGroupBox);
84 leftLayout->addWidget(debugGroupBox);
87 void RobomonAtlantis::createRightLayout()
89 rightLayout = new QVBoxLayout();
90 QGridLayout *layout = new QGridLayout();
91 QVBoxLayout *vlayout = new QVBoxLayout();
93 createPositionGroupBox();
95 createActuatorsGroupBox();
97 createPowerGroupBox();
98 createSensorsGroupBox();
100 vlayout->addWidget(positionGroupBox);
101 vlayout->addWidget(miscGroupBox);
102 layout->addLayout(vlayout, 0, 0);
103 layout->addWidget(actuatorsGroupBox, 0, 1);
104 // layout->addWidget(dioGroupBox, 0, 2);
106 rightLayout->addLayout(layout);
107 rightLayout->addWidget(powerGroupBox);
108 rightLayout->addWidget(sensorsGroupBox);
111 void RobomonAtlantis::createPlaygroundGroupBox()
113 playgroundGroupBox = new QGroupBox(tr("Playground"));
114 QHBoxLayout *layout = new QHBoxLayout();
117 new PlaygroundScene(PlaygroundScene::PLAYGROUND_EUROBOT2008);
118 playgroundSceneView = new QGraphicsView(playgroundScene);
119 playgroundSceneView->setMinimumWidth((int)(playgroundScene->width())+15);
120 playgroundSceneView->setMinimumHeight((int)(playgroundScene->height())+15);
121 layout->addWidget(playgroundSceneView);
123 playgroundGroupBox->setLayout(layout);
126 void RobomonAtlantis::createPositionGroupBox()
128 positionGroupBox = new QGroupBox(tr("Position state"));
129 QGridLayout *layout = new QGridLayout();
131 actPosX = new QLineEdit();
132 actPosY = new QLineEdit();
133 actPosPhi = new QLineEdit();
135 estPosX = new QLineEdit();
136 estPosY = new QLineEdit();
137 estPosPhi = new QLineEdit();
139 actPosX->setReadOnly(true);
140 actPosY->setReadOnly(true);
141 actPosPhi->setReadOnly(true);
143 estPosX->setReadOnly(true);
144 estPosY->setReadOnly(true);
145 estPosPhi->setReadOnly(true);
147 layout->addWidget(MiscGui::createLabel("X"), 1, 0);
148 layout->addWidget(MiscGui::createLabel("Y"), 2, 0);
149 layout->addWidget(MiscGui::createLabel("Phi"), 3, 0);
151 layout->addWidget(MiscGui::createLabel("X"), 5, 0);
152 layout->addWidget(MiscGui::createLabel("Y"), 6, 0);
153 layout->addWidget(MiscGui::createLabel("Phi"), 7, 0);
155 layout->addWidget(MiscGui::createLabel("Actual", Qt::AlignLeft), 0, 1);
156 layout->addWidget(actPosX, 1, 1);
157 layout->addWidget(actPosY, 2, 1);
158 layout->addWidget(actPosPhi, 3, 1);
160 layout->addWidget(MiscGui::createLabel("Estimated", Qt::AlignLeft), 4, 1);
161 layout->addWidget(estPosX, 5, 1);
162 layout->addWidget(estPosY, 6, 1);
163 layout->addWidget(estPosPhi, 7, 1);
165 positionGroupBox->setLayout(layout);
168 void RobomonAtlantis::createMiscGroupBox()
170 miscGroupBox = new QGroupBox(tr("Miscellaneous"));
171 QGridLayout *layout = new QGridLayout();
173 showMapPushButton = new QPushButton(tr("Show &map"));
174 showMapPushButton->setShortcut(tr("m"));
176 laserEngineCheckBox = new QCheckBox(tr("L&aser"));
177 laserEngineCheckBox->setShortcut(tr("a"));
179 layout->addWidget(showMapPushButton, 0, 0);
180 layout->addWidget(laserEngineCheckBox, 1, 0);
182 miscGroupBox->setLayout(layout);
185 void RobomonAtlantis::createDebugGroupBox()
187 debugGroupBox = new QGroupBox(tr("Debug window"));
188 QHBoxLayout *layout = new QHBoxLayout();
190 debugWindow = new QTextEdit();
191 debugWindow->setReadOnly(true);
193 layout->addWidget(debugWindow);
194 debugGroupBox->setLayout(layout);
197 void RobomonAtlantis::createActuatorsGroupBox()
199 actuatorsGroupBox = new QGroupBox(tr("Actuators"));
200 QHBoxLayout *layout = new QHBoxLayout();
202 createMotorsGroupBox();
203 createServosGroupBox();
204 createDrivesGroupBox();
206 layout->setAlignment(Qt::AlignLeft);
207 layout->addWidget(enginesGroupBox);
208 layout->addWidget(servosGroupBox);
209 layout->addWidget(drivesGroupBox);
210 actuatorsGroupBox->setLayout(layout);
213 void RobomonAtlantis::createDIOGroupBox()
215 dioGroupBox = new QGroupBox(tr("DIO"));
216 QGridLayout *layout = new QGridLayout();
219 font.setPointSize(5);
220 dioGroupBox->setFont(font);
222 for (int i=0; i<8; i++) {
223 diCheckBox[i] = new QCheckBox(QString("DI%1").arg(i));
224 doCheckBox[i] = new QCheckBox(QString("D0%1").arg(i));
225 layout->addWidget(diCheckBox[i], i, 0);
226 layout->addWidget(doCheckBox[i], i+8, 0);
229 dioGroupBox->setMaximumWidth(70);
230 dioGroupBox->setLayout(layout);
233 void RobomonAtlantis::createPowerGroupBox()
235 powerGroupBox = new QGroupBox(tr("Power management"));
236 QGridLayout *layout = new QGridLayout();
238 voltage33CheckBox = new QCheckBox(tr("&3.3V"));
239 voltage50CheckBox = new QCheckBox(tr("&5.0V"));
240 voltage80CheckBox = new QCheckBox(tr("&8.0V"));
242 voltage33CheckBox->setShortcut(tr("3"));
243 voltage50CheckBox->setShortcut(tr("5"));
244 voltage80CheckBox->setShortcut(tr("8"));
246 layout->addWidget(voltage33CheckBox, 0, 0);
247 layout->addWidget(voltage50CheckBox, 0, 2);
248 layout->addWidget(voltage80CheckBox, 0, 4); //1, 0);
249 layout->addWidget(MiscGui::createLabel("BAT"), 0, 6); //1, 2);
251 voltage33LineEdit = new QLineEdit();
252 voltage50LineEdit = new QLineEdit();
253 voltage80LineEdit = new QLineEdit();
254 voltageBATLineEdit = new QLineEdit();
256 voltage33LineEdit->setReadOnly(true);
257 voltage50LineEdit->setReadOnly(true);
258 voltage80LineEdit->setReadOnly(true);
259 voltageBATLineEdit->setReadOnly(true);
261 layout->addWidget(voltage33LineEdit, 0, 1);
262 layout->addWidget(voltage50LineEdit, 0, 3);
263 layout->addWidget(voltage80LineEdit, 0, 5); //1, 1);
264 layout->addWidget(voltageBATLineEdit, 0, 7); //1, 3);
266 powerGroupBox->setLayout(layout);
269 void RobomonAtlantis::createSensorsGroupBox()
271 sensorsGroupBox = new QGroupBox(tr("Sensors"));
272 QVBoxLayout *layout = new QVBoxLayout();
274 createSharpSensorsLayout();
276 layout->addLayout(sharpSensorsLayout);
277 sensorsGroupBox->setLayout(layout);
280 void RobomonAtlantis::createMotorsGroupBox()
282 enginesGroupBox = new QGroupBox(tr("Motors"));
283 QVBoxLayout *layout = new QVBoxLayout();
284 QHBoxLayout *layout1 = new QHBoxLayout();
285 QHBoxLayout *layout2 = new QHBoxLayout();
287 leftMotorSlider = new QSlider(Qt::Vertical);
288 rightMotorSlider = new QSlider(Qt::Vertical);
289 bothMotorsCheckBox = new QCheckBox(tr("Lock both"));
290 stopMotorsPushButton = new QPushButton(tr("Stop Motors"));
292 leftMotorSlider->setMinimum(-100);
293 leftMotorSlider->setMaximum(100);
294 leftMotorSlider->setTracking(false);
295 leftMotorSlider->setTickPosition(QSlider::TicksLeft);
297 rightMotorSlider->setMinimum(-100);
298 rightMotorSlider->setMaximum(100);
299 rightMotorSlider->setTracking(false);
300 rightMotorSlider->setTickPosition(QSlider::TicksRight);
302 stopMotorsPushButton->setMaximumWidth(90);
304 layout1->addWidget(leftMotorSlider);
305 layout1->addWidget(MiscGui::createLabel("0"));
306 layout1->addWidget(rightMotorSlider);
308 layout2->addWidget(bothMotorsCheckBox);
310 layout->addWidget(MiscGui::createLabel("100"));
311 layout->addLayout(layout1);
312 layout->addWidget(MiscGui::createLabel("-100"));
313 layout->addLayout(layout2);
314 layout->addWidget(stopMotorsPushButton);
315 enginesGroupBox->setLayout(layout);
318 void RobomonAtlantis::createServosGroupBox()
320 servosGroupBox = new QGroupBox(tr("Servos"));
321 QGridLayout *layout = new QGridLayout();
323 leftBrushCheckBox = new QCheckBox(tr("&Left Brush"));
324 rightBrushCheckBox = new QCheckBox(tr("&Right Brush"));
325 pusherCheckBox = new QCheckBox(tr("&Pusher"));
327 leftBrushCheckBox->setShortcut(tr("l"));
328 rightBrushCheckBox->setShortcut(tr("r"));
329 pusherCheckBox->setShortcut(tr("p"));
331 layout->addWidget(leftBrushCheckBox, 0, 0);
332 layout->addWidget(rightBrushCheckBox, 1, 0);
333 layout->addWidget(pusherCheckBox, 2, 0);
334 servosGroupBox->setLayout(layout);
338 void RobomonAtlantis::createDrivesGroupBox()
340 drivesGroupBox = new QGroupBox(tr("Drives"));
341 QGridLayout *layout = new QGridLayout();
343 leftBrushDial = new QDial();
344 rightBrushDial = new QDial();
345 lift_posDial = new QDial();
346 pusherDial = new QDial();
348 leftBrushDriveCheckBox = new QCheckBox(tr("L&eft brush"));
349 rightBrushDriveCheckBox = new QCheckBox(tr("Ri&ght brush"));
350 lift_posDriveCheckBox = new QCheckBox(tr("B&agr"));
352 leftBrushDial->setRange(0,200);
353 leftBrushDial->setValue(100);
354 rightBrushDial->setRange(0,200);
355 rightBrushDial->setValue(100);
356 lift_posDial->setRange(0,200);
357 lift_posDial->setValue(100);
358 pusherDial->setRange(0,4);
359 pusherDial->setNotchesVisible(1);
361 layout->addWidget(leftBrushDial, 0, 0);
362 layout->addWidget(leftBrushDriveCheckBox, 0, 1);
363 layout->addWidget(rightBrushDial, 1, 0);
364 layout->addWidget(rightBrushDriveCheckBox, 1, 1);
365 layout->addWidget(lift_posDial, 2, 0);
366 layout->addWidget(lift_posDriveCheckBox, 2, 1);
367 layout->addWidget(pusherDial, 3, 0);
368 drivesGroupBox->setLayout(layout);
371 void RobomonAtlantis::createSharpSensorsLayout()
373 sharpSensorsLayout = new QVBoxLayout();
374 QGridLayout *layout3 = new QGridLayout();
376 obstacleSimulationCheckBox = new QCheckBox(tr("&Obstacle simulation"));
378 obstacleSimulationCheckBox->setShortcut(tr("o"));
380 layout3->addWidget(obstacleSimulationCheckBox, 0, 1);
382 sharpSensorsLayout->addLayout(layout3);
385 void RobomonAtlantis::createRobots()
387 robotActPos = new Robot(NULL, playgroundScene, Qt::black);
388 robotActPos->setZValue(10);
389 robotEstPos = new Robot(NULL, playgroundScene, Qt::green);
390 robotEstPos->setZValue(10);
392 playgroundScene->addItem(robotActPos);
393 playgroundScene->addItem(robotEstPos);
396 /**********************************************************************
398 **********************************************************************/
399 void RobomonAtlantis::createActions()
401 /* power management */
402 connect(voltage33CheckBox, SIGNAL(stateChanged(int)),
403 this, SLOT(setVoltage33(int)));
404 connect(voltage50CheckBox, SIGNAL(stateChanged(int)),
405 this, SLOT(setVoltage50(int)));
406 connect(voltage80CheckBox, SIGNAL(stateChanged(int)),
407 this, SLOT(setVoltage80(int)));
410 connect(leftMotorSlider, SIGNAL(valueChanged(int)),
411 this, SLOT(setLeftMotor(int)));
412 connect(rightMotorSlider, SIGNAL(valueChanged(int)),
413 this, SLOT(setRightMotor(int)));
414 connect(stopMotorsPushButton, SIGNAL(clicked()),
415 this, SLOT(stopMotors()));
418 connect(leftBrushCheckBox, SIGNAL(stateChanged(int)),
419 this, SLOT(moveLeftBrush(int)));
420 connect(rightBrushCheckBox, SIGNAL(stateChanged(int)),
421 this, SLOT(moveRightBrush(int)));
424 connect(leftBrushDial, SIGNAL(valueChanged(int)),
425 this, SLOT(setDrives(int)));
426 connect(rightBrushDial, SIGNAL(valueChanged(int)),
427 this, SLOT(setDrives(int)));
428 connect(lift_posDial, SIGNAL(valueChanged(int)),
429 this, SLOT(setDrives(int)));
430 connect(pusherDial, SIGNAL(valueChanged(int)),
431 this, SLOT(setDrives(int)));
432 connect(leftBrushDriveCheckBox, SIGNAL(stateChanged(int)),
433 this, SLOT(setLeftBrushDriveCB(int)));
434 connect(rightBrushDriveCheckBox, SIGNAL(stateChanged(int)),
435 this, SLOT(setRightBrushDriveCB(int)));
436 connect(lift_posDriveCheckBox, SIGNAL(stateChanged(int)),
437 this, SLOT(setlift_posDriveCB(int)));
439 for (int i=0; i<8; i++)
440 connect(doCheckBox[0], SIGNAL(stateChanged(int)),
441 this, SLOT(setDO(int)));
444 connect(laserEngineCheckBox, SIGNAL(stateChanged(int)),
445 this, SLOT(setLaser(int)));
447 /* path planning map */
448 connect(showMapPushButton, SIGNAL(clicked()),
449 this, SLOT(showMap()));
451 /* obstacle simulation */
452 simulationEnabled = 0;
453 connect(obstacleSimulationCheckBox, SIGNAL(stateChanged(int)),
454 this, SLOT(setSimulation(int)));
455 connect(obstacleSimulationCheckBox, SIGNAL(stateChanged(int)),
456 this, SLOT(setObstacleSimulation(int)));
457 connect(obstacleSimulationCheckBox, SIGNAL(stateChanged(int)),
458 playgroundScene, SLOT(showObstacle(int)));
459 connect(playgroundScene, SIGNAL(obstacleChanged(QPointF)),
460 this, SLOT(changeObstacle(QPointF)));
463 void RobomonAtlantis::setVoltage33(int state)
466 orte.pwr_ctrl.voltage33 = true;
468 orte.pwr_ctrl.voltage33 = false;
471 void RobomonAtlantis::setVoltage50(int state)
474 orte.pwr_ctrl.voltage50 = true;
476 orte.pwr_ctrl.voltage50 = false;
479 void RobomonAtlantis::setVoltage80(int state)
482 orte.pwr_ctrl.voltage80 = true;
484 orte.pwr_ctrl.voltage80 = false;
487 void RobomonAtlantis::setLeftMotor(int value)
490 short int rightMotor;
492 if(bothMotorsCheckBox->isChecked())
493 rightMotorSlider->setValue(value);
495 leftMotor = (short int)(MOTOR_LIMIT * (leftMotorSlider->value()/100.0));
496 rightMotor = (short int)(MOTOR_LIMIT * (rightMotorSlider->value()/100.0));
498 orte.motion_speed.left = leftMotor;
499 orte.motion_speed.right = rightMotor;
503 void RobomonAtlantis::setRightMotor(int value)
506 short int rightMotor;
508 if(bothMotorsCheckBox->isChecked())
509 leftMotorSlider->setValue(value);
511 leftMotor = (short int)(MOTOR_LIMIT * (leftMotorSlider->value()/100.0));
512 rightMotor = (short int)(MOTOR_LIMIT * (rightMotorSlider->value()/100.0));
514 orte.motion_speed.left = leftMotor;
515 orte.motion_speed.right = rightMotor;
519 void RobomonAtlantis::stopMotors()
521 leftMotorSlider->setValue(0);
522 rightMotorSlider->setValue(0);
525 void RobomonAtlantis::moveServos(int value)
527 /* FIXME: servos action comes here */
530 void RobomonAtlantis::moveLeftBrush(int state)
533 actlib_servos_brush_left_open();
535 actlib_servos_brush_left_close();
538 void RobomonAtlantis::moveRightBrush(int state)
541 actlib_servos_brush_right_open();
543 actlib_servos_brush_right_close();
546 void RobomonAtlantis::setDrives(int state)
548 orte.drives.brush_left = leftBrushDial->value();
549 orte.drives.brush_right = rightBrushDial->value();
550 orte.drives.lift_pos = lift_posDial->value();
551 orte.drives.pusher = pusherDial->value();
554 void RobomonAtlantis::setLeftBrushDriveCB(int state) {
557 actlib_drives_brush_left_on();
559 actlib_drives_brush_left_off();
563 void RobomonAtlantis::setRightBrushDriveCB(int state) {
566 actlib_drives_brush_right_on();
568 actlib_drives_brush_right_off();
572 void RobomonAtlantis::setlift_posDriveCB(int state) {
575 orte.drives.lift_pos = 1;
577 orte.drives.lift_pos = 2;
580 void RobomonAtlantis::setDO(int state)
582 /* FIXME: digital output control comes here */
585 void RobomonAtlantis::setLaser(int state) {
593 void RobomonAtlantis::showMap()
597 if (sharedMemoryOpened == false)
600 mapTimer = new QTimer(this);
601 connect(mapTimer, SIGNAL(timeout()), this, SLOT(paintMap()));
602 mapTimer->start(200);
604 showMapPushButton->setText(tr("Show Playground"));
605 showMapPushButton->setShortcut(tr("p"));
606 disconnect(showMapPushButton, SIGNAL(clicked()), this, SLOT(showMap()));
607 connect(showMapPushButton, SIGNAL(clicked()),
608 this, SLOT(showPlayground()));
611 void RobomonAtlantis::showPlayground()
614 disconnect(mapTimer, SIGNAL(timeout()), this, SLOT(paintMap()));
618 showMapPushButton->setText(tr("Show &map"));
619 showMapPushButton->setShortcut(tr("m"));
620 disconnect(showMapPushButton, SIGNAL(clicked()),
621 this, SLOT(showPlayground()));
622 connect(showMapPushButton, SIGNAL(clicked()), this, SLOT(showMap()));
625 void RobomonAtlantis::paintMap()
628 static QGraphicsRectItem *rects[MAP_WIDTH][MAP_HEIGHT];
629 static bool firstMap = true;
631 struct map *map = ShmapIsMapInit();
635 for(int i=0; i < MAP_WIDTH; i++) {
636 for(int j=0; j<MAP_HEIGHT; j++) {
637 x = TOP_LEFT_X+i*cellSize;
638 y = TOP_LEFT_Y+j*cellSize;
641 rects[i][j] = playgroundScene->addRect(
642 QRectF(x,y,cellSize,cellSize),
643 QPen(QBrush(Qt::black),
648 QBrush(Qt::lightGray));
649 rects[i][j]->setZValue(4);
654 struct map_cell *cell = &map->cells[j][i];
656 if (cell->flags & MAP_FLAG_WALL)
658 if (cell->flags & MAP_FLAG_IGNORE_OBST)
660 if (cell->flags & MAP_FLAG_SIMULATED_WALL)
662 if (cell->flags & MAP_FLAG_PATH)
664 if (cell->flags & MAP_FLAG_START)
666 if (cell->flags & MAP_FLAG_GOAL)
668 if (cell->detected_obstacle) {
669 QColor c1(color), c2(blue);
670 double f = (double)cell->detected_obstacle/MAP_NEW_OBSTACLE*0.7;
671 QColor c(c1.red() + (int)(f*(c2.red() - c1.red())),
672 c1.green() + (int)(f*(c2.green() - c1.green())),
673 c1.blue() + (int)(f*(c2.blue() - c1.blue())));
676 if (cell->flags & MAP_FLAG_DET_OBST)
679 rects[i][j]->setBrush(QBrush(color));
685 void RobomonAtlantis::setSimulation(int state)
688 robottype_publisher_hokuyo_scan_create(&orte,
689 dummy_publisher_callback, this);
691 if (!simulationEnabled)
693 robottype_publisher_hokuyo_scan_destroy(&orte);
695 simulationEnabled = state;
699 \fn RobomonAtlantis::setObstacleSimulation(int state)
701 void RobomonAtlantis::setObstacleSimulation(int state)
704 /* TODO Maybe it is possible to attach only once to Shmap */
706 obstacleSimulationTimer = new QTimer(this);
707 connect(obstacleSimulationTimer, SIGNAL(timeout()),
708 this, SLOT(simulateObstaclesHokuyo()));
709 obstacleSimulationTimer->start(100);
710 setMouseTracking(true);
712 if (obstacleSimulationTimer)
713 delete obstacleSimulationTimer;
714 double distance = 0.8;
719 void RobomonAtlantis::simulateObstaclesHokuyo()
721 double distance, wall_distance;
723 uint16_t *hokuyo[34] = { // FIXME should be HOKUYO_CLUSTER_CNT
724 &orte.hokuyo_scan.data1,
725 &orte.hokuyo_scan.data2,
726 &orte.hokuyo_scan.data3,
727 &orte.hokuyo_scan.data4,
728 &orte.hokuyo_scan.data5,
729 &orte.hokuyo_scan.data6,
730 &orte.hokuyo_scan.data7,
731 &orte.hokuyo_scan.data8,
732 &orte.hokuyo_scan.data9,
733 &orte.hokuyo_scan.data10,
734 &orte.hokuyo_scan.data11,
735 &orte.hokuyo_scan.data12,
736 &orte.hokuyo_scan.data13,
737 &orte.hokuyo_scan.data14,
738 &orte.hokuyo_scan.data15,
739 &orte.hokuyo_scan.data16,
740 &orte.hokuyo_scan.data17,
741 &orte.hokuyo_scan.data18,
742 &orte.hokuyo_scan.data19,
743 &orte.hokuyo_scan.data20,
744 &orte.hokuyo_scan.data21,
745 &orte.hokuyo_scan.data22,
746 &orte.hokuyo_scan.data23,
747 &orte.hokuyo_scan.data24,
748 &orte.hokuyo_scan.data25,
749 &orte.hokuyo_scan.data26,
750 &orte.hokuyo_scan.data27,
751 &orte.hokuyo_scan.data28,
752 &orte.hokuyo_scan.data29,
753 &orte.hokuyo_scan.data30,
754 &orte.hokuyo_scan.data31,
755 &orte.hokuyo_scan.data32,
756 &orte.hokuyo_scan.data33,
757 &orte.hokuyo_scan.data34,
761 for (i=0; i<HOKUYO_CLUSTER_CNT; i++) {
762 wall_distance = distanceToWallHokuyo(i);
763 distance = distanceToObstacleHokuyo(i, simulatedObstacle, SIM_OBST_SIZE_M/*meters*/);
764 if (wall_distance < distance)
765 distance = wall_distance;
766 *hokuyo[i] = (uint16_t)(distance*1000);
768 ORTEPublicationSend(orte.publication_hokuyo_scan);
772 void RobomonAtlantis::changeObstacle(QPointF position)
774 if (!simulationEnabled) {
775 simulationEnabled = 1;
776 obstacleSimulationCheckBox->setChecked(true);
779 simulatedObstacle.x = position.x();
780 simulatedObstacle.y = position.y();
781 simulateObstaclesHokuyo();
784 /**********************************************************************
786 **********************************************************************/
787 bool RobomonAtlantis::event(QEvent *event)
789 switch (event->type()) {
790 case QEVENT(QEV_MOTION_STATUS):
791 emit motionStatusReceivedSignal();
793 case QEVENT(QEV_ACTUAL_POSITION):
794 emit actualPositionReceivedSignal();
796 case QEVENT(QEV_ESTIMATED_POSITION):
797 emit estimatedPositionReceivedSignal();
800 emit diReceivedSignal();
802 case QEVENT(QEV_ACCELEROMETER):
803 emit accelerometerReceivedSignal();
805 case QEVENT(QEV_ACCUMULATOR):
806 emit accumulatorReceivedSignal();
808 case QEVENT(QEV_POWER_VOLTAGE):
809 emit powerVoltageReceivedSignal();
812 if (event->type() == QEvent::Close)
813 closeEvent((QCloseEvent *)event);
814 else if (event->type() == QEvent::KeyPress)
815 keyPressEvent((QKeyEvent *)event);
816 else if (event->type() == QEvent::KeyRelease)
817 keyReleaseEvent((QKeyEvent *)event);
818 else if (event->type() == QEvent::FocusIn)
820 else if (event->type() == QEvent::FocusOut)
832 void RobomonAtlantis::keyPressEvent(QKeyEvent *event)
836 if (event->isAutoRepeat()) {
837 switch (event->key()) {
839 peak = leftMotorSlider->minimum()/2;
840 if (leftMotorValue < peak ||
841 rightMotorValue < peak)
845 leftMotorValue *= gain;
846 rightMotorValue *= gain;
847 leftMotorSlider->setValue((int)leftMotorValue);
848 rightMotorSlider->setValue((int)rightMotorValue);
854 peak = leftMotorSlider->maximum()/2;
855 if (leftMotorValue > peak ||
856 rightMotorValue > peak)
860 leftMotorValue *= gain;
861 rightMotorValue *= gain;
862 leftMotorSlider->setValue((int)leftMotorValue);
863 rightMotorSlider->setValue((int)rightMotorValue);
873 switch (event->key()) {
877 bothMotorsCheckBox->setChecked(true);
878 leftMotorSlider->setValue((int)leftMotorValue);
879 setLeftMotor((int)leftMotorValue);
883 rightMotorValue = -1;
884 bothMotorsCheckBox->setChecked(true);
885 leftMotorSlider->setValue((int)leftMotorValue);
886 setLeftMotor((int)leftMotorValue);
891 leftMotorSlider->setValue((int)leftMotorValue);
892 rightMotorSlider->setValue((int)rightMotorValue);
893 setLeftMotor((int)leftMotorValue);
894 setRightMotor((int)leftMotorValue);
898 rightMotorValue = -1;
899 leftMotorSlider->setValue((int)leftMotorValue);
900 rightMotorSlider->setValue((int)rightMotorValue);
901 setLeftMotor((int)leftMotorValue);
902 setRightMotor((int)rightMotorValue);
911 void RobomonAtlantis::keyReleaseEvent(QKeyEvent *event)
913 if (event->isAutoRepeat()) {
918 switch (event->key()) {
925 bothMotorsCheckBox->setChecked(false);
926 leftMotorSlider->setValue((int)leftMotorValue);
927 rightMotorSlider->setValue((int)rightMotorValue);
936 void RobomonAtlantis::closeEvent(QCloseEvent *event)
938 robottype_roboorte_destroy(&orte);
941 /**********************************************************************
943 **********************************************************************/
944 void RobomonAtlantis::createOrte()
950 rv = robottype_roboorte_init(&orte);
952 printf("RobomonAtlantis: Unable to initialize ORTE\n");
956 robottype_publisher_motion_speed_create(&orte, dummy_publisher_callback, NULL);
958 robottype_publisher_pwr_ctrl_create(&orte, dummy_publisher_callback, NULL);
960 robottype_publisher_servos_create(&orte, dummy_publisher_callback, NULL);
961 robottype_publisher_drives_create(&orte, dummy_publisher_callback, NULL);
962 robottype_publisher_laser_cmd_create(&orte, NULL, NULL);
965 robottype_subscriber_pwr_voltage_create(&orte,
966 receivePowerVoltageCallBack, this);
967 robottype_subscriber_motion_status_create(&orte,
968 receiveMotionStatusCallBack, this);
969 robottype_subscriber_ref_pos_create(&orte,
970 receiveActualPositionCallBack, this);
971 robottype_subscriber_est_pos_create(&orte,
972 receiveEstimatedPositionCallBack, this);
974 /*createDISubscriber(this, &orteData);*/
975 /*createAccelerometerSubscriber(this, &orteData);*/
976 /*createAccumulatorSubscriber(this, &orteData);*/
978 orte.motion_speed.left = 0;
979 orte.motion_speed.right = 0;
980 /* power management */
981 orte.pwr_ctrl.voltage33 = true;
982 orte.pwr_ctrl.voltage50 = true;
983 orte.pwr_ctrl.voltage80 = true;
984 voltage33CheckBox->setChecked(true);
985 voltage50CheckBox->setChecked(true);
986 voltage80CheckBox->setChecked(true);
989 leftBrushCheckBox->setChecked(false);
990 rightBrushCheckBox->setChecked(false);
997 /* set actions to do when we receive data from orte */
998 connect(this, SIGNAL(motionStatusReceivedSignal()),
999 this, SLOT(motionStatusReceived()));
1000 connect(this, SIGNAL(actualPositionReceivedSignal()),
1001 this, SLOT(actualPositionReceived()));
1002 connect(this, SIGNAL(estimatedPositionReceivedSignal()),
1003 this, SLOT(estimatedPositionReceived()));
1004 connect(this, SIGNAL(diReceivedSignal()),
1005 this, SLOT(diReceived()));
1006 connect(this, SIGNAL(accelerometerReceivedSignal()),
1007 this, SLOT(accelerometerReceived()));
1008 connect(this, SIGNAL(accumulatorReceivedSignal()),
1009 this, SLOT(accumulatorReceived()));
1010 connect(this, SIGNAL(powerVoltageReceivedSignal()),
1011 this, SLOT(powerVoltageReceived()));
1014 void RobomonAtlantis::motionStatusReceived()
1016 WDBG("ORTE received: motion status");
1019 void RobomonAtlantis::actualPositionReceived()
1021 actPosX->setText(QString("%1").arg(orte.ref_pos.x, 0, 'f', 3));
1022 actPosY->setText(QString("%1").arg(orte.ref_pos.y, 0, 'f', 3));
1023 actPosPhi->setText(QString("%1(%2)")
1024 .arg(DEGREES(orte.ref_pos.phi), 0, 'f', 0)
1025 .arg(orte.ref_pos.phi, 0, 'f', 1));
1026 robotActPos->moveRobot(orte.ref_pos.x,
1027 orte.ref_pos.y, orte.ref_pos.phi);
1030 void RobomonAtlantis::estimatedPositionReceived()
1032 estPosX->setText(QString("%1").arg(orte.est_pos.x, 0, 'f', 3));
1033 estPosY->setText(QString("%1").arg(orte.est_pos.y, 0, 'f', 3));
1034 estPosPhi->setText(QString("%1(%2)")
1035 .arg(DEGREES(orte.est_pos.phi), 0, 'f', 0)
1036 .arg(orte.est_pos.phi, 0, 'f', 1));
1037 robotEstPos->moveRobot(orte.est_pos.x,
1038 orte.est_pos.y, orte.est_pos.phi);
1041 void RobomonAtlantis::diReceived()
1043 WDBG("ORTE received: DI");
1046 void RobomonAtlantis::accelerometerReceived()
1048 WDBG("ORTE received: accelerometer");
1051 void RobomonAtlantis::accumulatorReceived()
1053 WDBG("ORTE received: accumulator");
1056 void RobomonAtlantis::powerVoltageReceived()
1058 voltage33LineEdit->setText(QString("%1").arg(
1059 orte.pwr_voltage.voltage33, 0, 'f', 3));
1060 voltage50LineEdit->setText(QString("%1").arg(
1061 orte.pwr_voltage.voltage50, 0, 'f', 3));
1062 voltage80LineEdit->setText(QString("%1").arg(
1063 orte.pwr_voltage.voltage80, 0, 'f', 3));
1064 voltageBATLineEdit->setText(QString("%1").arg(
1065 orte.pwr_voltage.voltageBAT, 0, 'f', 3));
1069 /**********************************************************************
1071 **********************************************************************/
1072 void RobomonAtlantis::openSharedMemory()
1075 int sharedSegmentSize;
1077 if (sharedMemoryOpened)
1080 cellSize = PG_X / MAP_WIDTH;
1082 sharedSegmentSize = sizeof(char) * MAP_WIDTH * MAP_HEIGHT;
1084 /* Get segment identificator in a read only mode */
1085 segmentId = shmget(SHM_MAP_KEY, sharedSegmentSize, S_IRUSR);
1086 if(segmentId == -1) {
1087 QMessageBox::critical(this, "robomon",
1088 "Unable to open shared memory segment!");
1095 /* Attach the shared memory segment */
1096 //map = (_Map*)shmat (segmentId, (void*) 0, 0);
1098 sharedMemoryOpened = true;
1101 double RobomonAtlantis::distanceToWallHokuyo(int beamnum)
1103 double distance=4.0, min_distance=4.0;
1106 struct map *map = ShmapIsMapInit();
1108 if (!map) return min_distance;
1110 // Simulate obstacles
1111 for(j=0;j<MAP_HEIGHT;j++) {
1112 for (i=0;i<MAP_WIDTH;i++) {
1113 struct map_cell *cell = &map->cells[j][i];
1114 if( cell->flags & MAP_FLAG_SIMULATED_WALL) {
1116 ShmapCell2Point(i, j, &wall.x, &wall.y);
1118 distance = distanceToObstacleHokuyo(beamnum, wall, MAP_CELL_SIZE_M);
1119 if (distance<min_distance) min_distance = distance;
1124 return min_distance;
1128 * Calculation for Hokuyo simulation. Calculates distance that would
1129 * be returned by Hokuyo sensors, if there is only one obstacle (as
1130 * specified by parameters).
1132 * @param beamnum Hokuyo's bean number [0..HOKUYO_CLUSTER_CNT]
1133 * @param obstacle Position of the obstacle (x, y in meters).
1134 * @param obstacleSize Size (diameter) of the obstacle in meters.
1136 * @return Distance measured by sensors in meters.
1138 double RobomonAtlantis::distanceToObstacleHokuyo(int beamnum, Point obstacle, double obstacleSize)
1140 struct est_pos_type e = orte.est_pos;
1144 s.x = HOKUYO_CENTER_OFFSET_M;
1146 s.ang = HOKUYO_CLUSTER_TO_RAD(beamnum);
1148 Point sensor(e.x + s.x*cos(e.phi) - s.y*sin(e.phi),
1149 e.y + s.x*sin(e.phi) + s.y*cos(e.phi));
1150 sensor_a = e.phi + s.ang;
1152 const double sensorRange = 4.0; /*[meters]*/
1154 double distance, angle;
1156 angle = sensor.angleTo(obstacle) - sensor_a;
1157 angle = fmod(angle, 2.0*M_PI);
1158 if (angle > +M_PI) angle -= 2.0*M_PI;
1159 if (angle < -M_PI) angle += 2.0*M_PI;
1160 angle = fabs(angle);
1161 distance = sensor.distanceTo(obstacle)-0.11;
1162 if (angle < atan(obstacleSize/2.0 / (distance+0.001))) {
1163 // We can see the obstackle from here.
1164 if (angle < M_PI/2.0) {
1165 distance = distance/cos(angle);
1167 if (distance > sensorRange)
1168 distance = sensorRange;
1170 distance = sensorRange;