Apollo 10.0
自动驾驶开放平台
odometry_parser.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2023 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#pragma once
18
19#include <algorithm>
20#include <atomic>
21#include <cstring>
22#include <memory>
23#include <string>
24
26
27#include "cyber/proto/simple.pb.h"
28#include "cyber/cyber.h"
30
31#define ACCEPT_USE_OF_DEPRECATED_PROJ_API_H
32#include <proj_api.h> // NOLINT
33
34#include <Eigen/Dense> // NOLINT
35
36#if __has_include("sensor_msgs/msg/nav_sat_fix.hpp")
37#include "sensor_msgs/msg/nav_sat_fix.hpp"
38#define ROS_NAVFOUND_FOUND
39#endif
40
41#if __has_include("sensor_msgs/msg/imu.hpp")
42#include "sensor_msgs/msg/imu.hpp"
43#define ROS_IMU_FOUND
44#endif
45
46#if __has_include("nav_msgs/msg/odometry.hpp")
47#include "nav_msgs/msg/odometry.hpp"
48#define ROS_ODOMETRY_FOUND
49#endif
50
51#if defined(ROS_NAVFOUND_FOUND) && defined(ROS_IMU_FOUND) && \
52 defined(ROS_ODOMETRY_FOUND)
53#define ENABLE_ROS_MSG
54#endif
55
56#ifdef ENABLE_ROS_MSG
57using RosNavMsg = sensor_msgs::msg::NavSatFix;
58using RosImuMsg = sensor_msgs::msg::Imu;
59using RosWrapMsg = std_msgs::msg::Header;
60using RosOdomMsg = nav_msgs::msg::Odometry;
61#else
62// fake wrap
66#endif
67
69
70using RosWrapMsgPtr = std::shared_ptr<RosWrapMsg>;
71using OutputWrapMsgPtr = std::shared_ptr<OutputWrapMsg>;
72
73namespace apollo {
74namespace cyber {
75
78 InputTypes<RosWrapMsgPtr>, OutputTypes<OutputWrapMsgPtr>> {
79 public:
81#ifdef ENABLE_ROS_MSG
82 imu_sub_ = ros_node_->create_subscription<RosImuMsg>(
84 std::bind(&OdometryParser::imuCallback, this, std::placeholders::_1));
85 gps_sub_ = ros_node_->create_subscription<RosNavMsg>(
87 std::bind(&OdometryParser::gpsCallback, this, std::placeholders::_1));
88 odom_pub_ = ros_node_->create_publisher<RosOdomMsg>(
90 timer_ = ros_node_->create_wall_timer(
91 std::chrono::milliseconds(100),
92 std::bind(&OdometryParser::publishOdometry, this));
93#endif
94
95 wgs84pj_source_ = pj_init_plus("+proj=latlong +ellps=WGS84");
96 X_ = Eigen::VectorXd::Zero(9);
97 P_ = Eigen::MatrixXd::Identity(9, 9);
98 F_ = Eigen::MatrixXd::Identity(9, 9);
99 H_ = Eigen::MatrixXd::Identity(9, 9);
100 R_ = Eigen::MatrixXd::Identity(9, 9) * 0.1;
101 Q_ = Eigen::MatrixXd::Identity(9, 9) * 0.01;
102 last_time_ = apollo::cyber::Time::Now().ToSecond();
103 }
105
106 void imuCallback(std::shared_ptr<RosImuMsg> msg);
107
108 void gpsCallback(std::shared_ptr<RosNavMsg> msg);
109
110 void publishOdometry();
111
121
122 private:
123 projPJ wgs84pj_source_ = NULL;
124 projPJ utm_target_ = NULL;
125#ifdef ENABLE_ROS_MSG
126 ::rclcpp::SubscriptionBase::SharedPtr imu_sub_;
127 ::rclcpp::SubscriptionBase::SharedPtr gps_sub_;
128 ::rclcpp::Publisher<RosOdomMsg>::SharedPtr odom_pub_;
129 ::rclcpp::TimerBase::SharedPtr timer_;
130#endif
131
132 std::atomic<bool> imu_received_ = false;
133 std::atomic<bool> gps_received_ = false;
134
135 double last_time_;
136
137 double quaternion_[4];
138
139 // Kalman filter
140 Eigen::VectorXd X_; // State vector: [x, y, z, vx, vy, vz, roll, pitch, yaw]
141 Eigen::MatrixXd P_; // Covariance matrix
142 Eigen::MatrixXd F_; // State transition matrix
143 Eigen::MatrixXd H_; // Measurement matrix
144 Eigen::MatrixXd R_; // Measurement noise covariance matrix
145 Eigen::MatrixXd Q_; // Process noise covariance matrix
146};
147
150
151} // namespace cyber
152} // namespace apollo
void gpsCallback(std::shared_ptr< RosNavMsg > msg)
virtual bool ConvertMsg(InputTypes< RosWrapMsgPtr > &, OutputTypes< OutputWrapMsgPtr > &)
convert the message between ros and apollo
void imuCallback(std::shared_ptr< RosImuMsg > msg)
static Time Now()
get the current time.
Definition time.cc:57
double ToSecond() const
convert time to second.
Definition time.cc:77
#define CYBER_PLUGIN_MANAGER_REGISTER_PLUGIN(name, base)
class register implement
Definition arena_queue.h:37
std::shared_ptr< OutputWrapMsg > OutputWrapMsgPtr
std::shared_ptr< RosWrapMsg > RosWrapMsgPtr