Formula Student Autonomous Systems
The code for the main driverless system
Loading...
Searching...
No Matches
ros_node.cpp
Go to the documentation of this file.
2
3using namespace common_lib::structures;
4
5ControlNode::ControlNode(const ControlParameters& params)
6 : Node("control"),
7 params_(params),
8 _execution_times_(std::make_shared<std::vector<double>>(5, 0.0)),
9 control_solver_(controller_map.at(params_.control_solver_)(params)),
10 execution_time_pub_(create_publisher<std_msgs::msg::Float64MultiArray>(
11 "/control/execution_time", 10)),
12 control_timer_(create_wall_timer(std::chrono::milliseconds(this->params_.command_time_interval_),
13 std::bind(&ControlNode::control_timer_callback, this))),
14 path_point_array_sub_(create_subscription<custom_interfaces::msg::PathPointArray>(
15 params.use_simulated_planning_ ? "/path_planning/mock_path" : "/path_planning/path",
16 rclcpp::QoS(10),
17 std::bind(&ControlNode::path_callback, this, std::placeholders::_1))) {
18 if (!params_.using_simulated_slam_) {
19 vehicle_pose_sub_ = this->create_subscription<custom_interfaces::msg::Pose>(
20 "/state_estimation/vehicle_pose", 10,
21 std::bind(&ControlNode::vehicle_pose_callback, this, std::placeholders::_1));
22 }
23 if (!params_.using_simulated_velocities_) {
24 velocity_sub_ = this->create_subscription<custom_interfaces::msg::Velocities>(
25 "/state_estimation/velocities", 10,
26 std::bind(&ControlNode::vehicle_state_callback, this, std::placeholders::_1));
27 }
28}
29
31 if (!go_signal_) return;
32
33 rclcpp::Time start = this->now();
34 common_lib::structures::ControlCommand command = this->control_solver_->get_control_command();
35 double execution_time = (this->now() - start).seconds() * 1000;
36
37 this->publish_command(command);
38
39 this->_execution_times_->at(0) = execution_time;
40 std_msgs::msg::Float64MultiArray execution_time_msg;
41 execution_time_msg.data = *this->_execution_times_;
42 this->execution_time_pub_->publish(execution_time_msg);
43 this->control_solver_->publish_solver_data(shared_from_this(), publisher_map_);
44}
45
46// This function is called when a new pose is received
47void ControlNode::vehicle_pose_callback(const custom_interfaces::msg::Pose& vehicle_pose_msg) {
48 this->control_solver_->vehicle_pose_callback(vehicle_pose_msg);
49}
50
51void ControlNode::path_callback(const custom_interfaces::msg::PathPointArray& path_msg) {
52 this->control_solver_->path_callback(path_msg);
53}
54
55void ControlNode::vehicle_state_callback(const custom_interfaces::msg::Velocities& vel_msg) {
56 this->control_solver_->vehicle_state_callback(vel_msg);
57}
Class responsible for the ROS2 communication of the control module.
Definition ros_node.hpp:26
std::shared_ptr< std::vector< double > > _execution_times_
Definition ros_node.hpp:51
std::shared_ptr< ControlSolver > control_solver_
Definition ros_node.hpp:57
bool go_signal_
Definition ros_node.hpp:28
void vehicle_state_callback(const custom_interfaces::msg::Velocities &msg)
Called when a new velocity is received.
Definition ros_node.cpp:55
virtual void publish_command(common_lib::structures::ControlCommand cmd)=0
Adapters override this function to publish control commands in their environment.
std::map< std::string, std::shared_ptr< rclcpp::PublisherBase > > publisher_map_
Definition ros_node.hpp:54
ControlParameters params_
Definition ros_node.hpp:29
void vehicle_pose_callback(const custom_interfaces::msg::Pose &msg)
Called when a new vehicle pose is received.
Definition ros_node.cpp:47
rclcpp::Publisher< std_msgs::msg::Float64MultiArray >::SharedPtr execution_time_pub_
Definition ros_node.hpp:60
void path_callback(const custom_interfaces::msg::PathPointArray &msg)
Called when a new path is received.
Definition ros_node.cpp:51
ControlNode(const ControlParameters &params)
Definition ros_node.cpp:5
rclcpp::Subscription< custom_interfaces::msg::Pose >::SharedPtr vehicle_pose_sub_
Definition ros_node.hpp:67
rclcpp::Subscription< custom_interfaces::msg::Velocities >::SharedPtr velocity_sub_
Definition ros_node.hpp:68
void control_timer_callback()
Function that publishes control commands on timer ticks.
Definition ros_node.cpp:30
const std::map< std::string, std::function< std::shared_ptr< ControlSolver >(const ControlParameters &)>, std::less<> > controller_map
Definition map.hpp:15
Hash function for cones.
Definition cone.hpp:36