SMACC2
cl_autoware.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
24
25//#include <autoware_auto_msgs//>
26#include <geometry_msgs/msg/pose_stamped.hpp>
27#include <geometry_msgs/msg/pose_with_covariance_stamped.hpp>
29#include <rclcpp/rclcpp.hpp>
30#include <rclcpp/logging.hpp>
31
33{
34namespace clients
35{
36
37 using namespace std::chrono_literals;
38template <typename TSource, typename TObject>
39struct EvAutoLocalized : sc::event<EvAutoLocalized<TSource, TObject>>
40{
41 geometry_msgs::msg::PoseWithCovarianceStamped location;
42};
43
44template <typename TSource, typename TObject>
45struct EvGoalReached : sc::event<EvGoalReached<TSource, TObject>>
46{
47};
48
50{
51public:
53
55
56 std::optional<geometry_msgs::msg::PoseWithCovarianceStamped> lastPose_;
57 std::optional<geometry_msgs::msg::PoseStamped> lastGoal_;
58
59 double goalToleranceMeters_ = 3.5; //m
60
61 std::function<void(const geometry_msgs::msg::PoseWithCovarianceStamped &)> postEvAutoLocalized_;
62 std::function<void()> postEvGoalReached_;
63
64 template <typename TOrthogonal, typename TSourceObject>
66 {
67 this->postEvAutoLocalized_ = [this](const geometry_msgs::msg::PoseWithCovarianceStamped & msg)
68 {
70 ev->location = msg;
71 this->postEvent(ev);
72 };
73
74 this->postEvGoalReached_ = [this]()
75 {
76 this->postEvent<EvGoalReached<TSourceObject, TOrthogonal>>();
77 };
78 }
79
80 void onInitialize() override
81 {
82 /*
83 auto cppubLocation = client->createNamedComponent<
84 smacc2::components::CpTopicPublisher<geometry_msgs::msg::PoseStamped>>(
85 "initialPoseEstimation", "/localization/initialpose");
86
87 auto cpppubGoalPose = client->createNamedComponent<
88 smacc2::components::CpTopicPublisher<geometry_msgs::msg::PoseStamped>>(
89 "goalPose", "planning/goal_pose");
90 */
91
93 this->getComponent<smacc2::components::CpTopicPublisher<geometry_msgs::msg::PoseWithCovarianceStamped>>(
94 "initialPoseEstimation");
95
97 this->getComponent<smacc2::components::CpTopicPublisher<geometry_msgs::msg::PoseStamped>>(
98 "goalPose");
99
100 // auto subscriberNdtPose = client->createNamedComponent<
101 // smacc2::components::CpTopicSubscriber<geometry_msgs::msg::PoseWithCovarianceStamped>>(
102 // "ndtPose", "/localization/ndt_pose");
103
104 auto subscriberNdtPose = this->getComponent<
106 "ndtPose");
107
108 subscriberNdtPose->onMessageReceived(&ClAutoware::onNdtPoseReceived, this);
109 }
110
112 {
113 if (lastGoal_ && lastPose_)
114 {
115 auto & pos = this->lastPose_->pose.pose.position;
116 auto & goalpos = this->lastGoal_->pose.position;
117
118 double dx = pos.x - goalpos.x;
119 double dy = pos.y - goalpos.y;
120 double dz = pos.z - goalpos.z;
121
122 double dist = sqrt(dx * dx + dy * dy + dz * dz);
123
124 RCLCPP_INFO_STREAM_THROTTLE(getLogger(), *(getNode()->get_clock()),2000, "[" << getName() << "] goal linear error: " << dist);
125
126 if (dist < goalToleranceMeters_)
127 {
128 return true;
129 }
130 }
131
132 return false;
133 }
134
135 void onNdtPoseReceived(const geometry_msgs::msg::PoseWithCovarianceStamped & msg)
136 {
137 lastPose_ = msg;
138 this->postEvAutoLocalized_(msg);
139
140 if(checkGoalReached())
141 {
142 this->postEvGoalReached_();
143 }
144 }
145
146 void publishGoalPose(const geometry_msgs::msg::PoseStamped & msg)
147 {
148 lastGoal_ = msg;
149 this->cpppubGoalPose_->publish(msg);
150 }
151
152 void publishInitialPoseEstimation(const geometry_msgs::msg::PoseWithCovarianceStamped & msg)
153 {
154 this->cppubLocation_->publish(msg);
155 }
156};
157} // namespace clients
158} // namespace sm_autoware_avp
smacc2::components::CpTopicPublisher< geometry_msgs::msg::PoseWithCovarianceStamped > * cppubLocation_
Definition: cl_autoware.hpp:52
void publishGoalPose(const geometry_msgs::msg::PoseStamped &msg)
smacc2::components::CpTopicPublisher< geometry_msgs::msg::PoseStamped > * cpppubGoalPose_
Definition: cl_autoware.hpp:54
std::optional< geometry_msgs::msg::PoseStamped > lastGoal_
Definition: cl_autoware.hpp:57
std::function< void(const geometry_msgs::msg::PoseWithCovarianceStamped &)> postEvAutoLocalized_
Definition: cl_autoware.hpp:61
std::optional< geometry_msgs::msg::PoseWithCovarianceStamped > lastPose_
Definition: cl_autoware.hpp:56
std::function< void()> postEvGoalReached_
Definition: cl_autoware.hpp:62
void onNdtPoseReceived(const geometry_msgs::msg::PoseWithCovarianceStamped &msg)
void publishInitialPoseEstimation(const geometry_msgs::msg::PoseWithCovarianceStamped &msg)
rclcpp::Node::SharedPtr getNode()
Definition: client.cpp:60
virtual std::string getName() const
Definition: client.cpp:42
rclcpp::Logger getLogger()
TComponent * getComponent()
void publish(const MessageType &msg)
boost::signals2::connection onMessageReceived(void(T::*callback)(const MessageType &), T *object)
geometry_msgs::msg::PoseWithCovarianceStamped location
Definition: cl_autoware.hpp:41