Skip to content

Commit 79b9056

Browse files
committed
DetectedObjects msg type added
1 parent e059201 commit 79b9056

File tree

2 files changed

+69
-7
lines changed

2 files changed

+69
-7
lines changed

tools/reaction_analyzer/include/reaction_analyzer_node.hpp

+10-6
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ using autoware_adapi_v1_msgs::msg::OperationModeState;
4949
using autoware_adapi_v1_msgs::msg::RouteState;
5050
using autoware_adapi_v1_msgs::srv::ChangeOperationMode;
5151
using autoware_auto_control_msgs::msg::AckermannControlCommand;
52+
using autoware_auto_perception_msgs::msg::DetectedObject;
5253
using autoware_auto_perception_msgs::msg::DetectedObjects;
5354
using autoware_auto_perception_msgs::msg::PredictedObject;
5455
using autoware_auto_perception_msgs::msg::PredictedObjects;
@@ -62,8 +63,10 @@ using ControlCommandBuffer =
6263
using TrajectoryBuffer = std::optional<Trajectory>;
6364
using PointCloud2Buffer = std::optional<PointCloud2>;
6465
using PredictedObjectsBuffer = std::optional<PredictedObjects>;
65-
using BufferVariant =
66-
std::variant<ControlCommandBuffer, TrajectoryBuffer, PointCloud2Buffer, PredictedObjectsBuffer>;
66+
using DetectedObjectsBuffer = std::optional<DetectedObjects>;
67+
using BufferVariant = std::variant<
68+
ControlCommandBuffer, TrajectoryBuffer, PointCloud2Buffer, PredictedObjectsBuffer,
69+
DetectedObjectsBuffer>;
6770

