Apollo 10.0
自动驾驶开放平台
vehicle_state_provider.cc
浏览该文件的文档.
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
18
19#include <cmath>
20
21#include "Eigen/Core"
22#include "absl/strings/str_cat.h"
23#include "cyber/common/log.h"
27
28namespace apollo {
29namespace common {
30
32 const localization::LocalizationEstimate &localization,
33 const canbus::Chassis &chassis) {
34 original_localization_ = localization;
35 if (!ConstructExceptLinearVelocity(localization)) {
36 std::string msg = absl::StrCat(
37 "Fail to update because ConstructExceptLinearVelocity error.",
38 "localization:\n", localization.DebugString());
40 }
41 if (localization.has_measurement_time()) {
42 vehicle_state_.set_timestamp(localization.measurement_time());
43 } else if (localization.header().has_timestamp_sec()) {
44 vehicle_state_.set_timestamp(localization.header().timestamp_sec());
45 } else if (chassis.has_header() && chassis.header().has_timestamp_sec()) {
46 AERROR << "Unable to use location timestamp for vehicle state. Use chassis "
47 "time instead.";
48 vehicle_state_.set_timestamp(chassis.header().timestamp_sec());
49 }
50
51 if (chassis.has_gear_location()) {
52 vehicle_state_.set_gear(chassis.gear_location());
53 } else {
54 vehicle_state_.set_gear(canbus::Chassis::GEAR_NONE);
55 }
56
57 if (chassis.has_speed_mps()) {
58 vehicle_state_.set_linear_velocity(chassis.speed_mps());
59 if (!FLAGS_reverse_heading_vehicle_state &&
60 vehicle_state_.gear() == canbus::Chassis::GEAR_REVERSE) {
61 vehicle_state_.set_linear_velocity(-vehicle_state_.linear_velocity());
62 }
63 }
64
65 if (chassis.has_steering_percentage()) {
66 vehicle_state_.set_steering_percentage(chassis.steering_percentage());
67 }
68
69 static constexpr double kEpsilon = 0.1;
70 if (std::abs(vehicle_state_.linear_velocity()) < kEpsilon) {
71 vehicle_state_.set_kappa(0.0);
72 } else {
73 vehicle_state_.set_kappa(vehicle_state_.angular_velocity() /
74 vehicle_state_.linear_velocity());
75 }
76
77 vehicle_state_.set_driving_mode(chassis.driving_mode());
78
79 return Status::OK();
80}
81
82bool VehicleStateProvider::ConstructExceptLinearVelocity(
83 const localization::LocalizationEstimate &localization) {
84 if (!localization.has_pose()) {
85 AERROR << "Invalid localization input.";
86 return false;
87 }
88
89 // skip localization update when it is in use_navigation_mode.
90 if (FLAGS_use_navigation_mode) {
91 ADEBUG << "Skip localization update when it is in use_navigation_mode.";
92 return true;
93 }
94
95 vehicle_state_.mutable_pose()->CopyFrom(localization.pose());
96 if (localization.pose().has_position()) {
97 vehicle_state_.set_x(localization.pose().position().x());
98 vehicle_state_.set_y(localization.pose().position().y());
99 vehicle_state_.set_z(localization.pose().position().z());
100 }
101
102 const auto &orientation = localization.pose().orientation();
103
104 if (localization.pose().has_heading()) {
105 vehicle_state_.set_heading(localization.pose().heading());
106 } else {
107 vehicle_state_.set_heading(
108 math::QuaternionToHeading(orientation.qw(), orientation.qx(),
109 orientation.qy(), orientation.qz()));
110 }
111
112 if (FLAGS_enable_map_reference_unify) {
113 if (!localization.pose().has_angular_velocity_vrf()) {
114 AERROR << "localization.pose().has_angular_velocity_vrf() must be true "
115 "when FLAGS_enable_map_reference_unify is true.";
116 return false;
117 }
118 vehicle_state_.set_angular_velocity(
119 localization.pose().angular_velocity_vrf().z());
120
121 if (!localization.pose().has_linear_acceleration_vrf()) {
122 AERROR << "localization.pose().has_linear_acceleration_vrf() must be "
123 "true when FLAGS_enable_map_reference_unify is true.";
124 return false;
125 }
126 vehicle_state_.set_linear_acceleration(
127 localization.pose().linear_acceleration_vrf().y());
128 } else {
129 if (!localization.pose().has_angular_velocity()) {
130 AERROR << "localization.pose() has no angular velocity.";
131 return false;
132 }
133 vehicle_state_.set_angular_velocity(
134 localization.pose().angular_velocity().z());
135
136 if (!localization.pose().has_linear_acceleration()) {
137 AERROR << "localization.pose() has no linear acceleration.";
138 return false;
139 }
140 vehicle_state_.set_linear_acceleration(
141 localization.pose().linear_acceleration().y());
142 }
143
144 if (localization.pose().has_euler_angles()) {
145 vehicle_state_.set_roll(localization.pose().euler_angles().y());
146 vehicle_state_.set_pitch(localization.pose().euler_angles().x());
147 vehicle_state_.set_yaw(localization.pose().euler_angles().z());
148 } else {
149 math::EulerAnglesZXYd euler_angle(orientation.qw(), orientation.qx(),
150 orientation.qy(), orientation.qz());
151 vehicle_state_.set_roll(euler_angle.roll());
152 vehicle_state_.set_pitch(euler_angle.pitch());
153 vehicle_state_.set_yaw(euler_angle.yaw());
154 }
155
156 return true;
157}
158
159double VehicleStateProvider::x() const { return vehicle_state_.x(); }
160
161double VehicleStateProvider::y() const { return vehicle_state_.y(); }
162
163double VehicleStateProvider::z() const { return vehicle_state_.z(); }
164
165double VehicleStateProvider::roll() const { return vehicle_state_.roll(); }
166
167double VehicleStateProvider::pitch() const { return vehicle_state_.pitch(); }
168
169double VehicleStateProvider::yaw() const { return vehicle_state_.yaw(); }
170
172 return vehicle_state_.heading();
173}
174
175double VehicleStateProvider::kappa() const { return vehicle_state_.kappa(); }
176
178 return vehicle_state_.linear_velocity();
179}
180
182 return vehicle_state_.angular_velocity();
183}
184
186 return vehicle_state_.linear_acceleration();
187}
188
189double VehicleStateProvider::gear() const { return vehicle_state_.gear(); }
190
192 return vehicle_state_.steering_percentage();
193}
194
196 return vehicle_state_.timestamp();
197}
198
200 return vehicle_state_.pose();
201}
202
204 return original_localization_.pose();
205}
206
207void VehicleStateProvider::set_linear_velocity(const double linear_velocity) {
208 vehicle_state_.set_linear_velocity(linear_velocity);
209}
210
212 return vehicle_state_;
213}
214
216 Eigen::Vector3d vec_distance(0.0, 0.0, 0.0);
217 double v = vehicle_state_.linear_velocity();
218 // Predict distance travel vector
219 if (std::fabs(vehicle_state_.angular_velocity()) < 0.0001) {
220 vec_distance[0] = 0.0;
221 vec_distance[1] = v * t;
222 } else {
223 vec_distance[0] = -v / vehicle_state_.angular_velocity() *
224 (1.0 - std::cos(vehicle_state_.angular_velocity() * t));
225 vec_distance[1] = std::sin(vehicle_state_.angular_velocity() * t) * v /
226 vehicle_state_.angular_velocity();
227 }
228
229 // If we have rotation information, take it into consideration.
230 if (vehicle_state_.pose().has_orientation()) {
231 const auto &orientation = vehicle_state_.pose().orientation();
232 Eigen::Quaternion<double> quaternion(orientation.qw(), orientation.qx(),
233 orientation.qy(), orientation.qz());
234 Eigen::Vector3d pos_vec(vehicle_state_.x(), vehicle_state_.y(),
235 vehicle_state_.z());
236 const Eigen::Vector3d future_pos_3d =
237 quaternion.toRotationMatrix() * vec_distance + pos_vec;
238 return math::Vec2d(future_pos_3d[0], future_pos_3d[1]);
239 }
240
241 // If no valid rotation information provided from localization,
242 // return the estimated future position without rotation.
243 return math::Vec2d(vec_distance[0] + vehicle_state_.x(),
244 vec_distance[1] + vehicle_state_.y());
245}
246
248 const double rear_to_com_distance) const {
249 // set length as distance between rear wheel and center of mass.
250 Eigen::Vector3d v;
251 if ((FLAGS_state_transform_to_com_reverse &&
252 vehicle_state_.gear() == canbus::Chassis::GEAR_REVERSE) ||
253 (FLAGS_state_transform_to_com_drive &&
254 vehicle_state_.gear() == canbus::Chassis::GEAR_DRIVE)) {
255 v << 0.0, rear_to_com_distance, 0.0;
256 } else {
257 v << 0.0, 0.0, 0.0;
258 }
259 Eigen::Vector3d pos_vec(vehicle_state_.x(), vehicle_state_.y(),
260 vehicle_state_.z());
261 // Initialize the COM position without rotation
262 Eigen::Vector3d com_pos_3d = v + pos_vec;
263
264 // If we have rotation information, take it into consideration.
265 if (vehicle_state_.pose().has_orientation()) {
266 const auto &orientation = vehicle_state_.pose().orientation();
267 Eigen::Quaternion<double> quaternion(orientation.qw(), orientation.qx(),
268 orientation.qy(), orientation.qz());
269 // Update the COM position with rotation
270 com_pos_3d = quaternion.toRotationMatrix() * v + pos_vec;
271 }
272 return math::Vec2d(com_pos_3d[0], com_pos_3d[1]);
273}
274
275} // namespace common
276} // namespace apollo
A general class to denote the return status of an API call.
Definition status.h:43
static Status OK()
generate a success status.
Definition status.h:60
Status Update(const localization::LocalizationEstimate &localization, const canbus::Chassis &chassis)
Constructor by information of localization and chassis.
double linear_velocity() const
Get the vehicle's linear velocity.
const localization::Pose & pose() const
double angular_velocity() const
Get the vehicle's angular velocity.
double yaw() const
Get the vehicle yaw angle.
const localization::Pose & original_pose() const
double x() const
Get the x-coordinate of vehicle position.
double kappa() const
Get the kappa of vehicle position.
double z() const
Get the z coordinate of vehicle position.
double pitch() const
Get the vehicle pitch angle.
double steering_percentage() const
Get the vehicle's steering angle.
double gear() const
Get the vehicle's gear position.
void set_linear_velocity(const double linear_velocity)
Set the vehicle's linear velocity.
double roll() const
Get the vehicle roll angle.
math::Vec2d ComputeCOMPosition(const double rear_to_com_distance) const
Compute the position of center of mass(COM) of the vehicle, given the distance from rear wheels to th...
double y() const
Get the y-coordinate of vehicle position.
double linear_acceleration() const
Get the vehicle's linear acceleration.
double heading() const
Get the heading of vehicle position, which is the angle between the vehicle's heading direction and t...
math::Vec2d EstimateFuturePosition(const double t) const
Estimate future position from current position and heading, along a period of time,...
Implements a class of 2-dimensional vectors.
Definition vec2d.h:42
Defines the EulerAnglesZXY class.
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
EulerAnglesZXY< double > EulerAnglesZXYd
double QuaternionToHeading(const double qw, const double qx, const double qy, const double qz)
Definition quaternion.h:56
class register implement
Definition arena_queue.h:37
Contains a number of helper functions related to quaternions.
optional GearPosition gear_location
optional apollo::common::Header header
optional DrivingMode driving_mode
optional float steering_percentage
Definition chassis.proto:88
optional float speed_mps
Definition chassis.proto:70
optional double timestamp_sec
Definition header.proto:9
optional apollo::localization::Pose pose
optional apollo::canbus::Chassis::GearPosition gear
optional apollo::localization::Pose pose
optional apollo::common::Header header
optional apollo::common::Point3D euler_angles
Definition pose.proto:64
optional apollo::common::Point3D linear_acceleration_vrf
Definition pose.proto:52
optional apollo::common::Point3D linear_acceleration
Definition pose.proto:39
optional apollo::common::Quaternion orientation
Definition pose.proto:31
optional apollo::common::Point3D angular_velocity_vrf
Definition pose.proto:56
optional double heading
Definition pose.proto:48
optional apollo::common::PointENU position
Definition pose.proto:26
optional apollo::common::Point3D angular_velocity
Definition pose.proto:43