Autoware.Auto
vehicle_interface_node.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 VEHICLE_INTERFACE__VEHICLE_INTERFACE_NODE_HPP_
19 #define VEHICLE_INTERFACE__VEHICLE_INTERFACE_NODE_HPP_
20 
23 #include <vehicle_interface/visibility_control.hpp>
24 
25 #include <mpark_variant_vendor/variant.hpp>
26 #include <rclcpp/rclcpp.hpp>
29 
30 #include <autoware_auto_msgs/msg/high_level_control_command.hpp>
31 #include <autoware_auto_msgs/msg/raw_control_command.hpp>
32 #include <autoware_auto_msgs/msg/vehicle_control_command.hpp>
33 #include <autoware_auto_msgs/msg/vehicle_odometry.hpp>
34 #include <autoware_auto_msgs/msg/vehicle_state_command.hpp>
35 #include <autoware_auto_msgs/msg/vehicle_state_report.hpp>
36 #include <autoware_auto_msgs/srv/autonomy_mode_change.hpp>
37 
38 #include <experimental/optional>
39 #include <chrono>
40 #include <exception>
41 #include <memory>
42 #include <string>
43 
44 namespace autoware
45 {
46 namespace drivers
47 {
48 namespace vehicle_interface
49 {
50 using Real = decltype(BasicControlCommand::long_accel_mps2);
51 
54 {
55  std::string topic;
56 }; // struct TopicNumMatches
57 
60 {
61  std::string type;
63 }; // struct FilterConfig
64 
67 class VEHICLE_INTERFACE_PUBLIC VehicleInterfaceNode : public ::rclcpp::Node
68 {
69 public:
74  const std::string & node_name,
75  const rclcpp::NodeOptions & options);
76 
77 protected:
78  using ControllerBasePtr =
79  std::unique_ptr<common::reference_tracking_controller::ReferenceTrackerBase<Real>>;
80  using FilterBasePtr = std::unique_ptr<common::signal_filters::FilterBase<Real>>;
81  using ModeChangeRequest = autoware_auto_msgs::srv::AutonomyModeChange_Request;
82  using ModeChangeResponse = autoware_auto_msgs::srv::AutonomyModeChange_Response;
83 
85  {
90  };
92  void set_filter(VehicleFilter && filter) noexcept;
94  void set_reference_tracker(ControllerBasePtr && controller) noexcept;
96  void set_interface(std::unique_ptr<PlatformInterface> && interface) noexcept;
98  rclcpp::Logger logger() const noexcept;
100  const SafetyStateMachine get_state_machine() const noexcept;
101 
105  virtual void on_control_send_failure();
109  virtual void on_state_send_failure();
112  virtual void on_mode_change_failure();
116  virtual void on_read_timeout();
118  virtual void on_error(std::exception_ptr eptr);
119 
120 private:
121  // Helper function called in constructors
122  VEHICLE_INTERFACE_LOCAL void init(
123  const TopicNumMatches & control_command,
124  const TopicNumMatches & state_command,
125  const TopicNumMatches & odometry,
126  const TopicNumMatches & state_report,
127  const std::experimental::optional<StateMachineConfig> & state_machine_config,
128  const FilterConfig & longitudinal_filter,
129  const FilterConfig & curvature_filter,
130  const FilterConfig & front_steer_filter,
131  const FilterConfig & rear_steer_filter,
132  const std::chrono::nanoseconds & cycle_time);
133 
134  // Run just before main loop, ensure that all invariants (possibly from child class) are enforced
135  VEHICLE_INTERFACE_LOCAL void check_invariants();
136 
137  // Send state command
138  VEHICLE_INTERFACE_LOCAL void send_state_command(const MaybeStateCommand & maybe_command);
139  // Read data from vehicle platform for time budget, publish data
140  VEHICLE_INTERFACE_LOCAL void read_and_publish();
141  // Core loop for different input commands. Specialized differently for each topic type
142  template<typename T>
143  VEHICLE_INTERFACE_LOCAL void on_command_message(const T & msg);
144  // Callback for requests to change autonomoy mode
145  VEHICLE_INTERFACE_LOCAL void on_mode_change_request(
146  ModeChangeRequest::SharedPtr request, ModeChangeResponse::SharedPtr response);
148  VEHICLE_INTERFACE_LOCAL void state_machine_report();
149 
150  rclcpp::TimerBase::SharedPtr m_read_timer{nullptr};
151  rclcpp::Publisher<autoware_auto_msgs::msg::VehicleOdometry>::SharedPtr m_odom_pub{nullptr};
152  rclcpp::Publisher<autoware_auto_msgs::msg::VehicleStateReport>::SharedPtr m_state_pub{nullptr};
153  rclcpp::Subscription<autoware_auto_msgs::msg::VehicleStateCommand>::SharedPtr m_state_sub{};
154  rclcpp::Service<autoware_auto_msgs::srv::AutonomyModeChange>::SharedPtr m_mode_service{nullptr};
155 
156  using BasicSub = rclcpp::Subscription<BasicControlCommand>::SharedPtr;
157  using RawSub = rclcpp::Subscription<autoware_auto_msgs::msg::RawControlCommand>::SharedPtr;
158  using HighLevelSub =
159  rclcpp::Subscription<autoware_auto_msgs::msg::HighLevelControlCommand>::SharedPtr;
160 
161  mpark::variant<RawSub, BasicSub, HighLevelSub> m_command_sub{};
162 
163  std::unique_ptr<PlatformInterface> m_interface{nullptr};
164  VehicleFilter m_filter{nullptr, nullptr, nullptr, nullptr};
165  ControllerBasePtr m_controller{nullptr};
166  std::unique_ptr<SafetyStateMachine> m_state_machine{nullptr};
167  std::chrono::system_clock::time_point m_last_command_stamp{};
168  std::chrono::nanoseconds m_cycle_time{};
169  MaybeStateCommand m_last_state_command{};
170 }; // class VehicleInterfaceNode
171 
172 } // namespace vehicle_interface
173 } // namespace drivers
174 } // namespace autoware
175 
176 #endif // VEHICLE_INTERFACE__VEHICLE_INTERFACE_NODE_HPP_
T
Definition: catr_diff.py:22
This class defines an interface for simple reference tracking controllers.
#define VEHICLE_INTERFACE_PUBLIC
Definition: drivers/vehicle_interface/include/vehicle_interface/visibility_control.hpp:44
Interface definition for signal processing filters.
std::string topic
Definition: vehicle_interface_node.hpp:55
std::unique_ptr< common::reference_tracking_controller::ReferenceTrackerBase< Real > > ControllerBasePtr
Definition: vehicle_interface_node.hpp:79
Convenience struct to construct filters.
Definition: vehicle_interface_node.hpp:59
Base class for vehicle "translator".
autoware_auto_msgs::srv::AutonomyModeChange_Request ModeChangeRequest
Definition: vehicle_interface_node.hpp:81
Definition: vehicle_interface_node.hpp:67
FilterBasePtr longitudinal
Definition: vehicle_interface_node.hpp:86
Configuration class for SafetyStateMachine.
Definition: safety_state_machine.hpp:115
autoware_auto_msgs::srv::AutonomyModeChange_Response ModeChangeResponse
Definition: vehicle_interface_node.hpp:82
std::string type
Definition: vehicle_interface_node.hpp:61
FilterBasePtr curvature
Definition: vehicle_interface_node.hpp:87
std::unique_ptr< common::signal_filters::FilterBase< Real > > FilterBasePtr
Definition: vehicle_interface_node.hpp:80
FilterBasePtr rear_steer
Definition: vehicle_interface_node.hpp:89
decltype(BasicControlCommand::long_accel_mps2) Real
Definition: vehicle_interface_node.hpp:50
#define VEHICLE_INTERFACE_LOCAL
Definition: drivers/vehicle_interface/include/vehicle_interface/visibility_control.hpp:45
Convenience struct for construction.
Definition: vehicle_interface_node.hpp:53
std::experimental::optional< autoware_auto_msgs::msg::VehicleStateCommand > MaybeStateCommand
Definition: safety_state_machine.hpp:41
Input validation state machine.
Definition: safety_state_machine.hpp:173
Real cutoff_frequency
Definition: vehicle_interface_node.hpp:62
FilterBasePtr front_steer
Definition: vehicle_interface_node.hpp:88
This file defines the lanelet2_map_provider_node class.
Definition: quick_sort.hpp:24