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);
69 setFocusPolicy(Qt::StrongFocus);
70 sharedMemoryOpened = false;
71 WDBG("Youuuhouuuu!!");
74 /**********************************************************************
76 **********************************************************************/
77 void RobomonAtlantis::createLeftLayout()
79 leftLayout = new QVBoxLayout();
81 createDebugGroupBox();
82 debugWindowEnabled = true;
83 createPlaygroundGroupBox();
84 leftLayout->addWidget(playgroundGroupBox);
85 leftLayout->addWidget(debugGroupBox);
88 void RobomonAtlantis::createRightLayout()
90 rightLayout = new QVBoxLayout();
91 QGridLayout *layout = new QGridLayout();
92 QVBoxLayout *vlayout = new QVBoxLayout();
94 createPositionGroupBox();
97 createActuatorsGroupBox();
99 createPowerGroupBox();
100 createSensorsGroupBox();
102 vlayout->addWidget(positionGroupBox);
103 vlayout->addWidget(miscGroupBox);
104 vlayout->addWidget(fsmGroupBox);
105 layout->addLayout(vlayout, 0, 0);
106 layout->addWidget(actuatorsGroupBox, 0, 1);
107 // layout->addWidget(dioGroupBox, 0, 2);
109 rightLayout->addLayout(layout);
110 rightLayout->addWidget(powerGroupBox);
111 rightLayout->addWidget(sensorsGroupBox);
114 void RobomonAtlantis::createPlaygroundGroupBox()
116 playgroundGroupBox = new QGroupBox(tr("Playground"));
117 QHBoxLayout *layout = new QHBoxLayout();
119 playgroundScene = new PlaygroundScene();
120 playgroundSceneView = new PlaygroundView(playgroundScene);
121 //playgroundSceneView->setMinimumWidth(630);
122 //playgroundSceneView->setMinimumHeight(445);
123 playgroundSceneView->setMatrix(QMatrix(1,0,0,-1,0,0), true);
124 playgroundSceneView->fitInView(playgroundScene->itemsBoundingRect());
125 playgroundSceneView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
126 layout->addWidget(playgroundSceneView);
128 playgroundGroupBox->setLayout(layout);
131 void RobomonAtlantis::createPositionGroupBox()
133 positionGroupBox = new QGroupBox(tr("Position state"));
134 positionGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
135 QGridLayout *layout = new QGridLayout();
137 actPosX = new QLineEdit();
138 actPosY = new QLineEdit();
139 actPosPhi = new QLineEdit();
141 estPosX = new QLineEdit();
142 estPosY = new QLineEdit();
143 estPosPhi = new QLineEdit();
145 actPosX->setReadOnly(true);
146 actPosY->setReadOnly(true);
147 actPosPhi->setReadOnly(true);
149 estPosX->setReadOnly(true);
150 estPosY->setReadOnly(true);
151 estPosPhi->setReadOnly(true);
153 layout->addWidget(MiscGui::createLabel("X"), 1, 0);
154 layout->addWidget(MiscGui::createLabel("Y"), 2, 0);
155 layout->addWidget(MiscGui::createLabel("Phi"), 3, 0);
157 layout->addWidget(MiscGui::createLabel("X"), 5, 0);
158 layout->addWidget(MiscGui::createLabel("Y"), 6, 0);
159 layout->addWidget(MiscGui::createLabel("Phi"), 7, 0);
161 layout->addWidget(MiscGui::createLabel("Reference", Qt::AlignLeft), 0, 1);
162 layout->addWidget(actPosX, 1, 1);
163 layout->addWidget(actPosY, 2, 1);
164 layout->addWidget(actPosPhi, 3, 1);
166 layout->addWidget(MiscGui::createLabel("Estimated", Qt::AlignLeft), 4, 1);
167 layout->addWidget(estPosX, 5, 1);
168 layout->addWidget(estPosY, 6, 1);
169 layout->addWidget(estPosPhi, 7, 1);
171 positionGroupBox->setLayout(layout);
174 void RobomonAtlantis::createMiscGroupBox()
176 miscGroupBox = new QGroupBox(tr("Miscellaneous"));
177 miscGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
178 QGridLayout *layout = new QGridLayout();
180 showMapPushButton = new QCheckBox(tr("Show &map"));
181 showMapPushButton->setShortcut(tr("m"));
183 layout->addWidget(showMapPushButton, 0, 0);
185 obstacleSimulationCheckBox = new QCheckBox(tr("&Obstacle simulation"));
186 obstacleSimulationCheckBox->setShortcut(tr("o"));
187 layout->addWidget(obstacleSimulationCheckBox);
189 startPlug = new QCheckBox("Start plug");
190 layout->addWidget(startPlug);
192 puckInside = new QCheckBox("Puck inside");
193 layout->addWidget(puckInside);
195 miscGroupBox->setLayout(layout);
198 void RobomonAtlantis::createFSMGroupBox()
200 fsmGroupBox = new QGroupBox(tr("FSM"));
201 fsmGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
202 QGridLayout *layout = new QGridLayout();
204 layout->addWidget(MiscGui::createLabel("Main:"), 1, 0);
205 fsm_main_state = new QLabel();
206 fsm_main_state->setMinimumWidth(100);
207 layout->addWidget(fsm_main_state, 1, 1);
209 layout->addWidget(MiscGui::createLabel("Act:"), 2, 0);
210 fsm_act_state = new QLabel();
211 layout->addWidget(fsm_act_state, 2, 1);
213 layout->addWidget(MiscGui::createLabel("Motion:"), 3, 0);
214 fsm_motion_state = new QLabel();
215 layout->addWidget(fsm_motion_state, 3, 1);
217 fsmGroupBox->setLayout(layout);
220 void RobomonAtlantis::createDebugGroupBox()
222 debugGroupBox = new QGroupBox(tr("Debug window"));
223 debugGroupBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
224 QHBoxLayout *layout = new QHBoxLayout();
226 debugWindow = new QTextEdit();
227 debugWindow->setReadOnly(true);
229 layout->addWidget(debugWindow);
230 debugGroupBox->setLayout(layout);
233 void RobomonAtlantis::createActuatorsGroupBox()
235 actuatorsGroupBox = new QGroupBox(tr("Actuators"));
236 actuatorsGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
237 QHBoxLayout *layout = new QHBoxLayout();
239 createMotorsGroupBox();
240 createPickerGroupBox();
242 layout->setAlignment(Qt::AlignLeft);
243 layout->addWidget(enginesGroupBox);
244 layout->addWidget(pickerGroupBox);
245 actuatorsGroupBox->setLayout(layout);
248 void RobomonAtlantis::createDIOGroupBox()
250 dioGroupBox = new QGroupBox(tr("DIO"));
251 QGridLayout *layout = new QGridLayout();
254 font.setPointSize(5);
255 dioGroupBox->setFont(font);
257 for (int i=0; i<8; i++) {
258 diCheckBox[i] = new QCheckBox(QString("DI%1").arg(i));
259 doCheckBox[i] = new QCheckBox(QString("D0%1").arg(i));
260 layout->addWidget(diCheckBox[i], i, 0);
261 layout->addWidget(doCheckBox[i], i+8, 0);
264 dioGroupBox->setMaximumWidth(70);
265 dioGroupBox->setLayout(layout);
268 void RobomonAtlantis::createPowerGroupBox()
270 powerGroupBox = new QGroupBox(tr("Power management"));
271 powerGroupBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
272 powerGroupBox->setMaximumWidth(450);
273 QGridLayout *layout = new QGridLayout();
275 voltage33CheckBox = new QCheckBox(tr("&3.3V"));
276 voltage50CheckBox = new QCheckBox(tr("&5.0V"));
277 voltage80CheckBox = new QCheckBox(tr("&8.0V"));
279 voltage33CheckBox->setShortcut(tr("3"));
280 voltage50CheckBox->setShortcut(tr("5"));
281 voltage80CheckBox->setShortcut(tr("8"));
283 layout->addWidget(voltage33CheckBox, 0, 0);
284 layout->addWidget(voltage50CheckBox, 0, 2);
285 layout->addWidget(voltage80CheckBox, 0, 4); //1, 0);
286 layout->addWidget(MiscGui::createLabel("BAT"), 0, 6); //1, 2);
288 voltage33LineEdit = new QLineEdit();
289 voltage50LineEdit = new QLineEdit();
290 voltage80LineEdit = new QLineEdit();
291 voltageBATLineEdit = new QLineEdit();
293 voltage33LineEdit->setReadOnly(true);
294 voltage50LineEdit->setReadOnly(true);
295 voltage80LineEdit->setReadOnly(true);
296 voltageBATLineEdit->setReadOnly(true);
298 layout->addWidget(voltage33LineEdit, 0, 1);
299 layout->addWidget(voltage50LineEdit, 0, 3);
300 layout->addWidget(voltage80LineEdit, 0, 5); //1, 1);
301 layout->addWidget(voltageBATLineEdit, 0, 7); //1, 3);
303 powerGroupBox->setLayout(layout);
306 void RobomonAtlantis::createSensorsGroupBox()
308 sensorsGroupBox = new QGroupBox(tr("Sensors"));
309 sensorsGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
310 sensorsGroupBox->setMaximumWidth(450);
311 QHBoxLayout *layout = new QHBoxLayout();
313 layout->addWidget(MiscGui::createLabel("Puck Sharp"));
314 sharpPuck = new QProgressBar();
315 sharpPuck->setFormat("%v mm");
316 sharpPuck->setTextVisible(true);
317 sharpPuck->setMaximum(250);
318 layout->addWidget(sharpPuck);
320 layout->addWidget(MiscGui::createLabel("Lift"));
321 liftPos = new QProgressBar();
322 liftPos->setFormat("%v IRC");
323 liftPos->setTextVisible(true);
324 liftPos->setMaximum(1000);
325 layout->addWidget(liftPos);
327 layout->addWidget(MiscGui::createLabel("Pusher"));
328 pusherPos = new QProgressBar();
329 pusherPos->setFormat("%v IRC");
330 pusherPos->setTextVisible(true);
331 pusherPos->setMaximum(250);
332 layout->addWidget(pusherPos);
334 sensorsGroupBox->setLayout(layout);
337 void RobomonAtlantis::createMotorsGroupBox()
339 enginesGroupBox = new QGroupBox(tr("Motors"));
340 QVBoxLayout *layout = new QVBoxLayout();
341 QHBoxLayout *layout1 = new QHBoxLayout();
342 QHBoxLayout *layout2 = new QHBoxLayout();
344 leftMotorSlider = new QSlider(Qt::Vertical);
345 rightMotorSlider = new QSlider(Qt::Vertical);
346 bothMotorsCheckBox = new QCheckBox(tr("Lock both"));
347 stopMotorsPushButton = new QPushButton(tr("Stop Motors"));
349 leftMotorSlider->setMinimum(-100);
350 leftMotorSlider->setMaximum(100);
351 leftMotorSlider->setTracking(false);
352 leftMotorSlider->setTickPosition(QSlider::TicksLeft);
354 rightMotorSlider->setMinimum(-100);
355 rightMotorSlider->setMaximum(100);
356 rightMotorSlider->setTracking(false);
357 rightMotorSlider->setTickPosition(QSlider::TicksRight);
359 stopMotorsPushButton->setMaximumWidth(90);
361 layout1->addWidget(leftMotorSlider);
362 layout1->addWidget(MiscGui::createLabel("0"));
363 layout1->addWidget(rightMotorSlider);
365 layout2->addWidget(bothMotorsCheckBox);
367 layout->addWidget(MiscGui::createLabel("100"));
368 layout->addLayout(layout1);
369 layout->addWidget(MiscGui::createLabel("-100"));
370 layout->addLayout(layout2);
371 layout->addWidget(stopMotorsPushButton);
372 enginesGroupBox->setLayout(layout);
375 void RobomonAtlantis::createPickerGroupBox()
377 pickerGroupBox = new QGroupBox(tr("Picker"));
378 QVBoxLayout *layout = new QVBoxLayout();
380 leftBeltCheckBox = new QCheckBox(tr("L Belt"));
381 rightBeltCheckBox = new QCheckBox(tr("R Belt"));
382 leftChelaCheckBox = new QCheckBox(tr("L Chela"));
383 rightChelaCheckBox = new QCheckBox(tr("R Chela"));
384 leftBeltDial = new QDial();
385 rightBeltDial = new QDial();
386 leftChelaDial = new QDial();
387 rightChelaDial = new QDial();
388 pickPushButton = new QPushButton(tr("Pick!"));
390 leftBeltDial->setMinimum(0);
391 leftBeltDial->setMaximum(200);
392 leftBeltDial->setValue(100);
394 rightBeltDial->setMinimum(0);
395 rightBeltDial->setMaximum(200);
396 rightBeltDial->setValue(100);
398 leftChelaDial->setMinimum(0);
399 leftChelaDial->setMaximum(255);
400 leftChelaDial->setValue(127);
402 rightChelaDial->setMinimum(0);
403 rightChelaDial->setMaximum(255);
404 rightChelaDial->setValue(127);
406 layout->addWidget(leftChelaDial);
407 layout->addWidget(leftChelaCheckBox);
408 layout->addWidget(rightChelaDial);
409 layout->addWidget(rightChelaCheckBox);
410 layout->addWidget(leftBeltDial);
411 layout->addWidget(leftBeltCheckBox);
412 layout->addWidget(rightBeltDial);
413 layout->addWidget(rightBeltCheckBox);
414 layout->addWidget(pickPushButton);
416 pickerGroupBox->setLayout(layout);
419 void RobomonAtlantis::createRobots()
421 robotActPos = new Robot("Ref", QPen(Qt::darkBlue), QBrush(Qt::NoBrush));
422 robotActPos->setZValue(11);
423 trailRefPos = new Trail(QPen(Qt::darkBlue));
424 trailRefPos->setZValue(11);
426 robotEstPosOdo = new Robot("Odo", QPen(Qt::white), QBrush(Qt::darkRed));
427 robotEstPosOdo->setZValue(10);
428 trailOdoPos = new Trail(QPen(Qt::white));
429 trailOdoPos->setZValue(10);
430 robotEstPosUzv = new Robot("Uzv", QPen(), QBrush(Qt::darkGray));
431 robotEstPosUzv->setZValue(10);
432 trailUzvPos = new Trail(QPen());
433 trailUzvPos->setZValue(10);
435 playgroundScene->addItem(robotActPos);
436 playgroundScene->addItem(robotEstPosUzv);
437 playgroundScene->addItem(robotEstPosOdo);
439 playgroundScene->addItem(trailRefPos);
440 playgroundScene->addItem(trailUzvPos);
441 playgroundScene->addItem(trailOdoPos);
444 /**********************************************************************
446 **********************************************************************/
447 void RobomonAtlantis::createActions()
449 /* power management */
450 connect(voltage33CheckBox, SIGNAL(stateChanged(int)),
451 this, SLOT(setVoltage33(int)));
452 connect(voltage50CheckBox, SIGNAL(stateChanged(int)),
453 this, SLOT(setVoltage50(int)));
454 connect(voltage80CheckBox, SIGNAL(stateChanged(int)),
455 this, SLOT(setVoltage80(int)));
458 connect(leftMotorSlider, SIGNAL(valueChanged(int)),
459 this, SLOT(setLeftMotor(int)));
460 connect(rightMotorSlider, SIGNAL(valueChanged(int)),
461 this, SLOT(setRightMotor(int)));
462 connect(stopMotorsPushButton, SIGNAL(clicked()),
463 this, SLOT(stopMotors()));
466 for (int i=0; i<8; i++)
467 connect(doCheckBox[0], SIGNAL(stateChanged(int)),
468 this, SLOT(setDO(int)));
470 /* path planning map */
471 connect(showMapPushButton, SIGNAL(clicked()),
472 this, SLOT(showMap()));
474 connect(startPlug, SIGNAL(stateChanged(int)), this, SLOT(sendStart(int)));
475 connect(puckInside, SIGNAL(stateChanged(int)), this, SLOT(sendPuckInside(int)));
477 /* obstacle simulation */
478 simulationEnabled = 0;
479 connect(obstacleSimulationCheckBox, SIGNAL(stateChanged(int)),
480 this, SLOT(setSimulation(int)));
481 connect(obstacleSimulationCheckBox, SIGNAL(stateChanged(int)),
482 this, SLOT(setObstacleSimulation(int)));
483 connect(obstacleSimulationCheckBox, SIGNAL(stateChanged(int)),
484 playgroundScene, SLOT(showObstacle(int)));
485 connect(playgroundScene, SIGNAL(obstacleChanged(QPointF)),
486 this, SLOT(changeObstacle(QPointF)));
489 connect(pickPushButton, SIGNAL(clicked()), this, SLOT(pick()));
490 connect(leftBeltCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setBelts(int)));
491 connect(rightBeltCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setBelts(int)));
492 connect(leftChelaCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setChelae(int)));
493 connect(rightChelaCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setChelae(int)));
494 connect(leftBeltDial, SIGNAL(valueChanged(int)), this, SLOT(setBelts(int)));
495 connect(rightBeltDial, SIGNAL(valueChanged(int)), this, SLOT(setBelts(int)));
496 connect(leftChelaDial, SIGNAL(valueChanged(int)), this, SLOT(setChelae(int)));
497 connect(rightChelaDial, SIGNAL(valueChanged(int)), this, SLOT(setChelae(int)));
500 void RobomonAtlantis::setVoltage33(int state)
503 orte.pwr_ctrl.voltage33 = true;
505 orte.pwr_ctrl.voltage33 = false;
508 void RobomonAtlantis::setVoltage50(int state)
511 orte.pwr_ctrl.voltage50 = true;
513 orte.pwr_ctrl.voltage50 = false;
516 void RobomonAtlantis::setVoltage80(int state)
519 orte.pwr_ctrl.voltage80 = true;
521 orte.pwr_ctrl.voltage80 = false;
524 void RobomonAtlantis::setLeftMotor(int value)
527 short int rightMotor;
529 if(bothMotorsCheckBox->isChecked())
530 rightMotorSlider->setValue(value);
532 leftMotor = (short int)(MOTOR_LIMIT * (leftMotorSlider->value()/100.0));
533 rightMotor = (short int)(MOTOR_LIMIT * (rightMotorSlider->value()/100.0));
535 orte.motion_speed.left = leftMotor;
536 orte.motion_speed.right = rightMotor;
540 void RobomonAtlantis::setRightMotor(int value)
543 short int rightMotor;
545 if(bothMotorsCheckBox->isChecked())
546 leftMotorSlider->setValue(value);
548 leftMotor = (short int)(MOTOR_LIMIT * (leftMotorSlider->value()/100.0));
549 rightMotor = (short int)(MOTOR_LIMIT * (rightMotorSlider->value()/100.0));
551 orte.motion_speed.left = leftMotor;
552 orte.motion_speed.right = rightMotor;
556 void RobomonAtlantis::stopMotors()
558 leftMotorSlider->setValue(0);
559 rightMotorSlider->setValue(0);
563 void RobomonAtlantis::setDO(int state)
566 /* FIXME: digital output control comes here */
569 void RobomonAtlantis::pick()
571 // TODO: send signal to the fsmact
574 void RobomonAtlantis::setBelts(int value)
577 unsigned char leftBelt;
578 unsigned char rightBelt;
580 leftBelt = (unsigned char)leftBeltDial->value();
581 rightBelt = (unsigned char)rightBeltDial->value();
583 act_belts(leftBelt, rightBelt);
586 void RobomonAtlantis::setChelae(int value)
589 unsigned char leftChela;
590 unsigned char rightChela;
592 leftChela = (unsigned char)leftChelaDial->value();
593 rightChela = (unsigned char)rightChelaDial->value();
595 act_chelae(leftChela, rightChela);
598 void RobomonAtlantis::showMap()
602 if (sharedMemoryOpened == false)
605 mapTimer = new QTimer(this);
606 connect(mapTimer, SIGNAL(timeout()), this, SLOT(paintMap()));
607 mapTimer->start(200);
609 disconnect(showMapPushButton, SIGNAL(clicked()), this, SLOT(showMap()));
610 connect(showMapPushButton, SIGNAL(clicked()),
611 this, SLOT(showPlayground()));
612 playgroundScene->showMap(true);
615 void RobomonAtlantis::showPlayground()
618 disconnect(mapTimer, SIGNAL(timeout()), this, SLOT(paintMap()));
620 disconnect(showMapPushButton, SIGNAL(clicked()),
621 this, SLOT(showPlayground()));
622 connect(showMapPushButton, SIGNAL(clicked()), this, SLOT(showMap()));
623 playgroundScene->showMap(false);
626 void RobomonAtlantis::paintMap()
629 struct map *map = ShmapIsMapInit();
633 for(int i=0; i < MAP_WIDTH; i++) {
634 for(int j=0; j<MAP_HEIGHT; j++) {
637 struct map_cell *cell = &map->cells[j][i];
640 if ((cell->flags & MAP_FLAG_WALL) &&
641 (cell->flags & MAP_FLAG_INVALIDATE_WALL) == 0)
643 if (cell->flags & MAP_FLAG_IGNORE_OBST)
645 if (cell->flags & MAP_FLAG_SIMULATED_WALL)
647 if (cell->flags & MAP_FLAG_PATH)
649 if (cell->flags & MAP_FLAG_START)
651 if (cell->flags & MAP_FLAG_GOAL)
653 if (cell->detected_obstacle) {
654 QColor c1(color), c2(blue);
655 double f = (double)cell->detected_obstacle/MAP_NEW_OBSTACLE*0.7;
656 QColor c(c1.red() + (int)(f*(c2.red() - c1.red())),
657 c1.green() + (int)(f*(c2.green() - c1.green())),
658 c1.blue() + (int)(f*(c2.blue() - c1.blue())));
661 if (cell->flags & MAP_FLAG_DET_OBST)
664 playgroundScene->setMapColor(i, j, color);
669 void RobomonAtlantis::setSimulation(int state)
672 robottype_publisher_hokuyo_scan_create(&orte,
673 dummy_publisher_callback, this);
675 if (!simulationEnabled)
677 robottype_publisher_hokuyo_scan_destroy(&orte);
679 simulationEnabled = state;
683 \fn RobomonAtlantis::setObstacleSimulation(int state)
685 void RobomonAtlantis::setObstacleSimulation(int state)
688 /* TODO Maybe it is possible to attach only once to Shmap */
690 obstacleSimulationTimer = new QTimer(this);
691 connect(obstacleSimulationTimer, SIGNAL(timeout()),
692 this, SLOT(simulateObstaclesHokuyo()));
693 obstacleSimulationTimer->start(100);
694 setMouseTracking(true);
696 if (obstacleSimulationTimer)
697 delete obstacleSimulationTimer;
698 //double distance = 0.8;
703 void RobomonAtlantis::simulateObstaclesHokuyo()
705 double distance, wall_distance;
707 uint16_t *hokuyo = orte.hokuyo_scan.data;
709 for (i=0; i<HOKUYO_CLUSTER_CNT; i++) {
710 wall_distance = distanceToWallHokuyo(i);
711 distance = distanceToObstacleHokuyo(i, simulatedObstacle, SIM_OBST_SIZE_M/*meters*/);
712 if (wall_distance < distance)
713 distance = wall_distance;
714 hokuyo[i] = distance*1000;
716 ORTEPublicationSend(orte.publication_hokuyo_scan);
720 void RobomonAtlantis::changeObstacle(QPointF position)
722 if (!simulationEnabled) {
723 simulationEnabled = 1;
724 obstacleSimulationCheckBox->setChecked(true);
727 simulatedObstacle.x = position.x();
728 simulatedObstacle.y = position.y();
729 simulateObstaclesHokuyo();
732 /**********************************************************************
734 **********************************************************************/
735 bool RobomonAtlantis::event(QEvent *event)
737 switch (event->type()) {
738 case QEVENT(QEV_MOTION_STATUS):
739 emit motionStatusReceivedSignal();
741 case QEVENT(QEV_REFERENCE_POSITION):
742 emit actualPositionReceivedSignal();
744 case QEVENT(QEV_ESTIMATED_POSITION_UZV):
745 emit estimatedPositionReceivedSignal();
747 case QEVENT(QEV_ESTIMATED_POSITION_ODO):
748 robotEstPosOdo->moveRobot(orte.est_pos_odo.x,
749 orte.est_pos_odo.y, orte.est_pos_odo.phi);
750 trailOdoPos->addPoint(QPointF(orte.est_pos_odo.x,
751 orte.est_pos_odo.y));
754 emit diReceivedSignal();
756 case QEVENT(QEV_ACCELEROMETER):
757 emit accelerometerReceivedSignal();
759 case QEVENT(QEV_ACCUMULATOR):
760 emit accumulatorReceivedSignal();
762 case QEVENT(QEV_POWER_VOLTAGE):
763 emit powerVoltageReceivedSignal();
765 case QEVENT(QEV_FSM_MAIN):
766 fsm_main_state->setText(orte.fsm_main.state_name);
768 case QEVENT(QEV_FSM_ACT):
769 fsm_act_state->setText(orte.fsm_act.state_name);
771 case QEVENT(QEV_FSM_MOTION):
772 fsm_motion_state->setText(orte.fsm_motion.state_name);
774 case QEVENT(QEV_PUCK_DISTANCE):
775 sharpPuck->setValue(orte.puck_distance.distance*1000);
777 case QEVENT(QEV_LIFT_PUSHER):
778 liftPos->setValue(orte.actuator_status.lift_pos);
779 pusherPos->setValue(orte.actuator_status.pusher_pos);
782 if (event->type() == QEvent::Close)
783 closeEvent((QCloseEvent *)event);
784 else if (event->type() == QEvent::KeyPress)
785 keyPressEvent((QKeyEvent *)event);
786 else if (event->type() == QEvent::KeyRelease)
787 keyReleaseEvent((QKeyEvent *)event);
788 else if (event->type() == QEvent::FocusIn)
790 else if (event->type() == QEvent::FocusOut)
802 void RobomonAtlantis::keyPressEvent(QKeyEvent *event)
806 if (event->isAutoRepeat()) {
807 switch (event->key()) {
809 peak = leftMotorSlider->minimum()/2;
810 if (leftMotorValue < peak ||
811 rightMotorValue < peak)
815 leftMotorValue *= gain;
816 rightMotorValue *= gain;
817 leftMotorSlider->setValue((int)leftMotorValue);
818 rightMotorSlider->setValue((int)rightMotorValue);
824 peak = leftMotorSlider->maximum()/2;
825 if (leftMotorValue > peak ||
826 rightMotorValue > peak)
830 leftMotorValue *= gain;
831 rightMotorValue *= gain;
832 leftMotorSlider->setValue((int)leftMotorValue);
833 rightMotorSlider->setValue((int)rightMotorValue);
843 switch (event->key()) {
847 bothMotorsCheckBox->setChecked(true);
848 leftMotorSlider->setValue((int)leftMotorValue);
849 setLeftMotor((int)leftMotorValue);
853 rightMotorValue = -1;
854 bothMotorsCheckBox->setChecked(true);
855 leftMotorSlider->setValue((int)leftMotorValue);
856 setLeftMotor((int)leftMotorValue);
861 leftMotorSlider->setValue((int)leftMotorValue);
862 rightMotorSlider->setValue((int)rightMotorValue);
863 setLeftMotor((int)leftMotorValue);
864 setRightMotor((int)leftMotorValue);
868 rightMotorValue = -1;
869 leftMotorSlider->setValue((int)leftMotorValue);
870 rightMotorSlider->setValue((int)rightMotorValue);
871 setLeftMotor((int)leftMotorValue);
872 setRightMotor((int)rightMotorValue);
881 void RobomonAtlantis::keyReleaseEvent(QKeyEvent *event)
883 if (event->isAutoRepeat()) {
888 switch (event->key()) {
895 bothMotorsCheckBox->setChecked(false);
896 leftMotorSlider->setValue((int)leftMotorValue);
897 rightMotorSlider->setValue((int)rightMotorValue);
906 void RobomonAtlantis::closeEvent(QCloseEvent *)
908 robottype_roboorte_destroy(&orte);
911 /**********************************************************************
913 **********************************************************************/
914 void RobomonAtlantis::createOrte()
920 rv = robottype_roboorte_init(&orte);
922 printf("RobomonAtlantis: Unable to initialize ORTE\n");
926 robottype_publisher_motion_speed_create(&orte, dummy_publisher_callback, NULL);
928 robottype_publisher_pwr_ctrl_create(&orte, dummy_publisher_callback, NULL);
929 robottype_publisher_lift_create(&orte, NULL, &orte);
930 robottype_publisher_pusher_create(&orte, NULL, &orte);
931 robottype_publisher_chelae_create(&orte, NULL, &orte);
932 robottype_publisher_belts_create(&orte, NULL, &orte);
933 robottype_publisher_holder_create(&orte, NULL, &orte);
934 robottype_publisher_robot_cmd_create(&orte, NULL, &orte);
935 robottype_publisher_puck_inside_create(&orte, NULL, &orte);
938 robottype_subscriber_pwr_voltage_create(&orte,
939 receivePowerVoltageCallBack, this);
940 robottype_subscriber_motion_status_create(&orte,
941 receiveMotionStatusCallBack, this);
942 robottype_subscriber_ref_pos_create(&orte,
943 receiveActualPositionCallBack, this);
944 robottype_subscriber_est_pos_uzv_create(&orte,
945 generic_rcv_cb, new OrteCallbackInfo(this, QEV_ESTIMATED_POSITION_UZV));
946 robottype_subscriber_est_pos_odo_create(&orte,
947 generic_rcv_cb, new OrteCallbackInfo(this, QEV_ESTIMATED_POSITION_ODO));
948 robottype_subscriber_fsm_main_create(&orte,
949 rcv_fsm_main_cb, this);
950 robottype_subscriber_fsm_motion_create(&orte,
951 rcv_fsm_motion_cb, this);
952 robottype_subscriber_fsm_act_create(&orte,
953 rcv_fsm_act_cb, this);
954 robottype_subscriber_puck_distance_create(&orte, generic_rcv_cb, new OrteCallbackInfo(this, QEV_PUCK_DISTANCE));
955 robottype_subscriber_actuator_status_create(&orte, generic_rcv_cb, new OrteCallbackInfo(this, QEV_LIFT_PUSHER));
958 /*createDISubscriber(this, &orteData);*/
959 /*createAccelerometerSubscriber(this, &orteData);*/
960 /*createAccumulatorSubscriber(this, &orteData);*/
962 orte.motion_speed.left = 0;
963 orte.motion_speed.right = 0;
964 /* power management */
965 orte.pwr_ctrl.voltage33 = true;
966 orte.pwr_ctrl.voltage50 = true;
967 orte.pwr_ctrl.voltage80 = true;
968 voltage33CheckBox->setChecked(true);
969 voltage50CheckBox->setChecked(true);
970 voltage80CheckBox->setChecked(true);
974 /* set actions to do when we receive data from orte */
975 connect(this, SIGNAL(motionStatusReceivedSignal()),
976 this, SLOT(motionStatusReceived()));
977 connect(this, SIGNAL(actualPositionReceivedSignal()),
978 this, SLOT(actualPositionReceived()));
979 connect(this, SIGNAL(estimatedPositionReceivedSignal()),
980 this, SLOT(estimatedPositionReceived()));
981 connect(this, SIGNAL(diReceivedSignal()),
982 this, SLOT(diReceived()));
983 connect(this, SIGNAL(accelerometerReceivedSignal()),
984 this, SLOT(accelerometerReceived()));
985 connect(this, SIGNAL(accumulatorReceivedSignal()),
986 this, SLOT(accumulatorReceived()));
987 connect(this, SIGNAL(powerVoltageReceivedSignal()),
988 this, SLOT(powerVoltageReceived()));
991 void RobomonAtlantis::motionStatusReceived()
993 WDBG("ORTE received: motion status");
996 void RobomonAtlantis::actualPositionReceived()
998 actPosX->setText(QString("%1").arg(orte.ref_pos.x, 0, 'f', 3));
999 actPosY->setText(QString("%1").arg(orte.ref_pos.y, 0, 'f', 3));
1000 actPosPhi->setText(QString("%1(%2)")
1001 .arg(DEGREES(orte.ref_pos.phi), 0, 'f', 0)
1002 .arg(orte.ref_pos.phi, 0, 'f', 1));
1003 robotActPos->moveRobot(orte.ref_pos.x,
1004 orte.ref_pos.y, orte.ref_pos.phi);
1005 trailRefPos->addPoint(QPointF(orte.ref_pos.x, orte.ref_pos.y));
1008 void RobomonAtlantis::estimatedPositionReceived()
1010 estPosX->setText(QString("%1").arg(orte.est_pos_uzv.x, 0, 'f', 3));
1011 estPosY->setText(QString("%1").arg(orte.est_pos_uzv.y, 0, 'f', 3));
1012 estPosPhi->setText(QString("%1(%2)")
1013 .arg(DEGREES(orte.est_pos_uzv.phi), 0, 'f', 0)
1014 .arg(orte.est_pos_uzv.phi, 0, 'f', 1));
1015 robotEstPosUzv->moveRobot(orte.est_pos_uzv.x,
1016 orte.est_pos_uzv.y, orte.est_pos_uzv.phi);
1017 trailUzvPos->addPoint(QPointF(orte.est_pos_odo.x,
1018 orte.est_pos_odo.y));
1021 void RobomonAtlantis::diReceived()
1023 WDBG("ORTE received: DI");
1026 void RobomonAtlantis::accelerometerReceived()
1028 WDBG("ORTE received: accelerometer");
1031 void RobomonAtlantis::accumulatorReceived()
1033 WDBG("ORTE received: accumulator");
1036 void RobomonAtlantis::powerVoltageReceived()
1038 voltage33LineEdit->setText(QString("%1").arg(
1039 orte.pwr_voltage.voltage33, 0, 'f', 3));
1040 voltage50LineEdit->setText(QString("%1").arg(
1041 orte.pwr_voltage.voltage50, 0, 'f', 3));
1042 voltage80LineEdit->setText(QString("%1").arg(
1043 orte.pwr_voltage.voltage80, 0, 'f', 3));
1044 voltageBATLineEdit->setText(QString("%1").arg(
1045 orte.pwr_voltage.voltageBAT, 0, 'f', 3));
1049 /**********************************************************************
1051 **********************************************************************/
1052 void RobomonAtlantis::openSharedMemory()
1055 int sharedSegmentSize;
1057 if (sharedMemoryOpened)
1060 sharedSegmentSize = sizeof(unsigned int) * MAP_WIDTH * MAP_HEIGHT;
1062 /* Get segment identificator in a read only mode */
1063 segmentId = shmget(SHM_MAP_KEY, sharedSegmentSize, S_IRUSR);
1064 if(segmentId == -1) {
1065 QMessageBox::critical(this, "robomon",
1066 "Unable to open shared memory segment!");
1073 /* Attach the shared memory segment */
1074 //map = (_Map*)shmat (segmentId, (void*) 0, 0);
1076 sharedMemoryOpened = true;
1079 double RobomonAtlantis::distanceToWallHokuyo(int beamnum)
1081 double distance=4.0, min_distance=4.0;
1084 struct map *map = ShmapIsMapInit();
1086 if (!map) return min_distance;
1088 // Simulate obstacles
1089 for(j=0;j<MAP_HEIGHT;j++) {
1090 for (i=0;i<MAP_WIDTH;i++) {
1091 struct map_cell *cell = &map->cells[j][i];
1092 if( cell->flags & MAP_FLAG_SIMULATED_WALL) {
1094 ShmapCell2Point(i, j, &wall.x, &wall.y);
1096 distance = distanceToObstacleHokuyo(beamnum, wall, MAP_CELL_SIZE_M);
1097 if (distance<min_distance) min_distance = distance;
1102 return min_distance;
1106 * Calculation for Hokuyo simulation. Calculates distance that would
1107 * be returned by Hokuyo sensors, if there is only one obstacle (as
1108 * specified by parameters).
1110 * @param beamnum Hokuyo's bean number [0..HOKUYO_CLUSTER_CNT]
1111 * @param obstacle Position of the obstacle (x, y in meters).
1112 * @param obstacleSize Size (diameter) of the obstacle in meters.
1114 * @return Distance measured by sensors in meters.
1116 double RobomonAtlantis::distanceToObstacleHokuyo(int beamnum, Point obstacle, double obstacleSize)
1118 struct robot_pos_type e = orte.est_pos_uzv;
1122 s.x = HOKUYO_CENTER_OFFSET_M;
1124 s.ang = HOKUYO_CLUSTER_TO_RAD(beamnum);
1126 Point sensor(e.x + s.x*cos(e.phi) - s.y*sin(e.phi),
1127 e.y + s.x*sin(e.phi) + s.y*cos(e.phi));
1128 sensor_a = e.phi + s.ang;
1130 const double sensorRange = 4.0; /*[meters]*/
1132 double distance, angle;
1134 angle = sensor.angleTo(obstacle) - sensor_a;
1135 angle = fmod(angle, 2.0*M_PI);
1136 if (angle > +M_PI) angle -= 2.0*M_PI;
1137 if (angle < -M_PI) angle += 2.0*M_PI;
1138 angle = fabs(angle);
1139 distance = sensor.distanceTo(obstacle)-0.11;
1140 if (angle < atan(obstacleSize/2.0 / (distance+0.001))) {
1141 // We can see the obstackle from here.
1142 if (angle < M_PI/2.0) {
1143 distance = distance/cos(angle);
1145 if (distance > sensorRange)
1146 distance = sensorRange;
1148 distance = sensorRange;
1154 void RobomonAtlantis::sendStart(int plug)
1156 orte.robot_cmd.start = plug ? 0 : 1;
1157 ORTEPublicationSend(orte.publication_robot_cmd);
1160 void RobomonAtlantis::sendPuckInside(int value)
1162 orte.puck_inside.status = value ? 1 : 0;
1163 ORTEPublicationSend(orte.publication_puck_inside);