SMACC2
cp_square_shape_bondary.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#pragma once
21
22#include <math.h>
23
24#include <smacc2/smacc.hpp>
26
28{
29namespace cl_nav2z
30{
31using namespace std::chrono_literals;
32
33// computes the distance to move forward inside a square region
34// if the robot is located in the center of it
36{
37public:
40
41 CpSquareShapeBoundary(float squareLenghtMeters)
42 : squareLenghtMeters_(squareLenghtMeters)
43 {
44
45 }
46
47 void onInitialize() override
48 {
50 }
51
53 {
54 float currentAngle = robotPose_->getYaw();
55
56 // warp 0 to 2pi
57 while(currentAngle < 0)
58 {
59 currentAngle+=2*M_PI;
60 }
61
62 float side = squareLenghtMeters_/2.0;
63 // float maxraysize = sqrt(2*side*side);
64 // float radius = maxraysize;
65
66 float fwdist = -1;
67 float x,y;
68
69 if((currentAngle > 0 && currentAngle < M_PI/4 ) // rightside
70 || (currentAngle > 7*M_PI/4 && currentAngle <= 2*M_PI) // rightside
71 || (currentAngle > 3*M_PI/4 && currentAngle <= 5*M_PI/4) // leftside
72 )
73 {
74 float m = tan(currentAngle);
75 x = side;
76 y = m*x;
77
78 fwdist = sqrt(x*x + y*y);
79 }
80 else if((currentAngle > M_PI/4 && currentAngle <= 3*M_PI/4) // top side
81 || (currentAngle > 5*M_PI/4 && currentAngle <= 7*M_PI/4)) // bottom side
82 {
83 float cotan = cos(currentAngle)/sin(currentAngle);
84 y = side;
85 x = cotan*y;
86
87 fwdist = sqrt(x*x + y*y);
88 }
89 else
90 {
91 RCLCPP_FATAL_STREAM(getLogger(), "[" << getName() << " ] not implemented case");
92 }
93
94 RCLCPP_INFO_STREAM(getLogger(), "[" << getName() << " ] distance for yaw angle: " << currentAngle << " and square size: "<< squareLenghtMeters_ << " -- dist: " << fwdist <<"(x: "<< x <<" y:" << y <<")");
95
96 return fwdist;
97 }
98
99};
100} // namespace cl_lidar
101} // namespace sm_dance_bot_strikes_back
float getYaw()
Definition: cp_pose.cpp:118
void requiresComponent(TComponent *&requiredComponentStorage)
rclcpp::Logger getLogger()
virtual std::string getName() const