sim_perfect_control.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /******************************************************************************
  2. * Copyright 2017 The Apollo Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *****************************************************************************/
  16. /**
  17. * @file
  18. */
  19. #pragma once
  20. #include <memory>
  21. #include "gtest/gtest_prod.h"
  22. #include "modules/common_msgs/localization_msgs/localization.pb.h"
  23. #include "modules/common_msgs/planning_msgs/navigation.pb.h"
  24. #include "modules/common_msgs/planning_msgs/planning.pb.h"
  25. #include "modules/common_msgs/planning_msgs/planning_command.pb.h"
  26. #include "modules/common_msgs/prediction_msgs/prediction_obstacle.pb.h"
  27. #include "modules/common_msgs/routing_msgs/routing.pb.h"
  28. #include "modules/common_msgs/control_msgs/control_cmd.pb.h"
  29. #include "cyber/cyber.h"
  30. #include "modules/dreamview/backend/common/dreamview_gflags.h"
  31. #include "modules/dreamview/backend/common/map_service/map_service.h"
  32. #include "modules/dreamview/backend/common/sim_control_manager/core/sim_control_base.h"
  33. /**
  34. * @namespace apollo::dreamview
  35. * @brief apollo::dreamview
  36. */
  37. namespace apollo {
  38. namespace dreamview {
  39. /**
  40. * @class SimPerfectControl
  41. * @brief A module that simulates a 'perfect control' algorithm, which assumes
  42. * an ideal world where the car can be perfectly placed wherever the planning
  43. * asks it to be, with the expected speed, acceleration, etc.
  44. */
  45. class SimPerfectControl final : public SimControlBase {
  46. public:
  47. /**
  48. * @brief Constructor of SimPerfectControl.
  49. * @param map_service the pointer of MapService.
  50. */
  51. explicit SimPerfectControl(const MapService *map_service);
  52. /**
  53. * @brief setup callbacks and timer
  54. * @param set_start_point initialize localization.
  55. */
  56. void Init(bool set_start_point, nlohmann::json start_point_attr,
  57. bool use_start_point_position = false) override;
  58. /**
  59. * @brief Starts the timer to publish simulated localization and chassis
  60. * messages.
  61. */
  62. void Start() override;
  63. /**
  64. * @brief Stops the timer.
  65. */
  66. void Stop() override;
  67. /**
  68. * @brief Set vehicle position.
  69. */
  70. void ReSetPoinstion(double x, double y, double heading) override;
  71. /**
  72. * @brief Resets the internal state.
  73. */
  74. void Reset() override;
  75. void RunOnce() override;
  76. private:
  77. void OnPlanning(
  78. const std::shared_ptr<apollo::planning::ADCTrajectory> &trajectory);
  79. void OnPlanningCommand(
  80. const std::shared_ptr<apollo::planning::PlanningCommand>
  81. &planning_command);
  82. void OnReceiveNavigationInfo(
  83. const std::shared_ptr<apollo::relative_map::NavigationInfo>
  84. &navigation_info);
  85. void OnPredictionObstacles(
  86. const std::shared_ptr<apollo::prediction::PredictionObstacles>
  87. &obstacles);
  88. void onADCLoc(const std::shared_ptr<apollo::localization::LocalizationEstimate> &xloc);
  89. void onControl(
  90. const std::shared_ptr<apollo::control::ControlCommand> &xCmd);
  91. /**
  92. * @brief Predict the next trajectory point using perfect control model
  93. */
  94. bool PerfectControlModel(
  95. apollo::common::TrajectoryPoint *point,
  96. apollo::canbus::Chassis::GearPosition *gear_position);
  97. void PublishChassis(double cur_speed,
  98. apollo::canbus::Chassis::GearPosition gear_position);
  99. void PublishLocalization(const apollo::common::TrajectoryPoint &point);
  100. void PublishDummyPrediction();
  101. void InitTimerAndIO();
  102. /**
  103. * @brief Starts the timer to publish simulated localization and chassis
  104. * messages. Designated Start point for scenario
  105. */
  106. void Start(double x, double y, double v = 0.0, double a = 0.0) override;
  107. void InitStartPoint(double start_velocity, double start_acceleration);
  108. // use scenario start point to init start point under the simulation
  109. // condition.
  110. void InitStartPoint(double x, double y, double start_velocity,
  111. double start_acceleration);
  112. // Reset the start point, which can be a dummy point on the map, a current
  113. // localization pose, or a start position received from the routing module.
  114. void SetStartPoint(const apollo::common::TrajectoryPoint &point);
  115. void Freeze();
  116. void ClearPlanning();
  117. void InternalReset();
  118. const MapService *map_service_ = nullptr;
  119. std::unique_ptr<cyber::Node> node_;
  120. std::shared_ptr<cyber::Reader<apollo::localization::LocalizationEstimate>>
  121. localization_reader_;
  122. std::shared_ptr<cyber::Reader<apollo::planning::ADCTrajectory>>
  123. planning_reader_;
  124. std::shared_ptr<cyber::Reader<apollo::relative_map::NavigationInfo>>
  125. navigation_reader_;
  126. std::shared_ptr<cyber::Reader<apollo::prediction::PredictionObstacles>>
  127. prediction_reader_;
  128. std::shared_ptr<cyber::Reader<apollo::planning::PlanningCommand>>
  129. planning_command_reader_;
  130. std::shared_ptr<cyber::Writer<apollo::localization::LocalizationEstimate>>
  131. localization_writer_;
  132. std::shared_ptr<cyber::Writer<apollo::canbus::Chassis>> chassis_writer_;
  133. std::shared_ptr<cyber::Writer<apollo::prediction::PredictionObstacles>>
  134. prediction_writer_;
  135. std::shared_ptr<cyber::Reader<apollo::localization::LocalizationEstimate>>
  136. localization_adc_reader_;
  137. std::shared_ptr<apollo::cyber::Reader<apollo::control::ControlCommand>>
  138. control_command_reader_;
  139. // The timer to publish simulated localization and chassis messages.
  140. std::unique_ptr<cyber::Timer> sim_control_timer_;
  141. // The timer to publish dummy prediction
  142. std::unique_ptr<cyber::Timer> sim_prediction_timer_;
  143. // Time interval of the timer, in milliseconds.
  144. static constexpr double kSimControlIntervalMs = 10;
  145. static constexpr double kSimPredictionIntervalMs = 100;
  146. // The latest received planning trajectory.
  147. std::shared_ptr<apollo::planning::ADCTrajectory> current_trajectory_;
  148. // The index of the previous and next point with regard to the
  149. // current_trajectory.
  150. int prev_point_index_ = 0;
  151. int next_point_index_ = 0;
  152. // Whether there's a planning received after the most recent routing.
  153. bool received_planning_ = false;
  154. std::shared_ptr<apollo::localization::LocalizationEstimate> adcloc_;
  155. bool receiveadcloc = false;
  156. // Whether start point is initialized from actual localization data
  157. bool start_point_from_localization_ = false;
  158. // Whether to send dummy predictions
  159. bool send_dummy_prediction_ = true;
  160. // The header of the routing planning is following.
  161. apollo::common::Header current_routing_header_;
  162. apollo::common::TrajectoryPoint prev_point_;
  163. apollo::common::TrajectoryPoint next_point_;
  164. common::PathPoint adc_position_;
  165. // Linearize reader/timer callbacks and external operations.
  166. std::mutex mutex_;
  167. // Locks related to timer.
  168. std::mutex timer_mutex_;
  169. double fcmdsteer_ = 0.0;
  170. double fcmdbrake_ = 0.0;
  171. double fcmdacc_ = 0.0;
  172. FRIEND_TEST(SimControlTest, Test);
  173. FRIEND_TEST(SimControlTest, TestDummyPrediction);
  174. };
  175. } // namespace dreamview
  176. } // namespace apollo