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 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(startPlug, SIGNAL(stateChanged(int)), this, SLOT(sendStart(int)));
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()
610 struct map *map = ShmapIsMapInit();
614 for(int i=0; i < MAP_WIDTH; i++) {
615 for(int j=0; j<MAP_HEIGHT; j++) {
618 struct map_cell *cell = &map->cells[j][i];
621 if ((cell->flags & MAP_FLAG_WALL) &&
622 (cell->flags & MAP_FLAG_INVALIDATE_WALL) == 0)
624 if (cell->flags & MAP_FLAG_IGNORE_OBST)
626 if (cell->flags & MAP_FLAG_SIMULATED_WALL)
628 if (cell->flags & MAP_FLAG_PATH)
630 if (cell->flags & MAP_FLAG_START)
632 if (cell->flags & MAP_FLAG_GOAL)
634 if (cell->detected_obstacle) {
635 QColor c1(color), c2(blue);
636 double f = (double)cell->detected_obstacle/MAP_NEW_OBSTACLE*0.7;
637 QColor c(c1.red() + (int)(f*(c2.red() - c1.red())),
638 c1.green() + (int)(f*(c2.green() - c1.green())),
639 c1.blue() + (int)(f*(c2.blue() - c1.blue())));
642 if (cell->flags & MAP_FLAG_DET_OBST)
645 playgroundScene->setMapColor(i, j, color);
650 void RobomonAtlantis::setSimulation(int state)
653 robottype_publisher_hokuyo_scan_create(&orte,
654 dummy_publisher_callback, this);
656 if (!simulationEnabled)
658 robottype_publisher_hokuyo_scan_destroy(&orte);
660 simulationEnabled = state;
664 \fn RobomonAtlantis::setObstacleSimulation(int state)
666 void RobomonAtlantis::setObstacleSimulation(int state)
669 /* TODO Maybe it is possible to attach only once to Shmap */
671 obstacleSimulationTimer = new QTimer(this);
672 connect(obstacleSimulationTimer, SIGNAL(timeout()),
673 this, SLOT(simulateObstaclesHokuyo()));
674 obstacleSimulationTimer->start(100);
675 setMouseTracking(true);
677 if (obstacleSimulationTimer)
678 delete obstacleSimulationTimer;
679 //double distance = 0.8;
684 void RobomonAtlantis::simulateObstaclesHokuyo()
686 double distance, wall_distance;
688 uint16_t *hokuyo = orte.hokuyo_scan.data;
690 for (i=0; i<HOKUYO_CLUSTER_CNT; i++) {
691 wall_distance = distanceToWallHokuyo(i);
692 distance = distanceToObstacleHokuyo(i, simulatedObstacle, SIM_OBST_SIZE_M/*meters*/);
693 if (wall_distance < distance)
694 distance = wall_distance;
695 hokuyo[i] = distance*1000;
697 ORTEPublicationSend(orte.publication_hokuyo_scan);
701 void RobomonAtlantis::changeObstacle(QPointF position)
703 if (!simulationEnabled) {
704 simulationEnabled = 1;
705 obstacleSimulationCheckBox->setChecked(true);
708 simulatedObstacle.x = position.x();
709 simulatedObstacle.y = position.y();
710 simulateObstaclesHokuyo();
713 /**********************************************************************
715 **********************************************************************/
716 bool RobomonAtlantis::event(QEvent *event)
718 switch (event->type()) {
719 case QEVENT(QEV_MOTION_STATUS):
720 emit motionStatusReceivedSignal();
722 case QEVENT(QEV_ACTUAL_POSITION):
723 emit actualPositionReceivedSignal();
725 case QEVENT(QEV_ESTIMATED_POSITION):
726 emit estimatedPositionReceivedSignal();
729 emit diReceivedSignal();
731 case QEVENT(QEV_ACCELEROMETER):
732 emit accelerometerReceivedSignal();
734 case QEVENT(QEV_ACCUMULATOR):
735 emit accumulatorReceivedSignal();
737 case QEVENT(QEV_POWER_VOLTAGE):
738 emit powerVoltageReceivedSignal();
740 case QEVENT(QEV_FSM_MAIN):
741 fsm_main_state->setText(orte.fsm_main.state_name);
743 case QEVENT(QEV_FSM_ACT):
744 fsm_act_state->setText(orte.fsm_act.state_name);
746 case QEVENT(QEV_FSM_MOTION):
747 fsm_motion_state->setText(orte.fsm_motion.state_name);
749 case QEVENT(QEV_PUCK_DISTANCE):
750 sharpPuck->setValue(orte.puck_distance.distance*1000);
752 case QEVENT(QEV_LIFT_PUSHER):
753 liftPos->setValue(orte.actuator_status.lift_pos);
754 pusherPos->setValue(orte.actuator_status.pusher_pos);
757 if (event->type() == QEvent::Close)
758 closeEvent((QCloseEvent *)event);
759 else if (event->type() == QEvent::KeyPress)
760 keyPressEvent((QKeyEvent *)event);
761 else if (event->type() == QEvent::KeyRelease)
762 keyReleaseEvent((QKeyEvent *)event);
763 else if (event->type() == QEvent::FocusIn)
765 else if (event->type() == QEvent::FocusOut)
777 void RobomonAtlantis::keyPressEvent(QKeyEvent *event)
781 if (event->isAutoRepeat()) {
782 switch (event->key()) {
784 peak = leftMotorSlider->minimum()/2;
785 if (leftMotorValue < peak ||
786 rightMotorValue < peak)
790 leftMotorValue *= gain;
791 rightMotorValue *= gain;
792 leftMotorSlider->setValue((int)leftMotorValue);
793 rightMotorSlider->setValue((int)rightMotorValue);
799 peak = leftMotorSlider->maximum()/2;
800 if (leftMotorValue > peak ||
801 rightMotorValue > peak)
805 leftMotorValue *= gain;
806 rightMotorValue *= gain;
807 leftMotorSlider->setValue((int)leftMotorValue);
808 rightMotorSlider->setValue((int)rightMotorValue);
818 switch (event->key()) {
822 bothMotorsCheckBox->setChecked(true);
823 leftMotorSlider->setValue((int)leftMotorValue);
824 setLeftMotor((int)leftMotorValue);
828 rightMotorValue = -1;
829 bothMotorsCheckBox->setChecked(true);
830 leftMotorSlider->setValue((int)leftMotorValue);
831 setLeftMotor((int)leftMotorValue);
836 leftMotorSlider->setValue((int)leftMotorValue);
837 rightMotorSlider->setValue((int)rightMotorValue);
838 setLeftMotor((int)leftMotorValue);
839 setRightMotor((int)leftMotorValue);
843 rightMotorValue = -1;
844 leftMotorSlider->setValue((int)leftMotorValue);
845 rightMotorSlider->setValue((int)rightMotorValue);
846 setLeftMotor((int)leftMotorValue);
847 setRightMotor((int)rightMotorValue);
856 void RobomonAtlantis::keyReleaseEvent(QKeyEvent *event)
858 if (event->isAutoRepeat()) {
863 switch (event->key()) {
870 bothMotorsCheckBox->setChecked(false);
871 leftMotorSlider->setValue((int)leftMotorValue);
872 rightMotorSlider->setValue((int)rightMotorValue);
881 void RobomonAtlantis::closeEvent(QCloseEvent *)
883 robottype_roboorte_destroy(&orte);
886 /**********************************************************************
888 **********************************************************************/
889 void RobomonAtlantis::createOrte()
895 rv = robottype_roboorte_init(&orte);
897 printf("RobomonAtlantis: Unable to initialize ORTE\n");
901 robottype_publisher_motion_speed_create(&orte, dummy_publisher_callback, NULL);
903 robottype_publisher_pwr_ctrl_create(&orte, dummy_publisher_callback, NULL);
904 robottype_publisher_lift_create(&orte, NULL, &orte);
905 robottype_publisher_pusher_create(&orte, NULL, &orte);
906 robottype_publisher_chelae_create(&orte, NULL, &orte);
907 robottype_publisher_belts_create(&orte, NULL, &orte);
908 robottype_publisher_holder_create(&orte, NULL, &orte);
909 robottype_publisher_robot_cmd_create(&orte, NULL, &orte);
912 robottype_subscriber_pwr_voltage_create(&orte,
913 receivePowerVoltageCallBack, this);
914 robottype_subscriber_motion_status_create(&orte,
915 receiveMotionStatusCallBack, this);
916 robottype_subscriber_ref_pos_create(&orte,
917 receiveActualPositionCallBack, this);
918 robottype_subscriber_est_pos_create(&orte,
919 receiveEstimatedPositionCallBack, this);
920 robottype_subscriber_fsm_main_create(&orte,
921 rcv_fsm_main_cb, this);
922 robottype_subscriber_fsm_motion_create(&orte,
923 rcv_fsm_motion_cb, this);
924 robottype_subscriber_fsm_act_create(&orte,
925 rcv_fsm_act_cb, this);
926 robottype_subscriber_puck_distance_create(&orte, generic_rcv_cb, new OrteCallbackInfo(this, QEV_PUCK_DISTANCE));
927 robottype_subscriber_actuator_status_create(&orte, generic_rcv_cb, new OrteCallbackInfo(this, QEV_LIFT_PUSHER));
930 /*createDISubscriber(this, &orteData);*/
931 /*createAccelerometerSubscriber(this, &orteData);*/
932 /*createAccumulatorSubscriber(this, &orteData);*/
934 orte.motion_speed.left = 0;
935 orte.motion_speed.right = 0;
936 /* power management */
937 orte.pwr_ctrl.voltage33 = true;
938 orte.pwr_ctrl.voltage50 = true;
939 orte.pwr_ctrl.voltage80 = true;
940 voltage33CheckBox->setChecked(true);
941 voltage50CheckBox->setChecked(true);
942 voltage80CheckBox->setChecked(true);
946 /* set actions to do when we receive data from orte */
947 connect(this, SIGNAL(motionStatusReceivedSignal()),
948 this, SLOT(motionStatusReceived()));
949 connect(this, SIGNAL(actualPositionReceivedSignal()),
950 this, SLOT(actualPositionReceived()));
951 connect(this, SIGNAL(estimatedPositionReceivedSignal()),
952 this, SLOT(estimatedPositionReceived()));
953 connect(this, SIGNAL(diReceivedSignal()),
954 this, SLOT(diReceived()));
955 connect(this, SIGNAL(accelerometerReceivedSignal()),
956 this, SLOT(accelerometerReceived()));
957 connect(this, SIGNAL(accumulatorReceivedSignal()),
958 this, SLOT(accumulatorReceived()));
959 connect(this, SIGNAL(powerVoltageReceivedSignal()),
960 this, SLOT(powerVoltageReceived()));
963 void RobomonAtlantis::motionStatusReceived()
965 WDBG("ORTE received: motion status");
968 void RobomonAtlantis::actualPositionReceived()
970 actPosX->setText(QString("%1").arg(orte.ref_pos.x, 0, 'f', 3));
971 actPosY->setText(QString("%1").arg(orte.ref_pos.y, 0, 'f', 3));
972 actPosPhi->setText(QString("%1(%2)")
973 .arg(DEGREES(orte.ref_pos.phi), 0, 'f', 0)
974 .arg(orte.ref_pos.phi, 0, 'f', 1));
975 robotActPos->moveRobot(orte.ref_pos.x,
976 orte.ref_pos.y, orte.ref_pos.phi);
979 void RobomonAtlantis::estimatedPositionReceived()
981 estPosX->setText(QString("%1").arg(orte.est_pos.x, 0, 'f', 3));
982 estPosY->setText(QString("%1").arg(orte.est_pos.y, 0, 'f', 3));
983 estPosPhi->setText(QString("%1(%2)")
984 .arg(DEGREES(orte.est_pos.phi), 0, 'f', 0)
985 .arg(orte.est_pos.phi, 0, 'f', 1));
986 robotEstPos->moveRobot(orte.est_pos.x,
987 orte.est_pos.y, orte.est_pos.phi);
990 void RobomonAtlantis::diReceived()
992 WDBG("ORTE received: DI");
995 void RobomonAtlantis::accelerometerReceived()
997 WDBG("ORTE received: accelerometer");
1000 void RobomonAtlantis::accumulatorReceived()
1002 WDBG("ORTE received: accumulator");
1005 void RobomonAtlantis::powerVoltageReceived()
1007 voltage33LineEdit->setText(QString("%1").arg(
1008 orte.pwr_voltage.voltage33, 0, 'f', 3));
1009 voltage50LineEdit->setText(QString("%1").arg(
1010 orte.pwr_voltage.voltage50, 0, 'f', 3));
1011 voltage80LineEdit->setText(QString("%1").arg(
1012 orte.pwr_voltage.voltage80, 0, 'f', 3));
1013 voltageBATLineEdit->setText(QString("%1").arg(
1014 orte.pwr_voltage.voltageBAT, 0, 'f', 3));
1018 /**********************************************************************
1020 **********************************************************************/
1021 void RobomonAtlantis::openSharedMemory()
1024 int sharedSegmentSize;
1026 if (sharedMemoryOpened)
1029 sharedSegmentSize = sizeof(unsigned int) * MAP_WIDTH * MAP_HEIGHT;
1031 /* Get segment identificator in a read only mode */
1032 segmentId = shmget(SHM_MAP_KEY, sharedSegmentSize, S_IRUSR);
1033 if(segmentId == -1) {
1034 QMessageBox::critical(this, "robomon",
1035 "Unable to open shared memory segment!");
1042 /* Attach the shared memory segment */
1043 //map = (_Map*)shmat (segmentId, (void*) 0, 0);
1045 sharedMemoryOpened = true;
1048 double RobomonAtlantis::distanceToWallHokuyo(int beamnum)
1050 double distance=4.0, min_distance=4.0;
1053 struct map *map = ShmapIsMapInit();
1055 if (!map) return min_distance;
1057 // Simulate obstacles
1058 for(j=0;j<MAP_HEIGHT;j++) {
1059 for (i=0;i<MAP_WIDTH;i++) {
1060 struct map_cell *cell = &map->cells[j][i];
1061 if( cell->flags & MAP_FLAG_SIMULATED_WALL) {
1063 ShmapCell2Point(i, j, &wall.x, &wall.y);
1065 distance = distanceToObstacleHokuyo(beamnum, wall, MAP_CELL_SIZE_M);
1066 if (distance<min_distance) min_distance = distance;
1071 return min_distance;
1075 * Calculation for Hokuyo simulation. Calculates distance that would
1076 * be returned by Hokuyo sensors, if there is only one obstacle (as
1077 * specified by parameters).
1079 * @param beamnum Hokuyo's bean number [0..HOKUYO_CLUSTER_CNT]
1080 * @param obstacle Position of the obstacle (x, y in meters).
1081 * @param obstacleSize Size (diameter) of the obstacle in meters.
1083 * @return Distance measured by sensors in meters.
1085 double RobomonAtlantis::distanceToObstacleHokuyo(int beamnum, Point obstacle, double obstacleSize)
1087 struct est_pos_type e = orte.est_pos;
1091 s.x = HOKUYO_CENTER_OFFSET_M;
1093 s.ang = HOKUYO_CLUSTER_TO_RAD(beamnum);
1095 Point sensor(e.x + s.x*cos(e.phi) - s.y*sin(e.phi),
1096 e.y + s.x*sin(e.phi) + s.y*cos(e.phi));
1097 sensor_a = e.phi + s.ang;
1099 const double sensorRange = 4.0; /*[meters]*/
1101 double distance, angle;
1103 angle = sensor.angleTo(obstacle) - sensor_a;
1104 angle = fmod(angle, 2.0*M_PI);
1105 if (angle > +M_PI) angle -= 2.0*M_PI;
1106 if (angle < -M_PI) angle += 2.0*M_PI;
1107 angle = fabs(angle);
1108 distance = sensor.distanceTo(obstacle)-0.11;
1109 if (angle < atan(obstacleSize/2.0 / (distance+0.001))) {
1110 // We can see the obstackle from here.
1111 if (angle < M_PI/2.0) {
1112 distance = distance/cos(angle);
1114 if (distance > sensorRange)
1115 distance = sensorRange;
1117 distance = sensorRange;
1123 void RobomonAtlantis::sendStart(int plug)
1125 orte.robot_cmd.start = plug ? 0 : 1;
1126 ORTEPublicationSend(orte.publication_robot_cmd);