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 obstacleSimulationCheckBox = new QCheckBox(tr("&Obstacle simulation"));
181 obstacleSimulationCheckBox->setShortcut(tr("o"));
182 layout->addWidget(obstacleSimulationCheckBox);
184 startPlug = new QCheckBox("Start plug");
185 layout->addWidget(startPlug);
187 puckInside = new QCheckBox("Puck inside");
188 layout->addWidget(puckInside);
190 miscGroupBox->setLayout(layout);
193 void RobomonAtlantis::createFSMGroupBox()
195 fsmGroupBox = new QGroupBox(tr("FSM"));
196 fsmGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
197 QGridLayout *layout = new QGridLayout();
199 layout->addWidget(MiscGui::createLabel("Main:"), 1, 0);
200 fsm_main_state = new QLabel();
201 fsm_main_state->setMinimumWidth(100);
202 layout->addWidget(fsm_main_state, 1, 1);
204 layout->addWidget(MiscGui::createLabel("Act:"), 2, 0);
205 fsm_act_state = new QLabel();
206 layout->addWidget(fsm_act_state, 2, 1);
208 layout->addWidget(MiscGui::createLabel("Motion:"), 3, 0);
209 fsm_motion_state = new QLabel();
210 layout->addWidget(fsm_motion_state, 3, 1);
212 fsmGroupBox->setLayout(layout);
215 void RobomonAtlantis::createDebugGroupBox()
217 debugGroupBox = new QGroupBox(tr("Debug window"));
218 debugGroupBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
219 QHBoxLayout *layout = new QHBoxLayout();
221 debugWindow = new QTextEdit();
222 debugWindow->setReadOnly(true);
224 layout->addWidget(debugWindow);
225 debugGroupBox->setLayout(layout);
228 void RobomonAtlantis::createActuatorsGroupBox()
230 actuatorsGroupBox = new QGroupBox(tr("Actuators"));
231 actuatorsGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
232 QHBoxLayout *layout = new QHBoxLayout();
234 createMotorsGroupBox();
235 createPickerGroupBox();
237 layout->setAlignment(Qt::AlignLeft);
238 layout->addWidget(enginesGroupBox);
239 layout->addWidget(pickerGroupBox);
240 actuatorsGroupBox->setLayout(layout);
243 void RobomonAtlantis::createDIOGroupBox()
245 dioGroupBox = new QGroupBox(tr("DIO"));
246 QGridLayout *layout = new QGridLayout();
249 font.setPointSize(5);
250 dioGroupBox->setFont(font);
252 for (int i=0; i<8; i++) {
253 diCheckBox[i] = new QCheckBox(QString("DI%1").arg(i));
254 doCheckBox[i] = new QCheckBox(QString("D0%1").arg(i));
255 layout->addWidget(diCheckBox[i], i, 0);
256 layout->addWidget(doCheckBox[i], i+8, 0);
259 dioGroupBox->setMaximumWidth(70);
260 dioGroupBox->setLayout(layout);
263 void RobomonAtlantis::createPowerGroupBox()
265 powerGroupBox = new QGroupBox(tr("Power management"));
266 powerGroupBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
267 powerGroupBox->setMaximumWidth(450);
268 QGridLayout *layout = new QGridLayout();
270 voltage33CheckBox = new QCheckBox(tr("&3.3V"));
271 voltage50CheckBox = new QCheckBox(tr("&5.0V"));
272 voltage80CheckBox = new QCheckBox(tr("&8.0V"));
274 voltage33CheckBox->setShortcut(tr("3"));
275 voltage50CheckBox->setShortcut(tr("5"));
276 voltage80CheckBox->setShortcut(tr("8"));
278 layout->addWidget(voltage33CheckBox, 0, 0);
279 layout->addWidget(voltage50CheckBox, 0, 2);
280 layout->addWidget(voltage80CheckBox, 0, 4); //1, 0);
281 layout->addWidget(MiscGui::createLabel("BAT"), 0, 6); //1, 2);
283 voltage33LineEdit = new QLineEdit();
284 voltage50LineEdit = new QLineEdit();
285 voltage80LineEdit = new QLineEdit();
286 voltageBATLineEdit = new QLineEdit();
288 voltage33LineEdit->setReadOnly(true);
289 voltage50LineEdit->setReadOnly(true);
290 voltage80LineEdit->setReadOnly(true);
291 voltageBATLineEdit->setReadOnly(true);
293 layout->addWidget(voltage33LineEdit, 0, 1);
294 layout->addWidget(voltage50LineEdit, 0, 3);
295 layout->addWidget(voltage80LineEdit, 0, 5); //1, 1);
296 layout->addWidget(voltageBATLineEdit, 0, 7); //1, 3);
298 powerGroupBox->setLayout(layout);
301 void RobomonAtlantis::createSensorsGroupBox()
303 sensorsGroupBox = new QGroupBox(tr("Sensors"));
304 sensorsGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
305 sensorsGroupBox->setMaximumWidth(450);
306 QHBoxLayout *layout = new QHBoxLayout();
308 layout->addWidget(MiscGui::createLabel("Puck Sharp"));
309 sharpPuck = new QProgressBar();
310 sharpPuck->setFormat("%v mm");
311 sharpPuck->setTextVisible(true);
312 sharpPuck->setMaximum(250);
313 layout->addWidget(sharpPuck);
315 layout->addWidget(MiscGui::createLabel("Lift"));
316 liftPos = new QProgressBar();
317 liftPos->setFormat("%v IRC");
318 liftPos->setTextVisible(true);
319 liftPos->setMaximum(1000);
320 layout->addWidget(liftPos);
322 layout->addWidget(MiscGui::createLabel("Pusher"));
323 pusherPos = new QProgressBar();
324 pusherPos->setFormat("%v IRC");
325 pusherPos->setTextVisible(true);
326 pusherPos->setMaximum(250);
327 layout->addWidget(pusherPos);
329 sensorsGroupBox->setLayout(layout);
332 void RobomonAtlantis::createMotorsGroupBox()
334 enginesGroupBox = new QGroupBox(tr("Motors"));
335 QVBoxLayout *layout = new QVBoxLayout();
336 QHBoxLayout *layout1 = new QHBoxLayout();
337 QHBoxLayout *layout2 = new QHBoxLayout();
339 leftMotorSlider = new QSlider(Qt::Vertical);
340 rightMotorSlider = new QSlider(Qt::Vertical);
341 bothMotorsCheckBox = new QCheckBox(tr("Lock both"));
342 stopMotorsPushButton = new QPushButton(tr("Stop Motors"));
344 leftMotorSlider->setMinimum(-100);
345 leftMotorSlider->setMaximum(100);
346 leftMotorSlider->setTracking(false);
347 leftMotorSlider->setTickPosition(QSlider::TicksLeft);
349 rightMotorSlider->setMinimum(-100);
350 rightMotorSlider->setMaximum(100);
351 rightMotorSlider->setTracking(false);
352 rightMotorSlider->setTickPosition(QSlider::TicksRight);
354 stopMotorsPushButton->setMaximumWidth(90);
356 layout1->addWidget(leftMotorSlider);
357 layout1->addWidget(MiscGui::createLabel("0"));
358 layout1->addWidget(rightMotorSlider);
360 layout2->addWidget(bothMotorsCheckBox);
362 layout->addWidget(MiscGui::createLabel("100"));
363 layout->addLayout(layout1);
364 layout->addWidget(MiscGui::createLabel("-100"));
365 layout->addLayout(layout2);
366 layout->addWidget(stopMotorsPushButton);
367 enginesGroupBox->setLayout(layout);
370 void RobomonAtlantis::createPickerGroupBox()
372 pickerGroupBox = new QGroupBox(tr("Picker"));
373 QVBoxLayout *layout = new QVBoxLayout();
375 leftBeltCheckBox = new QCheckBox(tr("L Belt"));
376 rightBeltCheckBox = new QCheckBox(tr("R Belt"));
377 leftChelaCheckBox = new QCheckBox(tr("L Chela"));
378 rightChelaCheckBox = new QCheckBox(tr("R Chela"));
379 leftBeltDial = new QDial();
380 rightBeltDial = new QDial();
381 leftChelaDial = new QDial();
382 rightChelaDial = new QDial();
383 pickPushButton = new QPushButton(tr("Pick!"));
385 leftBeltDial->setMinimum(0);
386 leftBeltDial->setMaximum(200);
387 leftBeltDial->setValue(100);
389 rightBeltDial->setMinimum(0);
390 rightBeltDial->setMaximum(200);
391 rightBeltDial->setValue(100);
393 leftChelaDial->setMinimum(0);
394 leftChelaDial->setMaximum(255);
395 leftChelaDial->setValue(127);
397 rightChelaDial->setMinimum(0);
398 rightChelaDial->setMaximum(255);
399 rightChelaDial->setValue(127);
401 layout->addWidget(leftChelaDial);
402 layout->addWidget(leftChelaCheckBox);
403 layout->addWidget(rightChelaDial);
404 layout->addWidget(rightChelaCheckBox);
405 layout->addWidget(leftBeltDial);
406 layout->addWidget(leftBeltCheckBox);
407 layout->addWidget(rightBeltDial);
408 layout->addWidget(rightBeltCheckBox);
409 layout->addWidget(pickPushButton);
411 pickerGroupBox->setLayout(layout);
414 void RobomonAtlantis::createRobots()
416 robotActPos = new Robot("Ref", QPen(Qt::darkBlue), QBrush(Qt::NoBrush));
417 robotActPos->setZValue(11);
418 trailRefPos = new Trail(QPen(Qt::darkBlue));
419 trailRefPos->setZValue(11);
421 robotEstPosOdo = new Robot("Odo", QPen(Qt::white), QBrush(Qt::darkRed));
422 robotEstPosOdo->setZValue(10);
423 trailOdoPos = new Trail(QPen(Qt::white));
424 trailOdoPos->setZValue(10);
425 robotEstPosUzv = new Robot("Uzv", QPen(), QBrush(Qt::darkGray));
426 robotEstPosUzv->setZValue(10);
427 trailUzvPos = new Trail(QPen());
428 trailUzvPos->setZValue(10);
430 playgroundScene->addItem(robotActPos);
431 playgroundScene->addItem(robotEstPosUzv);
432 playgroundScene->addItem(robotEstPosOdo);
436 playgroundScene->addItem(trailRefPos);
437 playgroundScene->addItem(trailUzvPos);
438 playgroundScene->addItem(trailOdoPos);
441 /**********************************************************************
443 **********************************************************************/
444 void RobomonAtlantis::createActions()
446 /* power management */
447 connect(voltage33CheckBox, SIGNAL(stateChanged(int)),
448 this, SLOT(setVoltage33(int)));
449 connect(voltage50CheckBox, SIGNAL(stateChanged(int)),
450 this, SLOT(setVoltage50(int)));
451 connect(voltage80CheckBox, SIGNAL(stateChanged(int)),
452 this, SLOT(setVoltage80(int)));
455 connect(leftMotorSlider, SIGNAL(valueChanged(int)),
456 this, SLOT(setLeftMotor(int)));
457 connect(rightMotorSlider, SIGNAL(valueChanged(int)),
458 this, SLOT(setRightMotor(int)));
459 connect(stopMotorsPushButton, SIGNAL(clicked()),
460 this, SLOT(stopMotors()));
463 for (int i=0; i<8; i++)
464 connect(doCheckBox[0], SIGNAL(stateChanged(int)),
465 this, SLOT(setDO(int)));
467 connect(startPlug, SIGNAL(stateChanged(int)), this, SLOT(sendStart(int)));
468 connect(puckInside, SIGNAL(stateChanged(int)), this, SLOT(sendPuckInside(int)));
470 /* obstacle simulation */
471 simulationEnabled = 0;
472 connect(obstacleSimulationCheckBox, SIGNAL(stateChanged(int)),
473 this, SLOT(setSimulation(int)));
474 connect(obstacleSimulationCheckBox, SIGNAL(stateChanged(int)),
475 this, SLOT(setObstacleSimulation(int)));
476 connect(obstacleSimulationCheckBox, SIGNAL(stateChanged(int)),
477 playgroundScene, SLOT(showObstacle(int)));
478 connect(playgroundScene, SIGNAL(obstacleChanged(QPointF)),
479 this, SLOT(changeObstacle(QPointF)));
482 connect(pickPushButton, SIGNAL(clicked()), this, SLOT(pick()));
483 connect(leftBeltCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setBelts(int)));
484 connect(rightBeltCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setBelts(int)));
485 connect(leftChelaCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setChelae(int)));
486 connect(rightChelaCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setChelae(int)));
487 connect(leftBeltDial, SIGNAL(valueChanged(int)), this, SLOT(setBelts(int)));
488 connect(rightBeltDial, SIGNAL(valueChanged(int)), this, SLOT(setBelts(int)));
489 connect(leftChelaDial, SIGNAL(valueChanged(int)), this, SLOT(setChelae(int)));
490 connect(rightChelaDial, SIGNAL(valueChanged(int)), this, SLOT(setChelae(int)));
493 void RobomonAtlantis::setVoltage33(int state)
496 orte.pwr_ctrl.voltage33 = true;
498 orte.pwr_ctrl.voltage33 = false;
501 void RobomonAtlantis::setVoltage50(int state)
504 orte.pwr_ctrl.voltage50 = true;
506 orte.pwr_ctrl.voltage50 = false;
509 void RobomonAtlantis::setVoltage80(int state)
512 orte.pwr_ctrl.voltage80 = true;
514 orte.pwr_ctrl.voltage80 = false;
517 void RobomonAtlantis::setLeftMotor(int value)
520 short int rightMotor;
522 if(bothMotorsCheckBox->isChecked())
523 rightMotorSlider->setValue(value);
525 leftMotor = (short int)(MOTOR_LIMIT * (leftMotorSlider->value()/100.0));
526 rightMotor = (short int)(MOTOR_LIMIT * (rightMotorSlider->value()/100.0));
528 orte.motion_speed.left = leftMotor;
529 orte.motion_speed.right = rightMotor;
533 void RobomonAtlantis::setRightMotor(int value)
536 short int rightMotor;
538 if(bothMotorsCheckBox->isChecked())
539 leftMotorSlider->setValue(value);
541 leftMotor = (short int)(MOTOR_LIMIT * (leftMotorSlider->value()/100.0));
542 rightMotor = (short int)(MOTOR_LIMIT * (rightMotorSlider->value()/100.0));
544 orte.motion_speed.left = leftMotor;
545 orte.motion_speed.right = rightMotor;
549 void RobomonAtlantis::stopMotors()
551 leftMotorSlider->setValue(0);
552 rightMotorSlider->setValue(0);
556 void RobomonAtlantis::setDO(int state)
559 /* FIXME: digital output control comes here */
562 void RobomonAtlantis::pick()
564 // TODO: send signal to the fsmact
567 void RobomonAtlantis::setBelts(int value)
570 unsigned char leftBelt;
571 unsigned char rightBelt;
573 leftBelt = (unsigned char)leftBeltDial->value();
574 rightBelt = (unsigned char)rightBeltDial->value();
576 act_belts(leftBelt, rightBelt);
579 void RobomonAtlantis::setChelae(int value)
582 unsigned char leftChela;
583 unsigned char rightChela;
585 leftChela = (unsigned char)leftChelaDial->value();
586 rightChela = (unsigned char)rightChelaDial->value();
588 act_chelae(leftChela, rightChela);
591 void RobomonAtlantis::showMap(bool show)
595 if (sharedMemoryOpened == false)
599 mapTimer = new QTimer(this);
600 connect(mapTimer, SIGNAL(timeout()), this, SLOT(paintMap()));
601 mapTimer->start(200);
604 disconnect(mapTimer, SIGNAL(timeout()), this, SLOT(paintMap()));
606 playgroundScene->showMap(show);
609 void RobomonAtlantis::paintMap()
612 struct map *map = ShmapIsMapInit();
616 for(int i=0; i < MAP_WIDTH; i++) {
617 for(int j=0; j<MAP_HEIGHT; j++) {
620 struct map_cell *cell = &map->cells[j][i];
623 if ((cell->flags & MAP_FLAG_WALL) &&
624 (cell->flags & MAP_FLAG_INVALIDATE_WALL) == 0)
626 if (cell->flags & MAP_FLAG_IGNORE_OBST)
628 if (cell->flags & MAP_FLAG_SIMULATED_WALL)
630 if (cell->flags & MAP_FLAG_PATH)
632 if (cell->flags & MAP_FLAG_START)
634 if (cell->flags & MAP_FLAG_GOAL)
636 if (cell->detected_obstacle) {
637 QColor c1(color), c2(blue);
638 double f = (double)cell->detected_obstacle/MAP_NEW_OBSTACLE*0.7;
639 QColor c(c1.red() + (int)(f*(c2.red() - c1.red())),
640 c1.green() + (int)(f*(c2.green() - c1.green())),
641 c1.blue() + (int)(f*(c2.blue() - c1.blue())));
644 if (cell->flags & MAP_FLAG_DET_OBST)
647 playgroundScene->setMapColor(i, j, color);
652 void RobomonAtlantis::setSimulation(int state)
655 robottype_publisher_hokuyo_scan_create(&orte,
656 dummy_publisher_callback, this);
658 if (!simulationEnabled)
660 robottype_publisher_hokuyo_scan_destroy(&orte);
662 simulationEnabled = state;
666 \fn RobomonAtlantis::setObstacleSimulation(int state)
668 void RobomonAtlantis::setObstacleSimulation(int state)
671 /* TODO Maybe it is possible to attach only once to Shmap */
673 obstacleSimulationTimer = new QTimer(this);
674 connect(obstacleSimulationTimer, SIGNAL(timeout()),
675 this, SLOT(simulateObstaclesHokuyo()));
676 obstacleSimulationTimer->start(100);
677 setMouseTracking(true);
679 if (obstacleSimulationTimer)
680 delete obstacleSimulationTimer;
681 //double distance = 0.8;
686 void RobomonAtlantis::simulateObstaclesHokuyo()
688 double distance, wall_distance;
690 uint16_t *hokuyo = orte.hokuyo_scan.data;
692 for (i=0; i<HOKUYO_CLUSTER_CNT; i++) {
693 wall_distance = distanceToWallHokuyo(i);
694 distance = distanceToObstacleHokuyo(i, simulatedObstacle, SIM_OBST_SIZE_M/*meters*/);
695 if (wall_distance < distance)
696 distance = wall_distance;
697 hokuyo[i] = distance*1000;
699 ORTEPublicationSend(orte.publication_hokuyo_scan);
703 void RobomonAtlantis::changeObstacle(QPointF position)
705 if (!simulationEnabled) {
706 simulationEnabled = 1;
707 obstacleSimulationCheckBox->setChecked(true);
710 simulatedObstacle.x = position.x();
711 simulatedObstacle.y = position.y();
712 simulateObstaclesHokuyo();
715 /**********************************************************************
717 **********************************************************************/
718 bool RobomonAtlantis::event(QEvent *event)
720 switch (event->type()) {
721 case QEVENT(QEV_MOTION_STATUS):
722 emit motionStatusReceivedSignal();
724 case QEVENT(QEV_REFERENCE_POSITION):
725 emit actualPositionReceivedSignal();
727 case QEVENT(QEV_ESTIMATED_POSITION_UZV):
728 emit estimatedPositionReceivedSignal();
730 case QEVENT(QEV_ESTIMATED_POSITION_ODO):
731 robotEstPosOdo->moveRobot(orte.est_pos_odo.x,
732 orte.est_pos_odo.y, orte.est_pos_odo.phi);
733 trailOdoPos->addPoint(QPointF(orte.est_pos_odo.x,
734 orte.est_pos_odo.y));
737 emit diReceivedSignal();
739 case QEVENT(QEV_ACCELEROMETER):
740 emit accelerometerReceivedSignal();
742 case QEVENT(QEV_ACCUMULATOR):
743 emit accumulatorReceivedSignal();
745 case QEVENT(QEV_POWER_VOLTAGE):
746 emit powerVoltageReceivedSignal();
748 case QEVENT(QEV_FSM_MAIN):
749 fsm_main_state->setText(orte.fsm_main.state_name);
751 case QEVENT(QEV_FSM_ACT):
752 fsm_act_state->setText(orte.fsm_act.state_name);
754 case QEVENT(QEV_FSM_MOTION):
755 fsm_motion_state->setText(orte.fsm_motion.state_name);
757 case QEVENT(QEV_PUCK_DISTANCE):
758 sharpPuck->setValue(orte.puck_distance.distance*1000);
760 case QEVENT(QEV_LIFT_PUSHER):
761 liftPos->setValue(orte.actuator_status.lift_pos);
762 pusherPos->setValue(orte.actuator_status.pusher_pos);
765 if (event->type() == QEvent::Close)
766 closeEvent((QCloseEvent *)event);
767 else if (event->type() == QEvent::KeyPress)
768 keyPressEvent((QKeyEvent *)event);
769 else if (event->type() == QEvent::KeyRelease)
770 keyReleaseEvent((QKeyEvent *)event);
771 else if (event->type() == QEvent::FocusIn)
773 else if (event->type() == QEvent::FocusOut)
785 void RobomonAtlantis::keyPressEvent(QKeyEvent *event)
789 if (event->isAutoRepeat()) {
790 switch (event->key()) {
792 peak = leftMotorSlider->minimum()/2;
793 if (leftMotorValue < peak ||
794 rightMotorValue < peak)
798 leftMotorValue *= gain;
799 rightMotorValue *= gain;
800 leftMotorSlider->setValue((int)leftMotorValue);
801 rightMotorSlider->setValue((int)rightMotorValue);
807 peak = leftMotorSlider->maximum()/2;
808 if (leftMotorValue > peak ||
809 rightMotorValue > peak)
813 leftMotorValue *= gain;
814 rightMotorValue *= gain;
815 leftMotorSlider->setValue((int)leftMotorValue);
816 rightMotorSlider->setValue((int)rightMotorValue);
826 switch (event->key()) {
830 bothMotorsCheckBox->setChecked(true);
831 leftMotorSlider->setValue((int)leftMotorValue);
832 setLeftMotor((int)leftMotorValue);
836 rightMotorValue = -1;
837 bothMotorsCheckBox->setChecked(true);
838 leftMotorSlider->setValue((int)leftMotorValue);
839 setLeftMotor((int)leftMotorValue);
844 leftMotorSlider->setValue((int)leftMotorValue);
845 rightMotorSlider->setValue((int)rightMotorValue);
846 setLeftMotor((int)leftMotorValue);
847 setRightMotor((int)leftMotorValue);
851 rightMotorValue = -1;
852 leftMotorSlider->setValue((int)leftMotorValue);
853 rightMotorSlider->setValue((int)rightMotorValue);
854 setLeftMotor((int)leftMotorValue);
855 setRightMotor((int)rightMotorValue);
864 void RobomonAtlantis::keyReleaseEvent(QKeyEvent *event)
866 if (event->isAutoRepeat()) {
871 switch (event->key()) {
878 bothMotorsCheckBox->setChecked(false);
879 leftMotorSlider->setValue((int)leftMotorValue);
880 rightMotorSlider->setValue((int)rightMotorValue);
889 void RobomonAtlantis::closeEvent(QCloseEvent *)
891 robottype_roboorte_destroy(&orte);
894 /**********************************************************************
896 **********************************************************************/
897 void RobomonAtlantis::createOrte()
903 rv = robottype_roboorte_init(&orte);
905 printf("RobomonAtlantis: Unable to initialize ORTE\n");
909 robottype_publisher_motion_speed_create(&orte, dummy_publisher_callback, NULL);
911 robottype_publisher_pwr_ctrl_create(&orte, dummy_publisher_callback, NULL);
912 robottype_publisher_lift_create(&orte, NULL, &orte);
913 robottype_publisher_pusher_create(&orte, NULL, &orte);
914 robottype_publisher_chelae_create(&orte, NULL, &orte);
915 robottype_publisher_belts_create(&orte, NULL, &orte);
916 robottype_publisher_holder_create(&orte, NULL, &orte);
917 robottype_publisher_robot_cmd_create(&orte, NULL, &orte);
918 robottype_publisher_puck_inside_create(&orte, NULL, &orte);
921 robottype_subscriber_pwr_voltage_create(&orte,
922 receivePowerVoltageCallBack, this);
923 robottype_subscriber_motion_status_create(&orte,
924 receiveMotionStatusCallBack, this);
925 robottype_subscriber_ref_pos_create(&orte,
926 receiveActualPositionCallBack, this);
927 robottype_subscriber_est_pos_uzv_create(&orte,
928 generic_rcv_cb, new OrteCallbackInfo(this, QEV_ESTIMATED_POSITION_UZV));
929 robottype_subscriber_est_pos_odo_create(&orte,
930 generic_rcv_cb, new OrteCallbackInfo(this, QEV_ESTIMATED_POSITION_ODO));
931 robottype_subscriber_fsm_main_create(&orte,
932 rcv_fsm_main_cb, this);
933 robottype_subscriber_fsm_motion_create(&orte,
934 rcv_fsm_motion_cb, this);
935 robottype_subscriber_fsm_act_create(&orte,
936 rcv_fsm_act_cb, this);
937 robottype_subscriber_puck_distance_create(&orte, generic_rcv_cb, new OrteCallbackInfo(this, QEV_PUCK_DISTANCE));
938 robottype_subscriber_actuator_status_create(&orte, generic_rcv_cb, new OrteCallbackInfo(this, QEV_LIFT_PUSHER));
941 /*createDISubscriber(this, &orteData);*/
942 /*createAccelerometerSubscriber(this, &orteData);*/
943 /*createAccumulatorSubscriber(this, &orteData);*/
945 orte.motion_speed.left = 0;
946 orte.motion_speed.right = 0;
947 /* power management */
948 orte.pwr_ctrl.voltage33 = true;
949 orte.pwr_ctrl.voltage50 = true;
950 orte.pwr_ctrl.voltage80 = true;
951 voltage33CheckBox->setChecked(true);
952 voltage50CheckBox->setChecked(true);
953 voltage80CheckBox->setChecked(true);
957 /* set actions to do when we receive data from orte */
958 connect(this, SIGNAL(motionStatusReceivedSignal()),
959 this, SLOT(motionStatusReceived()));
960 connect(this, SIGNAL(actualPositionReceivedSignal()),
961 this, SLOT(actualPositionReceived()));
962 connect(this, SIGNAL(estimatedPositionReceivedSignal()),
963 this, SLOT(estimatedPositionReceived()));
964 connect(this, SIGNAL(diReceivedSignal()),
965 this, SLOT(diReceived()));
966 connect(this, SIGNAL(accelerometerReceivedSignal()),
967 this, SLOT(accelerometerReceived()));
968 connect(this, SIGNAL(accumulatorReceivedSignal()),
969 this, SLOT(accumulatorReceived()));
970 connect(this, SIGNAL(powerVoltageReceivedSignal()),
971 this, SLOT(powerVoltageReceived()));
974 void RobomonAtlantis::motionStatusReceived()
976 WDBG("ORTE received: motion status");
979 void RobomonAtlantis::actualPositionReceived()
981 actPosX->setText(QString("%1").arg(orte.ref_pos.x, 0, 'f', 3));
982 actPosY->setText(QString("%1").arg(orte.ref_pos.y, 0, 'f', 3));
983 actPosPhi->setText(QString("%1(%2)")
984 .arg(DEGREES(orte.ref_pos.phi), 0, 'f', 0)
985 .arg(orte.ref_pos.phi, 0, 'f', 1));
986 robotActPos->moveRobot(orte.ref_pos.x,
987 orte.ref_pos.y, orte.ref_pos.phi);
988 trailRefPos->addPoint(QPointF(orte.ref_pos.x, orte.ref_pos.y));
991 void RobomonAtlantis::estimatedPositionReceived()
993 estPosX->setText(QString("%1").arg(orte.est_pos_uzv.x, 0, 'f', 3));
994 estPosY->setText(QString("%1").arg(orte.est_pos_uzv.y, 0, 'f', 3));
995 estPosPhi->setText(QString("%1(%2)")
996 .arg(DEGREES(orte.est_pos_uzv.phi), 0, 'f', 0)
997 .arg(orte.est_pos_uzv.phi, 0, 'f', 1));
998 robotEstPosUzv->moveRobot(orte.est_pos_uzv.x,
999 orte.est_pos_uzv.y, orte.est_pos_uzv.phi);
1000 trailUzvPos->addPoint(QPointF(orte.est_pos_odo.x,
1001 orte.est_pos_odo.y));
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);
1150 void RobomonAtlantis::resetTrails()
1152 trailRefPos->reset();
1153 trailUzvPos->reset();
1154 trailOdoPos->reset();
1157 void RobomonAtlantis::showTrails(bool show)
1159 trailRefPos->setVisible(show);
1160 trailUzvPos->setVisible(show);
1161 trailOdoPos->setVisible(show);