Apollo  6.0
Open source self driving car software
sim_control.h
Go to the documentation of this file.
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 
21 #pragma once
22 
23 #include <memory>
24 
25 #include "cyber/cyber.h"
26 
27 #include "gtest/gtest_prod.h"
28 
29 #include "modules/localization/proto/localization.pb.h"
30 #include "modules/map/relative_map/proto/navigation.pb.h"
31 #include "modules/planning/proto/planning.pb.h"
32 #include "modules/prediction/proto/prediction_obstacle.pb.h"
33 
37 
42 namespace apollo {
43 namespace dreamview {
44 
52  public:
57  explicit SimControl(const MapService *map_service);
58 
59  bool IsEnabled() const { return enabled_; }
60 
65  void Init(double start_velocity = 0.0,
66  double start_acceleration = 0.0) override;
67 
72  void Start() override;
73 
77  void Stop() override;
78 
82  void Reset() override;
83 
84  void RunOnce() override;
85 
86  private:
87  void OnPlanning(
88  const std::shared_ptr<apollo::planning::ADCTrajectory> &trajectory);
89  void OnRoutingResponse(
90  const std::shared_ptr<apollo::routing::RoutingResponse> &routing);
91  void OnReceiveNavigationInfo(
92  const std::shared_ptr<apollo::relative_map::NavigationInfo>
93  &navigation_info);
94  void OnPredictionObstacles(
95  const std::shared_ptr<apollo::prediction::PredictionObstacles>
96  &obstacles);
97 
101  bool PerfectControlModel(
102  apollo::common::TrajectoryPoint *point,
103  apollo::canbus::Chassis::GearPosition *gear_position);
104 
105  void PublishChassis(double cur_speed,
106  apollo::canbus::Chassis::GearPosition gear_position);
107 
108  void PublishLocalization(const apollo::common::TrajectoryPoint &point);
109 
110  void PublishDummyPrediction();
111 
112  void InitTimerAndIO();
113 
114  void InitStartPoint(double start_velocity, double start_acceleration);
115 
116  // Reset the start point, which can be a dummy point on the map, a current
117  // localization pose, or a start position received from the routing module.
118  void SetStartPoint(const apollo::common::TrajectoryPoint &point);
119 
120  void Freeze();
121 
122  void ClearPlanning();
123 
124  void InternalReset();
125 
126  const MapService *map_service_ = nullptr;
127 
128  std::unique_ptr<cyber::Node> node_;
129 
130  std::shared_ptr<cyber::Reader<apollo::localization::LocalizationEstimate>>
131  localization_reader_;
132  std::shared_ptr<cyber::Reader<apollo::planning::ADCTrajectory>>
133  planning_reader_;
134  std::shared_ptr<cyber::Reader<apollo::routing::RoutingResponse>>
135  routing_response_reader_;
136  std::shared_ptr<cyber::Reader<apollo::relative_map::NavigationInfo>>
137  navigation_reader_;
138  std::shared_ptr<cyber::Reader<apollo::prediction::PredictionObstacles>>
139  prediction_reader_;
140 
141  std::shared_ptr<cyber::Writer<apollo::localization::LocalizationEstimate>>
142  localization_writer_;
143  std::shared_ptr<cyber::Writer<apollo::canbus::Chassis>> chassis_writer_;
144  std::shared_ptr<cyber::Writer<apollo::prediction::PredictionObstacles>>
145  prediction_writer_;
146 
147  // The timer to publish simulated localization and chassis messages.
148  std::unique_ptr<cyber::Timer> sim_control_timer_;
149 
150  // The timer to publish dummy prediction
151  std::unique_ptr<cyber::Timer> sim_prediction_timer_;
152 
153  // Time interval of the timer, in milliseconds.
154  static constexpr double kSimControlIntervalMs = 10;
155  static constexpr double kSimPredictionIntervalMs = 100;
156 
157  // The latest received planning trajectory.
158  std::shared_ptr<apollo::planning::ADCTrajectory> current_trajectory_;
159  // The index of the previous and next point with regard to the
160  // current_trajectory.
161  int prev_point_index_ = 0;
162  int next_point_index_ = 0;
163 
164  // Whether there's a planning received after the most recent routing.
165  bool received_planning_ = false;
166 
167  // Whether planning has requested a re-routing.
168  bool re_routing_triggered_ = false;
169 
170  // Whether the sim control is enabled.
171  bool enabled_ = false;
172 
173  // Whether start point is initialized from actual localization data
174  bool start_point_from_localization_ = false;
175 
176  // Whether to send dummy predictions
177  bool send_dummy_prediction_ = true;
178 
179  // The header of the routing planning is following.
180  apollo::common::Header current_routing_header_;
181 
182  apollo::common::TrajectoryPoint prev_point_;
183  apollo::common::TrajectoryPoint next_point_;
184 
185  common::PathPoint adc_position_;
186 
187  // Linearize reader/timer callbacks and external operations.
188  std::mutex mutex_;
189 
190  FRIEND_TEST(SimControlTest, Test);
191  FRIEND_TEST(SimControlTest, TestDummyPrediction);
192 };
193 
194 } // namespace dreamview
195 } // namespace apollo
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
Definition: map_service.h:40
void Init(double start_velocity=0.0, double start_acceleration=0.0) override
setup callbacks and timer
bool IsEnabled() const
Definition: sim_control.h:59
SimControl(const MapService *map_service)
Constructor of SimControl.
A module that simulates a &#39;perfect control&#39; algorithm, which assumes an ideal world where the car can...
Definition: sim_control.h:51
Interface of simulated control algorithm.
Definition: sim_control_interface.h:31
void RunOnce() override
Main logic of the simulated control algorithm.
void Stop() override
Stops the timer.
void Start() override
Starts the timer to publish simulated localization and chassis messages.
void Reset() override
Resets the internal state.