/orunav_motion_planner/src/VehicleMission.cpp

https://github.com/OrebroUniversity/navigation_oru-release · C++ · 167 lines · 128 code · 25 blank · 14 comment · 25 complexity · 871a48624ceef5da57a815ebfa56d992 MD5 · raw file

  1. /**
  2. * @file VehicleMission.cpp
  3. * @author Marcello Cirillo
  4. *
  5. * Created on: Mar 28, 2013
  6. * Author: marcello
  7. */
  8. #include "orunav_motion_planner/VehicleMission.h"
  9. #include "orunav_motion_planner/UnicycleModel.h"
  10. #include "orunav_motion_planner/CarModel.h"
  11. #include "orunav_motion_planner/LHDModel.h"
  12. #include "orunav_motion_planner/CarConfiguration.h"
  13. #include "orunav_motion_planner/UnicycleConfiguration.h"
  14. #include "orunav_motion_planner/LHDConfiguration.h"
  15. // init the static counter
  16. unsigned short int VehicleMission::vehicleNum_ = 0;
  17. unsigned short int VehicleMission::activeMissions_ = 0;
  18. VehicleMission::VehicleMission(VehicleModel* m, double start_x, double start_y, double start_o, double start_steering,
  19. double goal_x, double goal_y, double goal_o, double goal_steering, std::string name /*= ""*/) {
  20. vm_ = m;
  21. vehicleID_ = vehicleNum_;
  22. // increment the static counter and the number of active missions
  23. vehicleNum_++;
  24. activeMissions_++;
  25. unsigned short int startXcell, startYcell, goalXcell, goalYcell;
  26. uint8_t startOrientationID, startSteeringID, goalSteeringID, goalOrientationID;
  27. // snap the start pose into the grid
  28. if (fmod(start_x, WP::WORLD_SPACE_GRANULARITY) >= (WP::WORLD_SPACE_GRANULARITY / 2)) {
  29. startXcell = ceil(start_x * (1/WP::WORLD_SPACE_GRANULARITY));
  30. } else {
  31. startXcell = floor(start_x * (1/WP::WORLD_SPACE_GRANULARITY));
  32. }
  33. if (fmod(start_y, WP::WORLD_SPACE_GRANULARITY) >= (WP::WORLD_SPACE_GRANULARITY / 2)) {
  34. startYcell = ceil(start_y * (1/WP::WORLD_SPACE_GRANULARITY));
  35. } else {
  36. startYcell = floor(start_y * (1/WP::WORLD_SPACE_GRANULARITY));
  37. }
  38. startOrientationID = vm_->getClosestAllowedOrientationID(start_o);
  39. startSteeringID = vm_->getClosestAllowedSteeringID(start_steering);
  40. // snap the goal pose into the grid
  41. if (fmod(goal_x, WP::WORLD_SPACE_GRANULARITY) >= (WP::WORLD_SPACE_GRANULARITY / 2)) {
  42. goalXcell = ceil(goal_x * (1/WP::WORLD_SPACE_GRANULARITY));
  43. } else {
  44. goalXcell = floor(goal_x * (1/WP::WORLD_SPACE_GRANULARITY));
  45. }
  46. if (fmod(goal_y, WP::WORLD_SPACE_GRANULARITY) >= (WP::WORLD_SPACE_GRANULARITY / 2)) {
  47. goalYcell = ceil(goal_y * (1/WP::WORLD_SPACE_GRANULARITY));
  48. } else {
  49. goalYcell = floor(goal_y * (1/WP::WORLD_SPACE_GRANULARITY));
  50. }
  51. goalOrientationID = vm_->getClosestAllowedOrientationID(goal_o);
  52. goalSteeringID = vm_->getClosestAllowedSteeringID(goal_steering);
  53. // create a dummy motion primitive for the starting point
  54. MotionPrimitiveData* startPrimitive = new MotionPrimitiveData();
  55. double startOrient = startOrientationID * ((M_PI*2) / vm_->getOrientationAngles());
  56. double startSteer = startSteeringID * ((M_PI*2) / vm_->getSteeringAnglePartitions());
  57. std::vector<vehicleSimplePoint*> traj;
  58. vehicleSimplePoint* p = new vehicleSimplePoint;
  59. p->x = startXcell*WP::WORLD_SPACE_GRANULARITY;
  60. p->y = startYcell*WP::WORLD_SPACE_GRANULARITY;
  61. p->orient = startOrient;
  62. p->steering = startSteer;
  63. traj.push_back(p);
  64. startPrimitive->setTrajectory(traj, vm_->getOrientationAngles(),vm_->getSteeringAnglePartitions());
  65. // Now get the occupied and swept cells
  66. vehicleSimplePoint* dummypoint = new vehicleSimplePoint;
  67. dummypoint->x = 0;
  68. dummypoint->y = 0;
  69. dummypoint->orient = startOrient;
  70. dummypoint->steering = startSteer;
  71. std::vector<cellPosition*> sweptCells = vm_->getCellsOccupiedInPosition(dummypoint);
  72. std::vector<cellPosition*> occCells = vm_->getCellsOccupiedInPosition(dummypoint);
  73. startPrimitive->setOccCells(occCells);
  74. startPrimitive->setSweptCells(sweptCells);
  75. delete dummypoint;
  76. if (dynamic_cast<CarModel*> (m)) {
  77. start_ = new CarConfiguration(startXcell, startYcell, startOrientationID, startSteeringID, this);
  78. goal_ = new CarConfiguration(goalXcell, goalYcell, goalOrientationID, goalSteeringID, this);
  79. } else if (dynamic_cast<LHDModel*> (m)) {
  80. start_ = new LHDConfiguration(startXcell, startYcell, startOrientationID, startSteeringID, this);
  81. goal_ = new LHDConfiguration(goalXcell, goalYcell, goalOrientationID, goalSteeringID, this);
  82. } else if (dynamic_cast<UnicycleModel*> (m)) {
  83. start_ = new UnicycleConfiguration(startXcell, startYcell, startOrientationID, startSteeringID, this);
  84. goal_ = new UnicycleConfiguration(goalXcell, goalYcell, goalOrientationID, goalSteeringID, this);
  85. } else {
  86. writeLogLine(std::string("ERROR: vehicle model not recognized!"), "VehicleMission", WP::LOG_FILE);
  87. exit(0);
  88. }
  89. start_->setConfigurationPrimitive(startPrimitive);
  90. vehicleName_ = name;
  91. gridDistanceFromGoal_.clear();
  92. }
  93. VehicleMission::~VehicleMission() {
  94. MotionPrimitiveData* mpd = start_->getPrimitiveOfThisConfiguration();
  95. delete mpd;
  96. delete start_;
  97. delete goal_;
  98. for (std::vector<std::vector<double> >::iterator it = gridDistanceFromGoal_.begin(); it != gridDistanceFromGoal_.end(); it++ ){
  99. (*it).clear();
  100. }
  101. gridDistanceFromGoal_.clear();
  102. // reduce the number of active missions and reset the ID counter if this was the last one
  103. activeMissions_--;
  104. if (activeMissions_ == 0) {
  105. vehicleNum_ = 0;
  106. }
  107. }
  108. void VehicleMission::setGridDistanceFromGoal(std::vector<std::vector<double> > distances) {
  109. for (std::vector<std::vector<double> >::iterator it = gridDistanceFromGoal_.begin(); it != gridDistanceFromGoal_.end(); it++ ){
  110. (*it).clear();
  111. }
  112. gridDistanceFromGoal_.clear();
  113. gridDistanceFromGoal_.resize(distances.size());
  114. for (unsigned int i = 0; i < gridDistanceFromGoal_.size(); i++) {
  115. gridDistanceFromGoal_[i].resize(distances[i].size());
  116. for (unsigned int j = 0; j < gridDistanceFromGoal_[i].size(); j++) {
  117. gridDistanceFromGoal_[i][j] = distances[i][j];
  118. }
  119. }
  120. }
  121. Configuration* VehicleMission::getStartConfiguration() {
  122. return start_;
  123. }
  124. Configuration* VehicleMission::getGoalConfiguration() {
  125. return goal_;
  126. }
  127. unsigned short int VehicleMission::getVehicleID() {
  128. return vehicleID_;
  129. }
  130. std::string VehicleMission::getVehicleName() {
  131. return vehicleName_;
  132. }
  133. VehicleModel* VehicleMission::getVehicleModel() {
  134. return vm_;
  135. }
  136. double VehicleMission::getGridDistanceFromGoal(int x, int y) {
  137. if (gridDistanceFromGoal_.size() != 0) {
  138. return gridDistanceFromGoal_[y][x];
  139. } else {
  140. return 0;
  141. }
  142. }