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"
41 #include <QCoreApplication>
45 #include <QMessageBox>
47 RobomonAtlantis::RobomonAtlantis(QWidget *parent)
54 debugWindowEnabled = false;
59 QHBoxLayout *mainLayout = new QHBoxLayout;
60 mainLayout->addLayout(leftLayout);
61 mainLayout->addLayout(rightLayout);
62 setLayout(mainLayout);
68 setFocusPolicy(Qt::StrongFocus);
69 sharedMemoryOpened = false;
70 WDBG("Youuuhouuuu!!");
73 /**********************************************************************
75 **********************************************************************/
76 void RobomonAtlantis::createLeftLayout()
78 leftLayout = new QVBoxLayout();
80 createDebugGroupBox();
81 debugWindowEnabled = true;
82 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();
96 createActuatorsGroupBox();
98 createPowerGroupBox();
99 createSensorsGroupBox();
101 vlayout->addWidget(positionGroupBox);
102 vlayout->addWidget(miscGroupBox);
103 vlayout->addWidget(fsmGroupBox);
104 layout->addLayout(vlayout, 0, 0);
105 layout->addWidget(actuatorsGroupBox, 0, 1);
106 // layout->addWidget(dioGroupBox, 0, 2);
108 rightLayout->addLayout(layout);
109 rightLayout->addWidget(powerGroupBox);
110 rightLayout->addWidget(sensorsGroupBox);
113 void RobomonAtlantis::createPlaygroundGroupBox()
115 playgroundGroupBox = new QGroupBox(tr("Playground"));
116 QHBoxLayout *layout = new QHBoxLayout();
118 playgroundScene = new PlaygroundScene();
119 playgroundSceneView = new PlaygroundView(playgroundScene);
120 //playgroundSceneView->setMinimumWidth(630);
121 //playgroundSceneView->setMinimumHeight(445);
122 playgroundSceneView->setMatrix(QMatrix(1,0,0,-1,0,0), true);
123 playgroundSceneView->fitInView(playgroundScene->itemsBoundingRect());
124 playgroundSceneView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
125 layout->addWidget(playgroundSceneView);
127 playgroundGroupBox->setLayout(layout);
130 void RobomonAtlantis::createPositionGroupBox()
132 positionGroupBox = new QGroupBox(tr("Position state"));
133 positionGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
134 QGridLayout *layout = new QGridLayout();
136 actPosX = new QLineEdit();
137 actPosY = new QLineEdit();
138 actPosPhi = new QLineEdit();
140 estPosX = new QLineEdit();
141 estPosY = new QLineEdit();
142 estPosPhi = new QLineEdit();
144 actPosX->setReadOnly(true);
145 actPosY->setReadOnly(true);
146 actPosPhi->setReadOnly(true);
148 estPosX->setReadOnly(true);
149 estPosY->setReadOnly(true);
150 estPosPhi->setReadOnly(true);
152 layout->addWidget(MiscGui::createLabel("X"), 1, 0);
153 layout->addWidget(MiscGui::createLabel("Y"), 2, 0);
154 layout->addWidget(MiscGui::createLabel("Phi"), 3, 0);
156 layout->addWidget(MiscGui::createLabel("X"), 5, 0);
157 layout->addWidget(MiscGui::createLabel("Y"), 6, 0);
158 layout->addWidget(MiscGui::createLabel("Phi"), 7, 0);
160 layout->addWidget(MiscGui::createLabel("Reference", Qt::AlignLeft), 0, 1);
161 layout->addWidget(actPosX, 1, 1);
162 layout->addWidget(actPosY, 2, 1);
163 layout->addWidget(actPosPhi, 3, 1);
165 layout->addWidget(MiscGui::createLabel("Estimated", Qt::AlignLeft), 4, 1);
166 layout->addWidget(estPosX, 5, 1);
167 layout->addWidget(estPosY, 6, 1);
168 layout->addWidget(estPosPhi, 7, 1);
170 positionGroupBox->setLayout(layout);
173 void RobomonAtlantis::createMiscGroupBox()
175 miscGroupBox = new QGroupBox(tr("Miscellaneous"));
176 miscGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
177 QGridLayout *layout = new QGridLayout();
179 showMapPushButton = new QCheckBox(tr("Show &map"));
180 showMapPushButton->setShortcut(tr("m"));
182 layout->addWidget(showMapPushButton, 0, 0);
184 obstacleSimulationCheckBox = new QCheckBox(tr("&Obstacle simulation"));
185 obstacleSimulationCheckBox->setShortcut(tr("o"));
186 layout->addWidget(obstacleSimulationCheckBox);
188 startPlug = new QCheckBox("Start plug");
189 layout->addWidget(startPlug);
191 puckInside = new QCheckBox("Puck inside");
192 layout->addWidget(puckInside);
194 miscGroupBox->setLayout(layout);
197 void RobomonAtlantis::createFSMGroupBox()
199 fsmGroupBox = new QGroupBox(tr("FSM"));
200 fsmGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
201 QGridLayout *layout = new QGridLayout();
203 layout->addWidget(MiscGui::createLabel("Main:"), 1, 0);
204 fsm_main_state = new QLabel();
205 fsm_main_state->setMinimumWidth(100);
206 layout->addWidget(fsm_main_state, 1, 1);
208 layout->addWidget(MiscGui::createLabel("Act:"), 2, 0);
209 fsm_act_state = new QLabel();
210 layout->addWidget(fsm_act_state, 2, 1);
212 layout->addWidget(MiscGui::createLabel("Motion:"), 3, 0);
213 fsm_motion_state = new QLabel();
214 layout->addWidget(fsm_motion_state, 3, 1);
216 fsmGroupBox->setLayout(layout);
219 void RobomonAtlantis::createDebugGroupBox()
221 debugGroupBox = new QGroupBox(tr("Debug window"));
222 debugGroupBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
223 QHBoxLayout *layout = new QHBoxLayout();
225 debugWindow = new QTextEdit();
226 debugWindow->setReadOnly(true);
228 layout->addWidget(debugWindow);
229 debugGroupBox->setLayout(layout);
232 void RobomonAtlantis::createActuatorsGroupBox()
234 actuatorsGroupBox = new QGroupBox(tr("Actuators"));
235 actuatorsGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
236 QHBoxLayout *layout = new QHBoxLayout();
238 createMotorsGroupBox();
239 createPickerGroupBox();
241 layout->setAlignment(Qt::AlignLeft);
242 layout->addWidget(enginesGroupBox);
243 layout->addWidget(pickerGroupBox);
244 actuatorsGroupBox->setLayout(layout);
247 void RobomonAtlantis::createDIOGroupBox()
249 dioGroupBox = new QGroupBox(tr("DIO"));
250 QGridLayout *layout = new QGridLayout();
253 font.setPointSize(5);
254 dioGroupBox->setFont(font);
256 for (int i=0; i<8; i++) {
257 diCheckBox[i] = new QCheckBox(QString("DI%1").arg(i));
258 doCheckBox[i] = new QCheckBox(QString("D0%1").arg(i));
259 layout->addWidget(diCheckBox[i], i, 0);
260 layout->addWidget(doCheckBox[i], i+8, 0);
263 dioGroupBox->setMaximumWidth(70);
264 dioGroupBox->setLayout(layout);
267 void RobomonAtlantis::createPowerGroupBox()
269 powerGroupBox = new QGroupBox(tr("Power management"));
270 powerGroupBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
271 powerGroupBox->setMaximumWidth(450);
272 QGridLayout *layout = new QGridLayout();
274 voltage33CheckBox = new QCheckBox(tr("&3.3V"));
275 voltage50CheckBox = new QCheckBox(tr("&5.0V"));
276 voltage80CheckBox = new QCheckBox(tr("&8.0V"));
278 voltage33CheckBox->setShortcut(tr("3"));
279 voltage50CheckBox->setShortcut(tr("5"));
280 voltage80CheckBox->setShortcut(tr("8"));
282 layout->addWidget(voltage33CheckBox, 0, 0);
283 layout->addWidget(voltage50CheckBox, 0, 2);
284 layout->addWidget(voltage80CheckBox, 0, 4); //1, 0);
285 layout->addWidget(MiscGui::createLabel("BAT"), 0, 6); //1, 2);
287 voltage33LineEdit = new QLineEdit();
288 voltage50LineEdit = new QLineEdit();
289 voltage80LineEdit = new QLineEdit();
290 voltageBATLineEdit = new QLineEdit();
292 voltage33LineEdit->setReadOnly(true);
293 voltage50LineEdit->setReadOnly(true);
294 voltage80LineEdit->setReadOnly(true);
295 voltageBATLineEdit->setReadOnly(true);
297 layout->addWidget(voltage33LineEdit, 0, 1);
298 layout->addWidget(voltage50LineEdit, 0, 3);
299 layout->addWidget(voltage80LineEdit, 0, 5); //1, 1);
300 layout->addWidget(voltageBATLineEdit, 0, 7); //1, 3);
302 powerGroupBox->setLayout(layout);
305 void RobomonAtlantis::createSensorsGroupBox()
307 sensorsGroupBox = new QGroupBox(tr("Sensors"));
308 sensorsGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
309 sensorsGroupBox->setMaximumWidth(450);
310 QHBoxLayout *layout = new QHBoxLayout();
312 layout->addWidget(MiscGui::createLabel("Puck Sharp"));
313 sharpPuck = new QProgressBar();
314 sharpPuck->setFormat("%v mm");
315 sharpPuck->setTextVisible(true);
316 sharpPuck->setMaximum(250);
317 layout->addWidget(sharpPuck);
319 layout->addWidget(MiscGui::createLabel("Lift"));
320 liftPos = new QProgressBar();
321 liftPos->setFormat("%v IRC");
322 liftPos->setTextVisible(true);
323 liftPos->setMaximum(1000);
324 layout->addWidget(liftPos);
326 layout->addWidget(MiscGui::createLabel("Pusher"));
327 pusherPos = new QProgressBar();
328 pusherPos->setFormat("%v IRC");
329 pusherPos->setTextVisible(true);
330 pusherPos->setMaximum(250);
331 layout->addWidget(pusherPos);
333 sensorsGroupBox->setLayout(layout);
336 void RobomonAtlantis::createMotorsGroupBox()
338 enginesGroupBox = new QGroupBox(tr("Motors"));
339 QVBoxLayout *layout = new QVBoxLayout();
340 QHBoxLayout *layout1 = new QHBoxLayout();
341 QHBoxLayout *layout2 = new QHBoxLayout();
343 leftMotorSlider = new QSlider(Qt::Vertical);
344 rightMotorSlider = new QSlider(Qt::Vertical);
345 bothMotorsCheckBox = new QCheckBox(tr("Lock both"));
346 stopMotorsPushButton = new QPushButton(tr("Stop Motors"));
348 leftMotorSlider->setMinimum(-100);
349 leftMotorSlider->setMaximum(100);
350 leftMotorSlider->setTracking(false);
351 leftMotorSlider->setTickPosition(QSlider::TicksLeft);
353 rightMotorSlider->setMinimum(-100);
354 rightMotorSlider->setMaximum(100);
355 rightMotorSlider->setTracking(false);
356 rightMotorSlider->setTickPosition(QSlider::TicksRight);
358 stopMotorsPushButton->setMaximumWidth(90);
360 layout1->addWidget(leftMotorSlider);
361 layout1->addWidget(MiscGui::createLabel("0"));
362 layout1->addWidget(rightMotorSlider);
364 layout2->addWidget(bothMotorsCheckBox);
366 layout->addWidget(MiscGui::createLabel("100"));
367 layout->addLayout(layout1);
368 layout->addWidget(MiscGui::createLabel("-100"));
369 layout->addLayout(layout2);
370 layout->addWidget(stopMotorsPushButton);
371 enginesGroupBox->setLayout(layout);
374 void RobomonAtlantis::createPickerGroupBox()
376 pickerGroupBox = new QGroupBox(tr("Picker"));
377 QVBoxLayout *layout = new QVBoxLayout();
379 leftBeltCheckBox = new QCheckBox(tr("L Belt"));
380 rightBeltCheckBox = new QCheckBox(tr("R Belt"));
381 leftChelaCheckBox = new QCheckBox(tr("L Chela"));
382 rightChelaCheckBox = new QCheckBox(tr("R Chela"));
383 leftBeltDial = new QDial();
384 rightBeltDial = new QDial();
385 leftChelaDial = new QDial();
386 rightChelaDial = new QDial();
387 pickPushButton = new QPushButton(tr("Pick!"));
389 leftBeltDial->setMinimum(0);
390 leftBeltDial->setMaximum(200);
391 leftBeltDial->setValue(100);
393 rightBeltDial->setMinimum(0);
394 rightBeltDial->setMaximum(200);
395 rightBeltDial->setValue(100);
397 leftChelaDial->setMinimum(0);
398 leftChelaDial->setMaximum(255);
399 leftChelaDial->setValue(127);
401 rightChelaDial->setMinimum(0);
402 rightChelaDial->setMaximum(255);
403 rightChelaDial->setValue(127);
405 layout->addWidget(leftChelaDial);
406 layout->addWidget(leftChelaCheckBox);
407 layout->addWidget(rightChelaDial);
408 layout->addWidget(rightChelaCheckBox);
409 layout->addWidget(leftBeltDial);
410 layout->addWidget(leftBeltCheckBox);
411 layout->addWidget(rightBeltDial);
412 layout->addWidget(rightBeltCheckBox);
413 layout->addWidget(pickPushButton);
415 pickerGroupBox->setLayout(layout);
418 void RobomonAtlantis::createRobots()
420 robotActPos = new Robot(QPen(Qt::darkBlue), QBrush(Qt::NoBrush));
421 robotActPos->setZValue(11);
422 robotEstPosOdo = new Robot(QPen(Qt::white), QBrush(Qt::darkRed));
423 robotEstPosOdo->setZValue(10);
424 robotEstPosUzv = new Robot(QPen(), QBrush(Qt::darkGray));
425 robotEstPosUzv->setZValue(10);
427 playgroundScene->addItem(robotActPos);
428 playgroundScene->addItem(robotEstPosUzv);
429 playgroundScene->addItem(robotEstPosOdo);
432 /**********************************************************************
434 **********************************************************************/
435 void RobomonAtlantis::createActions()
437 /* power management */
438 connect(voltage33CheckBox, SIGNAL(stateChanged(int)),
439 this, SLOT(setVoltage33(int)));
440 connect(voltage50CheckBox, SIGNAL(stateChanged(int)),
441 this, SLOT(setVoltage50(int)));
442 connect(voltage80CheckBox, SIGNAL(stateChanged(int)),
443 this, SLOT(setVoltage80(int)));
446 connect(leftMotorSlider, SIGNAL(valueChanged(int)),
447 this, SLOT(setLeftMotor(int)));
448 connect(rightMotorSlider, SIGNAL(valueChanged(int)),
449 this, SLOT(setRightMotor(int)));
450 connect(stopMotorsPushButton, SIGNAL(clicked()),
451 this, SLOT(stopMotors()));
454 for (int i=0; i<8; i++)
455 connect(doCheckBox[0], SIGNAL(stateChanged(int)),
456 this, SLOT(setDO(int)));
458 /* path planning map */
459 connect(showMapPushButton, SIGNAL(clicked()),
460 this, SLOT(showMap()));
462 connect(startPlug, SIGNAL(stateChanged(int)), this, SLOT(sendStart(int)));
463 connect(puckInside, SIGNAL(stateChanged(int)), this, SLOT(sendPuckInside(int)));
465 /* obstacle simulation */
466 simulationEnabled = 0;
467 connect(obstacleSimulationCheckBox, SIGNAL(stateChanged(int)),
468 this, SLOT(setSimulation(int)));
469 connect(obstacleSimulationCheckBox, SIGNAL(stateChanged(int)),
470 this, SLOT(setObstacleSimulation(int)));
471 connect(obstacleSimulationCheckBox, SIGNAL(stateChanged(int)),
472 playgroundScene, SLOT(showObstacle(int)));
473 connect(playgroundScene, SIGNAL(obstacleChanged(QPointF)),
474 this, SLOT(changeObstacle(QPointF)));
477 connect(pickPushButton, SIGNAL(clicked()), this, SLOT(pick()));
478 connect(leftBeltCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setBelts(int)));
479 connect(rightBeltCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setBelts(int)));
480 connect(leftChelaCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setChelae(int)));
481 connect(rightChelaCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setChelae(int)));
482 connect(leftBeltDial, SIGNAL(valueChanged(int)), this, SLOT(setBelts(int)));
483 connect(rightBeltDial, SIGNAL(valueChanged(int)), this, SLOT(setBelts(int)));
484 connect(leftChelaDial, SIGNAL(valueChanged(int)), this, SLOT(setChelae(int)));
485 connect(rightChelaDial, SIGNAL(valueChanged(int)), this, SLOT(setChelae(int)));
488 void RobomonAtlantis::setVoltage33(int state)
491 orte.pwr_ctrl.voltage33 = true;
493 orte.pwr_ctrl.voltage33 = false;
496 void RobomonAtlantis::setVoltage50(int state)
499 orte.pwr_ctrl.voltage50 = true;
501 orte.pwr_ctrl.voltage50 = false;
504 void RobomonAtlantis::setVoltage80(int state)
507 orte.pwr_ctrl.voltage80 = true;
509 orte.pwr_ctrl.voltage80 = false;
512 void RobomonAtlantis::setLeftMotor(int value)
515 short int rightMotor;
517 if(bothMotorsCheckBox->isChecked())
518 rightMotorSlider->setValue(value);
520 leftMotor = (short int)(MOTOR_LIMIT * (leftMotorSlider->value()/100.0));
521 rightMotor = (short int)(MOTOR_LIMIT * (rightMotorSlider->value()/100.0));
523 orte.motion_speed.left = leftMotor;
524 orte.motion_speed.right = rightMotor;
528 void RobomonAtlantis::setRightMotor(int value)
531 short int rightMotor;
533 if(bothMotorsCheckBox->isChecked())
534 leftMotorSlider->setValue(value);
536 leftMotor = (short int)(MOTOR_LIMIT * (leftMotorSlider->value()/100.0));
537 rightMotor = (short int)(MOTOR_LIMIT * (rightMotorSlider->value()/100.0));
539 orte.motion_speed.left = leftMotor;
540 orte.motion_speed.right = rightMotor;
544 void RobomonAtlantis::stopMotors()
546 leftMotorSlider->setValue(0);
547 rightMotorSlider->setValue(0);
551 void RobomonAtlantis::setDO(int state)
554 /* FIXME: digital output control comes here */
557 void RobomonAtlantis::pick()
559 // TODO: send signal to the fsmact
562 void RobomonAtlantis::setBelts(int value)
565 unsigned char leftBelt;
566 unsigned char rightBelt;
568 leftBelt = (unsigned char)leftBeltDial->value();
569 rightBelt = (unsigned char)rightBeltDial->value();
571 act_belts(leftBelt, rightBelt);
574 void RobomonAtlantis::setChelae(int value)
577 unsigned char leftChela;
578 unsigned char rightChela;
580 leftChela = (unsigned char)leftChelaDial->value();
581 rightChela = (unsigned char)rightChelaDial->value();
583 act_chelae(leftChela, rightChela);
586 void RobomonAtlantis::showMap()
590 if (sharedMemoryOpened == false)
593 mapTimer = new QTimer(this);
594 connect(mapTimer, SIGNAL(timeout()), this, SLOT(paintMap()));
595 mapTimer->start(200);
597 disconnect(showMapPushButton, SIGNAL(clicked()), this, SLOT(showMap()));
598 connect(showMapPushButton, SIGNAL(clicked()),
599 this, SLOT(showPlayground()));
600 playgroundScene->showMap(true);
603 void RobomonAtlantis::showPlayground()
606 disconnect(mapTimer, SIGNAL(timeout()), this, SLOT(paintMap()));
608 disconnect(showMapPushButton, SIGNAL(clicked()),
609 this, SLOT(showPlayground()));
610 connect(showMapPushButton, SIGNAL(clicked()), this, SLOT(showMap()));
611 playgroundScene->showMap(false);
614 void RobomonAtlantis::paintMap()
617 struct map *map = ShmapIsMapInit();
621 for(int i=0; i < MAP_WIDTH; i++) {
622 for(int j=0; j<MAP_HEIGHT; j++) {
625 struct map_cell *cell = &map->cells[j][i];
628 if ((cell->flags & MAP_FLAG_WALL) &&
629 (cell->flags & MAP_FLAG_INVALIDATE_WALL) == 0)
631 if (cell->flags & MAP_FLAG_IGNORE_OBST)
633 if (cell->flags & MAP_FLAG_SIMULATED_WALL)
635 if (cell->flags & MAP_FLAG_PATH)
637 if (cell->flags & MAP_FLAG_START)
639 if (cell->flags & MAP_FLAG_GOAL)
641 if (cell->detected_obstacle) {
642 QColor c1(color), c2(blue);
643 double f = (double)cell->detected_obstacle/MAP_NEW_OBSTACLE*0.7;
644 QColor c(c1.red() + (int)(f*(c2.red() - c1.red())),
645 c1.green() + (int)(f*(c2.green() - c1.green())),
646 c1.blue() + (int)(f*(c2.blue() - c1.blue())));
649 if (cell->flags & MAP_FLAG_DET_OBST)
652 playgroundScene->setMapColor(i, j, color);
657 void RobomonAtlantis::setSimulation(int state)
660 robottype_publisher_hokuyo_scan_create(&orte,
661 dummy_publisher_callback, this);
663 if (!simulationEnabled)
665 robottype_publisher_hokuyo_scan_destroy(&orte);
667 simulationEnabled = state;
671 \fn RobomonAtlantis::setObstacleSimulation(int state)
673 void RobomonAtlantis::setObstacleSimulation(int state)
676 /* TODO Maybe it is possible to attach only once to Shmap */
678 obstacleSimulationTimer = new QTimer(this);
679 connect(obstacleSimulationTimer, SIGNAL(timeout()),
680 this, SLOT(simulateObstaclesHokuyo()));
681 obstacleSimulationTimer->start(100);
682 setMouseTracking(true);
684 if (obstacleSimulationTimer)
685 delete obstacleSimulationTimer;
686 //double distance = 0.8;
691 void RobomonAtlantis::simulateObstaclesHokuyo()
693 double distance, wall_distance;
695 uint16_t *hokuyo = orte.hokuyo_scan.data;
697 for (i=0; i<HOKUYO_CLUSTER_CNT; i++) {
698 wall_distance = distanceToWallHokuyo(i);
699 distance = distanceToObstacleHokuyo(i, simulatedObstacle, SIM_OBST_SIZE_M/*meters*/);
700 if (wall_distance < distance)
701 distance = wall_distance;
702 hokuyo[i] = distance*1000;
704 ORTEPublicationSend(orte.publication_hokuyo_scan);
708 void RobomonAtlantis::changeObstacle(QPointF position)
710 if (!simulationEnabled) {
711 simulationEnabled = 1;
712 obstacleSimulationCheckBox->setChecked(true);
715 simulatedObstacle.x = position.x();
716 simulatedObstacle.y = position.y();
717 simulateObstaclesHokuyo();
720 /**********************************************************************
722 **********************************************************************/
723 bool RobomonAtlantis::event(QEvent *event)
725 switch (event->type()) {
726 case QEVENT(QEV_MOTION_STATUS):
727 emit motionStatusReceivedSignal();
729 case QEVENT(QEV_REFERENCE_POSITION):
730 emit actualPositionReceivedSignal();
732 case QEVENT(QEV_ESTIMATED_POSITION_UZV):
733 emit estimatedPositionReceivedSignal();
735 case QEVENT(QEV_ESTIMATED_POSITION_ODO):
736 robotEstPosOdo->moveRobot(orte.est_pos_odo.x,
737 orte.est_pos_odo.y, orte.est_pos_odo.phi);
740 emit diReceivedSignal();
742 case QEVENT(QEV_ACCELEROMETER):
743 emit accelerometerReceivedSignal();
745 case QEVENT(QEV_ACCUMULATOR):
746 emit accumulatorReceivedSignal();
748 case QEVENT(QEV_POWER_VOLTAGE):
749 emit powerVoltageReceivedSignal();
751 case QEVENT(QEV_FSM_MAIN):
752 fsm_main_state->setText(orte.fsm_main.state_name);
754 case QEVENT(QEV_FSM_ACT):
755 fsm_act_state->setText(orte.fsm_act.state_name);
757 case QEVENT(QEV_FSM_MOTION):
758 fsm_motion_state->setText(orte.fsm_motion.state_name);
760 case QEVENT(QEV_PUCK_DISTANCE):
761 sharpPuck->setValue(orte.puck_distance.distance*1000);
763 case QEVENT(QEV_LIFT_PUSHER):
764 liftPos->setValue(orte.actuator_status.lift_pos);
765 pusherPos->setValue(orte.actuator_status.pusher_pos);
768 if (event->type() == QEvent::Close)
769 closeEvent((QCloseEvent *)event);
770 else if (event->type() == QEvent::KeyPress)
771 keyPressEvent((QKeyEvent *)event);
772 else if (event->type() == QEvent::KeyRelease)
773 keyReleaseEvent((QKeyEvent *)event);
774 else if (event->type() == QEvent::FocusIn)
776 else if (event->type() == QEvent::FocusOut)
788 void RobomonAtlantis::keyPressEvent(QKeyEvent *event)
792 if (event->isAutoRepeat()) {
793 switch (event->key()) {
795 peak = leftMotorSlider->minimum()/2;
796 if (leftMotorValue < peak ||
797 rightMotorValue < peak)
801 leftMotorValue *= gain;
802 rightMotorValue *= gain;
803 leftMotorSlider->setValue((int)leftMotorValue);
804 rightMotorSlider->setValue((int)rightMotorValue);
810 peak = leftMotorSlider->maximum()/2;
811 if (leftMotorValue > peak ||
812 rightMotorValue > peak)
816 leftMotorValue *= gain;
817 rightMotorValue *= gain;
818 leftMotorSlider->setValue((int)leftMotorValue);
819 rightMotorSlider->setValue((int)rightMotorValue);
829 switch (event->key()) {
833 bothMotorsCheckBox->setChecked(true);
834 leftMotorSlider->setValue((int)leftMotorValue);
835 setLeftMotor((int)leftMotorValue);
839 rightMotorValue = -1;
840 bothMotorsCheckBox->setChecked(true);
841 leftMotorSlider->setValue((int)leftMotorValue);
842 setLeftMotor((int)leftMotorValue);
847 leftMotorSlider->setValue((int)leftMotorValue);
848 rightMotorSlider->setValue((int)rightMotorValue);
849 setLeftMotor((int)leftMotorValue);
850 setRightMotor((int)leftMotorValue);
854 rightMotorValue = -1;
855 leftMotorSlider->setValue((int)leftMotorValue);
856 rightMotorSlider->setValue((int)rightMotorValue);
857 setLeftMotor((int)leftMotorValue);
858 setRightMotor((int)rightMotorValue);
867 void RobomonAtlantis::keyReleaseEvent(QKeyEvent *event)
869 if (event->isAutoRepeat()) {
874 switch (event->key()) {
881 bothMotorsCheckBox->setChecked(false);
882 leftMotorSlider->setValue((int)leftMotorValue);
883 rightMotorSlider->setValue((int)rightMotorValue);
892 void RobomonAtlantis::closeEvent(QCloseEvent *)
894 robottype_roboorte_destroy(&orte);
897 /**********************************************************************
899 **********************************************************************/
900 void RobomonAtlantis::createOrte()
906 rv = robottype_roboorte_init(&orte);
908 printf("RobomonAtlantis: Unable to initialize ORTE\n");
912 robottype_publisher_motion_speed_create(&orte, dummy_publisher_callback, NULL);
914 robottype_publisher_pwr_ctrl_create(&orte, dummy_publisher_callback, NULL);
915 robottype_publisher_lift_create(&orte, NULL, &orte);
916 robottype_publisher_pusher_create(&orte, NULL, &orte);
917 robottype_publisher_chelae_create(&orte, NULL, &orte);
918 robottype_publisher_belts_create(&orte, NULL, &orte);
919 robottype_publisher_holder_create(&orte, NULL, &orte);
920 robottype_publisher_robot_cmd_create(&orte, NULL, &orte);
921 robottype_publisher_puck_inside_create(&orte, NULL, &orte);
924 robottype_subscriber_pwr_voltage_create(&orte,
925 receivePowerVoltageCallBack, this);
926 robottype_subscriber_motion_status_create(&orte,
927 receiveMotionStatusCallBack, this);
928 robottype_subscriber_ref_pos_create(&orte,
929 receiveActualPositionCallBack, this);
930 robottype_subscriber_est_pos_uzv_create(&orte,
931 generic_rcv_cb, new OrteCallbackInfo(this, QEV_ESTIMATED_POSITION_UZV));
932 robottype_subscriber_est_pos_odo_create(&orte,
933 generic_rcv_cb, new OrteCallbackInfo(this, QEV_ESTIMATED_POSITION_ODO));
934 robottype_subscriber_fsm_main_create(&orte,
935 rcv_fsm_main_cb, this);
936 robottype_subscriber_fsm_motion_create(&orte,
937 rcv_fsm_motion_cb, this);
938 robottype_subscriber_fsm_act_create(&orte,
939 rcv_fsm_act_cb, this);
940 robottype_subscriber_puck_distance_create(&orte, generic_rcv_cb, new OrteCallbackInfo(this, QEV_PUCK_DISTANCE));
941 robottype_subscriber_actuator_status_create(&orte, generic_rcv_cb, new OrteCallbackInfo(this, QEV_LIFT_PUSHER));
944 /*createDISubscriber(this, &orteData);*/
945 /*createAccelerometerSubscriber(this, &orteData);*/
946 /*createAccumulatorSubscriber(this, &orteData);*/
948 orte.motion_speed.left = 0;
949 orte.motion_speed.right = 0;
950 /* power management */
951 orte.pwr_ctrl.voltage33 = true;
952 orte.pwr_ctrl.voltage50 = true;
953 orte.pwr_ctrl.voltage80 = true;
954 voltage33CheckBox->setChecked(true);
955 voltage50CheckBox->setChecked(true);
956 voltage80CheckBox->setChecked(true);
960 /* set actions to do when we receive data from orte */
961 connect(this, SIGNAL(motionStatusReceivedSignal()),
962 this, SLOT(motionStatusReceived()));
963 connect(this, SIGNAL(actualPositionReceivedSignal()),
964 this, SLOT(actualPositionReceived()));
965 connect(this, SIGNAL(estimatedPositionReceivedSignal()),
966 this, SLOT(estimatedPositionReceived()));
967 connect(this, SIGNAL(diReceivedSignal()),
968 this, SLOT(diReceived()));
969 connect(this, SIGNAL(accelerometerReceivedSignal()),
970 this, SLOT(accelerometerReceived()));
971 connect(this, SIGNAL(accumulatorReceivedSignal()),
972 this, SLOT(accumulatorReceived()));
973 connect(this, SIGNAL(powerVoltageReceivedSignal()),
974 this, SLOT(powerVoltageReceived()));
977 void RobomonAtlantis::motionStatusReceived()
979 WDBG("ORTE received: motion status");
982 void RobomonAtlantis::actualPositionReceived()
984 actPosX->setText(QString("%1").arg(orte.ref_pos.x, 0, 'f', 3));
985 actPosY->setText(QString("%1").arg(orte.ref_pos.y, 0, 'f', 3));
986 actPosPhi->setText(QString("%1(%2)")
987 .arg(DEGREES(orte.ref_pos.phi), 0, 'f', 0)
988 .arg(orte.ref_pos.phi, 0, 'f', 1));
989 robotActPos->moveRobot(orte.ref_pos.x,
990 orte.ref_pos.y, orte.ref_pos.phi);
993 void RobomonAtlantis::estimatedPositionReceived()
995 estPosX->setText(QString("%1").arg(orte.est_pos_uzv.x, 0, 'f', 3));
996 estPosY->setText(QString("%1").arg(orte.est_pos_uzv.y, 0, 'f', 3));
997 estPosPhi->setText(QString("%1(%2)")
998 .arg(DEGREES(orte.est_pos_uzv.phi), 0, 'f', 0)
999 .arg(orte.est_pos_uzv.phi, 0, 'f', 1));
1000 robotEstPosUzv->moveRobot(orte.est_pos_uzv.x,
1001 orte.est_pos_uzv.y, orte.est_pos_uzv.phi);
1004 void RobomonAtlantis::diReceived()
1006 WDBG("ORTE received: DI");
1009 void RobomonAtlantis::accelerometerReceived()
1011 WDBG("ORTE received: accelerometer");
1014 void RobomonAtlantis::accumulatorReceived()
1016 WDBG("ORTE received: accumulator");
1019 void RobomonAtlantis::powerVoltageReceived()
1021 voltage33LineEdit->setText(QString("%1").arg(
1022 orte.pwr_voltage.voltage33, 0, 'f', 3));
1023 voltage50LineEdit->setText(QString("%1").arg(
1024 orte.pwr_voltage.voltage50, 0, 'f', 3));
1025 voltage80LineEdit->setText(QString("%1").arg(
1026 orte.pwr_voltage.voltage80, 0, 'f', 3));
1027 voltageBATLineEdit->setText(QString("%1").arg(
1028 orte.pwr_voltage.voltageBAT, 0, 'f', 3));
1032 /**********************************************************************
1034 **********************************************************************/
1035 void RobomonAtlantis::openSharedMemory()
1038 int sharedSegmentSize;
1040 if (sharedMemoryOpened)
1043 sharedSegmentSize = sizeof(unsigned int) * MAP_WIDTH * MAP_HEIGHT;
1045 /* Get segment identificator in a read only mode */
1046 segmentId = shmget(SHM_MAP_KEY, sharedSegmentSize, S_IRUSR);
1047 if(segmentId == -1) {
1048 QMessageBox::critical(this, "robomon",
1049 "Unable to open shared memory segment!");
1056 /* Attach the shared memory segment */
1057 //map = (_Map*)shmat (segmentId, (void*) 0, 0);
1059 sharedMemoryOpened = true;
1062 double RobomonAtlantis::distanceToWallHokuyo(int beamnum)
1064 double distance=4.0, min_distance=4.0;
1067 struct map *map = ShmapIsMapInit();
1069 if (!map) return min_distance;
1071 // Simulate obstacles
1072 for(j=0;j<MAP_HEIGHT;j++) {
1073 for (i=0;i<MAP_WIDTH;i++) {
1074 struct map_cell *cell = &map->cells[j][i];
1075 if( cell->flags & MAP_FLAG_SIMULATED_WALL) {
1077 ShmapCell2Point(i, j, &wall.x, &wall.y);
1079 distance = distanceToObstacleHokuyo(beamnum, wall, MAP_CELL_SIZE_M);
1080 if (distance<min_distance) min_distance = distance;
1085 return min_distance;
1089 * Calculation for Hokuyo simulation. Calculates distance that would
1090 * be returned by Hokuyo sensors, if there is only one obstacle (as
1091 * specified by parameters).
1093 * @param beamnum Hokuyo's bean number [0..HOKUYO_CLUSTER_CNT]
1094 * @param obstacle Position of the obstacle (x, y in meters).
1095 * @param obstacleSize Size (diameter) of the obstacle in meters.
1097 * @return Distance measured by sensors in meters.
1099 double RobomonAtlantis::distanceToObstacleHokuyo(int beamnum, Point obstacle, double obstacleSize)
1101 struct robot_pos_type e = orte.est_pos_uzv;
1105 s.x = HOKUYO_CENTER_OFFSET_M;
1107 s.ang = HOKUYO_CLUSTER_TO_RAD(beamnum);
1109 Point sensor(e.x + s.x*cos(e.phi) - s.y*sin(e.phi),
1110 e.y + s.x*sin(e.phi) + s.y*cos(e.phi));
1111 sensor_a = e.phi + s.ang;
1113 const double sensorRange = 4.0; /*[meters]*/
1115 double distance, angle;
1117 angle = sensor.angleTo(obstacle) - sensor_a;
1118 angle = fmod(angle, 2.0*M_PI);
1119 if (angle > +M_PI) angle -= 2.0*M_PI;
1120 if (angle < -M_PI) angle += 2.0*M_PI;
1121 angle = fabs(angle);
1122 distance = sensor.distanceTo(obstacle)-0.11;
1123 if (angle < atan(obstacleSize/2.0 / (distance+0.001))) {
1124 // We can see the obstackle from here.
1125 if (angle < M_PI/2.0) {
1126 distance = distance/cos(angle);
1128 if (distance > sensorRange)
1129 distance = sensorRange;
1131 distance = sensorRange;
1137 void RobomonAtlantis::sendStart(int plug)
1139 orte.robot_cmd.start = plug ? 0 : 1;
1140 ORTEPublicationSend(orte.publication_robot_cmd);
1143 void RobomonAtlantis::sendPuckInside(int value)
1145 orte.puck_inside.status = value ? 1 : 0;
1146 ORTEPublicationSend(orte.publication_puck_inside);