Apollo  v5.5.0
Open source self driving car software
transform_wrapper.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 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 #pragma once
17 
18 #include <deque>
19 #include <memory>
20 #include <string>
21 
22 #include "Eigen/Core"
23 #include "Eigen/Dense"
24 
25 #include "modules/transform/buffer.h"
26 
27 namespace apollo {
28 namespace perception {
29 namespace onboard {
30 
31 using apollo::transform::Buffer;
32 
33 DECLARE_string(obs_sensor2novatel_tf2_frame_id);
34 DECLARE_string(obs_novatel2world_tf2_frame_id);
35 DECLARE_string(obs_novatel2world_tf2_child_frame_id);
36 DECLARE_double(obs_tf2_buff_size);
37 
39  double timestamp = 0.0; // in second
40  Eigen::Translation3d translation;
41  Eigen::Quaterniond rotation;
42 };
43 
45  public:
46  TransformCache() = default;
47  ~TransformCache() = default;
48 
49  void AddTransform(const StampedTransform& transform);
50  bool QueryTransform(double timestamp, StampedTransform* transform,
51  double max_duration = 0.0);
52 
53  inline void SetCacheDuration(double duration) { cache_duration_ = duration; }
54 
55  protected:
56  // in ascending order of time
57  std::deque<StampedTransform> transforms_;
58  double cache_duration_ = 1.0;
59 };
60 
62  public:
64  ~TransformWrapper() = default;
65 
66  void Init(const std::string& sensor2novatel_tf2_child_frame_id);
67  void Init(const std::string& sensor2novatel_tf2_frame_id,
68  const std::string& sensor2novatel_tf2_child_frame_id,
69  const std::string& novatel2world_tf2_frame_id,
70  const std::string& novatel2world_tf2_child_frame_id);
71 
72  // Attention: must initialize TransformWrapper first
73  bool GetSensor2worldTrans(double timestamp,
74  Eigen::Affine3d* sensor2world_trans,
75  Eigen::Affine3d* novatel2world_trans = nullptr);
76 
77  bool GetExtrinsics(Eigen::Affine3d* trans);
78 
79  // Attention: can be called without initlization
80  bool GetTrans(double timestamp, Eigen::Affine3d* trans,
81  const std::string& frame_id, const std::string& child_frame_id);
82 
83  bool GetExtrinsicsBySensorId(const std::string& from_sensor_id,
84  const std::string& to_sensor_id,
85  Eigen::Affine3d* trans);
86 
87  protected:
88  bool QueryTrans(double timestamp, StampedTransform* trans,
89  const std::string& frame_id,
90  const std::string& child_frame_id);
91 
92  private:
93  bool inited_ = false;
94 
95  Buffer* tf2_buffer_ = Buffer::Instance();
96 
97  std::string sensor2novatel_tf2_frame_id_;
98  std::string sensor2novatel_tf2_child_frame_id_;
99  std::string novatel2world_tf2_frame_id_;
100  std::string novatel2world_tf2_child_frame_id_;
101 
102  std::unique_ptr<Eigen::Affine3d> sensor2novatel_extrinsics_;
103 
104  TransformCache transform_cache_;
105 };
106 
107 } // namespace onboard
108 } // namespace perception
109 } // namespace apollo
Eigen::Quaterniond rotation
Definition: transform_wrapper.h:41
double timestamp
Definition: transform_wrapper.h:39
TransformWrapper()
Definition: transform_wrapper.h:63
Definition: blob.h:72
std::deque< StampedTransform > transforms_
Definition: transform_wrapper.h:57
Definition: transform_wrapper.h:61
Definition: transform_wrapper.h:44
Definition: transform_wrapper.h:38
DECLARE_double(obs_buffer_match_precision)
DECLARE_string(obs_screen_output_dir)
void SetCacheDuration(double duration)
Definition: transform_wrapper.h:53
Eigen::Translation3d translation
Definition: transform_wrapper.h:40