@@ -57,6 +57,31 @@ PoseInitializer::PoseInitializer() : Node("pose_initializer")
57
57
logger_configure_ = std::make_unique<tier4_autoware_utils::LoggerLevelConfigure>(this );
58
58
59
59
change_state (State::Message::UNINITIALIZED);
60
+
61
+ if (declare_parameter<bool >(" user_defined_initial_pose.enable" )) {
62
+ const auto initial_pose_array =
63
+ declare_parameter<std::vector<double >>(" user_defined_initial_pose.pose" );
64
+ if (initial_pose_array.size () != 7 ) {
65
+ throw std::invalid_argument (
66
+ " Could not set user defined initial pose. The size of initial_pose is " +
67
+ std::to_string (initial_pose_array.size ()) + " . It must be 7." );
68
+ } else if (
69
+ std::abs (initial_pose_array[3 ]) < 1e-6 && std::abs (initial_pose_array[4 ]) < 1e-6 &&
70
+ std::abs (initial_pose_array[5 ]) < 1e-6 && std::abs (initial_pose_array[6 ]) < 1e-6 ) {
71
+ throw std::invalid_argument (" Input quaternion is invalid. All elements are close to zero." );
72
+ } else {
73
+ geometry_msgs::msg::Pose initial_pose;
74
+ initial_pose.position .x = initial_pose_array[0 ];
75
+ initial_pose.position .y = initial_pose_array[1 ];
76
+ initial_pose.position .z = initial_pose_array[2 ];
77
+ initial_pose.orientation .x = initial_pose_array[3 ];
78
+ initial_pose.orientation .y = initial_pose_array[4 ];
79
+ initial_pose.orientation .z = initial_pose_array[5 ];
80
+ initial_pose.orientation .w = initial_pose_array[6 ];
81
+
82
+ set_user_defined_initial_pose (initial_pose);
83
+ }
84
+ }
60
85
}
61
86
62
87
PoseInitializer::~PoseInitializer ()
@@ -71,6 +96,47 @@ void PoseInitializer::change_state(State::Message::_state_type state)
71
96
pub_state_->publish (state_);
72
97
}
73
98
99
+ // To execute in the constructor, you need to call ros spin.
100
+ // Conversely, ros spin should not be called elsewhere
101
+ void PoseInitializer::change_node_trigger (bool flag, bool need_spin)
102
+ {
103
+ try {
104
+ if (ekf_localization_trigger_) {
105
+ ekf_localization_trigger_->wait_for_service ();
106
+ ekf_localization_trigger_->send_request (flag, need_spin);
107
+ }
108
+ if (ndt_localization_trigger_) {
109
+ ndt_localization_trigger_->wait_for_service ();
110
+ ndt_localization_trigger_->send_request (flag, need_spin);
111
+ }
112
+ } catch (const ServiceException & error) {
113
+ throw ;
114
+ }
115
+ }
116
+
117
+ void PoseInitializer::set_user_defined_initial_pose (const geometry_msgs::msg::Pose initial_pose)
118
+ {
119
+ try {
120
+ change_state (State::Message::INITIALIZING);
121
+ change_node_trigger (false , true );
122
+
123
+ PoseWithCovarianceStamped pose;
124
+ pose.header .frame_id = " map" ;
125
+ pose.header .stamp = now ();
126
+ pose.pose .pose = initial_pose;
127
+ pose.pose .covariance = output_pose_covariance_;
128
+ pub_reset_->publish (pose);
129
+
130
+ change_node_trigger (true , true );
131
+ change_state (State::Message::INITIALIZED);
132
+
133
+ RCLCPP_INFO (get_logger (), " Set user defined initial pose" );
134
+ } catch (const ServiceException & error) {
135
+ change_state (State::Message::UNINITIALIZED);
136
+ RCLCPP_WARN (get_logger (), " Could not set user defined initial pose" );
137
+ }
138
+ }
139
+
74
140
void PoseInitializer::on_initialize (
75
141
const Initialize::Service::Request::SharedPtr req,
76
142
const Initialize::Service::Response::SharedPtr res)
@@ -82,12 +148,8 @@ void PoseInitializer::on_initialize(
82
148
}
83
149
try {
84
150
change_state (State::Message::INITIALIZING);
85
- if (ekf_localization_trigger_) {
86
- ekf_localization_trigger_->send_request (false );
87
- }
88
- if (ndt_localization_trigger_) {
89
- ndt_localization_trigger_->send_request (false );
90
- }
151
+ change_node_trigger (false , false );
152
+
91
153
auto pose = req->pose .empty () ? get_gnss_pose () : req->pose .front ();
92
154
if (ndt_) {
93
155
pose = ndt_->align_pose (pose);
@@ -98,12 +160,8 @@ void PoseInitializer::on_initialize(
98
160
}
99
161
pose.pose .covariance = output_pose_covariance_;
100
162
pub_reset_->publish (pose);
101
- if (ekf_localization_trigger_) {
102
- ekf_localization_trigger_->send_request (true );
103
- }
104
- if (ndt_localization_trigger_) {
105
- ndt_localization_trigger_->send_request (true );
106
- }
163
+
164
+ change_node_trigger (true , false );
107
165
res->status .success = true ;
108
166
change_state (State::Message::INITIALIZED);
109
167
} catch (const ServiceException & error) {
0 commit comments