6871
enum class MessageType {
6972
Unknown = 0,
@@ -160,6 +163,8 @@ class ReactionAnalyzerNode : public rclcpp::Node
160163

161164
bool searchPredictedObjectsNearEntity(const PredictedObjects & predicted_objects);
162165

166+
bool searchDetectedObjectsNearEntity(const DetectedObjects & detected_objects);
167+
163168
bool loadChainModules();
164169

165170
bool initObservers(const ChainModules & modules);
@@ -184,10 +189,6 @@ class ReactionAnalyzerNode : public rclcpp::Node
184189
void spawnObstacle(
185190
const geometry_msgs::msg::Point & ego_pose, std::optional<rclcpp::Time> & spawn_cmd_time);
186191

187-
// void searchForReactions(
188-
// const std::unordered_map<std::string, BufferVariant> & message_buffers,
189-
// const geometry_msgs::msg::Point & ego_pose);
190-
191192
void printResults(const std::unordered_map<std::string, BufferVariant> & message_buffers);
192193

193194
void onTimer();
@@ -214,6 +215,9 @@ class ReactionAnalyzerNode : public rclcpp::Node
214215
void predictedObjectsOutputCallback(
215216
const std::string & node_name, const PredictedObjects::ConstSharedPtr msg_ptr);
216217

218+
void detectedObjectsOutputCallback(
219+
const std::string & node_name, const DetectedObjects::ConstSharedPtr msg_ptr);
220+
217221
// Variables
218222
std::vector<rclcpp::SubscriptionBase::SharedPtr> subscribers_;
219223
std::unordered_map<std::string, BufferVariant> message_buffers_;

tools/reaction_analyzer/src/reaction_analyzer_node.cpp

+59-1
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,31 @@ void ReactionAnalyzerNode::predictedObjectsOutputCallback(
175175
mutex_.unlock();
176176
}
177177

178+
void ReactionAnalyzerNode::detectedObjectsOutputCallback(
179+
const std::string & node_name, const DetectedObjects::ConstSharedPtr msg_ptr)
180+
{
181+
mutex_.lock();
182+
auto variant = message_buffers_[node_name];
183+
// const auto is_spawned = spawn_cmd_time_;
184+
mutex_.unlock();
185+
186+
if (!std::holds_alternative<DetectedObjectsBuffer>(variant)) {
187+
// If the variant doesn't hold a Trajectory message yet, initialize it
188+
DetectedObjectsBuffer buffer(std::nullopt);
189+
variant = buffer;
190+
} else if (std::get<DetectedObjectsBuffer>(variant).has_value()) {
191+
// reacted
192+
return;
193+
}
194+
if (searchDetectedObjectsNearEntity(*msg_ptr)) {
195+
std::get<DetectedObjectsBuffer>(variant) = *msg_ptr;
196+
}
197+
// if (!is_spawned) return;
198+
mutex_.lock();
199+
message_buffers_[node_name] = variant;
200+
mutex_.unlock();
201+
}
202+
178203
void ReactionAnalyzerNode::operationModeCallback(const OperationModeState::ConstSharedPtr msg_ptr)
179204
{
180205
std::lock_guard<std::mutex> lock(mutex_);
@@ -320,7 +345,6 @@ void ReactionAnalyzerNode::onTimer()
320345
void ReactionAnalyzerNode::spawnObstacle(
321346
const geometry_msgs::msg::Point & ego_pose, std::optional<rclcpp::Time> & spawn_cmd_time)
322347
{
323-
324348
if (
325349
tier4_autoware_utils::calcDistance3d(ego_pose, entity_pose_.position) >=
326350
node_params_.spawn_distance_threshold) {
@@ -410,6 +434,13 @@ void ReactionAnalyzerNode::printResults(
410434
} else {
411435
all_reacted = false;
412436
}
437+
} else if (auto * detected_objects_message = std::get_if<DetectedObjectsBuffer>(&variant)) {
438+
if (detected_objects_message->has_value()) {
439+
reaction_times.emplace_back(
440+
key, rclcpp::Time(detected_objects_message->value().header.stamp));
441+
} else {
442+
all_reacted = false;
443+
}
413444
}
414445
}
415446

@@ -460,6 +491,8 @@ bool ReactionAnalyzerNode::loadChainModules()
460491
return MessageType::PointCloud2;
461492
} else if (input == "autoware_auto_perception_msgs::msg::PredictedObjects") {
462493
return MessageType::PredictedObjects;
494+
} else if (input == "autoware_auto_perception_msgs::msg::DetectedObjects") {
495+
return MessageType::DetectedObjects;
463496
} else {
464497
return MessageType::Unknown;
465498
}
@@ -625,6 +658,15 @@ bool ReactionAnalyzerNode::initObservers(const reaction_analyzer::ChainModules &
625658
subscribers_.push_back(subscriber);
626659
break;
627660
}
661+
case MessageType::DetectedObjects: {
662+
auto callback = [this, module](const DetectedObjects::ConstSharedPtr msg) {
663+
this->detectedObjectsOutputCallback(module.node_name, msg);
664+
};
665+
auto subscriber = this->create_subscription<DetectedObjects>(
666+
module.topic_address, rclcpp::SensorDataQoS(), callback, createSubscriptionOptions());
667+
subscribers_.push_back(subscriber);
668+
break;
669+
}
628670
case MessageType::Unknown:
629671
RCLCPP_WARN(
630672
this->get_logger(), "Unknown message type for module name: %s, skipping..",
@@ -849,6 +891,22 @@ bool ReactionAnalyzerNode::searchPredictedObjectsNearEntity(
849891
return false;
850892
}
851893

894+
bool ReactionAnalyzerNode::searchDetectedObjectsNearEntity(const DetectedObjects & detected_objects)
895+
{
896+
bool isAnyObjectWithinRadius = std::any_of(
897+
detected_objects.objects.begin(), detected_objects.objects.end(),
898+
[this](const DetectedObject & object) {
899+
return tier4_autoware_utils::calcDistance3d(
900+
entity_pose_.position, object.kinematics.pose_with_covariance.pose.position) <=
901+
entity_search_radius_;
902+
});
903+
904+
if (isAnyObjectWithinRadius) {
905+
return true;
906+
}
907+
return false;
908+
}
909+
852910
} // namespace reaction_analyzer
853911

854912
#include <rclcpp_components/register_node_macro.hpp>

0 commit comments

Comments
 (0)