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
64 float fwdist = -1;
65 float x,y;
66
67 if((currentAngle > 0 && currentAngle < M_PI/4 ) // rightside
68 || (currentAngle > 7*M_PI/4 && currentAngle <= 2*M_PI) // rightside
69 || (currentAngle > 3*M_PI/4 && currentAngle <= 5*M_PI/4) // leftside
70 )
71 {
72 float m = tan(currentAngle);
73 x = side;
74 y = m*x;
75
76 fwdist = sqrt(x*x + y*y);
77 }
78 else if((currentAngle > M_PI/4 && currentAngle <= 3*M_PI/4) // top side
79 || (currentAngle > 5*M_PI/4 && currentAngle <= 7*M_PI/4)) // bottom side
80 {
81 float cotan = cos(currentAngle)/sin(currentAngle);
82 y = side;
83 x = cotan*y;
84
85 fwdist = sqrt(x*x + y*y);
86 }
87 else
88 {
89 RCLCPP_FATAL_STREAM(getLogger(), "[" << getName() << " ] not implemented case");
90 }
91
92 RCLCPP_INFO_STREAM(getLogger(), "[" << getName() << " ] distance for yaw angle: " << currentAngle << " and square size: "<< squareLenghtMeters_ << " -- dist: " << fwdist <<"(x: "<< x <<" y:" << y <<")");
93
94 return fwdist;
95 }
96
97};
98} // namespace cl_lidar
99} // 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