/****************************************************************************** * Copyright 2017 The Apollo Authors. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *****************************************************************************/ #include "modules/dreamview/backend/common/sim_control_manager/dynamic_model/perfect_control/sim_perfect_control.h" #include "cyber/common/file.h" #include "cyber/time/clock.h" #include "modules/common/adapters/adapter_gflags.h" #include "modules/common/math/linear_interpolation.h" #include "modules/common/math/math_utils.h" #include "modules/common/math/quaternion.h" #include "modules/common/util/message_util.h" #include "modules/common/util/util.h" #include namespace apollo { namespace dreamview { using apollo::canbus::Chassis; using apollo::common::Header; using apollo::common::Point3D; using apollo::common::Quaternion; using apollo::common::TrajectoryPoint; using apollo::common::math::HeadingToQuaternion; using apollo::common::math::InterpolateUsingLinearApproximation; using apollo::common::math::InverseQuaternionRotate; using apollo::common::util::FillHeader; using apollo::cyber::Clock; using apollo::localization::LocalizationEstimate; using apollo::planning::ADCTrajectory; using apollo::planning::PlanningCommand; using apollo::prediction::PredictionObstacles; using apollo::relative_map::NavigationInfo; using Json = nlohmann::json; #include #include #include #include void adclog1(const char * strlog){ std::ofstream myFile; // myFile.open("/home/yuchuli/adclog.txt", std::ios_base::app); myFile.open("/apollo/adclog.txt", std::ios_base::app); if(myFile.is_open()) { auto now = std::chrono::system_clock::now(); // std::time_t now_c = std::chrono::system_clock::to_time_t(now - std::chrono::hours(24)); std::time_t now_c = std::chrono::system_clock::to_time_t(now); myFile< trajectory.Get(next_point_index_).relative_time() + current_trajectory_->header().timestamp_sec()) { ++next_point_index_; } if (next_point_index_ >= trajectory.size()) { next_point_index_ = trajectory.size() - 1; } if (next_point_index_ == 0) { AERROR << "First trajectory point is a future point!"; return false; } prev_point_index_ = next_point_index_ - 1; next_point_ = trajectory.Get(next_point_index_); prev_point_ = trajectory.Get(prev_point_index_); } } if (current_time > next_point_.relative_time() + current_trajectory_->header().timestamp_sec()) { // Don't try to extrapolate if relative_time passes last point *point = next_point_; } else { *point = InterpolateUsingLinearApproximation( prev_point_, next_point_, current_time - current_trajectory_->header().timestamp_sec()); } return true; } void SimPerfectControl::PublishChassis(double cur_speed, Chassis::GearPosition gear_position) { auto chassis = std::make_shared(); FillHeader("SimPerfectControl", chassis.get()); chassis->set_engine_started(true); chassis->set_driving_mode(Chassis::COMPLETE_AUTO_DRIVE); chassis->set_gear_location(gear_position); chassis->set_speed_mps(static_cast(cur_speed)); if (gear_position == canbus::Chassis::GEAR_REVERSE) { chassis->set_speed_mps(-chassis->speed_mps()); } if((FLAGS_use_realveh == true) && (receiveadcloc)){ chassis->set_speed_mps(static_cast(fNowVel)); } chassis->set_throttle_percentage(fcmdacc_); chassis->set_brake_percentage(fcmdbrake_); chassis->set_steering_percentage(fcmdsteer_); chassis_writer_->Write(chassis); } void SimPerfectControl::PublishLocalization(const TrajectoryPoint &point) { auto localization = std::make_shared(); FillHeader("SimPerfectControl", localization.get()); if((receiveadcloc)) { localization = adcloc_; localization_writer_->Write(localization); auto *pose = localization->mutable_pose(); fNowVel = sqrt(pow(pose->mutable_linear_velocity()->x(),2)+pow(pose->mutable_linear_velocity()->y(),2)); adc_position_.set_x(pose->position().x()); adc_position_.set_y(pose->position().y()); adc_position_.set_z(pose->position().z()); return; } auto *pose = localization->mutable_pose(); auto prev = prev_point_.path_point(); auto next = next_point_.path_point(); // Set position 507530.95 4331929.85 pose->mutable_position()->set_x(point.path_point().x()); pose->mutable_position()->set_y(point.path_point().y()); pose->mutable_position()->set_z(point.path_point().z()); // Set orientation and heading double cur_theta = point.path_point().theta(); if (FLAGS_use_navigation_mode) { double flu_x = point.path_point().x(); double flu_y = point.path_point().y(); Eigen::Vector2d enu_coordinate = common::math::RotateVector2d({flu_x, flu_y}, cur_theta); enu_coordinate.x() += adc_position_.x(); enu_coordinate.y() += adc_position_.y(); pose->mutable_position()->set_x(enu_coordinate.x()); pose->mutable_position()->set_y(enu_coordinate.y()); } Eigen::Quaternion cur_orientation = HeadingToQuaternion(cur_theta); pose->mutable_orientation()->set_qw(cur_orientation.w()); pose->mutable_orientation()->set_qx(cur_orientation.x()); pose->mutable_orientation()->set_qy(cur_orientation.y()); pose->mutable_orientation()->set_qz(cur_orientation.z()); pose->set_heading(cur_theta); // Set linear_velocity pose->mutable_linear_velocity()->set_x(std::cos(cur_theta) * point.v()); pose->mutable_linear_velocity()->set_y(std::sin(cur_theta) * point.v()); pose->mutable_linear_velocity()->set_z(0); // Set angular_velocity in both map reference frame and vehicle reference // frame pose->mutable_angular_velocity()->set_x(0); pose->mutable_angular_velocity()->set_y(0); pose->mutable_angular_velocity()->set_z(point.v() * point.path_point().kappa()); TransformToVRF(pose->angular_velocity(), pose->orientation(), pose->mutable_angular_velocity_vrf()); // Set linear_acceleration in both map reference frame and vehicle reference // frame auto *linear_acceleration = pose->mutable_linear_acceleration(); linear_acceleration->set_x(std::cos(cur_theta) * point.a()); linear_acceleration->set_y(std::sin(cur_theta) * point.a()); linear_acceleration->set_z(0); TransformToVRF(pose->linear_acceleration(), pose->orientation(), pose->mutable_linear_acceleration_vrf()); localization_writer_->Write(localization); adc_position_.set_x(pose->position().x()); adc_position_.set_y(pose->position().y()); adc_position_.set_z(pose->position().z()); } void SimPerfectControl::PublishDummyPrediction() { if(receiveadcloc)return; auto prediction = std::make_shared(); { std::lock_guard lock(mutex_); if (!send_dummy_prediction_) { return; } FillHeader("SimPrediction", prediction.get()); } prediction_writer_->Write(prediction); } } // namespace dreamview } // namespace apollo