Autoware.Auto
lgsvl_interface.hpp
Go to the documentation of this file.
1 // Copyright 2020 the Autoware Foundation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // Co-developed by Tier IV, Inc. and Apex.AI, Inc.
18 #ifndef LGSVL_INTERFACE__LGSVL_INTERFACE_HPP_
19 #define LGSVL_INTERFACE__LGSVL_INTERFACE_HPP_
20 
22 
23 #include <autoware_auto_msgs/msg/raw_control_command.hpp>
24 #include <autoware_auto_msgs/msg/vehicle_kinematic_state.hpp>
25 #include <autoware_auto_msgs/msg/vehicle_state_command.hpp>
26 #include <autoware_auto_msgs/msg/vehicle_state_report.hpp>
27 #include <autoware_auto_msgs/srv/autonomy_mode_change.hpp>
28 
29 #include <lgsvl_msgs/msg/vehicle_odometry.hpp>
30 #include <lgsvl_msgs/msg/can_bus_data.hpp>
31 #include <lgsvl_msgs/msg/vehicle_control_data.hpp>
32 #include <lgsvl_msgs/msg/vehicle_state_data.hpp>
33 
34 #include <geometry_msgs/msg/pose_with_covariance_stamped.hpp>
35 #include <nav_msgs/msg/odometry.hpp>
36 #include <tf2_msgs/msg/tf_message.hpp>
37 
39 #include <rclcpp/rclcpp.hpp>
41 
42 #include <tf2_ros/transform_listener.h>
43 #include <tf2_ros/buffer.h>
44 
45 #include <chrono>
46 #include <memory>
47 #include <string>
48 #include <unordered_map>
49 #include <utility>
50 
51 namespace lgsvl_interface
52 {
53 
55 
56 // initialise default covariance for each measurement
57 // if simulator does not provide estimate of a state variable
58 // variance should be set high
59 constexpr static double COV_X_VAR = 0.1; // ros covariance array is float64 = double
60 constexpr static double COV_Y_VAR = 0.1;
61 constexpr static double COV_Z_VAR = 0.1;
62 constexpr static double COV_RX_VAR = 1000.0;
63 constexpr static double COV_RY_VAR = 1000.0;
64 constexpr static double COV_RZ_VAR = 1000.0;
65 
66 // Covariance array index values
67 constexpr static int32_t COV_X = 0;
68 constexpr static int32_t COV_Y = 7;
69 constexpr static int32_t COV_Z = 14;
70 constexpr static int32_t COV_RX = 21;
71 constexpr static int32_t COV_RY = 28;
72 constexpr static int32_t COV_RZ = 35;
73 
74 constexpr bool PUBLISH = true;
75 constexpr bool NO_PUBLISH = false;
76 
77 // in lgsvl 0 is drive and 1 is reverse https://github.com/lgsvl/simulator/blob/cb937deb8e633573f6c0cc76c9f451398b8b9eff/Assets/Scripts/Sensors/VehicleStateSensor.cs#L70
78 using VSC = autoware_auto_msgs::msg::VehicleStateCommand;
79 using VSD = lgsvl_msgs::msg::VehicleStateData;
80 using WIPER_TYPE = decltype(VSC::wiper);
81 using GEAR_TYPE = decltype(VSC::gear);
82 using MODE_TYPE = decltype(VSC::mode);
83 
89 {
90 public:
92  rclcpp::Node & node,
93  const std::string & sim_cmd_topic,
94  const std::string & sim_state_cmd_topic,
95  const std::string & sim_state_report_topic,
96  const std::string & sim_nav_odom_topic,
97  const std::string & sim_veh_odom_topic,
98  const std::string & kinematic_state_topic,
99  const std::string & sim_odom_child_frame,
100  Table1D && throttle_table,
101  Table1D && brake_table,
102  Table1D && steer_table,
103  bool publish_tf = NO_PUBLISH,
104  bool publish_pose = PUBLISH);
105 
106  ~LgsvlInterface() noexcept override = default;
109  bool update(std::chrono::nanoseconds timeout) override;
112  bool send_state_command(const autoware_auto_msgs::msg::VehicleStateCommand & msg) override;
114  bool send_control_command(const autoware_auto_msgs::msg::VehicleControlCommand & msg) override;
117  bool send_control_command(const autoware_auto_msgs::msg::RawControlCommand & msg) override;
119  bool handle_mode_change_request(
120  autoware_auto_msgs::srv::AutonomyModeChange_Request::SharedPtr request) override;
121 
122 private:
123  // Mappings from Autoware to LGSVL values
124  static const std::unordered_map<WIPER_TYPE, WIPER_TYPE> autoware_to_lgsvl_wiper;
125  static const std::unordered_map<GEAR_TYPE, GEAR_TYPE> autoware_to_lgsvl_gear;
126  static const std::unordered_map<MODE_TYPE, MODE_TYPE> autoware_to_lgsvl_mode;
127 
128  // Convert odometry into vehicle kinematic state and pose
129  void on_odometry(const nav_msgs::msg::Odometry & msg);
130 
131  // store state_report with gear value correction
132  void on_state_report(const autoware_auto_msgs::msg::VehicleStateReport & msg);
133 
134  rclcpp::Publisher<lgsvl_msgs::msg::VehicleControlData>::SharedPtr m_cmd_pub{};
135  rclcpp::Publisher<lgsvl_msgs::msg::VehicleStateData>::SharedPtr m_state_pub{};
136  rclcpp::Publisher<autoware_auto_msgs::msg::VehicleKinematicState>::SharedPtr
137  m_kinematic_state_pub{};
138  rclcpp::Publisher<tf2_msgs::msg::TFMessage>::SharedPtr m_tf_pub{};
139  rclcpp::Publisher<geometry_msgs::msg::PoseWithCovarianceStamped>::SharedPtr m_pose_pub{};
140  rclcpp::Subscription<nav_msgs::msg::Odometry>::SharedPtr m_nav_odom_sub{};
141  rclcpp::Subscription<lgsvl_msgs::msg::CanBusData>::SharedPtr m_state_sub{};
142  rclcpp::Subscription<lgsvl_msgs::msg::VehicleOdometry>::SharedPtr m_veh_odom_sub{};
143  rclcpp::TimerBase::SharedPtr m_nav_base_tf_timer{};
144 
145  Table1D m_throttle_table;
146  Table1D m_brake_table;
147  Table1D m_steer_table;
148 
149  // transforms
150  std::shared_ptr<tf2_ros::Buffer> m_tf_buffer;
151  std::shared_ptr<tf2_ros::TransformListener> m_tf_listener;
152 
153  bool m_nav_base_tf_set{false};
154  autoware_auto_msgs::msg::VehicleKinematicState m_nav_base_in_child_frame{};
155 
156  bool m_odom_set{false}; // TODO(c.ho) this should be optional<Vector3>
157  geometry_msgs::msg::Vector3 m_odom_zero{};
158 
159  rclcpp::Logger m_logger;
160 }; // class LgsvlInterface
161 
162 } // namespace lgsvl_interface
163 
164 #endif // LGSVL_INTERFACE__LGSVL_INTERFACE_HPP_
static constexpr double COV_Y_VAR
Definition: lgsvl_interface.hpp:60
static constexpr int32_t COV_Z
Definition: lgsvl_interface.hpp:69
autoware_auto_msgs::msg::VehicleKinematicState VehicleKinematicState
Definition: trajectory_spoofer.hpp:44
static constexpr double COV_Z_VAR
Definition: lgsvl_interface.hpp:61
Definition: lgsvl_interface.hpp:51
Base class for vehicle "translator".
#define LGSVL_INTERFACE_PUBLIC
Definition: drivers/lgsvl_interface/include/lgsvl_interface/visibility_control.hpp:46
autoware_auto_msgs::msg::VehicleStateCommand VSC
Definition: lgsvl_interface.hpp:78
static constexpr int32_t COV_RY
Definition: lgsvl_interface.hpp:71
Definition: platform_interface.hpp:52
static constexpr int32_t COV_RZ
Definition: lgsvl_interface.hpp:72
constexpr bool NO_PUBLISH
Definition: lgsvl_interface.hpp:75
static constexpr int32_t COV_X
Definition: lgsvl_interface.hpp:67
Definition: lgsvl_interface.hpp:87
autoware_auto_msgs::msg::VehicleControlCommand VehicleControlCommand
Definition: pure_pursuit.hpp:41
static constexpr double COV_RY_VAR
Definition: lgsvl_interface.hpp:63
decltype(VSC::mode) MODE_TYPE
Definition: lgsvl_interface.hpp:82
static constexpr int32_t COV_Y
Definition: lgsvl_interface.hpp:68
Visibility contorl.
static constexpr int32_t COV_RX
Definition: lgsvl_interface.hpp:70
decltype(VSC::wiper) WIPER_TYPE
Definition: lgsvl_interface.hpp:80
constexpr bool PUBLISH
Definition: lgsvl_interface.hpp:74
static constexpr double COV_RZ_VAR
Definition: lgsvl_interface.hpp:64
static constexpr double COV_X_VAR
Definition: lgsvl_interface.hpp:59
This file contains a 1D linear lookup table implementation.
decltype(VSC::gear) GEAR_TYPE
Definition: lgsvl_interface.hpp:81
static constexpr double COV_RX_VAR
Definition: lgsvl_interface.hpp:62
lgsvl_msgs::msg::VehicleStateData VSD
Definition: lgsvl_interface.hpp:79
autoware_auto_msgs::msg::VehicleOdometry Odometry
Definition: safety_state_machine.hpp:43