SMACC2
cb_pure_spinning.hpp
Go to the documentation of this file.
1// Copyright 2021 RobosoftAI Inc.
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/*****************************************************************************************************************
16 *
17 * Authors: Pablo Inigo Blasco, Brett Aldrich
18 *
19 ******************************************************************************************************************/
20
21#pragma once
22
24#include <sensor_msgs/msg/laser_scan.hpp>
26#include <geometry_msgs/msg/twist.hpp>
28#include <angles/angles.h>
29
31{
32namespace cl_nav2zclient
33{
35{
36 private:
37 double targetYaw_;
39 double k_betta_;
41
42 rclcpp::Publisher<geometry_msgs::msg::Twist>::SharedPtr cmd_vel_pub_;
43
44 public:
46
47 CbPureSpinning(double targetYaw, double max_angular_yaw_speed= 0.5)
48 : targetYaw_(targetYaw),
49 k_betta_(1.0),
50 max_angular_yaw_speed_(max_angular_yaw_speed),
52 {
53
54 }
55
57 {
58 }
59
60 void onEntry() override
61 {
62 auto nh = this->getNode();
63 cmd_vel_pub_ = nh->create_publisher<geometry_msgs::msg::Twist>("/cmd_vel", 1);
64
65 cl_nav2z::Pose* pose;
66 this->requiresComponent(pose);
67
68 geometry_msgs::msg::Twist cmd_vel;
69 goalReached_ = false;
70
71 geometry_msgs::msg::PoseStamped currentPose = pose->toPoseStampedMsg();
72
73 rclcpp::Rate loop_rate(10);
74 double countAngle = 0;
75 auto prevyaw = tf2::getYaw(currentPose.pose.orientation);
76 while(rclcpp::ok() && !goalReached_)
77 {
78 tf2::Quaternion q;
79 currentPose = pose->toPoseStampedMsg();
80 tf2::fromMsg(currentPose.pose.orientation, q);
81 auto currentYaw = tf2::getYaw(currentPose.pose.orientation);
82 auto deltaAngle = angles::shortest_angular_distance(prevyaw, currentYaw);
83 countAngle += deltaAngle;
84
85 prevyaw = currentYaw;
86 double angular_error = targetYaw_ - countAngle ;
87
88 auto omega = angular_error * k_betta_;
89 cmd_vel.linear.x = 0;
90 cmd_vel.linear.y = 0;
91 cmd_vel.linear.z = 0;
92 cmd_vel.angular.z =
93 std::min(std::max(omega, -fabs(max_angular_yaw_speed_)), fabs(max_angular_yaw_speed_));
94
95 RCLCPP_INFO_STREAM(getLogger(), "["<<getName() << "] delta angle: " << deltaAngle);
96 RCLCPP_INFO_STREAM(getLogger(), "["<<getName() << "] cummulated angle: " << countAngle);
97 RCLCPP_INFO_STREAM(getLogger(), "["<<getName() << "] k_betta_: " << k_betta_);
98
99 RCLCPP_INFO_STREAM(
100 getLogger(), "["<<getName() << "] angular error: " << angular_error << "("
101 << yaw_goal_tolerance_rads_ << ")");
102 RCLCPP_INFO_STREAM(
103 getLogger(),
104 "["<<getName() << "] command angular speed: " << cmd_vel.angular.z);
105
106 if (fabs(angular_error) < yaw_goal_tolerance_rads_)
107 {
108 RCLCPP_INFO_STREAM(
109 getLogger(), "["<<getName() << "] GOAL REACHED. Sending stop command.");
110 goalReached_ = true;
111 cmd_vel.linear.x = 0;
112 cmd_vel.angular.z = 0;
113 break;
114 }
115
116 this->cmd_vel_pub_->publish(cmd_vel);
117
118 loop_rate.sleep();
119 }
120
121 this->postSuccessEvent();
122 }
123
124 void onExit() override
125 {
126 }
127 };
128}
129}
virtual rclcpp::Node::SharedPtr getNode()
void requiresComponent(SmaccComponentType *&storage)
rclcpp::Publisher< geometry_msgs::msg::Twist >::SharedPtr cmd_vel_pub_
CbPureSpinning(double targetYaw, double max_angular_yaw_speed=0.5)