SimuDecisionMaker.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /// \file SimuDecisionMaker.cpp
  2. /// \brief Decision Maker for Simulated Vehicles
  3. /// \author Hatem Darweesh
  4. /// \date Jan 10, 2018
  5. #include "op_planner/SimuDecisionMaker.h"
  6. #include "op_utility/UtilityH.h"
  7. #include "op_planner/PlanningHelpers.h"
  8. #include "op_planner/MappingHelpers.h"
  9. #include "op_planner/MatrixOperations.h"
  10. #include "op_planner/PlannerH.h"
  11. namespace PlannerHNS
  12. {
  13. SimuDecisionMaker::SimuDecisionMaker()
  14. {
  15. m_CurrentVelocity = m_CurrentVelocityD =0;
  16. m_CurrentSteering = m_CurrentSteeringD =0;
  17. m_CurrentShift = m_CurrentShiftD = PlannerHNS::SHIFT_POS_NN;
  18. m_CurrentAccSteerAngle = m_CurrentAccVelocity = 0;
  19. m_SimulationSteeringDelayFactor = 0.1;
  20. UtilityHNS::UtilityH::GetTickCount(m_SteerDelayTimer);
  21. }
  22. SimuDecisionMaker::~SimuDecisionMaker()
  23. {
  24. }
  25. void SimuDecisionMaker::ReInitializePlanner(const WayPoint& start_pose)
  26. {
  27. m_pidVelocity.Init(0.01, 0.004, 0.01);
  28. m_pidVelocity.Setlimit(m_params.maxSpeed, 0);
  29. m_pidStopping.Init(0.05, 0.05, 0.1);
  30. m_pidStopping.Setlimit(m_params.horizonDistance, 0);
  31. m_pidFollowing.Init(0.05, 0.05, 0.01);
  32. m_pidFollowing.Setlimit(m_params.minFollowingDistance, 0);
  33. m_iCurrentTotalPathId = 0;
  34. m_CurrentVelocity = m_CurrentVelocityD =0;
  35. m_CurrentSteering = m_CurrentSteeringD =0;
  36. m_CurrentShift = m_CurrentShiftD = SHIFT_POS_NN;
  37. m_CurrentAccSteerAngle = m_CurrentAccVelocity = 0;
  38. m_pCurrentBehaviorState = m_pFollowState;
  39. m_TotalPath.clear();
  40. m_TotalOriginalPath.clear();
  41. m_Path.clear();
  42. m_RollOuts.clear();
  43. m_pCurrentBehaviorState->m_Behavior = PlannerHNS::FORWARD_STATE;
  44. FirstLocalizeMe(start_pose);
  45. LocalizeMe(0);
  46. }
  47. void SimuDecisionMaker::SetSimulatedTargetOdometryReadings(const double& velocity_d, const double& steering_d, const SHIFT_POS& shift_d)
  48. {
  49. m_CurrentVelocityD = velocity_d;
  50. m_CurrentSteeringD = steering_d;
  51. m_CurrentShiftD = shift_d;
  52. }
  53. void SimuDecisionMaker::FirstLocalizeMe(const WayPoint& initCarPos)
  54. {
  55. pLane = initCarPos.pLane;
  56. state = initCarPos;
  57. m_OdometryState.pos.a = initCarPos.pos.a;
  58. m_OdometryState.pos.x = initCarPos.pos.x + (m_CarInfo.wheel_base/2.0 * cos(initCarPos.pos.a));
  59. m_OdometryState.pos.y = initCarPos.pos.y + (m_CarInfo.wheel_base/2.0 * sin(initCarPos.pos.a));
  60. }
  61. void SimuDecisionMaker::LocalizeMe(const double& dt)
  62. {
  63. //calculate the new x, y ,
  64. WayPoint currPose = state;
  65. if(m_CurrentShift == SHIFT_POS_DD)
  66. {
  67. m_OdometryState.pos.x += m_CurrentVelocity * dt * cos(currPose.pos.a);
  68. m_OdometryState.pos.y += m_CurrentVelocity * dt * sin(currPose.pos.a);
  69. m_OdometryState.pos.a += m_CurrentVelocity * dt * tan(m_CurrentSteering) / m_CarInfo.wheel_base;
  70. }
  71. else if(m_CurrentShift == SHIFT_POS_RR )
  72. {
  73. m_OdometryState.pos.x += -m_CurrentVelocity * dt * cos(currPose.pos.a);
  74. m_OdometryState.pos.y += -m_CurrentVelocity * dt * sin(currPose.pos.a);
  75. m_OdometryState.pos.a += -m_CurrentVelocity * dt * tan(m_CurrentSteering);
  76. }
  77. m_OdometryState.pos.a = atan2(sin(m_OdometryState.pos.a), cos(m_OdometryState.pos.a));
  78. m_OdometryState.pos.a = UtilityHNS::UtilityH::FixNegativeAngle(m_OdometryState.pos.a);
  79. state.pos.a = m_OdometryState.pos.a;
  80. state.pos.x = m_OdometryState.pos.x - (m_CurrentVelocity*dt* (m_CarInfo.wheel_base) * cos (m_OdometryState.pos.a));
  81. state.pos.y = m_OdometryState.pos.y - (m_CurrentVelocity*dt* (m_CarInfo.wheel_base/2.0) * sin (m_OdometryState.pos.a));
  82. }
  83. void SimuDecisionMaker::UpdateState(const PlannerHNS::VehicleState& state, const bool& bUseDelay)
  84. {
  85. if(!bUseDelay)
  86. {
  87. m_CurrentSteering = m_CurrentSteeringD;
  88. }
  89. else
  90. {
  91. double currSteerDeg = RAD2DEG * m_CurrentSteering;
  92. double desiredSteerDeg = RAD2DEG * m_CurrentSteeringD;
  93. double mFact = UtilityHNS::UtilityH::GetMomentumScaleFactor(state.speed);
  94. double diff = desiredSteerDeg - currSteerDeg;
  95. double diffSign = UtilityHNS::UtilityH::GetSign(diff);
  96. double inc = 1.0*diffSign;
  97. if(fabs(diff) < 1.0 )
  98. inc = diff;
  99. if(UtilityHNS::UtilityH::GetTimeDiffNow(m_SteerDelayTimer) > m_SimulationSteeringDelayFactor*mFact)
  100. {
  101. UtilityHNS::UtilityH::GetTickCount(m_SteerDelayTimer);
  102. currSteerDeg += inc;
  103. }
  104. m_CurrentSteering = DEG2RAD * currSteerDeg;
  105. }
  106. m_CurrentShift = m_CurrentShiftD;
  107. m_CurrentVelocity = m_CurrentVelocityD;
  108. }
  109. void SimuDecisionMaker::GenerateLocalRollOuts()
  110. {
  111. std::vector<PlannerHNS::WayPoint> sampledPoints_debug;
  112. std::vector<std::vector<std::vector<PlannerHNS::WayPoint> > > _roll_outs;
  113. PlannerHNS::PlannerH _planner;
  114. _planner.GenerateRunoffTrajectory(m_TotalPath, state,
  115. m_params.enableLaneChange,
  116. state.v,
  117. m_params.microPlanDistance,
  118. m_params.maxSpeed,
  119. m_params.minSpeed,
  120. m_params.carTipMargin,
  121. m_params.rollInMargin,
  122. m_params.rollInSpeedFactor,
  123. m_params.pathDensity,
  124. m_params.rollOutDensity,
  125. m_params.rollOutNumber,
  126. m_params.smoothingDataWeight,
  127. m_params.smoothingSmoothWeight,
  128. m_params.smoothingToleranceError,
  129. m_params.speedProfileFactor,
  130. m_params.enableHeadingSmoothing,
  131. -1 , -1,
  132. _roll_outs, sampledPoints_debug);
  133. if(_roll_outs.size()>0)
  134. m_RollOuts.clear();
  135. for(unsigned int i=0; i < _roll_outs.size(); i++)
  136. {
  137. for(unsigned int j=0; j < _roll_outs.at(i).size(); j++)
  138. {
  139. PlannerHNS::PlanningHelpers::PredictConstantTimeCostForTrajectory(_roll_outs.at(i).at(j), state, m_params.minSpeed, m_params.microPlanDistance);
  140. m_RollOuts.push_back(_roll_outs.at(i).at(j));
  141. }
  142. }
  143. }
  144. bool SimuDecisionMaker::SelectSafeTrajectory()
  145. {
  146. bool bNewTrajectory = false;
  147. PlannerHNS::PreCalculatedConditions *preCalcPrams = m_pCurrentBehaviorState->GetCalcParams();
  148. if(!preCalcPrams || m_TotalPath.size()==0) return bNewTrajectory;
  149. int currIndex = PlannerHNS::PlanningHelpers::GetClosestNextPointIndexFast(m_Path, state);
  150. int index_limit = 0;
  151. if(index_limit<=0)
  152. index_limit = m_Path.size()/2.0;
  153. if(currIndex > index_limit
  154. || preCalcPrams->bRePlan
  155. || preCalcPrams->bNewGlobalPath)
  156. {
  157. GenerateLocalRollOuts();
  158. if(m_RollOuts.size() <= preCalcPrams->iCurrSafeTrajectory)
  159. return false;
  160. std::cout << "New Local Plan !! " << currIndex << ", "<< preCalcPrams->bRePlan << ", " << preCalcPrams->bNewGlobalPath << ", " << m_TotalOriginalPath.at(0).size() << ", PrevLocal: " << m_Path.size();
  161. std::cout << ", NewLocal: " << m_Path.size() << std::endl;
  162. m_Path = m_RollOuts.at(preCalcPrams->iCurrSafeTrajectory);
  163. preCalcPrams->bNewGlobalPath = false;
  164. preCalcPrams->bRePlan = false;
  165. bNewTrajectory = true;
  166. }
  167. return bNewTrajectory;
  168. }
  169. PlannerHNS::VehicleState SimuDecisionMaker::LocalizeStep(const double& dt, const PlannerHNS::VehicleState& desiredStatus)
  170. {
  171. SetSimulatedTargetOdometryReadings(desiredStatus.speed, desiredStatus.steer, desiredStatus.shift);
  172. UpdateState(desiredStatus, false);
  173. LocalizeMe(dt);
  174. PlannerHNS::VehicleState currStatus;
  175. currStatus.shift = desiredStatus.shift;
  176. currStatus.steer = m_CurrentSteering;
  177. currStatus.speed = m_CurrentVelocity;
  178. return currStatus;
  179. }
  180. PlannerHNS::BehaviorState SimuDecisionMaker::DoOneStep(const double& dt,
  181. const PlannerHNS::VehicleState& vehicleState,
  182. const int& goalID,
  183. const std::vector<TrafficLight>& trafficLight,
  184. const std::vector<PlannerHNS::DetectedObject>& objects,
  185. const bool& bEmergencyStop)
  186. {
  187. PlannerHNS::BehaviorState beh;
  188. state.v = vehicleState.speed;
  189. m_TotalPath.clear();
  190. for(unsigned int i = 0; i < m_TotalOriginalPath.size(); i++)
  191. {
  192. t_centerTrajectorySmoothed.clear();
  193. PlannerHNS::PlanningHelpers::ExtractPartFromPointToDistanceDirectionFast(m_TotalOriginalPath.at(i), state, m_params.horizonDistance , m_params.pathDensity , t_centerTrajectorySmoothed);
  194. m_TotalPath.push_back(t_centerTrajectorySmoothed);
  195. }
  196. if(m_TotalPath.size()==0) return beh;
  197. UpdateCurrentLane(m_MaxLaneSearchDistance);
  198. PlannerHNS::TrajectoryCost tc = m_TrajectoryCostsCalculator.DoOneStepStatic(m_RollOuts, m_TotalPath.at(m_iCurrentTotalPathId), state, m_params, m_CarInfo, vehicleState, objects);
  199. //std::cout << "Detected Objects Distance: " << tc.closest_obj_distance << ", N RollOuts: " << m_RollOuts.size() << std::endl;
  200. CalculateImportantParameterForDecisionMaking(vehicleState, goalID, bEmergencyStop, trafficLight, tc);
  201. beh = GenerateBehaviorState(vehicleState);
  202. beh.bNewPlan = SelectSafeTrajectory();
  203. beh.maxVelocity = UpdateVelocityDirectlyToTrajectory(beh, vehicleState, dt);
  204. //std::cout << "Eval_i: " << tc.index << ", Curr_i: " << m_pCurrentBehaviorState->GetCalcParams()->iCurrSafeTrajectory << ", Prev_i: " << m_pCurrentBehaviorState->GetCalcParams()->iPrevSafeTrajectory << std::endl;
  205. return beh;
  206. }
  207. }