Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
dummy_controller.cpp
1 // Copyright (c) 2018 Intel Corporation
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 #include <chrono>
16 #include <ctime>
17 #include <thread>
18 #include <memory>
19 #include <utility>
20 
21 #include "dummy_controller.hpp"
22 
23 using namespace std::chrono_literals;
24 
25 namespace nav2_system_tests
26 {
27 
28 DummyController::DummyController()
29 : Node("DummyController")
30 {
31  RCLCPP_INFO(get_logger(), "Initializing DummyController...");
32 
33  auto temp_node = std::shared_ptr<rclcpp::Node>(this, [](auto) {});
34 
35  vel_pub_ =
36  this->create_publisher<geometry_msgs::msg::Twist>("cmd_vel", 1);
37 
38  task_server_ = std::make_unique<nav2_behavior_tree::FollowPathTaskServer>(temp_node, false),
39  task_server_->setExecuteCallback(
40  std::bind(&DummyController::followPath, this, std::placeholders::_1));
41 
42  // Start listening for incoming ComputePathToPose action server requests
43  task_server_->start();
44 
45  RCLCPP_INFO(get_logger(), "Initialized DummyController");
46 }
47 
48 DummyController::~DummyController()
49 {
50  RCLCPP_INFO(get_logger(), "Shutting down DummyController");
51 }
52 
53 void
54 DummyController::followPath(const nav2_behavior_tree::FollowPathCommand::SharedPtr /*command*/)
55 {
56  RCLCPP_INFO(get_logger(), "Starting controller ");
57 
58  auto start_time = std::chrono::system_clock::now();
59  auto time_since_msg = std::chrono::system_clock::now();
60 
61  while (true) {
62  // Dummy controller computation time
63  std::this_thread::sleep_for(50ms);
64 
65  if (task_server_->cancelRequested()) {
66  RCLCPP_INFO(get_logger(), "Task cancelled");
67  setZeroVelocity();
68  task_server_->setCanceled();
69  return;
70  }
71 
72  // Log a message every second
73  auto current_time = std::chrono::system_clock::now();
74  if (current_time - time_since_msg >= 1s) {
75  RCLCPP_INFO(get_logger(), "Following path");
76  time_since_msg = std::chrono::system_clock::now();
77  }
78 
79  // Output control command
80  auto cmd_vel = std::make_unique<geometry_msgs::msg::Twist>();
81  cmd_vel->linear.x = 0.1;
82  vel_pub_->publish(std::move(cmd_vel));
83 
84  if (current_time - start_time >= 30s) {
85  RCLCPP_INFO(get_logger(), "Reached end point");
86  setZeroVelocity();
87  break;
88  }
89  }
90 
91  nav2_behavior_tree::FollowPathResult result;
92  task_server_->setResult(result);
93 }
94 
95 void DummyController::setZeroVelocity()
96 {
97  auto cmd_vel = std::make_unique<geometry_msgs::msg::Twist>();
98  cmd_vel->linear.x = 0.0;
99  cmd_vel->linear.y = 0.0;
100  cmd_vel->angular.z = 0.0;
101  vel_pub_->publish(std::move(cmd_vel));
102 }
103 
104 } // namespace nav2_system_tests