32using apollo::common::util::FillHeader;
38using Json = nlohmann::json;
46 : node_(cyber::CreateNode(node_name)),
56 control_cmd_reader_config.
channel_name = FLAGS_control_command_topic;
58 FLAGS_reader_pending_queue_size;
60 FLAGS_prediction_topic,
61 [
this](
const std::shared_ptr<PredictionObstacles>& obstacles) {
65 FLAGS_planning_command,
66 [
this](
const std::shared_ptr<PlanningCommand>& planning_command) {
70 control_cmd_reader_config,
71 [
this](
const std::shared_ptr<ControlCommand>& cmd) {
72 ADEBUG <<
"Received control data: run canbus callback.";
77 localization_reader_config.
channel_name = FLAGS_localization_topic;
78 localization_reader_config.pending_queue_size =
79 FLAGS_reader_pending_queue_size;
81 localization_reader_config,
nullptr);
110 const std::shared_ptr<PredictionObstacles>& obstacles) {
111 std::lock_guard<std::mutex> lock(
mutex_);
116 obstacles->header().module_name() ==
"SimDMPrediction";
120 std::lock_guard<std::mutex> lock(
mutex_);
124 Json start_point_attr({});
127 start_point_attr[
"start_velocity"] = 0.0;
128 start_point_attr[
"start_acceleration"] = 0.0;
129 start_point_attr[
"start_heading"] = std::numeric_limits<double>::max();
130 Init(
true, start_point_attr);
138 std::lock_guard<std::mutex> lock(
mutex_);
141 Json start_point_attr({});
142 start_point_attr[
"start_velocity"] = v;
143 start_point_attr[
"start_acceleration"] = a;
144 start_point_attr[
"start_heading"] = std::numeric_limits<double>::max();
145 start_point_attr[
"x"] = x;
146 start_point_attr[
"y"] = y;
147 Init(
true, start_point_attr,
true);
156 std::lock_guard<std::mutex> lock(
mutex_);
162 point.set_theta(heading);
168 std::lock_guard<std::mutex> lock(
mutex_);
178 std::lock_guard<std::mutex> lock(
mutex_);
193 std::lock_guard<std::mutex> lock(
mutex_);
205 const std::shared_ptr<PlanningCommand>& planning_command) {
206 std::lock_guard<std::mutex> lock(
mutex_);
215 AINFO <<
"planning_command: " << planning_command->DebugString();
228 bool use_start_point_position) {
229 AINFO <<
"Init start point with position!";
237 if (use_start_point_position) {
240 point.set_x(start_point_attr[
"x"]);
241 point.set_y(start_point_attr[
"y"]);
247 start_point_attr[
"y"], &theta, &s);
248 point.set_theta(theta);
254 auto x = pose.position().
x();
255 auto y = pose.position().y();
256 auto z = pose.position().z();
261 point.set_theta(pose.heading());
263 std::hypot(pose.linear_velocity().x(), pose.linear_velocity().y()));
268 pose.linear_acceleration().
x() * pose.linear_velocity().x() +
269 pose.linear_acceleration().y() * pose.linear_velocity().y();
273 double magnitude = std::hypot(pose.linear_acceleration().x(),
274 pose.linear_acceleration().y());
275 point.set_acceleration_s(std::signbit(projection) ? -magnitude
278 point.set_gear_position(0);
285 AWARN <<
"Failed to get a dummy start point from map!";
288 point.set_x(start_point.
x());
289 point.set_y(start_point.
y());
290 point.set_z(start_point.
z());
295 point.set_theta(theta);
298 AINFO <<
"start point from map default point"
299 <<
" x: " << start_point.
x() <<
" y: " << start_point.
y();
311 auto chassis = std::make_shared<Chassis>();
312 apollo::common::util::FillHeader(model_name, chassis.get());
314 chassis->set_engine_started(
true);
318 if (FLAGS_enable_steering_latency) {
339 auto prediction = std::make_shared<PredictionObstacles>();
341 std::lock_guard<std::mutex> lock(
mutex_);
345 FillHeader(
"SimDMPrediction", prediction.get());
351 const std::string model_name) {
352 auto localization = std::make_shared<LocalizationEstimate>();
353 FillHeader(model_name, localization.get());
355 auto* pose = localization->mutable_pose();
360 pose->mutable_position()->set_z(0.0);
364 Eigen::Quaternion<double> cur_orientation =
365 HeadingToQuaternion<double>(cur_theta);
366 ADEBUG <<
"cur_theta" << cur_theta;
367 ADEBUG <<
"cur_orientation_w" << cur_orientation.w() <<
"cur_orientation_x"
368 << cur_orientation.x() <<
"cur_orientation_y" << cur_orientation.y()
369 <<
"cur_orientation_z" << cur_orientation.z();
370 pose->mutable_orientation()->set_qw(cur_orientation.w());
371 pose->mutable_orientation()->set_qx(cur_orientation.x());
372 pose->mutable_orientation()->set_qy(cur_orientation.y());
373 pose->mutable_orientation()->set_qz(cur_orientation.z());
374 pose->set_heading(cur_theta);
378 pose->mutable_linear_velocity()->set_x(std::cos(cur_theta) * -1.0 *
380 pose->mutable_linear_velocity()->set_y(std::sin(cur_theta) * -1.0 *
383 pose->mutable_linear_velocity()->set_x(std::cos(cur_theta) *
385 pose->mutable_linear_velocity()->set_y(std::sin(cur_theta) *
389 pose->mutable_linear_velocity()->set_z(0);
398 pose->mutable_angular_velocity()->set_x(0);
399 pose->mutable_angular_velocity()->set_y(0);
404 pose->mutable_angular_velocity_vrf());
408 auto* linear_acceleration = pose->mutable_linear_acceleration();
409 linear_acceleration->set_x(std::cos(cur_theta) *
411 linear_acceleration->set_y(std::sin(cur_theta) *
413 linear_acceleration->set_z(0);
416 pose->mutable_linear_acceleration_vrf());
Used to perform oneshot or periodic timing tasks
bool PointIsValid(const double x, const double y) const
bool GetStartPoint(apollo::common::PointENU *start_point) const
bool GetPoseWithRegardToLane(const double x, const double y, double *theta, double *s) const
void TransformToVRF(const apollo::common::Point3D &point_mrf, const apollo::common::Quaternion &orientation, apollo::common::Point3D *point_vrf)
std::unique_ptr< cyber::Timer > sim_control_timer_
double start_acceleration_
std::unique_ptr< cyber::Timer > sim_prediction_timer_
bool start_point_from_localization_
virtual void RunOnce()=0
Main logic of the simulated control algorithm.
virtual void Init(bool set_start_point, nlohmann::json start_point_attr, bool use_start_point_position=false)=0
Initialization.
bool send_dummy_prediction_
apollo::sim_control::SimCarStatus current_point_
std::shared_ptr< cyber::Reader< apollo::planning::PlanningCommand > > planning_command_reader_
apollo::control::ControlCommand filtered_control_cmd_
std::shared_ptr< cyber::Writer< apollo::localization::LocalizationEstimate > > localization_writer_
void PublishLocalization(const std::string model_name)
void OnControlCommand(const apollo::control::ControlCommand &control_command)
bool planning_command_is_arrival_
apollo::common::VehicleParam vehicle_param_
void UpdateGearPosition()
void Start() override
Starts the timer to publish simulated localization and chassis messages.
void ReSetPoinstion(double x, double y, double heading) override
Set vehicle position.
bool re_routing_triggered_
static constexpr double kSimPredictionIntervalMs
static constexpr double kModelIntervalMs
void OnPlanningCommand(const std::shared_ptr< apollo::planning::PlanningCommand > &planning_command)
void InitStartPoint(nlohmann::json start_point_attr, bool use_start_point_position=false)
SimControlWithModelBase(const std::string &node_name)
Construct a new Sim Control With Model Base object
MapService * map_service_
void Stop() override
Stops the algorithm.
std::shared_ptr< cyber::Reader< apollo::localization::LocalizationEstimate > > localization_reader_
void OnPredictionObstacles(const std::shared_ptr< apollo::prediction::PredictionObstacles > &obstacles)
std::shared_ptr< cyber::Reader< apollo::prediction::PredictionObstacles > > prediction_reader_
void PublishChassis(const std::string model_name)
pubish chassis info
std::shared_ptr< cyber::Writer< apollo::prediction::PredictionObstacles > > prediction_writer_
std::unique_ptr< cyber::Node > node_
apollo::sim_control::SimCarStatus previous_point_
void PublishDummyPrediction()
publish prediction info
apollo::common::Header current_routing_header_
bool control_command_is_arrival_
std::shared_ptr< cyber::Writer< apollo::canbus::Chassis > > chassis_writer_
std::shared_ptr< cyber::Reader< apollo::control::ControlCommand > > control_command_reader_
apollo::control::ControlCommand control_cmd_
void Reset() override
Resets the internal state.
apollo::common::PathPoint adc_position_
virtual void SetStartPoint(const ::apollo::sim_control::SimCarStatus &point)
SLPoint InterpolateUsingLinearApproximation(const SLPoint &p0, const SLPoint &p1, const double w)
Eigen::Quaternion< T > HeadingToQuaternion(T heading)
Eigen::Vector3d InverseQuaternionRotate(const Quaternion &orientation, const Eigen::Vector3d &rotated)
double QuaternionToHeading(const double qw, const double qx, const double qy, const double qz)
double NormalizeAngle(const double angle)
Normalize angle to [-PI, PI).
optional double max_steer_angle
optional double steer_ratio
optional double wheel_base
optional double steering_target
optional apollo::canbus::Chassis::GearPosition gear_location
uint32_t pending_queue_size
configuration for responding ChannelBuffer.
optional double acceleration_s