00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <iostream>
00019 #include <fstream>
00020 #include <string>
00021 #include "MainWindow.h"
00022 #include "uvsim/guidance/GeoCoord.h"
00023 #include "uvsim/control/ControlComm.h"
00024 #include "uvsim/systems/UgvSystem.h"
00025 #include <uvsim/utilities/RealtimeThread.h>
00026
00027 namespace uvsim
00028 {
00029
00030 int keyDown;
00031 int setCounter = 0;
00032 int orderCounter = 0;
00033
00034 MainWindow::MainWindow(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& glade)
00035 : Gtk::Window(cobject), m_glade(glade), ManualButton("Manual Button is Off"), AutomaticButton("Automatic Button is Off")
00036 {
00037 std::cout << "Constructing MainWindow" << std::endl;
00038
00039
00040 show_all();
00041 m_glade->get_widget_derived("mapArea", m_pMapArea);
00042 m_glade->get_widget_derived("hudDisplay", m_HUDView);
00043 m_glade->get_widget_derived("3DView", m_ThreeDView);
00044 m_glade->get_widget("Commands", m_Commands);
00045 m_glade->get_widget("About", m_About);
00046 m_glade->get_widget("OffControl", m_OffControl);
00047 m_glade->get_widget("AutomaticControl", m_AutomaticControl);
00048 m_glade->get_widget("ManualControl", m_ManualControl);
00049
00050
00051 m_Commands->signal_activate().connect(sigc::mem_fun(*this, &MainWindow::Commands));
00052 m_About->signal_activate().connect(sigc::mem_fun(*this, &MainWindow::About));
00053 m_OffControl->signal_pressed().connect(sigc::mem_fun(*this, &MainWindow::offControl));
00054 m_AutomaticControl->signal_pressed().connect(sigc::mem_fun(*this, &MainWindow::automaticControl));
00055 m_ManualControl->signal_pressed().connect(sigc::mem_fun(*this, &MainWindow::manualControl));
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 add_events(Gdk::POINTER_MOTION_MASK | Gdk::POINTER_MOTION_HINT_MASK |
00069 Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
00070
00071 show_all();
00072 }
00073
00074 void MainWindow::Commands()
00075 {
00076
00077 Gtk::MessageDialog command_dialog(*this,
00078 "\tKeyboard Commands\n---------------------------------------------\nArrow Keys\tPanning\n+\t\t\tZooming In\n-\t\t\tZooming Out\nA or a\t\tAdding Waypoints\nM or m\t\tMoving Waypoints\nD or d\t\tDeleting Waypoints\nJ or j\t\tRolling Left\nL or l\t\tRolling Right\nK or k\t\tPitching Up\nI or i\t\tPitching Down\nU or u\t\tYawing Left\nO or o\t\tYawing Right",false, Gtk::MESSAGE_INFO, Gtk::BUTTONS_OK, true);
00079 command_dialog.run();
00080 }
00081
00082 void MainWindow::About()
00083 {
00084
00085 Gtk::MessageDialog about_dialog(*this,
00086 "\t\tUVsim GUI\n\n\n\n\t\tVersion 0.0.0\n\n\t\tPurdue University", false,
00087 Gtk::MESSAGE_INFO, Gtk::BUTTONS_OK, true);
00088 about_dialog.run();
00089 }
00090
00091 void MainWindow::offControl()
00092 {
00093 std::cout << "Control off " << std::endl;
00094
00095
00096 if (m_ugvSystem.getControlThread() != NULL)
00097 {
00098 std::cout<<"Destructor"<<std::endl;
00099 delete m_ugvSystem.getControlThread();
00100 m_ugvSystem.setControlThread(NULL);
00101 }
00102 }
00103
00104 void MainWindow::automaticControl()
00105 {
00106 std::cout << "Automatic Control On " << std::endl;
00107
00108 if (m_ugvSystem.getControlThread() == NULL)
00109 {
00110 m_ugvSystem.setControlThread(new ControlThread("/dev/rfcomm1",7,0,true));
00111 m_ugvSystem.getControlThread()->start();
00112 }
00113
00114
00115 m_ugvSystem.getControlThread()->setControlMode(AUTO);
00116 }
00117
00118 void MainWindow::manualControl()
00119 {
00120 std::cout << "Manual Control On " << std::endl;
00121
00122
00123
00124
00125 if (m_ugvSystem.getControlThread() == NULL)
00126 {
00127 m_ugvSystem.setControlThread(new ControlThread("/dev/rfcomm1",7,0,true));
00128 m_ugvSystem.getControlThread()->start();
00129 }
00130
00131
00132 m_ugvSystem.getControlThread()->setControlMode(MANUAL);
00133 }
00134
00135 bool MainWindow::on_key_press_event(GdkEventKey* event)
00136 {
00137 if (event->keyval == GDK_Escape)
00138 {
00139 Gtk::Main::quit();
00140 }
00141 else
00142 {
00143 keyDown = event->keyval;
00144 keyboardInput(keyDown);
00145 }
00146 return true;
00147 }
00148
00149 void MainWindow::keyboardInput(int keyDown)
00150 {
00151 double height = (m_pMapArea->image)->get_height();
00152 double width = (m_pMapArea->image)->get_width();
00153 double height_window = m_pMapArea->get_height();
00154 double width_window = m_pMapArea->get_width();
00155 double panLRMax = width * m_pMapArea->m_zoom - width_window;
00156 double panUDMax = height * m_pMapArea->m_zoom - height_window;
00157 double panLRsize = width / width_window / m_pMapArea->m_zoom * 10;
00158 double panUDsize = height / height_window / m_pMapArea->m_zoom * 10;
00159 int counter;
00160 std::vector<GeoCoord> waypoint;
00161
00162
00163 if (panLRMax < 0)
00164 {
00165 panLRMax = 0;
00166 }
00167 if (panUDMax < 0)
00168 {
00169 panUDMax = 0;
00170 }
00171
00172
00173 flightPlan = m_guide.getFlightPlan();
00174 waypoint = *flightPlan;
00175
00176
00177 if (keyDown == GDK_plus)
00178 {
00179
00180
00181 if (m_pMapArea->m_zoom <= 5)
00182 {
00183
00184 m_pMapArea->m_zoom = m_pMapArea->m_zoom + 0.1;
00185 }
00186 else
00187 {
00188
00189 m_pMapArea->m_zoom = 5;
00190 }
00191 std::cout << "Zoom Level: " << m_pMapArea->m_zoom << std::endl;
00192 m_pMapArea->drawInitial();
00193 }
00194
00195 if (keyDown == GDK_minus)
00196 {
00197
00198 if (m_pMapArea->m_zoom > 1)
00199 {
00200
00201 m_pMapArea->m_zoom = m_pMapArea->m_zoom - 0.1;
00202 }
00203 else
00204 {
00205
00206 m_pMapArea->m_zoom = 1;
00207 }
00208 std::cout << "Zoom Level: " << m_pMapArea->m_zoom << std::endl;
00209 m_pMapArea->drawInitial();
00210 }
00211
00212
00213 if (keyDown == GDK_Right)
00214 {
00215
00216 if (m_pMapArea->m_panLR >= (panLRMax * 0.95))
00217 {
00218
00219
00220 m_pMapArea->m_panLR = panLRMax;
00221 }
00222 else if (m_pMapArea->m_panLR >= (panLRMax * 0.90))
00223 {
00224
00225 m_pMapArea->m_panLR = m_pMapArea->m_panLR + panLRsize * 0.2;
00226 }
00227 else
00228 {
00229
00230 m_pMapArea->m_panLR = m_pMapArea->m_panLR + panLRsize;
00231 }
00232 if (m_pMapArea->m_panLR >= panLRMax)
00233 {
00234
00235
00236 m_pMapArea->m_panLR = panLRMax;
00237 }
00238 std::cout << "Pan Right: " << m_pMapArea->m_panLR << std::endl;
00239 m_pMapArea->drawInitial();
00240 }
00241
00242
00243 if (keyDown == GDK_Left)
00244 {
00245
00246 if (m_pMapArea->m_panLR <= panLRMax * 0.05)
00247 {
00248
00249 m_pMapArea->m_panLR = 0;
00250 }
00251 else if (m_pMapArea->m_panLR <= panLRMax * 0.1)
00252 {
00253
00254 m_pMapArea->m_panLR = m_pMapArea->m_panLR - panLRsize * 0.2 ;
00255 }
00256 else
00257 {
00258
00259 m_pMapArea->m_panLR = m_pMapArea->m_panLR - panLRsize;
00260 }
00261
00262 if (m_pMapArea->m_panLR <= panLRMax * 0.05)
00263 {
00264 m_pMapArea->m_panLR = 0;
00265 }
00266 std::cout << "Pan Left: " << m_pMapArea->m_panLR << std::endl;
00267 m_pMapArea->drawInitial();
00268 }
00269
00270
00271 if (keyDown == GDK_Up)
00272 {
00273
00274 if (m_pMapArea->m_panUD < panUDMax * 0.05)
00275 {
00276
00277 m_pMapArea->m_panUD = 0;
00278 }
00279 else if (m_pMapArea->m_panUD <= panUDMax * 0.1)
00280 {
00281
00282 m_pMapArea->m_panUD = m_pMapArea->m_panUD - panUDsize * 0.2;
00283 }
00284 else
00285 {
00286
00287 m_pMapArea->m_panUD = m_pMapArea->m_panUD - panUDsize;
00288 }
00289
00290 if (m_pMapArea->m_panUD < panUDMax * 0.05)
00291 {
00292 m_pMapArea->m_panUD = 0;
00293 }
00294 std::cout << "Pan Up: " << m_pMapArea->m_panUD << std::endl;
00295 m_pMapArea->drawInitial();
00296 }
00297
00298
00299 if (keyDown == GDK_Down)
00300 {
00301
00302 if (m_pMapArea->m_panUD >= (panUDMax * 0.95))
00303 {
00304
00305
00306 m_pMapArea->m_panUD = panUDMax;
00307 }
00308 else if (m_pMapArea->m_panUD >= (panUDMax * 0.90))
00309 {
00310
00311 m_pMapArea->m_panUD = m_pMapArea->m_panUD + panUDsize * 0.2;
00312 }
00313 else
00314 {
00315
00316 m_pMapArea->m_panUD = m_pMapArea->m_panUD + panUDsize;
00317 }
00318 if (m_pMapArea->m_panUD >= panUDMax)
00319 {
00320
00321
00322 m_pMapArea->m_panUD = panUDMax;
00323 }
00324 std::cout << "Pan Down: " << m_pMapArea->m_panUD << std::endl;
00325 m_pMapArea->drawInitial();
00326 }
00327
00328
00329 if ((keyDown == GDK_M || keyDown == GDK_m || keyDown == GDK_D ||
00330 keyDown == GDK_d || keyDown == GDK_A || keyDown == GDK_a) && waypoint.size() != 0)
00331 {
00332
00333 m_pMapArea->drawRedCircle(waypoint);
00334 }
00335
00336 if (keyDown == GDK_K || keyDown == GDK_k)
00337 {
00338
00339 m_HUDView->pitchangle = m_HUDView->pitchangle + 1;
00340 if (m_HUDView->pitchangle >= 360)
00341 {
00342
00343 m_HUDView->pitchangle = -360 + m_HUDView->pitchangle;
00344 }
00345 m_HUDView->drawTopPart();
00346 std::cout << "Pitching: " << m_HUDView->pitchangle << std::endl;
00347 }
00348
00349 if (keyDown == GDK_I || keyDown == GDK_i)
00350 {
00351
00352 m_HUDView->pitchangle = m_HUDView->pitchangle - 1;
00353 if (m_HUDView->pitchangle <= -360)
00354 {
00355
00356 m_HUDView->pitchangle = -360 - m_HUDView->pitchangle;
00357 }
00358 m_HUDView->drawTopPart();
00359 std::cout << "Pitching: " << m_HUDView->pitchangle << std::endl;
00360 }
00361
00362 if (keyDown == GDK_J || keyDown == GDK_j)
00363 {
00364
00365 m_HUDView->rolldegree = m_HUDView->rolldegree + 1;
00366 if (m_HUDView->rolldegree >= 360)
00367 {
00368
00369 m_HUDView->rolldegree = m_HUDView->rolldegree - 360;
00370 }
00371 m_HUDView->drawTopPart();
00372 std::cout << "Rolling: " << m_HUDView->rolldegree << std::endl;
00373 }
00374
00375 if (keyDown == GDK_L || keyDown == GDK_l)
00376 {
00377
00378 m_HUDView->rolldegree = m_HUDView->rolldegree - 1;
00379 if (m_HUDView->rolldegree <= -360)
00380 {
00381
00382 m_HUDView->rolldegree = -(m_HUDView->rolldegree + 360);
00383 }
00384 m_HUDView->drawTopPart();
00385 std::cout << "Rolling: " << m_HUDView->rolldegree << std::endl;
00386 }
00387
00388 if (keyDown == GDK_O || keyDown == GDK_o)
00389 {
00390
00391 if (m_HUDView->yaw < 45)
00392 {
00393 m_HUDView->yaw = m_HUDView->yaw + 1;
00394 }
00395 else
00396 {
00397
00398 m_HUDView->yaw = 45;
00399 }
00400
00401 m_HUDView->drawBottomPart();
00402 std::cout << "Yawing: " << m_HUDView->yaw << std::endl;
00403 }
00404
00405 if (keyDown == GDK_U || keyDown == GDK_u)
00406 {
00407
00408 if (m_HUDView->yaw > -45)
00409 {
00410 m_HUDView->yaw = m_HUDView->yaw - 1;
00411 }
00412 else
00413 {
00414
00415 m_HUDView->yaw = -45;
00416 }
00417
00418 m_HUDView->drawBottomPart();
00419 std::cout << "Yawing: " << m_HUDView->yaw << std::endl;
00420 }
00421 }
00422
00423 bool MainWindow::on_button_press_event(GdkEventButton *event)
00424 {
00425 double height = (m_pMapArea->image)->get_height();
00426 double width = (m_pMapArea->image)->get_width();
00427 double height_window = m_pMapArea->get_height();
00428 double width_window = m_pMapArea->get_width();
00429 double relative_x;
00430 double relative_y;
00431 double scaleX = m_pMapArea->scaleX;
00432 double scaleY = m_pMapArea->scaleY;
00433 double distance;
00434 double minimumdistance;
00435 int counter;
00436 std::vector<GeoCoord> waypoint;
00437
00438
00439 flightPlan = m_guide.getFlightPlan();
00440 waypoint = *flightPlan;
00441
00442
00443 relative_x = m_pMapArea->relative_originX + 1 / (m_pMapArea->m_zoom)
00444 * (event->x + m_pMapArea->m_panLR) * scaleX;
00445 relative_y = m_pMapArea->relative_originY + 1 / (m_pMapArea->m_zoom)
00446 * (event->y + m_pMapArea->m_panUD) * scaleY;
00447 if ((keyDown == GDK_A || keyDown == GDK_a) && orderCounter == 0 && waypoint.size() != 0)
00448 {
00449
00450 for (counter = 0 ; counter < (waypoint.size() - 1); counter++)
00451 {
00452
00453 distance = calculateDistance(relative_x, relative_y,
00454 waypoint[counter].m_Latitude, waypoint[counter].m_Longitude);
00455 if (counter == 0)
00456 {
00457
00458
00459 setCounter = counter;
00460 minimumdistance = distance;
00461 }
00462 if (distance < minimumdistance && distance <= 5)
00463 {
00464
00465 minimumdistance = distance;
00466 setCounter = counter;
00467 }
00468
00469 }
00470
00471 flightPlan->pop_back();
00472 waypoint = *flightPlan;
00473 m_pMapArea->drawInitial();
00474 m_pMapArea->drawRedCircle(waypoint[setCounter]);
00475 orderCounter = 1;
00476
00477 }
00478 else if ((keyDown == GDK_A) || (keyDown == GDK_a) && orderCounter == 1 && waypoint.size() != 0)
00479 {
00480
00481 std::cout << "Adding (" << relative_x << ", " << relative_y << ", " << "190) after ("
00482 << waypoint[setCounter].m_Latitude << ", " << waypoint[setCounter].m_Longitude
00483 << ", " << waypoint[setCounter].m_Altitude << ")" << std::endl;
00484 flightPlan->pop_back();
00485
00486
00487 waypoint[setCounter].m_Latitude = relative_x;
00488 waypoint[setCounter].m_Longitude = relative_y;
00489
00490 flightPlan->insert(flightPlan->begin() + setCounter + 1, waypoint[setCounter]);
00491
00492 waypoint = *flightPlan;
00493 m_pMapArea->drawInitial();
00494
00495 orderCounter = 0;
00496 keyDown = 0;
00497 }
00498
00499
00500 if ((keyDown == GDK_M || keyDown == GDK_m) && orderCounter == 0 && waypoint.size() != 0)
00501 {
00502
00503 for (counter = 0 ; counter < (waypoint.size() - 1); counter++)
00504 {
00505 distance = calculateDistance(relative_x, relative_y,
00506 waypoint[counter].m_Latitude, waypoint[counter].m_Longitude);
00507 if (counter == 0)
00508 {
00509 setCounter = counter;
00510 minimumdistance = distance;
00511 }
00512 if (distance < minimumdistance && distance <= 5)
00513 {
00514 minimumdistance = distance;
00515 setCounter = counter;
00516 }
00517
00518 }
00519 flightPlan->pop_back();
00520 waypoint = *flightPlan;
00521 m_pMapArea->drawInitial();
00522 m_pMapArea->drawRedCircle(waypoint[setCounter]);
00523 orderCounter = 1;
00524
00525 }
00526 else if ((keyDown == GDK_M) || (keyDown == GDK_m) && orderCounter == 1 && waypoint.size() != 0)
00527 {
00528
00529 std::cout << "Moving (" << waypoint[setCounter].m_Latitude << ", " << waypoint[setCounter].m_Longitude
00530 << ", " << waypoint[setCounter].m_Altitude << ") to ("
00531 << relative_x << ", " << relative_y << ", 190)" << std::endl;
00532 flightPlan->pop_back();
00533 waypoint[setCounter].m_Latitude = relative_x;
00534 waypoint[setCounter].m_Longitude = relative_y;
00535
00536 flightPlan->at(setCounter) = waypoint[setCounter];
00537 waypoint = *flightPlan;
00538 m_pMapArea->drawInitial();
00539
00540 orderCounter = 0;
00541 keyDown = 0;
00542 }
00543
00544 if ((keyDown == GDK_D) || (keyDown == GDK_d) && waypoint.size() != 0)
00545 {
00546
00547 std::cout << "Deleting (" << waypoint[setCounter].m_Latitude << ", " << waypoint[setCounter].m_Longitude
00548 << ", " << waypoint[setCounter].m_Altitude << ")" << std::endl;
00549 for (counter = 0 ; counter < (waypoint.size()- 1) ; counter++)
00550 {
00551 distance = calculateDistance(relative_x, relative_y,
00552 waypoint[counter].m_Latitude, waypoint[counter].m_Longitude);
00553 if (counter == 0)
00554 {
00555 setCounter = counter;
00556 minimumdistance = distance;
00557 }
00558 if (distance < minimumdistance && distance <= 5)
00559 {
00560 minimumdistance = distance;
00561 setCounter = counter;
00562 }
00563
00564 }
00565 flightPlan->pop_back();
00566 flightPlan->erase(flightPlan->begin() + setCounter,
00567 flightPlan->begin() + setCounter + 1);
00568 waypoint = *flightPlan;
00569 m_pMapArea->drawInitial();
00570
00571 keyDown = 0;
00572 }
00573
00574 std::cout <<*flightPlan << std::endl;
00575
00576 return true;
00577 }
00578
00579 double MainWindow::calculateDistance(double x1, double y1, double x2, double y2)
00580 {
00581 double distance;
00582 distance = sqrt(pow((x2 - x1), 2) + pow((y2 - y1), 2));
00583 return (distance);
00584 }
00585
00586 }
00587
00588
00589