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 startBtn = new QPushButton("Start");
189 layout->addWidget(startBtn);
191 miscGroupBox->setLayout(layout);
194 void RobomonAtlantis::createFSMGroupBox()
196 fsmGroupBox = new QGroupBox(tr("FSM"));
197 fsmGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
198 QGridLayout *layout = new QGridLayout();
200 layout->addWidget(MiscGui::createLabel("Main:"), 1, 0);
201 fsm_main_state = new QLabel();
202 fsm_main_state->setMinimumWidth(100);
203 layout->addWidget(fsm_main_state, 1, 1);
205 layout->addWidget(MiscGui::createLabel("Act:"), 2, 0);
206 fsm_act_state = new QLabel();
207 layout->addWidget(fsm_act_state, 2, 1);
209 layout->addWidget(MiscGui::createLabel("Motion:"), 3, 0);
210 fsm_motion_state = new QLabel();
211 layout->addWidget(fsm_motion_state, 3, 1);
213 fsmGroupBox->setLayout(layout);
216 void RobomonAtlantis::createDebugGroupBox()
218 debugGroupBox = new QGroupBox(tr("Debug window"));
219 debugGroupBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
220 QHBoxLayout *layout = new QHBoxLayout();
222 debugWindow = new QTextEdit();
223 debugWindow->setReadOnly(true);
225 layout->addWidget(debugWindow);
226 debugGroupBox->setLayout(layout);
229 void RobomonAtlantis::createActuatorsGroupBox()
231 actuatorsGroupBox = new QGroupBox(tr("Actuators"));
232 actuatorsGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
233 QHBoxLayout *layout = new QHBoxLayout();
235 createMotorsGroupBox();
236 createPickerGroupBox();
238 layout->setAlignment(Qt::AlignLeft);
239 layout->addWidget(enginesGroupBox);
240 layout->addWidget(pickerGroupBox);
241 actuatorsGroupBox->setLayout(layout);
244 void RobomonAtlantis::createDIOGroupBox()
246 dioGroupBox = new QGroupBox(tr("DIO"));
247 QGridLayout *layout = new QGridLayout();
250 font.setPointSize(5);
251 dioGroupBox->setFont(font);
253 for (int i=0; i<8; i++) {
254 diCheckBox[i] = new QCheckBox(QString("DI%1").arg(i));
255 doCheckBox[i] = new QCheckBox(QString("D0%1").arg(i));
256 layout->addWidget(diCheckBox[i], i, 0);
257 layout->addWidget(doCheckBox[i], i+8, 0);
260 dioGroupBox->setMaximumWidth(70);
261 dioGroupBox->setLayout(layout);
264 void RobomonAtlantis::createPowerGroupBox()
266 powerGroupBox = new QGroupBox(tr("Power management"));
267 powerGroupBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
268 powerGroupBox->setMaximumWidth(450);
269 QGridLayout *layout = new QGridLayout();
271 voltage33CheckBox = new QCheckBox(tr("&3.3V"));
272 voltage50CheckBox = new QCheckBox(tr("&5.0V"));
273 voltage80CheckBox = new QCheckBox(tr("&8.0V"));
275 voltage33CheckBox->setShortcut(tr("3"));
276 voltage50CheckBox->setShortcut(tr("5"));
277 voltage80CheckBox->setShortcut(tr("8"));
279 layout->addWidget(voltage33CheckBox, 0, 0);
280 layout->addWidget(voltage50CheckBox, 0, 2);
281 layout->addWidget(voltage80CheckBox, 0, 4); //1, 0);
282 layout->addWidget(MiscGui::createLabel("BAT"), 0, 6); //1, 2);
284 voltage33LineEdit = new QLineEdit();
285 voltage50LineEdit = new QLineEdit();
286 voltage80LineEdit = new QLineEdit();
287 voltageBATLineEdit = new QLineEdit();
289 voltage33LineEdit->setReadOnly(true);
290 voltage50LineEdit->setReadOnly(true);
291 voltage80LineEdit->setReadOnly(true);
292 voltageBATLineEdit->setReadOnly(true);
294 layout->addWidget(voltage33LineEdit, 0, 1);
295 layout->addWidget(voltage50LineEdit, 0, 3);
296 layout->addWidget(voltage80LineEdit, 0, 5); //1, 1);
297 layout->addWidget(voltageBATLineEdit, 0, 7); //1, 3);
299 powerGroupBox->setLayout(layout);
302 void RobomonAtlantis::createSensorsGroupBox()
304 sensorsGroupBox = new QGroupBox(tr("Sensors"));
305 sensorsGroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
306 sensorsGroupBox->setMaximumWidth(450);
307 QHBoxLayout *layout = new QHBoxLayout();
309 layout->addWidget(MiscGui::createLabel("Puck Sharp"));
310 sharpPuck = new QProgressBar();
311 sharpPuck->setFormat("%v mm");
312 sharpPuck->setTextVisible(true);
313 sharpPuck->setMaximum(250);
314 layout->addWidget(sharpPuck);
316 layout->addWidget(MiscGui::createLabel("Lift"));
317 liftPos = new QProgressBar();
318 liftPos->setFormat("%v IRC");
319 liftPos->setTextVisible(true);
320 liftPos->setMaximum(1000);
321 layout->addWidget(liftPos);
323 layout->addWidget(MiscGui::createLabel("Pusher"));
324 pusherPos = new QProgressBar();
325 pusherPos->setFormat("%v IRC");
326 pusherPos->setTextVisible(true);
327 pusherPos->setMaximum(250);
328 layout->addWidget(pusherPos);
330 sensorsGroupBox->setLayout(layout);
333 void RobomonAtlantis::createMotorsGroupBox()
335 enginesGroupBox = new QGroupBox(tr("Motors"));
336 QVBoxLayout *layout = new QVBoxLayout();
337 QHBoxLayout *layout1 = new QHBoxLayout();
338 QHBoxLayout *layout2 = new QHBoxLayout();
340 leftMotorSlider = new QSlider(Qt::Vertical);
341 rightMotorSlider = new QSlider(Qt::Vertical);
342 bothMotorsCheckBox = new QCheckBox(tr("Lock both"));
343 stopMotorsPushButton = new QPushButton(tr("Stop Motors"));
345 leftMotorSlider->setMinimum(-100);
346 leftMotorSlider->setMaximum(100);
347 leftMotorSlider->setTracking(false);
348 leftMotorSlider->setTickPosition(QSlider::TicksLeft);
350 rightMotorSlider->setMinimum(-100);
351 rightMotorSlider->setMaximum(100);
352 rightMotorSlider->setTracking(false);
353 rightMotorSlider->setTickPosition(QSlider::TicksRight);
355 stopMotorsPushButton->setMaximumWidth(90);
357 layout1->addWidget(leftMotorSlider);
358 layout1->addWidget(MiscGui::createLabel("0"));
359 layout1->addWidget(rightMotorSlider);
361 layout2->addWidget(bothMotorsCheckBox);
363 layout->addWidget(MiscGui::createLabel("100"));
364 layout->addLayout(layout1);
365 layout->addWidget(MiscGui::createLabel("-100"));
366 layout->addLayout(layout2);
367 layout->addWidget(stopMotorsPushButton);
368 enginesGroupBox->setLayout(layout);
371 void RobomonAtlantis::createPickerGroupBox()
373 pickerGroupBox = new QGroupBox(tr("Picker"));
374 QVBoxLayout *layout = new QVBoxLayout();
376 leftBeltCheckBox = new QCheckBox(tr("L Belt"));
377 rightBeltCheckBox = new QCheckBox(tr("R Belt"));
378 leftChelaCheckBox = new QCheckBox(tr("L Chela"));
379 rightChelaCheckBox = new QCheckBox(tr("R Chela"));
380 leftBeltDial = new QDial();
381 rightBeltDial = new QDial();
382 leftChelaDial = new QDial();
383 rightChelaDial = new QDial();
384 pickPushButton = new QPushButton(tr("Pick!"));
386 leftBeltDial->setMinimum(0);
387 leftBeltDial->setMaximum(200);
388 leftBeltDial->setValue(100);
390 rightBeltDial->setMinimum(0);
391 rightBeltDial->setMaximum(200);
392 rightBeltDial->setValue(100);
394 leftChelaDial->setMinimum(0);
395 leftChelaDial->setMaximum(255);
396 leftChelaDial->setValue(127);
398 rightChelaDial->setMinimum(0);
399 rightChelaDial->setMaximum(255);
400 rightChelaDial->setValue(127);
402 layout->addWidget(leftChelaDial);
403 layout->addWidget(leftChelaCheckBox);
404 layout->addWidget(rightChelaDial);
405 layout->addWidget(rightChelaCheckBox);
406 layout->addWidget(leftBeltDial);
407 layout->addWidget(leftBeltCheckBox);
408 layout->addWidget(rightBeltDial);
409 layout->addWidget(rightBeltCheckBox);
410 layout->addWidget(pickPushButton);
412 pickerGroupBox->setLayout(layout);
415 void RobomonAtlantis::createRobots()
417 robotActPos = new Robot(QPen(Qt::darkBlue), QBrush(Qt::NoBrush));
418 robotActPos->setZValue(11);
419 robotEstPos = new Robot(QPen(), QBrush(Qt::darkGray));
420 robotEstPos->setZValue(10);
422 playgroundScene->addItem(robotActPos);
423 playgroundScene->addItem(robotEstPos);
426 /**********************************************************************
428 **********************************************************************/
429 void RobomonAtlantis::createActions()
431 /* power management */
432 connect(voltage33CheckBox, SIGNAL(stateChanged(int)),
433 this, SLOT(setVoltage33(int)));
434 connect(voltage50CheckBox, SIGNAL(stateChanged(int)),
435 this, SLOT(setVoltage50(int)));
436 connect(voltage80CheckBox, SIGNAL(stateChanged(int)),
437 this, SLOT(setVoltage80(int)));
440 connect(leftMotorSlider, SIGNAL(valueChanged(int)),
441 this, SLOT(setLeftMotor(int)));
442 connect(rightMotorSlider, SIGNAL(valueChanged(int)),
443 this, SLOT(setRightMotor(int)));
444 connect(stopMotorsPushButton, SIGNAL(clicked()),
445 this, SLOT(stopMotors()));
448 for (int i=0; i<8; i++)
449 connect(doCheckBox[0], SIGNAL(stateChanged(int)),
450 this, SLOT(setDO(int)));
452 /* path planning map */
453 connect(showMapPushButton, SIGNAL(clicked()),
454 this, SLOT(showMap()));
456 connect(startBtn, SIGNAL(clicked()), this, SLOT(sendStart()));
458 /* obstacle simulation */
459 simulationEnabled = 0;
460 connect(obstacleSimulationCheckBox, SIGNAL(stateChanged(int)),
461 this, SLOT(setSimulation(int)));
462 connect(obstacleSimulationCheckBox, SIGNAL(stateChanged(int)),
463 this, SLOT(setObstacleSimulation(int)));
464 connect(obstacleSimulationCheckBox, SIGNAL(stateChanged(int)),
465 playgroundScene, SLOT(showObstacle(int)));
466 connect(playgroundScene, SIGNAL(obstacleChanged(QPointF)),
467 this, SLOT(changeObstacle(QPointF)));
470 connect(pickPushButton, SIGNAL(clicked()), this, SLOT(pick()));
471 connect(leftBeltCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setBelts(int)));
472 connect(rightBeltCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setBelts(int)));
473 connect(leftChelaCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setChelae(int)));
474 connect(rightChelaCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setChelae(int)));
475 connect(leftBeltDial, SIGNAL(valueChanged(int)), this, SLOT(setBelts(int)));
476 connect(rightBeltDial, SIGNAL(valueChanged(int)), this, SLOT(setBelts(int)));
477 connect(leftChelaDial, SIGNAL(valueChanged(int)), this, SLOT(setChelae(int)));
478 connect(rightChelaDial, SIGNAL(valueChanged(int)), this, SLOT(setChelae(int)));
481 void RobomonAtlantis::setVoltage33(int state)
484 orte.pwr_ctrl.voltage33 = true;
486 orte.pwr_ctrl.voltage33 = false;
489 void RobomonAtlantis::setVoltage50(int state)
492 orte.pwr_ctrl.voltage50 = true;
494 orte.pwr_ctrl.voltage50 = false;
497 void RobomonAtlantis::setVoltage80(int state)
500 orte.pwr_ctrl.voltage80 = true;
502 orte.pwr_ctrl.voltage80 = false;
505 void RobomonAtlantis::setLeftMotor(int value)
508 short int rightMotor;
510 if(bothMotorsCheckBox->isChecked())
511 rightMotorSlider->setValue(value);
513 leftMotor = (short int)(MOTOR_LIMIT * (leftMotorSlider->value()/100.0));
514 rightMotor = (short int)(MOTOR_LIMIT * (rightMotorSlider->value()/100.0));
516 orte.motion_speed.left = leftMotor;
517 orte.motion_speed.right = rightMotor;
521 void RobomonAtlantis::setRightMotor(int value)
524 short int rightMotor;
526 if(bothMotorsCheckBox->isChecked())
527 leftMotorSlider->setValue(value);
529 leftMotor = (short int)(MOTOR_LIMIT * (leftMotorSlider->value()/100.0));
530 rightMotor = (short int)(MOTOR_LIMIT * (rightMotorSlider->value()/100.0));
532 orte.motion_speed.left = leftMotor;
533 orte.motion_speed.right = rightMotor;
537 void RobomonAtlantis::stopMotors()
539 leftMotorSlider->setValue(0);
540 rightMotorSlider->setValue(0);
544 void RobomonAtlantis::setDO(int state)
547 /* FIXME: digital output control comes here */
550 void RobomonAtlantis::pick()
552 // TODO: send signal to the fsmact
555 void RobomonAtlantis::setBelts(int value)
558 unsigned char leftBelt;
559 unsigned char rightBelt;
561 leftBelt = (unsigned char)leftBeltDial->value();
562 rightBelt = (unsigned char)rightBeltDial->value();
564 act_belts(leftBelt, rightBelt);
567 void RobomonAtlantis::setChelae(int value)
570 unsigned char leftChela;
571 unsigned char rightChela;
573 leftChela = (unsigned char)leftChelaDial->value();
574 rightChela = (unsigned char)rightChelaDial->value();
576 act_chelae(leftChela, rightChela);
579 void RobomonAtlantis::showMap()
583 if (sharedMemoryOpened == false)
586 mapTimer = new QTimer(this);
587 connect(mapTimer, SIGNAL(timeout()), this, SLOT(paintMap()));
588 mapTimer->start(200);
590 disconnect(showMapPushButton, SIGNAL(clicked()), this, SLOT(showMap()));
591 connect(showMapPushButton, SIGNAL(clicked()),
592 this, SLOT(showPlayground()));
593 playgroundScene->showMap(true);
596 void RobomonAtlantis::showPlayground()
599 disconnect(mapTimer, SIGNAL(timeout()), this, SLOT(paintMap()));
601 disconnect(showMapPushButton, SIGNAL(clicked()),
602 this, SLOT(showPlayground()));
603 connect(showMapPushButton, SIGNAL(clicked()), this, SLOT(showMap()));
604 playgroundScene->showMap(false);
607 void RobomonAtlantis::paintMap()
611 struct map *map = ShmapIsMapInit();
615 for(int i=0; i < MAP_WIDTH; i++) {
616 for(int j=0; j<MAP_HEIGHT; j++) {
619 struct map_cell *cell = &map->cells[j][i];
622 if ((cell->flags & MAP_FLAG_WALL) &&
623 (cell->flags & MAP_FLAG_INVALIDATE_WALL) == 0)
625 if (cell->flags & MAP_FLAG_IGNORE_OBST)
627 if (cell->flags & MAP_FLAG_SIMULATED_WALL)
629 if (cell->flags & MAP_FLAG_PATH)
631 if (cell->flags & MAP_FLAG_START)
633 if (cell->flags & MAP_FLAG_GOAL)
635 if (cell->detected_obstacle) {
636 QColor c1(color), c2(blue);
637 double f = (double)cell->detected_obstacle/MAP_NEW_OBSTACLE*0.7;
638 QColor c(c1.red() + (int)(f*(c2.red() - c1.red())),
639 c1.green() + (int)(f*(c2.green() - c1.green())),
640 c1.blue() + (int)(f*(c2.blue() - c1.blue())));
643 if (cell->flags & MAP_FLAG_DET_OBST)
646 playgroundScene->setMapColor(i, j, color);
651 void RobomonAtlantis::setSimulation(int state)
654 robottype_publisher_hokuyo_scan_create(&orte,
655 dummy_publisher_callback, this);
657 if (!simulationEnabled)
659 robottype_publisher_hokuyo_scan_destroy(&orte);
661 simulationEnabled = state;
665 \fn RobomonAtlantis::setObstacleSimulation(int state)
667 void RobomonAtlantis::setObstacleSimulation(int state)
670 /* TODO Maybe it is possible to attach only once to Shmap */
672 obstacleSimulationTimer = new QTimer(this);
673 connect(obstacleSimulationTimer, SIGNAL(timeout()),
674 this, SLOT(simulateObstaclesHokuyo()));
675 obstacleSimulationTimer->start(100);
676 setMouseTracking(true);
678 if (obstacleSimulationTimer)
679 delete obstacleSimulationTimer;
680 //double distance = 0.8;
685 void RobomonAtlantis::simulateObstaclesHokuyo()
687 double distance, wall_distance;
689 uint16_t *hokuyo = orte.hokuyo_scan.data;
691 for (i=0; i<HOKUYO_CLUSTER_CNT; i++) {
692 wall_distance = distanceToWallHokuyo(i);
693 distance = distanceToObstacleHokuyo(i, simulatedObstacle, SIM_OBST_SIZE_M/*meters*/);
694 if (wall_distance < distance)
695 distance = wall_distance;
696 hokuyo[i] = distance*1000;
698 ORTEPublicationSend(orte.publication_hokuyo_scan);
702 void RobomonAtlantis::changeObstacle(QPointF position)
704 if (!simulationEnabled) {
705 simulationEnabled = 1;
706 obstacleSimulationCheckBox->setChecked(true);
709 simulatedObstacle.x = position.x();
710 simulatedObstacle.y = position.y();
711 simulateObstaclesHokuyo();
714 /**********************************************************************
716 **********************************************************************/
717 bool RobomonAtlantis::event(QEvent *event)
719 switch (event->type()) {
720 case QEVENT(QEV_MOTION_STATUS):
721 emit motionStatusReceivedSignal();
723 case QEVENT(QEV_ACTUAL_POSITION):
724 emit actualPositionReceivedSignal();
726 case QEVENT(QEV_ESTIMATED_POSITION):
727 emit estimatedPositionReceivedSignal();
730 emit diReceivedSignal();
732 case QEVENT(QEV_ACCELEROMETER):
733 emit accelerometerReceivedSignal();
735 case QEVENT(QEV_ACCUMULATOR):
736 emit accumulatorReceivedSignal();
738 case QEVENT(QEV_POWER_VOLTAGE):
739 emit powerVoltageReceivedSignal();
741 case QEVENT(QEV_FSM_MAIN):
742 fsm_main_state->setText(orte.fsm_main.state_name);
744 case QEVENT(QEV_FSM_ACT):
745 fsm_act_state->setText(orte.fsm_act.state_name);
747 case QEVENT(QEV_FSM_MOTION):
748 fsm_motion_state->setText(orte.fsm_motion.state_name);
750 case QEVENT(QEV_PUCK_DISTANCE):
751 sharpPuck->setValue(orte.puck_distance.distance*1000);
753 case QEVENT(QEV_LIFT_PUSHER):
754 liftPos->setValue(orte.actuator_status.lift_pos);
755 pusherPos->setValue(orte.actuator_status.pusher_pos);
758 if (event->type() == QEvent::Close)
759 closeEvent((QCloseEvent *)event);
760 else if (event->type() == QEvent::KeyPress)
761 keyPressEvent((QKeyEvent *)event);
762 else if (event->type() == QEvent::KeyRelease)
763 keyReleaseEvent((QKeyEvent *)event);
764 else if (event->type() == QEvent::FocusIn)
766 else if (event->type() == QEvent::FocusOut)
778 void RobomonAtlantis::keyPressEvent(QKeyEvent *event)
782 if (event->isAutoRepeat()) {
783 switch (event->key()) {
785 peak = leftMotorSlider->minimum()/2;
786 if (leftMotorValue < peak ||
787 rightMotorValue < peak)
791 leftMotorValue *= gain;
792 rightMotorValue *= gain;
793 leftMotorSlider->setValue((int)leftMotorValue);
794 rightMotorSlider->setValue((int)rightMotorValue);
800 peak = leftMotorSlider->maximum()/2;
801 if (leftMotorValue > peak ||
802 rightMotorValue > peak)
806 leftMotorValue *= gain;
807 rightMotorValue *= gain;
808 leftMotorSlider->setValue((int)leftMotorValue);
809 rightMotorSlider->setValue((int)rightMotorValue);
819 switch (event->key()) {
823 bothMotorsCheckBox->setChecked(true);
824 leftMotorSlider->setValue((int)leftMotorValue);
825 setLeftMotor((int)leftMotorValue);
829 rightMotorValue = -1;
830 bothMotorsCheckBox->setChecked(true);
831 leftMotorSlider->setValue((int)leftMotorValue);
832 setLeftMotor((int)leftMotorValue);
837 leftMotorSlider->setValue((int)leftMotorValue);
838 rightMotorSlider->setValue((int)rightMotorValue);
839 setLeftMotor((int)leftMotorValue);
840 setRightMotor((int)leftMotorValue);
844 rightMotorValue = -1;
845 leftMotorSlider->setValue((int)leftMotorValue);
846 rightMotorSlider->setValue((int)rightMotorValue);
847 setLeftMotor((int)leftMotorValue);
848 setRightMotor((int)rightMotorValue);
857 void RobomonAtlantis::keyReleaseEvent(QKeyEvent *event)
859 if (event->isAutoRepeat()) {
864 switch (event->key()) {
871 bothMotorsCheckBox->setChecked(false);
872 leftMotorSlider->setValue((int)leftMotorValue);
873 rightMotorSlider->setValue((int)rightMotorValue);
882 void RobomonAtlantis::closeEvent(QCloseEvent *)
884 robottype_roboorte_destroy(&orte);
887 /**********************************************************************
889 **********************************************************************/
890 void RobomonAtlantis::createOrte()
896 rv = robottype_roboorte_init(&orte);
898 printf("RobomonAtlantis: Unable to initialize ORTE\n");
902 robottype_publisher_motion_speed_create(&orte, dummy_publisher_callback, NULL);
904 robottype_publisher_pwr_ctrl_create(&orte, dummy_publisher_callback, NULL);
905 robottype_publisher_lift_create(&orte, NULL, &orte);
906 robottype_publisher_pusher_create(&orte, NULL, &orte);
907 robottype_publisher_chelae_create(&orte, NULL, &orte);
908 robottype_publisher_belts_create(&orte, NULL, &orte);
909 robottype_publisher_holder_create(&orte, NULL, &orte);
910 robottype_publisher_robot_cmd_create(&orte, NULL, &orte);
913 robottype_subscriber_pwr_voltage_create(&orte,
914 receivePowerVoltageCallBack, this);
915 robottype_subscriber_motion_status_create(&orte,
916 receiveMotionStatusCallBack, this);
917 robottype_subscriber_ref_pos_create(&orte,
918 receiveActualPositionCallBack, this);
919 robottype_subscriber_est_pos_create(&orte,
920 receiveEstimatedPositionCallBack, this);
921 robottype_subscriber_fsm_main_create(&orte,
922 rcv_fsm_main_cb, this);
923 robottype_subscriber_fsm_motion_create(&orte,
924 rcv_fsm_motion_cb, this);
925 robottype_subscriber_fsm_act_create(&orte,
926 rcv_fsm_act_cb, this);
927 robottype_subscriber_puck_distance_create(&orte, generic_rcv_cb, new OrteCallbackInfo(this, QEV_PUCK_DISTANCE));
928 robottype_subscriber_actuator_status_create(&orte, generic_rcv_cb, new OrteCallbackInfo(this, QEV_LIFT_PUSHER));
931 /*createDISubscriber(this, &orteData);*/
932 /*createAccelerometerSubscriber(this, &orteData);*/
933 /*createAccumulatorSubscriber(this, &orteData);*/
935 orte.motion_speed.left = 0;
936 orte.motion_speed.right = 0;
937 /* power management */
938 orte.pwr_ctrl.voltage33 = true;
939 orte.pwr_ctrl.voltage50 = true;
940 orte.pwr_ctrl.voltage80 = true;
941 voltage33CheckBox->setChecked(true);
942 voltage50CheckBox->setChecked(true);
943 voltage80CheckBox->setChecked(true);
947 /* set actions to do when we receive data from orte */
948 connect(this, SIGNAL(motionStatusReceivedSignal()),
949 this, SLOT(motionStatusReceived()));
950 connect(this, SIGNAL(actualPositionReceivedSignal()),
951 this, SLOT(actualPositionReceived()));
952 connect(this, SIGNAL(estimatedPositionReceivedSignal()),
953 this, SLOT(estimatedPositionReceived()));
954 connect(this, SIGNAL(diReceivedSignal()),
955 this, SLOT(diReceived()));
956 connect(this, SIGNAL(accelerometerReceivedSignal()),
957 this, SLOT(accelerometerReceived()));
958 connect(this, SIGNAL(accumulatorReceivedSignal()),
959 this, SLOT(accumulatorReceived()));
960 connect(this, SIGNAL(powerVoltageReceivedSignal()),
961 this, SLOT(powerVoltageReceived()));
964 void RobomonAtlantis::motionStatusReceived()
966 WDBG("ORTE received: motion status");
969 void RobomonAtlantis::actualPositionReceived()
971 actPosX->setText(QString("%1").arg(orte.ref_pos.x, 0, 'f', 3));
972 actPosY->setText(QString("%1").arg(orte.ref_pos.y, 0, 'f', 3));
973 actPosPhi->setText(QString("%1(%2)")
974 .arg(DEGREES(orte.ref_pos.phi), 0, 'f', 0)
975 .arg(orte.ref_pos.phi, 0, 'f', 1));
976 robotActPos->moveRobot(orte.ref_pos.x,
977 orte.ref_pos.y, orte.ref_pos.phi);
980 void RobomonAtlantis::estimatedPositionReceived()
982 estPosX->setText(QString("%1").arg(orte.est_pos.x, 0, 'f', 3));
983 estPosY->setText(QString("%1").arg(orte.est_pos.y, 0, 'f', 3));
984 estPosPhi->setText(QString("%1(%2)")
985 .arg(DEGREES(orte.est_pos.phi), 0, 'f', 0)
986 .arg(orte.est_pos.phi, 0, 'f', 1));
987 robotEstPos->moveRobot(orte.est_pos.x,
988 orte.est_pos.y, orte.est_pos.phi);
991 void RobomonAtlantis::diReceived()
993 WDBG("ORTE received: DI");
996 void RobomonAtlantis::accelerometerReceived()
998 WDBG("ORTE received: accelerometer");
1001 void RobomonAtlantis::accumulatorReceived()
1003 WDBG("ORTE received: accumulator");
1006 void RobomonAtlantis::powerVoltageReceived()
1008 voltage33LineEdit->setText(QString("%1").arg(
1009 orte.pwr_voltage.voltage33, 0, 'f', 3));
1010 voltage50LineEdit->setText(QString("%1").arg(
1011 orte.pwr_voltage.voltage50, 0, 'f', 3));
1012 voltage80LineEdit->setText(QString("%1").arg(
1013 orte.pwr_voltage.voltage80, 0, 'f', 3));
1014 voltageBATLineEdit->setText(QString("%1").arg(
1015 orte.pwr_voltage.voltageBAT, 0, 'f', 3));
1019 /**********************************************************************
1021 **********************************************************************/
1022 void RobomonAtlantis::openSharedMemory()
1025 int sharedSegmentSize;
1027 if (sharedMemoryOpened)
1030 sharedSegmentSize = sizeof(unsigned int) * MAP_WIDTH * MAP_HEIGHT;
1032 /* Get segment identificator in a read only mode */
1033 segmentId = shmget(SHM_MAP_KEY, sharedSegmentSize, S_IRUSR);
1034 if(segmentId == -1) {
1035 QMessageBox::critical(this, "robomon",
1036 "Unable to open shared memory segment!");
1043 /* Attach the shared memory segment */
1044 //map = (_Map*)shmat (segmentId, (void*) 0, 0);
1046 sharedMemoryOpened = true;
1049 double RobomonAtlantis::distanceToWallHokuyo(int beamnum)
1051 double distance=4.0, min_distance=4.0;
1054 struct map *map = ShmapIsMapInit();
1056 if (!map) return min_distance;
1058 // Simulate obstacles
1059 for(j=0;j<MAP_HEIGHT;j++) {
1060 for (i=0;i<MAP_WIDTH;i++) {
1061 struct map_cell *cell = &map->cells[j][i];
1062 if( cell->flags & MAP_FLAG_SIMULATED_WALL) {
1064 ShmapCell2Point(i, j, &wall.x, &wall.y);
1066 distance = distanceToObstacleHokuyo(beamnum, wall, MAP_CELL_SIZE_M);
1067 if (distance<min_distance) min_distance = distance;
1072 return min_distance;
1076 * Calculation for Hokuyo simulation. Calculates distance that would
1077 * be returned by Hokuyo sensors, if there is only one obstacle (as
1078 * specified by parameters).
1080 * @param beamnum Hokuyo's bean number [0..HOKUYO_CLUSTER_CNT]
1081 * @param obstacle Position of the obstacle (x, y in meters).
1082 * @param obstacleSize Size (diameter) of the obstacle in meters.
1084 * @return Distance measured by sensors in meters.
1086 double RobomonAtlantis::distanceToObstacleHokuyo(int beamnum, Point obstacle, double obstacleSize)
1088 struct est_pos_type e = orte.est_pos;
1092 s.x = HOKUYO_CENTER_OFFSET_M;
1094 s.ang = HOKUYO_CLUSTER_TO_RAD(beamnum);
1096 Point sensor(e.x + s.x*cos(e.phi) - s.y*sin(e.phi),
1097 e.y + s.x*sin(e.phi) + s.y*cos(e.phi));
1098 sensor_a = e.phi + s.ang;
1100 const double sensorRange = 4.0; /*[meters]*/
1102 double distance, angle;
1104 angle = sensor.angleTo(obstacle) - sensor_a;
1105 angle = fmod(angle, 2.0*M_PI);
1106 if (angle > +M_PI) angle -= 2.0*M_PI;
1107 if (angle < -M_PI) angle += 2.0*M_PI;
1108 angle = fabs(angle);
1109 distance = sensor.distanceTo(obstacle)-0.11;
1110 if (angle < atan(obstacleSize/2.0 / (distance+0.001))) {
1111 // We can see the obstackle from here.
1112 if (angle < M_PI/2.0) {
1113 distance = distance/cos(angle);
1115 if (distance > sensorRange)
1116 distance = sensorRange;
1118 distance = sensorRange;
1124 void RobomonAtlantis::sendStart()
1126 orte.robot_cmd.start = 1;
1127 ORTEPublicationSend(orte.publication_robot_cmd);